Class: MortalToken::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/mortal-token/token.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(salt = nil, start_time = nil, config = nil) ⇒ Token

To create a brand new token, do not pass any arguments. To validate a hash from an existing token, pass the existing token’s salt.



9
10
11
12
13
# File 'lib/mortal-token/token.rb', line 9

def initialize(salt=nil, start_time=nil, config=nil)
  @config = config || MortalToken.config
  @salt = salt || config.salt
  @start_time = start_time || (config.units == :days ? Date.today : Time.now.utc)
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



5
6
7
# File 'lib/mortal-token/token.rb', line 5

def config
  @config
end

#saltObject (readonly)

The token’s salt



4
5
6
# File 'lib/mortal-token/token.rb', line 4

def salt
  @salt
end

Instance Method Details

#==(other_token_or_hash) ⇒ Object Also known as: ===

Tests this token against another token or token hash



25
26
27
28
29
30
31
# File 'lib/mortal-token/token.rb', line 25

def ==(other_token_or_hash)
  each_time do |time|
    guess = config.token(salt, time)
    return true if guess.to_s == other_token_or_hash.to_s
  end
  return false
end

#hashObject Also known as: to_s

Returns the hash value of the token



16
17
18
19
20
# File 'lib/mortal-token/token.rb', line 16

def hash
  @hash ||= (0..config.rounds-1).inject("#{config.secret}_#{salt}_#{end_time.strftime(strfmt)}") do |val, i|
    Digest::SHA512.hexdigest(val)
  end
end