Skip to content

Commit 5f78149

Browse files
committed
and now legacy-type token is also an instance of Rack::OAuth2::AccessToken::Legacy
1 parent 50d8d39 commit 5f78149

File tree

8 files changed

+123
-35
lines changed

8 files changed

+123
-35
lines changed

lib/rack/oauth2/access_token.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,26 @@ def token_response(options = {})
2222
:scope => Array(scope).join(' ')
2323
}
2424
end
25+
26+
def get(url, headers = {}, &block)
27+
RestClient.get url, authenticate(headers), &block
28+
end
29+
30+
def post(url, payload, headers = {}, &block)
31+
RestClient.post url, payload, authenticate(headers), &block
32+
end
33+
34+
def put(url, payload, headers = {}, &block)
35+
RestClient.put url, payload, authenticate(headers), &block
36+
end
37+
38+
def delete(url, headers = {}, &block)
39+
RestClient.delete url, authenticate(headers), &block
40+
end
2541
end
2642
end
2743
end
2844

2945
require 'rack/oauth2/access_token/bearer'
30-
require 'rack/oauth2/access_token/mac'
46+
require 'rack/oauth2/access_token/mac'
47+
require 'rack/oauth2/access_token/legacy'

lib/rack/oauth2/access_token/bearer.rb

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,7 @@ module Rack
22
module OAuth2
33
class AccessToken
44
class Bearer < AccessToken
5-
def get(url, headers = {}, &block)
6-
RestClient.get url, authenticate(headers), &block
7-
end
8-
9-
def post(url, payload, headers = {}, &block)
10-
RestClient.post url, payload, authenticate(headers), &block
11-
end
12-
13-
def put(url, payload, headers = {}, &block)
14-
RestClient.put url, payload, authenticate(headers), &block
15-
end
16-
17-
def delete(url, headers = {}, &block)
18-
RestClient.delete url, authenticate(headers), &block
19-
end
20-
215
private
22-
236
def authenticate(headers)
247
headers.merge(:AUTHORIZATION => "Bearer #{access_token}")
258
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module Rack
2+
module OAuth2
3+
class AccessToken
4+
class Legacy < AccessToken
5+
def initialize(attributes = {})
6+
super
7+
self.expires_in = self.expires_in.try(:to_i)
8+
end
9+
10+
private
11+
def authenticate(headers)
12+
headers.merge(:AUTHORIZATION => "OAuth2 #{access_token}")
13+
end
14+
end
15+
end
16+
end
17+
end

lib/rack/oauth2/client.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,14 @@ def handle_response
7373
AccessToken::Bearer.new(token_hash)
7474
when 'mac'
7575
AccessToken::MAC.new(token_hash)
76+
when nil
77+
AccessToken::Legacy.new(token_hash)
7678
else
77-
token_hash
79+
raise 'Unknown Token Type'
7880
end
7981
rescue JSON::ParserError
8082
# NOTE: Facebook support (They don't use JSON as token response)
81-
Rack::Utils.parse_nested_query(response.body).with_indifferent_access
83+
AccessToken::Legacy.new Rack::Utils.parse_nested_query(response.body).with_indifferent_access
8284
rescue RestClient::Exception => e
8385
error = JSON.parse(e.http_body).with_indifferent_access
8486
raise Error.new(e.http_code, error)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
access_token=access_token
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"access_token":"access_token",
3+
"refresh_token":"refresh_token",
4+
"token_type":"unknown",
5+
"expires_in":3600
6+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require 'spec_helper'
2+
3+
describe Rack::OAuth2::AccessToken::Legacy do
4+
let :token do
5+
Rack::OAuth2::AccessToken::Legacy.new(
6+
:access_token => 'access_token'
7+
)
8+
end
9+
let(:resource_endpoint) { 'https://server.example.com/resources/fake' }
10+
11+
[:get, :delete].each do |method|
12+
before do
13+
fake_response(method, resource_endpoint, 'resources/fake.txt')
14+
end
15+
16+
describe method.to_s.upcase do
17+
it 'should have OAuth2 Authorization header' do
18+
RestClient.should_receive(method).with(
19+
resource_endpoint,
20+
:AUTHORIZATION => 'OAuth2 access_token'
21+
)
22+
token.send method, resource_endpoint
23+
end
24+
end
25+
end
26+
27+
[:post, :put].each do |method|
28+
before do
29+
fake_response(method, resource_endpoint, 'resources/fake.txt')
30+
end
31+
32+
describe method.to_s.upcase do
33+
it 'should have OAuth2 Authorization header' do
34+
RestClient.should_receive(method).with(
35+
resource_endpoint,
36+
{:key => :value},
37+
{:AUTHORIZATION => 'OAuth2 access_token'}
38+
)
39+
token.send method, resource_endpoint, {:key => :value}
40+
end
41+
end
42+
end
43+
end

spec/rack/oauth2/client_spec.rb

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
its(:expires_in) { should == 3600 }
107107
end
108108

109-
context 'when legacy-style (JSON) token is given' do
109+
context 'when no-type token is given (JSON)' do
110110
before do
111111
client.authorization_code = 'code'
112112
fake_response(
@@ -115,30 +115,49 @@
115115
'tokens/legacy.json'
116116
)
117117
end
118-
it { should be_instance_of ActiveSupport::HashWithIndifferentAccess }
119-
it do
120-
client.access_token!.should == {
121-
'access_token' => 'access_token',
122-
'refresh_token' => 'refresh_token',
123-
'expires_in' => 3600
124-
}
125-
end
118+
it { should be_instance_of Rack::OAuth2::AccessToken::Legacy }
119+
its(:token_type) { should == :legacy }
120+
its(:access_token) { should == 'access_token' }
121+
its(:refresh_token) { should == 'refresh_token' }
122+
its(:expires_in) { should == 3600 }
126123
end
127124

128-
context 'when legacy-style (key-value) response is given' do
125+
context 'when no-type token is given (key-value)' do
129126
before do
130127
fake_response(
131128
:post,
132129
'https://server.example.com/oauth2/token',
133130
'tokens/legacy.txt'
134131
)
135132
end
136-
it { should be_instance_of ActiveSupport::HashWithIndifferentAccess }
133+
it { should be_instance_of Rack::OAuth2::AccessToken::Legacy }
134+
its(:token_type) { should == :legacy }
135+
its(:access_token) { should == 'access_token' }
136+
its(:expires_in) { should == 3600 }
137+
138+
context 'when expires_in is not given' do
139+
before do
140+
fake_response(
141+
:post,
142+
'https://server.example.com/oauth2/token',
143+
'tokens/legacy_without_expires_in.txt'
144+
)
145+
end
146+
its(:expires_in) { should be_nil }
147+
end
148+
end
149+
150+
context 'when unknown-type token is given' do
151+
before do
152+
client.authorization_code = 'code'
153+
fake_response(
154+
:post,
155+
'https://server.example.com/oauth2/token',
156+
'tokens/unknown.json'
157+
)
158+
end
137159
it do
138-
client.access_token!.should == {
139-
'access_token' => 'access_token',
140-
'expires_in' => '3600' # NOTE: String not Integer
141-
}
160+
expect { client.access_token! }.should raise_error(StandardError, 'Unknown Token Type')
142161
end
143162
end
144163

0 commit comments

Comments
 (0)