This repository has been archived by the owner on Jun 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from edx/jsa/ecom-2839-2
adjust JWT_LEEWAY and use custom JWT decode function
- Loading branch information
Showing
4 changed files
with
97 additions
and
3 deletions.
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,42 @@ | ||
""" | ||
Custom JWT decoding function for django_rest_framework jwt package. | ||
Adds logging to facilitate debugging of InvalidTokenErrors. Also | ||
requires "exp" and "iat" claims to be present - the base package | ||
doesn't expose settings to enforce this. | ||
""" | ||
import logging | ||
|
||
import jwt | ||
from rest_framework_jwt.settings import api_settings | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def decode(token): | ||
""" | ||
Ensure InvalidTokenErrors are logged for diagnostic purposes, before | ||
failing authentication. | ||
""" | ||
|
||
options = { | ||
'verify_exp': api_settings.JWT_VERIFY_EXPIRATION, | ||
'require_exp': True, | ||
'require_iat': True, | ||
} | ||
|
||
try: | ||
return jwt.decode( | ||
token, | ||
api_settings.JWT_SECRET_KEY, | ||
api_settings.JWT_VERIFY, | ||
options=options, | ||
leeway=api_settings.JWT_LEEWAY, | ||
audience=api_settings.JWT_AUDIENCE, | ||
issuer=api_settings.JWT_ISSUER, | ||
algorithms=[api_settings.JWT_ALGORITHM] | ||
) | ||
except jwt.InvalidTokenError as exc: | ||
exc_type = u'{}.{}'.format(exc.__class__.__module__, exc.__class__.__name__) | ||
logger.info("raised_invalid_token: exc_type=%r, exc_detail=%r", exc_type, exc.message) | ||
raise |
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
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
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