2017-10-12 16:19:43 +00:00
|
|
|
# Copyright 2017, Google Inc.
|
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are
|
|
|
|
# met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above
|
|
|
|
# copyright notice, this list of conditions and the following disclaimer
|
|
|
|
# in the documentation and/or other materials provided with the
|
|
|
|
# distribution.
|
|
|
|
# * Neither the name of Google Inc. nor the names of its
|
|
|
|
# contributors may be used to endorse or promote products derived from
|
|
|
|
# this software without specific prior written permission.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
2019-02-27 16:06:41 +00:00
|
|
|
require "forwardable"
|
|
|
|
require "json"
|
|
|
|
require "signet/oauth_2/client"
|
2017-10-12 16:19:43 +00:00
|
|
|
|
2019-02-27 16:06:41 +00:00
|
|
|
require "googleauth/credentials_loader"
|
2017-10-12 16:19:43 +00:00
|
|
|
|
|
|
|
module Google
|
|
|
|
module Auth
|
|
|
|
# This class is intended to be inherited by API-specific classes
|
|
|
|
# which overrides the SCOPE constant.
|
|
|
|
class Credentials
|
2019-02-27 16:06:41 +00:00
|
|
|
TOKEN_CREDENTIAL_URI = "https://oauth2.googleapis.com/token".freeze
|
|
|
|
AUDIENCE = "https://oauth2.googleapis.com/token".freeze
|
2017-10-12 16:19:43 +00:00
|
|
|
SCOPE = [].freeze
|
|
|
|
PATH_ENV_VARS = [].freeze
|
|
|
|
JSON_ENV_VARS = [].freeze
|
|
|
|
DEFAULT_PATHS = [].freeze
|
|
|
|
|
|
|
|
attr_accessor :client
|
2018-10-24 16:23:37 +00:00
|
|
|
attr_reader :project_id
|
2017-10-12 16:19:43 +00:00
|
|
|
|
|
|
|
# Delegate client methods to the client object.
|
|
|
|
extend Forwardable
|
|
|
|
def_delegators :@client,
|
|
|
|
:token_credential_uri, :audience,
|
|
|
|
:scope, :issuer, :signing_key, :updater_proc
|
|
|
|
|
2019-03-15 19:34:54 +00:00
|
|
|
# rubocop:disable Metrics/AbcSize
|
|
|
|
def initialize keyfile, options = {}
|
2017-10-12 16:19:43 +00:00
|
|
|
scope = options[:scope]
|
|
|
|
verify_keyfile_provided! keyfile
|
2019-02-27 16:06:41 +00:00
|
|
|
@project_id = options["project_id"] || options["project"]
|
2017-10-12 16:19:43 +00:00
|
|
|
if keyfile.is_a? Signet::OAuth2::Client
|
|
|
|
@client = keyfile
|
2018-10-24 16:23:37 +00:00
|
|
|
@project_id ||= keyfile.project_id if keyfile.respond_to? :project_id
|
2017-10-12 16:19:43 +00:00
|
|
|
elsif keyfile.is_a? Hash
|
|
|
|
hash = stringify_hash_keys keyfile
|
2019-02-27 16:06:41 +00:00
|
|
|
hash["scope"] ||= scope
|
2019-01-02 22:42:42 +00:00
|
|
|
@client = init_client hash, options
|
2019-02-27 16:06:41 +00:00
|
|
|
@project_id ||= (hash["project_id"] || hash["project"])
|
2017-10-12 16:19:43 +00:00
|
|
|
else
|
|
|
|
verify_keyfile_exists! keyfile
|
|
|
|
json = JSON.parse ::File.read(keyfile)
|
2019-02-27 16:06:41 +00:00
|
|
|
json["scope"] ||= scope
|
|
|
|
@project_id ||= (json["project_id"] || json["project"])
|
2019-01-02 22:42:42 +00:00
|
|
|
@client = init_client json, options
|
2017-10-12 16:19:43 +00:00
|
|
|
end
|
2018-07-18 20:54:03 +00:00
|
|
|
CredentialsLoader.warn_if_cloud_sdk_credentials @client.client_id
|
2018-10-24 16:23:37 +00:00
|
|
|
@project_id ||= CredentialsLoader.load_gcloud_project_id
|
2017-10-12 16:19:43 +00:00
|
|
|
@client.fetch_access_token!
|
|
|
|
end
|
2019-03-15 19:34:54 +00:00
|
|
|
# rubocop:enable Metrics/AbcSize
|
2017-10-12 16:19:43 +00:00
|
|
|
|
|
|
|
# Returns the default credentials checking, in this order, the path env
|
|
|
|
# evironment variables, json environment variables, default paths. If the
|
|
|
|
# previously stated locations do not contain keyfile information,
|
|
|
|
# this method defaults to use the application default.
|
2019-03-15 19:34:54 +00:00
|
|
|
def self.default options = {}
|
2017-10-12 16:19:43 +00:00
|
|
|
# First try to find keyfile file from environment variables.
|
2019-01-02 22:42:42 +00:00
|
|
|
client = from_path_vars options
|
2017-10-12 16:19:43 +00:00
|
|
|
|
|
|
|
# Second try to find keyfile json from environment variables.
|
2019-01-02 22:42:42 +00:00
|
|
|
client ||= from_json_vars options
|
2017-10-12 16:19:43 +00:00
|
|
|
|
|
|
|
# Third try to find keyfile file from known file paths.
|
2019-01-02 22:42:42 +00:00
|
|
|
client ||= from_default_paths options
|
2017-10-12 16:19:43 +00:00
|
|
|
|
|
|
|
# Finally get instantiated client from Google::Auth
|
2019-01-02 22:42:42 +00:00
|
|
|
client ||= from_application_default options
|
2017-10-12 16:19:43 +00:00
|
|
|
client
|
|
|
|
end
|
|
|
|
|
2019-03-15 19:34:54 +00:00
|
|
|
def self.from_path_vars options
|
2017-10-12 16:19:43 +00:00
|
|
|
self::PATH_ENV_VARS
|
|
|
|
.map { |v| ENV[v] }
|
|
|
|
.compact
|
|
|
|
.select { |p| ::File.file? p }
|
|
|
|
.each do |file|
|
2019-01-02 22:42:42 +00:00
|
|
|
return new file, options
|
2017-10-12 16:19:43 +00:00
|
|
|
end
|
2017-11-10 22:47:17 +00:00
|
|
|
nil
|
2017-10-12 16:19:43 +00:00
|
|
|
end
|
|
|
|
|
2019-03-15 19:34:54 +00:00
|
|
|
def self.from_json_vars options
|
2017-10-12 16:19:43 +00:00
|
|
|
json = lambda do |v|
|
|
|
|
unless ENV[v].nil?
|
|
|
|
begin
|
|
|
|
JSON.parse ENV[v]
|
2019-03-15 19:34:54 +00:00
|
|
|
rescue StandardError
|
2017-10-12 16:19:43 +00:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-03-15 19:34:54 +00:00
|
|
|
self::JSON_ENV_VARS.map(&json).compact.each { |hash| return new hash, options }
|
2017-11-10 22:47:17 +00:00
|
|
|
nil
|
2017-10-12 16:19:43 +00:00
|
|
|
end
|
|
|
|
|
2019-03-15 19:34:54 +00:00
|
|
|
def self.from_default_paths options
|
2017-10-12 16:19:43 +00:00
|
|
|
self::DEFAULT_PATHS
|
|
|
|
.select { |p| ::File.file? p }
|
|
|
|
.each do |file|
|
2019-01-02 22:42:42 +00:00
|
|
|
return new file, options
|
2017-10-12 16:19:43 +00:00
|
|
|
end
|
2017-11-10 22:47:17 +00:00
|
|
|
nil
|
2017-10-12 16:19:43 +00:00
|
|
|
end
|
|
|
|
|
2019-03-15 19:34:54 +00:00
|
|
|
def self.from_application_default options
|
2019-01-02 22:42:42 +00:00
|
|
|
scope = options[:scope] || self::SCOPE
|
2017-10-12 16:19:43 +00:00
|
|
|
client = Google::Auth.get_application_default scope
|
2019-01-02 22:42:42 +00:00
|
|
|
new client, options
|
2017-10-12 16:19:43 +00:00
|
|
|
end
|
|
|
|
private_class_method :from_path_vars,
|
|
|
|
:from_json_vars,
|
|
|
|
:from_default_paths,
|
|
|
|
:from_application_default
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
# Verify that the keyfile argument is provided.
|
2019-03-15 19:34:54 +00:00
|
|
|
def verify_keyfile_provided! keyfile
|
2017-10-12 16:19:43 +00:00
|
|
|
return unless keyfile.nil?
|
2019-02-27 16:06:41 +00:00
|
|
|
raise "The keyfile passed to Google::Auth::Credentials.new was nil."
|
2017-10-12 16:19:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Verify that the keyfile argument is a file.
|
2019-03-15 19:34:54 +00:00
|
|
|
def verify_keyfile_exists! keyfile
|
2017-10-12 16:19:43 +00:00
|
|
|
exists = ::File.file? keyfile
|
|
|
|
raise "The keyfile '#{keyfile}' is not a valid file." unless exists
|
|
|
|
end
|
|
|
|
|
|
|
|
# Initializes the Signet client.
|
2019-03-15 19:34:54 +00:00
|
|
|
def init_client keyfile, connection_options = {}
|
2017-10-12 16:19:43 +00:00
|
|
|
client_opts = client_options keyfile
|
2019-01-02 22:42:42 +00:00
|
|
|
Signet::OAuth2::Client.new(client_opts)
|
|
|
|
.configure_connection(connection_options)
|
2017-10-12 16:19:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# returns a new Hash with string keys instead of symbol keys.
|
2019-03-15 19:34:54 +00:00
|
|
|
def stringify_hash_keys hash
|
2017-10-12 16:19:43 +00:00
|
|
|
Hash[hash.map { |k, v| [k.to_s, v] }]
|
|
|
|
end
|
|
|
|
|
2019-03-15 19:34:54 +00:00
|
|
|
def client_options options
|
2017-10-12 16:19:43 +00:00
|
|
|
# Keyfile options have higher priority over constructor defaults
|
2019-02-27 16:06:41 +00:00
|
|
|
options["token_credential_uri"] ||= self.class::TOKEN_CREDENTIAL_URI
|
|
|
|
options["audience"] ||= self.class::AUDIENCE
|
|
|
|
options["scope"] ||= self.class::SCOPE
|
2017-10-12 16:19:43 +00:00
|
|
|
|
|
|
|
# client options for initializing signet client
|
2019-02-27 16:06:41 +00:00
|
|
|
{ token_credential_uri: options["token_credential_uri"],
|
2019-03-15 19:34:54 +00:00
|
|
|
audience: options["audience"],
|
|
|
|
scope: Array(options["scope"]),
|
|
|
|
issuer: options["client_email"],
|
|
|
|
signing_key: OpenSSL::PKey::RSA.new(options["private_key"]) }
|
2017-10-12 16:19:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|