Sha256: 5c1dfdc6f195d6bc7bb271b8b8b3c34faec35c62b0426da9e2b62b848a62d35c

Contents?: true

Size: 685 Bytes

Versions: 13

Compression:

Stored size: 685 Bytes

Contents

require 'openssl'

module Devise
  class TokenGenerator
    def initialize(key_generator, digest = "SHA256")
      @key_generator = key_generator
      @digest = digest
    end

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

    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

    private

    def key_for(column)
      @key_generator.generate_key("Devise #{column}")
    end
  end
end

Version data entries

13 entries across 13 versions & 3 rubygems

Version Path
devise-4.3.0 lib/devise/token_generator.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/devise-4.2.1/lib/devise/token_generator.rb
devise-4.2.1 lib/devise/token_generator.rb
devise-4.2.0 lib/devise/token_generator.rb
devise-4.0.3 lib/devise/token_generator.rb
devise-4.1.1 lib/devise/token_generator.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/devise-4.1.0/lib/devise/token_generator.rb
devise-4.1.0 lib/devise/token_generator.rb
devise-4.0.2 lib/devise/token_generator.rb
devise-4.0.1 lib/devise/token_generator.rb
devise-4.0.0 lib/devise/token_generator.rb
devise-4.0.0.rc2 lib/devise/token_generator.rb
devise-4.0.0.rc1 lib/devise/token_generator.rb