Class: Kankri::PasswordCheck

Inherits:
Object
  • Object
show all
Defined in:
lib/kankri/password_check.rb

Overview

A method object that represents a check on username/password pairs

This is a basic check based on string comparison, failing if either username or password are empty or nil. If used with a hashing authenticator, the hashing must be done before the password checking. Similarly, PasswordCheck does not convert to or from symbols; the authenticator must do this itself.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, passwords) ⇒ PasswordCheck

Creates a password check instance

Passwords may be literal passwords, hashes or any other secret that can be compared by ==. Any hashing or other processing (such as type conversion) must be done by the Authenticator.

Examples:

Initialises a PasswordCheck.

PasswordCheck.new('alf', 'hunter2', 'alf' => 'hunter2')

Parameters:

  • The username; this is typically a Symbol or a String.

  • The password to check against the passwords database.

  • The hash mapping usernames to their passwords.

API:

  • public



25
26
27
28
29
# File 'lib/kankri/password_check.rb', line 25

def initialize(username, password, passwords)
  @username = username
  @password = password
  @passwords = passwords
end

Class Method Details

.check(*args) ⇒ Boolean

Creates and runs a password check

Examples:

Check a correct password.

PasswordCheck.check('alf', 'hunter2', 'alf' => 'hunter2')
#=> true

Check an incorrect password.

PasswordCheck.check('alf', 'nope', 'alf' => 'hunter2')
#=> false

Parameters:

  • The username; this is typically a Symbol or a String.

  • The password to check against the passwords database.

  • The hash mapping usernames to their passwords.

Returns:

  • True if the password matches; false otherwise.

API:

  • public



59
60
61
# File 'lib/kankri/password_check.rb', line 59

def self.check(*args)
  PasswordCheck.new(*args).ok?
end

Instance Method Details

#ok?Boolean

Checks to see if the authentication credentials are correct

Examples:

Perform a successful authentication check.

checker.ok?
#=> true

Perform an unsuccessful authentication check.

checker.ok?
#=> false

Returns:

  • True if the password matches; false otherwise.

API:

  • public



42
43
44
# File 'lib/kankri/password_check.rb', line 42

def ok?
  auth_present? && user_known? && password_match?
end