devise_ldap_authenticatable 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Curtis Schiewek
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.
@@ -0,0 +1,120 @@
1
+ Devise LDAP Authenticatable - Based on Devise-Imapable
2
+ =================
3
+
4
+ Devise LDAP Authenticatable is a LDAP 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 LDAP, this plugin is for you.
7
+
8
+ Requirements
9
+ ------------
10
+
11
+ - Rails 2.3.5
12
+ - Devise 1.0.6
13
+ - Net-LDAP 0.1.1
14
+
15
+ **_Please Note_**
16
+
17
+ You must use the net-ldap gem and _NOT_ the ruby-net-ldap gem.
18
+
19
+ Installation
20
+ ------------
21
+
22
+ script/plugin install git@github.com:cschiewek/devise\_ldap\_authenticatable.git
23
+
24
+ Setup
25
+ -----
26
+
27
+ Once devise\_ldap\_authenticatable is installed, all you need to do is setup the user model which includes a small addition to the model itself and to the schema.
28
+
29
+ First the schema :
30
+
31
+ create_table :users do |t|
32
+ t.ldap_authenticatable, :null => false
33
+ end
34
+
35
+ and indexes (optional) :
36
+
37
+ add_index :login, :unique => true
38
+
39
+ and don’t forget to migrate :
40
+
41
+ rake db:migrate.
42
+
43
+ then the model :
44
+
45
+ class User < ActiveRecord::Base
46
+ devise :ldap_authenticatable, :rememberable, :trackable, :timeoutable
47
+
48
+ # Setup accessible (or protected) attributes for your model
49
+ attr_accessible :login, :password, :remember_me
50
+ ...
51
+ end
52
+
53
+ and finally change the authentication key in the devise initializer :
54
+
55
+ Devise.setup do |config|
56
+ ...
57
+ config.authentication_keys = [ :login ]
58
+ ...
59
+ end
60
+
61
+ I recommend using :rememberable, :trackable, :timeoutable as it gives a full feature set for logins.
62
+
63
+ Usage
64
+ -----
65
+
66
+ Devise LDAP Authenticatable works in replacement of Authenticatable, allowing for LDAP authentication via simple bind. The standard sign\_in routes and views work out of the box as these are just reused from devise. I recommend you run :
67
+
68
+ script/generate devise_views
69
+
70
+ so you can customize your login pages.
71
+
72
+ ------------------------------------------------------------
73
+
74
+ **_Please Note_**
75
+
76
+ This devise plugin has not been tested with Authenticatable enabled at the same time. This is meant as a drop in replacement for Authenticatable allowing for a semi single sign on approach.
77
+
78
+
79
+ Configuration
80
+ ----------------------
81
+
82
+ In initializer `config/initializers/devise.rb` :
83
+
84
+ Devise.setup do |config|
85
+ # Required
86
+ config.ldap_host = 'ldap.mydomain.com'
87
+ config.ldap_port = 389
88
+
89
+ # Optional, these will default to false or nil if not set
90
+ config.ldap_ssl = true
91
+ config.ldap_create_user = true
92
+ end
93
+
94
+ * ldap\_host
95
+ * The host of your LDAP server
96
+ * ldap\_port
97
+ * The port your LDAP service is listening on. No default are set.
98
+ * ldap\_ssl
99
+ * Enables SSL (ldaps) encryption. START_TLS encryption will be added when the net-ldap gem adds support for it.
100
+ * ldap\_create\_user
101
+ * If set to true, all valid LDAP users will be allowed to login and an appropriate user record will be created.
102
+ If set to false, you will have to create the user record before they will be allowed to login.
103
+
104
+
105
+ References
106
+ ----------
107
+
108
+ * [Devise](http://github.com/plataformatec/devise)
109
+ * [Warden](http://github.com/hassox/warden)
110
+
111
+
112
+ TODO
113
+ ----
114
+
115
+ - Add support for defining DN format to make logins cleaner
116
+ - Tests
117
+
118
+ Released under the MIT license
119
+
120
+ Copyright (c) 2010 Curtis Schiewek
@@ -0,0 +1,41 @@
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
+ end
15
+
16
+ desc 'Generate documentation for the devise_ldap_authenticatable plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'DeviseLDAPAuthenticatable'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
24
+
25
+
26
+ begin
27
+ require 'jeweler'
28
+ Jeweler::Tasks.new do |gemspec|
29
+ gemspec.name = "devise_ldap_authenticatable"
30
+ gemspec.summary = "LDAP authentication module for Devise"
31
+ gemspec.description = "LDAP authentication module for Devise"
32
+ gemspec.email = "curtis.schiewek@gmail.com"
33
+ gemspec.homepage = "http://github.com/cschiewek/devise_ldap_authenticatable"
34
+ gemspec.authors = ["Curtis Schiewek"]
35
+ gemspec.add_runtime_dependency "devise", "> 1.0.4"
36
+ gemspec.add_runtime_dependency "net-ldap", ">= 0.0.0"
37
+ end
38
+ Jeweler::GemcutterTasks.new
39
+ rescue LoadError
40
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
41
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.2
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+ require 'devise'
3
+
4
+ require 'devise_ldap_authenticatable/schema'
5
+ require 'devise_ldap_authenticatable/ldap_adapter'
6
+ require 'devise_ldap_authenticatable/routes'
7
+
8
+ module Devise
9
+ # host
10
+ mattr_accessor :ldap_host
11
+ @@ldap_host = nil
12
+
13
+ # port
14
+ mattr_accessor :ldap_port
15
+ @@ldap_port = nil
16
+
17
+ # Use SSL
18
+ mattr_accessor :ldap_ssl
19
+ @@ldap_ssl = false
20
+
21
+ # Add valid users to database
22
+ mattr_accessor :ldap_create_user
23
+ @ldap_create_user = false
24
+ end
25
+
26
+ # Add ldap_authenticatable strategy to defaults.
27
+ #
28
+ Devise.add_module(:ldap_authenticatable,
29
+ :strategy => true,
30
+ :controller => :sessions,
31
+ :model => 'devise_ldap_authenticatable/model')
@@ -0,0 +1,25 @@
1
+ require 'net/ldap'
2
+
3
+ module Devise
4
+
5
+ # simple adapter for ldap credential checking
6
+ # ::Devise.ldap_host
7
+ module LdapAdapter
8
+
9
+ def self.valid_credentials?(login, password)
10
+ @encryption = ::Devise.ldap_ssl ? :simple_tls : nil
11
+ ldap = Net::LDAP.new(:encryption => @encryption)
12
+ ldap.host = ::Devise.ldap_host
13
+ ldap.port = ::Devise.ldap_port
14
+ ldap.auth login, password
15
+ if ldap.bind
16
+ true
17
+ else
18
+ # errors.add_to_base(ldap.get_operation_result.message)
19
+ false
20
+ end
21
+ end
22
+
23
+ end
24
+
25
+ end
@@ -0,0 +1,68 @@
1
+ require 'devise_ldap_authenticatable/strategy'
2
+
3
+ module Devise
4
+ module Models
5
+ # LDAP Module, responsible for validating the user credentials via LDAP.
6
+ #
7
+ # Examples:
8
+ #
9
+ # User.authenticate('email@test.com', 'password123') # returns authenticated user or nil
10
+ # User.find(1).valid_password?('password123') # returns true/false
11
+ #
12
+ module LdapAuthenticatable
13
+ def self.included(base)
14
+ base.class_eval do
15
+ extend ClassMethods
16
+
17
+ attr_accessor :password
18
+ end
19
+ end
20
+
21
+ # Set password to nil
22
+ def clean_up_passwords
23
+ self.password = nil
24
+ end
25
+
26
+ # Checks if a resource is valid upon authentication.
27
+ def valid_ldap_authentication?(password)
28
+ Devise::LdapAdapter.valid_credentials?(self.login, password)
29
+ end
30
+
31
+ module ClassMethods
32
+ # Authenticate a user based on configured attribute keys. Returns the
33
+ # authenticated user if it's valid or nil.
34
+ def authenticate_with_ldap(attributes={})
35
+ return unless attributes[:login].present?
36
+ conditions = attributes.slice(:login)
37
+
38
+ unless conditions[:login]
39
+ conditions[:login] = "#{conditions[:login]}"
40
+ end
41
+
42
+ resource = find_for_ldap_authentication(conditions)
43
+ resource = new(conditions) if (resource.nil? and ::Devise.ldap_create_user)
44
+
45
+ if resource.try(:valid_ldap_authentication?, attributes[:password])
46
+ resource.new_record? ? create(conditions) : resource
47
+ end
48
+ end
49
+
50
+ protected
51
+
52
+ # Find first record based on conditions given (ie by the sign in form).
53
+ # Overwrite to add customized conditions, create a join, or maybe use a
54
+ # namedscope to filter records while authenticating.
55
+ # Example:
56
+ #
57
+ # def self.find_for_imap_authentication(conditions={})
58
+ # conditions[:active] = true
59
+ # find(:first, :conditions => conditions)
60
+ # end
61
+ #
62
+ def find_for_ldap_authentication(conditions)
63
+ find(:first, :conditions => conditions)
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,6 @@
1
+ ActionController::Routing::RouteSet::Mapper.class_eval do
2
+
3
+ protected
4
+ # reuse the session routes and controller
5
+ alias :ldap_authenticatable :database_authenticatable
6
+ end
@@ -0,0 +1,12 @@
1
+ Devise::Schema.class_eval do
2
+ # Creates login
3
+ #
4
+ # == Options
5
+ # * :null - When true, allow columns to be null.
6
+ def ldap_authenticatable(options={})
7
+ null = options[:null] || false
8
+
9
+ apply_schema :login, String, :null => null
10
+ end
11
+
12
+ end
@@ -0,0 +1,36 @@
1
+ require 'devise/strategies/base'
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 LdapAuthenticatable < Base
8
+ def valid?
9
+ valid_controller? && valid_params? && mapping.to.respond_to?(:authenticate_with_ldap)
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_ldap(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] == 'sessions'
27
+ end
28
+
29
+ def valid_params?
30
+ params[scope] && params[scope][:password].present?
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ Warden::Strategies.add(:ldap_authenticatable, Devise::Strategies::LdapAuthenticatable)
@@ -0,0 +1,2 @@
1
+ # Include hook code here
2
+ require 'devise_ldap_authenticatable'
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class DeviseLdapAuthenticatableTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: devise_ldap_authenticatable
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 2
9
+ version: 0.1.2
10
+ platform: ruby
11
+ authors:
12
+ - Curtis Schiewek
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-25 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: devise
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">"
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 0
30
+ - 4
31
+ version: 1.0.4
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: net-ldap
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ - 0
44
+ - 0
45
+ version: 0.0.0
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ description: LDAP authentication module for Devise
49
+ email: curtis.schiewek@gmail.com
50
+ executables: []
51
+
52
+ extensions: []
53
+
54
+ extra_rdoc_files:
55
+ - README.md
56
+ files:
57
+ - MIT-LICENSE
58
+ - README.md
59
+ - Rakefile
60
+ - VERSION
61
+ - lib/devise_ldap_authenticatable.rb
62
+ - lib/devise_ldap_authenticatable/ldap_adapter.rb
63
+ - lib/devise_ldap_authenticatable/model.rb
64
+ - lib/devise_ldap_authenticatable/routes.rb
65
+ - lib/devise_ldap_authenticatable/schema.rb
66
+ - lib/devise_ldap_authenticatable/strategy.rb
67
+ - rails/init.rb
68
+ - test/devise_ldap_authenticatable_test.rb
69
+ - test/test_helper.rb
70
+ has_rdoc: true
71
+ homepage: http://github.com/cschiewek/devise_ldap_authenticatable
72
+ licenses: []
73
+
74
+ post_install_message:
75
+ rdoc_options:
76
+ - --charset=UTF-8
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ requirements: []
94
+
95
+ rubyforge_project:
96
+ rubygems_version: 1.3.6
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: LDAP authentication module for Devise
100
+ test_files:
101
+ - test/devise_ldap_authenticatable_test.rb
102
+ - test/test_helper.rb