Source code for sanic_beskar.exceptions

from buzz import Buzz
from sanic import json
from sanic.exceptions import SanicException
from sanic.response import JSONResponse


[docs] class BeskarError(SanicException, Buzz): """ Provides a custom exception class for sanic-beskar based on py-buzz. `py-buzz on gitub <https://github.com/dusktreader/py-buzz>`_ """ status: int = 401 def __init__(self, message: str, *args: tuple, **kwargs: dict): self.status: int = self.status self.message: str = f"{self.__class__.__name__}: {message}" self.extra_args: tuple = args self.extra_kwargs: dict = kwargs self.json_response: JSONResponse = json( { "error": message, "data": self.__class__.__name__, "status": self.status, }, status=self.status, ) super().__init__(self.message, self.status) def __str__(self) -> str: """string repr""" return f"{super().__str__()} ({self.status})"
[docs] class MissingClaimError(BeskarError): """ The token is missing a required claim """ pass
[docs] class BlacklistedError(BeskarError): """ The token has been blacklisted and may not be used any more """ status = 403
[docs] class ExpiredAccessError(BeskarError): """ The token has expired for access and must be refreshed """ pass
[docs] class EarlyRefreshError(BeskarError): """ The token has not yet expired for access and may not be refreshed """ status = 425 # HTTP Status Code : 425 Too Early
[docs] class ExpiredRefreshError(BeskarError): """ The token has expired for refresh. An entirely new token must be issued """ pass
[docs] class MissingToken(BeskarError): """ The header is missing the required token """ pass
[docs] class InvalidTokenHeader(BeskarError): """ The token contained in the header is invalid """ pass
[docs] class VerifyError(InvalidTokenHeader): """ The token contained in the header is invalid """ pass
[docs] class InvalidUserError(BeskarError): """ The user is no longer valid and is now not authorized """ status = 403
[docs] class MissingRoleError(BeskarError): """ The token is missing a required role """ status = 403
[docs] class MissingRightError(BeskarError): """ The token is missing a required right based upon role breakdown """ status = 403
[docs] class MissingUserError(BeskarError): """ The user could not be identified """ pass
[docs] class AuthenticationError(BeskarError): """ The entered user's password did not match the stored password """ pass
[docs] class ClaimCollisionError(BeskarError): """ " Custom claims to pack into the payload collide with reserved claims """ pass
[docs] class LegacyScheme(BeskarError): """ The processed hash is using an outdated scheme """ pass
[docs] class InvalidResetToken(BeskarError): """ The supplied registration token is invalid """ pass
[docs] class InvalidRegistrationToken(BeskarError): """ The supplied registration token is invalid """ pass
[docs] class MisusedRegistrationToken(BeskarError): """ Attempted to use a registration token for normal access """ pass
[docs] class MisusedResetToken(BeskarError): """ Attempted to use a password reset token for normal access """ pass
[docs] class ConfigurationError(BeskarError): """ There was a problem with the configuration """ pass
[docs] class TOTPRequired(AuthenticationError): """ The user requires TOTP authentication, per configuration `BESKAR_TOTP_ENFORCE` which was not performed by this call to `authenticate()`. A call to `authenticate_totp()` should be performed separately, or a call to `authenticate()` again, but providing the users `token` value should be done. """ pass