Class: DeviseOtt::Tokens

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/devise_ott/tokens.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTokens

Returns a new instance of Tokens.



8
9
10
# File 'lib/devise_ott/tokens.rb', line 8

def initialize
  @redis ||= Redis.new(:host => Devise.ott_redis_host)
end

Class Method Details

.finalizeObject



12
13
14
# File 'lib/devise_ott/tokens.rb', line 12

def self.finalize(*)
  @redis.quit
end

Instance Method Details

#access(token, email) ⇒ Object

accesses token for given email if it is allowed



31
32
33
34
35
36
37
38
39
40
41
# 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) ⇒ Object

returns email for a given token



44
45
46
47
# File 'lib/devise_ott/tokens.rb', line 44

def email(token)
  config = load_config(token)
  config && config[:email]
end

#granted_to_email(token) ⇒ Object

returns config hash for a given token



50
51
52
53
# 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) ⇒ Object

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



18
19
20
21
22
23
# 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) ⇒ Object

deletes the token



26
27
28
# File 'lib/devise_ott/tokens.rb', line 26

def revoke(token)
  @redis.del(token)
end