Skip to content

Commit d7e862c

Browse files
authored
Add option to restrict overriding authorize_options by request params (#423)
1 parent aadbde5 commit d7e862c

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ You can configure several options, which you pass in to the `provider` method vi
8585

8686
* `provider_ignores_state`: You will need to set this to `true` when using the `One-time Code Flow` below. In this flow there is no server side redirect that would set the state.
8787

88+
* `overridable_authorize_options`: By default, all `authorize_options` can be overridden with request parameters. You can restrict the behavior by using this option.
89+
8890
Here's an example of a possible configuration where the strategy name is changed, the user is asked for extra permissions, the user is always prompted to select their account when logging in and the user's profile picture is returned as a thumbnail:
8991

9092
```ruby

lib/omniauth/strategies/google_oauth2.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ class GoogleOauth2 < OmniAuth::Strategies::OAuth2
1515
DEFAULT_SCOPE = 'email,profile'
1616
USER_INFO_URL = 'https://www.googleapis.com/oauth2/v3/userinfo'
1717
IMAGE_SIZE_REGEXP = /(s\d+(-c)?)|(w\d+-h\d+(-c)?)|(w\d+(-c)?)|(h\d+(-c)?)|c/
18+
AUTHORIZE_OPTIONS = %i[access_type hd login_hint prompt request_visible_actions scope state redirect_uri include_granted_scopes openid_realm device_id device_name]
1819

1920
option :name, 'google_oauth2'
2021
option :skip_friends, true
2122
option :skip_image_info, true
2223
option :skip_jwt, false
2324
option :jwt_leeway, 60
24-
option :authorize_options, %i[access_type hd login_hint prompt request_visible_actions scope state redirect_uri include_granted_scopes openid_realm device_id device_name]
25+
option :authorize_options, AUTHORIZE_OPTIONS
26+
option :overridable_authorize_options, AUTHORIZE_OPTIONS
2527
option :authorized_client_ids, []
2628

2729
option :client_options,
@@ -31,7 +33,7 @@ class GoogleOauth2 < OmniAuth::Strategies::OAuth2
3133

3234
def authorize_params
3335
super.tap do |params|
34-
options[:authorize_options].each do |k|
36+
(options[:authorize_options] & options[:overridable_authorize_options]).each do |k|
3537
params[k] = request.params[k.to_s] unless [nil, ''].include?(request.params[k.to_s])
3638
end
3739

spec/omniauth/strategies/google_oauth2_spec.rb

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -242,19 +242,37 @@
242242
context "authorize option #{k}" do
243243
let(:request) { double('Request', params: { k.to_s => 'http://example.com' }, cookies: {}, env: {}) }
244244

245-
it "should set the #{k} authorize option dynamically in the request" do
246-
@options = { k: '' }
247-
expect(subject.authorize_params[k.to_s]).to eq('http://example.com')
245+
context 'when overridable_authorize_options is default' do
246+
it "should set the #{k} authorize option dynamically in the request" do
247+
@options = { k: '' }
248+
expect(subject.authorize_params[k.to_s]).to eq('http://example.com')
249+
end
250+
end
251+
252+
context 'when overridable_authorize_options is empty' do
253+
it "should not set the #{k} authorize option dynamically in the request" do
254+
@options = { k: '', overridable_authorize_options: [] }
255+
expect(subject.authorize_params[k.to_s]).not_to eq('http://example.com')
256+
end
248257
end
249258
end
250259
end
251260

252261
describe 'custom authorize_options' do
253262
let(:request) { double('Request', params: { 'foo' => 'something' }, cookies: {}, env: {}) }
254263

255-
it 'should support request overrides from custom authorize_options' do
256-
@options = { authorize_options: [:foo], foo: '' }
257-
expect(subject.authorize_params['foo']).to eq('something')
264+
context 'when overridable_authorize_options is default' do
265+
it 'should not support request overrides from custom authorize_options' do
266+
@options = { authorize_options: [:foo], foo: '' }
267+
expect(subject.authorize_params['foo']).not_to eq('something')
268+
end
269+
end
270+
271+
context 'when overridable_authorize_options is customized' do
272+
it 'should support request overrides from custom authorize_options' do
273+
@options = { authorize_options: [:foo], overridable_authorize_options: [:foo], foo: '' }
274+
expect(subject.authorize_params['foo']).to eq('something')
275+
end
258276
end
259277
end
260278
end

0 commit comments

Comments
 (0)