class Google::Auth::IDTokens::HttpKeySource

A base key source that downloads keys from a URI. Subclasses should override {HttpKeySource#interpret_json} to parse the response.

Constants

DEFAULT_RETRY_INTERVAL

The default interval between retries in seconds (3600s = 1hr).

@return [Integer]

Attributes

current_keys[R]

Return the current keys, without attempting to re-download.

@return [Array<KeyInfo>]

uri[R]

The URI from which to download keys. @return [Array<KeyInfo>]

Public Class Methods

new(uri, retry_interval: nil) click to toggle source

Create an HTTP key source.

@param uri [String,URI] The URI from which to download keys. @param retry_interval [Integer,nil] Override the retry interval in

seconds. This is the minimum time between retries of failed key
downloads.
# File lib/googleauth/id_tokens/key_sources.rb, line 249
def initialize uri, retry_interval: nil
  @uri = URI uri
  @retry_interval = retry_interval || DEFAULT_RETRY_INTERVAL
  @allow_refresh_at = Time.now
  @current_keys = []
  @monitor = Monitor.new
end

Public Instance Methods

refresh_keys() click to toggle source

Attempt to re-download keys (if the retry interval has expired) and return the new keys.

@return [Array<KeyInfo>] @raise [KeySourceError] if key retrieval failed.

# File lib/googleauth/id_tokens/key_sources.rb, line 277
def refresh_keys
  @monitor.synchronize do
    return @current_keys if Time.now < @allow_refresh_at
    @allow_refresh_at = Time.now + @retry_interval

    response = Net::HTTP.get_response uri
    raise KeySourceError, "Unable to retrieve data from #{uri}" unless response.is_a? Net::HTTPSuccess

    data = begin
             JSON.parse response.body
           rescue JSON::ParserError
             raise KeySourceError, "Unable to parse JSON"
           end

    @current_keys = Array(interpret_json(data))
  end
end

Protected Instance Methods

interpret_json(_data) click to toggle source
# File lib/googleauth/id_tokens/key_sources.rb, line 297
def interpret_json _data
  nil
end