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

Commit 76cf670

Browse files
liamlinalvinchow86
authored andcommitted
Fix some errors with Python2.6 and Python3
For Python2.6: fix non-existing total_seconds by converting datetime delta to seconds. For Python3: fix "datetime is not JSON serializable" issue by converting the delta datetime to unix timestamp first.
1 parent 5fc75bf commit 76cf670

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

rest_framework_jwt/serializers.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from calendar import timegm
2-
from datetime import datetime
2+
from datetime import datetime, timedelta
33
import jwt
44

55
from django.contrib.auth import authenticate, get_user_model
@@ -101,9 +101,12 @@ def validate(self, attrs):
101101
orig_iat = payload.get('orig_iat')
102102
if orig_iat:
103103
# Verify expiration
104+
renewal_limit = api_settings.JWT_TOKEN_RENEWAL_LIMIT
105+
if isinstance(renewal_limit, timedelta):
106+
renewal_limit = renewal_limit.days * 24 * 3600 + renewal_limit.seconds
104107
expiration_timestamp = (
105108
orig_iat +
106-
int(api_settings.JWT_TOKEN_RENEWAL_LIMIT.total_seconds())
109+
int(renewal_limit)
107110
)
108111
now_timestamp = timegm(datetime.utcnow().utctimetuple())
109112
if now_timestamp > expiration_timestamp:

rest_framework_jwt/utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55

66

77
def jwt_payload_handler(user):
8+
delta_timestamp = (datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA) - datetime(1970,1,1)
9+
# total seconds
10+
exp = delta_timestamp.seconds + delta_timestamp.days * 24 * 3600
11+
812
return {
913
'user_id': user.pk,
1014
'email': user.email,
1115
'username': user.get_username(),
12-
'exp': datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA
16+
'exp': exp
1317
}
1418

1519

0 commit comments

Comments
 (0)