jpadilla / django-rest-framework-jwt

JSON Web Token Authentication support for Django REST Framework
http://jpadilla.github.io/django-rest-framework-jwt/
MIT License
3.19k stars 650 forks source link

Fix redundant `jwt.decode()` calls when `JWT_GET_USER_SECRET_KEY` is `None` #416

Closed fengsi closed 2 years ago

fengsi commented 6 years ago

In most cases, it does not make any sense to perform TWO jwt.decode(), just trying to get user_id even when it's not used. The logic is now updated so that it will be short-circuited.

Interestingly, I found a previous commit from the original contributor: https://github.com/GetBlimp/django-rest-framework-jwt/commit/13ca172251f229f1f59165da170e58cb458d6b7f

It used jwt_get_secret_key(user_id=None) before. However, it did not avoid making two jwt.decode().

This PR fixed such issue. jwt.decode() is somewhat expensive call and we should not call it unnecessarily.

codecov[bot] commented 6 years ago

Codecov Report

Merging #416 into master will decrease coverage by 0.33%. The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #416      +/-   ##
==========================================
- Coverage   90.67%   90.34%   -0.34%     
==========================================
  Files          14       12       -2     
  Lines         847      818      -29     
  Branches       29       29              
==========================================
- Hits          768      739      -29     
  Misses         66       66              
  Partials       13       13
Flag Coverage Δ
#codecov 90.34% <ø> (-0.34%) :arrow_down:
#dj110 87.04% <ø> (-0.45%) :arrow_down:
#dj111 87.04% <ø> (-0.45%) :arrow_down:
#dj18 89.48% <ø> (-0.36%) :arrow_down:
#dj19 89.48% <ø> (-0.36%) :arrow_down:
#drf31 89.48% <ø> (-0.36%) :arrow_down:
#drf32 89.48% <ø> (-0.36%) :arrow_down:
#drf33 89.48% <ø> (-0.36%) :arrow_down:
#drf34 90.34% <ø> (-0.34%) :arrow_down:
#drf35 89.97% <ø> (-0.35%) :arrow_down:
#drf36 89.97% <ø> (-0.35%) :arrow_down:
#py27 90.34% <ø> (-0.34%) :arrow_down:
#py33 89.11% <ø> (-0.38%) :arrow_down:
#py34 89.97% <ø> (+0.48%) :arrow_up:
#py35 87.04% <ø> (?)
#py36 87.04% <ø> (?)
Impacted Files Coverage Δ
rest_framework_jwt/models.py

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 0a0bd40...5d2896d. Read the comment docs.