Class: Google::Auth::Credentials

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/googleauth/credentials.rb

Overview

Credentials is responsible for representing the authentication when connecting to an API. This class is also intended to be inherited by API-specific classes.

Constant Summary collapse

TOKEN_CREDENTIAL_URI =

The default token credential URI to be used when none is provided during initialization.

"https://oauth2.googleapis.com/token".freeze
AUDIENCE =

The default target audience ID to be used when none is provided during initialization.

"https://oauth2.googleapis.com/token".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keyfile, options = {}) ⇒ Credentials

Creates a new Credentials instance with the provided auth credentials, and with the default values configured on the class.

Parameters:

  • keyfile (String, Hash, Signet::OAuth2::Client)

    The keyfile can be provided as one of the following:

    • The path to a JSON keyfile (as a String)

    • The contents of a JSON keyfile (as a Hash)

    • A Signet::OAuth2::Client object

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

    The options for configuring the credentials instance. The following is supported:

    • :scope - the scope for the client

    • “project_id” (and optionally “project”) - the project identifier for the client

    • :connection_builder - the connection builder to use for the client

    • :default_connection - the default connection to use for the client



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/googleauth/credentials.rb', line 238

def initialize keyfile, options = {}
  scope = options[:scope]
  verify_keyfile_provided! keyfile
  @project_id = options["project_id"] || options["project"]
  if keyfile.is_a? Signet::OAuth2::Client
    @client = keyfile
    @project_id ||= keyfile.project_id if keyfile.respond_to? :project_id
  elsif keyfile.is_a? Hash
    hash = stringify_hash_keys keyfile
    hash["scope"] ||= scope
    @client = init_client hash, options
    @project_id ||= (hash["project_id"] || hash["project"])
  else
    verify_keyfile_exists! keyfile
    json = JSON.parse ::File.read(keyfile)
    json["scope"] ||= scope
    @project_id ||= (json["project_id"] || json["project"])
    @client = init_client json, options
  end
  CredentialsLoader.warn_if_cloud_sdk_credentials @client.client_id
  @project_id ||= CredentialsLoader.load_gcloud_project_id
  @client.fetch_access_token!
  @env_vars = nil
  @paths = nil
  @scope = nil
end

Instance Attribute Details

#audienceString (readonly)

Returns The target audience ID when issuing assertions. Used only by the assertion grant type.

Returns:

  • (String)

    The target audience ID when issuing assertions. Used only by the assertion grant type.



214
215
216
# File 'lib/googleauth/credentials.rb', line 214

def_delegators :@client,
:token_credential_uri, :audience,
:scope, :issuer, :signing_key, :updater_proc

#clientSignet::OAuth2::Client

The Signet::OAuth2::Client object the Credentials instance is using.



179
180
181
# File 'lib/googleauth/credentials.rb', line 179

def client
  @client
end

#issuerString (readonly)

Returns The issuer ID associated with this client.

Returns:

  • (String)

    The issuer ID associated with this client.



214
215
216
# File 'lib/googleauth/credentials.rb', line 214

def_delegators :@client,
:token_credential_uri, :audience,
:scope, :issuer, :signing_key, :updater_proc

#project_idString (readonly)

Identifier for the project the client is authenticating with.

Returns:

  • (String)


186
187
188
# File 'lib/googleauth/credentials.rb', line 186

def project_id
  @project_id
end

#scopeString+ (readonly)

Returns The scope for this client. A scope is an access range defined by the authorization server. The scope can be a single value or a list of values.

Returns:

  • (String, Array<String>)

    The scope for this client. A scope is an access range defined by the authorization server. The scope can be a single value or a list of values.



214
215
216
# File 'lib/googleauth/credentials.rb', line 214

def_delegators :@client,
:token_credential_uri, :audience,
:scope, :issuer, :signing_key, :updater_proc

#signing_keyString, OpenSSL::PKey (readonly)

Returns The signing key associated with this client.

Returns:

  • (String, OpenSSL::PKey)

    The signing key associated with this client.



214
215
216
# File 'lib/googleauth/credentials.rb', line 214

def_delegators :@client,
:token_credential_uri, :audience,
:scope, :issuer, :signing_key, :updater_proc

#token_credential_uriString (readonly)

Returns The token credential URI. The URI is the authorization server’s HTTP endpoint capable of issuing tokens and refreshing expired tokens.

Returns:

  • (String)

    The token credential URI. The URI is the authorization server’s HTTP endpoint capable of issuing tokens and refreshing expired tokens.



214
215
216
# File 'lib/googleauth/credentials.rb', line 214

def_delegators :@client,
:token_credential_uri, :audience,
:scope, :issuer, :signing_key, :updater_proc

#updater_procProc (readonly)

Returns a reference to the Signet::OAuth2::Client#apply method, suitable for passing as a closure.

Returns:



214
215
216
# File 'lib/googleauth/credentials.rb', line 214

def_delegators :@client,
:token_credential_uri, :audience,
:scope, :issuer, :signing_key, :updater_proc

Class Method Details

.audienceString

The default target audience ID to be used when none is provided during initialization. Used only by the assertion grant type.

Returns:

  • (String)


79
80
81
82
83
# File 'lib/googleauth/credentials.rb', line 79

