Skip to content
This repository was archived by the owner on May 26, 2020. It is now read-only.

Commit db5dc36

Browse files
committed
Optimize jwt_decode_handler
1. There is no need to decode the payload without verification first, in case `api_settings.JWT_PUBLIC_KEY` is used. 2. It also skips decoding in case `api_settings.JWT_GET_USER_SECRET_KEY` is not used. This might be a problem in case somebody is monkey-patching `jwt_get_secret_key`, but that should not be considered stable/public API, should it?
1 parent 8d18e08 commit db5dc36

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

rest_framework_jwt/utils.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,21 @@ def jwt_encode_handler(payload):
9797

9898

9999
def jwt_decode_handler(token):
100+
key = api_settings.JWT_PUBLIC_KEY
101+
if not key:
102+
if api_settings.JWT_GET_USER_SECRET_KEY:
103+
# get user from token, BEFORE verification, to get user secret key
104+
unverified_payload = jwt.decode(token, None, False)
105+
key = jwt_get_secret_key(unverified_payload)
106+
else:
107+
key = api_settings.JWT_SECRET_KEY
108+
100109
options = {
101110
'verify_exp': api_settings.JWT_VERIFY_EXPIRATION,
102111
}
103-
# get user from token, BEFORE verification, to get user secret key
104-
unverified_payload = jwt.decode(token, None, False)
105-
secret_key = jwt_get_secret_key(unverified_payload)
106112
return jwt.decode(
107113
token,
108-
api_settings.JWT_PUBLIC_KEY or secret_key,
114+
key,
109115
api_settings.JWT_VERIFY,
110116
options=options,
111117
leeway=api_settings.JWT_LEEWAY,

0 commit comments

Comments
 (0)