Skip to content

Commit

Permalink
Error revision
Browse files Browse the repository at this point in the history
  • Loading branch information
na-stewart committed Jun 21, 2024
1 parent 647df58 commit 69d958e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
1 change: 0 additions & 1 deletion sanic_security/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
DeactivatedError,
SecondFactorFulfilledError,
ExpiredError,
RequiredRefreshError,
)
from sanic_security.models import Account, AuthenticationSession, Role, TwoStepSession
from sanic_security.utils import get_ip
Expand Down
6 changes: 3 additions & 3 deletions sanic_security/authorization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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:
Expand Down
9 changes: 9 additions & 0 deletions sanic_security/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 69d958e

Please sign in to comment.