Skip to content

Commit 28f5087

Browse files
Fix leeway type error (jazzband#554)
* Fix lewway type error * Add test case * Update Korean translation * Add type hints * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix translation revert POT-Creation-Date * update translation Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 4f223fe commit 28f5087

File tree

21 files changed

+219
-90
lines changed

21 files changed

+219
-90
lines changed

rest_framework_simplejwt/backends.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from datetime import timedelta
2+
from typing import Union
3+
14
import jwt
25
from django.utils.translation import gettext_lazy as _
36
from jwt import InvalidAlgorithmError, InvalidTokenError, algorithms
@@ -34,7 +37,7 @@ def __init__(
3437
audience=None,
3538
issuer=None,
3639
jwk_url: str = None,
37-
leeway=0,
40+
leeway: Union[float, int, timedelta] = None,
3841
):
3942
self._validate_algorithm(algorithm)
4043

@@ -48,6 +51,7 @@ def __init__(
4851
self.jwks_client = PyJWKClient(jwk_url) if jwk_url else None
4952
else:
5053
self.jwks_client = None
54+
5155
self.leeway = leeway
5256

5357
def _validate_algorithm(self, algorithm):
@@ -67,6 +71,23 @@ def _validate_algorithm(self, algorithm):
6771
)
6872
)
6973

