From a3cdc48ef072368d5993de98fcbe6a7db4982c2e Mon Sep 17 00:00:00 2001 From: Neha Bajaj Date: Thu, 7 Sep 2023 12:03:36 +0530 Subject: [PATCH] feat: Pass additional parameters to auhtorization url (#447) --- lib/googleauth/user_authorizer.rb | 17 ++++++++++++----- spec/googleauth/user_authorizer_spec.rb | 16 ++++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/lib/googleauth/user_authorizer.rb b/lib/googleauth/user_authorizer.rb index 59168a0b..92988176 100644 --- a/lib/googleauth/user_authorizer.rb +++ b/lib/googleauth/user_authorizer.rb @@ -80,6 +80,8 @@ def initialize client_id, scope, token_store, callback_uri = nil # @param [String, Array] scope # Authorization scope to request. Overrides the instance scopes if not # nil. + # @param [Hash] additional_parameters + # Additional query parameters to be added to the authorization URL. # @return [String] # Authorization url def get_authorization_url options = {} @@ -87,7 +89,8 @@ def get_authorization_url options = {} credentials = UserRefreshCredentials.new( client_id: @client_id.id, client_secret: @client_id.secret, - scope: scope + scope: scope, + additional_parameters: options[:additional_parameters] ) redirect_uri = redirect_uri_for options[:base_url] url = credentials.authorization_uri(access_type: "offline", @@ -144,6 +147,9 @@ def get_credentials user_id, scope = nil # Absolute URL to resolve the configured callback uri against. # Required if the configured # callback uri is a relative. + # @param [Hash] additional_parameters + # Additional parameters to be added to the post body of token + # endpoint request. # @return [Google::Auth::UserRefreshCredentials] # Credentials if exchange is successful def get_credentials_from_code options = {} @@ -152,10 +158,11 @@ def get_credentials_from_code options = {} scope = options[:scope] || @scope base_url = options[:base_url] credentials = UserRefreshCredentials.new( - client_id: @client_id.id, - client_secret: @client_id.secret, - redirect_uri: redirect_uri_for(base_url), - scope: scope + client_id: @client_id.id, + client_secret: @client_id.secret, + redirect_uri: redirect_uri_for(base_url), + scope: scope, + additional_parameters: options[:additional_parameters] ) credentials.code = code credentials.fetch_access_token!({}) diff --git a/spec/googleauth/user_authorizer_spec.rb b/spec/googleauth/user_authorizer_spec.rb index eabdcfab..2cde3245 100644 --- a/spec/googleauth/user_authorizer_spec.rb +++ b/spec/googleauth/user_authorizer_spec.rb @@ -29,6 +29,9 @@ let(:scope) { %w[email profile] } let(:token_store) { DummyTokenStore.new } let(:callback_uri) { "https://www.example.com/oauth/callback" } + let(:additional_parameters) do + { "param1": "value1", "param2": "value2" } + end let :authorizer do Google::Auth::UserAuthorizer.new(client_id, scope, @@ -139,6 +142,19 @@ end end + context "when generating authorization URLs with additional parameters" do + let(:uri) { authorizer.get_authorization_url additional_parameters: additional_parameters } + + it_behaves_like "valid authorization url" + + it "includes the additional parameters" do + expect(URI(uri).query).to match(/param1/) + expect(URI(uri).query).to match(/value1/) + expect(URI(uri).query).to match(/param2/) + expect(URI(uri).query).to match(/value2/) + end + end + context "when retrieving tokens" do let :token_json do MultiJson.dump(