Module: Google::Auth::BaseClient

Included in:
ExternalAccount::BaseCredentials, ImpersonatedServiceAccountCredentials, Signet::OAuth2::Client
Defined in:
lib/googleauth/base_client.rb

Overview

BaseClient is a class used to contain common methods that are required by any Credentials Client, including AwsCredentials, ServiceAccountCredentials, and UserRefreshCredentials. This is a superclass of Signet::OAuth2::Client and has been created to create a generic interface for all credentials clients to use, including ones which do not inherit from Signet::OAuth2::Client.

Constant Summary collapse

AUTH_METADATA_KEY =
:authorization

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#loggerObject

The logger used to log operations on this client, such as token refresh.



79
80
81
# File 'lib/googleauth/base_client.rb', line 79

def logger
  @logger
end

Instance Method Details

#apply(a_hash, opts = {}) ⇒ Object

Returns a clone of a_hash updated with the authentication token



45
46
47
48
49
# File 'lib/googleauth/base_client.rb', line 45

def apply a_hash, opts = {}
  a_copy = a_hash.clone
  apply! a_copy, opts
  a_copy
end

#apply!(a_hash, opts = {}) ⇒ Object

Updates a_hash updated with the authentication token



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/googleauth/base_client.rb', line 30

def apply! a_hash, opts = {}
  # fetch the access token there is currently not one, or if the client
  # has expired
  fetch_access_token! opts if needs_access_token?
  token = send token_type
  a_hash[AUTH_METADATA_KEY] = "Bearer #{token}"
  logger&.debug do
    hash = Digest::SHA256.hexdigest token
    Google::Logging::Message.from message: "Sending auth token. (sha256:#{hash})"
  end

  a_hash[AUTH_METADATA_KEY]
end

#expires_within?Boolean

Returns:

  • (Boolean)

Raises:

  • (NoMethodError)


74
75
76
# File 'lib/googleauth/base_client.rb', line 74

def expires_within?
  raise NoMethodError, "expires_within? not implemented"
end

#needs_access_token?Boolean

Whether the id_token or access_token is missing or about to expire.

Returns:

  • (Boolean)


52
53
54
# File 'lib/googleauth/base_client.rb', line 52

def needs_access_token?
  send(token_type).nil? || expires_within?(60)
end

#notify_refresh_listenersObject



67
68
69
70
71
72
# File 'lib/googleauth/base_client.rb', line 67

def notify_refresh_listeners
  listeners = defined?(@refresh_listeners) ? @refresh_listeners : []
  listeners.each do |block|
    block.call self
  end
end

#on_refresh(&block) ⇒ Object



62
63
64
65
# File 'lib/googleauth/base_client.rb', line 62

def on_refresh &block
  @refresh_listeners = [] unless defined? @refresh_listeners
  @refresh_listeners << block
end

#updater_procObject

Returns a reference to the #apply method, suitable for passing as a closure



58
59
60
# File 'lib/googleauth/base_client.rb', line 58

def updater_proc
  proc { |a_hash, opts = {}| apply a_hash, opts }
end