Skip to content

Instantly share code, notes, and snippets.

@edavis10
Created February 17, 2010 17:16
Show Gist options
  • Save edavis10/306821 to your computer and use it in GitHub Desktop.
Save edavis10/306821 to your computer and use it in GitHub Desktop.
# app/models/auth_source_ldap.rb
class AuthSourceLdap < AuthSource
# ...
def authenticate(login, password)
return nil if login.blank? || password.blank?
attrs = []
# get user's DN
ldap_con = initialize_ldap_con(self.account, self.account_password)
login_filter = Net::LDAP::Filter.eq( self.attr_login, login )
object_filter = Net::LDAP::Filter.eq( "objectClass", "*" )
dn = String.new
ldap_con.search( :base => self.base_dn,
:filter => object_filter & login_filter,
# only ask for the DN if on-the-fly registration is disabled
:attributes=> (onthefly_register? ? ['dn', self.attr_firstname, self.attr_lastname, self.attr_mail] : ['dn'])) do |entry|
dn = entry.dn
attrs = get_user_attributes_from_ldap_entry(entry) if onthefly_register?
end
if authenticate_dn(dn, password)
logger.debug "Authentication successful for '#{login}'" if logger && logger.debug?
return attrs
end
rescue Net::LDAP::LdapError => text
raise "LdapError: " + text
end
# Check if a DN (user record) authenticates with the password
def authenticate_dn(dn, password)
return false if dn.empty? || password.empty?
initialize_ldap_con(dn, password).bind
end
# ...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment