Class: ADAL::CacheDriver

Inherits:
Object
  • Object
show all
Includes:
Logging, RequestParameters
Defined in:
lib/adal/cache_driver.rb

Overview

Performs logical operations on the TokenCache in the context of one token request.

Constant Summary collapse

FIELDS =
{ user_info: USER_INFO,
username: USERNAME,
resource: RESOURCE }

Constants included from RequestParameters

RequestParameters::AAD_API_VERSION, RequestParameters::ASSERTION, RequestParameters::CLIENT_ASSERTION, RequestParameters::CLIENT_ASSERTION_TYPE, RequestParameters::CLIENT_ID, RequestParameters::CLIENT_REQUEST_ID, RequestParameters::CLIENT_RETURN_CLIENT_REQUEST_ID, RequestParameters::CLIENT_SECRET, RequestParameters::CODE, RequestParameters::FORM_POST, RequestParameters::GRANT_TYPE, RequestParameters::PASSWORD, RequestParameters::REDIRECT_URI, RequestParameters::REFRESH_TOKEN, RequestParameters::RESOURCE, RequestParameters::SCOPE, RequestParameters::UNIQUE_ID, RequestParameters::USERNAME, RequestParameters::USER_INFO

Constants included from Logging

Logging::DEFAULT_LOG_LEVEL, Logging::DEFAULT_LOG_OUTPUT

Instance Method Summary collapse

Methods included from Logging

#logger

Constructor Details

#initialize(authority, client, token_cache = NoopCache.new, expiration_buffer_sec = 0) ⇒ CacheDriver

Constructs a CacheDriver to interact with a token cache.

Parameters:

  • String

    authority The URL of the authority endpoint.

  • ClientAssertion|ClientCredential|etc

    client The credentials representing the calling application. We need this instead of just the client id so that the tokens can be refreshed if necessary.

  • TokenCache

    token_cache The cache implementation to store tokens.



54
55
56
57
58
59
60
# File 'lib/adal/cache_driver.rb', line 54

def initialize(
  authority, client, token_cache = NoopCache.new, expiration_buffer_sec = 0)
  @authority = authority
  @client = client
  @expiration_buffer_sec = expiration_buffer_sec
  @token_cache = token_cache
end

Instance Method Details

#add(token_response) ⇒ Object

Checks if a TokenResponse is successful and if so adds it to the token cache for future retrieval.

Parameters:

  • SuccessResponse

    token_response The successful token response to be cached. If it is not successful, it fails silently.



69
70
71
72
73
74
75
# File 'lib/adal/cache_driver.rb', line 69

def add(token_response)
  return unless token_response.instance_of? SuccessResponse
  logger.verbose('Adding successful TokenResponse to cache.')
  entry = CachedTokenResponse.new(@client, @authority, token_response)
  update_refresh_tokens(entry) if entry.mrrt?
  @token_cache.add(entry)
end

#find(query = {}) ⇒ Object

Searches the cache for a token matching a specific query of fields.

Parameters:

  • Hash

    query The fields to match against.

Returns:

  • TokenResponse



83
84
85
86
87
88
89
90
91
92
# File 'lib/adal/cache_driver.rb', line 83

def find(query = {})
  query = query.map { |k, v| [FIELDS[k], v] if FIELDS[k] }.compact.to_h
  resource = query.delete(RESOURCE)
  matches = validate(
    find_all_cached_entries(
      query.reverse_merge(
        authority: @authority, client_id: @client.client_id))
  )
  resource_specific(matches, resource) || refresh_mrrt(matches, resource)
end