@@ -1,98 +1,105 | |||||
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 = [:firstname => AuthSourceLdap.get_attr(entry, self.attr_firstname), |
|
47 | attrs = get_user_attributes_from_ldap_entry(entry) if onthefly_register? | |
48 | :lastname => AuthSourceLdap.get_attr(entry, self.attr_lastname), |
|
48 | ||
49 | :mail => AuthSourceLdap.get_attr(entry, self.attr_mail), |
|
|||
50 | :auth_source_id => self.id ] if onthefly_register? |
|
|||
51 | end |
|
49 | end | |
52 | return nil if dn.empty? |
|
50 | return nil if dn.empty? | |
53 | logger.debug "DN found for #{login}: #{dn}" if logger && logger.debug? |
|
51 | logger.debug "DN found for #{login}: #{dn}" if logger && logger.debug? | |
54 | # authenticate user |
|
52 | # authenticate user | |
55 | ldap_con = initialize_ldap_con(dn, password) |
|
53 | ldap_con = initialize_ldap_con(dn, password) | |
56 | return nil unless ldap_con.bind |
|
54 | return nil unless ldap_con.bind | |
57 | # return user's attributes |
|
55 | # return user's attributes | |
58 | logger.debug "Authentication successful for '#{login}'" if logger && logger.debug? |
|
56 | logger.debug "Authentication successful for '#{login}'" if logger && logger.debug? | |
59 | attrs |
|
57 | attrs | |
60 | rescue Net::LDAP::LdapError => text |
|
58 | rescue Net::LDAP::LdapError => text | |
61 | raise "LdapError: " + text |
|
59 | raise "LdapError: " + text | |
62 | end |
|
60 | end | |
63 |
|
61 | |||
64 | # test the connection to the LDAP |
|
62 | # test the connection to the LDAP | |
65 | def test_connection |
|
63 | def test_connection | |
66 | ldap_con = initialize_ldap_con(self.account, self.account_password) |
|
64 | ldap_con = initialize_ldap_con(self.account, self.account_password) | |
67 | ldap_con.open { } |
|
65 | ldap_con.open { } | |
68 | rescue Net::LDAP::LdapError => text |
|
66 | rescue Net::LDAP::LdapError => text | |
69 | raise "LdapError: " + text |
|
67 | raise "LdapError: " + text | |
70 | end |
|
68 | end | |
71 |
|
69 | |||
72 | def auth_method_name |
|
70 | def auth_method_name | |
73 | "LDAP" |
|
71 | "LDAP" | |
74 | end |
|
72 | end | |
75 |
|
73 | |||
76 | private |
|
74 | private | |
77 |
|
75 | |||
78 | def strip_ldap_attributes |
|
76 | def strip_ldap_attributes | |
79 | [:attr_login, :attr_firstname, :attr_lastname, :attr_mail].each do |attr| |
|
77 | [:attr_login, :attr_firstname, :attr_lastname, :attr_mail].each do |attr| | |
80 | write_attribute(attr, read_attribute(attr).strip) unless read_attribute(attr).nil? |
|
78 | write_attribute(attr, read_attribute(attr).strip) unless read_attribute(attr).nil? | |
81 | end |
|
79 | end | |
82 | end |
|
80 | end | |
83 |
|
81 | |||
84 | def initialize_ldap_con(ldap_user, ldap_password) |
|
82 | def initialize_ldap_con(ldap_user, ldap_password) | |
85 | options = { :host => self.host, |
|
83 | options = { :host => self.host, | |
86 | :port => self.port, |
|
84 | :port => self.port, | |
87 | :encryption => (self.tls ? :simple_tls : nil) |
|
85 | :encryption => (self.tls ? :simple_tls : nil) | |
88 | } |
|
86 | } | |
89 | options.merge!(:auth => { :method => :simple, :username => ldap_user, :password => ldap_password }) unless ldap_user.blank? && ldap_password.blank? |
|
87 | options.merge!(:auth => { :method => :simple, :username => ldap_user, :password => ldap_password }) unless ldap_user.blank? && ldap_password.blank? | |
90 | Net::LDAP.new options |
|
88 | Net::LDAP.new options | |
91 | end |
|
89 | end | |
92 |
|
90 | |||
|
91 | def get_user_attributes_from_ldap_entry(entry) | |||
|
92 | [ | |||
|
93 | :firstname => AuthSourceLdap.get_attr(entry, self.attr_firstname), | |||
|
94 | :lastname => AuthSourceLdap.get_attr(entry, self.attr_lastname), | |||
|
95 | :mail => AuthSourceLdap.get_attr(entry, self.attr_mail), | |||
|
96 | :auth_source_id => self.id | |||
|
97 | ] | |||
|
98 | end | |||
|
99 | ||||
93 | def self.get_attr(entry, attr_name) |
|
100 | def self.get_attr(entry, attr_name) | |
94 | if !attr_name.blank? |
|
101 | if !attr_name.blank? | |
95 | entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name] |
|
102 | entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name] | |
96 | end |
|
103 | end | |
97 | end |
|
104 | end | |
98 | end |
|
105 | end |
@@ -1,94 +1,97 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2008 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2008 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 File.dirname(__FILE__) + '/../test_helper' |
|
18 | require File.dirname(__FILE__) + '/../test_helper' | |
19 |
|
19 | |||
20 | class AuthSourceLdapTest < ActiveSupport::TestCase |
|
20 | class AuthSourceLdapTest < ActiveSupport::TestCase | |
21 |
|
21 | |||
22 | def setup |
|
22 | def setup | |
23 | end |
|
23 | end | |
24 |
|
24 | |||
25 | def test_create |
|
25 | def test_create | |
26 | a = AuthSourceLdap.new(:name => 'My LDAP', :host => 'ldap.example.net', :port => 389, :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName') |
|
26 | a = AuthSourceLdap.new(:name => 'My LDAP', :host => 'ldap.example.net', :port => 389, :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName') | |
27 | assert a.save |
|
27 | assert a.save | |
28 | end |
|
28 | end | |
29 |
|
29 | |||
30 | def test_should_strip_ldap_attributes |
|
30 | def test_should_strip_ldap_attributes | |
31 | a = AuthSourceLdap.new(:name => 'My LDAP', :host => 'ldap.example.net', :port => 389, :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName', |
|
31 | a = AuthSourceLdap.new(:name => 'My LDAP', :host => 'ldap.example.net', :port => 389, :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName', | |
32 | :attr_firstname => 'givenName ') |
|
32 | :attr_firstname => 'givenName ') | |
33 | assert a.save |
|
33 | assert a.save | |
34 | assert_equal 'givenName', a.reload.attr_firstname |
|
34 | assert_equal 'givenName', a.reload.attr_firstname | |
35 | end |
|
35 | end | |
36 |
|
36 | |||
37 | if ldap_configured? |
|
37 | if ldap_configured? | |
38 | context '#authenticate' do |
|
38 | context '#authenticate' do | |
39 | setup do |
|
39 | setup do | |
40 | @auth = AuthSourceLdap.generate!(:name => 'on the fly', |
|
40 | @auth = AuthSourceLdap.generate!(:name => 'on the fly', | |
41 | :host => '127.0.0.1', |
|
41 | :host => '127.0.0.1', | |
42 | :port => 389, |
|
42 | :port => 389, | |
43 | :base_dn => 'OU=Person,DC=redmine,DC=org', |
|
43 | :base_dn => 'OU=Person,DC=redmine,DC=org', | |
44 | :attr_login => 'uid', |
|
44 | :attr_login => 'uid', | |
45 | :attr_firstname => 'givenName', |
|
45 | :attr_firstname => 'givenName', | |
46 | :attr_lastname => 'sn', |
|
46 | :attr_lastname => 'sn', | |
47 | :attr_mail => 'mail', |
|
47 | :attr_mail => 'mail', | |
48 | :onthefly_register => true) |
|
48 | :onthefly_register => true) | |
49 |
|
49 | |||
50 | end |
|
50 | end | |
51 |
|
51 | |||
52 | context 'with a valid LDAP user' do |
|
52 | context 'with a valid LDAP user' do | |
53 | should 'return the firstname user attributes' do |
|
53 | should 'return the firstname user attributes' do | |
54 | response = @auth.authenticate('example1','123456') |
|
54 | response = @auth.authenticate('example1','123456') | |
55 | assert response |
|
55 | assert response.is_a?(Array), "An array was not returned" | |
|
56 | assert response.first.present?, "No user data returned" | |||
56 | assert_equal 'Example', response.first[:firstname] |
|
57 | assert_equal 'Example', response.first[:firstname] | |
57 | end |
|
58 | end | |
58 |
|
59 | |||
59 | should 'return the lastname user attributes' do |
|
60 | should 'return the lastname user attributes' do | |
60 | response = @auth.authenticate('example1','123456') |
|
61 | response = @auth.authenticate('example1','123456') | |
61 | assert response |
|
62 | assert response.is_a?(Array), "An array was not returned" | |
|
63 | assert response.first.present?, "No user data returned" | |||
62 | assert_equal 'One', response.first[:lastname] |
|
64 | assert_equal 'One', response.first[:lastname] | |
63 | end |
|
65 | end | |
64 |
|
66 | |||
65 | should 'return mail user attributes' do |
|
67 | should 'return mail user attributes' do | |
66 | response = @auth.authenticate('example1','123456') |
|
68 | response = @auth.authenticate('example1','123456') | |
67 | assert response |
|
69 | assert response.is_a?(Array), "An array was not returned" | |
|
70 | assert response.first.present?, "No user data returned" | |||
68 | assert_equal 'example1@redmine.org', response.first[:mail] |
|
71 | assert_equal 'example1@redmine.org', response.first[:mail] | |
69 | end |
|
72 | end | |
70 | end |
|
73 | end | |
71 |
|
74 | |||
72 | context 'with an invalid LDAP user' do |
|
75 | context 'with an invalid LDAP user' do | |
73 | should 'return nil' do |
|
76 | should 'return nil' do | |
74 | assert_equal nil, @auth.authenticate('nouser','123456') |
|
77 | assert_equal nil, @auth.authenticate('nouser','123456') | |
75 | end |
|
78 | end | |
76 | end |
|
79 | end | |
77 |
|
80 | |||
78 | context 'without a login' do |
|
81 | context 'without a login' do | |
79 | should 'return nil' do |
|
82 | should 'return nil' do | |
80 | assert_equal nil, @auth.authenticate('','123456') |
|
83 | assert_equal nil, @auth.authenticate('','123456') | |
81 | end |
|
84 | end | |
82 | end |
|
85 | end | |
83 |
|
86 | |||
84 | context 'without a password' do |
|
87 | context 'without a password' do | |
85 | should 'return nil' do |
|
88 | should 'return nil' do | |
86 | assert_equal nil, @auth.authenticate('edavis','') |
|
89 | assert_equal nil, @auth.authenticate('edavis','') | |
87 | end |
|
90 | end | |
88 | end |
|
91 | end | |
89 |
|
92 | |||
90 | end |
|
93 | end | |
91 | else |
|
94 | else | |
92 | puts '(Test LDAP server not configured)' |
|
95 | puts '(Test LDAP server not configured)' | |
93 | end |
|
96 | end | |
94 | end |
|
97 | end |
General Comments 0
You need to be logged in to leave comments.
Login now