prathe_devise_ldap_authenticatable 0.4.10 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,8 @@
1
+ .bundle
2
+ log
3
+ *.sqlite3
4
+ test/ldap/openldap-data/*
5
+ !test/ldap/openldap-data/run
6
+ test/ldap/openldap-data/run/slapd.*
7
+ test/rails_app/tmp
8
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem 'devise', '~> 2.0.0'
6
+ gem 'net-ldap', '~> 0.2.2'
7
+
8
+ platforms :mri_18 do
9
+ group :test do
10
+ gem 'ruby-debug', '>= 0.10.3'
11
+ end
12
+ end
@@ -0,0 +1,38 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ devise_ldap_authenticatable (0.5.1)
5
+ devise (~> 1.5.0)
6
+ net-ldap (~> 0.2.2)
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ bcrypt-ruby (3.0.1)
12
+ columnize (0.3.6)
13
+ devise (1.5.3)
14
+ bcrypt-ruby (~> 3.0)
15
+ orm_adapter (~> 0.0.3)
16
+ warden (~> 1.1)
17
+ linecache (0.46)
18
+ rbx-require-relative (> 0.0.4)
19
+ net-ldap (0.2.2)
20
+ orm_adapter (0.0.6)
21
+ rack (1.4.0)
22
+ rbx-require-relative (0.0.5)
23
+ ruby-debug (0.10.4)
24
+ columnize (>= 0.1)
25
+ ruby-debug-base (~> 0.10.4.0)
26
+ ruby-debug-base (0.10.4)
27
+ linecache (>= 0.3)
28
+ warden (1.1.0)
29
+ rack (>= 1.0)
30
+
31
+ PLATFORMS
32
+ ruby
33
+
34
+ DEPENDENCIES
35
+ devise (~> 1.5.0)
36
+ devise_ldap_authenticatable!
37
+ net-ldap (~> 0.2.2)
38
+ ruby-debug (>= 0.10.3)
data/Rakefile CHANGED
@@ -5,25 +5,6 @@ require 'rake/rdoctask'
5
5
  desc 'Default: run unit tests.'
6
6
  task :default => :test
7
7
 
8
- desc 'Test the devise_imapable plugin.'
9
- Rake::TestTask.new(:test) do |t|
10
- # t.libs << 'lib'
11
- # t.libs << 'test'
12
- # t.pattern = 'test/**/*_test.rb'
13
- # t.verbose = true
14
- puts <<-eof
15
-
16
- *** NOTICE ***
17
-
18
- All tests are done in the sample Rails app.
19
-
20
- Please go to test/rails_app and run the tests there.
21
-
22
- Make sure to bundle install and rake db:migrate
23
-
24
- eof
25
- end
26
-
27
8
  desc 'Generate documentation for the devise_ldap_authenticatable plugin.'
28
9
  Rake::RDocTask.new(:rdoc) do |rdoc|
29
10
  rdoc.rdoc_dir = 'rdoc'
@@ -32,21 +13,3 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
32
13
  rdoc.rdoc_files.include('README')
33
14
  rdoc.rdoc_files.include('lib/**/*.rb')
34
15
  end
35
-
36
-
37
- begin
38
- require 'jeweler'
39
- Jeweler::Tasks.new do |gemspec|
40
- gemspec.name = "devise_ldap_authenticatable"
41
- gemspec.summary = "LDAP authentication module for Devise"
42
- gemspec.description = "LDAP authentication module for Devise"
43
- gemspec.email = "curtis.schiewek@gmail.com"
44
- gemspec.homepage = "http://github.com/cschiewek/devise_ldap_authenticatable"
45
- gemspec.authors = ["Curtis Schiewek", "Daniel McNevin"]
46
- gemspec.add_runtime_dependency "devise", "~> 1.4.0"
47
- gemspec.add_runtime_dependency "prathe_net-ldap", "~> 0.2.2"
48
- end
49
- Jeweler::GemcutterTasks.new
50
- rescue LoadError
51
- puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
52
- end
@@ -1,133 +1,22 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
1
  # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "devise_ldap_authenticatable/version"
5
4
 
6
5
  Gem::Specification.new do |s|
