Class: Razoul::Token

Inherits:
Model
  • Object
show all
Defined in:
lib/razoul/token.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

persistence

Constructor Details

#initializeToken

Returns a new instance of Token.



7
8
9
10
# File 'lib/razoul/token.rb', line 7

def initialize
  @value = self.generate_key
  @created_at = Time.now.to_i
end

Instance Attribute Details

#created_atObject

Returns the value of attribute created_at.



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

def created_at
  @created_at
end

#valueObject

Returns the value of attribute value.



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

def value
  @value
end

Class Method Details

.current_tokenObject



25
26
27
# File 'lib/razoul/token.rb', line 25

def current_token
  Marshal.load(persistence.find(Razoul.configuration.token_key))
end

.generate!Object



29
30
31
# File 'lib/razoul/token.rb', line 29

def generate!
  persistence.save(Razoul.configuration.token_key, Marshal.dump(new))
end

.valid_token?(token) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/razoul/token.rb', line 33

def valid_token?(token)
  token.eql?(Marshal.load(persistence.find(Razoul.configuration.token_key)).value) ? true : false
end

Instance Method Details

#expired?Boolean

Returns:

  • (Boolean)


19
20
21
22
# File 'lib/razoul/token.rb', line 19

def expired?
  puts "Time now: #{Time.now.to_i} - #{self.created_at}"
  Time.now.to_i - self.created_at >= Razoul.configuration.expiration_time
end

#generate_keyObject



12
13
14
15
16
17
# File 'lib/razoul/token.rb', line 12

def generate_key
  digest = OpenSSL::Digest::SHA256.new
  secret = Razoul.configuration.password
  string = Razoul.configuration. + Time.now.to_s
  OpenSSL::HMAC.hexdigest(digest, secret, string)
end