Class: ADAL::CachedTokenResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/adal/cached_token_response.rb

Overview

Proxy object for a token response with metadata.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, authority, token_response) ⇒ CachedTokenResponse

Constructs a new CachedTokenResponse.

Parameters:

  • ClientCredential|ClientAssertion|ClientAssertionCertificate

    The credentials of the calling client application.

  • Authority

    authority The ADAL::Authority object that the response was retrieved from.

  • SuccessResponse

    token_response The token response to be cached.



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, authority, token_response)
  unless token_response.instance_of? SuccessResponse
    fail ArgumentError, 'Only SuccessResponses can be cached.'
  end
  @authority = 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

#authorityObject (readonly)

Returns the value of attribute authority.



26
27
28
# File 'lib/adal/cached_token_response.rb', line 26

def authority
  @authority
end

#client_idObject (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_responseObject (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.

Parameters:

  • JSON

    raw_json

Returns:

  • CachedTokenResponse



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.

Parameters:

  • CachedTokenResponse

    other

Returns:

  • (Boolean)

    Boolean



85
86
87
88
# File 'lib/adal/cached_token_response.rb', line 85

def can_refresh?(other)
  mrrt? && (authority == other.authority) &&
    ( == other.) && (client_id == other.client_id)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/adal/cached_token_response.rb', line 167

def eql?(other)
  self == other
end

#hashObject



171
172
173
# File 'lib/adal/cached_token_response.rb', line 171

def hash
  [authority, client_id, token_response].hash
end

#mrrt?Boolean

Is the token a Multi Resource Refresh Token?

Returns:

  • (Boolean)

    Boolean



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

Parameters:

  • String

    resource The resource that the new access token is beign requested for. Defaults to using the same resource as the original token.

Returns:

  • TokenResponse



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(authority, @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.

Parameters:

  • String

    token



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.

Parameters:

  • JSON::Ext::Generator::State

    We don’t care about the state, but JSON::unparse requires this.

Returns:

  • String



61
62
63
64
65
# File 'lib/adal/cached_token_response.rb', line 61

def to_json(_ = nil)
  JSON.unparse(authority: [authority.host, authority.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.

Parameters:

  • Fixnum

    expiration_buffer_sec The number of seconds to use as leeway in determining if the token is expired. A positive buffer will refresh the token early while a negative buffer will refresh it late. Used to counter clock skew and network latency.

Returns:

  • Boolean True if the token is still valid (even if it was refreshed). False if the token is expired an unable to be refreshed.



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