Skip to content

Commit 02ffbcf

Browse files
committed
Output granted scopes in credentials block of the auth hash
1 parent cfbf43f commit 02ffbcf

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

lib/omniauth/strategies/google_oauth2.rb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ def authorize_params
6060
)
6161
end
6262

63+
credentials do
64+
# Tokens and expiration will be used from OAuth2 strategy credentials block
65+
prune!({ 'scope' => token_info(access_token.token)['scope'] })
66+
end
67+
6368
extra do
6469
hash = {}
6570
hash[:id_token] = access_token['id_token']
@@ -215,12 +220,21 @@ def strip_unnecessary_query_parameters(query_parameters)
215220
URI.encode_www_form(stripped_params)
216221
end
217222

223+
def token_info(access_token)
224+
return nil unless access_token
225+
226+
@token_info ||= Hash.new do |h, k|
227+
h[k] = client.request(:get, 'https://www.googleapis.com/oauth2/v3/tokeninfo', params: { access_token: access_token }).parsed
228+
end
229+
230+
@token_info[access_token]
231+
end
232+
218233
def verify_token(access_token)
219234
return false unless access_token
220235

221-
raw_response = client.request(:get, 'https://www.googleapis.com/oauth2/v3/tokeninfo',
222-
params: { access_token: access_token }).parsed
223-
raw_response['aud'] == options.client_id || options.authorized_client_ids.include?(raw_response['aud'])
236+
token_info = token_info(access_token)
237+
token_info['aud'] == options.client_id || options.authorized_client_ids.include?(token_info['aud'])
224238
end
225239

226240
def verify_hd(access_token)

spec/omniauth/strategies/google_oauth2_spec.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,13 +350,28 @@
350350
describe '#credentials' do
351351
let(:client) { OAuth2::Client.new('abc', 'def') }
352352
let(:access_token) { OAuth2::AccessToken.from_hash(client, access_token: 'valid_access_token', expires_at: 123_456_789, refresh_token: 'valid_refresh_token') }
353-
before(:each) { allow(subject).to receive(:access_token).and_return(access_token) }
353+
before(:each) do
354+
allow(subject).to receive(:access_token).and_return(access_token)
355+
subject.options.client_options[:connection_build] = proc do |builder|
356+
builder.request :url_encoded
357+
builder.adapter :test do |stub|
358+
stub.get('/oauth2/v3/tokeninfo?access_token=valid_access_token') do
359+
[200, { 'Content-Type' => 'application/json; charset=UTF-8' }, JSON.dump(
360+
aud: '000000000000.apps.googleusercontent.com',
361+
sub: '123456789',
362+
scope: 'profile email'
363+
)]
364+
end
365+
end
366+
end
367+
end
354368

355369
it 'should return access token and (optionally) refresh token' do
356370
expect(subject.credentials.to_h).to \
357371
match(hash_including(
358372
'token' => 'valid_access_token',
359373
'refresh_token' => 'valid_refresh_token',
374+
'scope' => 'profile email',
360375
'expires_at' => 123_456_789,
361376
'expires' => true
362377
))

0 commit comments

Comments
 (0)