From 3d69b62d7d36383e8cec78901ac85dc9794f9555 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 28 May 2020 16:59:31 +0200 Subject: [PATCH] Fix password login exception --- royalnet/backpack/stars/api_login_royalnet.py | 2 +- royalnet/constellation/api/__init__.py | 4 ++-- royalnet/constellation/api/apierrors.py | 8 ++++++-- royalnet/constellation/api/apistar.py | 6 ++++-- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/royalnet/backpack/stars/api_login_royalnet.py b/royalnet/backpack/stars/api_login_royalnet.py index 86147465..ceec6a53 100644 --- a/royalnet/backpack/stars/api_login_royalnet.py +++ b/royalnet/backpack/stars/api_login_royalnet.py @@ -34,7 +34,7 @@ class ApiLoginRoyalnetStar(ApiStar): raise NotFoundError("User not found") pswd_check = user.test_password(password) if not pswd_check: - raise ApiError("Invalid password") + raise UnauthorizedError("Invalid password") token: Token = TokenT.generate(alchemy=self.alchemy, user=user, expiration_delta=datetime.timedelta(days=7)) session.add(token) await ru.asyncify(session.commit) diff --git a/royalnet/constellation/api/__init__.py b/royalnet/constellation/api/__init__.py index fd04cb9e..12ca0cee 100644 --- a/royalnet/constellation/api/__init__.py +++ b/royalnet/constellation/api/__init__.py @@ -9,7 +9,7 @@ from .apierrors import \ ParameterError, \ MissingParameterError, \ InvalidParameterError, \ - NotImplementedError, \ + MethodNotImplementedError, \ UnsupportedError @@ -29,6 +29,6 @@ __all__ = [ "ParameterError", "MissingParameterError", "InvalidParameterError", - "NotImplementedError", + "MethodNotImplementedError", "UnsupportedError", ] diff --git a/royalnet/constellation/api/apierrors.py b/royalnet/constellation/api/apierrors.py index b566a602..ceb6c459 100644 --- a/royalnet/constellation/api/apierrors.py +++ b/royalnet/constellation/api/apierrors.py @@ -6,6 +6,10 @@ class NotFoundError(ApiError): pass +class UnauthorizedError(ApiError): + pass + + class ForbiddenError(ApiError): pass @@ -26,9 +30,9 @@ class InvalidParameterError(ParameterError): pass -class NotImplementedError(ApiError): +class MethodNotImplementedError(ApiError): pass -class UnsupportedError(NotImplementedError): +class UnsupportedError(MethodNotImplementedError): pass diff --git a/royalnet/constellation/api/apistar.py b/royalnet/constellation/api/apistar.py index b6a429ec..27b45adc 100644 --- a/royalnet/constellation/api/apistar.py +++ b/royalnet/constellation/api/apistar.py @@ -32,11 +32,13 @@ class ApiStar(PageStar, ABC): apidata = ApiData(data=data, star=self, method=request.method) try: response = await self.api(apidata) + except UnauthorizedError as e: + return api_error(e, code=401) except NotFoundError as e: return api_error(e, code=404) except ForbiddenError as e: return api_error(e, code=403) - except NotImplementedError as e: + except MethodNotImplementedError as e: return api_error(e, code=501) except BadRequestError as e: return api_error(e, code=400) @@ -49,7 +51,7 @@ class ApiStar(PageStar, ABC): await apidata.session_close() async def api(self, data: ApiData) -> ru.JSON: - raise NotImplementedError() + raise MethodNotImplementedError() @classmethod def swagger(cls) -> ru.JSON: