class Rpush::Daemon::GoogleCredentialCache

Constants

TOKEN_VALID_FOR_SEC

Assuming tokens are valid for 1 hour

Public Class Methods

new() click to toggle source
# File lib/rpush/daemon/google_credential_cache.rb, line 11
def initialize
  @credentials_cache = {}
end

Public Instance Methods

access_token(scope, json_key) click to toggle source
# File lib/rpush/daemon/google_credential_cache.rb, line 15
def access_token(scope, json_key)
  key = hash_key(scope, json_key)

  if @credentials_cache[key].nil? || Time.now > @credentials_cache[key][:expires_at]
    token = fetch_fresh_token(scope, json_key)
    expires_at = Time.now + TOKEN_VALID_FOR_SEC
    @credentials_cache[key] = { token: token, expires_at: expires_at }
  end

  @credentials_cache[key][:token]
end

Private Instance Methods

fetch_fresh_token(scope, json_key) click to toggle source
# File lib/rpush/daemon/google_credential_cache.rb, line 29
def fetch_fresh_token(scope, json_key)
  json_key_io = json_key ? StringIO.new(json_key) : nil
  log_debug("FCM - Obtaining access token.")
  authorizer = Google::Auth::ServiceAccountCredentials.make_creds(scope: scope, json_key_io: json_key_io)
  authorizer.fetch_access_token
end
hash_key(scope, json_key) click to toggle source
# File lib/rpush/daemon/google_credential_cache.rb, line 36
def hash_key(scope, json_key)
  scope.hash ^ json_key.hash
end