Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions rest_framework_simplejwt/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ def check_exp(self, claim="exp", current_time=None):
raise TokenError(format_lazy(_("Token has no '{}' claim"), claim))

claim_time = datetime_from_epoch(claim_value)
if claim_time <= current_time:
leeway = self.get_token_backend().leeway
if claim_time <= current_time - timedelta(seconds=leeway):
raise TokenError(format_lazy(_("Token '{}' claim has expired"), claim))

@classmethod
Expand All @@ -183,13 +184,18 @@ def for_user(cls, user):

_token_backend = None

def get_token_backend(self):
@property
def token_backend(self):
if self._token_backend is None:
self._token_backend = import_string(
"rest_framework_simplejwt.state.token_backend"
)
return self._token_backend

def get_token_backend(self):
# Backward compatibility.
return self.token_backend


class BlacklistMixin:
"""
Expand Down
14 changes: 14 additions & 0 deletions tests/test_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,20 @@ def test_check_exp(self):
"refresh_exp", current_time=current_time + timedelta(days=2)
)

def test_check_token_not_expired_if_in_leeway(self):
token = MyToken()
token.set_exp("refresh_exp", lifetime=timedelta(days=1))

datetime_in_leeway = token.current_time + timedelta(days=1)

with self.assertRaises(TokenError):
token.check_exp("refresh_exp", current_time=datetime_in_leeway)

# a token 1 day expired is valid if leeway is 2 days
token.token_backend.leeway = timedelta(days=2).total_seconds()
token.check_exp("refresh_exp", current_time=datetime_in_leeway)
token.token_backend.leeway = 0

def test_for_user(self):
username = "test_user"
user = User.objects.create_user(
Expand Down