Skip to content

Commit 71d6216

Browse files
committed
Add Github Enterprise Support
* In addition to scope, client_options can now be passed in to replace the ones hard coded in the gem * Modify GET requests to use relative URLs instead of absolute URLs * Added specs to verify client_options are correctly overridden and relative URLs are utilized [Fixes #23]
1 parent 8589e2d commit 71d6216

File tree

3 files changed

+72
-7
lines changed

3 files changed

+72
-7
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,18 @@ on the [GitHub Applications Page](https://github.com/settings/applications).
1010
provider :github, ENV['GITHUB_KEY'], ENV['GITHUB_SECRET']
1111
end
1212

13-
### Scopes
13+
## Github Enterprise Usage
14+
15+
provider :github, ENV['GITHUB_KEY'], ENV['GITHUB_SECRET'],
16+
{
17+
:client_options => {
18+
:site => 'https://github.YOURDOMAIN.com/api/v3',
19+
:authorize_url => 'https://github.YOURDOMAIN.com/login/oauth/authorize',
20+
:token_url => 'https://github.YOURDOMAIN.com/login/oauth/access_token',
21+
}
22+
}
23+
24+
## Scopes
1425

1526
GitHub API v3 lets you set scopes to provide granular access to different types of data:
1627

lib/omniauth/strategies/github.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ def request_phase
1414
end
1515

1616
def authorize_params
17-
if request.params["scope"]
18-
super.merge({:scope => request.params["scope"]})
19-
else
20-
super
17+
super.tap do |params|
18+
%w[scope client_options].each do |v|
19+
if request.params[v]
20+
params[v.to_sym] = request.params[v]
21+
end
22+
end
2123
end
2224
end
2325

@@ -42,7 +44,7 @@ def authorize_params
4244

4345
def raw_info
4446
access_token.options[:mode] = :query
45-
@raw_info ||= access_token.get('/user').parsed
47+
@raw_info ||= access_token.get('user').parsed
4648
end
4749

4850
def email
@@ -58,7 +60,7 @@ def primary_email
5860
def emails
5961
return [] unless email_access_allowed?
6062
access_token.options[:mode] = :query
61-
@emails ||= access_token.get('/user/emails', :headers => { 'Accept' => 'application/vnd.github.v3' }).parsed
63+
@emails ||= access_token.get('user/emails', :headers => { 'Accept' => 'application/vnd.github.v3' }).parsed
6264
end
6365

6466
def email_access_allowed?

spec/omniauth/strategies/github_spec.rb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,33 @@
11
require 'spec_helper'
22

33
describe OmniAuth::Strategies::GitHub do
4+
let(:access_token) { stub('AccessToken', :options => {}) }
5+
let(:parsed_response) { stub('ParsedResponse') }
6+
let(:response) { stub('Response', :parsed => parsed_response) }
7+
8+
let(:enterprise_site) { 'https://some.other.site.com/api/v3' }
9+
let(:enterprise_authorize_url) { 'https://some.other.site.com/login/oauth/authorize' }
10+
let(:enterprise_token_url) { 'https://some.other.site.com/login/oauth/access_token' }
11+
let(:enterprise) do
12+
OmniAuth::Strategies::GitHub.new('GITHUB_KEY', 'GITHUB_SECRET',
13+
{
14+
:client_options => {
15+
:site => enterprise_site,
16+
:authorize_url => enterprise_authorize_url,
17+
:token_url => enterprise_token_url
18+
}
19+
}
20+
)
21+
end
22+
423
subject do
524
OmniAuth::Strategies::GitHub.new({})
625
end
726

27+
before(:each) do
28+
subject.stub!(:access_token).and_return(access_token)
29+
end
30+
831
context "client options" do
932
it 'should have correct site' do
1033
subject.options.client_options.site.should eq("https://api.github.com")
@@ -17,6 +40,20 @@
1740
it 'should have correct token url' do
1841
subject.options.client_options.token_url.should eq('https://github.com/login/oauth/access_token')
1942
end
43+
44+
describe "should be overrideable" do
45+
it "for site" do
46+
enterprise.options.client_options.site.should eq(enterprise_site)
47+
end
48+
49+
it "for authorize url" do
50+
enterprise.options.client_options.authorize_url.should eq(enterprise_authorize_url)
51+
end
52+
53+
it "for token url" do
54+
enterprise.options.client_options.token_url.should eq(enterprise_token_url)
55+
end
56+
end
2057
end
2158

2259
context "#email_access_allowed?" do
@@ -80,4 +117,19 @@
80117
end
81118
end
82119

120+
context "#raw_info" do
121+
it "should use relative paths" do
122+
access_token.should_receive(:get).with('user').and_return(response)
123+
subject.raw_info.should eq(parsed_response)
124+
end
125+
end
126+
127+
context "#emails" do
128+
it "should use relative paths" do
129+
access_token.should_receive(:get).with('user/emails', :headers=>{"Accept"=>"application/vnd.github.v3"}).and_return(response)
130+
subject.options['scope'] = 'user'
131+
subject.emails.should eq(parsed_response)
132+
end
133+
end
134+
83135
end

0 commit comments

Comments
 (0)