rack-oauth2 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -26,25 +26,47 @@ describe Rack::OAuth2::Server::Error, '#finish' do
26
26
  header['Content-Type'].should == "text/html"
27
27
  header['Location'].should == "#{@params.delete(:redirect_uri)}?#{@params.to_query}"
28
28
  end
29
+
30
+ context "when redirect_uri already includes query" do
31
+ before do
32
+ @params = {
33
+ :error => :invalid_request,
34
+ :error_description => "Something invalid!!",
35
+ :redirect_uri => "http://client.example.com?k=v"
36
+ }
37
+ @error = Rack::OAuth2::Server::Error.new(400, @params[:error], @params[:error_description], :redirect_uri => @params[:redirect_uri])
38
+ end
39
+
40
+ it "should keep original query" do
41
+ status, header, response = @error.finish
42
+ status.should == 302
43
+ header['Content-Type'].should == "text/html"
44
+ header['Location'].should == "#{@params.delete(:redirect_uri)}&#{@params.to_query}"
45
+ end
46
+ end
29
47
  end
30
48
 
31
- context "when www_authenticate isn given" do
49
+ context "when realm is given" do
32
50
  before do
33
51
  @params = {
34
52
  :error => :invalid_request,
35
53
  :error_description => "Something invalid!!"
36
54
  }
37
- @error = Rack::OAuth2::Server::Error.new(401, @params[:error], @params[:error_description], :www_authenticate => true)
55
+ @error = Rack::OAuth2::Server::Error.new(401, @params[:error], @params[:error_description], :realm => "server.example.com")
38
56
  end
39
57
 
40
58
  it "should return failure response with error message in WWW-Authenticate header" do
41
59
  status, header, response = @error.finish
42
60
  status.should === 401
43
- header['WWW-Authenticate'].should == "OAuth realm='' error_description='Something invalid!!' error='invalid_request'"
61
+ error_message = {
62
+ :error => "invalid_request",
63
+ :error_description => "Something invalid!!"
64
+ }
65
+ header['WWW-Authenticate'].should == "OAuth realm='server.example.com' #{error_message.collect {|k,v| "#{k}='#{v}'"}.join(' ')}"
44
66
  end
45
67
  end
46
68
 
47
- context "when either redirect_uri nor www_authenticate isn't given" do
69
+ context "when either redirect_uri nor realm isn't given" do
48
70
  before do
