omniauth-google-oauth2 0.1.9 → 0.1.10

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -16,3 +16,4 @@ test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
18
  .powenv
19
+ .idea/
data/examples/config.ru CHANGED
@@ -33,8 +33,11 @@ end
33
33
  use Rack::Session::Cookie, :secret => ENV['RACK_COOKIE_SECRET']
34
34
 
35
35
  use OmniAuth::Builder do
36
- provider :google_oauth2, ENV['GOOGLE_KEY'], ENV['GOOGLE_SECRET'], {
37
- }
36
+ # Regular usage
37
+ provider :google_oauth2, ENV['GOOGLE_KEY'], ENV['GOOGLE_SECRET'], {}
38
+
39
+ # Custom scope supporting youtube
40
+ # provider :google_oauth2, ENV['GOOGLE_KEY'], ENV['GOOGLE_SECRET'], {:scope => 'http://gdata.youtube.com,userinfo.email,userinfo.profile,plus.me', :access_type => 'online', :approval_prompt => ''}
38
41
  end
39
42
 
40
43
  run App.new
@@ -0,0 +1,6 @@
1
+ Rails.application.config.middleware.use OmniAuth::Builder do
2
+ # If you don't need a refresh token -- if you're only using Google for account creation/auth and don't need google services -- set the access_type to 'online'.
3
+ # Also, set the approval prompt to an empty string, since otherwise it will be set to 'force', which makes users manually approve to the Oauth every time they log in.
4
+ # See http://googleappsdeveloper.blogspot.com/2011/10/upcoming-changes-to-oauth-20-endpoint.html
5
+ provider :google_oauth2, ENV['GOOGLE_KEY'], ENV['GOOGLE_SECRET'], {access_type: 'online', approval_prompt: ''}
6
+ end
@@ -1,5 +1,5 @@
1
1
  module OmniAuth
2
2
  module GoogleOauth2
3
- VERSION = "0.1.9"
3
+ VERSION = "0.1.10"
4
4
  end
5
5
  end
@@ -8,7 +8,7 @@ module OmniAuth
8
8
  DEFAULT_SCOPE = "userinfo.email,userinfo.profile"
9
9
 
10
10
  option :name, 'google_oauth2'
11
- option :authorize_options, [:scope, :approval_prompt, :access_type]
11
+ option :authorize_options, [:scope, :approval_prompt, :access_type, :state]
12
12
 
13
13
  option :client_options, {
14
14
  :site => 'https://accounts.google.com',
@@ -21,11 +21,15 @@ module OmniAuth
21
21
  super.tap do |params|
22
22
  scopes = (params[:scope] || DEFAULT_SCOPE).split(",")
23
23
  scopes.map! { |s| s =~ /^https?:\/\// ? s : "#{base_scope_url}#{s}" }
24
+ params[:state] = request.params['state'] if request.params['state']
24
25
  params[:scope] = scopes.join(' ')
25
26
  # This makes sure we get a refresh_token.
26
27
  # http://googlecode.blogspot.com/2011/10/upcoming-changes-to-oauth-20-endpoint.html
27
28
  params[:access_type] = 'offline' if params[:access_type].nil?
28
29
  params[:approval_prompt] = 'force' if params[:approval_prompt].nil?
30
+ # allow overriding approval_prompt on the request itself
31
+ params[:approval_prompt] = request.params['approval_prompt'] if request_has_approval_prompt
32
+
29
33
  end
30
34
  end
31
35
 
@@ -40,11 +44,11 @@ module OmniAuth
40
44
  :image => raw_info['picture']
41
45
  })
42
46
  end
43
-
47
+
44
48
  extra do
45
- prune!({
46
- 'raw_info' => raw_info
47
- })
49
+ hash = {}
50
+ hash[:raw_info] = raw_info unless skip_info?
51
+ prune! hash
48
52
  end
49
53
 
50
54
  def raw_info
@@ -53,6 +57,10 @@ module OmniAuth
53
57
 
54
58
  private
55
59
 
60
+ def request_has_approval_prompt
61
+ request.env && request.params && request.params['approval_prompt']
62
+ end
63
+
56
64
  def prune!(hash)
57
65
  hash.delete_if do |_, value|
58
66
  prune!(value) if value.is_a?(Hash)
@@ -2,8 +2,18 @@ require 'spec_helper'
2
2
  require 'omniauth-google-oauth2'
