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