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.



5
6
7
8
# File 'lib/devise/token_generator.rb', line 5

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

Instance Method Details

#digest(klass, column, value) ⇒ Object



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

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

#generate(klass, column) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/devise/token_generator.rb', line 14

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