Class: Gcloud::Credentials
- Inherits:
-
Object
- Object
- Gcloud::Credentials
- 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.
Direct Known Subclasses
Datastore::Credentials, Pubsub::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 =
[]
- 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
-
#client ⇒ Object
Returns the value of attribute client.
Class Method Summary collapse
-
.default(options = {}) ⇒ Object
Returns the default credentials.
Instance Method Summary collapse
-
#initialize(keyfile, options = {}) ⇒ Credentials
constructor
A new instance of Credentials.
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, = {} verify_keyfile_provided! keyfile if keyfile.is_a? Signet::OAuth2::Client @client = keyfile elsif keyfile.is_a? Hash @client = init_client keyfile, else verify_keyfile_exists! keyfile @client = init_client JSON.parse(::File.read(keyfile)), end @client.fetch_access_token! end |
Instance Attribute Details
#client ⇒ Object
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 = {} 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, end # Second try to find keyfile json from environment variables. self::JSON_ENV_VARS.map(&json).reject(&:nil?).each do |hash| return new hash, end # Third try to find keyfile file from known file paths. self::DEFAULT_PATHS.select(&path).each do |file| return new file, end # Finally get instantiated client from Google::Auth. scope = [:scope] || ["scope"] || self::SCOPE client = Google::Auth.get_application_default scope new client end |