Class: Zas::Authenticators::SequelPasswordAuthenticator

Inherits:
Object
  • Object
show all
Defined in:
lib/zas/authenticators/sequel_password_authenticator.rb

Overview

Public: Authenticate a username/password combination against a database using Sequel.

Defined Under Namespace

Classes: Configuration

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sequel_db, config = Configuration.new) ⇒ SequelPasswordAuthenticator

Public: Initialize the autenticator with the given sequel DB. Optionally pass in additional configuration for the authenticator.

sequel_db - The Sequel Datbase instance config - The configuration instance



41
42
43
44
45
46
47
# File 'lib/zas/authenticators/sequel_password_authenticator.rb', line 41

def initialize(sequel_db, config=Configuration.new)
  self.db = sequel_db
  self.table_name = config.table_name.to_sym
  self.username_field = config.username_field.to_sym
  self.password_field = config.password_field.to_sym
  self.salt_field = config.salt_field.to_sym
end

Instance Attribute Details

#loggerObject

Public: a logger.



34
35
36
# File 'lib/zas/authenticators/sequel_password_authenticator.rb', line 34

def logger
  @logger
end

Instance Method Details

#authenticate(username, password) ⇒ Object

Public: Authenticate the given username/password pair

username - The username password - The password

Returns true if the username/password pair are authenticated. Will return a falsey value otherwise.



55
56
57
58
59
60
61
62
63
# File 'lib/zas/authenticators/sequel_password_authenticator.rb', line 55

def authenticate(username, password)
  logger.info "Authenticating #{username} (table: #{table_name}, username_field: #{username_field})" if logger
  record = db[table_name].filter(username_field => username).first
  if record
    logger.info "Record found for #{username}" if logger
    tokens = [password, record[salt_field]].compact
    matches?(record[password_field], *tokens)
  end
end