@@ -1,82 +1,83 | |||||
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_presence_of :attr_firstname, :attr_lastname, :attr_mail, :if => Proc.new { |a| a.onthefly_register? } |
|
23 | validates_presence_of :attr_firstname, :attr_lastname, :attr_mail, :if => Proc.new { |a| a.onthefly_register? } | |
24 |
|
24 | |||
25 | def after_initialize |
|
25 | def after_initialize | |
26 | self.port = 389 if self.port == 0 |
|
26 | self.port = 389 if self.port == 0 | |
27 | end |
|
27 | end | |
28 |
|
28 | |||
29 | def authenticate(login, password) |
|
29 | def authenticate(login, password) | |
|
30 | return nil if login.blank? || password.blank? | |||
30 | attrs = [] |
|
31 | attrs = [] | |
31 | # get user's DN |
|
32 | # get user's DN | |
32 | ldap_con = initialize_ldap_con(self.account, self.account_password) |
|
33 | ldap_con = initialize_ldap_con(self.account, self.account_password) | |
33 | login_filter = Net::LDAP::Filter.eq( self.attr_login, login ) |
|
34 | login_filter = Net::LDAP::Filter.eq( self.attr_login, login ) | |
34 | object_filter = Net::LDAP::Filter.eq( "objectClass", "*" ) |
|
35 | object_filter = Net::LDAP::Filter.eq( "objectClass", "*" ) | |
35 | dn = String.new |
|
36 | dn = String.new | |
36 | ldap_con.search( :base => self.base_dn, |
|
37 | ldap_con.search( :base => self.base_dn, | |
37 | :filter => object_filter & login_filter, |
|
38 | :filter => object_filter & login_filter, | |
38 | # only ask for the DN if on-the-fly registration is disabled |
|
39 | # only ask for the DN if on-the-fly registration is disabled | |
39 | :attributes=> (onthefly_register? ? ['dn', self.attr_firstname, self.attr_lastname, self.attr_mail] : ['dn'])) do |entry| |
|
40 | :attributes=> (onthefly_register? ? ['dn', self.attr_firstname, self.attr_lastname, self.attr_mail] : ['dn'])) do |entry| | |
40 | dn = entry.dn |
|
41 | dn = entry.dn | |
41 | attrs = [:firstname => AuthSourceLdap.get_attr(entry, self.attr_firstname), |
|
42 | attrs = [:firstname => AuthSourceLdap.get_attr(entry, self.attr_firstname), | |
42 | :lastname => AuthSourceLdap.get_attr(entry, self.attr_lastname), |
|
43 | :lastname => AuthSourceLdap.get_attr(entry, self.attr_lastname), | |
43 | :mail => AuthSourceLdap.get_attr(entry, self.attr_mail), |
|
44 | :mail => AuthSourceLdap.get_attr(entry, self.attr_mail), | |
44 | :auth_source_id => self.id ] if onthefly_register? |
|
45 | :auth_source_id => self.id ] if onthefly_register? | |
45 | end |
|
46 | end | |
46 | return nil if dn.empty? |
|
47 | return nil if dn.empty? | |
47 | logger.debug "DN found for #{login}: #{dn}" if logger && logger.debug? |
|
48 | logger.debug "DN found for #{login}: #{dn}" if logger && logger.debug? | |
48 | # authenticate user |
|
49 | # authenticate user | |
49 | ldap_con = initialize_ldap_con(dn, password) |
|
50 | ldap_con = initialize_ldap_con(dn, password) | |
50 | return nil unless ldap_con.bind |
|
51 | return nil unless ldap_con.bind | |
51 | # return user's attributes |
|
52 | # return user's attributes | |
52 | logger.debug "Authentication successful for '#{login}'" if logger && logger.debug? |
|
53 | logger.debug "Authentication successful for '#{login}'" if logger && logger.debug? | |
53 | attrs |
|
54 | attrs | |
54 | rescue Net::LDAP::LdapError => text |
|
55 | rescue Net::LDAP::LdapError => text | |
55 | raise "LdapError: " + text |
|
56 | raise "LdapError: " + text | |
56 | end |
|
57 | end | |
57 |
|
58 | |||
58 | # test the connection to the LDAP |
|
59 | # test the connection to the LDAP | |
59 | def test_connection |
|
60 | def test_connection | |
60 | ldap_con = initialize_ldap_con(self.account, self.account_password) |
|
61 | ldap_con = initialize_ldap_con(self.account, self.account_password) | |
61 | ldap_con.open { } |
|
62 | ldap_con.open { } | |
62 | rescue Net::LDAP::LdapError => text |
|
63 | rescue Net::LDAP::LdapError => text | |
63 | raise "LdapError: " + text |
|
64 | raise "LdapError: " + text | |
64 | end |
|
65 | end | |
65 |
|
66 | |||
66 | def auth_method_name |
|
67 | def auth_method_name | |
67 | "LDAP" |
|
68 | "LDAP" | |
68 | end |
|
69 | end | |
69 |
|
70 | |||
70 | private |
|
71 | private | |
71 | def initialize_ldap_con(ldap_user, ldap_password) |
|
72 | def initialize_ldap_con(ldap_user, ldap_password) | |
72 | Net::LDAP.new( {:host => self.host, |
|
73 | Net::LDAP.new( {:host => self.host, | |
73 | :port => self.port, |
|
74 | :port => self.port, | |
74 | :auth => { :method => :simple, :username => ldap_user, :password => ldap_password }, |
|
75 | :auth => { :method => :simple, :username => ldap_user, :password => ldap_password }, | |
75 | :encryption => (self.tls ? :simple_tls : nil)} |
|
76 | :encryption => (self.tls ? :simple_tls : nil)} | |
76 | ) |
|
77 | ) | |
77 | end |
|
78 | end | |
78 |
|
79 | |||
79 | def self.get_attr(entry, attr_name) |
|
80 | def self.get_attr(entry, attr_name) | |
80 | entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name] |
|
81 | entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name] | |
81 | end |
|
82 | end | |
82 | end |
|
83 | end |
General Comments 0
You need to be logged in to leave comments.
Login now