Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal to add a custom message for expired signature and incorrect token #7

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/docs/user_guide/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def refresh(
def read_current_user(
credentials: JwtAuthorizationCredentials = Security(access_security)
):
# auto_error=False, fo we should check manually
# auto_error=False, so we should check manually
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo correction

if not credentials:
raise HTTPException(status_code=401, detail='my-custom-details')

Expand Down
44 changes: 42 additions & 2 deletions fastapi_jwt/jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ def __init__(
algorithm: str = jwt.ALGORITHMS.HS256,
access_expires_delta: Optional[timedelta] = None,
refresh_expires_delta: Optional[timedelta] = None,
expired_signature_error_message: str = '',
incorrect_token_error_message: str = '',
):
assert jwt is not None, "python-jose must be installed to use JwtAuth"
if places:
Expand All @@ -82,6 +84,8 @@ def __init__(
self.algorithm = algorithm
self.access_expires_delta = access_expires_delta or timedelta(minutes=15)
self.refresh_expires_delta = refresh_expires_delta or timedelta(days=31)
self.expired_signature_error_message = expired_signature_error_message
self.incorrect_token_error_message = incorrect_token_error_message

@classmethod
def from_other(
Expand Down Expand Up @@ -113,14 +117,18 @@ def _decode(self, token: str) -> Optional[Dict[str, Any]]:
except jwt.ExpiredSignatureError as e:
if self.auto_error:
raise HTTPException(
status_code=HTTP_401_UNAUTHORIZED, detail=f"Token time expired: {e}"
status_code=HTTP_401_UNAUTHORIZED,
detail=f"Token time expired: {e}",
message=self.expired_signature_error_message
)
else:
return None
except jwt.JWTError as e:
if self.auto_error:
raise HTTPException(
status_code=HTTP_401_UNAUTHORIZED, detail=f"Wrong token: {e}"
status_code=HTTP_401_UNAUTHORIZED,
detail=f"Wrong token: {e}",
message=self.incorrect_token_error_message
)
else:
return None
Expand Down Expand Up @@ -253,6 +261,8 @@ def __init__(
algorithm: str = jwt.ALGORITHMS.HS256,
access_expires_delta: Optional[timedelta] = None,
refresh_expires_delta: Optional[timedelta] = None,
expired_signature_error_message: str = '',
incorrect_token_error_message: str = '',
):
super().__init__(
secret_key,
Expand All @@ -261,6 +271,8 @@ def __init__(
algorithm=algorithm,
access_expires_delta=access_expires_delta,
refresh_expires_delta=refresh_expires_delta,
expired_signature_error_message = expired_signature_error_message,
incorrect_token_error_message = incorrect_token_error_message,
)

async def _get_credentials(
Expand All @@ -285,6 +297,8 @@ def __init__(
algorithm: str = jwt.ALGORITHMS.HS256,
access_expires_delta: Optional[timedelta] = None,
refresh_expires_delta: Optional[timedelta] = None,
expired_signature_error_message: str = '',
incorrect_token_error_message: str = '',
):
super().__init__(
secret_key=secret_key,
Expand All @@ -293,6 +307,8 @@ def __init__(
algorithm=algorithm,
access_expires_delta=access_expires_delta,
refresh_expires_delta=refresh_expires_delta,
expired_signature_error_message = expired_signature_error_message,
incorrect_token_error_message = incorrect_token_error_message,
)

async def __call__(
Expand All @@ -309,6 +325,8 @@ def __init__(
algorithm: str = jwt.ALGORITHMS.HS256,
access_expires_delta: Optional[timedelta] = None,
refresh_expires_delta: Optional[timedelta] = None,
expired_signature_error_message: str = '',
incorrect_token_error_message: str = '',
):
super().__init__(
secret_key=secret_key,
Expand All @@ -317,6 +335,8 @@ def __init__(
algorithm=algorithm,
access_expires_delta=access_expires_delta,
refresh_expires_delta=refresh_expires_delta,
expired_signature_error_message = expired_signature_error_message,
incorrect_token_error_message = incorrect_token_error_message,
)

async def __call__(
Expand All @@ -334,6 +354,8 @@ def __init__(
algorithm: str = jwt.ALGORITHMS.HS256,
access_expires_delta: Optional[timedelta] = None,
refresh_expires_delta: Optional[timedelta] = None,
expired_signature_error_message: str = '',
incorrect_token_error_message: str = '',
):
super().__init__(
secret_key=secret_key,
Expand All @@ -342,6 +364,8 @@ def __init__(
algorithm=algorithm,
access_expires_delta=access_expires_delta,
refresh_expires_delta=refresh_expires_delta,
expired_signature_error_message = expired_signature_error_message,
incorrect_token_error_message = incorrect_token_error_message,
)

async def __call__(
Expand All @@ -364,6 +388,8 @@ def __init__(
algorithm: str = jwt.ALGORITHMS.HS256,
access_expires_delta: Optional[timedelta] = None,
refresh_expires_delta: Optional[timedelta] = None,
expired_signature_error_message: str = '',
incorrect_token_error_message: str = '',
):
super().__init__(
secret_key,
Expand All @@ -372,6 +398,8 @@ def __init__(
algorithm=algorithm,
access_expires_delta=access_expires_delta,
refresh_expires_delta=refresh_expires_delta,
expired_signature_error_message = expired_signature_error_message,
incorrect_token_error_message = incorrect_token_error_message,
)

async def _get_credentials(
Expand Down Expand Up @@ -406,6 +434,8 @@ def __init__(
algorithm: str = jwt.ALGORITHMS.HS256,
access_expires_delta: Optional[timedelta] = None,
refresh_expires_delta: Optional[timedelta] = None,
expired_signature_error_message: str = '',
incorrect_token_error_message: str = '',
):
super().__init__(
secret_key=secret_key,
Expand All @@ -414,6 +444,8 @@ def __init__(
algorithm=algorithm,
access_expires_delta=access_expires_delta,
refresh_expires_delta=refresh_expires_delta,
expired_signature_error_message = expired_signature_error_message,
incorrect_token_error_message = incorrect_token_error_message,
)

async def __call__(
Expand All @@ -430,6 +462,8 @@ def __init__(
algorithm: str = jwt.ALGORITHMS.HS256,
access_expires_delta: Optional[timedelta] = None,
refresh_expires_delta: Optional[timedelta] = None,
expired_signature_error_message: str = '',
incorrect_token_error_message: str = '',
):
super().__init__(
secret_key=secret_key,
Expand All @@ -438,6 +472,8 @@ def __init__(
algorithm=algorithm,
access_expires_delta=access_expires_delta,
refresh_expires_delta=refresh_expires_delta,
expired_signature_error_message = expired_signature_error_message,
incorrect_token_error_message = incorrect_token_error_message,
)

async def __call__(
Expand All @@ -455,6 +491,8 @@ def __init__(
algorithm: str = jwt.ALGORITHMS.HS256,
access_expires_delta: Optional[timedelta] = None,
refresh_expires_delta: Optional[timedelta] = None,
expired_signature_error_message: str = '',
incorrect_token_error_message: str = '',
):
super().__init__(
secret_key=secret_key,
Expand All @@ -463,6 +501,8 @@ def __init__(
algorithm=algorithm,
access_expires_delta=access_expires_delta,
refresh_expires_delta=refresh_expires_delta,
expired_signature_error_message = expired_signature_error_message,
incorrect_token_error_message = incorrect_token_error_message,
)

async def __call__(
Expand Down