Skip to content

Commit 1541de6

Browse files
authored
fix(authentication_callback): return errors detail instead of generic error 500 (#148)
1 parent 17d69db commit 1541de6

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

django_forest/authentication/exception.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ class BaseAuthenticationException(BaseForestException):
77
class AuthenticationClientException(BaseAuthenticationException):
88
STATUS = 401
99

10+
class AuthenticationOpenIdClientException(AuthenticationClientException):
11+
def __init__(self, msg, error, error_description, state) -> None:
12+
super().__init__(msg)
13+
self.error = error
14+
self.error_description = error_description
15+
self.state = state
1016

1117
class AuthenticationSettingsException(BaseAuthenticationException):
1218
STATUS = 500
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
from django.http import JsonResponse
2-
from django_forest.authentication.exception import BaseAuthenticationException
2+
from django_forest.authentication.exception import BaseAuthenticationException, AuthenticationOpenIdClientException
33

44

55
def authentication_exception(f):
66
def wrapper(*args, **kwargs):
77
try:
88
return f(*args, **kwargs)
9+
except AuthenticationOpenIdClientException as error:
10+
return JsonResponse(
11+
{
12+
"error": error.error,
13+
"error_description": error.error_description,
14+
"state": error.state
15+
},
16+
status=error.STATUS
17+
)
18+
919
except BaseAuthenticationException as error:
1020
return JsonResponse({'errors': [{'detail': str(error)}]}, status=error.STATUS)
1121
return wrapper

django_forest/authentication/views/callback.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
from oic.oauth2 import AuthorizationResponse
88
from django.http import JsonResponse
99
from django.views.generic import View
10-
from django_forest.authentication.exception import AuthenticationClientException, AuthenticationThirdPartyException
10+
from django_forest.authentication.exception import (
11+
AuthenticationClientException,
12+
AuthenticationOpenIdClientException,
13+
AuthenticationThirdPartyException
14+
)
1115

1216
from django_forest.authentication.oidc.client_manager import OidcClientManager
1317
from django_forest.authentication.utils import authentication_exception
@@ -63,6 +67,13 @@ def _handle_authent_error(self, response):
6367
)
6468

6569
def parse_authorization_response(self, client, state, full_path_info):
70+
if "error" in self.request.GET:
71+
raise AuthenticationOpenIdClientException(
72+
"error given in the query GET params",
73+
self.request.GET["error"],
74+
self.request.GET["error_description"],
75+
self.request.GET["state"]
76+
)
6677
return client.parse_response(
6778
AuthorizationResponse,
6879
info=full_path_info,

django_forest/tests/authentication/views/test_callback.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,19 @@ def test_iat_issued_in_future_outside_allowed_skew(self, mocked_requests_get, mo
276276
"""
277277
with self.assertRaises(IATError):
278278
self.client.get(self.url)
279+
280+
281+
def test_trial_period_ended(self):
282+
query={
283+
"error": "TrialBlockedError",
284+
"error_description": "Your free trial has ended. We hope you enjoyed your experience with Forest Admin. "
285+
"Upgrade now to continue accessing your project.",
286+
"state": "{\"renderingId\": 36}",
287+
}
288+
url = reverse('django_forest:authentication:callback')
289+
response = self.client.get(f'{url}?{urlencode(query)}')
290+
self.assertEqual(response.status_code, 401)
291+
body = response.json()
292+
self.assertEqual(body["error"], query["error"])
293+
self.assertEqual(body["error_description"], query["error_description"])
294+
self.assertEqual(body["state"], query["state"])

0 commit comments

Comments
 (0)