rack-oauth2 0.4.2 → 0.4.3

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.2
1
+ 0.4.3
@@ -3,20 +3,11 @@ module Rack
3
3
  class Client
4
4
  include AttrRequired, AttrOptional
5
5
  attr_required :identifier
6
- attr_optional :secret, :redirect_uri, :scheme, :host, :response_type, :authorization_endpoint, :token_endpoint
7
-
8
- class Exception < StandardError
9
- attr_accessor :status, :response
10
- def initialize(status, response)
11
- @status = status
12
- @response = response
13
- super response[:error_description]
14
- end
15
- end
6
+ attr_optional :secret, :redirect_uri, :scheme, :host, :authorization_endpoint, :token_endpoint
16
7
 
17
8
  def initialize(attributes = {})
18
9
  (required_attributes + optional_attributes).each do |key|
19
- self.send "#{key}=", attributes[key]
10
+ self.send :"#{key}=", attributes[key]
20
11
  end
21
12
  @grant = Grant::ClientCredentials.new
22
13
  @authorization_endpoint ||= '/oauth2/authorize'
@@ -24,11 +15,12 @@ module Rack
24
15
  attr_missing!
25
16
  end
26
17
 
27
- def authorization_url(response_type = :code, params = {})
28
- Util.redirect_uri absolute_url_for(authorization_endpoint), :query, params.merge(
18
+ def authorization_uri(params = {})
19
+ params[:response_type] ||= :code
20
+ params[:scope] = Array(params[:scope]).join(' ')
21
+ Util.redirect_uri absolute_uri_for(authorization_endpoint), :query, params.merge(
29
22
  :client_id => self.identifier,
30
- :redirect_uri => self.redirect_uri,
31
- :response_type => response_type
23
+ :redirect_uri => self.redirect_uri
32
24
  )
33
25
  end
34
26
 
@@ -53,13 +45,13 @@ module Rack
53
45
  :client_secret => self.secret
54
46
  )
55
47
  handle_response do
56
- RestClient.post absolute_url_for(token_endpoint), Util.compact_hash(params)
48
+ RestClient.post absolute_uri_for(token_endpoint), Util.compact_hash(params)
57
49
  end
58
50
  end
59
51
 
60
52
  private
61
53
 
62
- def absolute_url_for(endpoint)
54
+ def absolute_uri_for(endpoint)
63
55
  _endpoint_ = Util.parse_uri endpoint
64
56
  _endpoint_.scheme ||= self.scheme || 'https'
65
57
  _endpoint_.host ||= self.host
@@ -70,15 +62,12 @@ module Rack
70
62
  response = yield
71
63
  JSON.parse(response.body).with_indifferent_access
72
64
  rescue RestClient::Exception => e
73
- error = if e.http_body
74
- JSON.parse(e.http_body).with_indifferent_access
75
- else
76
- {}
77
- end
78
- raise Exception.new(e.http_code, error)
65
+ error = JSON.parse(e.http_body).with_indifferent_access
66
+ raise Error.new(e.http_code, error)
79
67
  end
80
68
  end
81
69
  end
82
70
  end
83
71
 
72
+ require 'rack/oauth2/client/error'
84
73
  require 'rack/oauth2/client/grant'
@@ -0,0 +1,14 @@
1
+ module Rack
2
+ module OAuth2
3
+ class Client
4
+ class Error < StandardError
5
+ attr_accessor :status, :response
6
+ def initialize(status, response)
7
+ @status = status
8
+ @response = response
9
+ super response[:error_description]
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
data/rack-oauth2.gemspec CHANGED
@@ -22,4 +22,5 @@ Gem::Specification.new do |s|
22
22
  s.add_development_dependency "rake", ">= 0.8"
23
23
  s.add_development_dependency "rcov", ">= 0.9"
24
24
  s.add_development_dependency "rspec", ">= 2"
25
+ s.add_development_dependency "fakeweb", ">= 1.3"
25
26
  end
