Module: Jm81auth::Models::AuthToken::ClassMethods
- Defined in:
- lib/jm81auth/models/auth_token.rb
Instance Method Summary collapse
- #config ⇒ Configuration
-
#decode(token) ⇒ AuthToken
Decode a JWT token and get AuthToken based on stored ID.
-
#encode(value) ⇒ String
Encode a value using jwt_secret and jwt_algorithm.
-
#use(token) ⇒ AuthToken
Decode a JWT token and get AuthToken based on stored ID.
Instance Method Details
#config ⇒ Configuration
110 111 112 |
# File 'lib/jm81auth/models/auth_token.rb', line 110 def config Jm81auth.config end |
#decode(token) ⇒ AuthToken
Decode a JWT token and get AuthToken based on stored ID.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/jm81auth/models/auth_token.rb', line 63 def decode token payload = JWT.decode( token, config.jwt_secret, config.jwt_algorithm ).first auth_token = self[payload['auth_token_id']] if payload['auth_token_id'].nil? raise DecodeError, "auth_token_id missing: #{payload}" elsif auth_token.nil? raise DecodeError, "auth_token_id not found: #{payload}" end auth_token end |
#encode(value) ⇒ String
Encode a value using jwt_secret and jwt_algorithm.
83 84 85 |
# File 'lib/jm81auth/models/auth_token.rb', line 83 def encode value JWT.encode value, config.jwt_secret, config.jwt_algorithm end |
#use(token) ⇒ AuthToken
Decode a JWT token and get AuthToken based on stored ID. If an open AuthToken is found, update its last_used_at value.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/jm81auth/models/auth_token.rb', line 94 def use token auth_token = decode token if auth_token && !auth_token.expired? if respond_to? :update_attributes! auth_token.update_attributes! last_used_at: Time.now else auth_token.update last_used_at: Time.now end auth_token else nil end end |