Class: Devise::TokenGenerator

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of TokenGenerator.



8
9
10
11
# File 'lib/devise/token_generator.rb', line 8

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

Instance Method Details

#digest(klass, column, value) ⇒ Object



13
14
15
# File 'lib/devise/token_generator.rb', line 13

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

#generate(klass, column) ⇒ Object



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

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

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