devise_active_directory_authenticatable 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Adam Kerr
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,94 @@
1
+ Devise Active Directory Authenticatable
2
+ ===========================
3
+
4
+ Devise ActiveDirectory Authenticatable is a AD based authentication strategy for the [Devise](http://github.com/plataformatec/devise) authentication framework.
5
+
6
+ If you are building applications for use within your organization which require authentication and you want to use AD, this plugin is for you.
7
+
8
+ Requirements
9
+ ------------
10
+
11
+ - An Active Directory server (tested on Server 2008)
12
+ - Rails 3.0.0
13
+
14
+ These gems are dependencies of the gem:
15
+
16
+ - Devise 1.1.2
17
+ - active_directory 1.0.4 from http://github.com/ajrkerr/activedirectory
18
+
19
+ Installation
20
+ ------------
21
+
22
+ **_Please Note_**
23
+
24
+ This will *only* work for Rails 3 applications.
25
+
26
+ In the Gemfile for your application:
27
+
28
+ gem "devise", ">=1.1.2"
29
+ gem "devise_active_directory_authenticatable"
30
+
31
+ To get the latest version, pull directly from github instead of the gem:
32
+
33
+ gem "devise_active_directory_authenticatable", :git => "git://github.com/ajrkerr/devise_active_directory_authenticatable.git"
34
+
35
+
36
+ Setup
37
+ -----
38
+
39
+ Run the rails generators for devise (please check the [devise](http://github.com/plataformatec/devise) documents for further instructions)
40
+
41
+ rails generate devise:install
42
+ rails generate devise MODEL_NAME
43
+
44
+ Run the rails generator for devise_active_directory_authenticatable
45
+
46
+ rails generate devise_active_directory_authenticatable:install [options]
47
+
48
+ This will update the devise.rb initializer, and update your user model. There are some options you can pass to it:
49
+
50
+ Options:
51
+
52
+ [--user-model=USER_MODEL] # Model to update
53
+ # Default: user
54
+ [--add-rescue] # Update Application Controller with resuce_from for DeviseActiveDirectoryAuthenticatable::ActiveDirectoryException
55
+ # Default: true
56
+
57
+
58
+ Usage
59
+ -----
60
+
61
+ **_Please Note_**
62
+
63
+ This devise plugin has not been tested with DatabaseAuthenticatable enabled at the same time. This is meant as a drop in replacement for DatabaseAuthenticatable allowing for a semi single sign on approach.
64
+
65
+ The field that is used for logins is the first key that's configured in the `config/devise.rb` file under `config.authentication_keys`, which by default is email. For help changing this, please see the [Railscast](http://railscasts.com/episodes/210-customizing-devise) that goes through how to customize Devise.
66
+
67
+ Configuration
68
+ -------------
69
+
70
+ In initializer `config/initializers/devise.rb` :
71
+
72
+ * ad\_settigns
73
+ * Active Directory server configuration settings
74
+
75
+ * ad\_attr\_mapping
76
+ * Attribute mapping between active directory and the user model
77
+
78
+ * ad\_username _(default: :userPrincipalName)_
79
+ * Username attribute on the AD to login with. Maps with the login_with attribute from devise.
80
+
81
+ * ad\_create\_user _(default: true)_
82
+ * If set to true, all valid Active Directory users will be allowed to login and an appropriate user record will be created.
83
+ If set to false, you will have to create the user record before they will be allowed to login.
84
+
85
+ * ad\_logger _(default: true)_
86
+ * If set to true, will log Active Directory queries to the Rails logger.
87
+
88
+
89
+ References
90
+ ----------
91
+
92
+ * [Devise](http://github.com/plataformatec/devise)
93
+ * [Warden](http://github.com/hassox/warden)
94
+
data/Rakefile ADDED
@@ -0,0 +1,52 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
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
+ desc 'Generate documentation for the devise_ldap_authenticatable plugin.'
28
+ Rake::RDocTask.new(:rdoc) do |rdoc|
29
+ rdoc.rdoc_dir = 'rdoc'
30
+ rdoc.title = 'DeviseLDAPAuthenticatable'
31
+ rdoc.options << '--line-numbers' << '--inline-source'
32
+ rdoc.rdoc_files.include('README')
33
+ rdoc.rdoc_files.include('lib/**/*.rb')
34
+ end
35
+
36
+
37
+ begin
38
+ require 'jeweler'
39
+ Jeweler::Tasks.new do |gemspec|
40
+ gemspec.name = "devise_active_directory_authenticatable"
41
+ gemspec.summary = "Active Directory authentication module for Devise"
42
+ gemspec.description = "Active Directory authentication module for Devise, based off of LDAP Authentication"
43
+ gemspec.email = "ajrkerr@gmail.com"
44
+ gemspec.homepage = "http://github.com/ajrkerr/devise_activedirectory_authenticatable"
45
+ gemspec.authors = ["Adam Kerr"]
46
+ gemspec.add_dependency "devise", ">= 1.1.5"
47
+ gemspec.add_dependency "activedirectory", ">= 1.0.4"
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
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,315 @@
1
+ {
2
+ "buffers":
3
+ [
4
+ {
5
+ "file": "/Users/ajrkerr/github/devise_active_directory_authenticatable/lib/devise_active_directory_authenticatable/model.rb",
6
+ "settings":
7
+ {
8
+ "buffer_size": 3917,
9
+ "line_ending": "Unix"
10
+ }
11
+ },
12
+ {
13
+ "file": "/Users/ajrkerr/github/devise_active_directory_authenticatable/Rakefile",
14
+ "settings":
15
+ {
16
+ "buffer_size": 1525,
17
+ "line_ending": "Unix"
18
+ }
19
+ },
20
+ {
21
+ "file": "/Users/ajrkerr/github/devise_active_directory_authenticatable/lib/devise_active_directory_authenticatable.rb",
22
+ "settings":
23
+ {
24
+ "buffer_size": 2719,
25
+ "line_ending": "Unix"
26
+ }
27
+ },
28
+ {
29
+ "file": "/Users/ajrkerr/test3.rb",
30
+ "settings":
31
+ {
32
+ "buffer_size": 1404,
33
+ "line_ending": "Unix"
34
+ }
35
+ },
36
+ {
37
+ "file": "/Users/ajrkerr/github/devise_active_directory_authenticatable/lib/devise_active_directory_authenticatable/strategy.rb",
38
+ "settings":
39
+ {
40
+ "buffer_size": 1196,
41
+ "line_ending": "Unix"
42
+ }
43
+ }
44
+ ],
45
+ "build_system": "",
46
+ "file_history":
47
+ [
48
+ "/Users/ajrkerr/github/devise_active_directory_authenticatable/lib/devise_active_directory_authenticatable/schema.rb",
49
+ "/Users/ajrkerr/test.rb",
50
+ "/Users/ajrkerr/github/devise_active_directory_authenticatable/lib/devise_active_directory_authenticatable/logger.rb",
51
+ "/Users/ajrkerr/github/devise_active_directory_authenticatable/lib/devise_active_directory_authenticatable/model.rb",
52
+ "/Users/ajrkerr/github/devise_activedirectory_authenticatable/lib/devise_active_directory_authenticatable.rb",
53
+ "/Users/ajrkerr/github/devise_activedirectory_authenticatable/lib/devise_active_directory_authenticatable/model.rb",
54
+ "/Users/ajrkerr/github/devise_activedirectory_authenticatable/lib/devise_activedirectory_authenticatable/model.rb",
55
+ "/Users/ajrkerr/github/devise_activedirectory_authenticatable/lib/devise_activedirectory_authenticatable/exception.rb",
56
+ "/Users/ajrkerr/github/devise_activedirectory_authenticatable/lib/devise_activedirectory_authenticatable/strategy.rb",
57
+ "/Users/ajrkerr/github/devise_activedirectory_authenticatable/Rakefile",
58
+ "/Users/ajrkerr/github/devise_activedirectory_authenticatable/devise_activedirectory_authenticatable.gemspec",
59
+ "/Users/ajrkerr/github/devise_activedirectory_authenticatable/lib/devise_activedirectory_authenticatable.rb",
60
+ "/Users/ajrkerr/github/devise_activedirectory_authenticatable/rails/init.rb",
61
+ "/Users/ajrkerr/test4.rb",
62
+ "/Users/ajrkerr/github/devise_activedirectory_authenticatable/lib/devise_ldap_authenticatable/strategy.rb",
63
+ "/Users/ajrkerr/github/devise_activedirectory_authenticatable/lib/devise_ldap_authenticatable.rb",
64
+ "/Users/ajrkerr/github/devise_activedirectory_authenticatable/lib/devise_ldap_authenticatable/ldap_adapter.rb",
65
+ "/Users/ajrkerr/github/devise_activedirectory_authenticatable/MIT-LICENSE",
66
+ "/Users/ajrkerr/github/devise_activedirectory_authenticatable/devise_ldap_authenticatable.gemspec",
67
+ "/Users/ajrkerr/github/devise_activedirectory_authenticatable/lib/devise_ldap_authenticatable/logger.rb",
68
+ "/Users/ajrkerr/github/devise_activedirectory_authenticatable/lib/devise_ldap_authenticatable/exception.rb",
69
+ "/Users/ajrkerr/github/devise_activedirectory_authenticatable/lib/devise_ldap_authenticatable/model.rb",
70
+ "/Users/ajrkerr/github/devise_activedirectory_authenticatable/lib/devise_ldap_authenticatable/version.rb",
71
+ "/Users/ajrkerr/github/devise_activedirectory_authenticatable/VERSION"
72
+ ],
73
+ "find_in_files":
74
+ {
75
+ "include_history":
76
+ [
77
+ ""
78
+ ],
79
+ "location_history":
80
+ [
81
+ "<open folders>"
82
+ ]
83
+ },
84
+ "find_state":
85
+ {
86
+ "case_sensitive": false,
87
+ "find_history":
88
+ [
89
+ "strategy",
90
+ "ldap",
91
+ "devise_active",
92
+ "ActiveDirectoryAuthenticatable",
93
+ "deviseadauthenti",
94
+ "DeviseLdapAuthenticatable"
95
+ ],
96
+ "highlight": true,
97
+ "in_selection": false,
98
+ "preserve_case": false,
99
+ "regex": false,
100
+ "replace_history":
101
+ [
102
+ "DeviseActiveDirectoryAuthenticatable"
103
+ ],
104
+ "reverse": false,
105
+ "show_context": true,
106
+ "use_buffer": false,
107
+ "whole_word": false,
108
+ "wrap": true
109
+ },
110
+ "folders":
111
+ {
112
+ "mount_points":
113
+ [
114
+ "/Users/ajrkerr/github/devise_active_directory_authenticatable"
115
+ ]
116
+ },
117
+ "groups":
118
+ [
119
+ {
120
+ "selected": 0,
121
+ "sheets":
122
+ [
123
+ {
124
+ "buffer": 0,
125
+ "file": "/Users/ajrkerr/github/devise_active_directory_authenticatable/lib/devise_active_directory_authenticatable/model.rb",
126
+ "settings":
127
+ {
128
+ "buffer_size": 3917,
129
+ "regions":
130
+ {
131
+ },
132
+ "selection":
133
+ [
134
+ [
135
+ 883,
136
+ 883
137
+ ]
138
+ ],
139
+ "settings":
140
+ {
141
+ "syntax": "Packages/Ruby/Ruby.tmLanguage",
142
+ "tab_size": 2,
143
+ "translate_tabs_to_spaces": true
144
+ },
145
+ "translation.x": 0,
146
+ "translation.y": 1007,
147
+ "zoom_level": 1
148
+ },
149
+ "type": "text"
150
+ },
151
+ {
152
+ "buffer": 1,
153
+ "file": "/Users/ajrkerr/github/devise_active_directory_authenticatable/Rakefile",
154
+ "settings":
155
+ {
156
+ "buffer_size": 1525,
157
+ "regions":
158
+ {
159
+ },
160
+ "selection":
161
+ [
162
+ [
163
+ 366,
164
+ 366
165
+ ]
166
+ ],
167
+ "settings":
168
+ {
169
+ "syntax": "Packages/Ruby/Ruby.tmLanguage",
170
+ "tab_size": 2,
171
+ "translate_tabs_to_spaces": true
172
+ },
173
+ "translation.x": 0,
174
+ "translation.y": 19,
175
+ "zoom_level": 1
176
+ },
177
+ "type": "text"
178
+ }
179
+ ]
180
+ },
181
+ {
182
+ "selected": 0,
183
+ "sheets":
184
+ [
185
+ {
186
+ "buffer": 2,
187
+ "file": "/Users/ajrkerr/github/devise_active_directory_authenticatable/lib/devise_active_directory_authenticatable.rb",
188
+ "settings":
189
+ {
190
+ "buffer_size": 2719,
191
+ "regions":
192
+ {
193
+ },
194
+ "selection":
195
+ [
196
+ [
197
+ 705,
198
+ 705
199
+ ]
200
+ ],
201
+ "settings":
202
+ {
203
+ "syntax": "Packages/Ruby/Ruby.tmLanguage",
204
+ "tab_size": 2,
205
+ "translate_tabs_to_spaces": true
206
+ },
207
+ "translation.x": 0,
208
+ "translation.y": 0,
209
+ "zoom_level": 1
210
+ },
211
+ "type": "text"
212
+ },
213
+ {
214
+ "buffer": 3,
215
+ "file": "/Users/ajrkerr/test3.rb",
216
+ "settings":
217
+ {
218
+ "buffer_size": 1404,
219
+ "regions":
220
+ {
221
+ },
222
+ "selection":
223
+ [
224
+ [
225
+ 754,
226
+ 754
227
+ ]
228
+ ],
229
+ "settings":
230
+ {
231
+ "syntax": "Packages/Ruby/Ruby.tmLanguage",
232
+ "tab_size": 2,
233
+ "translate_tabs_to_spaces": true
234
+ },
235
+ "translation.x": 0,
236
+ "translation.y": 0,
237
+ "zoom_level": 1
238
+ },
239
+ "type": "text"
240
+ },
241
+ {
242
+ "buffer": 4,
243
+ "file": "/Users/ajrkerr/github/devise_active_directory_authenticatable/lib/devise_active_directory_authenticatable/strategy.rb",
244
+ "settings":
245
+ {
246
+ "buffer_size": 1196,
247
+ "regions":
248
+ {
249
+ },
250
+ "selection":
251
+ [
252
+ [
253
+ 738,
254
+ 738
255
+ ]
256
+ ],
257
+ "settings":
258
+ {
259
+ "syntax": "Packages/Ruby/Ruby.tmLanguage",
260
+ "tab_size": 2,
261
+ "translate_tabs_to_spaces": true
262
+ },
263
+ "translation.x": 0,
264
+ "translation.y": 0,
265
+ "zoom_level": 1
266
+ },
267
+ "type": "text"
268
+ }
269
+ ]
270
+ }
271
+ ],
272
+ "layout":
273
+ {
274
+ "cells":
275
+ [
276
+ [
277
+ 0,
278
+ 0,
279
+ 1,
280
+ 1
281
+ ],
282
+ [
283
+ 1,
284
+ 0,
285
+ 2,
286
+ 1
287
+ ]
288
+ ],
289
+ "cols":
290
+ [
291
+ 0,
292
+ 0.489053,
293
+ 1
294
+ ],
295
+ "rows":
296
+ [
297
+ 0,
298
+ 1
299
+ ]
300
+ },
301
+ "save_all_on_build": true,
302
+ "select_file":
303
+ {
304
+ "height": 0,
305
+ "selected_items":
306
+ [
307
+ ],
308
+ "width": 0
309
+ },
310
+ "show_minimap": false,
311
+ "show_tabs": true,
312
+ "side_bar_visible": true,
313
+ "side_bar_width": 132,
314
+ "status_bar_visible": true
315
+ }
@@ -0,0 +1,53 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{devise_active_directory_authenticatable}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Adam Kerr"]
12
+ s.date = %q{2011-02-10}
13
+ s.description = %q{Active Directory authentication module for Devise, based off of LDAP Authentication}
14
+ s.email = %q{ajrkerr@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
+ "ad_auth.sublime.proj",
24
+ "devise_active_directory_authenticatable.gemspec",
25
+ "lib/devise_active_directory_authenticatable.rb",
26
+ "lib/devise_active_directory_authenticatable/exception.rb",
27
+ "lib/devise_active_directory_authenticatable/logger.rb",
28
+ "lib/devise_active_directory_authenticatable/model.rb",
29
+ "lib/devise_active_directory_authenticatable/strategy.rb",
30
+ "lib/generators/devise_active_directory_authenticatable/install_generator.rb",
31
+ "rails/init.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/ajrkerr/devise_activedirectory_authenticatable}
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.5.0}
36
+ s.summary = %q{Active Directory authentication module for Devise}
37
+
38
+ if s.respond_to? :specification_version then
39
+ s.specification_version = 3
40
+
41
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
42
+ s.add_runtime_dependency(%q<devise>, [">= 1.1.5"])
43
+ s.add_runtime_dependency(%q<activedirectory>, [">= 1.0.4"])
44
+ else
45
+ s.add_dependency(%q<devise>, [">= 1.1.5"])
46
+ s.add_dependency(%q<activedirectory>, [">= 1.0.4"])
47
+ end
48
+ else
49
+ s.add_dependency(%q<devise>, [">= 1.1.5"])
50
+ s.add_dependency(%q<activedirectory>, [">= 1.0.4"])
51
+ end
52
+ end
53
+
@@ -0,0 +1,54 @@
1
+ # encoding: utf-8
2
+ require 'devise'
3
+ require 'active_directory'
4
+
5
+ require 'devise_active_directory_authenticatable/exception'
6
+ require 'devise_active_directory_authenticatable/logger'
7
+
8
+ # Get ldap information from config/ldap.yml now
9
+ module Devise
10
+
11
+ ##TODO Revise these options/vars and their corresponding generator
12
+
13
+ #Active Directory settings
14
+ mattr_accessor :ad_settings
15
+ @@ad_settings = {
16
+ :host => 'domain-controller.example.local',
17
+ :base => 'dc=example,dc=local',
18
+ :port => 636,
19
+ :encryption => :simple_tls,
20
+ :auth => {
21
+ :method => :simple
22
+ }
23
+ }
24
+
25
+ #Attribute mapping for user object
26
+ mattr_accessor :ad_attr_mapping
27
+ @@ad_attr_mapping = {
28
+ :objectGUID => :objectGUID, #Required
29
+ :username => :userPrincipalName,
30
+ :dn => :dn,
31
+ :firstname => :givenName,
32
+ :lastname => :sn,
33
+ }
34
+
35
+ #Username attribute
36
+ mattr_accessor :ad_username
37
+ @@ad_username = :userPrincipalName
38
+
39
+ #Create the user if they're not found
40
+ mattr_accessor :ad_create_user
41
+ @@ad_create_user = true
42
+
43
+ # Log LDAP queries to the Rails logger
44
+ mattr_accessor :ad_logger
45
+ @@ad_logger = true
46
+ end
47
+
48
+ # Add ldap_authenticatable strategy to defaults.
49
+ #
50
+ Devise.add_module(:ad_user,
51
+ :route => :session, ## This will add the routes, rather than in the routes.rb
52
+ :strategy => true,
53
+ :controller => :sessions,
54
+ :model => 'devise_active_directory_authenticatable/model')
@@ -0,0 +1,6 @@
1
+ module DeviseActiveDirectoryAuthenticatable
2
+
3
+ class ActiveDirectoryException < Exception
4
+ end
5
+
6
+ end
@@ -0,0 +1,11 @@
1
+ module DeviseActiveDirectoryAuthenticatable
2
+
3
+ class Logger
4
+ def self.send(message, logger = Rails.logger)
5
+ if ::Devise.ad_logger
6
+ logger.add 0, " \e[36mActiveDirectory:\e[0m #{message}"
7
+ end
8
+ end
9
+ end
10
+
11
+ end
@@ -0,0 +1,124 @@
1
+ require 'devise_active_directory_authenticatable/strategy'
2
+ require 'devise_active_directory_authenticatable/exception'
3
+
4
+ module Devise
5
+ module Models
6
+ # Active Directory Module, responsible for validating the user credentials via Active Directory
7
+ #
8
+ module AdUser
9
+
10
+ #Remove this before production
11
+ ADConnect = DeviseActiveDirectoryAuthenticatable
12
+ ADUser = ActiveDirectory::User
13
+ Logger = DeviseActiveDirectoryAuthenticatable::Logger
14
+
15
+ extend ActiveSupport::Concern
16
+
17
+ included do
18
+ serialize :objectGUID
19
+ end
20
+
21
+ ## Devise key
22
+ def login_with
23
+ self[::Devise.authentication_keys.first]
24
+ end
25
+
26
+ # Update the attributes of the current object from the AD
27
+ # Defaults to current user if no parameters given
28
+ def sync_with_activedirectory(params = {})
29
+ params[:objectGUID] = self.objectGUID if params.empty?
30
+ user = params[:user] || User.find_in_activedirectory(params)
31
+
32
+ return false if user.nil?
33
+
34
+ Logger.send "Updating #{params.inspect}"
35
+
36
+ #Grab attributes from Devise mapping
37
+ ::Devise.ad_attr_mapping.each do |user_attr, active_directory_attr|
38
+ self[user_attr] = user.send(active_directory_attr)
39
+ end
40
+ end
41
+
42
+ # Login event handler. Triggered after authentication.
43
+ def login
44
+ sync_with_activedirectory
45
+ super if defined? super
46
+ end
47
+
48
+ def guid
49
+ objectGUID.unpack("H*")
50
+ end
51
+
52
+
53
+ module ClassMethods
54
+
55
+ # Authenticate a user based on configured attribute keys. Returns the
56
+ # authenticated user if it's valid or nil.
57
+ def authenticate_with_activedirectory(attributes={})
58
+ @login_with = ::Devise.authentication_keys.first
59
+
60
+ username = attributes[@login_with]
61
+ password = attributes[:password]
62
+
63
+ raise ADConnect::ActiveDirectoryException, "Annonymous binds are not permitted." unless attributes[@login_with].present?
64
+
65
+ Logger.send "Attempting to login :#{@login_with} => #{username}"
66
+ ad_connect(:username => username, :password => password)
67
+ ad_user = find_in_activedirectory(:username => username)
68
+ Logger.send "Attempt Result: #{ActiveDirectory::Base.error}"
69
+
70
+ raise ADConnect::ActiveDirectoryException, "Could not connect with Active Directory. Check your username, password, and ensure that your account is not locked." unless ad_user
71
+
72
+ # Find them in the local database
73
+ user = scoped.where(@login_with => attributes[@login_with]).first
74
+
75
+ if user.blank? and ::Devise.ad_create_user
76
+ Logger.send "Creating new user in database"
77
+ user = new
78
+ user[@login_with] = attributes[@login_with]
79
+ user.sync_with_activedirectory(:user => ad_user)
80
+ Logger.send "Created: #{user.inspect}"
81
+ end
82
+
83
+ Logger.send "Checking: #{ad_user.objectGUID} == #{user.objectGUID}"
84
+ # Check to see if we have the same user
85
+ if ad_user == user
86
+ user.save if user.new_record?
87
+ user.login if user.respond_to?(:login)
88
+ return user
89
+ else
90
+ raise ADConnect::ActiveDirectoryException, "Invalid Username or Password. Possible database inconsistency."
91
+ end
92
+
93
+ end
94
+
95
+ #Search based on GUID, DN or Username primarily
96
+ def find_in_activedirectory(params = {})
97
+
98
+ #Reverse mappings
99
+ params[::Devise.ad_username] ||= params[:username] if params[:username].present?
100
+ params[::Devise.ad_username] ||= params[@login_with] if params[@login_with].present?
101
+
102
+ params.delete(:username)
103
+ params.delete(@login_with)
104
+
105
+ Logger.send "Searching for #{params.inspect}"
106
+ user = ADUser.find(:first, params)
107
+ Logger.send "Found: #{user}"
108
+
109
+ return user
110
+ end
111
+
112
+ private
113
+
114
+ def ad_connect(params = {})
115
+ #Used for username and password
116
+ ::Devise.ad_settings[:auth].merge! params
117
+
118
+ ActiveDirectory::Base.setup(::Devise.ad_settings)
119
+ Logger.send "Connection Result: #{ActiveDirectory::Base.error}"
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,37 @@
1
+ require 'devise/strategies/authenticatable'
2
+
3
+ module Devise
4
+ module Strategies
5
+ # Strategy for signing in a user based on his login and password using LDAP.
6
+ # Redirects to sign_in page if it's not authenticated
7
+ class ActiveDirectoryAuthenticatable < Authenticatable
8
+ def valid?
9
+ valid_controller? && valid_params? && mapping.to.respond_to?(:authenticate_with_activedirectory)
10
+ end
11
+
12
+ # Authenticate a user based on login and password params, returning to warden
13
+ # success and the authenticated user if everything is okay. Otherwise redirect
14
+ # to sign in page.
15
+ def authenticate!
16
+ if resource = mapping.to.authenticate_with_activedirectory(params[scope])
17
+ success!(resource)
18
+ else
19
+ fail(:invalid)
20
+ end
21
+ end
22
+
23
+ protected
24
+
25
+ def valid_controller?
26
+ params[:controller] == 'devise/sessions'
27
+ end
28
+
29
+ def valid_params?
30
+ @login_with = ::Devise.authentication_keys.first
31
+ params[scope] && params[scope][@login_with].present? && params[scope][:password].present?
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ Warden::Strategies.add(:ad_user, Devise::Strategies::ActiveDirectoryAuthenticatable)
@@ -0,0 +1,74 @@
1
+ module DeviseActiveDirectoryAuthenticatable
2
+ class InstallGenerator < Rails::Generators::Base
3
+ source_root File.expand_path("../templates", __FILE__)
4
+
5
+ class_option :user_model, :type => :string, :default => "user", :desc => "Model to update"
6
+ class_option :update_model, :type => :boolean, :default => true, :desc => "Update model to change from database_authenticatable to active_directory_authenticatable"
7
+ class_option :add_rescue, :type => :boolean, :default => true, :desc => "Update Application Controller with resuce_from for DeviseActiveDirectoryAuthenticatable::ActiveDirectoryException"
8
+
9
+
10
+ def create_default_devise_settings
11
+ inject_into_file "config/initializers/devise.rb", default_devise_settings, :after => "Devise.setup do |config|\n"
12
+ end
13
+
14
+ def update_user_model
15
+ gsub_file "app/models/#{options.user_model}.rb", /:database_authenticatable/, ":ad_user" if options.update_model?
16
+ end
17
+
18
+ def update_application_controller
19
+ inject_into_class "app/controllers/application_controller.rb", ApplicationController, rescue_from_exception if options.add_rescue?
20
+ end
21
+
22
+ private
23
+
24
+ def default_devise_settings
25
+ settings = <<-eof
26
+ # ==> Basic Active Directory Configuration
27
+
28
+ ## Active Directory server settings
29
+ # config.ad_settings = {
30
+ # :host => 'domain-controller.example.local',
31
+ # :base => 'dc=example,dc=local',
32
+ # :port => 636,
33
+ # :encryption => :simple_tls,
34
+ # :auth => {
35
+ # :method => :simple
36
+ # }
37
+ # }
38
+
39
+ ##Attribute mapping for user object
40
+ # mattr_accessor :ad_attr_mapping
41
+ # config.ad_attr_mapping = {
42
+ # :objectGUID => :objectGUID, #Required
43
+ # :username => :userPrincipalName,
44
+ # :dn => :dn,
45
+ # :firstname => :givenName,
46
+ # :lastname => :sn
47
+ # }
48
+
49
+ ##Username attribute
50
+ ##Maps to :login_with in the devise configuration
51
+ # config.ad_username = :userPrincipalName
52
+
53
+ ##Create the user if they're not found
54
+ ##If this is false, you will need to create the user object before they will be allowed to login
55
+ # config.ad_create_user = true
56
+
57
+ ##Log LDAP queries to the Rails logger
58
+ # config.ad_logger = true
59
+
60
+ eof
61
+
62
+ settings
63
+ end
64
+
65
+ def rescue_from_exception
66
+ <<-eof
67
+ rescue_from DeviseActiveDirectoryAuthenticatable::ActiveDirectoryException do |exception|
68
+ render :text => exception, :status => 500
69
+ end
70
+ eof
71
+ end
72
+
73
+ end
74
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ # Include hook code here
2
+ require 'devise_active_directory_authenticatable'
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: devise_active_directory_authenticatable
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Adam Kerr
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-10 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: devise
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 25
30
+ segments:
31
+ - 1
32
+ - 1
33
+ - 5
34
+ version: 1.1.5
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: activedirectory
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 31
46
+ segments:
47
+ - 1
48
+ - 0
49
+ - 4
50
+ version: 1.0.4
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ description: Active Directory authentication module for Devise, based off of LDAP Authentication
54
+ email: ajrkerr@gmail.com
55
+ executables: []
56
+
57
+ extensions: []
58
+
59
+ extra_rdoc_files:
60
+ - README.md
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - VERSION
66
+ - ad_auth.sublime.proj
67
+ - devise_active_directory_authenticatable.gemspec
68
+ - lib/devise_active_directory_authenticatable.rb
69
+ - lib/devise_active_directory_authenticatable/exception.rb
70
+ - lib/devise_active_directory_authenticatable/logger.rb
71
+ - lib/devise_active_directory_authenticatable/model.rb
72
+ - lib/devise_active_directory_authenticatable/strategy.rb
73
+ - lib/generators/devise_active_directory_authenticatable/install_generator.rb
74
+ - rails/init.rb
75
+ has_rdoc: true
76
+ homepage: http://github.com/ajrkerr/devise_activedirectory_authenticatable
77
+ licenses: []
78
+
79
+ post_install_message:
80
+ rdoc_options: []
81
+
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ hash: 3
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ hash: 3
99
+ segments:
100
+ - 0
101
+ version: "0"
102
+ requirements: []
103
+
104
+ rubyforge_project:
105
+ rubygems_version: 1.5.0
106
+ signing_key:
107
+ specification_version: 3
108
+ summary: Active Directory authentication module for Devise
109
+ test_files: []
110
+