49
71
  @params = {
50
72
  :error => :invalid_request,
@@ -58,6 +80,7 @@ describe Rack::OAuth2::Server::Error, '#finish' do
58
80
  status.should === 400
59
81
  response.body.to_s.should == @params.to_json
60
82
  end
83
+
61
84
  end
62
85
 
63
86
  end
@@ -70,8 +93,15 @@ describe Rack::OAuth2::Server::BadRequest do
70
93
  end
71
94
 
72
95
  describe Rack::OAuth2::Server::Unauthorized do
73
- it "should use 400 as status" do
96
+ it "should use 401 as status" do
74
97
  error = Rack::OAuth2::Server::Unauthorized.new(:invalid_request)
75
98
  error.status.should == 401
76
99
  end
100
+ end
101
+
102
+ describe Rack::OAuth2::Server::Forbidden do
103
+ it "should use 403 as status" do
104
+ error = Rack::OAuth2::Server::Forbidden.new(:invalid_request)
105
+ error.status.should == 403
106
+ end
77
107
  end
@@ -15,11 +15,11 @@ describe Rack::OAuth2::Server::Resource, '#call' do
15
15
  when "valid_token"
16
16
  # nothing to do
17
17
  when "insufficient_scope_token"
18
- raise Rack::OAuth2::Server::Unauthorized.new(:insufficient_scope, "More scope is required.", :www_authenticate => true)
18
+ request.insufficient_scope!("More scope is required.")
19
19
  when "expired_token"
20
- raise Rack::OAuth2::Server::Unauthorized.new(:expired_token, "Given access token has been expired.", :www_authenticate => true)
20
+ request.expired_token!("Given access token has been expired.")
21
21
  else
22
- raise Rack::OAuth2::Server::Unauthorized.new(:invalid_token, "Given access token is invalid.", :www_authenticate => true)
22
+ request.invalid_token!("Given access token is invalid.")
23
23
  end
24
24
  end
25
25
  @request = Rack::MockRequest.new @app
@@ -45,13 +45,35 @@ describe Rack::OAuth2::Server::Resource, '#call' do
45
45
  @app.call(env)
46
46
  env[Rack::OAuth2::ACCESS_TOKEN].should == "valid_token"
47
47
  end
48
+
49
+ context "when Authorization header is used" do
50
+ it "should be accepted" do
51
+ env = Rack::MockRequest.env_for("/protected_resource", "HTTP_AUTHORIZATION" => "OAuth valid_token")
52
+ status, header, response = @app.call(env)
53
+ status.should == 200
54
+ env[Rack::OAuth2::ACCESS_TOKEN].should == "valid_token"
55
+ end
56
+ end
57
+
58
+ context "when request body is used" do
59
+ it "should be accepted" do
60
+ env = Rack::MockRequest.env_for("/protected_resource", :params => {:oauth_token => "valid_token"})
61
+ status, header, response = @app.call(env)
62
+ status.should == 200
63
+ env[Rack::OAuth2::ACCESS_TOKEN].should == "valid_token"
64
+ end
65
+ end
48
66
  end
49
67
 
50
68
  context "when expired_token is given" do
51
69
  it "should fail with expired_token error" do
52
70
  response = @request.get("/protected_resource?oauth_token=expired_token")
53
71
  response.status.should == 401
54
- response.headers["WWW-Authenticate"].should == "OAuth realm='server.example.com' error_description='Given access token has been expired.' error='expired_token'"
72
+ error_message = {
73
+ :error => :expired_token,
74
+ :error_description => "Given access token has been expired."
75
+ }
76
+ response.headers["WWW-Authenticate"].should == "OAuth realm='server.example.com' #{error_message.collect {|k,v| "#{k}='#{v}'"}.join(' ')}"
55
77
  end
56
78
 
57
79
  it "should not store access token in env" do
@@ -65,7 +87,11 @@ describe Rack::OAuth2::Server::Resource, '#call' do
65
87
  it "should fail with invalid_token error" do
66
88
  response = @request.get("/protected_resource?oauth_token=invalid_token")
67
89
  response.status.should == 401
68
- response.headers["WWW-Authenticate"].should == "OAuth realm='server.example.com' error_description='Given access token is invalid.' error='invalid_token'"
90
+ error_message = {
91
+ :error => :invalid_token,
92
+ :error_description => "Given access token is invalid."
93
+ }
94
+ response.headers["WWW-Authenticate"].should == "OAuth realm='server.example.com' #{error_message.collect {|k,v| "#{k}='#{v}'"}.join(' ')}"
69
95
  end
70
96
 
71
97
  it "should not store access token in env" do
@@ -79,7 +105,11 @@ describe Rack::OAuth2::Server::Resource, '#call' do
79
105
  it "should fail with invalid_request error" do
80
106
  response = @request.get("/protected_resource?oauth_token=invalid_token", "HTTP_AUTHORIZATION" => "OAuth valid_token")
81
107
  response.status.should == 400
82
- response.headers["WWW-Authenticate"].should == "OAuth realm='server.example.com' error_description='Both Authorization header and payload includes oauth_token.' error='invalid_request'"
108
+ error_message = {
109
+ :error => :invalid_request,
110
+ :error_description => "Both Authorization header and payload includes oauth_token."
111
+ }
112
+ response.headers["WWW-Authenticate"].should == "OAuth realm='server.example.com' #{error_message.collect {|k,v| "#{k}='#{v}'"}.join(' ')}"
83
113
  end
84
114
  end
85
115
 
@@ -5,7 +5,6 @@ describe Rack::OAuth2::Server::Token::Assertion do
5
5
  context "when valid assertion is given" do
6
6
 
7
7
  before do
8
- # NOTE: for some reason, test fails when called Rack::OAuth2::Server::Authorization::Token directly
9
8
  @app = Rack::OAuth2::Server::Token.new(simple_app) do |request, response|
10
9
  response.access_token = "access_token"
11
10
  end
@@ -21,7 +20,9 @@ describe Rack::OAuth2::Server::Token::Assertion do
21
20
  })
