Skip to content

Commit

Permalink
Include the received token's scope in the 'extra' hash
Browse files Browse the repository at this point in the history
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
  • Loading branch information
davebrace committed Jan 20, 2019
1 parent 2e77639 commit 471a791
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/omniauth/strategies/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def authorize_params
end

extra do
{:raw_info => raw_info, :all_emails => emails}
{:raw_info => raw_info, :all_emails => emails, :scope => scope }
end

def raw_info
Expand All @@ -51,6 +51,10 @@ def email
(email_access_allowed?) ? primary_email : raw_info['email']
end

def scope
access_token['scope']
end

def primary_email
primary = emails.find{ |i| i['primary'] && i['verified'] }
primary && primary['email'] || nil
Expand Down
8 changes: 7 additions & 1 deletion spec/omniauth/strategies/github_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'spec_helper'

describe OmniAuth::Strategies::GitHub do
let(:access_token) { instance_double('AccessToken', :options => {}) }
let(:access_token) { instance_double('AccessToken', :options => {}, :[] => 'user') }
let(:parsed_response) { instance_double('ParsedResponse') }
let(:response) { instance_double('Response', :parsed => parsed_response) }

Expand Down Expand Up @@ -150,6 +150,12 @@
end
end

context '#extra.scope' do
it 'returns the scope on the returned access_token' do
expect(subject.scope).to eq('user')
end
end

describe '#callback_url' do
it 'is a combination of host, script name, and callback path' do
allow(subject).to receive(:full_host).and_return('https://example.com')
Expand Down

0 comments on commit 471a791

Please sign in to comment.