Class: Gcloud::Credentials

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

Overview

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

Direct Known Subclasses

Datastore::Credentials, Storage::Credentials

Constant Summary collapse

TOKEN_CREDENTIAL_URI =

:nodoc:

"https://accounts.google.com/o/oauth2/token"
AUDIENCE =
"https://accounts.google.com/o/oauth2/token"
SCOPE =
[]
ENV_VARS =
["GOOGLE_CLOUD_KEYFILE"]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Credentials.



40
41
42
43
44
45
46
47
# File 'lib/gcloud/credentials.rb', line 40

def initialize keyfile, options = {}
  if keyfile.is_a? Signet::OAuth2::Client
    @client = keyfile
  else
    @client = init_client keyfile, options
  end
  @client.fetch_access_token!
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



31
32
33
# File 'lib/gcloud/credentials.rb', line 31

def client
  @client
end

Class Method Details

.defaultObject

Returns the default credentials.



52
53
54
55
56
57
58
59
60
# File 'lib/gcloud/credentials.rb', line 52

def self.default
  self::ENV_VARS.each do |env_var|
    keyfile = ENV[env_var].to_s
    return new keyfile if ::File.file? keyfile
  end
  return new sdk_default_creds if ::File.file? sdk_default_creds
  client = Google::Auth.get_application_default self::SCOPE
  new client
end

.sdk_default_credsObject

The filepath of the default application credentials used by the gcloud SDK.

This file is created when running gcloud auth login



67
68
69
70
71
72
# File 'lib/gcloud/credentials.rb', line 67

def self.sdk_default_creds #:nodoc:
  # This method will likely be moved once we gain better
  # support for running in a GCE environment.
  sdk_creds = "~/.config/gcloud/application_default_credentials.json"
  File.expand_path sdk_creds
end