omniauth-google-oauth2 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -34,7 +34,6 @@ use Rack::Session::Cookie, :secret => ENV['RACK_COOKIE_SECRET']
34
34
 
35
35
  use OmniAuth::Builder do
36
36
  provider :google_oauth2, ENV['GOOGLE_KEY'], ENV['GOOGLE_SECRET'], {
37
- :scope => 'https://www.googleapis.com/auth/plus.me'
38
37
  }
39
38
  end
40
39
 
@@ -1,2 +1 @@
1
- require 'omniauth/google_oauth2/version'
2
1
  require 'omniauth/strategies/google_oauth2'
@@ -1,5 +1,5 @@
1
1
  module OmniAuth
2
2
  module GoogleOauth2
3
- VERSION = "0.1.6"
3
+ VERSION = "0.1.7"
4
4
  end
5
5
  end
@@ -3,58 +3,62 @@ require 'omniauth/strategies/oauth2'
3
3
  module OmniAuth
4
4
  module Strategies
5
5
  class GoogleOauth2 < OmniAuth::Strategies::OAuth2
6
+
7
+ # Possible scopes: userinfo.email,userinfo.profile,plus.me
8
+ DEFAULT_SCOPE = "userinfo.email,userinfo.profile"
9
+
6
10
  option :name, 'google_oauth2'
7
11
 
8
12
  option :client_options, {
9
- :site => 'https://accounts.google.com',
13
+ :site => 'https://accounts.google.com',
10
14
  :authorize_url => '/o/oauth2/auth',
11
- :token_url => '/o/oauth2/token'
15
+ :token_url => '/o/oauth2/token'
12
16
  }
13
17
 
