Skip to content

Commit

Permalink
feat: Pass additional parameters to auhtorization url (#447)
Browse files Browse the repository at this point in the history
  • Loading branch information
bajajneha27 committed Sep 7, 2023
1 parent dddbf2a commit a3cdc48
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
17 changes: 12 additions & 5 deletions lib/googleauth/user_authorizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,17 @@ def initialize client_id, scope, token_store, callback_uri = nil
# @param [String, Array<String>] 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 = {}
scope = options[:scope] || @scope
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",
Expand Down Expand Up @@ -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 = {}
Expand All @@ -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!({})
Expand Down
16 changes: 16 additions & 0 deletions spec/googleauth/user_authorizer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit a3cdc48

Please sign in to comment.