class Google::Cloud::Credentials
@private Represents the OAuth 2.0 signing logic. This class is intended to be inherited by API-specific classes which overrides the SCOPE
constant.
Constants
- AUDIENCE
- DEFAULT_PATHS
- JSON_ENV_VARS
- PATH_ENV_VARS
- SCOPE
- TOKEN_CREDENTIAL_URI
Attributes
client[RW]
Public Class Methods
default(scope: nil)
click to toggle source
Returns the default credentials.
# File lib/google/cloud/credentials.rb, line 70 def self.default scope: nil 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).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
new(keyfile, scope: nil)
click to toggle source
# File lib/google/cloud/credentials.rb, line 49 def initialize keyfile, scope: nil verify_keyfile_provided! keyfile case keyfile when Signet::OAuth2::Client @client = keyfile when 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
Public Instance Methods
unknown()
click to toggle source
Delegate client methods to the client object.
# File lib/google/cloud/credentials.rb, line 44 extend Forwardable
Protected Instance Methods
client_options(options)
click to toggle source
# File lib/google/cloud/credentials.rb, line 121 def client_options options # Keyfile options have higher priority over constructor defaults options["token_credential_uri"] ||= self.class::TOKEN_CREDENTIAL_URI options["audience"] ||= self.class::AUDIENCE options["scope"] ||= self.class::SCOPE # client options for initializing signet client { token_credential_uri: options["token_credential_uri"], audience: options["audience"], scope: Array(options["scope"]), issuer: options["client_email"], signing_key: OpenSSL::PKey::RSA.new(options["private_key"]) } end
init_client(keyfile)
click to toggle source
Initializes the Signet client.
# File lib/google/cloud/credentials.rb, line 110 def init_client keyfile client_opts = client_options keyfile Signet::OAuth2::Client.new client_opts end
stringify_hash_keys(hash)
click to toggle source
returns a new Hash with string keys instead of symbol keys.
# File lib/google/cloud/credentials.rb, line 117 def stringify_hash_keys hash hash.transform_keys(&:to_s) end
verify_keyfile_exists!(keyfile)
click to toggle source
Verify that the keyfile argument is a file.
# File lib/google/cloud/credentials.rb, line 103 def verify_keyfile_exists! keyfile exists = ::File.file? keyfile raise "The keyfile '#{keyfile}' is not a valid file." unless exists end
verify_keyfile_provided!(keyfile)
click to toggle source
Verify that the keyfile argument is provided.
# File lib/google/cloud/credentials.rb, line 97 def verify_keyfile_provided! keyfile raise "You must provide a keyfile to connect with." if keyfile.nil? end