Class: Talis::Authentication::PublicKey

Inherits:
Resource show all
Defined in:
lib/talis/authentication/public_key.rb

Overview

Provides the ability to fetch a public key to verify tokens. There is no need to use this class directly as it is used by the Token class to verify tokens.

Instance Method Summary collapse

Methods inherited from Resource

handle_response, new_req_id

Constructor Details

#initialize(cache_store) ⇒ PublicKey

Construct an empty public key object.

Parameters:

  • cache_store (ActiveSupport::Cache::MemoryStore)

    A cache store to use to fetch locally cached keys before trying remotely.



15
16
17
# File 'lib/talis/authentication/public_key.rb', line 15

def initialize(cache_store)
  @cache_store = cache_store
end

Instance Method Details

#fetch(request_id: self.class.new_req_id) ⇒ String

Fetch a public key for use with token verification, either from the provided cache or remotely.

Parameters:

  • request_id (String) (defaults to: self.class.new_req_id)

    (uuid) unique ID for the remote request.

Returns:

  • (String)

    the public key.



23
24
25
26
27
28
29
30
31
32
# File 'lib/talis/authentication/public_key.rb', line 23

def fetch(request_id: self.class.new_req_id)
  # Token base URI may have changed after the class was loaded.
  self.class.base_uri(Token.base_uri)
  public_key = @cache_store.fetch(cache_key, cache_options) do
    opts = { format: :plain, headers: { 'X-Request-Id' => request_id } }
    response = self.class.get('/oauth/keys', opts)
    self.class.handle_response(response)
  end
  OpenSSL::PKey.read(public_key)
end