Skip to content

Commit

Permalink
fix(authentication): return errors detail instead of generic error 500 (
Browse files Browse the repository at this point in the history
  • Loading branch information
matthv authored Nov 16, 2023
1 parent 908f8ca commit 19f84e5
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module ForestAdminAgent
module Http
module Exceptions
class AuthenticationOpenIdClient < HttpException
attr_reader :error, :error_description, :state

def initialize(error, error_description, state)
super error, 401, error_description
@error = error
@error_description = error_description
@state = state
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module ForestAdminAgent
module Http
module Exceptions
class HttpException < StandardError
attr_reader :code, :status, :message, :name

def initialize(code, status, message, name = nil)
super(message)
@code = code
@status = status
@message = message
@name = name
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module Routes
module Security
class Authentication < AbstractRoute
include ForestAdminAgent::Builder
include ForestAdminAgent::Http::Exceptions

def setup_routes
add_route(
'forest_authentication',
Expand Down Expand Up @@ -39,6 +41,11 @@ def handle_authentication(args = {})
end

def handle_authentication_callback(args = {})
if args[:params].key?(:error)
raise AuthenticationOpenIdClient.new(args[:params][:error], args[:params][:error_description],
args[:params][:state])
end

if args.dig(:headers, 'action_dispatch.remote_ip')
Facades::Whitelist.check_ip(args[:headers]['action_dispatch.remote_ip'].to_s)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ module Security
end

it 'returns a token on the handle_authentication_callback method' do
result = authentication.handle_authentication_callback 'code' => 'abc',
'state' => "{'renderingId': #{rendering_id}}"
args = { params: { 'code' => 'abc', 'state' => "{'renderingId': #{rendering_id}}" } }
result = authentication.handle_authentication_callback args
expect(result[:content][:token]).to eq token
expect(result[:content][:tokenData]).to eq JWT.decode(
token,
Expand All @@ -81,6 +81,22 @@ module Security
end
end

context 'when callback is called with error argument in the query' do
it 'raises an error' do
args = {
params: {
error: 'TrialBlockedError',
error_description: 'Your free trial has ended...',
state: '{"renderingId"=>128}'
}
}

expect do
authentication.handle_authentication_callback args
end.to raise_error(ForestAdminAgent::Http::Exceptions::AuthenticationOpenIdClient)
end
end

context 'when handle the logout route' do
it 'returns a 204 status code' do
result = authentication.handle_authentication_logout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ def index
if ForestAdminAgent::Http::Router.routes.key? params['route_alias']
route = ForestAdminAgent::Http::Router.routes[params['route_alias']]

forest_response route[:closure].call({ params: params.to_unsafe_h, headers: request.headers.to_h })
begin
forest_response route[:closure].call({ params: params.to_unsafe_h, headers: request.headers.to_h })
rescue StandardError => e
exception_handler e
end
else
render json: { error: 'Route not found' }, status: 404
end
Expand All @@ -15,5 +19,33 @@ def index
def forest_response(data = {})
render json: data[:content], status: data[:status] || 200
end

def exception_handler(exception)
if exception.is_a? ForestAdminAgent::Http::Exceptions::AuthenticationOpenIdClient
data = {
error: exception.error,
error_description: exception.error_description,
state: exception.state
}
else
data = {
errors: [
{
name: exception.name,
detail: exception.message,
status: exception.status
}
]
}

data[:errors][0][:data] = exception.data if exception.defined? :data

# TODO: IMPLEMENT LOGGING
# if Facades::Container.cache(:is_production)
# end
end

render json: data, status: exception.status
end
end
end

0 comments on commit 19f84e5

Please sign in to comment.