Skip to content

Commit

Permalink
fix(authentication_callback): return errors detail instead of generic…
Browse files Browse the repository at this point in the history
… error 500 (#148)
  • Loading branch information
jbarreau committed Nov 7, 2023
1 parent 17d69db commit 1541de6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
6 changes: 6 additions & 0 deletions django_forest/authentication/exception.py
Expand Up @@ -7,6 +7,12 @@ class BaseAuthenticationException(BaseForestException):
class AuthenticationClientException(BaseAuthenticationException):
STATUS = 401

class AuthenticationOpenIdClientException(AuthenticationClientException):
def __init__(self, msg, error, error_description, state) -> None:
super().__init__(msg)
self.error = error
self.error_description = error_description
self.state = state

class AuthenticationSettingsException(BaseAuthenticationException):
STATUS = 500
Expand Down
12 changes: 11 additions & 1 deletion django_forest/authentication/utils.py
@@ -1,11 +1,21 @@
from django.http import JsonResponse
from django_forest.authentication.exception import BaseAuthenticationException
from django_forest.authentication.exception import BaseAuthenticationException, AuthenticationOpenIdClientException


def authentication_exception(f):
def wrapper(*args, **kwargs):
try:
return f(*args, **kwargs)
except AuthenticationOpenIdClientException as error:
return JsonResponse(
{
"error": error.error,
"error_description": error.error_description,
"state": error.state
},
status=error.STATUS
)

except BaseAuthenticationException as error:
return JsonResponse({'errors': [{'detail': str(error)}]}, status=error.STATUS)
return wrapper
13 changes: 12 additions & 1 deletion django_forest/authentication/views/callback.py
Expand Up @@ -7,7 +7,11 @@
from oic.oauth2 import AuthorizationResponse
from django.http import JsonResponse
from django.views.generic import View
from django_forest.authentication.exception import AuthenticationClientException, AuthenticationThirdPartyException
from django_forest.authentication.exception import (
AuthenticationClientException,
AuthenticationOpenIdClientException,
AuthenticationThirdPartyException
)

from django_forest.authentication.oidc.client_manager import OidcClientManager
from django_forest.authentication.utils import authentication_exception
Expand Down Expand Up @@ -63,6 +67,13 @@ def _handle_authent_error(self, response):
)

def parse_authorization_response(self, client, state, full_path_info):
if "error" in self.request.GET:
raise AuthenticationOpenIdClientException(
"error given in the query GET params",
self.request.GET["error"],
self.request.GET["error_description"],
self.request.GET["state"]
)
return client.parse_response(
AuthorizationResponse,
info=full_path_info,
Expand Down
16 changes: 16 additions & 0 deletions django_forest/tests/authentication/views/test_callback.py
Expand Up @@ -276,3 +276,19 @@ def test_iat_issued_in_future_outside_allowed_skew(self, mocked_requests_get, mo
"""
with self.assertRaises(IATError):
self.client.get(self.url)


def test_trial_period_ended(self):
query={
"error": "TrialBlockedError",
"error_description": "Your free trial has ended. We hope you enjoyed your experience with Forest Admin. "
"Upgrade now to continue accessing your project.",
"state": "{\"renderingId\": 36}",
}
url = reverse('django_forest:authentication:callback')
response = self.client.get(f'{url}?{urlencode(query)}')
self.assertEqual(response.status_code, 401)
body = response.json()
self.assertEqual(body["error"], query["error"])
self.assertEqual(body["error_description"], query["error_description"])
self.assertEqual(body["state"], query["state"])

0 comments on commit 1541de6

Please sign in to comment.