class LdapQuery::Authenticate
Used to authenticate a users LDAP credentials to a user in LDAP
Constants
- REQUIRED_CONNECTION_KEYS
Attributes
config[RW]
connection[RW]
Public Class Methods
new(credentials = {})
click to toggle source
Initialzile an ldap connection for authenticating a user
@params credentials [Hash]
# File lib/ldap_query/authenticate.rb, line 13 def initialize(credentials = {}) establish_connection(credentials) end
Public Instance Methods
auth_user(username, password)
click to toggle source
Authenticate
the user again ldap with the supplied username/password
@param username [String] @param password [String] @return [Boolean, Hash, Net::Ldap]
# File lib/ldap_query/authenticate.rb, line 22 def auth_user(username, password) return false if username.nil? || password.nil? response = @connection.link.bind_as(base: @config.base, size: 1, filter: LdapQuery::Filter.auth(username), password: password) # if no user was found return false, otherwise return the user (response && response[0]) ? response : false end
Private Instance Methods
establish_connection(credentials = {})
click to toggle source
Establish an ldap connection without fulling binding a connection yet
@params credentials [Hash]
# File lib/ldap_query/authenticate.rb, line 38 def establish_connection(credentials = {}) raise_keys_error if credentials.blank? || !required_credentials_supplied?(credentials) @config = LdapQuery::Config.new(credentials) @connection = LdapQuery::Connection.new(@config.auth_hash, type: :auth) @connection.link.auth(@config.username, @config.password) rescue raise(ConnectionError, 'Failure connecting to LDAP host') end
raise_keys_error()
click to toggle source
Raise an exception if any of the credentials are missing any key/value for authenticating a user
# File lib/ldap_query/authenticate.rb, line 57 def raise_keys_error raise(CredentialsError, "The following credentials attributes are required: #{REQUIRED_CONNECTION_KEYS}") end
required_credentials_supplied?(credentials = {})
click to toggle source
Verify if the required encryption keys have been supplied
@return [Boolean]
# File lib/ldap_query/authenticate.rb, line 51 def required_credentials_supplied?(credentials = {}) # Verify all reqiured credentail values have been supplied for the LDAP connection REQUIRED_CONNECTION_KEYS.all? { |req_key| credentials.key?(req_key) } end