class DeviseOtt::Tokens

Public Class Methods

finalize(*) click to toggle source
# File lib/devise_ott/tokens.rb, line 12
def self.finalize(*)
  @redis.quit
end
new() click to toggle source
# File lib/devise_ott/tokens.rb, line 8
def initialize
  @redis ||= Redis.new(:host => Devise.ott_redis_host)
end

Public Instance Methods

access(token, email) click to toggle source

accesses token for given email if it is allowed

# File lib/devise_ott/tokens.rb, line 31
def access(token, email)
  config = load_config(token)

  return false unless config
  return false unless config[:email].to_s == email.to_s
  return false unless config[:access_count] > 0

  save_config(token, config.merge(access_count: config[:access_count] - 1))

  true
end
email(token) click to toggle source

returns email for a given token

# File lib/devise_ott/tokens.rb, line 44
def email(token)
  config = load_config(token)
  config && config[:email]
end
granted_to_email(token) click to toggle source

returns config hash for a given token

# File lib/devise_ott/tokens.rb, line 50
def granted_to_email(token)
                    config = load_config(token)
                    config && config[:granted_to_email]
end
register(token, email, granted_to_email, access_count, expire) click to toggle source

register one time token for given user in redis the generated token will have a field “email” in order to identify the associated user later

# File lib/devise_ott/tokens.rb, line 18
def register(token, email, granted_to_email, access_count, expire)
  save_config(token, {email: email, granted_to_email: granted_to_email, access_count: access_count})
  @redis.expire(token, expire)

  token
end
revoke(token) click to toggle source

deletes the token

# File lib/devise_ott/tokens.rb, line 26
def revoke(token)
  @redis.del(token)
end

Private Instance Methods

load_config(token) click to toggle source
# File lib/devise_ott/tokens.rb, line 62
def load_config(token)
  if token_data = @redis[token]
    Marshal.load(token_data)
  end
end
save_config(token, hash) click to toggle source
# File lib/devise_ott/tokens.rb, line 56
def save_config(token, hash)
  time_left = ttl(token)
  @redis[token] = Marshal.dump(hash)
  @redis.expire(token, time_left) if time_left > 0
end
ttl(token) click to toggle source
# File lib/devise_ott/tokens.rb, line 68
def ttl(token)
  @redis.ttl(token)
end