22
21
  response.status.should == 200
23
22
  response.content_type.should == "application/json"
24
- response.body.should == "{\"access_token\":\"access_token\"}"
23
+ response.body.should == {
24
+ :access_token => "access_token"
25
+ }.to_json
25
26
  end
26
27
 
27
28
  end
@@ -29,9 +30,8 @@ describe Rack::OAuth2::Server::Token::Assertion do
29
30
  context "when invalid assertion is given" do
30
31
 
31
32
  before do
32
- # NOTE: for some reason, test fails when called Rack::OAuth2::Server::Authorization::Code directly
33
33
  @app = Rack::OAuth2::Server::Token.new(simple_app) do |request, response|
34
- raise Rack::OAuth2::Server::Unauthorized.new(:invalid_grant, 'Invalid assertion.')
34
+ request.invalid_grant! 'Invalid assertion.'
35
35
  end
36
36
  @request = Rack::MockRequest.new @app
37
37
  end
@@ -43,9 +43,12 @@ describe Rack::OAuth2::Server::Token::Assertion do
43
43
  :assertion => "invalid_assertion",
44
44
  :assertion_type => "something"
45
45
  })
46
- response.status.should == 401
46
+ response.status.should == 400
47
47
  response.content_type.should == "application/json"
48
- response.body.should == "{\"error_description\":\"Invalid assertion.\",\"error\":\"invalid_grant\"}"
48
+ response.body.should == {
49
+ :error => :invalid_grant,
50
+ :error_description => "Invalid assertion."
51
+ }.to_json
49
52
  end
50
53
 
51
54
  end
@@ -5,7 +5,6 @@ describe Rack::OAuth2::Server::Token::AuthorizationCode do
5
5
  context "when valid code is given" do
6
6
 
7
7
  before do
8
- # NOTE: for some reason, test fails when called Rack::OAuth2::Server::Authorization::Token directly
9
8
  @app = Rack::OAuth2::Server::Token.new(simple_app) do |request, response|
10
9
  response.access_token = "access_token"
11
10
  end
@@ -21,7 +20,9 @@ describe Rack::OAuth2::Server::Token::AuthorizationCode do
21
20
  })
22
21
  response.status.should == 200
23
22
  response.content_type.should == "application/json"
24
- response.body.should == "{\"access_token\":\"access_token\"}"
23
+ response.body.should == {
24
+ :access_token => "access_token"
25
+ }.to_json
25
26
  end
26
27
 
27
28
  end
@@ -29,9 +30,8 @@ describe Rack::OAuth2::Server::Token::AuthorizationCode do
29
30
  context "when invalid code is given" do
30
31
 
31
32
  before do
32
- # NOTE: for some reason, test fails when called Rack::OAuth2::Server::Authorization::Code directly
33
33
  @app = Rack::OAuth2::Server::Token.new(simple_app) do |request, response|
34
- raise Rack::OAuth2::Server::Unauthorized.new(:invalid_grant, 'Invalid authorization code.')
34
+ request.invalid_grant!('Invalid authorization code.')
35
35
  end
36
36
  @request = Rack::MockRequest.new @app
37
37
  end
@@ -43,9 +43,12 @@ describe Rack::OAuth2::Server::Token::AuthorizationCode do
43
43
  :code => "invalid_authorization_code",
44
44
  :redirect_uri => "http://client.example.com/callback"
45
45
  })
46
- response.status.should == 401
46
+ response.status.should == 400
47
47
  response.content_type.should == "application/json"
48
- response.body.should == "{\"error_description\":\"Invalid authorization code.\",\"error\":\"invalid_grant\"}"
48
+ response.body.should == {
49
+ :error => :invalid_grant,
50
+ :error_description => "Invalid authorization code."
51
+ }.to_json
49
52
  end
50
53
 
51
54
  end
@@ -53,23 +56,62 @@ describe Rack::OAuth2::Server::Token::AuthorizationCode do
53
56
  context "when invalid client_id is given" do
