Class: Google::Auth::IDTokens::HttpKeySource

Inherits:
Object
  • Object
show all
Defined in:
lib/googleauth/id_tokens/key_sources.rb

Overview

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

Direct Known Subclasses

JwkHttpKeySource, X509CertHttpKeySource

Constant Summary collapse

DEFAULT_RETRY_INTERVAL =

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

Returns:

  • (Integer)
3600

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, retry_interval: nil) ⇒ HttpKeySource

Create an HTTP key source.

Parameters:

  • uri (String, URI)

    The URI from which to download keys.

  • retry_interval (Integer, nil) (defaults to: nil)

    Override the retry interval in seconds. This is the minimum time between retries of failed key downloads.



238
239
240
241
242
243
244
# File 'lib/googleauth/id_tokens/key_sources.rb', line 238

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

Instance Attribute Details

#current_keysArray<KeyInfo> (readonly)

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

Returns:



257
258
259
# File 'lib/googleauth/id_tokens/key_sources.rb', line 257

def current_keys
  @current_keys
end

#uriArray<KeyInfo> (readonly)

The URI from which to download keys.

Returns:



250
251
252
# File 'lib/googleauth/id_tokens/key_sources.rb', line 250

def uri
  @uri
end

Instance Method Details

#refresh_keysArray<KeyInfo>

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

Returns:

Raises:



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/googleauth/id_tokens/key_sources.rb', line 266

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