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