Class: Google::Auth::Credentials

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

Overview

This class is intended to be inherited by API-specific classes which overrides the SCOPE constant.

Constant Summary collapse

TOKEN_CREDENTIAL_URI =
"https://oauth2.googleapis.com/token".freeze
AUDIENCE =
"https://oauth2.googleapis.com/token".freeze
SCOPE =
[].freeze
PATH_ENV_VARS =
[].freeze
JSON_ENV_VARS =
[].freeze
DEFAULT_PATHS =
[].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

rubocop:disable Metrics/AbcSize



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/googleauth/credentials.rb', line 58

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!
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



48
49
50
# File 'lib/googleauth/credentials.rb', line 48

def client
  @client
end

#project_idObject (readonly)

Returns the value of attribute project_id.



49
50
51
# File 'lib/googleauth/credentials.rb', line 49

def project_id
  @project_id
end

Class Method Details

.default(options = {}) ⇒ Object

Returns the default credentials checking, in this order, the path env evironment variables, json environment variables, default paths. If the previously stated locations do not contain keyfile information, this method defaults to use the application default.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/googleauth/credentials.rb', line 87

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

  # Second try to find keyfile json from environment variables.
  client ||= from_json_vars options

  # Third 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