omniauth-github 1.0.3 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -10,7 +10,18 @@ on the [GitHub Applications Page](https://github.com/settings/applications).
10
10
  provider :github, ENV['GITHUB_KEY'], ENV['GITHUB_SECRET']
11
11
  end
12
12
 
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
14
25
 
15
26
  GitHub API v3 lets you set scopes to provide granular access to different types of data:
16
27
 
@@ -1,5 +1,5 @@
1
1
  module OmniAuth
2
2
  module GitHub
3
- VERSION = "1.0.3"
3
+ VERSION = "1.1.0"
4
4
  end
5
5
  end
@@ -12,6 +12,16 @@ module OmniAuth
12
12
  def request_phase
13
13
  super
14
14
  end
15
+
16
+ def authorize_params
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
23
+ end
24
+ end
15
25
 
16
26
  uid { raw_info['id'].to_s }
17
27
 
@@ -34,20 +44,27 @@ module OmniAuth
34
44
 
35
45
  def raw_info
36
46
  access_token.options[:mode] = :query
37
- @raw_info ||= access_token.get('/user').parsed
47
+ @raw_info ||= access_token.get('user').parsed
38
48
  end
39
49
 
40
50
  def email
41
- raw_info['email'] || (email_access_allowed? ? emails.first : nil)
51
+ raw_info['email'] || primary_email
52
+ end
53
+
54
+ def primary_email
55
+ primary = emails.find{|i| i['primary'] }
56
+ primary && primary['email'] || emails.first && emails.first['email']
42
57
  end
43
58
 
59
+ # The new /user/emails API - http://developer.github.com/v3/users/emails/#future-response
44
60
  def emails
61
+ return [] unless email_access_allowed?
45
62
  access_token.options[:mode] = :query
46
- @emails ||= access_token.get('/user/emails').parsed
63
+ @emails ||= access_token.get('user/emails', :headers => { 'Accept' => 'application/vnd.github.v3' }).parsed
47
64
  end
48
65
 
49
66
  def email_access_allowed?
50
- options['scope'] && !(options['scope'] == 'public')
67
+ options['scope'] =~ /user/
51
68
  end
52
69
 
53
70
  end
@@ -1,10 +1,33 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  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
+
4
23
  subject do
5
24
  OmniAuth::Strategies::GitHub.new({})
6
25
  end
7
26
 
27
+ before(:each) do
28
+ subject.stub!(:access_token).and_return(access_token)
29
+ end
30
+
8
31
  context "client options" do
9
32
  it 'should have correct site' do
10
33
  subject.options.client_options.site.should eq("https://api.github.com")
@@ -17,6 +40,20 @@ describe OmniAuth::Strategies::GitHub do
17
40
  it 'should have correct token url' do
18
41
  subject.options.client_options.token_url.should eq('https://github.com/login/oauth/access_token')
19
42
  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
20
57
  end
21
58
 
22
59
  context "#email_access_allowed?" do
@@ -25,24 +62,24 @@ describe OmniAuth::Strategies::GitHub do
25
62
  subject.should_not be_email_access_allowed
26
63
  end
27
64
 
28
- it "should not allow email if scope is 'public'" do
29
- subject.options['scope'] = 'public'
30
- subject.should_not be_email_access_allowed
31
- end
32
-
33
65
  it "should allow email if scope is user" do
34
66
  subject.options['scope'] = 'user'
35
67
  subject.should be_email_access_allowed
36
68
  end
37
69
 
38
- it "should allow email if scope is scope is a bunch of stuff" do
39
- subject.options['scope'] = 'user,public_repo,repo,delete_repo,gist'
70
+ it "should allow email if scope is a bunch of stuff including user" do
71
+ subject.options['scope'] = 'public_repo,user,repo,delete_repo,gist'
40
72
  subject.should be_email_access_allowed
41
73
  end
42
74
 
43
- it "should assume email access allowed if scope is scope is something currently not documented " do
75
+ it "should not allow email if scope is other than user" do
76
+ subject.options['scope'] = 'repo'
77
+ subject.should_not be_email_access_allowed
78
+ end
79
+
80
+ it "should assume email access not allowed if scope is something currently not documented " do
44
81
  subject.options['scope'] = 'currently_not_documented'
45
- subject.should be_email_access_allowed
82
+ subject.should_not be_email_access_allowed
46
83
  end
47
84
  end
48
85
 
@@ -57,11 +94,41 @@ describe OmniAuth::Strategies::GitHub do
57
94
  subject.email.should be_nil
58
95
  end
59
96
 
97
+ it "should return the primary email if there is no raw_info and email access is allowed" do
98
+ emails = [
99
+ { 'email' => 'secondary@example.com', 'primary' => false },
100
+ { 'email' => 'primary@example.com', 'primary' => true }
101
+ ]
102
+ subject.stub!(:raw_info).and_return({})
103
+ subject.options['scope'] = 'user'
104
+ subject.stub!(:emails).and_return(emails)
105
+ subject.email.should eq('primary@example.com')
106
+ end
107
+
60
108
  it "should return the first email if there is no raw_info and email access is allowed" do
109
+ emails = [
110
+ { 'email' => 'first@example.com', 'primary' => false },
111
+ { 'email' => 'second@example.com', 'primary' => false }
112
+ ]
61
113
  subject.stub!(:raw_info).and_return({})
62
114
  subject.options['scope'] = 'user'
63
- subject.stub!(:emails).and_return([ 'you@example.com' ])
64
- subject.email.should eq('you@example.com')
115
+ subject.stub!(:emails).and_return(emails)
116
+ subject.email.should eq('first@example.com')
117
+ end
118
+ end
119
+
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)
65
132
  end
66
133
  end
67
134
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omniauth-github
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-28 00:00:00.000000000 Z
12
+ date: 2013-02-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: omniauth
@@ -146,7 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
146
  version: '0'
147
147
  requirements: []
148
148
  rubyforge_project:
149
- rubygems_version: 1.8.24
149
+ rubygems_version: 1.8.23
150
150
  signing_key:
151
151
  specification_version: 3
152
152
  summary: Official OmniAuth strategy for GitHub.