devise_active_directory_authenticatable 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/ad_auth.sublime.proj +21 -20
- data/devise_active_directory_authenticatable.gemspec +2 -2
- data/lib/devise_active_directory_authenticatable.rb +31 -19
- data/lib/devise_active_directory_authenticatable/models/ad_group.rb +13 -98
- data/lib/devise_active_directory_authenticatable/models/ad_object.rb +102 -45
- data/lib/devise_active_directory_authenticatable/models/ad_user.rb +29 -68
- data/lib/generators/devise_active_directory_authenticatable/install_generator.rb +14 -4
- metadata +4 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/ad_auth.sublime.proj
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
"file": "/Users/ajrkerr/github/devise_active_directory_authenticatable/lib/devise_active_directory_authenticatable/model.rb",
|
6
6
|
"settings":
|
7
7
|
{
|
8
|
-
"buffer_size":
|
8
|
+
"buffer_size": 0,
|
9
9
|
"line_ending": "Unix"
|
10
10
|
}
|
11
11
|
},
|
@@ -13,7 +13,7 @@
|
|
13
13
|
"file": "/Users/ajrkerr/github/devise_active_directory_authenticatable/Rakefile",
|
14
14
|
"settings":
|
15
15
|
{
|
16
|
-
"buffer_size":
|
16
|
+
"buffer_size": 1130,
|
17
17
|
"line_ending": "Unix"
|
18
18
|
}
|
19
19
|
},
|
@@ -21,7 +21,7 @@
|
|
21
21
|
"file": "/Users/ajrkerr/github/devise_active_directory_authenticatable/lib/devise_active_directory_authenticatable.rb",
|
22
22
|
"settings":
|
23
23
|
{
|
24
|
-
"buffer_size":
|
24
|
+
"buffer_size": 1786,
|
25
25
|
"line_ending": "Unix"
|
26
26
|
}
|
27
27
|
},
|
@@ -29,7 +29,7 @@
|
|
29
29
|
"file": "/Users/ajrkerr/test3.rb",
|
30
30
|
"settings":
|
31
31
|
{
|
32
|
-
"buffer_size":
|
32
|
+
"buffer_size": 896,
|
33
33
|
"line_ending": "Unix"
|
34
34
|
}
|
35
35
|
},
|
@@ -37,7 +37,7 @@
|
|
37
37
|
"file": "/Users/ajrkerr/github/devise_active_directory_authenticatable/lib/devise_active_directory_authenticatable/strategy.rb",
|
38
38
|
"settings":
|
39
39
|
{
|
40
|
-
"buffer_size":
|
40
|
+
"buffer_size": 1211,
|
41
41
|
"line_ending": "Unix"
|
42
42
|
}
|
43
43
|
}
|
@@ -125,15 +125,15 @@
|
|
125
125
|
"file": "/Users/ajrkerr/github/devise_active_directory_authenticatable/lib/devise_active_directory_authenticatable/model.rb",
|
126
126
|
"settings":
|
127
127
|
{
|
128
|
-
"buffer_size":
|
128
|
+
"buffer_size": 0,
|
129
129
|
"regions":
|
130
130
|
{
|
131
131
|
},
|
132
132
|
"selection":
|
133
133
|
[
|
134
134
|
[
|
135
|
-
|
136
|
-
|
135
|
+
0,
|
136
|
+
0
|
137
137
|
]
|
138
138
|
],
|
139
139
|
"settings":
|
@@ -153,15 +153,15 @@
|
|
153
153
|
"file": "/Users/ajrkerr/github/devise_active_directory_authenticatable/Rakefile",
|
154
154
|
"settings":
|
155
155
|
{
|
156
|
-
"buffer_size":
|
156
|
+
"buffer_size": 1130,
|
157
157
|
"regions":
|
158
158
|
{
|
159
159
|
},
|
160
160
|
"selection":
|
161
161
|
[
|
162
162
|
[
|
163
|
-
|
164
|
-
|
163
|
+
0,
|
164
|
+
0
|
165
165
|
]
|
166
166
|
],
|
167
167
|
"settings":
|
@@ -187,15 +187,15 @@
|
|
187
187
|
"file": "/Users/ajrkerr/github/devise_active_directory_authenticatable/lib/devise_active_directory_authenticatable.rb",
|
188
188
|
"settings":
|
189
189
|
{
|
190
|
-
"buffer_size":
|
190
|
+
"buffer_size": 1786,
|
191
191
|
"regions":
|
192
192
|
{
|
193
193
|
},
|
194
194
|
"selection":
|
195
195
|
[
|
196
196
|
[
|
197
|
-
|
198
|
-
|
197
|
+
0,
|
198
|
+
0
|
199
199
|
]
|
200
200
|
],
|
201
201
|
"settings":
|
@@ -215,15 +215,15 @@
|
|
215
215
|
"file": "/Users/ajrkerr/test3.rb",
|
216
216
|
"settings":
|
217
217
|
{
|
218
|
-
"buffer_size":
|
218
|
+
"buffer_size": 896,
|
219
219
|
"regions":
|
220
220
|
{
|
221
221
|
},
|
222
222
|
"selection":
|
223
223
|
[
|
224
224
|
[
|
225
|
-
|
226
|
-
|
225
|
+
0,
|
226
|
+
0
|
227
227
|
]
|
228
228
|
],
|
229
229
|
"settings":
|
@@ -243,15 +243,15 @@
|
|
243
243
|
"file": "/Users/ajrkerr/github/devise_active_directory_authenticatable/lib/devise_active_directory_authenticatable/strategy.rb",
|
244
244
|
"settings":
|
245
245
|
{
|
246
|
-
"buffer_size":
|
246
|
+
"buffer_size": 1211,
|
247
247
|
"regions":
|
248
248
|
{
|
249
249
|
},
|
250
250
|
"selection":
|
251
251
|
[
|
252
252
|
[
|
253
|
-
|
254
|
-
|
253
|
+
0,
|
254
|
+
0
|
255
255
|
]
|
256
256
|
],
|
257
257
|
"settings":
|
@@ -298,6 +298,7 @@
|
|
298
298
|
1
|
299
299
|
]
|
300
300
|
},
|
301
|
+
"menu_visible": true,
|
301
302
|
"save_all_on_build": true,
|
302
303
|
"select_file":
|
303
304
|
{
|
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{devise_active_directory_authenticatable}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.3.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Adam Kerr"]
|
12
|
-
s.date = %q{2011-02-
|
12
|
+
s.date = %q{2011-02-17}
|
13
13
|
s.description = %q{Active Directory authentication module for Devise, based off of LDAP Authentication}
|
14
14
|
s.email = %q{ajrkerr@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -20,31 +20,43 @@ module Devise
|
|
20
20
|
}
|
21
21
|
}
|
22
22
|
|
23
|
-
#Attribute mapping for
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
:
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
23
|
+
#Attribute mapping for AD to Rails objects
|
24
|
+
# :object => { :rails_attr => :ad_attr }
|
25
|
+
mattr_accessor :ad_attr_mapping
|
26
|
+
@@ad_attr_mapping = {
|
27
|
+
#Attribute mapping for user object
|
28
|
+
:AdUser => {
|
29
|
+
#Attributes are lowercase
|
30
|
+
:objectguid => :objectguid, #Required
|
31
|
+
:username => :userprincipalname,
|
32
|
+
:dn => :dn,
|
33
|
+
:firstname => :givenName,
|
34
|
+
:lastname => :sn,
|
35
|
+
:whenchanged => :whenchanged,
|
36
|
+
:whencreated => :whencreated,
|
37
|
+
},
|
33
38
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
39
|
+
#Attribute mapping for group objects
|
40
|
+
:AdGroup => {
|
41
|
+
#Attributes are lowercase
|
42
|
+
:objectguid => :objectguid, #Required
|
43
|
+
:dn => :dn,
|
44
|
+
:name => :name,
|
45
|
+
:description => :description,
|
46
|
+
:whencreated => :whencreated,
|
47
|
+
:whenchanged => :whenchanged,
|
48
|
+
}
|
42
49
|
}
|
43
50
|
|
44
|
-
#Username attribute
|
51
|
+
#Username attribute used for logging in
|
52
|
+
#Will be automagicaly mapped to authentication_keys.first
|
45
53
|
mattr_accessor :ad_username
|
46
54
|
@@ad_username = :userPrincipalName
|
47
55
|
|
56
|
+
#Map Devise authentication key accordingly
|
57
|
+
#Does this work when initializers are set too?
|
58
|
+
@@ad_attr_mapping[:AdUser][::Devise.authentication_keys.first] = @@ad_username
|
59
|
+
|
48
60
|
#Create the user if they're not found
|
49
61
|
mattr_accessor :ad_create_user
|
50
62
|
@@ad_create_user = true
|
@@ -1,116 +1,31 @@
|
|
1
|
-
require 'devise_active_directory_authenticatable/strategy'
|
2
1
|
require 'devise_active_directory_authenticatable/exception'
|
2
|
+
require 'devise_active_directory_authenticatable/models/ad_object'
|
3
3
|
|
4
4
|
module Devise
|
5
5
|
module Models
|
6
6
|
# Active Directory Module, responsible for validating the user credentials via Active Directory
|
7
7
|
#
|
8
8
|
module AdGroup
|
9
|
-
|
10
|
-
#Remove this before production
|
11
|
-
ADConnect = DeviseActiveDirectoryAuthenticatable
|
12
|
-
ADUser = ActiveDirectory::User
|
13
|
-
Logger = DeviseActiveDirectoryAuthenticatable::Logger
|
14
|
-
|
15
9
|
extend ActiveSupport::Concern
|
16
|
-
|
17
|
-
## Devise key
|
18
|
-
def login_with
|
19
|
-
self[::Devise.authentication_keys.first]
|
20
|
-
end
|
21
|
-
|
22
|
-
# Update the attributes of the current object from the AD
|
23
|
-
# Defaults to current user if no parameters given
|
24
|
-
def sync_with_activedirectory(params = {})
|
25
|
-
params[:objectGUID] = self.objectGUID if params.empty?
|
26
|
-
user = params[:user] || User.find_in_activedirectory(params)
|
27
|
-
|
28
|
-
return false if user.nil?
|
29
|
-
|
30
|
-
Logger.send "Updating #{params.inspect}"
|
31
|
-
|
32
|
-
#Grab attributes from Devise mapping
|
33
|
-
::Devise.ad_attr_mapping.each do |user_attr, active_directory_attr|
|
34
|
-
self[user_attr] = user.send(active_directory_attr)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
# Login event handler. Triggered after authentication.
|
39
|
-
def login
|
40
|
-
sync_with_activedirectory
|
41
|
-
super if defined? super
|
42
|
-
end
|
43
|
-
|
10
|
+
include AdObject
|
44
11
|
|
45
12
|
module ClassMethods
|
46
|
-
|
47
|
-
|
48
|
-
# authenticated user if it's valid or nil.
|
49
|
-
def authenticate_with_activedirectory(attributes={})
|
50
|
-
@login_with = ::Devise.authentication_keys.first
|
51
|
-
|
52
|
-
username = attributes[@login_with]
|
53
|
-
password = attributes[:password]
|
54
|
-
|
55
|
-
raise ADConnect::ActiveDirectoryException, "Annonymous binds are not permitted." unless attributes[@login_with].present?
|
56
|
-
|
57
|
-
Logger.send "Attempting to login :#{@login_with} => #{username}"
|
58
|
-
ad_connect(:username => username, :password => password)
|
59
|
-
ad_user = find_in_activedirectory(:username => username)
|
60
|
-
Logger.send "Attempt Result: #{ActiveDirectory::Base.error}"
|
61
|
-
|
62
|
-
raise ADConnect::ActiveDirectoryException, "Could not connect with Active Directory. Check your username, password, and ensure that your account is not locked." unless ad_user
|
63
|
-
|
64
|
-
# Find them in the local database
|
65
|
-
user = scoped.where(@login_with => attributes[@login_with]).first
|
66
|
-
|
67
|
-
if user.blank? and ::Devise.ad_create_user
|
68
|
-
Logger.send "Creating new user in database"
|
69
|
-
user = new
|
70
|
-
user[@login_with] = attributes[@login_with]
|
71
|
-
user.sync_with_activedirectory(:user => ad_user)
|
72
|
-
Logger.send "Created: #{user.inspect}"
|
73
|
-
end
|
74
|
-
|
75
|
-
Logger.send "Checking: #{ad_user.objectGUID} == #{user.objectGUID}"
|
76
|
-
# Check to see if we have the same user
|
77
|
-
if ad_user == user
|
78
|
-
user.save if user.new_record?
|
79
|
-
user.login if user.respond_to?(:login)
|
80
|
-
return user
|
81
|
-
else
|
82
|
-
raise ADConnect::ActiveDirectoryException, "Invalid Username or Password. Possible database inconsistency."
|
83
|
-
end
|
84
|
-
|
13
|
+
def activedirectory_class
|
14
|
+
ActiveDirectory::Group
|
85
15
|
end
|
86
16
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
#Reverse mappings
|
91
|
-
params[::Devise.ad_username] ||= params[:username] if params[:username].present?
|
92
|
-
params[::Devise.ad_username] ||= params[@login_with] if params[@login_with].present?
|
93
|
-
|
94
|
-
params.delete(:username)
|
95
|
-
params.delete(@login_with)
|
96
|
-
|
97
|
-
Logger.send "Searching for #{params.inspect}"
|
98
|
-
user = ADUser.find(:first, params)
|
99
|
-
Logger.send "Found: #{user}"
|
100
|
-
|
101
|
-
return user
|
17
|
+
def devise_model
|
18
|
+
AdGroup
|
102
19
|
end
|
103
20
|
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
ActiveDirectory::Base.setup(::Devise.ad_settings)
|
111
|
-
Logger.send "Connection Result: #{ActiveDirectory::Base.error}"
|
112
|
-
end
|
21
|
+
def sync_all
|
22
|
+
#return false unless connected_to_activedirectory?
|
23
|
+
find_or_create_from_activedirectory.each do |gp|
|
24
|
+
gp.save
|
25
|
+
end
|
26
|
+
end
|
113
27
|
end
|
28
|
+
|
114
29
|
end
|
115
30
|
end
|
116
31
|
end
|
@@ -1,72 +1,129 @@
|
|
1
1
|
module Devise
|
2
|
-
|
2
|
+
#Basic functions and shared methods for AD objects in ActiveRecord
|
3
|
+
module AdObject
|
4
|
+
extend ActiveSupport::Concern
|
3
5
|
|
4
|
-
#
|
5
|
-
|
6
|
+
#Constants for easy access
|
7
|
+
ADConnect = DeviseActiveDirectoryAuthenticatable
|
8
|
+
Logger = DeviseActiveDirectoryAuthenticatable::Logger
|
6
9
|
|
7
|
-
|
8
|
-
|
10
|
+
def klass
|
11
|
+
self.class
|
12
|
+
end
|
13
|
+
|
14
|
+
# Update the attributes of the current object from the AD
|
15
|
+
# Defaults to current user if no parameters given
|
16
|
+
def activedirectory_sync!(params = {})
|
17
|
+
params[:objectguid] = self.objectguid if params.empty?
|
18
|
+
ad_objs = params[:object] || klass.find_in_activedirectory(params)
|
9
19
|
|
10
|
-
|
20
|
+
return false if ad_objs.nil?
|
21
|
+
ad_objs = Array(ad_objs) unless ad_objs.is_a? Array
|
11
22
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
23
|
+
#Grab attributes from Devise mapping
|
24
|
+
ad_objs.each do |ad_obj|
|
25
|
+
::Devise.ad_attr_mapping[klass.devise_model_name.to_sym].each do |local_attr, active_directory_attr|
|
26
|
+
self[local_attr] = ad_obj.send(active_directory_attr)
|
27
|
+
end
|
17
28
|
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def activedirectory_self
|
32
|
+
find_in_activedirectory :objectGUID => objectGUID
|
33
|
+
end
|
18
34
|
|
19
|
-
|
20
|
-
|
21
|
-
|
35
|
+
module ClassMethods
|
36
|
+
|
37
|
+
# def devise_model
|
38
|
+
# self.ancestors.each do |mod|
|
39
|
+
# return mod if mod.include? self.class
|
40
|
+
# end
|
41
|
+
# end
|
42
|
+
|
43
|
+
def devise_model_name
|
44
|
+
devise_model.name[/.*::(.*)/, 1]
|
22
45
|
end
|
23
46
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
params[:objectGUID] = self.objectGUID if params.empty?
|
28
|
-
user = params[:user] || User.find_in_activedirectory(params)
|
47
|
+
def activedirectory_class_name
|
48
|
+
activedirectory_class.name[/.*::(.*)/, 1]
|
49
|
+
end
|
29
50
|
|
30
|
-
|
51
|
+
#TODO switch from reverse to rassoc to allow for multiple mappings
|
52
|
+
def ad_field_to_local field_name
|
53
|
+
@ad_to_local_map ||= ::Devise.ad_attr_mapping[devise_model_name.to_sym].invert
|
54
|
+
return (@ad_to_local_map.has_key? field_name) ? @ad_to_local_map[field_name] : field_name
|
55
|
+
end
|
31
56
|
|
32
|
-
|
57
|
+
#TODO switch from reverse to rassoc to allow for multiple mappings
|
58
|
+
def local_field_to_ad field_name
|
59
|
+
@local_to_ad_map ||= ::Devise.ad_attr_mapping[devise_model_name.to_sym]
|
60
|
+
return (@local_to_ad_map.has_key? field_name) ? @local_to_ad_map[field_name] : field_name
|
61
|
+
end
|
33
62
|
|
34
|
-
|
35
|
-
|
36
|
-
|
63
|
+
def ad_attrs_to_local ad_attrs
|
64
|
+
local_attrs = {}
|
65
|
+
ad_attrs.each do |ad_key, value|
|
66
|
+
local_key = ad_field_to_local(ad_key)
|
67
|
+
local_attrs[local_key] = value
|
37
68
|
end
|
69
|
+
local_attrs
|
38
70
|
end
|
39
71
|
|
72
|
+
def local_attrs_to_ad local_attrs
|
73
|
+
ad_attrs = {}
|
74
|
+
local_attrs.each do |local_key, value|
|
75
|
+
ad_key = local_field_to_ad(local_key)
|
76
|
+
ad_attrs[ad_key] = value
|
77
|
+
end
|
78
|
+
ad_attrs
|
79
|
+
end
|
80
|
+
|
81
|
+
#Search based on GUID, DN or Username primarily
|
82
|
+
def find_in_activedirectory(local_params = {})
|
83
|
+
#Reverse mappings for user
|
84
|
+
ad_params = local_attrs_to_ad local_params
|
85
|
+
|
86
|
+
return find_all_in_activedirectory if ad_params.empty?
|
40
87
|
|
41
|
-
|
88
|
+
ad_objs = activedirectory_class.find(:all, ad_params)
|
42
89
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
90
|
+
return ad_objs
|
91
|
+
end
|
92
|
+
|
93
|
+
def find_or_create_from_activedirectory params = {}
|
94
|
+
ad_objs = find_in_activedirectory params
|
95
|
+
local_objs = []
|
49
96
|
|
50
|
-
|
51
|
-
|
97
|
+
ad_objs.each do |ad_obj|
|
98
|
+
obj = scoped.where(:objectguid => ad_obj.objectguid).first
|
99
|
+
obj = new if obj.blank?
|
52
100
|
|
53
|
-
|
54
|
-
user = ADUser.find(:first, params)
|
55
|
-
Logger.send "Found: #{user}"
|
101
|
+
obj.activedirectory_sync! :object => ad_obj
|
56
102
|
|
57
|
-
|
103
|
+
local_objs << obj
|
58
104
|
end
|
59
105
|
|
60
|
-
|
106
|
+
local_objs
|
107
|
+
end
|
61
108
|
|
62
|
-
|
63
|
-
|
64
|
-
|
109
|
+
def find_all_in_activedirectory
|
110
|
+
activedirectory_class.find(:all)
|
111
|
+
end
|
112
|
+
|
113
|
+
def connected_to_activedirectory?
|
114
|
+
ActiveDirectory::Base.connected?
|
115
|
+
end
|
116
|
+
|
117
|
+
# Initializes connection with active directory
|
118
|
+
def set_activedirectory_credentials(params = {})
|
119
|
+
#Used for username and password
|
120
|
+
::Devise.ad_settings[:auth].merge! params
|
121
|
+
end
|
65
122
|
|
66
|
-
|
67
|
-
|
68
|
-
|
123
|
+
def activedirectory_connect
|
124
|
+
ActiveDirectory::Base.setup(::Devise.ad_settings)
|
125
|
+
raise DeviseActiveDirectoryAuthenticatable::ActiveDirectoryException, "Invliad Username or Password" unless ActiveDirectory::Base.connected?
|
69
126
|
end
|
70
127
|
end
|
71
128
|
end
|
72
|
-
end
|
129
|
+
end
|
@@ -1,49 +1,44 @@
|
|
1
1
|
require 'devise_active_directory_authenticatable/strategy'
|
2
2
|
require 'devise_active_directory_authenticatable/exception'
|
3
|
+
require 'devise_active_directory_authenticatable/models/ad_object'
|
4
|
+
require 'devise_active_directory_authenticatable/models/ad_group'
|
3
5
|
|
4
6
|
module Devise
|
5
7
|
module Models
|
6
8
|
# Active Directory Module, responsible for validating the user credentials via Active Directory
|
7
9
|
#
|
8
10
|
module AdUser
|
11
|
+
extend ActiveSupport::Concern
|
12
|
+
include AdObject
|
9
13
|
|
10
|
-
#Remove this before production
|
11
|
-
ADConnect = DeviseActiveDirectoryAuthenticatable
|
12
|
-
ADUser = ActiveDirectory::User
|
13
14
|
Logger = DeviseActiveDirectoryAuthenticatable::Logger
|
14
15
|
|
15
|
-
extend ActiveSupport::Concern
|
16
|
-
|
17
16
|
## Devise key
|
18
17
|
def login_with
|
19
18
|
self[::Devise.authentication_keys.first]
|
20
19
|
end
|
21
20
|
|
22
|
-
# Update the attributes of the current object from the AD
|
23
|
-
# Defaults to current user if no parameters given
|
24
|
-
def sync_with_activedirectory(params = {})
|
25
|
-
params[:objectGUID] = self.objectGUID if params.empty?
|
26
|
-
user = params[:user] || User.find_in_activedirectory(params)
|
27
|
-
|
28
|
-
return false if user.nil?
|
29
|
-
|
30
|
-
Logger.send "Updating #{params.inspect}"
|
31
|
-
|
32
|
-
#Grab attributes from Devise mapping
|
33
|
-
::Devise.ad_user_mapping.each do |user_attr, active_directory_attr|
|
34
|
-
Logger.send "Settings #{user_attr} = #{user.send(active_directory_attr)}"
|
35
|
-
self[user_attr] = user.send(active_directory_attr)
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
21
|
# Login event handler. Triggered after authentication.
|
40
22
|
def login
|
41
|
-
|
42
|
-
|
23
|
+
activedirectory_sync!
|
24
|
+
|
25
|
+
super if defined? super
|
43
26
|
end
|
44
27
|
|
28
|
+
def authenticate_with_activedirectory params = {}
|
29
|
+
params[:username] ||= self[login_with]
|
30
|
+
set_activedirectory_credentials params
|
31
|
+
activedirectory_connect
|
32
|
+
end
|
45
33
|
|
46
34
|
module ClassMethods
|
35
|
+
def activedirectory_class
|
36
|
+
ActiveDirectory::User
|
37
|
+
end
|
38
|
+
|
39
|
+
def devise_model
|
40
|
+
AdUser
|
41
|
+
end
|
47
42
|
|
48
43
|
# Authenticate a user based on configured attribute keys. Returns the
|
49
44
|
# authenticated user if it's valid or nil.
|
@@ -54,61 +49,27 @@ module Devise
|
|
54
49
|
password = attributes[:password]
|
55
50
|
|
56
51
|
Logger.send "Attempting to login :#{@login_with} => #{username}"
|
57
|
-
|
58
|
-
|
52
|
+
set_activedirectory_credentials :username => username, :password => password
|
53
|
+
activedirectory_connect
|
59
54
|
Logger.send "Attempt Result: #{ActiveDirectory::Base.error}"
|
60
55
|
|
61
|
-
raise ADConnect::ActiveDirectoryException, "Could not connect with Active Directory. Check your username, password, and ensure that your account is not locked." unless ad_user
|
62
56
|
|
63
|
-
#
|
64
|
-
|
57
|
+
# ad_user = find_in_activedirectory(@login_with => username)
|
58
|
+
# return false unless ad_user
|
65
59
|
|
66
|
-
|
67
|
-
|
68
|
-
user = new
|
69
|
-
user[@login_with] = attributes[@login_with]
|
70
|
-
user.sync_with_activedirectory(:user => ad_user)
|
71
|
-
Logger.send "Created: #{user.inspect}"
|
72
|
-
end
|
60
|
+
# Find them in the local database
|
61
|
+
user = find_or_create_from_activedirectory(@login_with => attributes[@login_with]).first
|
73
62
|
Logger.send "User: #{user.inspect}"
|
74
|
-
|
63
|
+
|
75
64
|
# Check to see if we have the same user
|
76
|
-
|
77
|
-
user.save if user.new_record?
|
65
|
+
unless user.nil?
|
66
|
+
user.save if user.new_record? and ::Devise.ad_create_user
|
78
67
|
user.login if user.respond_to?(:login)
|
79
68
|
return user
|
80
69
|
else
|
81
|
-
raise
|
70
|
+
raise DeviseActiveDirectoryAuthenticatable::ActiveDirectoryException, "Active Directory user and entry in local database have different GUIDs. Possible database inconsistency."
|
82
71
|
end
|
83
|
-
|
84
72
|
end
|
85
|
-
|
86
|
-
#Search based on GUID, DN or Username primarily
|
87
|
-
def find_in_activedirectory(params = {})
|
88
|
-
|
89
|
-
#Reverse mappings
|
90
|
-
params[::Devise.ad_username] ||= params[:username] if params[:username].present?
|
91
|
-
params[::Devise.ad_username] ||= params[@login_with] if params[@login_with].present?
|
92
|
-
|
93
|
-
params.delete(:username)
|
94
|
-
params.delete(@login_with)
|
95
|
-
|
96
|
-
Logger.send "Searching for #{params.inspect}"
|
97
|
-
user = ADUser.find(:first, params)
|
98
|
-
Logger.send "Found: #{user}"
|
99
|
-
|
100
|
-
return user
|
101
|
-
end
|
102
|
-
|
103
|
-
private
|
104
|
-
|
105
|
-
def ad_connect(params = {})
|
106
|
-
#Used for username and password
|
107
|
-
::Devise.ad_settings[:auth].merge! params
|
108
|
-
|
109
|
-
ActiveDirectory::Base.setup(::Devise.ad_settings)
|
110
|
-
Logger.send "Connection Result: #{ActiveDirectory::Base.error}"
|
111
|
-
end
|
112
73
|
end
|
113
74
|
end
|
114
75
|
end
|
@@ -36,15 +36,25 @@ module DeviseActiveDirectoryAuthenticatable
|
|
36
36
|
# }
|
37
37
|
# }
|
38
38
|
|
39
|
+
|
39
40
|
##Attribute mapping for user object
|
40
|
-
# config.
|
41
|
-
# :
|
42
|
-
# :username => :
|
41
|
+
# config.ad_user_mapping = {
|
42
|
+
# :objectguid => :objectguid, #Required
|
43
|
+
# :username => :userprincipalname,
|
43
44
|
# :dn => :dn,
|
44
|
-
# :firstname => :
|
45
|
+
# :firstname => :givenname,
|
45
46
|
# :lastname => :sn
|
46
47
|
# }
|
47
48
|
|
49
|
+
# config.ad_group_mapping = {
|
50
|
+
# :objectguid => :objectguid, #Required
|
51
|
+
# :dn => :dn,
|
52
|
+
# :name => :name,
|
53
|
+
# :description => :description,
|
54
|
+
# :whencreated => :whencreated,
|
55
|
+
# :whenchanged => :whenchanged,
|
56
|
+
# }
|
57
|
+
|
48
58
|
##Username attribute
|
49
59
|
##Maps to :login_with in the devise configuration
|
50
60
|
# config.ad_username = :userPrincipalName
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: devise_active_directory_authenticatable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 3
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.3.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Adam Kerr
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-02-
|
18
|
+
date: 2011-02-17 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|