diff --git a/lib/googleauth.rb b/lib/googleauth.rb index 65ea0f4..bc767d4 100644 --- a/lib/googleauth.rb +++ b/lib/googleauth.rb @@ -52,9 +52,9 @@ END # override CredentialsLoader#make_creds to use the class determined by # loading the json. - def self.make_creds(scope, json_key_io) + def self.make_creds(json_key_io, scope = nil) json_key, clz = determine_creds_class(json_key_io) - clz.new(scope, StringIO.new(MultiJson.dump(json_key))) + clz.new(StringIO.new(MultiJson.dump(json_key)), scope) end # Reads the input json and determines which creds class to use. @@ -76,11 +76,11 @@ END # at http://goo.gl/IUuyuX. # # If supplied, scope is used to create the credentials instance, when it - # can applied. E.g, on compute engine, the scope is ignored. + # can applied. E.g, on google compute engine, the scope is ignored. # - # @param scope [string|array] the scope(s) to access + # @param scope [string|array|nil] the scope(s) to access # @param options [hash] allows override of the connection being used - def get_application_default(scope, options = {}) + def get_application_default(scope = nil, options = {}) creds = DefaultCredentials.from_env(scope) return creds unless creds.nil? creds = DefaultCredentials.from_well_known_path(scope) diff --git a/lib/googleauth/credentials_loader.rb b/lib/googleauth/credentials_loader.rb index f4ea619..f5ed2b2 100644 --- a/lib/googleauth/credentials_loader.rb +++ b/lib/googleauth/credentials_loader.rb @@ -61,13 +61,13 @@ module Google # Creates an instance from the path specified in an environment # variable. # - # @param scope [string|array] the scope(s) to access - def from_env(scope) + # @param scope [string|array|nil] the scope(s) to access + def from_env(scope = nil) return nil unless ENV.key?(ENV_VAR) path = ENV[ENV_VAR] fail 'file #{path} does not exist' unless File.exist?(path) File.open(path) do |f| - return make_creds(scope, f) + return make_creds(f, scope) end rescue StandardError => e raise "#{NOT_FOUND_ERROR}: #{e}" @@ -75,15 +75,15 @@ module Google # Creates an instance from a well known path. # - # @param scope [string|array] the scope(s) to access - def from_well_known_path(scope) + # @param scope [string|array|nil] the scope(s) to access + def from_well_known_path(scope = nil) home_var, base = windows? ? 'APPDATA' : 'HOME', WELL_KNOWN_PATH root = ENV[home_var].nil? ? '' : ENV[home_var] base = File.join('.config', base) unless windows? path = File.join(root, base) return nil unless File.exist?(path) File.open(path) do |f| - return make_creds(scope, f) + return make_creds(f, scope) end rescue StandardError => e raise "#{WELL_KNOWN_ERROR}: #{e}" diff --git a/lib/googleauth/service_account.rb b/lib/googleauth/service_account.rb index fed67ca..66d7be6 100644 --- a/lib/googleauth/service_account.rb +++ b/lib/googleauth/service_account.rb @@ -57,9 +57,9 @@ module Google # Initializes a ServiceAccountCredentials. # - # @param scope [string|array] the scope(s) to access # @param json_key_io [IO] an IO from which the JSON key can be read - def initialize(scope, json_key_io) + # @param scope [string|array|nil] the scope(s) to access + def initialize(json_key_io, scope = nil) private_key, client_email = self.class.read_json_key(json_key_io) super(token_credential_uri: TOKEN_CRED_URI, audience: TOKEN_CRED_URI, diff --git a/lib/googleauth/user_refresh.rb b/lib/googleauth/user_refresh.rb index 3cfb1db..4256936 100644 --- a/lib/googleauth/user_refresh.rb +++ b/lib/googleauth/user_refresh.rb @@ -61,9 +61,9 @@ module Google # Initializes a UserRefreshCredentials. # - # @param scope [string|array] the scope(s) to access # @param json_key_io [IO] an IO from which the JSON key can be read - def initialize(scope, json_key_io) + # @param scope [string|array|nil] the scope(s) to access + def initialize(json_key_io, scope = nil) user_creds = self.class.read_json_key(json_key_io) super(token_credential_uri: TOKEN_CRED_URI, client_id: user_creds['client_id'], diff --git a/spec/googleauth/get_application_default_spec.rb b/spec/googleauth/get_application_default_spec.rb index 28ec6cd..43556ea 100644 --- a/spec/googleauth/get_application_default_spec.rb +++ b/spec/googleauth/get_application_default_spec.rb @@ -104,6 +104,18 @@ describe '#get_application_default' do end end + it 'succeeds with default file without a scope' do + ENV.delete(@var_name) unless ENV[@var_name].nil? + Dir.mktmpdir do |dir| + key_path = File.join(dir, '.config', + CredentialsLoader::WELL_KNOWN_PATH) + FileUtils.mkdir_p(File.dirname(key_path)) + File.write(key_path, cred_json_text) + ENV['HOME'] = dir + expect(Google::Auth.get_application_default).to_not be_nil + end + end + it 'succeeds without default file or env if on compute engine' do stubs = Faraday::Adapter::Test::Stubs.new do |stub| stub.get('/') do |_env| diff --git a/spec/googleauth/service_account_spec.rb b/spec/googleauth/service_account_spec.rb index 6298116..caaa878 100644 --- a/spec/googleauth/service_account_spec.rb +++ b/spec/googleauth/service_account_spec.rb @@ -47,8 +47,8 @@ describe Google::Auth::ServiceAccountCredentials do before(:example) do @key = OpenSSL::PKey::RSA.new(2048) @client = ServiceAccountCredentials.new( - 'https://www.googleapis.com/auth/userinfo.profile', - StringIO.new(cred_json_text)) + StringIO.new(cred_json_text), + 'https://www.googleapis.com/auth/userinfo.profile') end def make_auth_stubs(opts = {}) diff --git a/spec/googleauth/user_refresh_spec.rb b/spec/googleauth/user_refresh_spec.rb index bbf23d2..eced027 100644 --- a/spec/googleauth/user_refresh_spec.rb +++ b/spec/googleauth/user_refresh_spec.rb @@ -47,8 +47,8 @@ describe Google::Auth::UserRefreshCredentials do before(:example) do @key = OpenSSL::PKey::RSA.new(2048) @client = UserRefreshCredentials.new( - 'https://www.googleapis.com/auth/userinfo.profile', - StringIO.new(cred_json_text)) + StringIO.new(cred_json_text), + 'https://www.googleapis.com/auth/userinfo.profile') end def make_auth_stubs(opts = {})