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.



252
253
254
255
256
257
258
# File 'lib/googleauth/id_tokens/key_sources.rb', line 252

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:



271
272
273
# File 'lib/googleauth/id_tokens/key_sources.rb', line 271

def current_keys
  @current_keys
end

#uriArray<KeyInfo> (readonly)

The URI from which to download keys.

Returns:



264
265
266
# File 'lib/googleauth/id_tokens/key_sources.rb', line 264

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:



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/googleauth/id_tokens/key_sources.rb', line 280

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