def self.audience
  return @audience unless @audience.nil?

  const_get :AUDIENCE if const_defined? :AUDIENCE
end

.audience=(new_audience) ⇒ String

Sets the default target audience ID to be used when none is provided during initialization.

Parameters:

  • new_audience (String)

Returns:

  • (String)


91
92
93
# File 'lib/googleauth/credentials.rb', line 91

def self.audience= new_audience
  @audience = new_audience
end

.default(options = {}) ⇒ Credentials

Creates a new Credentials instance with auth credentials acquired by searching the environment variables and paths configured on the class, and with the default values configured on the class.

The auth credentials are searched for in the following order:

  1. configured environment variables (see env_vars)

  2. configured default file paths (see paths)

  3. application default (see Google::Auth.get_application_default)

Parameters:

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

    The options for configuring the credentials instance. The following is supported:

    • :scope - the scope for the client

    • “project_id” (and optionally “project”) - the project identifier for the client

    • :connection_builder - the connection builder to use for the client

    • :default_connection - the default connection to use for the client

Returns:



287
288
289
290
291
292
293
294
295
296
297
# File 'lib/googleauth/credentials.rb', line 287

def self.default options = {}
  # First try to find keyfile file or json from environment variables.
  client = from_env_vars options

  # Second try to find keyfile file from known file paths.
  client ||= from_default_paths options

  # Finally get instantiated client from Google::Auth
  client ||= from_application_default options
  client
end

.env_varsArray<String>

The environment variables to search for credentials. Values can either be a file path to the credentials file, or the JSON contents of the credentials file.

Returns:

  • (Array<String>)


128
129
130
131
132
133
134
135
136
# File 'lib/googleauth/credentials.rb', line 128

def self.env_vars
  return @env_vars unless @env_vars.nil?

  # Pull values when PATH_ENV_VARS or JSON_ENV_VARS constants exists.
  tmp_env_vars = []
  tmp_env_vars << const_get(:PATH_ENV_VARS) if const_defined? :PATH_ENV_VARS
  tmp_env_vars << const_get(:JSON_ENV_VARS) if const_defined? :JSON_ENV_VARS
  tmp_env_vars.flatten.uniq
end

.env_vars=(new_env_vars) ⇒ Array<String>

Sets the environment variables to search for credentials.

Parameters:

  • new_env_vars (Array<String>)

Returns:

  • (Array<String>)


144
145
146
147
# File 'lib/googleauth/credentials.rb', line 144

def self.env_vars= new_env_vars
  new_env_vars = Array new_env_vars unless new_env_vars.nil?
  @env_vars = new_env_vars
end

.pathsArray<String>

The file paths to search for credentials files.

Returns:

  • (Array<String>)


154
155
156
157
158
159
160
161
# File 'lib/googleauth/credentials.rb', line 154

def self.paths
  return @paths unless @paths.nil?

  tmp_paths = []
  # Pull in values is the DEFAULT_PATHS constant exists.
  tmp_paths << const_get(:DEFAULT_PATHS) if const_defined? :DEFAULT_PATHS
  tmp_paths.flatten.uniq
end

.paths=(new_paths) ⇒ Array<String>

Set the file paths to search for credentials files.

Parameters:

  • new_paths (Array<String>)

Returns:

  • (Array<String>)


169
170
171
172
# File 'lib/googleauth/credentials.rb', line 169

def self.paths= new_paths
  new_paths = Array new_paths unless new_paths.nil?
  @paths = new_paths
end

.scopeString+

The default scope to be used when none is provided during initialization. A scope is an access range defined by the authorization server. The scope can be a single value or a list of values.

Returns:

  • (String, Array<String>)


102
103
104
105
106
107
108
109
# File 'lib/googleauth/credentials.rb', line 102

def self.scope
  return @scope unless @scope.nil?

  tmp_scope = []
  # Pull in values is the SCOPE constant exists.
  tmp_scope << const_get(:SCOPE) if const_defined? :SCOPE
  tmp_scope.flatten.uniq
end

.scope=(new_scope) ⇒ String+

Sets the default scope to be used when none is provided during initialization.

Parameters:

  • new_scope (String, Array<String>)

Returns:

  • (String, Array<String>)


117
118
119
120
# File 'lib/googleauth/credentials.rb', line 117

def self.scope= new_scope
  new_scope = Array new_scope unless new_scope.nil?
  @scope = new_scope
end

.token_credential_uriString

The default token credential URI to be used when none is provided during initialization. The URI is the authorization server’s HTTP endpoint capable of issuing tokens and refreshing expired tokens.

Returns:

  • (String)


57
58
59
60
61
# File 'lib/googleauth/credentials.rb', line 57

def self.token_credential_uri
  return @token_credential_uri unless @token_credential_uri.nil?

  const_get :TOKEN_CREDENTIAL_URI if const_defined? :TOKEN_CREDENTIAL_URI
end

.token_credential_uri=(new_token_credential_uri) ⇒ String

Set the default token credential URI to be used when none is provided during initialization.

Parameters:

  • new_token_credential_uri (String)

Returns:

  • (String)


69
70
71
# File 'lib/googleauth/credentials.rb', line 69

def self.token_credential_uri= new_token_credential_uri
  @token_credential_uri = new_token_credential_uri
end