74+
def get_leeway(self) -> timedelta:
75+
if self.leeway is None:
76+
return timedelta(seconds=0)
77+
elif isinstance(self.leeway, (int, float)):
78+
return timedelta(seconds=self.leeway)
79+
elif isinstance(self.leeway, timedelta):
80+
return self.leeway
81+
else:
82+
raise TokenBackendError(
83+
format_lazy(
84+
_(
85+
"Unrecognized type '{}', 'leeway' must be of type int, float or timedelta."
86+
),
87+
type(self.leeway),
88+
)
89+
)
90+
7091
def get_verifying_key(self, token):
7192
if self.algorithm.startswith("HS"):
7293
return self.signing_key
@@ -108,7 +129,7 @@ def decode(self, token, verify=True):
108129
algorithms=[self.algorithm],
109130
audience=self.audience,
110131
issuer=self.issuer,
111-
leeway=self.leeway,
132+
leeway=self.get_leeway(),
112133
options={
113134
"verify_aud": self.audience is not None,
114135
"verify_signature": verify,

rest_framework_simplejwt/locale/cs/LC_MESSAGES/django.po

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ msgstr "Autorizační hlavička musí obsahovat dvě hodnoty oddělené mezerou"
1919
msgid "Given token not valid for any token type"
2020
msgstr "Daný token není validní pro žádný typ tokenu"
2121

22-
#: authentication.py:116 authentication.py:138
22+
#: authentication.py:116 authentication.py:143
2323
msgid "Token contained no recognizable user identification"
2424
msgstr "Token neobsahoval žádnou rozpoznatelnou identifikaci uživatele"
2525

@@ -31,19 +31,24 @@ msgstr "Uživatel nenalezen"
3131
msgid "User is inactive"
3232
msgstr "Uživatel není aktivní"
3333

34-
#: backends.py:60
34+
#: backends.py:64
3535
msgid "Unrecognized algorithm type '{}'"
3636
msgstr "Nerozpoznaný typ algoritmu '{}'"
3737

38-
#: backends.py:66
38+
#: backends.py:70
3939
msgid "You must have cryptography installed to use {}."
4040
msgstr ""
4141

42-
#: backends.py:118
42+
#: backends.py:85
43+
msgid ""
44+
"Unrecognized type '{}', 'leeway' must be of type int, float or timedelta."
45+
msgstr ""
46+
47+
#: backends.py:139
4348
msgid "Invalid algorithm specified"
4449
msgstr ""
4550

46-
#: backends.py:120 exceptions.py:38 tokens.py:44
51+
#: backends.py:141 exceptions.py:38 tokens.py:44
4752
msgid "Token is invalid or expired"
4853
msgstr "Token není validní nebo vypršela jeho platnost"
4954

rest_framework_simplejwt/locale/de_CH/LC_MESSAGES/django.po

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ msgstr ""
2121
msgid "Given token not valid for any token type"
2222
msgstr "Der Token ist für keinen Tokentyp gültig"
2323

24-
#: authentication.py:116 authentication.py:138
24+
#: authentication.py:116 authentication.py:143
2525
msgid "Token contained no recognizable user identification"
2626
msgstr "Token enthält keine erkennbare Benutzeridentifikation"
2727

@@ -33,19 +33,24 @@ msgstr "Benutzer nicht gefunden"
3333
msgid "User is inactive"
3434
msgstr "Inaktiver Benutzer"
3535

36-
#: backends.py:60
36+
#: backends.py:64
3737
msgid "Unrecognized algorithm type '{}'"
3838
msgstr "Unerkannter Algorithmustyp '{}'"
3939

40-
#: backends.py:66
40+
#: backends.py:70
4141
msgid "You must have cryptography installed to use {}."
4242
msgstr ""
4343

44-
#: backends.py:118
44+
#: backends.py:85
45+
msgid ""
46+
"Unrecognized type '{}', 'leeway' must be of type int, float or timedelta."
47+
msgstr ""
48+
49+
#: backends.py:139
4550
msgid "Invalid algorithm specified"
4651
msgstr ""
4752

48-
#: backends.py:120 exceptions.py:38 tokens.py:44
53+
#: backends.py:141 exceptions.py:38 tokens.py:44
4954
msgid "Token is invalid or expired"
5055
msgstr "Ungültiger oder abgelaufener Token"
5156

rest_framework_simplejwt/locale/es/LC_MESSAGES/django.po

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ msgstr ""
2121
msgid "Given token not valid for any token type"
2222
msgstr "El token dado no es valido para ningun tipo de token"
2323

24-
#: authentication.py:116 authentication.py:138
24+
#: authentication.py:116 authentication.py:143
2525
msgid "Token contained no recognizable user identification"
2626
msgstr "El token no contenía identificación de usuario reconocible"
2727

@@ -33,19 +33,24 @@ msgstr "Usuario no encontrado"
3333
msgid "User is inactive"
3434
msgstr "El usuario está inactivo"
3535

36-
#: backends.py:60
36+
#: backends.py:64
3737
msgid "Unrecognized algorithm type '{}'"
3838
msgstr "Tipo de algoritmo no reconocido '{}'"
3939

40-
#: backends.py:66
40+
#: backends.py:70
4141
msgid "You must have cryptography installed to use {}."
4242
msgstr "Debe tener criptografía instalada para usar {}."
4343

44-
#: backends.py:118
44+
#: backends.py:85
45+
msgid ""
46+
"Unrecognized type '{}', 'leeway' must be of type int, float or timedelta."
47+
msgstr ""
48+
49+
#: backends.py:139
4550
msgid "Invalid algorithm specified"
4651
msgstr "Algoritmo especificado no válido"
4752

48-
#: backends.py:120 exceptions.py:38 tokens.py:44
53+
#: backends.py:141 exceptions.py:38 tokens.py:44
4954
msgid "Token is invalid or expired"
5055
msgstr "El token es inválido o ha expirado"
5156

rest_framework_simplejwt/locale/es_AR/LC_MESSAGES/django.po

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ msgstr ""
2626
msgid "Given token not valid for any token type"
2727
msgstr "El token dado no es válido para ningún tipo de token"
2828

29-
#: authentication.py:116 authentication.py:138
29+
#: authentication.py:116 authentication.py:143
3030
msgid "Token contained no recognizable user identification"
3131
msgstr "El token no contiene ninguna identificación de usuario"
3232

@@ -38,19 +38,24 @@ msgstr "Usuario no encontrado"
3838
msgid "User is inactive"
3939
msgstr "El usuario está inactivo"
4040

41-
#: backends.py:60
41+
#: backends.py:64
4242
msgid "Unrecognized algorithm type '{}'"
4343
msgstr "Tipo de algoritmo no reconocido '{}'"
4444

45-
#: backends.py:66
45+
#: backends.py:70
4646
msgid "You must have cryptography installed to use {}."
4747
msgstr ""
4848

49-
#: backends.py:118
49+
#: backends.py:85
50+
msgid ""
51+
"Unrecognized type '{}', 'leeway' must be of type int, float or timedelta."
52+
msgstr ""
53+
54+
#: backends.py:139
5055
msgid "Invalid algorithm specified"
5156
msgstr ""
5257

53-
#: backends.py:120 exceptions.py:38 tokens.py:44
58+
#: backends.py:141 exceptions.py:38 tokens.py:44
5459
msgid "Token is invalid or expired"
5560
msgstr "El token es inválido o ha expirado"
5661

rest_framework_simplejwt/locale/es_CL/LC_MESSAGES/django.po

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ msgstr ""
2121
msgid "Given token not valid for any token type"
2222
msgstr "El token provisto no es válido para ningún tipo de token"
2323

24-
#: authentication.py:116 authentication.py:138
24+
#: authentication.py:116 authentication.py:143
2525
msgid "Token contained no recognizable user identification"
2626
msgstr "El token no contiene identificación de usuario reconocible"
2727

@@ -33,19 +33,24 @@ msgstr "Usuario no encontrado"
3333
msgid "User is inactive"
3434
msgstr "El usuario está inactivo"
3535

36-
#: backends.py:60
36+
#: backends.py:64
3737
msgid "Unrecognized algorithm type '{}'"
3838
msgstr "Tipo de algoritmo no reconocido '{}'"
3939

40-
#: backends.py:66
40+
#: backends.py:70
4141
msgid "You must have cryptography installed to use {}."
4242
msgstr ""
4343

44-
#: backends.py:118
44+
#: backends.py:85
45+
msgid ""
46+
"Unrecognized type '{}', 'leeway' must be of type int, float or timedelta."
47+
msgstr ""
48+
49+
#: backends.py:139
4550
msgid "Invalid algorithm specified"
4651
msgstr ""
4752

48-
#: backends.py:120 exceptions.py:38 tokens.py:44
53+
#: backends.py:141 exceptions.py:38 tokens.py:44
4954
msgid "Token is invalid or expired"
5055
msgstr "Token inválido o expirado"
5156

rest_framework_simplejwt/locale/fa_IR/LC_MESSAGES/django.po

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ msgstr "هدر اعتبارسنجی باید شامل دو مقدار جدا ش
1919
msgid "Given token not valid for any token type"
2020
msgstr "توکن داده شده برای هیچ نوع توکنی معتبر نمی‌باشد"
2121

22-
#: authentication.py:116 authentication.py:138
22+
#: authentication.py:116 authentication.py:143
2323
msgid "Token contained no recognizable user identification"
2424
msgstr "توکن شامل هیچ شناسه قابل تشخیصی از کاربر نیست"
2525

@@ -31,19 +31,24 @@ msgstr "کاربر یافت نشد"
3131
msgid "User is inactive"
3232
msgstr "کاربر غیرفعال است"
3333

34-
#: backends.py:60
34+
#: backends.py:64
3535
msgid "Unrecognized algorithm type '{}'"
3636
msgstr "نوع الگوریتم ناشناخته '{}'"
3737

38-
#: backends.py:66
38+
#: backends.py:70
3939
msgid "You must have cryptography installed to use {}."
4040
msgstr ""
4141

42-
#: backends.py:118
42+
#: backends.py:85
43+
msgid ""
44+
"Unrecognized type '{}', 'leeway' must be of type int, float or timedelta."
45+
msgstr ""
46+
47+
#: backends.py:139
4348
msgid "Invalid algorithm specified"
4449
msgstr ""
4550

46-
#: backends.py:120 exceptions.py:38 tokens.py:44
51+
#: backends.py:141 exceptions.py:38 tokens.py:44
4752
msgid "Token is invalid or expired"
4853
msgstr "توکن نامعتبر است یا منقضی شده است"
4954

rest_framework_simplejwt/locale/fr/LC_MESSAGES/django.po

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ msgstr ""
2020
msgid "Given token not valid for any token type"
2121
msgstr "Le type de jeton fourni n'est pas valide"
2222

23-
#: authentication.py:116 authentication.py:138
23+
#: authentication.py:116 authentication.py:143
2424
msgid "Token contained no recognizable user identification"
2525
msgstr ""
2626
"Le jeton ne contient aucune information permettant d'identifier l'utilisateur"
@@ -33,19 +33,24 @@ msgstr "L'utilisateur n'a pas été trouvé"
3333
msgid "User is inactive"
3434
msgstr "L'utilisateur est désactivé"
3535

36-
#: backends.py:60
36+
#: backends.py:64
3737
msgid "Unrecognized algorithm type '{}'"
3838
msgstr "Type d'algorithme non reconnu '{}'"
3939

40-
#: backends.py:66
40+
#: backends.py:70
4141
msgid "You must have cryptography installed to use {}."
4242
msgstr "Vous devez installer cryptography afin d'utiliser {}."
4343

44-
#: backends.py:118
44+
#: backends.py:85
45+
msgid ""
46+
"Unrecognized type '{}', 'leeway' must be of type int, float or timedelta."
47+
msgstr ""
48+
49+
#: backends.py:139
4550
msgid "Invalid algorithm specified"
4651
msgstr "L'algorithme spécifié est invalide"
4752

48-
#: backends.py:120 exceptions.py:38 tokens.py:44
53+
#: backends.py:141 exceptions.py:38 tokens.py:44
4954
msgid "Token is invalid or expired"
5055
msgstr "Le jeton est invalide ou expiré"
5156

rest_framework_simplejwt/locale/id_ID/LC_MESSAGES/django.po

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ msgstr "Header otorisasi harus berisi dua nilai yang dipisahkan spasi"
2020
msgid "Given token not valid for any token type"
2121
msgstr "Token yang diberikan tidak valid untuk semua jenis token"
2222

23-
#: authentication.py:116 authentication.py:138
23+
#: authentication.py:116 authentication.py:143
2424
msgid "Token contained no recognizable user identification"
2525
msgstr "Token tidak mengandung identifikasi pengguna yang dapat dikenali"
2626

@@ -32,19 +32,24 @@ msgstr "Pengguna tidak ditemukan"
3232
msgid "User is inactive"
3333
msgstr "Pengguna tidak aktif"
3434

35-
#: backends.py:60
35+
#: backends.py:64
3636
msgid "Unrecognized algorithm type '{}'"
3737
msgstr "Jenis algoritma tidak dikenal '{}'"
3838

39-
#: backends.py:66
39+
#: backends.py:70
4040
msgid "You must have cryptography installed to use {}."
4141
msgstr "Anda harus memasang kriptografi untuk menggunakan {}."
4242

43-
#: backends.py:118
43+
#: backends.py:85
44+
msgid ""
45+
"Unrecognized type '{}', 'leeway' must be of type int, float or timedelta."
46+
msgstr ""
47+
48+
#: backends.py:139
4449
msgid "Invalid algorithm specified"
4550
msgstr "Algoritma yang ditentukan tidak valid"
4651

47-
#: backends.py:120 exceptions.py:38 tokens.py:44
52+
#: backends.py:141 exceptions.py:38 tokens.py:44
4853
msgid "Token is invalid or expired"
4954
msgstr "Token tidak valid atau kedaluwarsa"
5055

rest_framework_simplejwt/locale/it_IT/LC_MESSAGES/django.po

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ msgstr ""
2424
msgid "Given token not valid for any token type"
2525
msgstr "Il token dato non è valido per qualsiasi tipo di token"
2626

27-
#: authentication.py:116 authentication.py:138
27+
#: authentication.py:116 authentication.py:143
2828
msgid "Token contained no recognizable user identification"
2929
msgstr "Il token non conteneva nessuna informazione riconoscibile dell'utente"
3030

@@ -36,19 +36,24 @@ msgstr "Utente non trovato"
3636
msgid "User is inactive"
3737
msgstr "Utente non attivo"
3838

39-
#: backends.py:60
39+
#: backends.py:64
4040
msgid "Unrecognized algorithm type '{}'"
4141
msgstr "Algoritmo di tipo '{}' non riconosciuto"
4242

43-
#: backends.py:66
43+
#: backends.py:70
4444
msgid "You must have cryptography installed to use {}."
4545
msgstr "Devi avere installato cryptography per usare '{}'."
4646

47-
#: backends.py:118
47+
#: backends.py:85
48+
msgid ""
49+
"Unrecognized type '{}', 'leeway' must be of type int, float or timedelta."
50+
msgstr ""
51+
52+
#: backends.py:139
4853
msgid "Invalid algorithm specified"
4954
msgstr "L'algoritmo specificato non è valido"
5055

51-
#: backends.py:120 exceptions.py:38 tokens.py:44
56+
#: backends.py:141 exceptions.py:38 tokens.py:44
5257
msgid "Token is invalid or expired"
5358
msgstr "Il token non è valido o è scaduto"
5459

0 commit comments

Comments
 (0)