Skip to content

Commit 0c85621

Browse files
Curtis SchiewekCurtis Schiewek
Curtis Schiewek
authored and
Curtis Schiewek
committed
first set of changes
1 parent 0969c0b commit 0c85621

11 files changed

+223
-14
lines changed

MIT-LICENSE

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2010 [name of plugin creator]
1+
Copyright (c) 2010 Curtis Schiewek
22

33
Permission is hereby granted, free of charge, to any person obtaining
44
a copy of this software and associated documentation files (the
@@ -17,4 +17,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
1717
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
1818
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
1919
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.
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Rakefile

+9-8
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ Rake::TestTask.new(:test) do |t|
1313
t.verbose = true
1414
end
1515

16-
desc 'Generate documentation for the devise_imapable plugin.'
16+
desc 'Generate documentation for the devise_ldap_authenticatable plugin.'
1717
Rake::RDocTask.new(:rdoc) do |rdoc|
1818
rdoc.rdoc_dir = 'rdoc'
19-
rdoc.title = 'DeviseImapable'
19+
rdoc.title = 'DeviseLDAPAuthenticatable'
2020
rdoc.options << '--line-numbers' << '--inline-source'
2121
rdoc.rdoc_files.include('README')
2222
rdoc.rdoc_files.include('lib/**/*.rb')
@@ -26,13 +26,14 @@ end
2626
begin
2727
require 'jeweler'
2828
Jeweler::Tasks.new do |gemspec|
29-
gemspec.name = "devise_imapable"
30-
gemspec.summary = "Devise Imap authentication module"
31-
gemspec.description = "For when you don't have access to LDAP"
32-
gemspec.email = "josh.kalderimis@gmail.com"
33-
gemspec.homepage = "http://github.com/joshk/devise_imapable"
34-
gemspec.authors = ["Josh Kalderimis"]
29+
gemspec.name = "devise_ldap_authenticatable"
30+
gemspec.summary = "Devise LDAP authentication module"
31+
gemspec.description = "LDAP 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"]
3535
gemspec.add_runtime_dependency "devise", "> 1.0.4"
36+
gemspec.add_runtime_dependency "net-ldap", "> 0.1.1"
3637
end
3738
Jeweler::GemcutterTasks.new
3839
rescue LoadError

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.5.1
1+
0.1.0

devise_ldap_authenticatable.gemspec

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Generated by jeweler
2+
# DO NOT EDIT THIS FILE DIRECTLY
3+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4+
# -*- encoding: utf-8 -*-
5+
6+
Gem::Specification.new do |s|
7+
s.name = %q{devise_ldap_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 = ["Curtis Schiewek"]
12+
s.date = %q{2010-04-14}
13+
s.description = %q{LDAP module for Devise}
14+
s.email = %q{[email protected]}
15+
s.extra_rdoc_files = [
16+
"README.md"
17+
]
18+
s.files = [
19+
"MIT-LICENSE",
20+
"README.md",
21+
"Rakefile",
22+
"VERSION",
23+
"devise_ldap_authenticatable.gemspec",
24+
"lib/devise_ldap_authenticatable.rb",
25+
"lib/devise_ldap_authenticatable/ldap_adapter.rb",
26+
"lib/devise_ldap_authenticatable/model.rb",
27+
"lib/devise_ldap_authenticatable/routes.rb",
28+
"lib/devise_ldap_authenticatable/schema.rb",
29+
"lib/devise_ldap_authenticatable/strategy.rb",
30+
"rails/init.rb",
31+
"test/devise_ldap_authenticatable_test.rb",
32+
"test/test_helper.rb"
33+
]
34+
s.homepage = %q{http://github.com/cschiewek/devise_ldap_authenticatable}
35+
s.rdoc_options = ["--charset=UTF-8"]
36+
s.require_paths = ["lib"]
37+
s.rubygems_version = %q{1.3.6}
38+
s.summary = %q{Devise LDAP authentication module}
39+
s.test_files = [
40+
"test/devise_ldap_authenticatable_test.rb",
41+
"test/test_helper.rb"
42+
]
43+
44+
s.add_dependency(%q<devise>, ["> 1.0.4"])
45+
s.add_dependency(%q<net-ldap>, ["> 0.1.1"])
46+
end
47+
end
48+

lib/devise_ldap_authenticatable.rb

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
end
17+
18+
# Add ldap_authenticatable strategy to defaults.
19+
#
20+
Devise.add_module(:ldap_authenticatable,
21+
:strategy => true,
22+
:controller => :sessions,
23+
:model => 'devise_ldap_authenticatable/model',
24+
:routes => :ldap_authenticatable)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
ldap = Net::LDAP.new( :host => ::Devise.ldap_host, :port => ::Devise.ldap_port )
11+
if ldap.bind( :method => :simple, :username => login, :password => password )
12+
true
13+
else
14+
false
15+
end
16+
end
17+
18+
end
19+
20+
end
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
require 'devise_imapable/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 protected]', '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] && conditions[:login].include?('@')
39+
conditions[:login] = "#{conditions[:login]}"
40+
end
41+
42+
resource = find_for_ldap_authentication(conditions) || new(conditions)
43+
44+
if resource.try(:valid_ldap_authentication?, attributes[:password])
45+
resource.new_record? ? create(conditions) : resource
46+
end
47+
end
48+
49+
protected
50+
51+
# Find first record based on conditions given (ie by the sign in form).
52+
# Overwrite to add customized conditions, create a join, or maybe use a
53+
# namedscope to filter records while authenticating.
54+
# Example:
55+
#
56+
# def self.find_for_imap_authentication(conditions={})
57+
# conditions[:active] = true
58+
# find(:first, :conditions => conditions)
59+
# end
60+
#
61+
def find_for_ldap_authentication(conditions)
62+
find(:first, :conditions => conditions)
63+
end
64+
end
65+
end
66+
end
67+
end
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Devise::Schema.class_eval do
2+
3+
# Creates login
4+
#
5+
# == Options
6+
# * :null - When true, allow columns to be null.
7+
def ldap_authenticatable(options={})
8+
null = options[:null] || false
9+
10+
apply_schema :login, String, :null => null
11+
end
12+
13+
end
Original file line numberDiff line numberDiff line change
@@ -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)

rails/init.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# Include hook code here
2-
require 'devise_imapable'
2+
require 'devise_ldap_authenticatable'

test/devise_imapable_test.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
require 'test_helper'
22

3-
class DeviseImapableTest < ActiveSupport::TestCase
3+
class DeviseLdapAuthenticatableTest < ActiveSupport::TestCase
44
# Replace this with your real tests.
55
test "the truth" do
66
assert true
77
end
8-
end
8+
end

0 commit comments

Comments
 (0)