##// END OF EJS Templates
Refactor: Rewrite authenticate_dn to use an implicit return....
Eric Davis -
r3336:6f930e9be647
parent child
Show More
@@ -1,113 +1,110
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 logger.debug "DN found for #{login}: #{dn}" if logger && logger.debug?
49 49
50 50 end
51 51
52 52 if authenticate_dn(dn, password)
53 53 logger.debug "Authentication successful for '#{login}'" if logger && logger.debug?
54 54 return attrs
55 else
56 return nil
57 55 end
58 56 rescue Net::LDAP::LdapError => text
59 57 raise "LdapError: " + text
60 58 end
61 59
62 60 # test the connection to the LDAP
63 61 def test_connection
64 62 ldap_con = initialize_ldap_con(self.account, self.account_password)
65 63 ldap_con.open { }
66 64 rescue Net::LDAP::LdapError => text
67 65 raise "LdapError: " + text
68 66 end
69 67
70 68 def auth_method_name
71 69 "LDAP"
72 70 end
73 71
74 72 private
75 73
76 74 def strip_ldap_attributes
77 75 [:attr_login, :attr_firstname, :attr_lastname, :attr_mail].each do |attr|
78 76 write_attribute(attr, read_attribute(attr).strip) unless read_attribute(attr).nil?
79 77 end
80 78 end
81 79
82 80 def initialize_ldap_con(ldap_user, ldap_password)
83 81 options = { :host => self.host,
84 82 :port => self.port,
85 83 :encryption => (self.tls ? :simple_tls : nil)
86 84 }
87 85 options.merge!(:auth => { :method => :simple, :username => ldap_user, :password => ldap_password }) unless ldap_user.blank? && ldap_password.blank?
88 86 Net::LDAP.new options
89 87 end
90 88
91 89 def get_user_attributes_from_ldap_entry(entry)
92 90 [
93 91 :firstname => AuthSourceLdap.get_attr(entry, self.attr_firstname),
94 92 :lastname => AuthSourceLdap.get_attr(entry, self.attr_lastname),
95 93 :mail => AuthSourceLdap.get_attr(entry, self.attr_mail),
96 94 :auth_source_id => self.id
97 95 ]
98 96 end
99 97
100 98 # Check if a DN (user record) authenticates with the password
101 99 def authenticate_dn(dn, password)
102 return nil if dn.empty?
103
104 ldap_con = initialize_ldap_con(dn, password)
105 return ldap_con.bind
100 if dn.present? && password.present?
101 initialize_ldap_con(dn, password).bind
102 end
106 103 end
107 104
108 105 def self.get_attr(entry, attr_name)
109 106 if !attr_name.blank?
110 107 entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name]
111 108 end
112 109 end
113 110 end
General Comments 0
You need to be logged in to leave comments. Login now