@@ -0,0 +1,4 @@
1
+ {
2
+ "error":"invalid_request",
3
+ "error_description":"error description"
4
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "access_token":"access_token",
3
+ "expires_in":3600
4
+ }
@@ -0,0 +1,18 @@
1
+ require 'spec_helper.rb'
2
+
3
+ describe Rack::OAuth2::Client::Error do
4
+ let :error do
5
+ {
6
+ :error => :invalid_request,
7
+ :error_description => 'Include invalid parameters',
8
+ :error_uri => 'http://server.example.com/error/invalid_request'
9
+ }
10
+ end
11
+ subject do
12
+ Rack::OAuth2::Client::Error.new 400, error
13
+ end
14
+
15
+ its(:status) { should == 400 }
16
+ its(:message) { should == error[:error_description] }
17
+ its(:response) { should == error }
18
+ end
@@ -0,0 +1,102 @@
1
+ require 'spec_helper.rb'
2
+
3
+ describe Rack::OAuth2::Client do
4
+ let :client do
5
+ Rack::OAuth2::Client.new(
6
+ :identifier => 'client_id',
7
+ :secret => 'client_secret',
8
+ :host => 'server.example.com',
9
+ :redirect_uri => 'https://client.example.com/callback'
10
+ )
11
+ end
12
+ subject { client }
13
+
14
+ its(:identifier) { should == 'client_id' }
15
+ its(:secret) { should == 'client_secret' }
16
+ its(:authorization_endpoint) { should == '/oauth2/authorize' }
17
+ its(:token_endpoint) { should == '/oauth2/token' }
18
+
19
+ context 'when identifier is missing' do
20
+ it do
21
+ lambda do
22
+ Rack::OAuth2::Client.new
23
+ end.should raise_error AttrRequired::AttrMissing
24
+ end
25
+ end
26
+
27
+ describe '#authorization_uri' do
28
+ subject { client.authorization_uri }
29
+ it { should include 'https://server.example.com/oauth2/authorize' }
30
+ it { should include 'client_id=client_id' }
31
+ it { should include 'redirect_uri=https%3A%2F%2Fclient.example.com%2Fcallback' }
32
+ it { should include 'response_type=code' }
33
+
34
+ context 'when endpoints are absolute URIs' do
35
+ before do
36
+ client.authorization_endpoint = 'https://server2.example.com/oauth/authorize'
37
+ client.token_endpoint = 'https://server2.example.com/oauth/token'
38
+ end
39
+ it { should include 'https://server2.example.com/oauth/authorize' }
40
+ end
41
+
42
+ context 'when scheme is specified' do
43
+ before { client.scheme = 'http' }
44
+ it { should include 'http://server.example.com/oauth2/authorize' }
45
+ end
46
+
47
+ context 'when response_type is token' do
48
+ subject { client.authorization_uri(:response_type => :token) }
49
+ it { should include 'response_type=token' }
50
+ end
51
+
52
+ context 'when scope is given' do
53
+ subject { client.authorization_uri(:scope => [:scope1, :scope2]) }
54
+ it { should include 'scope=scope1+scope2' }
55
+ end
56
+ end
57
+
58
+ describe '#authorization_code=' do
59
+ before { client.authorization_code = 'code' }
60
+ subject { client.instance_variable_get('@grant') }
61
+ it { should be_instance_of Rack::OAuth2::Client::Grant::AuthorizationCode }
62
+ end
63
+
64
+ describe '#resource_owner_credentials=' do
65
+ before { client.resource_owner_credentials = 'username', 'password' }
66
+ subject { client.instance_variable_get('@grant') }
67
+ it { should be_instance_of Rack::OAuth2::Client::Grant::Password }
68
+ end
69
+
70
+ describe '#access_token!' do
71
+ before do
72
+ client.authorization_code = 'code'
73
+ fake_response(
74
+ :post,
75
+ 'https://server.example.com/oauth2/token',
76
+ 'token.json'
77
+ )
78
+ end
79
+ it do
80
+ client.access_token!.should == {
81
+ 'access_token' => 'access_token',
82
+ 'expires_in' => 3600
83
+ }
84
+ end
85
+
86
+ context 'when error response is given' do
87
+ before do
88
+ fake_response(
89
+ :post,
90
+ 'https://server.example.com/oauth2/token',
91
+ 'invalid_request.json',
92
+ :status => 400
93
+ )
94
+ end
95
+ it do
96
+ lambda do
97
+ client.access_token!
98
+ end.should raise_error Rack::OAuth2::Client::Error
99
+ end
100
+ end
101
+ end
102
+ end
data/spec/spec_helper.rb CHANGED
@@ -3,9 +3,23 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
3
 
