##// END OF EJS Templates
Added LDAPS support for LDAP authentication....
Jean-Philippe Lang -
r831:4c509c9423a1
parent child
Show More
@@ -1,80 +1,81
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_presence_of :attr_firstname, :attr_lastname, :attr_mail, :if => Proc.new { |a| a.onthefly_register? }
24 24
25 25 def after_initialize
26 26 self.port = 389 if self.port == 0
27 27 end
28 28
29 29 def authenticate(login, password)
30 30 attrs = []
31 31 # get user's DN
32 32 ldap_con = initialize_ldap_con(self.account, self.account_password)
33 33 login_filter = Net::LDAP::Filter.eq( self.attr_login, login )
34 34 object_filter = Net::LDAP::Filter.eq( "objectClass", "*" )
35 35 dn = String.new
36 36 ldap_con.search( :base => self.base_dn,
37 37 :filter => object_filter & login_filter,
38 38 :attributes=> ['dn', self.attr_firstname, self.attr_lastname, self.attr_mail]) do |entry|
39 39 dn = entry.dn
40 40 attrs = [:firstname => AuthSourceLdap.get_attr(entry, self.attr_firstname),
41 41 :lastname => AuthSourceLdap.get_attr(entry, self.attr_lastname),
42 42 :mail => AuthSourceLdap.get_attr(entry, self.attr_mail),
43 43 :auth_source_id => self.id ]
44 44 end
45 45 return nil if dn.empty?
46 46 logger.debug "DN found for #{login}: #{dn}" if logger && logger.debug?
47 47 # authenticate user
48 48 ldap_con = initialize_ldap_con(dn, password)
49 49 return nil unless ldap_con.bind
50 50 # return user's attributes
51 51 logger.debug "Authentication successful for '#{login}'" if logger && logger.debug?
52 52 attrs
53 53 rescue Net::LDAP::LdapError => text
54 54 raise "LdapError: " + text
55 55 end
56 56
57 57 # test the connection to the LDAP
58 58 def test_connection
59 59 ldap_con = initialize_ldap_con(self.account, self.account_password)
60 60 ldap_con.open { }
61 61 rescue Net::LDAP::LdapError => text
62 62 raise "LdapError: " + text
63 63 end
64 64
65 65 def auth_method_name
66 66 "LDAP"
67 67 end
68 68
69 69 private
70 70 def initialize_ldap_con(ldap_user, ldap_password)
71 71 Net::LDAP.new( {:host => self.host,
72 72 :port => self.port,
73 :auth => { :method => :simple, :username => ldap_user, :password => ldap_password }}
73 :auth => { :method => :simple, :username => ldap_user, :password => ldap_password },
74 :encryption => (self.tls ? :simple_tls : nil)}
74 75 )
75 76 end
76 77
77 78 def self.get_attr(entry, attr_name)
78 79 entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name]
79 80 end
80 81 end
@@ -1,45 +1,45
1 1 <%= error_messages_for 'auth_source' %>
2 2
3 3 <div class="box">
4 4 <!--[form:auth_source]-->
5 5 <p><label for="auth_source_name"><%=l(:field_name)%> <span class="required">*</span></label>
6 6 <%= text_field 'auth_source', 'name' %></p>
7 7
8 8 <p><label for="auth_source_host"><%=l(:field_host)%> <span class="required">*</span></label>
9 9 <%= text_field 'auth_source', 'host' %></p>
10 10
11 11 <p><label for="auth_source_port"><%=l(:field_port)%> <span class="required">*</span></label>
12 <%= text_field 'auth_source', 'port', :size => 6 %></p>
12 <%= text_field 'auth_source', 'port', :size => 6 %> <%= check_box 'auth_source', 'tls' %> LDAPS</p>
13 13
14 14 <p><label for="auth_source_account"><%=l(:field_account)%></label>
15 15 <%= text_field 'auth_source', 'account' %></p>
16 16
17 17 <p><label for="auth_source_account_password"><%=l(:field_password)%></label>
18 18 <%= password_field 'auth_source', 'account_password' %></p>
19 19
20 20 <p><label for="auth_source_base_dn"><%=l(:field_base_dn)%> <span class="required">*</span></label>
21 21 <%= text_field 'auth_source', 'base_dn', :size => 60 %></p>
22 22 </div>
23 23
24 24 <div class="box">
25 25 <p><label for="auth_source_onthefly_register"><%=l(:field_onthefly)%></label>
26 26 <%= check_box 'auth_source', 'onthefly_register' %></p>
27 27
28 28 <p>
29 29 <fieldset><legend><%=l(:label_attribute_plural)%></legend>
30 30 <p><label for="auth_source_attr_login"><%=l(:field_login)%> <span class="required">*</span></label>
31 31 <%= text_field 'auth_source', 'attr_login', :size => 20 %></p>
32 32
33 33 <p><label for="auth_source_attr_firstname"><%=l(:field_firstname)%></label>
34 34 <%= text_field 'auth_source', 'attr_firstname', :size => 20 %></p>
35 35
36 36 <p><label for="auth_source_attr_lastname"><%=l(:field_lastname)%></label>
37 37 <%= text_field 'auth_source', 'attr_lastname', :size => 20 %></p>
38 38
39 39 <p><label for="auth_source_attr_mail"><%=l(:field_mail)%></label>
40 40 <%= text_field 'auth_source', 'attr_mail', :size => 20 %></p>
41 41 </fieldset>
42 42 </p>
43 43 </div>
44 44 <!--[eoform:auth_source]-->
45 45
General Comments 0
You need to be logged in to leave comments. Login now