class FirebaseTokenAuth::PublicKeyManager

Constants

PUBLIC_KEY_URL

Attributes

expire_time[RW]
public_keys[RW]

Public Class Methods

new() click to toggle source
# File lib/firebase_token_auth/public_key_manager.rb, line 9
def initialize
  fetch_publickeys_hash
end

Public Instance Methods

refresh_publickeys!() click to toggle source
# File lib/firebase_token_auth/public_key_manager.rb, line 13
def refresh_publickeys!
  return unless expired?

  fetch_publickeys_hash
end

Private Instance Methods

cache_control_header_to_expire_time(cache_control_header) click to toggle source
# File lib/firebase_token_auth/public_key_manager.rb, line 31
def cache_control_header_to_expire_time(cache_control_header)
  Time.now.to_i + cache_control_header.match(/max-age=([0-9]*)/)[1].to_i
end
exception_handler(response) click to toggle source
# File lib/firebase_token_auth/public_key_manager.rb, line 35
def exception_handler(response)
  error = STATUS_TO_EXCEPTION_MAPPING[response.code]
  raise error.new("Receieved an error response #{response.code} #{error.to_s.split('::').last}: #{response.body}", response) if error

  response
end
expired?() click to toggle source
# File lib/firebase_token_auth/public_key_manager.rb, line 27
def expired?
  @expire_time.to_i > Time.now.to_i
end
fetch_publickeys_hash() click to toggle source
# File lib/firebase_token_auth/public_key_manager.rb, line 21
def fetch_publickeys_hash
  res = exception_handler(Net::HTTP.get_response(URI(PUBLIC_KEY_URL)))
  @public_keys = JSON.parse(res.body).transform_values! { |v| OpenSSL::X509::Certificate.new(v) }
  @expire_time = cache_control_header_to_expire_time(res['Cache-Control'])
end