Skip to content

Commit 08354a6

Browse files
committed
Add simple_ldap_authenticator plugin
0 parents  commit 08354a6

File tree

7 files changed

+109
-0
lines changed

7 files changed

+109
-0
lines changed

README

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
SimpleLdapAuthenticator
2+
=======================
3+
4+
Allows for simple authentication to an LDAP server with a minimum of
5+
configuration. See the RDoc for details.

Rakefile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 simple_ldap_authenticator plugin.'
9+
Rake::TestTask.new(:test) do |t|
10+
t.libs << 'lib'
11+
t.pattern = 'test/**/*_test.rb'
12+
t.verbose = true
13+
end
14+
15+
desc 'Generate documentation for the simple_ldap_authenticator plugin.'
16+
Rake::RDocTask.new(:rdoc) do |rdoc|
17+
rdoc.rdoc_dir = 'rdoc'
18+
rdoc.title = 'SimpleLdapAuthenticator'
19+
rdoc.options << '--line-numbers' << '--inline-source'
20+
rdoc.rdoc_files.include('README')
21+
rdoc.rdoc_files.include('lib/**/*.rb')
22+
end

init.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Include hook code here
2+
#require 'simple_ldap_authenticator'

install.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Install hook code here

lib/simple_ldap_authenticator.rb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# SimpleLdapAuthenticator
2+
require 'ldap'
3+
require 'ldap/control'
4+
5+
# Allows for easily authenticated users via LDAP (or LDAPS). If authenticated
6+
# via LDAP to a server running on localhost, you should only have to configure
7+
# the login_format.
8+
#
9+
# Can be configured using the following accessors (with examples):
10+
# * login_format = '%[email protected]' # Active Directory, OR
11+
# * login_format = 'cn=%s,cn=users,o=organization,c=us' # Other LDAP servers
12+
# * servers = ['dc1.domain.com', 'dc2.domain.com'] # names/addresses of LDAP servers to use
13+
# * use_ssl = true # for logging in via LDAPS
14+
# * port = 3289 # instead of 389 for LDAP or 636 for LDAPS
15+
# * logger = RAILS_DEFAULT_LOGGER # for logging authentication successes/failures
16+
#
17+
# The class is used as a global variable, you are not supposed to create an
18+
# instance of it. For example:
19+
#
20+
# require 'simple_ldap_authenticator'
21+
# SimpleLdapAuthenticator.servers = %w'dc1.domain.com dc2.domain.com'
22+
# SimpleLdapAuthenticator.use_ssl = true
23+
# SimpleLdapAuthenticator.login_format = '%[email protected]'
24+
# SimpleLdapAuthenticator.logger = RAILS_DEFAULT_LOGGER
25+
# class LoginController < ApplicationController
26+
# def login
27+
# return redirect_to(:action=>'try_again') unless SimpleLdapAuthenticator.valid?(params[:username], params[:password])
28+
# session[:username] = params[:username]
29+
# end
30+
# end
31+
class SimpleLdapAuthenticator
32+
class << self
33+
@servers = ['127.0.0.1']
34+
@use_ssl = false
35+
@login_format = '%s'
36+
attr_accessor :servers, :use_ssl, :port, :login_format, :logger, :connection
37+
38+
# The next LDAP server to which to connect
39+
def server
40+
servers[0]
41+
end
42+
43+
# Disconnect from current LDAP server and use a different LDAP server on the
44+
# next authentication attempt
45+
def switch_server
46+
self.connection = nil
47+
servers << servers.shift
48+
end
49+
50+
# Check the validity of a login/password combination
51+
def valid?(login, password)
52+
self.connection ||= use_ssl ? LDAP::SSLConn.new(server, port || 636) : LDAP::Conn.new(server, port || 389)
53+
connection.unbind if connection.bound?
54+
begin
55+
connection.bind(login_format % login.to_s, password.to_s)
56+
connection.unbind
57+
logger.info("Authenticated #{login.to_s} by #{server}") if logger
58+
true
59+
rescue LDAP::ResultError => error
60+
connection.unbind if connection.bound?
61+
logger.info("Error attempting to authenticate #{login.to_s} by #{server}: #{error.message}") if logger
62+
switch_server unless error.message == 'Invalid credentials'
63+
false
64+
end
65+
end
66+
end
67+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# desc "Explaining what the task does"
2+
# task :simple_ldap_authenticator do
3+
# # Task goes here
4+
# end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require 'test/unit'
2+
3+
class SimpleLdapAuthenticatorTest < Test::Unit::TestCase
4+
# Replace this with your real tests.
5+
def test_this_plugin
6+
flunk
7+
end
8+
end

0 commit comments

Comments
 (0)