auth_source_ldap.rb
165 lines
| 5.1 KiB
| text/x-ruby
|
RubyLexer
|
r6392 | # Redmine - project management software | ||
# Copyright (C) 2006-2011 Jean-Philippe Lang | ||||
|
r330 | # | ||
# This program is free software; you can redistribute it and/or | ||||
# modify it under the terms of the GNU General Public License | ||||
# as published by the Free Software Foundation; either version 2 | ||||
# of the License, or (at your option) any later version. | ||||
|
r6392 | # | ||
|
r330 | # This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | ||||
|
r6392 | # | ||
|
r330 | # You should have received a copy of the GNU General Public License | ||
# along with this program; if not, write to the Free Software | ||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
require 'iconv' | ||||
|
r8787 | require 'net/ldap' | ||
|
r9121 | require 'net/ldap/dn' | ||
|
r330 | |||
|
r6392 | class AuthSourceLdap < AuthSource | ||
|
r330 | validates_presence_of :host, :port, :attr_login | ||
|
r4830 | validates_length_of :name, :host, :maximum => 60, :allow_nil => true | ||
|
r8924 | validates_length_of :account, :account_password, :base_dn, :filter, :maximum => 255, :allow_blank => true | ||
|
r1661 | validates_length_of :attr_login, :attr_firstname, :attr_lastname, :attr_mail, :maximum => 30, :allow_nil => true | ||
validates_numericality_of :port, :only_integer => true | ||||
|
r8924 | validate :validate_filter | ||
|
r6392 | |||
|
r1892 | before_validation :strip_ldap_attributes | ||
|
r6392 | |||
|
r9138 | def self.human_attribute_name(attribute_key_name, *args) | ||
attr_name = attribute_key_name.to_s | ||||
if attr_name == "filter" | ||||
attr_name = "ldap_filter" | ||||
end | ||||
super(attr_name, *args) | ||||
end | ||||
|
r8168 | def initialize(attributes=nil, *args) | ||
super | ||||
|
r330 | self.port = 389 if self.port == 0 | ||
end | ||||
|
r6392 | |||
|
r330 | def authenticate(login, password) | ||
|
r1155 | return nil if login.blank? || password.blank? | ||
|
r9121 | attrs = get_user_dn(login, password) | ||
|
r6392 | |||
|
r3378 | if attrs && attrs[:dn] && authenticate_dn(attrs[:dn], password) | ||
|
r3327 | logger.debug "Authentication successful for '#{login}'" if logger && logger.debug? | ||
|
r3378 | return attrs.except(:dn) | ||
|
r3327 | end | ||
|
r8791 | rescue Net::LDAP::LdapError => e | ||
raise AuthSourceException.new(e.message) | ||||
|
r330 | end | ||
# test the connection to the LDAP | ||||
def test_connection | ||||
ldap_con = initialize_ldap_con(self.account, self.account_password) | ||||
ldap_con.open { } | ||||
|
r9114 | rescue Net::LDAP::LdapError => e | ||
raise "LdapError: " + e.message | ||||
|
r330 | end | ||
|
r6392 | |||
|
r330 | def auth_method_name | ||
"LDAP" | ||||
end | ||||
|
r6392 | |||
|
r1892 | private | ||
|
r6392 | |||
|
r8924 | def ldap_filter | ||
if filter.present? | ||||
Net::LDAP::Filter.construct(filter) | ||||
end | ||||
rescue Net::LDAP::LdapError | ||||
nil | ||||
end | ||||
def validate_filter | ||||
if filter.present? && ldap_filter.nil? | ||||
errors.add(:filter, :invalid) | ||||
end | ||||
end | ||||
|
r1892 | def strip_ldap_attributes | ||
[:attr_login, :attr_firstname, :attr_lastname, :attr_mail].each do |attr| | ||||
write_attribute(attr, read_attribute(attr).strip) unless read_attribute(attr).nil? | ||||
end | ||||
end | ||||
|
r6392 | |||
|
r330 | def initialize_ldap_con(ldap_user, ldap_password) | ||
|
r1180 | options = { :host => self.host, | ||
:port => self.port, | ||||
:encryption => (self.tls ? :simple_tls : nil) | ||||
} | ||||
|
r1185 | options.merge!(:auth => { :method => :simple, :username => ldap_user, :password => ldap_password }) unless ldap_user.blank? && ldap_password.blank? | ||
|
r1180 | Net::LDAP.new options | ||
|
r330 | end | ||
|
r3325 | |||
def get_user_attributes_from_ldap_entry(entry) | ||||
|
r3378 | { | ||
|
r3340 | :dn => entry.dn, | ||
|
r3325 | :firstname => AuthSourceLdap.get_attr(entry, self.attr_firstname), | ||
:lastname => AuthSourceLdap.get_attr(entry, self.attr_lastname), | ||||
:mail => AuthSourceLdap.get_attr(entry, self.attr_mail), | ||||
:auth_source_id => self.id | ||||
|
r3378 | } | ||
|
r3325 | end | ||
|
r3327 | |||
|
r3339 | # Return the attributes needed for the LDAP search. It will only | ||
# include the user attributes if on-the-fly registration is enabled | ||||
def search_attributes | ||||
if onthefly_register? | ||||
['dn', self.attr_firstname, self.attr_lastname, self.attr_mail] | ||||
else | ||||
['dn'] | ||||
end | ||||
end | ||||
|
r3327 | # Check if a DN (user record) authenticates with the password | ||
def authenticate_dn(dn, password) | ||||
|
r3336 | if dn.present? && password.present? | ||
initialize_ldap_con(dn, password).bind | ||||
end | ||||
|
r3327 | end | ||
|
r3340 | |||
# Get the user's dn and any attributes for them, given their login | ||||
|
r9121 | def get_user_dn(login, password) | ||
ldap_con = nil | ||||
|
r9123 | if self.account && self.account.include?("$login") | ||
|
r9121 | ldap_con = initialize_ldap_con(self.account.sub("$login", Net::LDAP::DN.escape(login)), password) | ||
else | ||||
ldap_con = initialize_ldap_con(self.account, self.account_password) | ||||
end | ||||
|
r6392 | login_filter = Net::LDAP::Filter.eq( self.attr_login, login ) | ||
object_filter = Net::LDAP::Filter.eq( "objectClass", "*" ) | ||||
|
r3378 | attrs = {} | ||
|
r6392 | |||
|
r8924 | search_filter = object_filter & login_filter | ||
if f = ldap_filter | ||||
search_filter = search_filter & f | ||||
end | ||||
|
r6392 | ldap_con.search( :base => self.base_dn, | ||
|
r8924 | :filter => search_filter, | ||
|
r3340 | :attributes=> search_attributes) do |entry| | ||
if onthefly_register? | ||||
attrs = get_user_attributes_from_ldap_entry(entry) | ||||
else | ||||
|
r3378 | attrs = {:dn => entry.dn} | ||
|
r3340 | end | ||
|
r3378 | logger.debug "DN found for #{login}: #{attrs[:dn]}" if logger && logger.debug? | ||
|
r3340 | end | ||
attrs | ||||
end | ||||
|
r6392 | |||
|
r330 | def self.get_attr(entry, attr_name) | ||
|
r2054 | if !attr_name.blank? | ||
entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name] | ||||
end | ||||
|
r10 | end | ||
end | ||||