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

Returns a new instance of Credentials.



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

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
    @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
  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.



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

def client
  @client
end

#project_idObject (readonly)

Returns the value of attribute project_id.



51
52
53
# File 'lib/googleauth/credentials.rb', line 51

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
101
# File 'lib/googleauth/credentials.rb', line 87

def self.default(options = {})
  scope = options[:scope]
  # First try to find keyfile file from environment variables.
  client = from_path_vars scope

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

  # Third try to find keyfile file from known file paths.
  client ||= from_default_paths scope

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