Method: Google::Auth::Credentials#initialize

Defined in:
lib/googleauth/credentials.rb

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

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

Parameters:

  • source_creds (String, Pathname, Hash, Google::Auth::BaseClient)

    The source of credentials. It can be provided as one of the following:

    • The path to a JSON keyfile (as a String or a Pathname)
    • The contents of a JSON keyfile (as a Hash)
    • A Google::Auth::BaseClient credentials object, including but not limited to a Signet::OAuth2::Client object.
    • Any credentials object that supports the methods this wrapper delegates to an inner client.

    If this parameter is an object (Signet::OAuth2::Client or other) it will be used as an inner client. Otherwise the inner client will be constructed from the JSON keyfile or the contens of the hash.

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

    The options for configuring this wrapper credentials object and the inner client. The options hash is used in two ways:

    1. Configuring the wrapper object: Some options are used to directly configure the wrapper Credentials instance. These include:
    • :project_id (and optionally :project) - the project identifier for the client
    • :quota_project_id - the quota project identifier for the client
    • :logger - the logger used to log credential operations such as token refresh.
    1. Configuring the inner client: When the source_creds parameter is a String or Hash, a new Signet::OAuth2::Client is created internally. The following options are used to configure this inner client:
    • :scope - the scope for the client
    • :target_audience - the target audience for the client

    Any other options in the options hash are passed directly to the inner client constructor. This allows you to configure additional parameters of the Signet::OAuth2::Client, such as connection parameters, timeouts, etc.

Raises:



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/googleauth/credentials.rb', line 401

def initialize source_creds, options = {}
  if source_creds.nil?
    raise InitializationError,
          "The source credentials passed to Google::Auth::Credentials.new were nil."
  end

  options = symbolize_hash_keys options
  @project_id = options[:project_id] || options[:project]
  @quota_project_id = options[:quota_project_id]
  case source_creds
  when String, Pathname
    update_from_filepath source_creds, options
  when Hash
    update_from_hash source_creds, options
  else
    update_from_client source_creds
  end
  setup_logging logger: options.fetch(:logger, :default)
  @project_id ||= CredentialsLoader.load_gcloud_project_id
  @env_vars = nil
  @paths = nil
  @scope = nil
end