54
57
 
55
58
  before do
56
- # NOTE: for some reason, test fails when called Rack::OAuth2::Server::Authorization::Code directly
57
59
  @app = Rack::OAuth2::Server::Token.new(simple_app) do |request, response|
58
- raise Rack::OAuth2::Server::Unauthorized.new(:invalid_client, 'Invalid client identifier.')
60
+ request.invalid_client!('Invalid client identifier.')
59
61
  end
60
62
  @request = Rack::MockRequest.new @app
61
63
  end
62
64
 
63
- it "should return error message as json response body" do
64
- response = @request.post("/", :params => {
65
- :grant_type => "authorization_code",
66
- :client_id => "invalid_client",
67
- :code => "valid_authorization_code",
68
- :redirect_uri => "http://client.example.com/callback"
69
- })
70
- response.status.should == 401
71
- response.content_type.should == "application/json"
72
- response.body.should == "{\"error_description\":\"Invalid client identifier.\",\"error\":\"invalid_client\"}"
65
+ context "when client credentials is given via Authorization header" do
66
+ it "should return 401 error" do
67
+ response = @request.post("/", :params => {
68
+ :grant_type => "authorization_code",
69
+ :code => "valid_authorization_code",
70
+ :redirect_uri => "http://client.example.com/callback"
71
+ }, 'HTTP_AUTHORIZATION' => "Basic #{["invalid_client_id:client_secret"].pack("m*")}")
72
+ response.status.should == 401
73
+ response.content_type.should == "application/json"
74
+ response.body.should == {
75
+ :error => :invalid_client,
76
+ :error_description => "Invalid client identifier."
77
+ }.to_json
78
+ end
79
+ end
80
+
81
+ context "when client credentials is given via request body" do
82
+ it "should return 400 error" do
83
+ response = @request.post("/", :params => {
84
+ :grant_type => "authorization_code",
85
+ :client_id => "invalid_client",
86
+ :code => "valid_authorization_code",
87
+ :redirect_uri => "http://client.example.com/callback"
88
+ })
89
+ response.status.should == 400
90
+ response.content_type.should == "application/json"
91
+ response.body.should == {
92
+ :error => :invalid_client,
93
+ :error_description => "Invalid client identifier."
94
+ }.to_json
95
+ end
96
+ end
97
+
98
+ context "when client credentials is given via both Authorization header and request body" do
99
+ it "should return 401 error with multiple credentials error message" do
100
+ response = @request.post("/", :params => {
101
+ :grant_type => "authorization_code",
102
+ :client_id => "invalid_client",
103
+ :code => "valid_authorization_code",
104
+ :redirect_uri => "http://client.example.com/callback"
105
+ }, 'HTTP_AUTHORIZATION' => "Basic #{["invalid_client_id:client_secret"].pack("m*")}")
106
+ response.status.should == 401
107
+ response.content_type.should == "application/json"
108
+ response.body.should == {
109
+ :error => :invalid_client,
110
+ :error_description => "Multiple client credentials are provided."
111
+ }.to_json
112
+ end
113
+ # TODO
114
+
73
115
  end
74
116
 
75
117
  end
@@ -5,7 +5,6 @@ describe Rack::OAuth2::Server::Token::Password do
5
5
  context "when valid resource owner credentials are given" do
6
6
 
7
7
  before do
8
- # NOTE: for some reason, test fails when called Rack::OAuth2::Server::Authorization::Token directly
9
8
  @app = Rack::OAuth2::Server::Token.new(simple_app) do |request, response|
10
9
  response.access_token = "access_token"
11
10
  end
@@ -21,7 +20,9 @@ describe Rack::OAuth2::Server::Token::Password do
21
20
  })
22
21
  response.status.should == 200
23
22
  response.content_type.should == "application/json"
24
- response.body.should == "{\"access_token\":\"access_token\"}"
23
+ response.body.should == {
24
+ :access_token => "access_token"
25
+ }.to_json
25
26
  end
26
27
 
27
28
  end
@@ -29,9 +30,8 @@ describe Rack::OAuth2::Server::Token::Password do
29
30
  context "when invalid resource owner credentials are given" do
