Module: GrapeTokenAuth::LookupToken::ClassMethods

Included in:
GrapeTokenAuth::LookupToken
Defined in:
lib/grape_token_auth/lookup_token.rb

Instance Method Summary collapse

Instance Method Details

#digest(column, value) ⇒ Object



27
28
29
30
31
# File 'lib/grape_token_auth/lookup_token.rb', line 27

def digest(column, value)
  return unless value.present?
  key = key_for(column)
  OpenSSL::HMAC.hexdigest(open_ssl_digest, key, value)
end

#friendly_token(length = 20) ⇒ Object

copied from devise, creates a token that is url safe without ambigous characters



12
13
14
15
# File 'lib/grape_token_auth/lookup_token.rb', line 12

def friendly_token(length = 20)
  rlength = (length * 3) / 4
  SecureRandom.urlsafe_base64(rlength).tr('lIO0', 'sxyz')
end

#generate(authenticatable_klass, column) ⇒ Object



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

def generate(authenticatable_klass, column)
  loop do
    raw = friendly_token
    enc = digest(column, raw)
    unless authenticatable_klass.exists_in_column?(column, enc)
      break [raw, enc]
    end
  end
end

#open_ssl_digestObject



33
34
35
# File 'lib/grape_token_auth/lookup_token.rb', line 33

def open_ssl_digest
  GrapeTokenAuth.configuration.digest
end