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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 0 additions & 19 deletions
This file was deleted.

lib/rack/oauth2/client.rb

Lines changed: 3 additions & 8 deletions
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

Lines changed: 7 additions & 1 deletion
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

Lines changed: 0 additions & 5 deletions
This file was deleted.

spec/mock_response/tokens/legacy.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

spec/mock_response/tokens/legacy_without_expires_in.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

spec/rack/oauth2/access_token/authenticator_spec.rb

Lines changed: 0 additions & 9 deletions
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(

0 commit comments

Comments
 (0)