class TokenDie
Constants
- TIMESTAMP_KEY
- VERSION
Attributes
encryptor[R]
Set the encryptor strategy. Defaults Parsel::JSON
secret[R]
Set the encryption secret
ttl[R]
Set the token TTL Defaults 300 (5 minutes)
Public Class Methods
new(secret, ttl = 300, encryptor = Parsel::JSON)
click to toggle source
# File lib/token-die.rb, line 16 def initialize(secret, ttl = 300, encryptor = Parsel::JSON) @secret = secret @ttl = ttl @encryptor = encryptor end
Public Instance Methods
expired?(timestamp)
click to toggle source
# File lib/token-die.rb, line 46 def expired?(timestamp) timestamp.to_i < (self.timestamp - ttl) end
fresh?(timestamp)
click to toggle source
# File lib/token-die.rb, line 50 def fresh?(timestamp) !expired?(timestamp) end
generate(data = {})
click to toggle source
# File lib/token-die.rb, line 26 def generate(data = {}) timestamp = self.timestamp data.merge!(TIMESTAMP_KEY => timestamp) encryptor.encrypt(secret, data) end
recover(token)
click to toggle source
# File lib/token-die.rb, line 33 def recover(token) data = encryptor.decrypt(secret, token) return unless data return unless fresh?(data[TIMESTAMP_KEY]) data.delete(TIMESTAMP_KEY) data end
timestamp()
click to toggle source
# File lib/token-die.rb, line 22 def timestamp Time.now.utc.to_i end
valid?(token)
click to toggle source
# File lib/token-die.rb, line 42 def valid?(token) !recover(token).nil? end