4
4
  require 'rack/oauth2'
5
5
  require 'rspec'
6
+ require 'fakeweb'
6
7
 
7
8
  def simple_app
8
9
  lambda do |env|
9
10
  [ 200, {'Content-Type' => 'text/plain'}, ["HELLO"] ]
10
11
  end
11
- end
12
+ end
13
+
14
+ def fake_response(method, endpoint, file_path, options = {})
15
+ FakeWeb.register_uri(
16
+ method,
17
+ endpoint,
18
+ options.merge(
19
+ :body => File.read(
20
+ File.join(File.dirname(__FILE__), 'fake_response', file_path)
21
+ )
22
+ )
23
+ )
24
+ end
25
+ FakeWeb.allow_net_connect = false
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: 11
4
+ hash: 9
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 4
9
- - 2
10
- version: 0.4.2
9
+ - 3
10
+ version: 0.4.3
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: 2011-03-10 00:00:00 +09:00
18
+ date: 2011-03-21 00:00:00 +09:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -153,6 +153,21 @@ dependencies:
153
153
  version: "2"
154
154
  type: :development
155
155
  version_requirements: *id009
156
+ - !ruby/object:Gem::Dependency
157
+ name: fakeweb
158
+ prerelease: false
159
+ requirement: &id010 !ruby/object:Gem::Requirement
160
+ none: false
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ hash: 9
165
+ segments:
166
+ - 1
167
+ - 3
168
+ version: "1.3"
169
+ type: :development
170
+ version_requirements: *id010
156
171
  description: Rack Middleware for OAuth2 server. Experimental OAuth2 client library is also included.
157
172
  email: nov@matake.jp
158
173
  executables: []
@@ -173,6 +188,7 @@ files:
173
188
  - VERSION
174
189
  - lib/rack/oauth2.rb
175
190
  - lib/rack/oauth2/client.rb
191
+ - lib/rack/oauth2/client/error.rb
176
192
  - lib/rack/oauth2/client/grant.rb
177
193
  - lib/rack/oauth2/client/grant/authorization_code.rb
178
194
  - lib/rack/oauth2/client/grant/client_credentials.rb
@@ -197,6 +213,10 @@ files:
197
213
  - lib/rack/oauth2/server/token/refresh_token.rb
198
214
  - lib/rack/oauth2/util.rb
199
215
  - rack-oauth2.gemspec
216
+ - spec/fake_response/invalid_request.json
217
+ - spec/fake_response/token.json
218
+ - spec/rack/oauth2/client/error_spec.rb
219
+ - spec/rack/oauth2/client_spec.rb
200
220
  - spec/rack/oauth2/server/abstract/error_spec.rb
201
221
  - spec/rack/oauth2/server/authorize/code_spec.rb
202
222
  - spec/rack/oauth2/server/authorize/error_spec.rb
@@ -248,6 +268,10 @@ signing_key:
248
268
  specification_version: 3
249
269
  summary: Rack Middleware for OAuth2 server
250
270
  test_files:
271
+ - spec/fake_response/invalid_request.json
272
+ - spec/fake_response/token.json
273
+ - spec/rack/oauth2/client/error_spec.rb
274
+ - spec/rack/oauth2/client_spec.rb
251
275
  - spec/rack/oauth2/server/abstract/error_spec.rb
252
276
  - spec/rack/oauth2/server/authorize/code_spec.rb
253
277
  - spec/rack/oauth2/server/authorize/error_spec.rb