Class: Google::Auth::APIKeyCredentials
- Inherits:
-
Object
- Object
- Google::Auth::APIKeyCredentials
- 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
Instance Attribute Summary collapse
-
#api_key ⇒ String
readonly
The API key.
-
#universe_domain ⇒ String
The universe domain of the universe this API key is for.
Attributes included from BaseClient
Class Method Summary collapse
-
.from_env(_scope = nil, options = {}) ⇒ Google::Auth::APIKeyCredentials?
Creates an APIKeyCredentials from the environment.
-
.make_creds(options = {}) ⇒ Google::Auth::APIKeyCredentials
Create the APIKeyCredentials.
Instance Method Summary collapse
-
#apply!(a_hash, _opts = {}) ⇒ Hash
Updates the provided hash with the API Key header.
-
#duplicate(options = {}) ⇒ Google::Auth::APIKeyCredentials
Creates a duplicate of these credentials.
-
#expires_within?(_seconds) ⇒ Boolean
Determines if the credentials object has expired.
-
#initialize(options = {}) ⇒ APIKeyCredentials
constructor
Initialize the APIKeyCredentials.
Methods included from BaseClient
#apply, #needs_access_token?, #notify_refresh_listeners, #on_refresh, #updater_proc
Constructor Details
#initialize(options = {}) ⇒ APIKeyCredentials
Initialize the APIKeyCredentials.
88 89 90 91 92 |
# File 'lib/googleauth/api_key.rb', line 88 def initialize = {} raise ArgumentError, "API key must be provided" if [:api_key].nil? || [:api_key].empty? @api_key = [:api_key] @universe_domain = [:universe_domain] || "googleapis.com" end |
Instance Attribute Details
#api_key ⇒ String (readonly)
Returns The API key.
42 43 44 |
# File 'lib/googleauth/api_key.rb', line 42 def api_key @api_key end |
#universe_domain ⇒ String
Returns 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.
60 61 62 63 64 |
# File 'lib/googleauth/api_key.rb', line 60 def from_env _scope = nil, = {} api_key = ENV[API_KEY_VAR] return nil if api_key.nil? || api_key.empty? new .merge(api_key: api_key) end |
.make_creds(options = {}) ⇒ Google::Auth::APIKeyCredentials
Create the APIKeyCredentials.
75 76 77 |
# File 'lib/googleauth/api_key.rb', line 75 def make_creds = {} new 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.
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.
109 110 111 112 113 114 |
# File 'lib/googleauth/api_key.rb', line 109 def duplicate = {} self.class.new( api_key: [:api_key] || @api_key, universe_domain: [: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.
101 102 103 |
# File 'lib/googleauth/api_key.rb', line 101 def expires_within? _seconds false end |