Class: Kingsman::TokenGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/kingsman/token_generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(key_generator, digest = "SHA256") ⇒ TokenGenerator

Returns a new instance of TokenGenerator.



7
8
9
10
# File 'lib/kingsman/token_generator.rb', line 7

def initialize(key_generator, digest = "SHA256")
  @key_generator = key_generator
  @digest = digest
end

Instance Method Details

#digest(klass, column, value) ⇒ Object



12
13
14
# File 'lib/kingsman/token_generator.rb', line 12

def digest(klass, column, value)
  value.present? && OpenSSL::HMAC.hexdigest(@digest, key_for(column), value.to_s)
end

#generate(klass, column) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/kingsman/token_generator.rb', line 16

def generate(klass, column)
  key = key_for(column)

  loop do
    raw = Kingsman.friendly_token
    enc = OpenSSL::HMAC.hexdigest(@digest, key, raw)
    break [raw, enc] unless klass.to_adapter.find_first({ column => enc })
  end
end