diff --git a/sanic_security/authentication.py b/sanic_security/authentication.py index 79f2d69..e5c2e93 100644 --- a/sanic_security/authentication.py +++ b/sanic_security/authentication.py @@ -16,7 +16,6 @@ DeactivatedError, SecondFactorFulfilledError, ExpiredError, - RequiredRefreshError, ) from sanic_security.models import Account, AuthenticationSession, Role, TwoStepSession from sanic_security.utils import get_ip diff --git a/sanic_security/authorization.py b/sanic_security/authorization.py index f3bb437..945072b 100644 --- a/sanic_security/authorization.py +++ b/sanic_security/authorization.py @@ -6,7 +6,7 @@ from tortoise.exceptions import DoesNotExist from sanic_security.authentication import authenticate -from sanic_security.exceptions import AuthorizationError +from sanic_security.exceptions import AuthorizationError, AnonymousError from sanic_security.models import Role, Account, AuthenticationSession from sanic_security.utils import get_ip @@ -58,7 +58,7 @@ async def check_permissions( """ authentication_session = await authenticate(request) if authentication_session.is_anonymous: - raise AuthorizationError("Session is anonymous.") + raise AnonymousError() roles = await authentication_session.bearer.roles.filter(deleted=False).all() for role in roles: for required_permission, role_permission in zip( @@ -93,7 +93,7 @@ async def check_roles(request: Request, *required_roles: str) -> AuthenticationS """ authentication_session = await authenticate(request) if authentication_session.is_anonymous: - raise AuthorizationError("Session is anonymous.") + raise AnonymousError() roles = await authentication_session.bearer.roles.filter(deleted=False).all() for role in roles: if role.name in required_roles: diff --git a/sanic_security/exceptions.py b/sanic_security/exceptions.py index fbcc373..a395911 100644 --- a/sanic_security/exceptions.py +++ b/sanic_security/exceptions.py @@ -195,6 +195,15 @@ def __init__(self, message): super().__init__(message, 403) +class AnonymousError(AuthorizationError): + """ + Raised when attempting to authorize an anonymous session. + """ + + def __init__(self): + super().__init__("Session is anonymous.") + + class CredentialsError(SecurityError): """ Raised when credentials are invalid.