Class: MortalToken::Configuration

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

Overview

Holds a specific a configuration of parameters

Constant Summary collapse

RAND_SEEDS =

:nodoc;

[(0..9), ('a'..'z'), ('A'..'Z')].map(&:to_a).flatten

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Configuration

Instantiates a new Configuration object default values



22
23
24
25
26
27
28
29
30
# File 'lib/mortal-token/configuration.rb', line 22

def initialize(name)
  @name = name
  @digest = 'sha256'
  @units = :hours
  @valid_for = 1
  @max_salt_length = 50
  @min_salt_length = 10
  @secret = CONFIGS[:default].secret if CONFIGS[:default]
end

Instance Attribute Details

#digestObject

The digest to use (passed to OpenSSL::Digest.new). Defaults to ‘sha256’.



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

def digest
  @digest
end

#max_salt_lengthObject

The maximum salt length, defaults to 50



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

def max_salt_length
  @max_salt_length
end

#min_salt_lengthObject

The minimum salt length, defaults to 10



19
20
21
# File 'lib/mortal-token/configuration.rb', line 19

def min_salt_length
  @min_salt_length
end

#nameObject (readonly)

The configuration’s name



7
8
9
# File 'lib/mortal-token/configuration.rb', line 7

def name
  @name
end

#secretObject

The master secret token (Keep it secret! Keep it safe!). Changing this will invalidate all existing tokens.



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

def secret
  @secret
end

#unitsObject

The units that life is measured in - :days, :hours or :minutes (defaults to :hours)



13
14
15
# File 'lib/mortal-token/configuration.rb', line 13

def units
  @units
end

#valid_forObject

The number of units tokens will be valid across. Defaults to 1. Changing this will invalidate existing tokens.



15
16
17
# File 'lib/mortal-token/configuration.rb', line 15

def valid_for
  @valid_for
end

Instance Method Details

#_digestObject

:nodoc:



55
56
57
# File 'lib/mortal-token/configuration.rb', line 55

def _digest # :nodoc:
  @_digest ||= OpenSSL::Digest.new(self.digest)
end

#check(salt, expires) ⇒ Object

Returns a token reconstitued from the timestamp and salt



38
39
40
# File 'lib/mortal-token/configuration.rb', line 38

def check(salt, expires)
  Token.new(salt, expires, self)
end

#saltObject

Returns a random string of between min_salt_length and max_salt_length alphanumeric charachters



49
50
51
52
53
# File 'lib/mortal-token/configuration.rb', line 49

def salt
  max_length = [self.min_salt_length, rand(self.max_salt_length + 1)].max
  pool_size = RAND_SEEDS.size
  (0..max_length).map { RAND_SEEDS[rand(pool_size)] }.join('')
end

#token(salt = nil, expires = nil) ⇒ Object

Return a new token using this configuration



33
34
35
# File 'lib/mortal-token/configuration.rb', line 33

def token(salt=nil, expires=nil)
  Token.new(salt, expires, self)
end