##// END OF EJS Templates
Refactor: extract an #authenticate_dn method in AuthSourceLdap...
Eric Davis -
r3327:4f268c560641
parent child
Show More
@@ -1,105 +1,112
1 1 # redMine - project management software
2 2 # Copyright (C) 2006 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require 'net/ldap'
19 19 require 'iconv'
20 20
21 21 class AuthSourceLdap < AuthSource
22 22 validates_presence_of :host, :port, :attr_login
23 23 validates_length_of :name, :host, :account_password, :maximum => 60, :allow_nil => true
24 24 validates_length_of :account, :base_dn, :maximum => 255, :allow_nil => true
25 25 validates_length_of :attr_login, :attr_firstname, :attr_lastname, :attr_mail, :maximum => 30, :allow_nil => true
26 26 validates_numericality_of :port, :only_integer => true
27 27
28 28 before_validation :strip_ldap_attributes
29 29
30 30 def after_initialize
31 31 self.port = 389 if self.port == 0
32 32 end
33 33
34 34 def authenticate(login, password)
35 35 return nil if login.blank? || password.blank?
36 36 attrs = []
37 37 # get user's DN
38 38 ldap_con = initialize_ldap_con(self.account, self.account_password)
39 39 login_filter = Net::LDAP::Filter.eq( self.attr_login, login )
40 40 object_filter = Net::LDAP::Filter.eq( "objectClass", "*" )
41 41 dn = String.new
42 42 ldap_con.search( :base => self.base_dn,
43 43 :filter => object_filter & login_filter,
44 44 # only ask for the DN if on-the-fly registration is disabled
45 45 :attributes=> (onthefly_register? ? ['dn', self.attr_firstname, self.attr_lastname, self.attr_mail] : ['dn'])) do |entry|
46 46 dn = entry.dn
47 47 attrs = get_user_attributes_from_ldap_entry(entry) if onthefly_register?
48 48
49 49 end
50 50 return nil if dn.empty?
51 51 logger.debug "DN found for #{login}: #{dn}" if logger && logger.debug?
52 # authenticate user
53 ldap_con = initialize_ldap_con(dn, password)
54 return nil unless ldap_con.bind
55 # return user's attributes
56 logger.debug "Authentication successful for '#{login}'" if logger && logger.debug?
57 attrs
52
53 if authenticate_dn(dn, password)
54 logger.debug "Authentication successful for '#{login}'" if logger && logger.debug?
55 return attrs
56 else
57 return nil
58 end
58 59 rescue Net::LDAP::LdapError => text
59 60 raise "LdapError: " + text
60 61 end
61 62
62 63 # test the connection to the LDAP
63 64 def test_connection
64 65 ldap_con = initialize_ldap_con(self.account, self.account_password)
65 66 ldap_con.open { }
66 67 rescue Net::LDAP::LdapError => text
67 68 raise "LdapError: " + text
68 69 end
69 70
70 71 def auth_method_name
71 72 "LDAP"
72 73 end
73 74
74 75 private
75 76
76 77 def strip_ldap_attributes
77 78 [:attr_login, :attr_firstname, :attr_lastname, :attr_mail].each do |attr|
78 79 write_attribute(attr, read_attribute(attr).strip) unless read_attribute(attr).nil?
79 80 end
80 81 end
81 82
82 83 def initialize_ldap_con(ldap_user, ldap_password)
83 84 options = { :host => self.host,
84 85 :port => self.port,
85 86 :encryption => (self.tls ? :simple_tls : nil)
86 87 }
87 88 options.merge!(:auth => { :method => :simple, :username => ldap_user, :password => ldap_password }) unless ldap_user.blank? && ldap_password.blank?
88 89 Net::LDAP.new options
89 90 end
90 91
91 92 def get_user_attributes_from_ldap_entry(entry)
92 93 [
93 94 :firstname => AuthSourceLdap.get_attr(entry, self.attr_firstname),
94 95 :lastname => AuthSourceLdap.get_attr(entry, self.attr_lastname),
95 96 :mail => AuthSourceLdap.get_attr(entry, self.attr_mail),
96 97 :auth_source_id => self.id
97 98 ]
98 99 end
100
101 # Check if a DN (user record) authenticates with the password
102 def authenticate_dn(dn, password)
103 ldap_con = initialize_ldap_con(dn, password)
104 return ldap_con.bind
105 end
99 106
100 107 def self.get_attr(entry, attr_name)
101 108 if !attr_name.blank?
102 109 entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name]
103 110 end
104 111 end
105 112 end
General Comments 0
You need to be logged in to leave comments. Login now