rack-oauth2 0.2.3 → 0.3.0.alpha
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile +1 -0
- data/README.rdoc +1 -0
- data/VERSION +1 -1
- data/lib/rack/oauth2.rb +1 -7
- data/lib/rack/oauth2/server.rb +0 -1
- data/lib/rack/oauth2/server/abstract.rb +2 -1
- data/lib/rack/oauth2/server/abstract/error.rb +55 -0
- data/lib/rack/oauth2/server/abstract/handler.rb +2 -3
- data/lib/rack/oauth2/server/abstract/request.rb +2 -3
- data/lib/rack/oauth2/server/abstract/response.rb +0 -5
- data/lib/rack/oauth2/server/authorize.rb +19 -14
- data/lib/rack/oauth2/server/authorize/code.rb +8 -19
- data/lib/rack/oauth2/server/authorize/error.rb +60 -0
- data/lib/rack/oauth2/server/authorize/token.rb +15 -24
- data/lib/rack/oauth2/server/resource.rb +1 -79
- data/lib/rack/oauth2/server/resource/bearer.rb +74 -0
- data/lib/rack/oauth2/server/resource/bearer/error.rb +80 -0
- data/lib/rack/oauth2/server/token.rb +12 -19
- data/lib/rack/oauth2/server/token/authorization_code.rb +4 -5
- data/lib/rack/oauth2/server/token/error.rb +54 -0
- data/lib/rack/oauth2/server/token/password.rb +0 -2
- data/lib/rack/oauth2/server/token/refresh_token.rb +1 -1
- data/lib/rack/oauth2/server/util.rb +29 -0
- data/rack-oauth2.gemspec +1 -1
- data/spec/rack/oauth2/server/abstract/error_spec.rb +51 -0
- data/spec/rack/oauth2/server/authorize/code_spec.rb +42 -28
- data/spec/rack/oauth2/server/authorize/error_spec.rb +103 -0
- data/spec/rack/oauth2/server/authorize/token_spec.rb +55 -26
- data/spec/rack/oauth2/server/authorize_spec.rb +24 -68
- data/spec/rack/oauth2/server/resource/bearer/error_spec.rb +118 -0
- data/spec/rack/oauth2/server/resource/bearer_spec.rb +117 -0
- data/spec/rack/oauth2/server/token/authorization_code_spec.rb +26 -109
- data/spec/rack/oauth2/server/token/error_spec.rb +77 -0
- data/spec/rack/oauth2/server/token/password_spec.rb +27 -47
- data/spec/rack/oauth2/server/token/refresh_token_spec.rb +22 -43
- data/spec/rack/oauth2/server/token_spec.rb +77 -116
- data/spec/rack/oauth2/server/util_spec.rb +75 -16
- data/spec/spec_helper.rb +0 -12
- metadata +25 -29
- data/lib/rack/oauth2/server/authorize/code_and_token.rb +0 -62
- data/lib/rack/oauth2/server/error.rb +0 -73
- data/lib/rack/oauth2/server/error/authorize.rb +0 -54
- data/lib/rack/oauth2/server/error/resource.rb +0 -50
- data/lib/rack/oauth2/server/error/token.rb +0 -59
- data/lib/rack/oauth2/server/token/assertion.rb +0 -29
- data/spec/rack/oauth2/server/authorize/code_and_token_spec.rb +0 -53
- data/spec/rack/oauth2/server/error/authorize_spec.rb +0 -102
- data/spec/rack/oauth2/server/error/resource_spec.rb +0 -69
- data/spec/rack/oauth2/server/error/token_spec.rb +0 -115
- data/spec/rack/oauth2/server/error_spec.rb +0 -107
- data/spec/rack/oauth2/server/resource_spec.rb +0 -141
- data/spec/rack/oauth2/server/token/assertion_spec.rb +0 -56
@@ -1,52 +1,66 @@
|
|
1
1
|
require 'spec_helper.rb'
|
2
2
|
|
3
3
|
describe Rack::OAuth2::Server::Authorize::Code do
|
4
|
+
let(:request) { Rack::MockRequest.new app }
|
5
|
+
let(:redirect_uri) { 'http://client.example.com/callback' }
|
6
|
+
let(:authorization_code) { 'authorization_code' }
|
7
|
+
let(:response) { request.get "/?response_type=code&client_id=client&redirect_uri=#{redirect_uri}" }
|
4
8
|
|
5
|
-
context
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
+
context 'when approved' do
|
10
|
+
subject { response }
|
11
|
+
let :app do
|
12
|
+
Rack::OAuth2::Server::Authorize.new do |request, response|
|
13
|
+
response.redirect_uri = redirect_uri
|
14
|
+
response.code = authorization_code
|
9
15
|
response.approve!
|
10
|
-
response.code = "authorization_code"
|
11
16
|
end
|
12
|
-
@request = Rack::MockRequest.new @app
|
13
17
|
end
|
18
|
+
its(:status) { should == 302 }
|
19
|
+
its(:location) { should == "#{redirect_uri}?code=#{authorization_code}" }
|
14
20
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
response.location.should == "http://client.example.com/callback?code=authorization_code"
|
21
|
+
context 'when redirect_uri already includes query' do
|
22
|
+
let(:redirect_uri) { 'http://client.example.com/callback?k=v' }
|
23
|
+
its(:location) { should == "#{redirect_uri}&code=#{authorization_code}" }
|
19
24
|
end
|
20
25
|
|
21
|
-
context
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
+
context 'when redirect_uri is missing' do
|
27
|
+
let :app do
|
28
|
+
Rack::OAuth2::Server::Authorize.new do |request, response|
|
29
|
+
response.code = authorization_code
|
30
|
+
response.approve!
|
31
|
+
end
|
32
|
+
end
|
33
|
+
it do
|
34
|
+
expect { response }.should raise_error AttrRequired::AttrMissing
|
26
35
|
end
|
27
36
|
end
|
28
37
|
|
38
|
+
context 'when code is missing' do
|
39
|
+
let :app do
|
40
|
+
Rack::OAuth2::Server::Authorize.new do |request, response|
|
41
|
+
response.redirect_uri = redirect_uri
|
42
|
+
response.approve!
|
43
|
+
end
|
44
|
+
end
|
45
|
+
it do
|
46
|
+
expect { response }.should raise_error AttrRequired::AttrMissing
|
47
|
+
end
|
48
|
+
end
|
29
49
|
end
|
30
50
|
|
31
|
-
context
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
request.access_denied! 'User rejected the requested access.'
|
51
|
+
context 'when denied' do
|
52
|
+
let :app do
|
53
|
+
Rack::OAuth2::Server::Authorize.new do |request, response|
|
54
|
+
request.access_denied!
|
36
55
|
end
|
37
|
-
@request = Rack::MockRequest.new @app
|
38
56
|
end
|
39
|
-
|
40
|
-
it "should redirect to redirect_uri with error message" do
|
41
|
-
response = @request.get("/?response_type=code&client_id=client&redirect_uri=http://client.example.com/callback")
|
57
|
+
it 'should redirect with error in query' do
|
42
58
|
response.status.should == 302
|
43
59
|
error_message = {
|
44
60
|
:error => :access_denied,
|
45
|
-
:error_description =>
|
61
|
+
:error_description => Rack::OAuth2::Server::Authorize::ErrorMethods::DEFAULT_DESCRIPTION[:access_denied]
|
46
62
|
}
|
47
|
-
response.location.should == "
|
63
|
+
response.location.should == "#{redirect_uri}?#{error_message.to_query}"
|
48
64
|
end
|
49
|
-
|
50
65
|
end
|
51
|
-
|
52
66
|
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'spec_helper.rb'
|
2
|
+
|
3
|
+
describe Rack::OAuth2::Server::Authorize::BadRequest do
|
4
|
+
let(:klass) { Rack::OAuth2::Server::Authorize::BadRequest }
|
5
|
+
let(:error) { klass.new(:invalid_request) }
|
6
|
+
let(:redirect_uri) { 'http://client.example.com/callback' }
|
7
|
+
|
8
|
+
subject { error }
|
9
|
+
it { should be_a Rack::OAuth2::Server::Abstract::BadRequest }
|
10
|
+
its(:protocol_params) do
|
11
|
+
should == {
|
12
|
+
:error => :invalid_request,
|
13
|
+
:error_description => nil,
|
14
|
+
:error_uri => nil,
|
15
|
+
:state => nil
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
19
|
+
describe '#finish' do
|
20
|
+
context 'when redirect_uri is given' do
|
21
|
+
before { error.redirect_uri = redirect_uri }
|
22
|
+
|
23
|
+
context 'when protocol_params_location = :query' do
|
24
|
+
before { error.protocol_params_location = :query }
|
25
|
+
it 'should redirect with error in query' do
|
26
|
+
state, header, response = error.finish
|
27
|
+
state.should == 302
|
28
|
+
header["Location"].should == "#{redirect_uri}?error=invalid_request"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'when protocol_params_location = :fragment' do
|
33
|
+
before { error.protocol_params_location = :fragment }
|
34
|
+
it 'should redirect with error in fragment' do
|
35
|
+
state, header, response = error.finish
|
36
|
+
state.should == 302
|
37
|
+
header["Location"].should == "#{redirect_uri}#error=invalid_request"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'otherwise' do
|
42
|
+
before { error.protocol_params_location = :other }
|
43
|
+
it 'should redirect without error' do
|
44
|
+
state, header, response = error.finish
|
45
|
+
state.should == 302
|
46
|
+
header["Location"].should == redirect_uri
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context 'otherwise' do
|
52
|
+
it 'should raise itself' do
|
53
|
+
expect { error.finish }.should raise_error(klass) { |e|
|
54
|
+
e.should == error
|
55
|
+
}
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe Rack::OAuth2::Server::Authorize::ErrorMethods do
|
62
|
+
let(:klass) { Rack::OAuth2::Server::Authorize::BadRequest }
|
63
|
+
let(:redirect_uri) { 'http://client.example.com/callback' }
|
64
|
+
let(:default_description) { Rack::OAuth2::Server::Authorize::ErrorMethods::DEFAULT_DESCRIPTION }
|
65
|
+
let(:env) { Rack::MockRequest.env_for("/authorize?client_id=client_id") }
|
66
|
+
let(:request) { Rack::OAuth2::Server::Authorize::Request.new env }
|
67
|
+
let(:request_for_code) { Rack::OAuth2::Server::Authorize::Code::Request.new env }
|
68
|
+
let(:request_for_token) { Rack::OAuth2::Server::Authorize::Token::Request.new env }
|
69
|
+
|
70
|
+
describe 'bad_request!' do
|
71
|
+
it do
|
72
|
+
expect { request.bad_request! }.should raise_error klass
|
73
|
+
end
|
74
|
+
|
75
|
+
context 'when response_type = :code' do
|
76
|
+
it 'should set protocol_params_location = :query' do
|
77
|
+
expect { request_for_code.bad_request! }.should raise_error(klass) { |e|
|
78
|
+
e.protocol_params_location.should == :query
|
79
|
+
}
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
context 'when response_type = :token' do
|
84
|
+
it 'should set protocol_params_location = :fragment' do
|
85
|
+
expect { request_for_token.bad_request! }.should raise_error(klass) { |e|
|
86
|
+
e.protocol_params_location.should == :fragment
|
87
|
+
}
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
Rack::OAuth2::Server::Authorize::ErrorMethods::DEFAULT_DESCRIPTION.keys.each do |error_code|
|
93
|
+
method = "#{error_code}!"
|
94
|
+
describe method do
|
95
|
+
it "should raise Rack::OAuth2::Server::Authorize::BadRequest with error = :#{error_code}" do
|
96
|
+
expect { request.send method }.should raise_error(klass) { |error|
|
97
|
+
error.error.should == error_code
|
98
|
+
error.description.should == default_description[error_code]
|
99
|
+
}
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -1,52 +1,81 @@
|
|
1
1
|
require 'spec_helper.rb'
|
2
2
|
|
3
3
|
describe Rack::OAuth2::Server::Authorize::Token do
|
4
|
+
let(:request) { Rack::MockRequest.new app }
|
5
|
+
let(:redirect_uri) { 'http://client.example.com/callback' }
|
6
|
+
let(:access_token) { 'access_token' }
|
7
|
+
let(:token_type) { 'bearer' }
|
8
|
+
let(:response) { request.get("/?response_type=token&client_id=client&redirect_uri=#{redirect_uri}") }
|
4
9
|
|
5
|
-
context "when
|
6
|
-
|
7
|
-
|
8
|
-
|
10
|
+
context "when approved" do
|
11
|
+
let :app do
|
12
|
+
Rack::OAuth2::Server::Authorize.new do |request, response|
|
13
|
+
response.redirect_uri = redirect_uri
|
14
|
+
response.access_token = access_token
|
15
|
+
response.token_type = token_type
|
9
16
|
response.approve!
|
10
|
-
response.access_token = "access_token"
|
11
17
|
end
|
12
|
-
@request = Rack::MockRequest.new @app
|
13
18
|
end
|
14
19
|
|
15
|
-
it
|
16
|
-
response = @request.get("/?response_type=token&client_id=client&redirect_uri=http://client.example.com/callback")
|
20
|
+
it 'should redirect with authorization code in fragment' do
|
17
21
|
response.status.should == 302
|
18
|
-
response.location.should == "
|
22
|
+
response.location.should == "#{redirect_uri}#access_token=#{access_token}"
|
19
23
|
end
|
20
24
|
|
21
|
-
context
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
25
|
+
context 'when redirect_uri is missing' do
|
26
|
+
let :app do
|
27
|
+
Rack::OAuth2::Server::Authorize.new do |request, response|
|
28
|
+
response.access_token = access_token
|
29
|
+
response.token_type = token_type
|
30
|
+
response.approve!
|
31
|
+
end
|
32
|
+
end
|
33
|
+
it do
|
34
|
+
expect { response }.should raise_error AttrRequired::AttrMissing
|
26
35
|
end
|
27
36
|
end
|
28
37
|
|
29
|
-
|
38
|
+
context 'when access_token is missing' do
|
39
|
+
let :app do
|
40
|
+
Rack::OAuth2::Server::Authorize.new do |request, response|
|
41
|
+
response.redirect_uri = redirect_uri
|
42
|
+
response.token_type = token_type
|
43
|
+
response.approve!
|
44
|
+
end
|
45
|
+
end
|
46
|
+
it do
|
47
|
+
expect { response }.should raise_error AttrRequired::AttrMissing
|
48
|
+
end
|
49
|
+
end
|
30
50
|
|
31
|
-
|
51
|
+
context 'when token_type is missing' do
|
52
|
+
let :app do
|
53
|
+
Rack::OAuth2::Server::Authorize.new do |request, response|
|
54
|
+
response.redirect_uri = redirect_uri
|
55
|
+
response.access_token = access_token
|
56
|
+
response.approve!
|
57
|
+
end
|
58
|
+
end
|
32
59
|
|
33
|
-
|
34
|
-
|
35
|
-
request.access_denied! 'User rejected the requested access.'
|
60
|
+
it do
|
61
|
+
expect { response }.should raise_error AttrRequired::AttrMissing
|
36
62
|
end
|
37
|
-
@request = Rack::MockRequest.new @app
|
38
63
|
end
|
64
|
+
end
|
39
65
|
|
40
|
-
|
41
|
-
|
66
|
+
context 'when denied' do
|
67
|
+
let :app do
|
68
|
+
Rack::OAuth2::Server::Authorize.new do |request, response|
|
69
|
+
request.access_denied!
|
70
|
+
end
|
71
|
+
end
|
72
|
+
it 'should redirect with error in fragment' do
|
42
73
|
response.status.should == 302
|
43
74
|
error_message = {
|
44
75
|
:error => :access_denied,
|
45
|
-
:error_description =>
|
76
|
+
:error_description => Rack::OAuth2::Server::Authorize::ErrorMethods::DEFAULT_DESCRIPTION[:access_denied]
|
46
77
|
}
|
47
|
-
response.location.should == "
|
78
|
+
response.location.should == "#{redirect_uri}##{error_message.to_query}"
|
48
79
|
end
|
49
|
-
|
50
80
|
end
|
51
|
-
|
52
81
|
end
|
@@ -1,88 +1,44 @@
|
|
1
1
|
require 'spec_helper.rb'
|
2
2
|
|
3
3
|
describe Rack::OAuth2::Server::Authorize do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
end
|
9
|
-
|
10
|
-
describe Rack::OAuth2::Server::Authorize::Request do
|
4
|
+
let(:app) { Rack::OAuth2::Server::Authorize.new }
|
5
|
+
let(:request) { Rack::MockRequest.new app }
|
6
|
+
let(:redirect_uri) { 'http://client.example.com/callback' }
|
7
|
+
let(:bad_request) { Rack::OAuth2::Server::Authorize::BadRequest }
|
11
8
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
response.redirect_uri ||= "http://client.example.com/callback/pre-registered"
|
9
|
+
context 'when redirect_uri is missing' do
|
10
|
+
it do
|
11
|
+
expect { request.get '/' }.should raise_error bad_request
|
16
12
|
end
|
17
|
-
@request = Rack::MockRequest.new @app
|
18
13
|
end
|
19
14
|
|
20
|
-
context
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
end
|
25
|
-
assert_error_response(:json, :invalid_request) do
|
26
|
-
@request.get('/?response_type=code')
|
27
|
-
end
|
28
|
-
assert_error_response(:json, :invalid_request) do
|
29
|
-
@request.get('/?client_id=client')
|
15
|
+
context 'when redirect_uri is given' do
|
16
|
+
context 'when client_id is missing' do
|
17
|
+
it do
|
18
|
+
expect { request.get "/?redirect_uri=#{redirect_uri}" }.should raise_error bad_request
|
30
19
|
end
|
31
20
|
end
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
@request.get('/?response_type=hello&client_id=client&redirect_uri=http://client.example.com/callback')
|
21
|
+
context 'when client_id is given' do
|
22
|
+
context 'when response_type is missing' do
|
23
|
+
it do
|
24
|
+
expect { request.get "/?client_id=client&redirect_uri=#{redirect_uri}" }.should raise_error bad_request
|
25
|
+
end
|
38
26
|
end
|
39
27
|
end
|
40
28
|
end
|
41
29
|
|
42
|
-
context
|
43
|
-
it
|
44
|
-
|
45
|
-
response.status.should == 200
|
30
|
+
context 'when unknown response_type is given' do
|
31
|
+
it do
|
32
|
+
expect { request.get "/?response_type=unknown&client_id=client&redirect_uri=#{redirect_uri}" }.should raise_error bad_request
|
46
33
|
end
|
47
34
|
end
|
48
35
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
before do
|
56
|
-
@app = Rack::OAuth2::Server::Authorize.new(simple_app) do |request, response|
|
57
|
-
response.approve!
|
58
|
-
# code is missing
|
36
|
+
context 'when all required parameters are valid' do
|
37
|
+
[:code, :token].each do |request_type|
|
38
|
+
context "when response_type = :#{request_type}" do
|
39
|
+
subject { request.get "/?response_type=#{request_type}&client_id=client&redirect_uri=#{redirect_uri}" }
|
40
|
+
its(:status) { should == 200 }
|
59
41
|
end
|
60
|
-
@request = Rack::MockRequest.new @app
|
61
|
-
end
|
62
|
-
|
63
|
-
it "should raise an error" do
|
64
|
-
lambda do
|
65
|
-
@request.get("/?response_type=code&client_id=client&redirect_uri=http://client.example.com/callback")
|
66
|
-
end.should raise_error(StandardError)
|
67
42
|
end
|
68
|
-
|
69
|
-
end
|
70
|
-
|
71
|
-
context "when required response params are given" do
|
72
|
-
|
73
|
-
before do
|
74
|
-
@app = Rack::OAuth2::Server::Authorize.new(simple_app) do |request, response|
|
75
|
-
response.approve!
|
76
|
-
response.code = "authorization_code"
|
77
|
-
end
|
78
|
-
@request = Rack::MockRequest.new @app
|
79
|
-
end
|
80
|
-
|
81
|
-
it "should succeed" do
|
82
|
-
response = @request.get("/?response_type=code&client_id=client&redirect_uri=http://client.example.com/callback")
|
83
|
-
response.status.should == 302
|
84
|
-
end
|
85
|
-
|
86
43
|
end
|
87
|
-
|
88
44
|
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
require 'spec_helper.rb'
|
2
|
+
|
3
|
+
describe Rack::OAuth2::Server::Resource::Bearer::BadRequest do
|
4
|
+
let(:error) { Rack::OAuth2::Server::Resource::Bearer::BadRequest.new(:invalid_request) }
|
5
|
+
|
6
|
+
it { should be_a Rack::OAuth2::Server::Abstract::BadRequest }
|
7
|
+
describe '#finish' do
|
8
|
+
it 'should respond in JSON' do
|
9
|
+
status, header, response = error.finish
|
10
|
+
status.should == 400
|
11
|
+
header['Content-Type'].should == 'application/json'
|
12
|
+
response.body.should == ['{"error":"invalid_request"}']
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe Rack::OAuth2::Server::Resource::Bearer::Unauthorized do
|
18
|
+
let(:error) { Rack::OAuth2::Server::Resource::Bearer::Unauthorized.new(:invalid_token) }
|
19
|
+
|
20
|
+
it { should be_a Rack::OAuth2::Server::Abstract::Unauthorized }
|
21
|
+
describe '#finish' do
|
22
|
+
it 'should respond in JSON' do
|
23
|
+
status, header, response = error.finish
|
24
|
+
status.should == 401
|
25
|
+
header['Content-Type'].should == 'application/json'
|
26
|
+
header['WWW-Authenticate'].should == 'Bearer error="invalid_token"'
|
27
|
+
response.body.should == ['{"error":"invalid_token"}']
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'when error_code is not invalid_token' do
|
32
|
+
let(:error) { Rack::OAuth2::Server::Resource::Bearer::Unauthorized.new(:something) }
|
33
|
+
|
34
|
+
it 'should have error_code in body but not in WWW-Authenticate header' do
|
35
|
+
status, header, response = error.finish
|
36
|
+
header['WWW-Authenticate'].should == 'Bearer'
|
37
|
+
response.body.first.should include '"error":"something"'
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe Rack::OAuth2::Server::Resource::Bearer::Forbidden do
|
43
|
+
let(:error) { Rack::OAuth2::Server::Resource::Bearer::Forbidden.new(:insufficient_scope) }
|
44
|
+
|
45
|
+
it { should be_a Rack::OAuth2::Server::Abstract::Forbidden }
|
46
|
+
describe '#finish' do
|
47
|
+
it 'should respond in JSON' do
|
48
|
+
status, header, response = error.finish
|
49
|
+
status.should == 403
|
50
|
+
header['Content-Type'].should == 'application/json'
|
51
|
+
response.body.should == ['{"error":"insufficient_scope"}']
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context 'when scope option is given' do
|
56
|
+
let(:error) { Rack::OAuth2::Server::Resource::Bearer::Forbidden.new(:insufficient_scope, 'Desc', :scope => [:scope1, :scope2]) }
|
57
|
+
|
58
|
+
it 'should have blank WWW-Authenticate header' do
|
59
|
+
status, header, response = error.finish
|
60
|
+
response.body.first.should include '"scope":"scope1 scope2"'
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe Rack::OAuth2::Server::Resource::Bearer::ErrorMethods do
|
66
|
+
let(:bad_request) { Rack::OAuth2::Server::Resource::Bearer::BadRequest }
|
67
|
+
let(:unauthorized) { Rack::OAuth2::Server::Resource::Bearer::Unauthorized }
|
68
|
+
let(:forbidden) { Rack::OAuth2::Server::Resource::Bearer::Forbidden }
|
69
|
+
let(:redirect_uri) { 'http://client.example.com/callback' }
|
70
|
+
let(:default_description) { Rack::OAuth2::Server::Resource::Bearer::ErrorMethods::DEFAULT_DESCRIPTION }
|
71
|
+
let(:env) { Rack::MockRequest.env_for("/authorize?client_id=client_id") }
|
72
|
+
let(:request) { Rack::OAuth2::Server::Resource::Bearer::Request.new env }
|
73
|
+
|
74
|
+
describe 'bad_request!' do
|
75
|
+
it do
|
76
|
+
expect { request.bad_request! :invalid_request }.should raise_error bad_request
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe 'unauthorized!' do
|
81
|
+
it do
|
82
|
+
expect { request.unauthorized! :invalid_client }.should raise_error unauthorized
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
Rack::OAuth2::Server::Resource::Bearer::ErrorMethods::DEFAULT_DESCRIPTION.keys.each do |error_code|
|
87
|
+
method = "#{error_code}!"
|
88
|
+
case error_code
|
89
|
+
when :invalid_request
|
90
|
+
describe method do
|
91
|
+
it "should raise Rack::OAuth2::Server::Resource::Bearer::BadRequest with error = :#{error_code}" do
|
92
|
+
expect { request.send method }.should raise_error(bad_request) { |error|
|
93
|
+
error.error.should == error_code
|
94
|
+
error.description.should == default_description[error_code]
|
95
|
+
}
|
96
|
+
end
|
97
|
+
end
|
98
|
+
when :insufficient_scope
|
99
|
+
describe method do
|
100
|
+
it "should raise Rack::OAuth2::Server::Resource::Bearer::Forbidden with error = :#{error_code}" do
|
101
|
+
expect { request.send method }.should raise_error(forbidden) { |error|
|
102
|
+
error.error.should == error_code
|
103
|
+
error.description.should == default_description[error_code]
|
104
|
+
}
|
105
|
+
end
|
106
|
+
end
|
107
|
+
else
|
108
|
+
describe method do
|
109
|
+
it "should raise Rack::OAuth2::Server::Resource::Bearer::Unauthorized with error = :#{error_code}" do
|
110
|
+
expect { request.send method }.should raise_error(unauthorized) { |error|
|
111
|
+
error.error.should == error_code
|
112
|
+
error.description.should == default_description[error_code]
|
113
|
+
}
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|