30
31
 
31
32
  before do
32
- # NOTE: for some reason, test fails when called Rack::OAuth2::Server::Authorization::Code directly
33
33
  @app = Rack::OAuth2::Server::Token.new(simple_app) do |request, response|
34
- raise Rack::OAuth2::Server::Unauthorized.new(:invalid_grant, 'Invalid resource owner credentials.')
34
+ request.invalid_grant! 'Invalid resource owner credentials.'
35
35
  end
36
36
  @request = Rack::MockRequest.new @app
37
37
  end
@@ -43,9 +43,12 @@ describe Rack::OAuth2::Server::Token::Password do
43
43
  :username => "nov",
44
44
  :password => "invalid_pass"
45
45
  })
46
- response.status.should == 401
46
+ response.status.should == 400
47
47
  response.content_type.should == "application/json"
48
- response.body.should == "{\"error_description\":\"Invalid resource owner credentials.\",\"error\":\"invalid_grant\"}"
48
+ response.body.should == {
49
+ :error => :invalid_grant,
50
+ :error_description => "Invalid resource owner credentials."
51
+ }.to_json
49
52
  end
50
53
 
51
54
  end
@@ -5,7 +5,6 @@ describe Rack::OAuth2::Server::Token::RefreshToken do
5
5
  context "when valid refresh_token is given" do
6
6
 
7
7
  before do
8
- # NOTE: for some reason, test fails when called Rack::OAuth2::Server::Authorization::Token directly
9
8
  @app = Rack::OAuth2::Server::Token.new(simple_app) do |request, response|
10
9
  response.access_token = "access_token"
11
10
  end
@@ -20,7 +19,9 @@ describe Rack::OAuth2::Server::Token::RefreshToken do
20
19
  })
21
20
  response.status.should == 200
22
21
  response.content_type.should == "application/json"
23
- response.body.should == "{\"access_token\":\"access_token\"}"
22
+ response.body.should == {
23
+ :access_token => "access_token"
24
+ }.to_json
24
25
  end
25
26
 
26
27
  end
@@ -28,9 +29,8 @@ describe Rack::OAuth2::Server::Token::RefreshToken do
28
29
  context "when invalid refresh_token is given" do
29
30
 
30
31
  before do
31
- # NOTE: for some reason, test fails when called Rack::OAuth2::Server::Authorization::Code directly
32
32
  @app = Rack::OAuth2::Server::Token.new(simple_app) do |request, response|
33
- raise Rack::OAuth2::Server::Unauthorized.new(:invalid_grant, 'Invalid refresh_token.')
33
+ request.invalid_grant! 'Invalid refresh_token.'
34
34
  end
35
35
  @request = Rack::MockRequest.new @app
36
36
  end
@@ -41,9 +41,12 @@ describe Rack::OAuth2::Server::Token::RefreshToken do
41
41
  :client_id => "valid_client",
42
42
  :refresh_token => "invalid_refresh_token"
43
43
  })
44
- response.status.should == 401
44
+ response.status.should == 400
45
45
  response.content_type.should == "application/json"
46
- response.body.should == "{\"error_description\":\"Invalid refresh_token.\",\"error\":\"invalid_grant\"}"
46
+ response.body.should == {
47
+ :error => :invalid_grant,
48
+ :error_description => "Invalid refresh_token."
49
+ }.to_json
47
50
  end
48
51
 
49
52
  end
@@ -0,0 +1,26 @@
1
+ describe Rack::OAuth2::Server::Util, ".parse_uri" do
2
+
3
+ context "when String is given" do
4
+ it "should parse it as URI" do
5
+ uri = Rack::OAuth2::Server::Util.parse_uri "http://client.example.com"
6
+ uri.should be_a_kind_of(URI::Generic)
7
+ end
8
+ end
9
+
10
+ context "when URI is given" do
11
+ it "should return itself" do
12
+ _uri_ = URI.parse "http://client.example.com"
13
+ uri = Rack::OAuth2::Server::Util.parse_uri _uri_
14
+ uri.should == _uri_
15
+ end
16
+ end
17
+
18
+ context "when Integer is given" do
19
+ it "should raise error" do
20
+ lambda do
21
+ Rack::OAuth2::Server::Util.parse_uri 123
22
+ end.should raise_error(StandardError)
23
+ end
24
+ end
25
+
26
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-oauth2
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - nov matake
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-18 00:00:00 +09:00
18
+ date: 2010-10-03 00:00:00 +09:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -78,9 +78,6 @@ files:
78
78
  - README.rdoc
