Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruby auth make scopes optional #17

Merged
merged 4 commits into from
Mar 23, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/googleauth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ class DefaultCredentials

# 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.
Expand All @@ -76,11 +76,11 @@ def self.determine_creds_class(json_key_io)
# 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.

This comment was marked as spam.

This comment was marked as spam.

#
# @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)
Expand Down
12 changes: 6 additions & 6 deletions lib/googleauth/credentials_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,29 @@ def make_creds(*args)
# 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}"
end

# 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}"
Expand Down
4 changes: 2 additions & 2 deletions lib/googleauth/service_account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ def self.read_json_key(json_key_io)

# 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,
Expand Down
4 changes: 2 additions & 2 deletions lib/googleauth/user_refresh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ def self.read_json_key(json_key_io)

# 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'],
Expand Down
2 changes: 1 addition & 1 deletion lib/googleauth/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ module Google
# Module Auth provides classes that provide Google-specific authorization
# used to access Google APIs.
module Auth
VERSION = '0.2.0'
VERSION = '0.3.0'
end
end
12 changes: 12 additions & 0 deletions spec/googleauth/get_application_default_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@
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|
Expand Down
4 changes: 2 additions & 2 deletions spec/googleauth/service_account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
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 = {})
Expand Down
4 changes: 2 additions & 2 deletions spec/googleauth/user_refresh_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
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 = {})
Expand Down