To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / .svn / pristine / 28 / 287b739e36a269ed72c54dcd10ebdefa5f23d6ee.svn-base @ 1298:4f746d8966dd
History | View | Annotate | Download (5.33 KB)
| 1 |
# Redmine - project management software |
|---|---|
| 2 |
# Copyright (C) 2006-2012 Jean-Philippe Lang |
| 3 |
# |
| 4 |
# This program is free software; you can redistribute it and/or |
| 5 |
# modify it under the terms of the GNU General Public License |
| 6 |
# as published by the Free Software Foundation; either version 2 |
| 7 |
# of the License, or (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program; if not, write to the Free Software |
| 16 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 |
|
| 18 |
require 'iconv' |
| 19 |
require 'net/ldap' |
| 20 |
require 'net/ldap/dn' |
| 21 |
require 'timeout' |
| 22 |
|
| 23 |
class AuthSourceLdap < AuthSource |
| 24 |
validates_presence_of :host, :port, :attr_login |
| 25 |
validates_length_of :name, :host, :maximum => 60, :allow_nil => true |
| 26 |
validates_length_of :account, :account_password, :base_dn, :filter, :maximum => 255, :allow_blank => true |
| 27 |
validates_length_of :attr_login, :attr_firstname, :attr_lastname, :attr_mail, :maximum => 30, :allow_nil => true |
| 28 |
validates_numericality_of :port, :only_integer => true |
| 29 |
validates_numericality_of :timeout, :only_integer => true, :allow_blank => true |
| 30 |
validate :validate_filter |
| 31 |
|
| 32 |
before_validation :strip_ldap_attributes |
| 33 |
|
| 34 |
def initialize(attributes=nil, *args) |
| 35 |
super |
| 36 |
self.port = 389 if self.port == 0 |
| 37 |
end |
| 38 |
|
| 39 |
def authenticate(login, password) |
| 40 |
return nil if login.blank? || password.blank? |
| 41 |
|
| 42 |
with_timeout do |
| 43 |
attrs = get_user_dn(login, password) |
| 44 |
if attrs && attrs[:dn] && authenticate_dn(attrs[:dn], password) |
| 45 |
logger.debug "Authentication successful for '#{login}'" if logger && logger.debug?
|
| 46 |
return attrs.except(:dn) |
| 47 |
end |
| 48 |
end |
| 49 |
rescue Net::LDAP::LdapError => e |
| 50 |
raise AuthSourceException.new(e.message) |
| 51 |
end |
| 52 |
|
| 53 |
# test the connection to the LDAP |
| 54 |
def test_connection |
| 55 |
with_timeout do |
| 56 |
ldap_con = initialize_ldap_con(self.account, self.account_password) |
| 57 |
ldap_con.open { }
|
| 58 |
end |
| 59 |
rescue Net::LDAP::LdapError => e |
| 60 |
raise AuthSourceException.new(e.message) |
| 61 |
end |
| 62 |
|
| 63 |
def auth_method_name |
| 64 |
"LDAP" |
| 65 |
end |
| 66 |
|
| 67 |
private |
| 68 |
|
| 69 |
def with_timeout(&block) |
| 70 |
timeout = self.timeout |
| 71 |
timeout = 20 unless timeout && timeout > 0 |
| 72 |
Timeout.timeout(timeout) do |
| 73 |
return yield |
| 74 |
end |
| 75 |
rescue Timeout::Error => e |
| 76 |
raise AuthSourceTimeoutException.new(e.message) |
| 77 |
end |
| 78 |
|
| 79 |
def ldap_filter |
| 80 |
if filter.present? |
| 81 |
Net::LDAP::Filter.construct(filter) |
| 82 |
end |
| 83 |
rescue Net::LDAP::LdapError |
| 84 |
nil |
| 85 |
end |
| 86 |
|
| 87 |
def validate_filter |
| 88 |
if filter.present? && ldap_filter.nil? |
| 89 |
errors.add(:filter, :invalid) |
| 90 |
end |
| 91 |
end |
| 92 |
|
| 93 |
def strip_ldap_attributes |
| 94 |
[:attr_login, :attr_firstname, :attr_lastname, :attr_mail].each do |attr| |
| 95 |
write_attribute(attr, read_attribute(attr).strip) unless read_attribute(attr).nil? |
| 96 |
end |
| 97 |
end |
| 98 |
|
| 99 |
def initialize_ldap_con(ldap_user, ldap_password) |
| 100 |
options = { :host => self.host,
|
| 101 |
:port => self.port, |
| 102 |
:encryption => (self.tls ? :simple_tls : nil) |
| 103 |
} |
| 104 |
options.merge!(:auth => { :method => :simple, :username => ldap_user, :password => ldap_password }) unless ldap_user.blank? && ldap_password.blank?
|
| 105 |
Net::LDAP.new options |
| 106 |
end |
| 107 |
|
| 108 |
def get_user_attributes_from_ldap_entry(entry) |
| 109 |
{
|
| 110 |
:dn => entry.dn, |
| 111 |
:firstname => AuthSourceLdap.get_attr(entry, self.attr_firstname), |
| 112 |
:lastname => AuthSourceLdap.get_attr(entry, self.attr_lastname), |
| 113 |
:mail => AuthSourceLdap.get_attr(entry, self.attr_mail), |
| 114 |
:auth_source_id => self.id |
| 115 |
} |
| 116 |
end |
| 117 |
|
| 118 |
# Return the attributes needed for the LDAP search. It will only |
| 119 |
# include the user attributes if on-the-fly registration is enabled |
| 120 |
def search_attributes |
| 121 |
if onthefly_register? |
| 122 |
['dn', self.attr_firstname, self.attr_lastname, self.attr_mail] |
| 123 |
else |
| 124 |
['dn'] |
| 125 |
end |
| 126 |
end |
| 127 |
|
| 128 |
# Check if a DN (user record) authenticates with the password |
| 129 |
def authenticate_dn(dn, password) |
| 130 |
if dn.present? && password.present? |
| 131 |
initialize_ldap_con(dn, password).bind |
| 132 |
end |
| 133 |
end |
| 134 |
|
| 135 |
# Get the user's dn and any attributes for them, given their login |
| 136 |
def get_user_dn(login, password) |
| 137 |
ldap_con = nil |
| 138 |
if self.account && self.account.include?("$login")
|
| 139 |
ldap_con = initialize_ldap_con(self.account.sub("$login", Net::LDAP::DN.escape(login)), password)
|
| 140 |
else |
| 141 |
ldap_con = initialize_ldap_con(self.account, self.account_password) |
| 142 |
end |
| 143 |
login_filter = Net::LDAP::Filter.eq( self.attr_login, login ) |
| 144 |
object_filter = Net::LDAP::Filter.eq( "objectClass", "*" ) |
| 145 |
attrs = {}
|
| 146 |
|
| 147 |
search_filter = object_filter & login_filter |
| 148 |
if f = ldap_filter |
| 149 |
search_filter = search_filter & f |
| 150 |
end |
| 151 |
|
| 152 |
ldap_con.search( :base => self.base_dn, |
| 153 |
:filter => search_filter, |
| 154 |
:attributes=> search_attributes) do |entry| |
| 155 |
|
| 156 |
if onthefly_register? |
| 157 |
attrs = get_user_attributes_from_ldap_entry(entry) |
| 158 |
else |
| 159 |
attrs = {:dn => entry.dn}
|
| 160 |
end |
| 161 |
|
| 162 |
logger.debug "DN found for #{login}: #{attrs[:dn]}" if logger && logger.debug?
|
| 163 |
end |
| 164 |
|
| 165 |
attrs |
| 166 |
end |
| 167 |
|
| 168 |
def self.get_attr(entry, attr_name) |
| 169 |
if !attr_name.blank? |
| 170 |
entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name] |
| 171 |
end |
| 172 |
end |
| 173 |
end |