79
79
  - Rakefile
80
80
  - VERSION
81
- - example/server/authorize.rb
82
- - example/server/oauth2_controller.rb
83
- - example/server/token.rb
84
81
  - lib/rack/oauth2.rb
85
82
  - lib/rack/oauth2/server.rb
86
83
  - lib/rack/oauth2/server/abstract.rb
@@ -92,6 +89,9 @@ files:
92
89
  - lib/rack/oauth2/server/authorize/code_and_token.rb
93
90
  - lib/rack/oauth2/server/authorize/token.rb
94
91
  - lib/rack/oauth2/server/error.rb
92
+ - lib/rack/oauth2/server/error/authorize.rb
93
+ - lib/rack/oauth2/server/error/resource.rb
94
+ - lib/rack/oauth2/server/error/token.rb
95
95
  - lib/rack/oauth2/server/resource.rb
96
96
  - lib/rack/oauth2/server/token.rb
97
97
  - lib/rack/oauth2/server/token/assertion.rb
@@ -104,6 +104,9 @@ files:
104
104
  - spec/rack/oauth2/server/authorize/code_spec.rb
105
105
  - spec/rack/oauth2/server/authorize/token_spec.rb
106
106
  - spec/rack/oauth2/server/authorize_spec.rb
107
+ - spec/rack/oauth2/server/error/authorize_spec.rb
108
+ - spec/rack/oauth2/server/error/resource_spec.rb
109
+ - spec/rack/oauth2/server/error/token_spec.rb
107
110
  - spec/rack/oauth2/server/error_spec.rb
108
111
  - spec/rack/oauth2/server/resource_spec.rb
109
112
  - spec/rack/oauth2/server/token/assertion_spec.rb
@@ -111,6 +114,7 @@ files:
111
114
  - spec/rack/oauth2/server/token/password_spec.rb
112
115
  - spec/rack/oauth2/server/token/refresh_token_spec.rb
113
116
  - spec/rack/oauth2/server/token_spec.rb
117
+ - spec/rack/oauth2/server/util_spec.rb
114
118
  - spec/spec.opts
115
119
  - spec/spec_helper.rb
116
120
  has_rdoc: true
@@ -146,12 +150,15 @@ rubyforge_project:
146
150
  rubygems_version: 1.3.7
147
151
  signing_key:
148
152
  specification_version: 3
149
- summary: Rack Middleware for OAuth2 Client & Server
153
+ summary: Rack Middleware for OAuth2 Server
150
154
  test_files:
151
155
  - spec/rack/oauth2/server/authorize/code_and_token_spec.rb
152
156
  - spec/rack/oauth2/server/authorize/code_spec.rb
153
157
  - spec/rack/oauth2/server/authorize/token_spec.rb
154
158
  - spec/rack/oauth2/server/authorize_spec.rb
159
+ - spec/rack/oauth2/server/error/authorize_spec.rb
160
+ - spec/rack/oauth2/server/error/resource_spec.rb
161
+ - spec/rack/oauth2/server/error/token_spec.rb
155
162
  - spec/rack/oauth2/server/error_spec.rb
156
163
  - spec/rack/oauth2/server/resource_spec.rb
157
164
  - spec/rack/oauth2/server/token/assertion_spec.rb
@@ -159,4 +166,5 @@ test_files:
159
166
  - spec/rack/oauth2/server/token/password_spec.rb
160
167
  - spec/rack/oauth2/server/token/refresh_token_spec.rb
161
168
  - spec/rack/oauth2/server/token_spec.rb
169
+ - spec/rack/oauth2/server/util_spec.rb
162
170
  - spec/spec_helper.rb