Class: ADAL::CachedTokenResponse
- Inherits:
-
Object
- Object
- ADAL::CachedTokenResponse
- Defined in:
- lib/adal/cached_token_response.rb
Overview
Proxy object for a token response with metadata.
Instance Attribute Summary collapse
-
#authority ⇒ Object
readonly
Returns the value of attribute authority.
-
#client_id ⇒ Object
readonly
Returns the value of attribute client_id.
-
#token_response ⇒ Object
readonly
Returns the value of attribute token_response.
Class Method Summary collapse
-
.from_json(json) ⇒ Object
Reconstructs an object from JSON that was serialized with CachedTokenResponse#to_json.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Since the token cache may be implemented by the user of this library, all means of checking equality must be consistent.
-
#can_refresh?(other) ⇒ Boolean
Determines if self can be used to refresh other.
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(client, authority, token_response) ⇒ CachedTokenResponse
constructor
Constructs a new CachedTokenResponse.
-
#mrrt? ⇒ Boolean
Is the token a Multi Resource Refresh Token?.
-
#refresh(new_resource = resource) ⇒ Object
Attempts to refresh the access token for a given resource.
-
#refresh_token=(token) ⇒ Object
Changes the refresh token of the underlying token response.
-
#to_json(_ = nil) ⇒ Object
Converts the fields in this object and its proxied SuccessResponse into a JSON string.
-
#validate(expiration_buffer_sec = 0) ⇒ Object
If the access token is within the expiration buffer of expiring, an attempt will be made to retrieve a new token with the refresh token.
Constructor Details
#initialize(client, authority, token_response) ⇒ CachedTokenResponse
Constructs a new CachedTokenResponse.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/adal/cached_token_response.rb', line 39 def initialize(client, , token_response) unless token_response.instance_of? SuccessResponse fail ArgumentError, 'Only SuccessResponses can be cached.' end @authority = if client.respond_to? :client_id @client = client @client_id = client.client_id else @client = ClientCredential.new(client) @client_id = client end @token_response = token_response end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object (private)
CachedTokenResponse is just a proxy for TokenResponse.
178 179 180 181 182 183 184 |
# File 'lib/adal/cached_token_response.rb', line 178 def method_missing(method, *args, &block) if token_response.respond_to?(method) token_response.send(method, *args, &block) else super(method) end end |
Instance Attribute Details
#authority ⇒ Object (readonly)
Returns the value of attribute authority.
26 27 28 |
# File 'lib/adal/cached_token_response.rb', line 26 def @authority end |
#client_id ⇒ Object (readonly)
Returns the value of attribute client_id.
27 28 29 |
# File 'lib/adal/cached_token_response.rb', line 27 def client_id @client_id end |
#token_response ⇒ Object (readonly)
Returns the value of attribute token_response.
28 29 30 |
# File 'lib/adal/cached_token_response.rb', line 28 def token_response @token_response end |
Class Method Details
.from_json(json) ⇒ Object
Reconstructs an object from JSON that was serialized with CachedTokenResponse#to_json.
73 74 75 76 77 78 |
# File 'lib/adal/cached_token_response.rb', line 73 def self.from_json(json) json = JSON.parse(json) if json.instance_of? String CachedTokenResponse.new(json['client_id'], Authority.new(*json['authority']), SuccessResponse.new(json['token_response'])) end |
Instance Method Details
#==(other) ⇒ Object
Since the token cache may be implemented by the user of this library, all means of checking equality must be consistent.
161 162 163 164 165 |
# File 'lib/adal/cached_token_response.rb', line 161 def ==(other) [:authority, :client_id, :token_response].all? do |field| (other.respond_to? field) && (send(field) == other.send(field)) end end |
#can_refresh?(other) ⇒ Boolean
Determines if self can be used to refresh other.
85 86 87 88 |
# File 'lib/adal/cached_token_response.rb', line 85 def can_refresh?(other) mrrt? && ( == other.) && (user_info == other.user_info) && (client_id == other.client_id) end |
#eql?(other) ⇒ Boolean
167 168 169 |
# File 'lib/adal/cached_token_response.rb', line 167 def eql?(other) self == other end |
#hash ⇒ Object
171 172 173 |
# File 'lib/adal/cached_token_response.rb', line 171 def hash [, client_id, token_response].hash end |
#mrrt? ⇒ Boolean
Is the token a Multi Resource Refresh Token?
154 155 156 |
# File 'lib/adal/cached_token_response.rb', line 154 def mrrt? token_response.refresh_token && token_response.resource end |
#refresh(new_resource = resource) ⇒ Object
Attempts to refresh the access token for a given resource. Note that you can call this method with a different resource even if the token is not an MRRT, but it will fail
131 132 133 134 135 136 137 138 139 |
# File 'lib/adal/cached_token_response.rb', line 131 def refresh(new_resource = resource) token_response = TokenRequest .new(, @client) .get_with_refresh_token(refresh_token, new_resource) if token_response.instance_of? SuccessResponse token_response.parse_id_token(id_token) end token_response end |
#refresh_token=(token) ⇒ Object
Changes the refresh token of the underlying token response.
145 146 147 148 |
# File 'lib/adal/cached_token_response.rb', line 145 def refresh_token=(token) token_response.instance_variable_set(:@refresh_token, token) logger.verbose("Updated the refresh token for #{token_response}.") end |
#to_json(_ = nil) ⇒ Object
Converts the fields in this object and its proxied SuccessResponse into a JSON string.
61 62 63 64 65 |
# File 'lib/adal/cached_token_response.rb', line 61 def to_json(_ = nil) JSON.unparse(authority: [.host, .tenant], client_id: client_id, token_response: token_response) end |
#validate(expiration_buffer_sec = 0) ⇒ Object
If the access token is within the expiration buffer of expiring, an attempt will be made to retrieve a new token with the refresh token.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/adal/cached_token_response.rb', line 102 def validate(expiration_buffer_sec = 0) return true if (Time.now + expiration_buffer_sec).to_i < expires_on unless refresh_token logger.verbose('Cached token is almost expired but no refresh token ' \ 'is available.') return false end logger.verbose('Cached token is almost expired, attempting to refresh ' \ ' with refresh token.') refresh_response = refresh if refresh_response.instance_of? SuccessResponse logger.verbose('Successfully refreshed token in cache.') @token_response = refresh_response true else logger.warn('Failed to refresh token in cache with refresh token.') false end end |