Class: Gcloud::Credentials

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/gcloud/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 =

:nodoc:

"https://accounts.google.com/o/oauth2/token"
AUDIENCE =
"https://accounts.google.com/o/oauth2/token"
SCOPE =
[]
PATH_ENV_VARS =
["GOOGLE_CLOUD_KEYFILE"]
JSON_ENV_VARS =
["GOOGLE_CLOUD_KEYFILE_JSON"]
DEFAULT_PATHS =
["~/.config/gcloud/application_default_credentials.json"]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Credentials.



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/gcloud/credentials.rb', line 45

def initialize keyfile, options = {}
  verify_keyfile_provided! keyfile
  if keyfile.is_a? Signet::OAuth2::Client
    @client = keyfile
  elsif keyfile.is_a? Hash
    @client = init_client keyfile, options
  else
    verify_keyfile_exists! keyfile
    @client = init_client JSON.parse(::File.read(keyfile)), options
  end
  @client.fetch_access_token!
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



36
37
38
# File 'lib/gcloud/credentials.rb', line 36

def client
  @client
end

Class Method Details

.default(options = {}) ⇒ Object

Returns the default credentials.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/gcloud/credentials.rb', line 64

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

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