Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ You can configure several options, which you pass in to the `provider` method vi

* `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.

* `overridable_authorize_options`: By default, all `authorize_options` can be overridden with request parameters. You can restrict the behavior by using this option.

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:

```ruby
Expand Down
6 changes: 4 additions & 2 deletions lib/omniauth/strategies/google_oauth2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ class GoogleOauth2 < OmniAuth::Strategies::OAuth2
DEFAULT_SCOPE = 'email,profile'
USER_INFO_URL = 'https://www.googleapis.com/oauth2/v3/userinfo'
IMAGE_SIZE_REGEXP = /(s\d+(-c)?)|(w\d+-h\d+(-c)?)|(w\d+(-c)?)|(h\d+(-c)?)|c/
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]

option :name, 'google_oauth2'
option :skip_friends, true
option :skip_image_info, true
option :skip_jwt, false
option :jwt_leeway, 60
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]
option :authorize_options, AUTHORIZE_OPTIONS
option :overridable_authorize_options, AUTHORIZE_OPTIONS
option :authorized_client_ids, []

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

def authorize_params
super.tap do |params|
options[:authorize_options].each do |k|
(options[:authorize_options] & options[:overridable_authorize_options]).each do |k|
params[k] = request.params[k.to_s] unless [nil, ''].include?(request.params[k.to_s])
end

Expand Down
30 changes: 24 additions & 6 deletions spec/omniauth/strategies/google_oauth2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,19 +242,37 @@
context "authorize option #{k}" do
let(:request) { double('Request', params: { k.to_s => 'http://example.com' }, cookies: {}, env: {}) }

it "should set the #{k} authorize option dynamically in the request" do
@options = { k: '' }
expect(subject.authorize_params[k.to_s]).to eq('http://example.com')
context 'when overridable_authorize_options is default' do
it "should set the #{k} authorize option dynamically in the request" do
@options = { k: '' }
expect(subject.authorize_params[k.to_s]).to eq('http://example.com')
end
end

context 'when overridable_authorize_options is empty' do
it "should not set the #{k} authorize option dynamically in the request" do
@options = { k: '', overridable_authorize_options: [] }
expect(subject.authorize_params[k.to_s]).not_to eq('http://example.com')
end
end
end
end

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

it 'should support request overrides from custom authorize_options' do
@options = { authorize_options: [:foo], foo: '' }
expect(subject.authorize_params['foo']).to eq('something')
context 'when overridable_authorize_options is default' do
it 'should not support request overrides from custom authorize_options' do
@options = { authorize_options: [:foo], foo: '' }
expect(subject.authorize_params['foo']).not_to eq('something')
end
end

context 'when overridable_authorize_options is customized' do
it 'should support request overrides from custom authorize_options' do
@options = { authorize_options: [:foo], overridable_authorize_options: [:foo], foo: '' }
expect(subject.authorize_params['foo']).to eq('something')
end
end
end
end
Expand Down