annotate app/models/auth_source_ldap.rb @ 1296:038ba2d95de8 redmine-2.2

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