annotate .svn/pristine/28/287b739e36a269ed72c54dcd10ebdefa5f23d6ee.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children
rev   line source
Chris@1295 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@1295 3 #
Chris@1295 4 # This program is free software; you can redistribute it and/or
Chris@1295 5 # modify it under the terms of the GNU General Public License
Chris@1295 6 # as published by the Free Software Foundation; either version 2
Chris@1295 7 # of the License, or (at your option) any later version.
Chris@1295 8 #
Chris@1295 9 # This program is distributed in the hope that it will be useful,
Chris@1295 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1295 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1295 12 # GNU General Public License for more details.
Chris@1295 13 #
Chris@1295 14 # You should have received a copy of the GNU General Public License
Chris@1295 15 # along with this program; if not, write to the Free Software
Chris@1295 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1295 17
Chris@1295 18 require 'iconv'
Chris@1295 19 require 'net/ldap'
Chris@1295 20 require 'net/ldap/dn'
Chris@1295 21 require 'timeout'
Chris@1295 22
Chris@1295 23 class AuthSourceLdap < AuthSource
Chris@1295 24 validates_presence_of :host, :port, :attr_login
Chris@1295 25 validates_length_of :name, :host, :maximum => 60, :allow_nil => true
Chris@1295 26 validates_length_of :account, :account_password, :base_dn, :filter, :maximum => 255, :allow_blank => true
Chris@1295 27 validates_length_of :attr_login, :attr_firstname, :attr_lastname, :attr_mail, :maximum => 30, :allow_nil => true
Chris@1295 28 validates_numericality_of :port, :only_integer => true
Chris@1295 29 validates_numericality_of :timeout, :only_integer => true, :allow_blank => true
Chris@1295 30 validate :validate_filter
Chris@1295 31
Chris@1295 32 before_validation :strip_ldap_attributes
Chris@1295 33
Chris@1295 34 def initialize(attributes=nil, *args)
Chris@1295 35 super
Chris@1295 36 self.port = 389 if self.port == 0
Chris@1295 37 end
Chris@1295 38
Chris@1295 39 def authenticate(login, password)
Chris@1295 40 return nil if login.blank? || password.blank?
Chris@1295 41
Chris@1295 42 with_timeout do
Chris@1295 43 attrs = get_user_dn(login, password)
Chris@1295 44 if attrs && attrs[:dn] && authenticate_dn(attrs[:dn], password)
Chris@1295 45 logger.debug "Authentication successful for '#{login}'" if logger && logger.debug?
Chris@1295 46 return attrs.except(:dn)
Chris@1295 47 end
Chris@1295 48 end
Chris@1295 49 rescue Net::LDAP::LdapError => e
Chris@1295 50 raise AuthSourceException.new(e.message)
Chris@1295 51 end
Chris@1295 52
Chris@1295 53 # test the connection to the LDAP
Chris@1295 54 def test_connection
Chris@1295 55 with_timeout do
Chris@1295 56 ldap_con = initialize_ldap_con(self.account, self.account_password)
Chris@1295 57 ldap_con.open { }
Chris@1295 58 end
Chris@1295 59 rescue Net::LDAP::LdapError => e
Chris@1295 60 raise AuthSourceException.new(e.message)
Chris@1295 61 end
Chris@1295 62
Chris@1295 63 def auth_method_name
Chris@1295 64 "LDAP"
Chris@1295 65 end
Chris@1295 66
Chris@1295 67 private
Chris@1295 68
Chris@1295 69 def with_timeout(&block)
Chris@1295 70 timeout = self.timeout
Chris@1295 71 timeout = 20 unless timeout && timeout > 0
Chris@1295 72 Timeout.timeout(timeout) do
Chris@1295 73 return yield
Chris@1295 74 end
Chris@1295 75 rescue Timeout::Error => e
Chris@1295 76 raise AuthSourceTimeoutException.new(e.message)
Chris@1295 77 end
Chris@1295 78
Chris@1295 79 def ldap_filter
Chris@1295 80 if filter.present?
Chris@1295 81 Net::LDAP::Filter.construct(filter)
Chris@1295 82 end
Chris@1295 83 rescue Net::LDAP::LdapError
Chris@1295 84 nil
Chris@1295 85 end
Chris@1295 86
Chris@1295 87 def validate_filter
Chris@1295 88 if filter.present? && ldap_filter.nil?
Chris@1295 89 errors.add(:filter, :invalid)
Chris@1295 90 end
Chris@1295 91 end
Chris@1295 92
Chris@1295 93 def strip_ldap_attributes
Chris@1295 94 [:attr_login, :attr_firstname, :attr_lastname, :attr_mail].each do |attr|
Chris@1295 95 write_attribute(attr, read_attribute(attr).strip) unless read_attribute(attr).nil?
Chris@1295 96 end
Chris@1295 97 end
Chris@1295 98
Chris@1295 99 def initialize_ldap_con(ldap_user, ldap_password)
Chris@1295 100 options = { :host => self.host,
Chris@1295 101 :port => self.port,
Chris@1295 102 :encryption => (self.tls ? :simple_tls : nil)
Chris@1295 103 }
Chris@1295 104 options.merge!(:auth => { :method => :simple, :username => ldap_user, :password => ldap_password }) unless ldap_user.blank? && ldap_password.blank?
Chris@1295 105 Net::LDAP.new options
Chris@1295 106 end
Chris@1295 107
Chris@1295 108 def get_user_attributes_from_ldap_entry(entry)
Chris@1295 109 {
Chris@1295 110 :dn => entry.dn,
Chris@1295 111 :firstname => AuthSourceLdap.get_attr(entry, self.attr_firstname),
Chris@1295 112 :lastname => AuthSourceLdap.get_attr(entry, self.attr_lastname),
Chris@1295 113 :mail => AuthSourceLdap.get_attr(entry, self.attr_mail),
Chris@1295 114 :auth_source_id => self.id
Chris@1295 115 }
Chris@1295 116 end
Chris@1295 117
Chris@1295 118 # Return the attributes needed for the LDAP search. It will only
Chris@1295 119 # include the user attributes if on-the-fly registration is enabled
Chris@1295 120 def search_attributes
Chris@1295 121 if onthefly_register?
Chris@1295 122 ['dn', self.attr_firstname, self.attr_lastname, self.attr_mail]
Chris@1295 123 else
Chris@1295 124 ['dn']
Chris@1295 125 end
Chris@1295 126 end
Chris@1295 127
Chris@1295 128 # Check if a DN (user record) authenticates with the password
Chris@1295 129 def authenticate_dn(dn, password)
Chris@1295 130 if dn.present? && password.present?
Chris@1295 131 initialize_ldap_con(dn, password).bind
Chris@1295 132 end
Chris@1295 133 end
Chris@1295 134
Chris@1295 135 # Get the user's dn and any attributes for them, given their login
Chris@1295 136 def get_user_dn(login, password)
Chris@1295 137 ldap_con = nil
Chris@1295 138 if self.account && self.account.include?("$login")
Chris@1295 139 ldap_con = initialize_ldap_con(self.account.sub("$login", Net::LDAP::DN.escape(login)), password)
Chris@1295 140 else
Chris@1295 141 ldap_con = initialize_ldap_con(self.account, self.account_password)
Chris@1295 142 end
Chris@1295 143 login_filter = Net::LDAP::Filter.eq( self.attr_login, login )
Chris@1295 144 object_filter = Net::LDAP::Filter.eq( "objectClass", "*" )
Chris@1295 145 attrs = {}
Chris@1295 146
Chris@1295 147 search_filter = object_filter & login_filter
Chris@1295 148 if f = ldap_filter
Chris@1295 149 search_filter = search_filter & f
Chris@1295 150 end
Chris@1295 151
Chris@1295 152 ldap_con.search( :base => self.base_dn,
Chris@1295 153 :filter => search_filter,
Chris@1295 154 :attributes=> search_attributes) do |entry|
Chris@1295 155
Chris@1295 156 if onthefly_register?
Chris@1295 157 attrs = get_user_attributes_from_ldap_entry(entry)
Chris@1295 158 else
Chris@1295 159 attrs = {:dn => entry.dn}
Chris@1295 160 end
Chris@1295 161
Chris@1295 162 logger.debug "DN found for #{login}: #{attrs[:dn]}" if logger && logger.debug?
Chris@1295 163 end
Chris@1295 164
Chris@1295 165 attrs
Chris@1295 166 end
Chris@1295 167
Chris@1295 168 def self.get_attr(entry, attr_name)
Chris@1295 169 if !attr_name.blank?
Chris@1295 170 entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name]
Chris@1295 171 end
Chris@1295 172 end
Chris@1295 173 end