3
3
 
4
4
  describe OmniAuth::Strategies::GoogleOauth2 do
5
+
6
+ before :each do
7
+ @request = double('Request')
8
+ @request.stub(:params) { {} }
9
+ @request.stub(:cookies) { {} }
10
+ @request.stub(:env) { {} }
11
+ end
12
+
5
13
  subject do
6
- OmniAuth::Strategies::GoogleOauth2.new(nil, @options || {})
14
+ OmniAuth::Strategies::GoogleOauth2.new(nil, @options || {}).tap do |strategy|
15
+ strategy.stub(:request) { @request }
16
+ end
7
17
  end
8
18
 
9
19
  it_should_behave_like 'an oauth2 strategy'
@@ -28,7 +38,7 @@ describe OmniAuth::Strategies::GoogleOauth2 do
28
38
  end
29
39
  end
30
40
 
31
- describe '#authorize_params' do
41
+ describe '#authorize_params' do
32
42
  it 'should expand scope shortcuts' do
33
43
  @options = { :authorize_options => [:scope], :scope => 'userinfo.email'}
34
44
  subject.authorize_params['scope'].should eq('https://www.googleapis.com/auth/userinfo.email')
@@ -48,6 +58,33 @@ describe OmniAuth::Strategies::GoogleOauth2 do
48
58
  @options = { :authorize_options => [:scope]}
49
59
  subject.authorize_params['scope'].should eq('https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile')
50
60
  end
61
+
62
+ it 'should set the state parameter' do
63
+ @options = {:state => "some_state"}
64
+ subject.authorize_params['state'].should eq('some_state')
65
+ end
66
+
67
+ it 'should set the state parameter dynamically' do
68
+ subject.stub(:request) { double('Request', {:params => { 'state' => 'some_state' }, :env => {}}) }
69
+ subject.authorize_params['state'].should eq('some_state')
70
+ end
71
+
72
+ it 'should allow request parameter to override approval_prompt' do
73
+ @options = {:approval_prompt => ''} # non-nil prevent default 'force'
74
+ # stub the request
75
+ subject.stub!(:request).and_return( Rack::Request.new( {'QUERY_STRING' => "approval_prompt=force", "rack.input" => ""}))
76
+ subject.authorize_params['approval_prompt'].should eq('force')
77
+ end
78
+
79
+ it 'should include raw_info in extras hash by default' do
80
+ subject.stub(:raw_info) { { :foo => 'bar' } }
81
+ subject.extra[:raw_info].should eq({ :foo => 'bar' })
82
+ end
83
+
84
+ it 'should not include raw_info in extras hash when skip_info is specified' do
85
+ @options = { :skip_info => true }
86
+ subject.extra.should_not have_key(:raw_info)
87
+ end
51
88
  end
52
89
 
53
90
  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: 9
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 9
10
- version: 0.1.9
9
+ - 10
10
+ version: 0.1.10
11
11
  platform: ruby
12
12
  authors:
13
13
  - Josh Ellithorpe
@@ -16,8 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2012-01-06 00:00:00 -08:00
20
- default_executable:
19
+ date: 2012-06-17 00:00:00 Z
21
20
  dependencies:
22
21
  - !ruby/object:Gem::Dependency
23
22
  name: omniauth
@@ -94,6 +93,7 @@ files:
94
93
  - README.md
95
94
  - Rakefile
96
95
  - examples/config.ru
96
+ - examples/omni_auth.rb
97
97
  - lib/omniauth-google-oauth2.rb
98
98
  - lib/omniauth/google_oauth2.rb
99
99
  - lib/omniauth/google_oauth2/version.rb
@@ -102,7 +102,6 @@ files:
102
102
  - spec/omniauth/strategies/google_oauth2_spec.rb
103
103
  - spec/spec_helper.rb
104
104
  - spec/support/shared_examples.rb
105
- has_rdoc: true
106
105
  homepage: ""
107
106
  licenses: []
108
107
 
@@ -132,7 +131,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
132
131
  requirements: []
133
132
 
134
133
  rubyforge_project:
135
- rubygems_version: 1.6.2
134
+ rubygems_version: 1.8.10
136
135
  signing_key:
137
136
  specification_version: 3
138
137
  summary: A Google oauth2 strategy for OmniAuth 1.0