14
- def request_phase
15
- setup_authorize_params
16
- super
18
+ def authorize_params
19
+ base_scope_url = "https://www.googleapis.com/auth/"
20
+ super.tap do |params|
21
+ scopes = (params[:scope] || DEFAULT_SCOPE).split(",")
22
+ scopes.map! { |s| s =~ /^https?:\/\// ? s : "#{base_scope_url}#{s}" }
23
+ params[:scope] = scopes.join(' ')
24
+ end
17
25
  end
18
26
 
19
- def setup_authorize_params
20
- opts = {
21
- :client_id => options[:client_id],
22
- :redirect_uri => options[:redirect_uri] || callback_url,
23
- :response_type => "code",
24
- :scope => options[:scope]
25
- }
26
- google_email_scope = "www.googleapis.com/auth/userinfo.email"
27
- opts[:scope] ||= "https://#{google_email_scope}"
28
- opts[:scope] << " https://#{google_email_scope}" unless opts[:scope] =~ %r[http[s]?:\/\/#{google_email_scope}]
29
- options[:authorize_params] = opts.merge(options[:authorize_params])
27
+ uid{ raw_info['id'] || verified_email }
28
+
29
+ info do
30
+ prune!({
31
+ :name => raw_info['name'],
32
+ :email => verified_email,
33
+ :first_name => raw_info['given_name'],
34
+ :last_name => raw_info['family_name'],
35
+ :image => raw_info['picture']
36
+ })
30
37
  end
31
38
 
32
- def auth_hash
33
- OmniAuth::Utils.deep_merge(super, {
34
- 'uid' => info['uid'],
35
- 'info' => info,
36
- 'credentials' => {'expires_at' => access_token.expires_at},
37
- 'extra' => {'user_hash' => user_data}
39
+ extra do
40
+ prune!({
41
+ 'raw_info' => raw_info
38
42
  })
39
43
  end
40
44
 
41
- info do
42
- if user_data['data']['isVerified']
43
- email = user_data['data']['email'] rescue nil
44
- else
45
- email = nil
46
- end
45
+ def raw_info
46
+ @raw_info ||= access_token.get('https://www.googleapis.com/oauth2/v1/userinfo').parsed
47
+ end
47
48
 
48
- {
49
- 'email' => email,
50
- 'uid' => email,
51
- 'name' => email
52
- }
49
+ private
50
+
51
+ def prune!(hash)
52
+ hash.delete_if do |_, value|
53
+ prune!(value) if value.is_a?(Hash)
54
+ value.nil? || (value.respond_to?(:empty?) && value.empty?)
55
+ end
53
56
  end
54
57
 
55
- def user_data
56
- @data ||= access_token.get("https://www.googleapis.com/userinfo/email?alt=json").parsed
58
+ def verified_email
59
+ raw_info['verified_email'] ? raw_info['email'] : nil
57
60
  end
61
+
58
62
  end
59
63
  end
60
64
  end
@@ -22,34 +22,32 @@ describe OmniAuth::Strategies::GoogleOauth2 do
22
22
  end
23
23
  end
24
24
 
25
- describe 'redirect_uri' do
26
- before do
27
- subject.stub(:callback_url).and_return('http://example.host/default')
28
- end
29
-
30
- it 'should be callback_url by default' do
31
- subject.request_phase
32
- subject.options[:authorize_params][:redirect_uri].should eql('http://example.host/default')
33
- end
34
-
35
- it 'should be overriden by an option' do
36
- subject.options[:redirect_uri] = 'http://example.host/override'
37
- subject.request_phase
38
- subject.options[:authorize_params][:redirect_uri].should eql('http://example.host/override')
39
- end
40
- end
41
-
42
25
  describe '#callback_path' do
43
26
  it "has the correct callback path" do
44
27
  subject.callback_path.should eq('/auth/google_oauth2/callback')
45
28
  end
46
29
  end
47
30
 
48
- # These are setup during the request_phase
49
- # At init they are blank
50
31
  describe '#authorize_params' do
51
- it "has no authorize params at init" do
52
- subject.authorize_params.should be_empty
32
+ it 'should expand scope shortcuts' do
33
+ @options = { :authorize_options => [:scope], :scope => 'userinfo.email'}
34
+ subject.authorize_params['scope'].should eq('https://www.googleapis.com/auth/userinfo.email')
35
+ end
36
+
37
+ it 'should leave full scopes as is' do
38
+ @options = { :authorize_options => [:scope], :scope => 'https://www.googleapis.com/auth/userinfo.profile'}
39
+ subject.authorize_params['scope'].should eq('https://www.googleapis.com/auth/userinfo.profile')
40
+ end
41
+
42
+ it 'should join scopes' do
43
+ @options = { :authorize_options => [:scope], :scope => 'userinfo.profile,userinfo.email'}
44
+ subject.authorize_params['scope'].should eq('https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email')
45
+ end
46
+
47
+ it 'should set default scope to userinfo.email,userinfo.profile' do
48
+ @options = { :authorize_options => [:scope]}
49
+ subject.authorize_params['scope'].should eq('https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile')
53
50
  end
54
51
  end
52
+
55
53
  end
@@ -15,8 +15,8 @@ shared_examples 'an oauth2 strategy' do
15
15
  end
16
16
 
17
17
  it 'should include top-level options that are marked as :authorize_options' do
18
- @options = { :authorize_options => [:scope, :foo], :scope => 'bar', :foo => 'baz' }
19
- subject.authorize_params['scope'].should eq('bar')
18
+ @options = { :authorize_options => [:scope, :foo], :scope => 'http://bar', :foo => 'baz' }
19
+ subject.authorize_params['scope'].should eq('http://bar')
20
20
  subject.authorize_params['foo'].should eq('baz')
21
21
  end
22
22
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omniauth-google-oauth2
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 6
10
- version: 0.1.6
9
+ - 7
10
+ version: 0.1.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Josh Ellithorpe
@@ -15,8 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-11-22 00:00:00 -08:00
19
- default_executable:
18
+ date: 2011-11-24 00:00:00 Z
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
21
  name: omniauth
@@ -101,7 +100,6 @@ files:
101
100
  - spec/omniauth/strategies/google_oauth2_spec.rb
102
101
  - spec/spec_helper.rb
103
102
  - spec/support/shared_examples.rb
104
- has_rdoc: true
105
103
  homepage: ""
106
104
  licenses: []
107
105
 
@@ -131,7 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
131
129
  requirements: []
132
130
 
133
131
  rubyforge_project:
134
- rubygems_version: 1.6.2
132
+ rubygems_version: 1.8.10
135
133
  signing_key:
136
134
  specification_version: 3
137
135
  summary: A Google oauth2 strategy for OmniAuth 1.0