omniauth-oauth2 1.0.3 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of omniauth-oauth2 might be problematic. Click here for more details.

@@ -3,6 +3,7 @@ require 'uri'
3
3
  require 'oauth2'
4
4
  require 'omniauth'
5
5
  require 'timeout'
6
+ require 'securerandom'
6
7
 
7
8
  module OmniAuth
8
9
  module Strategies
@@ -47,7 +48,16 @@ module OmniAuth
47
48
  end
48
49
 
49
50
  def authorize_params
50
- options.authorize_params.merge(options.authorize_options.inject({}){|h,k| h[k.to_sym] = options[k] if options[k]; h})
51
+ if options.authorize_params[:state].to_s.empty?
52
+ options.authorize_params[:state] = SecureRandom.hex(24)
53
+ end
54
+ params = options.authorize_params.merge(options.authorize_options.inject({}){|h,k| h[k.to_sym] = options[k] if options[k]; h})
55
+ if OmniAuth.config.test_mode
56
+ @env ||= {}
57
+ @env['rack.session'] ||= {}
58
+ end
59
+ session['omniauth.state'] = params[:state]
60
+ params
51
61
  end
52
62
 
53
63
  def token_params
@@ -58,6 +68,9 @@ module OmniAuth
58
68
  if request.params['error'] || request.params['error_reason']
59
69
  raise CallbackError.new(request.params['error'], request.params['error_description'] || request.params['error_reason'], request.params['error_uri'])
60
70
  end
71
+ if request.params['state'].to_s.empty? || request.params['state'] != session.delete('omniauth.state')
72
+ raise CallbackError.new(nil, :csrf_detected)
73
+ end
61
74
 
62
75
  self.access_token = build_access_token
63
76
  self.access_token = access_token.refresh! if access_token.expired?
@@ -1,5 +1,5 @@
1
1
  module OmniAuth
2
2
  module OAuth2
3
- VERSION = "1.0.3"
3
+ VERSION = "1.1.0"
4
4
  end
5
5
  end
@@ -4,6 +4,14 @@ describe OmniAuth::Strategies::OAuth2 do
4
4
  def app; lambda{|env| [200, {}, ["Hello."]]} end
5
5
  let(:fresh_strategy){ Class.new(OmniAuth::Strategies::OAuth2) }
6
6
 
7
+ before do
8
+ OmniAuth.config.test_mode = true
9
+ end
10
+
11
+ after do
12
+ OmniAuth.config.test_mode = false
13
+ end
14
+
7
15
  describe '#client' do
8
16
  subject{ fresh_strategy }
9
17
 
@@ -22,13 +30,20 @@ describe OmniAuth::Strategies::OAuth2 do
22
30
  subject { fresh_strategy }
23
31
 
24
32
  it 'should include any authorize params passed in the :authorize_params option' do
25
- instance = subject.new('abc', 'def', :authorize_params => {:foo => 'bar', :baz => 'zip'})
26
- instance.authorize_params.should == {'foo' => 'bar', 'baz' => 'zip'}
33
+ instance = subject.new('abc', 'def', :authorize_params => {:foo => 'bar', :baz => 'zip', :state => '123'})
34
+ instance.authorize_params.should == {'foo' => 'bar', 'baz' => 'zip', 'state' => '123'}
27
35
  end
28
36
 
29
37
  it 'should include top-level options that are marked as :authorize_options' do
30
- instance = subject.new('abc', 'def', :authorize_options => [:scope, :foo], :scope => 'bar', :foo => 'baz')
31
- instance.authorize_params.should == {'scope' => 'bar', 'foo' => 'baz'}
38
+ instance = subject.new('abc', 'def', :authorize_options => [:scope, :foo], :scope => 'bar', :foo => 'baz', :authorize_params => {:state => '123'})
39
+ instance.authorize_params.should == {'scope' => 'bar', 'foo' => 'baz', 'state' => '123'}
40
+ end
41
+
42
+ it 'should include random state in the authorize params' do
43
+ instance = subject.new('abc', 'def')
44
+ instance.authorize_params.keys.should == ['state']
45
+ instance.session['omniauth.state'].should_not be_empty
46
+ instance.session['omniauth.state'].should == instance.authorize_params['state']
32
47
  end
33
48
  end
34
49
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omniauth-oauth2
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-07-05 00:00:00.000000000 Z
12
+ date: 2012-07-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: omniauth