simple_ldap_authenticator 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/LICENSE +19 -0
  2. data/README +22 -0
  3. data/lib/simple_ldap_authenticator.rb +127 -0
  4. metadata +60 -0
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2004-2007 Jeremy Evans
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
data/README ADDED
@@ -0,0 +1,22 @@
1
+ SimpleLdapAuthenticator
2
+ =======================
3
+
4
+ Allows for simple authentication to an LDAP server with a minimum of
5
+ configuration. Requires either Ruby/LDAP or Net::LDAP.
6
+
7
+ Usage is fairly simple:
8
+ require 'simple_ldap_authenticator'
9
+ SimpleLdapAuthenticator.servers = %w'dc1.domain.com dc2.domain.com'
10
+ SimpleLdapAuthenticator.use_ssl = true
11
+ SimpleLdapAuthenticator.login_format = '%s @domain.com'
12
+ SimpleLdapAuthenticator.logger = RAILS_DEFAULT_LOGGER
13
+ class LoginController < ApplicationController
14
+ def login
15
+ return redirect_to(:action=>'try_again') unless \
16
+ SimpleLdapAuthenticator.valid?(params[:username], \
17
+ params[:password])
18
+ session[:username] = params[:username]
19
+ end
20
+ end
21
+
22
+ github: http://github.com/jeremyevans/simple_ldap_authenticator/tree/master
@@ -0,0 +1,127 @@
1
+ # SimpleLdapAuthenticator
2
+ #
3
+ # This plugin supports both Ruby/LDAP and Net::LDAP, defaulting to Ruby/LDAP
4
+ # if it is available. If both are installed and you want to force the use of
5
+ # Net::LDAP, set SimpleLdapAuthenticator.ldap_library = 'net/ldap'.
6
+
7
+ # Allows for easily authenticating users via LDAP (or LDAPS). If authenticating
8
+ # via LDAP to a server running on localhost, you should only have to configure
9
+ # the login_format.
10
+ #
11
+ # Can be configured using the following accessors (with examples):
12
+ # * login_format = '%s@domain.com' # Active Directory, OR
13
+ # * login_format = 'cn=%s,cn=users,o=organization,c=us' # Other LDAP servers
14
+ # * servers = ['dc1.domain.com', 'dc2.domain.com'] # names/addresses of LDAP servers to use
15
+ # * use_ssl = true # for logging in via LDAPS
16
+ # * port = 3289 # instead of 389 for LDAP or 636 for LDAPS
17
+ # * logger = RAILS_DEFAULT_LOGGER # for logging authentication successes/failures
18
+ #
19
+ # The class is used as a global variable, you are not supposed to create an
20
+ # instance of it. For example:
21
+ #
22
+ # require 'simple_ldap_authenticator'
23
+ # SimpleLdapAuthenticator.servers = %w'dc1.domain.com dc2.domain.com'
24
+ # SimpleLdapAuthenticator.use_ssl = true
25
+ # SimpleLdapAuthenticator.login_format = '%s@domain.com'
26
+ # SimpleLdapAuthenticator.logger = RAILS_DEFAULT_LOGGER
27
+ # class LoginController < ApplicationController
28
+ # def login
29
+ # return redirect_to(:action=>'try_again') unless SimpleLdapAuthenticator.valid?(params[:username], params[:password])
30
+ # session[:username] = params[:username]
31
+ # end
32
+ # end
33
+ class SimpleLdapAuthenticator
34
+ class << self
35
+ @servers = ['127.0.0.1']
36
+ @use_ssl = false
37
+ @login_format = '%s'
38
+ attr_accessor :servers, :use_ssl, :port, :login_format, :logger, :connection, :ldap_library
39
+
40
+ # Load the required LDAP library, either 'ldap' or 'net/ldap'
41
+ def load_ldap_library
42
+ return if @ldap_library_loaded
43
+ if ldap_library
44
+ if ldap_library == 'net/ldap'
45
+ require 'net/ldap'
46
+ else
47
+ require 'ldap'
48
+ require 'ldap/control'
49
+ end
50
+ else
51
+ begin
52
+ require 'ldap'
53
+ require 'ldap/control'
54
+ ldap_library = 'ldap'
55
+ rescue LoadError
56
+ require 'net/ldap'
57
+ ldap_library = 'net/ldap'
58
+ end
59
+ end
60
+ @ldap_library_loaded = true
61
+ end
62
+
63
+ # The next LDAP server to which to connect
64
+ def server
65
+ servers[0]
66
+ end
67
+
68
+ # The connection to the LDAP server. A single connection is made and the
69
+ # connection is only changed if a server returns an error other than
70
+ # invalid password.
71
+ def connection
72
+ return @connection if @connection
73
+ load_ldap_library
74
+ @connection = if ldap_library == 'net/ldap'
75
+ Net::LDAP.new(:host=>server, :port=>(port), :encryption=>(:simple_tls if use_ssl))
76
+ else
77
+ (use_ssl ? LDAP::SSLConn : LDAP::Conn).new(server, port)
78
+ end
79
+ end
80
+
81
+ # The port to use. Defaults to 389 for LDAP and 636 for LDAPS.
82
+ def port
83
+ @port ||= use_ssl ? 636 : 389
84
+ end
85
+
86
+ # Disconnect from current LDAP server and use a different LDAP server on the
87
+ # next authentication attempt
88
+ def switch_server
89
+ self.connection = nil
90
+ servers << servers.shift
91
+ end
92
+
93
+ # Check the validity of a login/password combination
94
+ def valid?(login, password)
95
+ if ldap_library == 'net/ldap'
96
+ connection.authenticate(login_format % login.to_s, password.to_s)
97
+ begin
98
+ if connection.bind
99
+ logger.info("Authenticated #{login.to_s} by #{server}") if logger
100
+ true
101
+ else
102
+ logger.info("Error attempting to authenticate #{login.to_s} by #{server}: #{connection.get_operation_result.code} #{connection.get_operation_result.message}") if logger
103
+ switch_server unless connection.get_operation_result.code == 49
104
+ false
105
+ end
106
+ rescue Net::LDAP::LdapError => error
107
+ logger.info("Error attempting to authenticate #{login.to_s} by #{server}: #{error.message}") if logger
108
+ switch_server
109
+ false
110
+ end
111
+ else
112
+ connection.unbind if connection.bound?
113
+ begin
114
+ connection.bind(login_format % login.to_s, password.to_s)
115
+ connection.unbind
116
+ logger.info("Authenticated #{login.to_s} by #{server}") if logger
117
+ true
118
+ rescue LDAP::ResultError => error
119
+ connection.unbind if connection.bound?
120
+ logger.info("Error attempting to authenticate #{login.to_s} by #{server}: #{error.message}") if logger
121
+ switch_server unless error.message == 'Invalid credentials'
122
+ false
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_ldap_authenticator
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeremy Evans
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-11 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: code@jeremyevans.net
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ files:
25
+ - README
26
+ - LICENSE
27
+ - lib/simple_ldap_authenticator.rb
28
+ has_rdoc: true
29
+ homepage:
30
+ licenses: []
31
+
32
+ post_install_message:
33
+ rdoc_options:
34
+ - --inline-source
35
+ - --line-numbers
36
+ - README
37
+ - lib
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.3.5
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: Easy authentication to an LDAP server(s)
59
+ test_files: []
60
+