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.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/googleauth/credentials.rb', line 56

def initialize(keyfile, options = {})
  scope = options[:scope]
  verify_keyfile_provided! keyfile
  if keyfile.is_a? Signet::OAuth2::Client
    @client = keyfile
  elsif keyfile.is_a? Hash
    hash = stringify_hash_keys keyfile
    hash['scope'] ||= scope
    @client = init_client hash
  else
    verify_keyfile_exists! keyfile
    json = JSON.parse ::File.read(keyfile)
    json['scope'] ||= scope
    @client = init_client json
  end
  CredentialsLoader.warn_if_cloud_sdk_credentials @client.client_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

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.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/googleauth/credentials.rb', line 79

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