Skip to content

Commit 641560f

Browse files
committed
automatic json encoding & decoding, and remove legacy token support
1 parent 2fa5370 commit 641560f

File tree

12 files changed

+32
-134
lines changed

12 files changed

+32
-134
lines changed

lib/rack/oauth2.rb

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def self.http_client(agent_name = "Rack::OAuth2 (#{VERSION})", &local_http_confi
4444
Faraday.new(headers: {user_agent: agent_name}) do |faraday|
4545
faraday.request :url_encoded
4646
faraday.request :json
47+
faraday.response :json
4748
faraday.response :logger, Rack::OAuth2.logger, {bodies: true} if debugging?
4849
faraday.adapter Faraday.default_adapter
4950
local_http_config&.call(faraday)

lib/rack/oauth2/access_token.rb

-1
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,4 @@ def token_response(options = {})
3939

4040
require 'rack/oauth2/access_token/authenticator'
4141
require 'rack/oauth2/access_token/bearer'
42-
require 'rack/oauth2/access_token/legacy'
4342
require 'rack/oauth2/access_token/mtls'

lib/rack/oauth2/access_token/legacy.rb

-19
This file was deleted.

lib/rack/oauth2/client.rb

+3-8
Original file line numberDiff line numberDiff line change
@@ -213,24 +213,19 @@ def handle_revocation_response
213213
end
214214

215215
def handle_success_response(response)
216-
token_hash = JSON.parse(response.body).with_indifferent_access
216+
token_hash = response.body.with_indifferent_access
217217
case (@forced_token_type || token_hash[:token_type])&.downcase
218218
when 'bearer'
219219
AccessToken::Bearer.new(token_hash)
220-
when nil
221-
AccessToken::Legacy.new(token_hash)
222220
else
223221
raise 'Unknown Token Type'
224222
end
225-
rescue JSON::ParserError
226-
# NOTE: Facebook support (They don't use JSON as token response)
227-
AccessToken::Legacy.new Rack::Utils.parse_nested_query(response.body).with_indifferent_access
228223
end
229224

230225
def handle_error_response(response)
231-
error = JSON.parse(response.body).with_indifferent_access
226+
error = response.body.with_indifferent_access
232227
raise Error.new(response.status, error)
233-
rescue JSON::ParserError
228+
rescue Faraday::ParsingError, NoMethodError
234229
raise Error.new(response.status, error: 'Unknown', error_description: response.body)
235230
end
236231
end

spec/helpers/webmock_helper.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ def request_for(method, options = {})
2828

2929
def response_for(response_file, options = {})
3030
response = {}
31-
response[:body] = File.new(File.join(File.dirname(__FILE__), '../mock_response', response_file))
31+
format = options[:format] || :json
32+
if format == :json
33+
response[:headers] = {
34+
'Content-Type': 'application/json'
35+
}
36+
end
37+
response[:body] = File.new(File.join(File.dirname(__FILE__), '../mock_response', "#{response_file}.#{format}"))
3238
if options[:status]
3339
response[:status] = options[:status]
3440
end
File renamed without changes.

spec/mock_response/tokens/legacy.json

-5
This file was deleted.

spec/mock_response/tokens/legacy.txt

-1
This file was deleted.

spec/mock_response/tokens/legacy_without_expires_in.txt

-1
This file was deleted.

spec/rack/oauth2/access_token/authenticator_spec.rb

-9
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,6 @@
1212
end
1313
end
1414

15-
context 'when Legacy token is given' do
16-
let(:token) do
17-
Rack::OAuth2::AccessToken::Legacy.new(
18-
access_token: 'access_token'
19-
)
20-
end
21-
it_behaves_like :authenticator
22-
end
23-
2415
context 'when Bearer token is given' do
2516
let(:token) do
2617
Rack::OAuth2::AccessToken::Bearer.new(

spec/rack/oauth2/access_token/legacy_spec.rb

-23
This file was deleted.

spec/rack/oauth2/client_spec.rb

+21-66
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
mock_response(
9494
:post,
9595
'https://server.example.com/oauth2/token',
96-
'tokens/bearer.json',
96+
'tokens/bearer',
9797
request_header: {
9898
'Authorization' => 'Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ='
9999
}
@@ -109,7 +109,7 @@
109109
mock_response(
110110
:post,
111111
'https://server.example.com/oauth2/token',
112-
'tokens/bearer.json',
112+
'tokens/bearer',
113113
request_header: {
114114
'Authorization' => 'Basic aHR0cHMlM0ElMkYlMkZjbGllbnQuZXhhbXBsZS5jb206Y2xpZW50X3NlY3JldA=='
115115
}
@@ -127,7 +127,7 @@
127127
mock_response(
128128
:post,
129129
'https://server.example.com/oauth2/token',
130-
'tokens/bearer.json',
130+
'tokens/bearer',
131131
request_header: {
132132
'Authorization' => 'Basic aHR0cHM6Ly9jbGllbnQuZXhhbXBsZS5jb206Y2xpZW50X3NlY3JldA=='
133133
}
@@ -143,7 +143,7 @@
143143
mock_response(
144144
:post,
145145
'https://server.example.com/oauth2/token',
146-
'tokens/bearer.json',
146+
'tokens/bearer',
147147
params: {
148148
client_assertion: /^eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9\..+/, # NOTE: HS256
149149
client_assertion_type: Rack::OAuth2::URN::ClientAssertionType::JWT_BEARER,
@@ -171,7 +171,7 @@
171171
mock_response(
172172
:post,
173173
'https://server.example.com/oauth2/token',
174-
'tokens/bearer.json',
174+
'tokens/bearer',
175175
params: {
176176
client_assertion: /^eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9\..+/, # NOTE: RS256
177177
client_assertion_type: Rack::OAuth2::URN::ClientAssertionType::JWT_BEARER,
@@ -198,7 +198,7 @@
198198
mock_response(
199199
:post,
200200
'https://server.example.com/oauth2/token',
201-
'tokens/bearer.json',
201+
'tokens/bearer',
202202
params: {
203203
client_assertion: /^eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9\..+/, # NOTE: ES256
204204
client_assertion_type: Rack::OAuth2::URN::ClientAssertionType::JWT_BEARER,
@@ -225,7 +225,7 @@
225225
mock_response(
226226
:post,
227227
'https://server.example.com/oauth2/token',
228-
'tokens/bearer.json',
228+
'tokens/bearer',
229229
params: {
230230
client_assertion: 'any.jwt.assertion',
231231
client_assertion_type: Rack::OAuth2::URN::ClientAssertionType::JWT_BEARER,
@@ -244,7 +244,7 @@
244244
mock_response(
245245
:post,
246246
'https://server.example.com/oauth2/token',
247-
'tokens/bearer.json',
247+
'tokens/bearer',
248248
params: {
249249
client_id: 'client_id',
250250
client_secret: 'client_secret',
@@ -262,7 +262,7 @@
262262
mock_response(
263263
:post,
264264
'https://server.example.com/oauth2/token',
265-
'tokens/bearer.json',
265+
'tokens/bearer',
266266
params: {
267267
client_id: 'client_id',
268268
client_secret: 'client_secret',
@@ -282,7 +282,7 @@
282282
mock_response(
283283
:post,
284284
'https://server.example.com/oauth2/token',
285-
'tokens/bearer.json',
285+
'tokens/bearer',
286286
params: {
287287
grant_type: 'client_credentials',
288288
scope: 'a b'
@@ -298,7 +298,7 @@
298298
mock_response(
299299
:post,
300300
'https://server.example.com/oauth2/token',
301-
'tokens/bearer.json',
301+
'tokens/bearer',
302302
params: {
303303
grant_type: 'client_credentials',
304304
resource: 'something'
@@ -314,7 +314,7 @@
314314
mock_response(
315315
:post,
316316
'https://server.example.com/oauth2/token',
317-
'tokens/bearer.json',
317+
'tokens/bearer',
318318
request_header: {
319319
'Authorization' => 'Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=',
320320
'X-Foo' => 'bar'
@@ -332,7 +332,7 @@
332332
mock_response(
333333
:post,
334334
'https://server.example.com/oauth2/token',
335-
'tokens/bearer.json'
335+
'tokens/bearer'
336336
)
337337
end
338338
it { should be_instance_of Rack::OAuth2::AccessToken::Bearer }
@@ -347,70 +347,21 @@
347347
mock_response(
348348
:post,
349349
'https://server.example.com/oauth2/token',
350-
'tokens/_Bearer.json'
350+
'tokens/_Bearer'
351351
)
352352
end
353353
it { should be_instance_of Rack::OAuth2::AccessToken::Bearer }
354354
its(:token_type) { should == :bearer }
355355
end
356356
end
357357

358-
context 'when no-type token is given (JSON)' do
359-
before do
360-
client.authorization_code = 'code'
361-
mock_response(
362-
:post,
363-
'https://server.example.com/oauth2/token',
364-
'tokens/legacy.json'
365-
)
366-
end
367-
it { should be_instance_of Rack::OAuth2::AccessToken::Legacy }
368-
its(:token_type) { should == :legacy }
369-
its(:access_token) { should == 'access_token' }
370-
its(:refresh_token) { should == 'refresh_token' }
371-
its(:expires_in) { should == 3600 }
372-
373-
context 'when token_type is forced' do
374-
before do
375-
client.force_token_type! :bearer
376-
end
377-
it { should be_instance_of Rack::OAuth2::AccessToken::Bearer }
378-
its(:token_type) { should == :bearer }
379-
end
380-
end
381-
382-
context 'when no-type token is given (key-value)' do
383-
before do
384-
mock_response(
385-
:post,
386-
'https://server.example.com/oauth2/token',
387-
'tokens/legacy.txt'
388-
)
389-
end
390-
it { should be_instance_of Rack::OAuth2::AccessToken::Legacy }
391-
its(:token_type) { should == :legacy }
392-
its(:access_token) { should == 'access_token' }
393-
its(:expires_in) { should == 3600 }
394-
395-
context 'when expires_in is not given' do
396-
before do
397-
mock_response(
398-
:post,
399-
'https://server.example.com/oauth2/token',
400-
'tokens/legacy_without_expires_in.txt'
401-
)
402-
end
403-
its(:expires_in) { should be_nil }
404-
end
405-
end
406-
407358
context 'when unknown-type token is given' do
408359
before do
409360
client.authorization_code = 'code'
410361
mock_response(
411362
:post,
412363
'https://server.example.com/oauth2/token',
413-
'tokens/unknown.json'
364+
'tokens/unknown'
414365
)
415366
end
416367
it do
@@ -423,7 +374,7 @@
423374
mock_response(
424375
:post,
425376
'https://server.example.com/oauth2/token',
426-
'errors/invalid_request.json',
377+
'errors/invalid_request',
427378
status: 400
428379
)
429380
end
@@ -439,6 +390,7 @@
439390
:post,
440391
'https://server.example.com/oauth2/token',
441392
'blank',
393+
format: 'txt',
442394
status: 400
443395
)
444396
end
@@ -456,6 +408,7 @@
456408
:post,
457409
'https://server.example.com/oauth2/revoke',
458410
'blank',
411+
format: 'txt',
459412
status: 200,
460413
body: {
461414
token: 'access_token',
@@ -478,6 +431,7 @@
478431
:post,
479432
'https://server.example.com/oauth2/revoke',
480433
'blank',
434+
format: 'txt',
481435
status: 200,
482436
body: {
483437
token: 'access_token',
@@ -496,6 +450,7 @@
496450
:post,
497451
'https://server.example.com/oauth2/revoke',
498452
'blank',
453+
format: 'txt',
499454
status: 200,
500455
body: {
501456
token: 'refresh_token',
@@ -523,7 +478,7 @@
523478
mock_response(
524479
:post,
525480
'https://server.example.com/oauth2/revoke',
526-
'errors/invalid_request.json',
481+
'errors/invalid_request',
527482
status: 400
528483
)
529484
end

0 commit comments

Comments
 (0)