Class: Kankri::SimpleAuthenticator

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

Overview

An object that takes in a user hash and authenticates users

This object holds user data in memory, including passwords. It is thus not secure for mission-critical applications.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(users, hash_maker = nil) ⇒ SimpleAuthenticator

Returns a new instance of SimpleAuthenticator.



24
25
26
27
28
29
30
31
# File 'lib/kankri/simple_authenticator.rb', line 24

def initialize(users, hash_maker = nil)
  hash_maker ||= self.class.method(:sha256_hasher)
  @users = users

  @hashers = hash_maker.call(@users.keys)
  @passwords = passwords
  @privilege_sets = privilege_sets
end

Class Method Details

.digest_hasher(usernames, hasher) ⇒ Object

Makes hashing functions for users based on a Digest implementation.



15
16
17
18
19
20
21
22
# File 'lib/kankri/simple_authenticator.rb', line 15

def self.digest_hasher(usernames, hasher)
  Hash[
    usernames.map do |username|
      salt = SecureRandom.random_bytes
      [username, ->(password) { hasher.digest(password + salt) } ]
    end
  ]
end

.sha256_hasher(usernames) ⇒ Object

Makes hashing functions for users based on SHA256.



10
11
12
# File 'lib/kankri/simple_authenticator.rb', line 10

def self.sha256_hasher(usernames)
  digest_hasher(usernames, Digest::SHA256)
end

Instance Method Details

#authenticate(username, password) ⇒ Object



33
34
35
36
# File 'lib/kankri/simple_authenticator.rb', line 33

def authenticate(username, password)
  auth_fail unless auth_ok?(username.intern, password.to_s)
  privileges_for(username.intern)
end