Skip to content

Commit 1b3a35c

Browse files
committed
Include the received access token's scope in the 'extra' hash
According to [GitHub's documentation](https://developer.github.com/apps/building-oauth-apps/understanding-scopes-for-oauth-apps/#requested-scopes-and-granted-scopes): > The scope attribute lists scopes attached to the token that were granted > by the user. Normally, these scopes will be identical to what you > requested. However, users can edit their scopes, effectively granting > your application less access than you originally requested. Also, users > can edit token scopes after the OAuth flow is completed. You should be > aware of this possibility and adjust your application's behavior > accordingly. Therefore, include the scope returned with the OAuth token in the 'extra' hash generated for the omniauth callback. According to the OAuth2 gem's code, extra params returned with the access token response can accessed via indexing on the AccessToken class: https://github.com/oauth-xx/oauth2/blob/58471c95c5473d9a494e45534df96f0cf935a2bb/lib/oauth2/access_token.rb#L60-L65
1 parent 2e77639 commit 1b3a35c

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

Diff for: lib/omniauth/strategies/github.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def authorize_params
3939
end
4040

4141
extra do
42-
{:raw_info => raw_info, :all_emails => emails}
42+
{:raw_info => raw_info, :all_emails => emails, :scope => scope }
4343
end
4444

4545
def raw_info
@@ -51,6 +51,10 @@ def email
5151
(email_access_allowed?) ? primary_email : raw_info['email']
5252
end
5353

54+
def scope
55+
access_token['scope']
56+
end
57+
5458
def primary_email
5559
primary = emails.find{ |i| i['primary'] && i['verified'] }
5660
primary && primary['email'] || nil

Diff for: spec/omniauth/strategies/github_spec.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'spec_helper'
22

33
describe OmniAuth::Strategies::GitHub do
4-
let(:access_token) { instance_double('AccessToken', :options => {}) }
4+
let(:access_token) { instance_double('AccessToken', :options => {}, :[] => 'user') }
55
let(:parsed_response) { instance_double('ParsedResponse') }
66
let(:response) { instance_double('Response', :parsed => parsed_response) }
77

@@ -150,6 +150,12 @@
150150
end
151151
end
152152

153+
context '#extra.scope' do
154+
it 'returns the scope on the returned access_token' do
155+
expect(subject.scope).to eq('user')
156+
end
157+
end
158+
153159
describe '#callback_url' do
154160
it 'is a combination of host, script name, and callback path' do
155161
allow(subject).to receive(:full_host).and_return('https://example.com')

0 commit comments

Comments
 (0)