7
- s.name = %q{prathe_devise_ldap_authenticatable}
8
- s.version = "0.4.10"
6
+ s.name = 'prathe_devise_ldap_authenticatable'
7
+ s.version = DeviseLdapAuthenticatable::VERSION.dup
8
+ s.platform = Gem::Platform::RUBY
9
+ s.summary = 'Devise extension to allow authentication via LDAP'
10
+ s.email = 'curtis.schiewek@gmail.com'
11
+ s.homepage = 'https://github.com/cschiewek/devise_ldap_authenticatable'
12
+ s.description = s.summary
13
+ s.authors = ['Curtis Schiewek', 'Daniel McNevin', 'Steven Xu']
9
14
 
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Curtis Schiewek", "Daniel McNevin", "Steven Xu"]
12
- s.date = %q{2011-10-17}
13
- s.description = %q{LDAP authentication module for Devise}
14
- s.email = %q{curtis.schiewek@gmail.com}
15
- s.extra_rdoc_files = [
16
- "README.md"
17
- ]
18
- s.files = [
19
- "MIT-LICENSE",
20
- "README.md",
21
- "Rakefile",
22
- "VERSION",
23
- "devise_ldap_authenticatable.gemspec",
24
- "lib/devise_ldap_authenticatable.rb",
25
- "lib/devise_ldap_authenticatable/exception.rb",
26
- "lib/devise_ldap_authenticatable/ldap_adapter.rb",
27
- "lib/devise_ldap_authenticatable/logger.rb",
28
- "lib/devise_ldap_authenticatable/model.rb",
29
- "lib/devise_ldap_authenticatable/routes.rb",
30
- "lib/devise_ldap_authenticatable/schema.rb",
31
- "lib/devise_ldap_authenticatable/strategy.rb",
32
- "lib/devise_ldap_authenticatable/version.rb",
33
- "lib/generators/devise_ldap_authenticatable/install_generator.rb",
34
- "lib/generators/devise_ldap_authenticatable/templates/ldap.yml",
35
- "rails/init.rb",
36
- "test/devise_ldap_authenticatable_test.rb",
37
- "test/ldap/base.ldif",
38
- "test/ldap/clear.ldif",
39
- "test/ldap/local.schema",
40
- "test/ldap/run-server.sh",
41
- "test/ldap/server.pem",
42
- "test/ldap/slapd-ssl-test.conf",
43
- "test/ldap/slapd-test.conf",
44
- "test/rails_app/Gemfile",
45
- "test/rails_app/Gemfile.lock",
46
- "test/rails_app/Rakefile",
47
- "test/rails_app/app/controllers/application_controller.rb",
48
- "test/rails_app/app/controllers/posts_controller.rb",
49
- "test/rails_app/app/helpers/application_helper.rb",
50
- "test/rails_app/app/helpers/posts_helper.rb",
51
- "test/rails_app/app/models/post.rb",
52
- "test/rails_app/app/models/user.rb",
53
- "test/rails_app/app/views/layouts/application.html.erb",
54
- "test/rails_app/app/views/posts/index.html.erb",
55
- "test/rails_app/config.ru",
56
- "test/rails_app/config/application.rb",
57
- "test/rails_app/config/boot.rb",
58
- "test/rails_app/config/cucumber.yml",
59
- "test/rails_app/config/database.yml",
60
- "test/rails_app/config/environment.rb",
61
- "test/rails_app/config/environments/development.rb",
62
- "test/rails_app/config/environments/production.rb",
63
- "test/rails_app/config/environments/test.rb",
64
- "test/rails_app/config/initializers/backtrace_silencers.rb",
65
- "test/rails_app/config/initializers/devise.rb",
66
- "test/rails_app/config/initializers/inflections.rb",
67
- "test/rails_app/config/initializers/mime_types.rb",
68
- "test/rails_app/config/initializers/secret_token.rb",
69
- "test/rails_app/config/initializers/session_store.rb",
70
- "test/rails_app/config/ldap.yml",
71
- "test/rails_app/config/ldap_with_erb.yml",
72
- "test/rails_app/config/ldap_with_uid.yml",
73
- "test/rails_app/config/locales/devise.en.yml",
74
- "test/rails_app/config/locales/en.yml",
75
- "test/rails_app/config/routes.rb",
76
- "test/rails_app/config/ssl_ldap.yml",
77
- "test/rails_app/config/ssl_ldap_with_erb.yml",
78
- "test/rails_app/config/ssl_ldap_with_uid.yml",
79
- "test/rails_app/db/migrate/20100708120302_create_posts.rb",
80
- "test/rails_app/db/migrate/20100708120448_devise_create_users.rb",
81
- "test/rails_app/db/schema.rb",
82
- "test/rails_app/db/seeds.rb",
83
- "test/rails_app/features/manage_logins.feature",
84
- "test/rails_app/features/step_definitions/login_steps.rb",
85
- "test/rails_app/features/step_definitions/web_steps.rb",
86
- "test/rails_app/features/support/env.rb",
87
- "test/rails_app/features/support/paths.rb",
88
- "test/rails_app/lib/tasks/.gitkeep",
89
- "test/rails_app/lib/tasks/cucumber.rake",
90
- "test/rails_app/public/404.html",
91
- "test/rails_app/public/422.html",
92
- "test/rails_app/public/500.html",
93
- "test/rails_app/public/images/rails.png",
94
- "test/rails_app/public/javascripts/application.js",
95
- "test/rails_app/public/javascripts/controls.js",
96
- "test/rails_app/public/javascripts/dragdrop.js",
97
- "test/rails_app/public/javascripts/effects.js",
98
- "test/rails_app/public/javascripts/prototype.js",
99
- "test/rails_app/public/javascripts/rails.js",
100
- "test/rails_app/public/stylesheets/.gitkeep",
101
- "test/rails_app/script/cucumber",
102
- "test/rails_app/script/rails",
103
- "test/rails_app/test/factories/users.rb",
104
- "test/rails_app/test/functional/posts_controller_test.rb",
105
- "test/rails_app/test/performance/browsing_test.rb",
106
- "test/rails_app/test/test_helper.rb",
107
- "test/rails_app/test/unit/helpers/posts_helper_test.rb",
108
- "test/rails_app/test/unit/post_test.rb",
109
- "test/rails_app/test/unit/user_test.rb",
110
- "test/test_helper.rb"
111
- ]
112
- s.homepage = %q{http://github.com/cschiewek/devise_ldap_authenticatable}
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
113
18
  s.require_paths = ["lib"]
114
- s.rubygems_version = %q{1.3.7}
115
- s.summary = %q{LDAP authentication module for Devise}
116
19
 
117
- if s.respond_to? :specification_version then
118
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
119
- s.specification_version = 3
120
-
121
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
122
- s.add_runtime_dependency(%q<devise>, ["~> 1.4.0"])
123
- s.add_runtime_dependency(%q<prathe_net-ldap>, ["~> 0.2.2"])
124
- else
125
- s.add_dependency(%q<devise>, ["~> 1.4.0"])
126
- s.add_dependency(%q<prathe_net-ldap>, ["~> 0.2.2"])
127
- end
128
- else
129
- s.add_dependency(%q<devise>, ["~> 1.4.0"])
130
- s.add_dependency(%q<prathe_net-ldap>, ["~> 0.2.2"])
131
- end
20
+ s.add_dependency('devise', '>= 2.0.0')
21
+ s.add_dependency('net-ldap', '>= 0.2.2')
132
22
  end
133
-
@@ -3,60 +3,69 @@ require "net/ldap"
3
3
  module Devise
4
4
 
5
5
  module LdapAdapter
6
-
6
+
7
7
  def self.valid_credentials?(login, password_plaintext)
8
- options = {:login => login,
9
- :password => password_plaintext,
8
+ options = {:login => login,
9
+ :password => password_plaintext,
10
10
  :ldap_auth_username_builder => ::Devise.ldap_auth_username_builder,
11
11
  :admin => ::Devise.ldap_use_admin_to_bind}
12
-
12
+
13
13
  resource = LdapConnect.new(options)
14
14
  resource.authorized?
15
15
  end
16
16
 
17
- def self.valid_login?(login)
18
- options = {:login => login,
19
- :ldap_auth_username_builder => ::Devise.ldap_auth_username_builder,
20
- :admin => ::Devise.ldap_use_admin_to_bind}
21
- resource = LdapConnect.new(options)
22
- resource.valid_login?
23
- end
24
-
25
17
  def self.update_password(login, new_password)
26
18
  options = {:login => login,
27
19
  :new_password => new_password,
28
20
  :ldap_auth_username_builder => ::Devise.ldap_auth_username_builder,
29
21
  :admin => ::Devise.ldap_use_admin_to_bind}
30
-
22
+
31
23
  resource = LdapConnect.new(options)
32
- resource.change_password! if new_password.present?
24
+ resource.change_password! if new_password.present?
33
25
  end
34
-
35
- def self.get_groups(login)
36
- options = {:login => login,
26
+
27
+ def self.update_own_password(login, new_password, current_password)
28
+ set_ldap_param(login, :userpassword, new_password, current_password)
29
+ end
30
+
31
+ def self.ldap_connect(login)
32
+ options = {:login => login,
37
33
  :ldap_auth_username_builder => ::Devise.ldap_auth_username_builder,
38
34
  :admin => ::Devise.ldap_use_admin_to_bind}
39
35
 
40
- ldap = LdapConnect.new(options)
41
- ldap.user_groups
36
+ resource = LdapConnect.new(options)
42
37
  end
43
-
38
+
39
+ def self.valid_login?(login)
40
+ self.ldap_connect(login).valid_login?
41
+ end
42
+
43
+ def self.get_groups(login)
44
+ self.ldap_connect(login).user_groups
45
+ end
46
+
44
47
  def self.get_dn(login)
45
- options = {:login => login,
46
- :ldap_auth_username_builder => ::Devise.ldap_auth_username_builder,
47
- :admin => ::Devise.ldap_use_admin_to_bind}
48
+ self.ldap_connect(login).dn
49
+ end
50
+
51
+ def self.set_ldap_param(login, param, new_value, password = nil)
52
+ options = { :login => login,
53
+ :ldap_auth_username_builder => ::Devise.ldap_auth_username_builder,
54
+ :password => password }
55
+
48
56
  resource = LdapConnect.new(options)
49
- resource.dn
57
+ resource.set_param(param, new_value)
50
58
  end
51
59
 
52
60
  def self.get_ldap_param(login,param)
53
- options = {:login => login,
54
- :ldap_auth_username_builder => ::Devise.ldap_auth_username_builder,
55
- :admin => ::Devise.ldap_use_admin_to_bind}
56
- resource = LdapConnect.new(options)
61
+ resource = self.ldap_connect(login)
57
62
  resource.ldap_param_value(param)
58
63
  end
59
64
 
65
+ def self.get_ldap_entry(login)
66
+ self.ldap_connect(login).search_for_login
67
+ end
68
+
60
69
  class LdapConnect
61
70
 
62
71
  attr_reader :ldap, :login
@@ -73,18 +82,22 @@ module Devise
73
82
  @ldap.base = ldap_config["base"]
74
83
  @attribute = ldap_config["attribute"]
75
84
  @ldap_auth_username_builder = params[:ldap_auth_username_builder]
76
-
85
+
77
86
  @group_base = ldap_config["group_base"]
78
- @required_groups = ldap_config["required_groups"]
87
+ @required_groups = ldap_config["required_groups"]
79
88
  @required_attributes = ldap_config["require_attribute"]
80
-
81
- @ldap.auth ldap_config["admin_user"], ldap_config["admin_password"] if params[:admin]
82
-
89
+
90
+ @ldap.auth ldap_config["admin_user"], ldap_config["admin_password"] if params[:admin]
91
+
83
92
  @login = params[:login]
84
93
  @password = params[:password]
85
94
  @new_password = params[:new_password]
86
95
  end
87
96
 
97
+ def set_param(param, new_value)
98
+ update_ldap( { param.to_sym => new_value } )
99
+ end
100
+
88
101
  def dn
89
102
  DeviseLdapAuthenticatable::Logger.send("LDAP dn lookup: #{@attribute}=#{@login}")
90
103
  ldap_entry = search_for_login
@@ -95,15 +108,27 @@ module Devise
95
108
  end
96
109
  end
97
110
 
98
- def ldap_param_value(param)
99
- filter = Net::LDAP::Filter.eq(@attribute.to_s, @login.to_s)
111
+ def ldap_param_value(param)
112
+ filter = Net::LDAP::Filter.eq(@attribute.to_s, @login.to_s)
100
113
  ldap_entry = nil
101
114
  @ldap.search(:filter => filter) {|entry| ldap_entry = entry}
102
115
 
103
- DeviseLdapAuthenticatable::Logger.send("Requested param #{param} has value #{ldap_entry.send(param)}")
104
- ldap_entry.send(param)
105
- end
106
-
116
+ if ldap_entry
117
+ if ldap_entry[param]
118
+ DeviseLdapAuthenticatable::Logger.send("Requested param #{param} has value #{ldap_entry.send(param)}")
119
+ value = ldap_entry.send(param)
120
+ value = value.first if value.is_a?(Array) and value.count == 1
121
+ value
122
+ else
123
+ DeviseLdapAuthenticatable::Logger.send("Requested param #{param} does not exist")
124
+ value = nil
125
+ end
126
+ else
127
+ DeviseLdapAuthenticatable::Logger.send("Requested ldap entry does not exist")
128
+ value = nil
129
+ end
130
+ end
131
+
107
132
  def authenticate!
108
133
  @ldap.auth(dn, @password)
109
134
  @ldap.bind
@@ -112,24 +137,24 @@ module Devise
112
137
  def authenticated?
113
138
  authenticate!
114
139
  end
115
-
140
+
116
141
  def authorized?
117
142
  DeviseLdapAuthenticatable::Logger.send("Authorizing user #{dn}")
118
143
  authenticated? && in_required_groups? && has_required_attribute?
119
144
  end
120
-
145
+
121
146
  def change_password!
122
147
  update_ldap(:userpassword => Net::LDAP::Password.generate(:sha, @new_password))
123
148
  end
124
149
 
125
- def in_required_groups?
150
+ def in_required_groups?
126
151
  return true unless ::Devise.ldap_check_group_membership
127
-
152
+
128
153
  ## FIXME set errors here, the ldap.yml isn't set properly.
129
- return false if @required_groups.nil?
130
-
154
+ return false if @required_groups.nil?
155
+
131
156
  admin_ldap = LdapConnect.admin
132
-
157
+
133
158
  for group in @required_groups
134
159
  if group.is_a?(Array)
135
160
  group_attribute, group_name = group
@@ -147,9 +172,9 @@ module Devise
147
172
  else
148
173
  # AD optimization - extension will recursively check sub-groups with one query
149
174
  # "(memberof:1.2.840.113556.1.4.1941:=group_name)"
150
- search_result = admin_ldap.search(:base => dn,
175
+ search_result = admin_ldap.search(:base => dn,
151
176
  :filter => Net::LDAP::Filter.ex("memberof:1.2.840.113556.1.4.1941", group_name),
152
- :scope => Net::LDAP::SearchScope_BaseObject)
177
+ :scope => Net::LDAP::SearchScope_BaseObject)
153
178
  # Will return the user entry if belongs to group otherwise nothing
154
179
  unless search_result.length == 1 && search_result[0].dn.eql?(dn)
155
180
  DeviseLdapAuthenticatable::Logger.send("User #{dn} is not in group: #{group_name }")
@@ -157,27 +182,27 @@ module Devise
157
182
  end
158
183
  end
159
184
  end
160
-
185
+
161
186
  return true
162
187
  end
163
-
188
+
164
189
  def has_required_attribute?
165
190
  return true unless ::Devise.ldap_check_attributes
166
-
191
+
167
192
  admin_ldap = LdapConnect.admin
168
-
193
+
169
194
  user = find_ldap_user(admin_ldap)
170
-
195
+
171
196
  @required_attributes.each do |key,val|
172
197
  unless user[key].include? val
173
198
  DeviseLdapAuthenticatable::Logger.send("User #{dn} did not match attribute #{key}:#{val}")
174
- return false
199
+ return false
175
200
  end
176
201
  end
177
-
202
+
178
203
  return true
179
204
  end
180
-
205
+
181
206
  def user_groups
182
207
  admin_ldap = LdapConnect.admin
183
208
 
@@ -189,36 +214,36 @@ module Devise
189
214
  def valid_login?
190
215
  !search_for_login.nil?
191
216
  end
192
-
217
+
218
+ # Searches the LDAP for the login
219
+ #
220
+ # @return [Object] the LDAP entry found; nil if not found
221
+ def search_for_login
222
+ DeviseLdapAuthenticatable::Logger.send("LDAP search for login: #{@attribute}=#{@login}")
223
+ filter = Net::LDAP::Filter.eq(@attribute.to_s, @login.to_s)
224
+ ldap_entry = nil
225
+ @ldap.search(:filter => filter) {|entry| ldap_entry = entry}
226
+ ldap_entry
227
+ end
228
+
193
229
  private
194
-
230
+
195
231
  def self.admin
196
232
  ldap = LdapConnect.new(:admin => true).ldap
197
-
233
+
198
234
  unless ldap.bind
199
235
  DeviseLdapAuthenticatable::Logger.send("Cannot bind to admin LDAP user")
200
236
  raise DeviseLdapAuthenticatable::LdapException, "Cannot connect to admin LDAP user"
201
237
  end
202
-
238
+
203
239
  return ldap
204
240
  end
205
-
241
+
206
242
  def find_ldap_user(ldap)
207
243
  DeviseLdapAuthenticatable::Logger.send("Finding user: #{dn}")
208
244
  ldap.search(:base => dn, :scope => Net::LDAP::SearchScope_BaseObject).try(:first)
209
245
  end
210
246
 
211
- # Searches the LDAP for the login
212
- #
213
- # @return [Object] the LDAP entry found; nil if not found
214
- def search_for_login
215
- DeviseLdapAuthenticatable::Logger.send("LDAP search for login: #{@attribute}=#{@login}")
216
- filter = Net::LDAP::Filter.eq(@attribute.to_s, @login.to_s)
217
- ldap_entry = nil
218
- @ldap.search(:filter => filter) {|entry| ldap_entry = entry}
219
- ldap_entry
220
- end
221
-
222
247
  def update_ldap(ops)
223
248
  operations = []
224
249
  if ops.is_a? Hash
@@ -229,10 +254,15 @@ module Devise
229
254
  operations = ops
230
255
  end
231
256
 
232
- admin_ldap = LdapConnect.admin
233
-
257
+ if ::Devise.ldap_use_admin_to_bind
258
+ privileged_ldap = LdapConnect.admin
259
+ else
260
+ authenticate!
261
+ privileged_ldap = self.ldap
262
+ end
263
+
234
264
  DeviseLdapAuthenticatable::Logger.send("Modifying user #{dn}")
235
- admin_ldap.modify(:dn => dn, :operations => operations)
265
+ privileged_ldap.modify(:dn => dn, :operations => operations)
236
266
  end
237
267
 
238
268
  end
@@ -21,6 +21,12 @@ module Devise
21
21
  @login_with ||= Devise.mappings[self.class.to_s.underscore.to_sym].to.authentication_keys.first
22
22
  self[@login_with]
23
23
  end
24
+
25
+ def change_password!(current_password)
26
+ raise "Need to set new password first" if @password.blank?
27
+
28
+ Devise::LdapAdapter.update_own_password(login_with, @password, current_password)
29
+ end
24
30
 
25
31
  def reset_password!(new_password, new_password_confirmation)
26
32
  if new_password == new_password_confirmation && ::Devise.ldap_update_password
@@ -42,11 +48,11 @@ module Devise
42
48
  return false
43
49
  end
44
50
  end
45
-
51
+
46
52
  def ldap_groups
47
53
  Devise::LdapAdapter.get_groups(login_with)
48
54
  end
49
-
55
+
50
56
  def ldap_dn
51
57
  Devise::LdapAdapter.get_dn(login_with)
52
58
  end
@@ -67,34 +73,36 @@ module Devise
67
73
  module ClassMethods
68
74
  # Authenticate a user based on configured attribute keys. Returns the
69
75
  # authenticated user if it's valid or nil.
70
- def authenticate_with_ldap(attributes={})
76
+ def authenticate_with_ldap(attributes={})
71
77
  auth_key = self.authentication_keys.first
72
- return nil unless attributes[auth_key].present?
78
+ return nil unless attributes[auth_key].present?
79
+
80
+ auth_key_value = (self.case_insensitive_keys || []).include?(auth_key) ? attributes[auth_key].downcase : attributes[auth_key]
73
81
 
74
82
  # resource = find_for_ldap_authentication(conditions)
75
- resource = where(auth_key => attributes[auth_key]).first
76
-
83
+ resource = where(auth_key => auth_key_value).first
84
+
77
85
  if (resource.blank? and ::Devise.ldap_create_user)
78
86
  resource = new
79
- resource[auth_key] = attributes[auth_key]
87
+ resource[auth_key] = auth_key_value
80
88
  resource.password = attributes[:password]
81
89
  end
82
-
90
+
83
91
  if resource.try(:valid_ldap_authentication?, attributes[:password])
84
92
  if resource.new_record?
85
93
  resource.ldap_before_save if resource.respond_to?(:ldap_before_save)
86
- resource.save
94
+ resource.save
87
95
  end
88
96
  return resource
89
97
  else
90
98
  return nil
91
99
  end
92
100
  end
93
-
101
+
94
102
  def update_with_password(resource)
95
103
  puts "UPDATE_WITH_PASSWORD: #{resource.inspect}"
96
104
  end
97
-
105
+
98
106
  end
99
107
  end
100
108
  end
@@ -5,30 +5,17 @@ module Devise
5
5
  # Strategy for signing in a user based on his login and password using LDAP.
6
6
  # Redirects to sign_in page if it's not authenticated
7
7
  class LdapAuthenticatable < Authenticatable
8
- def valid?
9
- valid_controller? && valid_params? && mapping.to.respond_to?(:authenticate_with_ldap)
10
- end
11
-
12
8
  # Authenticate a user based on login and password params, returning to warden
13
9
  # success and the authenticated user if everything is okay. Otherwise redirect
14
10
  # to sign in page.
15
11
  def authenticate!
16
- if resource = mapping.to.authenticate_with_ldap(params[scope])
12
+ resource = valid_password? && mapping.to.authenticate_with_ldap(params[scope])
13
+ if validate(resource)
17
14
  success!(resource)
18
15
  else
19
16
  fail(:invalid)
20
17
  end
21
- end
22
-
23
- protected
24
-
25
- def valid_controller?
26
- params[:controller] == mapping.controllers[:sessions]
27
- end
28
-
29
- def valid_params?
30
- params[scope] && params[scope][:password].present?
31
- end
18
+ end
32
19
  end
33
20
  end
34
21
  end
@@ -1,4 +1,3 @@
1
1
  module DeviseLdapAuthenticatable
2
- VERSION = "0.4.6"
3
- end
4
-
2
+ VERSION = "0.6.0".freeze
3
+ end
File without changes
@@ -3,7 +3,7 @@ source 'http://rubygems.org'
3
3
  gem 'rails', '3.0.0'
4
4
  gem 'sqlite3-ruby', :require => 'sqlite3'
5
5
 
6
- gem "devise", "~> 1.4.0"
6
+ gem "devise", "~> 1.5.0"
7
7
  gem "devise_ldap_authenticatable", :path => "../../"
8
8
 
9
9
  group :test do
@@ -1,8 +1,8 @@
1
1
  PATH
2
2
  remote: ../../
3
3
  specs:
4
- devise_ldap_authenticatable (0.4.9)
5
- devise (~> 1.4.0)
4
+ devise_ldap_authenticatable (0.5.1)
5
+ devise (~> 1.5.0)
6
6
  net-ldap (~> 0.2.2)
7
7
 
8
8
  GEM
@@ -64,10 +64,10 @@ GEM
64
64
  cucumber (>= 0.8.0)
65
65
  culerity (0.2.12)
66
66
  database_cleaner (0.5.2)
67
- devise (1.4.7)
67
+ devise (1.5.3)
68
68
  bcrypt-ruby (~> 3.0)
69
69
  orm_adapter (~> 0.0.3)
70
- warden (~> 1.0.3)
70
+ warden (~> 1.1)
71
71
  diff-lcs (1.1.2)
72
72
  erubis (2.6.6)
73
73
  abstract (>= 1.0.0)
@@ -94,7 +94,7 @@ GEM
94
94
  rake
95
95
  net-ldap (0.2.2)
96
96
  nokogiri (1.4.3.1)
97
- orm_adapter (0.0.5)
97
+ orm_adapter (0.0.6)
98
98
  polyglot (0.3.1)
99
99
  rack (1.2.1)
100
100
  rack-mount (0.6.12)
@@ -134,7 +134,7 @@ GEM
134
134
  polyglot (>= 0.3.1)
135
135
  trollop (1.16.2)
136
136
  tzinfo (0.3.23)
137
- warden (1.0.5)
137
+ warden (1.1.0)
138
138
  rack (>= 1.0)
139
139
 
140
140
  PLATFORMS
@@ -147,7 +147,7 @@ DEPENDENCIES
147
147
  capybara
148
148
  cucumber-rails
149
149
  database_cleaner
150
- devise (~> 1.4.0)
150
+ devise (~> 1.5.0)
151
151
  devise_ldap_authenticatable!
152
152
  factory_girl_rails
153
153
  launchy
@@ -0,0 +1,22 @@
1
+ authorizations: &AUTHORIZATIONS
2
+ ## Authorization
3
+ group_base: ou=groups,dc=test,dc=com
4
+ required_groups:
5
+ - cn=admins,ou=groups,dc=test,dc=com
6
+ - ["authorizationRole", "cn=users,ou=groups,dc=test,dc=com"]
7
+ require_attribute:
8
+ objectClass: inetOrgPerson
9
+ authorizationRole: blogAdmin
10
+
11
+ test: &TEST
12
+ host: localhost
13
+ port: 3389
14
+ attribute: cn
15
+ base: ou=people,dc=test,dc=com
16
+ admin_user: cn=admin,dc=test,dc=com
17
+ admin_password: secret
18
+ ssl: true
19
+ <<: *AUTHORIZATIONS
20
+
21
+ development:
22
+ <<: *TEST
@@ -5,7 +5,7 @@ class UserTest < ActiveSupport::TestCase
5
5
  def should_be_validated(user, password, message = "Password is invalid")
6
6
  assert(user.valid_ldap_authentication?(password), message)
7
7
  end
8
-
8
+
9
9
  def should_not_be_validated(user, password, message = "Password is not properly set")
10
10
  assert(!user.valid_ldap_authentication?(password), message)
11
11
  end
@@ -25,12 +25,12 @@ class UserTest < ActiveSupport::TestCase
25
25
  assert_equal false, ::Devise::LdapAdapter.valid_login?('barneystinson')
26
26
  end
27
27
  end
28
-
28
+
29
29
  context "create a basic user" do
30
30
  setup do
31
31
  @user = Factory(:user)
32
32
  end
33
-
33
+
34
34
  should "check for password validation" do
35
35
  assert_equal(@user.email, "example.user@test.com")
36
36
  should_be_validated @user, "secret"
@@ -38,18 +38,18 @@ class UserTest < ActiveSupport::TestCase
38
38
  should_not_be_validated @user, "Secret"
39
39
  end
40
40
  end
41
-
41
+
42
42
  context "change a LDAP password" do
43
43
  setup do
44
44
  @user = Factory(:user)
45
45
  end
46
-
46
+
47
47
  should "change password" do
48
48
  should_be_validated @user, "secret"
49
49
  @user.reset_password!("changed","changed")
50
50
  should_be_validated @user, "changed", "password was not changed properly on the LDAP sevrer"
51
51
  end
52
-
52
+
53
53
  should "not allow to change password if setting is false" do
54
54
  should_be_validated @user, "secret"
55
55
  ::Devise.ldap_update_password = false
@@ -58,42 +58,67 @@ class UserTest < ActiveSupport::TestCase
58
58
  should_be_validated @user, "secret"
59
59
  end
60
60
  end
61
-
61
+
62
62
  context "create new local user if user is in LDAP" do
63
-
63
+
64
64
  setup do
65
65
  assert(User.all.blank?, "There shouldn't be any users in the database")
66
66
  end
67
-
67
+
68
68
  should "don't create user in the database" do
69
69
  @user = User.authenticate_with_ldap(:email => "example.user@test.com", :password => "secret")
70
70
  assert(User.all.blank?)
71
71
  end
72
-
72
+
73
73
  context "creating users is enabled" do
74
74
  setup do
75
75
  ::Devise.ldap_create_user = true
76
76
  end
77
-
77
+
78
78
  should "create a user in the database" do
79
79
  @user = User.authenticate_with_ldap(:email => "example.user@test.com", :password => "secret")
80
80
  assert_equal(User.all.size, 1)
81
81
  assert_contains(User.all.collect(&:email), "example.user@test.com", "user not in database")
82
82
  end
83
-
83
+
84
84
  should "not create a user in the database if the password is wrong_secret" do
85
85
  @user = User.authenticate_with_ldap(:email => "example.user", :password => "wrong_secret")
86
86
  assert(User.all.blank?, "There's users in the database")
87
87
  end
88
-
88
+
89
89
  should "create a user if the user is not in LDAP" do
90
90
  @user = User.authenticate_with_ldap(:email => "wrong_secret.user@test.com", :password => "wrong_secret")
91
91
  assert(User.all.blank?, "There's users in the database")
92
92
  end
93
+
94
+ should "create a user in the database if case insensitivity does not matter" do
95
+ ::Devise.case_insensitive_keys = false
96
+ @user = Factory(:user)
97
+
98
+ assert_difference "User.count", +1 do
99
+ User.authenticate_with_ldap(:email => "EXAMPLE.user@test.com", :password => "secret")
100
+ end
101
+ end
102
+
103
+ should "not create a user in the database if case insensitivity matters" do
104
+ ::Devise.case_insensitive_keys = [:email]
105
+ @user = Factory(:user)
106
+
107
+ assert_no_difference "User.count" do
108
+ User.authenticate_with_ldap(:email => "EXAMPLE.user@test.com", :password => "secret")
109
+ end
110
+ end
111
+
112
+ should "create a user with downcased email in the database if case insensitivity matters" do
113
+ ::Devise.case_insensitive_keys = [:email]
114
+
115
+ @user = User.authenticate_with_ldap(:email => "EXAMPLE.user@test.com", :password => "secret")
116
+ assert_contains(User.all.collect(&:email), "example.user@test.com", "user not in database")
117
+ end
93
118
  end
94
-
119
+
95
120
  end
96
-
121
+
97
122
  context "use groups for authorization" do
98
123
  setup do
99
124
  @admin = Factory(:admin)
@@ -101,55 +126,55 @@ class UserTest < ActiveSupport::TestCase
101
126
  ::Devise.authentication_keys = [:email]
102
127
  ::Devise.ldap_check_group_membership = true
103
128
  end
104
-
129
+
105
130
  should "admin should be allowed in" do
106
131
  should_be_validated @admin, "admin_secret"
107
132
  end
108
-
133
+
109
134
  should "admin should have the proper groups set" do
110
135
  assert_contains(@admin.ldap_groups, /cn=admins/, "groups attribute not being set properly")
111
136
  end
112
-
137
+
113
138
  should "user should not be allowed in" do
114
139
  should_not_be_validated @user, "secret"
115
140
  end
116
-
141
+
117
142
  should "not be validated if group with different attribute is removed" do
118
143
  `ldapmodify #{ldap_connect_string} -f ../ldap/delete_authorization_role.ldif`
119
144
  should_not_be_validated @admin, "admin_secret"
120
145
  end
121
146
  end
122
-
147
+
123
148
  context "use role attribute for authorization" do
124
149
  setup do
125
150
  @admin = Factory(:admin)
126
151
  @user = Factory(:user)
127
152
  ::Devise.ldap_check_attributes = true
128
153
  end
129
-
154
+
130
155
  should "admin should be allowed in" do
131
156
  should_be_validated @admin, "admin_secret"
132
157
  end
133
-
158
+
134
159
  should "user should not be allowed in" do
135
160
  should_not_be_validated @user, "secret"
136
161
  end
137
162
  end
138
-
163
+
139
164
  context "use admin setting to bind" do
140
165
  setup do
141
166
  @admin = Factory(:admin)
142
167
  @user = Factory(:user)
143
168
  ::Devise.ldap_use_admin_to_bind = true
144
169
  end
145
-
170
+
146
171
  should "description" do
147
172
  should_be_validated @admin, "admin_secret"
148
173
  end
149
174
  end
150
-
175
+
151
176
  end
152
-
177
+
153
178
  context "use uid for login" do
154
179
  setup do
155
180
  default_devise_settings!
@@ -157,24 +182,24 @@ class UserTest < ActiveSupport::TestCase
157
182
  ::Devise.ldap_config = "#{Rails.root}/config/#{"ssl_" if ENV["LDAP_SSL"]}ldap_with_uid.yml"
158
183
  ::Devise.authentication_keys = [:uid]
159
184
  end
160
-
185
+
161
186
  context "description" do
162
187
  setup do
163
188
  @admin = Factory(:admin)
164
189
  @user = Factory(:user, :uid => "example_user")
165
190
  end
166
-
191
+
167
192
  should "be able to authenticate using uid" do
168
193
  should_be_validated @user, "secret"
169
194
  should_not_be_validated @admin, "admin_secret"
170
195
  end
171
196
  end
172
-
197
+
173
198
  context "create user" do
174
199
  setup do
175
200
  ::Devise.ldap_create_user = true
176
201
  end
177
-
202
+
178
203
  should "create a user in the database" do
179
204
  @user = User.authenticate_with_ldap(:uid => "example_user", :password => "secret")
180
205
  assert_equal(User.all.size, 1)
@@ -199,22 +224,22 @@ class UserTest < ActiveSupport::TestCase
199
224
  should_be_validated Factory(:user, :uid => "example_user"), "secret"
200
225
  end
201
226
  end
202
- end
227
+ end
203
228
  end
204
-
229
+
205
230
  context "using ERB in the config file" do
206
231
  setup do
207
232
  default_devise_settings!
208
233
  reset_ldap_server!
209
234
  ::Devise.ldap_config = "#{Rails.root}/config/#{"ssl_" if ENV["LDAP_SSL"]}ldap_with_erb.yml"
210
235
  end
211
-
236
+
212
237
  context "authenticate" do
213
238
  setup do
214
239
  @admin = Factory(:admin)
215
240
  @user = Factory(:user)
216
241
  end
217
-
242
+
218
243
  should "be able to authenticate" do
219
244
  should_be_validated @user, "secret"
220
245
  should_be_validated @admin, "admin_secret"
@@ -235,7 +260,7 @@ class UserTest < ActiveSupport::TestCase
235
260
  end
236
261
  end
237
262
  end
238
-
263
+
239
264
  context "use username builder" do
240
265
  setup do
241
266
  default_devise_settings!
@@ -250,5 +275,5 @@ class UserTest < ActiveSupport::TestCase
250
275
  should_be_validated @other, "other_secret"
251
276
  end
252
277
  end
253
-
278
+
254
279
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prathe_devise_ldap_authenticatable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.10
4
+ version: 0.6.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,41 +11,42 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2011-10-17 00:00:00.000000000Z
14
+ date: 2012-06-13 00:00:00.000000000Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: devise
18
- requirement: &70183830643780 !ruby/object:Gem::Requirement
18
+ requirement: &70343752987900 !ruby/object:Gem::Requirement
19
19
  none: false
20
20
  requirements:
21
- - - ~>
21
+ - - ! '>='
22
22
  - !ruby/object:Gem::Version
23
- version: 1.4.0
23
+ version: 2.0.0
24
24
  type: :runtime
25
25
  prerelease: false
26
- version_requirements: *70183830643780
26
+ version_requirements: *70343752987900
27
27
  - !ruby/object:Gem::Dependency
28
- name: prathe_net-ldap
29
- requirement: &70183830643300 !ruby/object:Gem::Requirement
28
+ name: net-ldap
29
+ requirement: &70343752987400 !ruby/object:Gem::Requirement
30
30
  none: false
31
31
  requirements:
32
- - - ~>
32
+ - - ! '>='
33
33
  - !ruby/object:Gem::Version
34
34
  version: 0.2.2
35
35
  type: :runtime
36
36
  prerelease: false
37
- version_requirements: *70183830643300
38
- description: LDAP authentication module for Devise
37
+ version_requirements: *70343752987400
38
+ description: Devise extension to allow authentication via LDAP
39
39
  email: curtis.schiewek@gmail.com
40
40
  executables: []
41
41
  extensions: []
42
- extra_rdoc_files:
43
- - README.md
42
+ extra_rdoc_files: []
44
43
  files:
44
+ - .gitignore
45
+ - Gemfile
46
+ - Gemfile.lock
45
47
  - MIT-LICENSE
46
48
  - README.md
47
49
  - Rakefile
48
- - VERSION
49
50
  - devise_ldap_authenticatable.gemspec
50
51
  - lib/devise_ldap_authenticatable.rb
51
52
  - lib/devise_ldap_authenticatable/exception.rb
@@ -63,6 +64,7 @@ files:
63
64
  - test/ldap/base.ldif
64
65
  - test/ldap/clear.ldif
65
66
  - test/ldap/local.schema
67
+ - test/ldap/openldap-data/run/.gitkeep
66
68
  - test/ldap/run-server.sh
67
69
  - test/ldap/server.pem
68
70
  - test/ldap/slapd-ssl-test.conf
@@ -94,6 +96,7 @@ files:
94
96
  - test/rails_app/config/initializers/secret_token.rb
95
97
  - test/rails_app/config/initializers/session_store.rb
96
98
  - test/rails_app/config/ldap.yml
99
+ - test/rails_app/config/ldap_with_boolean_ssl.yml
97
100
  - test/rails_app/config/ldap_with_erb.yml
98
101
  - test/rails_app/config/ldap_with_uid.yml
99
102
  - test/rails_app/config/locales/devise.en.yml
@@ -134,7 +137,7 @@ files:
134
137
  - test/rails_app/test/unit/post_test.rb
135
138
  - test/rails_app/test/unit/user_test.rb
136
139
  - test/test_helper.rb
137
- homepage: http://github.com/cschiewek/devise_ldap_authenticatable
140
+ homepage: https://github.com/cschiewek/devise_ldap_authenticatable
138
141
  licenses: []
139
142
  post_install_message:
140
143
  rdoc_options: []
@@ -157,5 +160,82 @@ rubyforge_project:
157
160
  rubygems_version: 1.8.10
158
161
  signing_key:
159
162
  specification_version: 3
160
- summary: LDAP authentication module for Devise
161
- test_files: []
163
+ summary: Devise extension to allow authentication via LDAP
164
+ test_files:
165
+ - test/devise_ldap_authenticatable_test.rb
166
+ - test/ldap/base.ldif
167
+ - test/ldap/clear.ldif
168
+ - test/ldap/local.schema
169
+ - test/ldap/openldap-data/run/.gitkeep
170
+ - test/ldap/run-server.sh
171
+ - test/ldap/server.pem
172
+ - test/ldap/slapd-ssl-test.conf
173
+ - test/ldap/slapd-test.conf
174
+ - test/rails_app/Gemfile
175
+ - test/rails_app/Gemfile.lock
176
+ - test/rails_app/Rakefile
177
+ - test/rails_app/app/controllers/application_controller.rb
178
+ - test/rails_app/app/controllers/posts_controller.rb
179
+ - test/rails_app/app/helpers/application_helper.rb
180
+ - test/rails_app/app/helpers/posts_helper.rb
181
+ - test/rails_app/app/models/post.rb
182
+ - test/rails_app/app/models/user.rb
183
+ - test/rails_app/app/views/layouts/application.html.erb
184
+ - test/rails_app/app/views/posts/index.html.erb
185
+ - test/rails_app/config.ru
186
+ - test/rails_app/config/application.rb
187
+ - test/rails_app/config/boot.rb
188
+ - test/rails_app/config/cucumber.yml
189
+ - test/rails_app/config/database.yml
190
+ - test/rails_app/config/environment.rb
191
+ - test/rails_app/config/environments/development.rb
192
+ - test/rails_app/config/environments/production.rb
193
+ - test/rails_app/config/environments/test.rb
194
+ - test/rails_app/config/initializers/backtrace_silencers.rb
195
+ - test/rails_app/config/initializers/devise.rb
196
+ - test/rails_app/config/initializers/inflections.rb
197
+ - test/rails_app/config/initializers/mime_types.rb
198
+ - test/rails_app/config/initializers/secret_token.rb
199
+ - test/rails_app/config/initializers/session_store.rb
200
+ - test/rails_app/config/ldap.yml
201
+ - test/rails_app/config/ldap_with_boolean_ssl.yml
202
+ - test/rails_app/config/ldap_with_erb.yml
203
+ - test/rails_app/config/ldap_with_uid.yml
204
+ - test/rails_app/config/locales/devise.en.yml
205
+ - test/rails_app/config/locales/en.yml
206
+ - test/rails_app/config/routes.rb
207
+ - test/rails_app/config/ssl_ldap.yml
208
+ - test/rails_app/config/ssl_ldap_with_erb.yml
209
+ - test/rails_app/config/ssl_ldap_with_uid.yml
210
+ - test/rails_app/db/migrate/20100708120302_create_posts.rb
211
+ - test/rails_app/db/migrate/20100708120448_devise_create_users.rb
212
+ - test/rails_app/db/schema.rb
213
+ - test/rails_app/db/seeds.rb
214
+ - test/rails_app/features/manage_logins.feature
215
+ - test/rails_app/features/step_definitions/login_steps.rb
216
+ - test/rails_app/features/step_definitions/web_steps.rb
217
+ - test/rails_app/features/support/env.rb
218
+ - test/rails_app/features/support/paths.rb
219
+ - test/rails_app/lib/tasks/.gitkeep
220
+ - test/rails_app/lib/tasks/cucumber.rake
221
+ - test/rails_app/public/404.html
222
+ - test/rails_app/public/422.html
223
+ - test/rails_app/public/500.html
224
+ - test/rails_app/public/images/rails.png
225
+ - test/rails_app/public/javascripts/application.js
226
+ - test/rails_app/public/javascripts/controls.js
227
+ - test/rails_app/public/javascripts/dragdrop.js
228
+ - test/rails_app/public/javascripts/effects.js
229
+ - test/rails_app/public/javascripts/prototype.js
230
+ - test/rails_app/public/javascripts/rails.js
231
+ - test/rails_app/public/stylesheets/.gitkeep
232
+ - test/rails_app/script/cucumber
233
+ - test/rails_app/script/rails
234
+ - test/rails_app/test/factories/users.rb
235
+ - test/rails_app/test/functional/posts_controller_test.rb
236
+ - test/rails_app/test/performance/browsing_test.rb
237
+ - test/rails_app/test/test_helper.rb
238
+ - test/rails_app/test/unit/helpers/posts_helper_test.rb
239
+ - test/rails_app/test/unit/post_test.rb
240
+ - test/rails_app/test/unit/user_test.rb
241
+ - test/test_helper.rb
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.4.10