Class: Google::Auth::APIKeyCredentials

Inherits:
Object
  • Object
show all
Includes:
BaseClient
Defined in:
lib/googleauth/api_key.rb

Overview

Implementation of Google API Key authentication.

API Keys are text strings. They don't have an associated JSON file.

The end-user is managing their API Keys directly, not via an authentication library.

API Keys provide project information for an API request. API Keys don't reference an IAM principal, they do not expire, and cannot be refreshed.

Constant Summary

Constants included from BaseClient

BaseClient::AUTH_METADATA_KEY

Instance Attribute Summary collapse

Attributes included from BaseClient

#logger

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BaseClient

#apply, #needs_access_token?, #notify_refresh_listeners, #on_refresh, #updater_proc

Constructor Details

#initialize(options = {}) ⇒ APIKeyCredentials

Initialize the APIKeyCredentials.

Parameters:

  • options (Hash) (defaults to: {})

    The credentials options

Options Hash (options):

  • :api_key (String)

    The API key to use for authentication

  • :universe_domain (String)

    The universe domain of the universe this API key belongs to (defaults to googleapis.com)

Raises:

  • (ArgumentError)


88
89
90
91
92
# File 'lib/googleauth/api_key.rb', line 88

def initialize options = {}
  raise ArgumentError, "API key must be provided" if options[:api_key].nil? || options[:api_key].empty?
  @api_key = options[:api_key]
  @universe_domain = options[:universe_domain] || "googleapis.com"
end

Instance Attribute Details

#api_keyString (readonly)

Returns The API key.

Returns:

  • (String)

    The API key



42
43
44
# File 'lib/googleauth/api_key.rb', line 42

def api_key
  @api_key
end

#universe_domainString

Returns The universe domain of the universe this API key is for.

Returns:

  • (String)

    The universe domain of the universe this API key is for



46
47
48
# File 'lib/googleauth/api_key.rb', line 46

def universe_domain
  @universe_domain
end

Class Method Details

.from_env(_scope = nil, options = {}) ⇒ Google::Auth::APIKeyCredentials?

Creates an APIKeyCredentials from the environment. Checks the ENV['GOOGLE_API_KEY'] variable.

Parameters:

  • _scope (String) (defaults to: nil)

    The scope to use for OAuth. Not used by API key auth.

  • options (Hash) (defaults to: {})

    The options to pass to the credentials instance

Returns:



60
61
62
63
64
# File 'lib/googleauth/api_key.rb', line 60

def from_env _scope = nil, options = {}
  api_key = ENV[API_KEY_VAR]
  return nil if api_key.nil? || api_key.empty?
  new options.merge(api_key: api_key)
end

.make_creds(options = {}) ⇒ Google::Auth::APIKeyCredentials

Create the APIKeyCredentials.

Parameters:

  • options (Hash) (defaults to: {})

    The credentials options

Options Hash (options):

  • :api_key (String)

    The API key to use for authentication

  • :universe_domain (String)

    The universe domain of the universe this API key belongs to (defaults to googleapis.com)

Returns:



75
76
77
# File 'lib/googleauth/api_key.rb', line 75

def make_creds options = {}
  new options
end

Instance Method Details

#apply!(a_hash, _opts = {}) ⇒ Hash

Updates the provided hash with the API Key header.

The apply! method modifies the provided hash in place, adding the x-goog-api-key header with the API Key value.

The API Key is hashed before being logged for security purposes.

NB: this method typically would be called through updater_proc. Some older clients call it directly though, so it has to be public.

Parameters:

  • a_hash (Hash)

    The hash to which the API Key header should be added. This is typically a hash representing the request headers. This hash will be modified in place.

  • _opts (Hash) (defaults to: {})

    Additional options (currently not used). Included for consistency with the BaseClient interface.

Returns:

  • (Hash)

    The modified hash (the same hash passed as the a_hash argument).



133
134
135
136
137
138
139
140
# File 'lib/googleauth/api_key.rb', line 133

def apply! a_hash, _opts = {}
  a_hash[API_KEY_HEADER] = @api_key
  logger&.debug do
    hash = Digest::SHA256.hexdigest @api_key
    Google::Logging::Message.from message: "Sending API key auth token. (sha256:#{hash})"
  end
  a_hash
end

#duplicate(options = {}) ⇒ Google::Auth::APIKeyCredentials

Creates a duplicate of these credentials.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options for configuring the credentials

Returns:



109
110
111
112
113
114
# File 'lib/googleauth/api_key.rb', line 109

def duplicate options = {}
  self.class.new(
    api_key: options[:api_key] || @api_key,
    universe_domain: options[:universe_domain] || @universe_domain
  )
end

#expires_within?(_seconds) ⇒ Boolean

Determines if the credentials object has expired. Since API keys don't expire, this always returns false.

Parameters:

  • _seconds (Fixnum)

    The optional timeout in seconds since the last refresh

Returns:

  • (Boolean)

    True if the token has expired, false otherwise.



101
102
103
# File 'lib/googleauth/api_key.rb', line 101

def expires_within? _seconds
  false
end