forked from Kamva-Academy/Kamva-Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: don't check users with not-null origin expiration
- Loading branch information
1 parent
93c4549
commit aa07b8d
Showing
2 changed files
with
40 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from rest_framework_simplejwt.authentication import JWTAuthentication | ||
from rest_framework_simplejwt.exceptions import InvalidToken | ||
from rest_framework_simplejwt.settings import api_settings | ||
from jwt import decode as jwt_decode, ExpiredSignatureError, InvalidTokenError | ||
|
||
|
||
class CustomJWTAuthentication(JWTAuthentication): | ||
def get_validated_token(self, raw_token): | ||
try: | ||
# Decode token without signature verification | ||
unverified_payload = jwt_decode( | ||
raw_token, | ||
options={"verify_signature": False}, | ||
algorithms=[api_settings.ALGORITHM], | ||
) | ||
|
||
# Check if "origin" claim is present and not null | ||
if unverified_payload.get("origin"): | ||
# Decode the token without expiration check | ||
return jwt_decode( | ||
raw_token, | ||
self.get_signing_key(), # Use the signing key for decoding | ||
algorithms=[api_settings.ALGORITHM], | ||
options={"verify_exp": False}, # Skip expiration check | ||
) | ||
|
||
# If "origin" is null, use the default validation process | ||
return super().get_validated_token(raw_token) | ||
|
||
except ExpiredSignatureError: | ||
raise InvalidToken({"detail": "Token has expired."}) | ||
except InvalidTokenError as e: | ||
raise InvalidToken({"detail": str(e)}) | ||
|
||
def get_signing_key(self): | ||
""" | ||
Retrieve the signing key from the settings. | ||
""" | ||
return api_settings.SIGNING_KEY |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters