Class: Google::Gax::Credentials

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

Overview

Represents the OAuth 2.0 signing logic. This class is intended to be inherited by API-specific classes which overrides the SCOPE constant.

Constant Summary collapse

TOKEN_CREDENTIAL_URI =
'https://accounts.google.com/o/oauth2/token'.freeze
AUDIENCE =
'https://accounts.google.com/o/oauth2/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, scope: nil) ⇒ Credentials

Returns a new instance of Credentials.



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

def initialize(keyfile, scope: nil)
  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
  @client.fetch_access_token!
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



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

def client
  @client
end

Class Method Details

.default(scope: nil) ⇒ Object

Returns the default credentials.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/google/gax/credentials.rb', line 78

def self.default(scope: nil)
  env = ->(v) { ENV[v] }
  json = lambda do |v|
    unless ENV[v].nil?
      begin
        JSON.parse ENV[v]
      rescue
        nil
      end
    end
  end
  path = ->(p) { ::File.file? p }

  # First try to find keyfile file from environment variables.
  self::PATH_ENV_VARS.map(&env).compact.select(&path)
                     .each do |file|
    return new file, scope: scope
  end
  # Second try to find keyfile json from environment variables.
  self::JSON_ENV_VARS.map(&json).compact.each do |hash|
    return new hash, scope: scope
  end
  # Third try to find keyfile file from known file paths.
  self::DEFAULT_PATHS.select(&path).each do |file|
    return new file, scope: scope
  end
  # Finally get instantiated client from Google::Auth.
  scope ||= self::SCOPE
  client = Google::Auth.get_application_default scope
  new client
end