Class: Talis::Authentication::Token
- Defined in:
- lib/talis/authentication/token.rb
Overview
Represents a JWT-based OAuth access token.
Optionally configure an ActiveSupport-based cache store for caching the public key and tokens. The default cache used is an in-memory one. See api.rubyonrails.org/classes/ActiveSupport/Cache.html for supported cache types.
Class Method Summary collapse
-
.generate(request_id: new_req_id, client_id:, client_secret:, host: base_uri) ⇒ Talis::Authentication::Token
Generate a new token for the given client.
Instance Method Summary collapse
-
#initialize(jwt:, public_key: nil) ⇒ Token
constructor
Create a new token object from an existing JWT.
-
#to_s ⇒ String
The encoded version of the token - a JWT string.
-
#validate(request_id: self.class.new_req_id, scopes: [], all: true) ⇒ Symbol, Nil
Validate the token, optionally against one or more required scopes.
Methods inherited from Resource
Constructor Details
Class Method Details
.generate(request_id: new_req_id, client_id:, client_secret:, host: base_uri) ⇒ Talis::Authentication::Token
Generate a new token for the given client. If a previous token has been generated for the client and has not expired then this will be returned from the cache.
116 117 118 119 120 121 122 123 124 |
# File 'lib/talis/authentication/token.rb', line 116 def generate(request_id: new_req_id, client_id:, client_secret:, host: base_uri) token = cached_token(client_id, host) if token new(jwt: token) else generate_remote_token(request_id, client_id, client_secret, host) end end |
Instance Method Details
#to_s ⇒ String
Returns the encoded version of the token - a JWT string.
66 67 68 |
# File 'lib/talis/authentication/token.rb', line 66 def to_s @jwt end |
#validate(request_id: self.class.new_req_id, scopes: [], all: true) ⇒ Symbol, Nil
Validate the token, optionally against one or more required scopes.
Scope validation is performed locally unless there are too many tokens to list inside the token payload. When this is the case, a remote request is performed to validate the token against the scopes.
The validation error returned can be one of the following:
-
‘:expired_token` if the token has expired.
-
‘:insufficient_scope` if the provided scopes are not in the token.
-
‘:invalid_token` if the token could not be verified by the public key.
-
‘:invalid_token` if the token could not be decoded.
-
‘:invalid_key` if the public key is corrupt.
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/talis/authentication/token.rb', line 52 def validate(request_id: self.class.new_req_id, scopes: [], all: true) decoded = JWT.decode(@jwt, p_key(request_id), true, algorithm: 'RS256') validate_scopes(request_id, scopes, decoded[0], all) rescue JWT::ExpiredSignature return :expired_token rescue JWT::VerificationError, JWT::DecodeError return :invalid_token rescue NoMethodError return :invalid_key rescue Talis::ClientError :insufficient_scope end |