[go: nahoru, domu]

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

[supply] added support for refresh tokens as an authentication method #16414

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
fixed exception when Client is created without any supply parameters
  • Loading branch information
moly committed May 13, 2020
commit f8100064bb7c5c7ac5a484f0b4171fb0f4022967
40 changes: 26 additions & 14 deletions supply/lib/supply/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ class AbstractGoogleServiceClient

def self.make_from_config(params: nil)
params ||= Supply.config
service_account_data = self.service_account_authentication(params: params)
return self.new(service_account_json: service_account_data, params: params)
if params[:json_key] || params[:json_key_data] || params[:key]
service_account_data = self.service_account_authentication(params: params)
return self.new(service_account_json: service_account_data, params: params)
elsif params[:refresh_token] || params[:refresh_token_data]
refresh_token_data = self.refresh_token_authentication(params: params)
return self.new(refresh_token_json: refresh_token_data, params: params)
else
UI.user_error!("No authentication parameters were specified. These must be provided in order to authenticate with Google")
end
end

# Supply authentication file
Expand All @@ -42,13 +49,26 @@ def self.service_account_authentication(params: nil)
service_account_json
end

# Supply refresh token file
def self.refresh_token_authentication(params: nil)
if params[:refresh_token]
refresh_token_json = File.open(File.expand_path(params[:refresh_token]))
elsif params[:refresh_token_data]
refresh_token_json = StringIO.new(params[:refresh_token_data])
end

refresh_token_json
end

# Initializes the service and its auth_client using the specified information
# Provide one of either service_account_json or refresh_token_json
# @param service_account_json: The raw service account Json data
def initialize(service_account_json: nil, params: nil)
if params[:json_key] || params[:json_key_data] || params[:key]
# @param refresh_token_json: The raw refresh token Json data
def initialize(service_account_json: nil, refresh_token_json: nil, params: nil)
if service_account_json
auth_client = Google::Auth::ServiceAccountCredentials.make_creds(json_key_io: service_account_json, scope: self.class::SCOPE)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is now failing for with NoMethodError: [!] undefined method gsub' for nil:NilClass `. Did this break for you?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I'd accidentally caused service_account_authentication to return nil when using a json key. Should be fixed now.

else
auth_client = Google::Auth::UserRefreshCredentials.make_creds(json_key_io: service_account_json, scope: self.class::SCOPE)
elsif refresh_token_json
auth_client = Google::Auth::UserRefreshCredentials.make_creds(json_key_io: refresh_token_json, scope: self.class::SCOPE)
end

UI.verbose("Fetching a new access token from Google...")
Expand Down Expand Up @@ -126,14 +146,6 @@ def self.service_account_authentication(params: nil)
}
service_account_json = StringIO.new(JSON.dump(cred_json))
service_account_json
elsif params[:refresh_token]
service_account_json = File.open(File.expand_path(params[:refresh_token]))
service_account_json
elsif params[:refresh_token_data]
service_account_json = StringIO.new(params[:refresh_token_data])
service_account_json
else
UI.user_error!("No authentication parameters were specified. These must be provided in order to authenticate with Google")
end
end

Expand Down