Class: DeviseOtt::Tokens
- Inherits:
-
Object
- Object
- DeviseOtt::Tokens
- Includes:
- Singleton
- Defined in:
- lib/devise_ott/tokens.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#access(token, email) ⇒ Object
accesses token for given email if it is allowed.
-
#email(token) ⇒ Object
returns email for a given token.
-
#granted_to_email(token) ⇒ Object
returns config hash for a given token.
-
#initialize ⇒ Tokens
constructor
A new instance of Tokens.
-
#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.
-
#revoke(token) ⇒ Object
deletes the token.
Constructor Details
Class Method Details
.finalize ⇒ Object
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 |