class Google::Cloud::Credentials
which overrides the SCOPE constant.
This class is intended to be inherited by API-specific classes
Represents the OAuth 2.0 signing logic.
@private
#
def self.default scope: nil
Returns the default credentials.
#
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).reject(&:nil?).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).reject(&:nil?).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
def client_options options
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
def init_client keyfile
#
def init_client keyfile client_opts = client_options keyfile Signet::OAuth2::Client.new client_opts end
def initialize keyfile, scope: nil
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
def stringify_hash_keys hash
#
def stringify_hash_keys hash Hash[hash.map { |(k, v)| [k.to_s, v] }] end
def verify_keyfile_exists! keyfile
#
def verify_keyfile_exists! keyfile exists = ::File.file? keyfile fail "The keyfile '#{keyfile}' is not a valid file." unless exists end
def verify_keyfile_provided! keyfile
#
def verify_keyfile_provided! keyfile fail "You must provide a keyfile to connect with." if keyfile.nil? end