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