Skip to content

Commit

Permalink
Fix leeway type error (#554)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
2ykwang and pre-commit-ci[bot] committed Apr 10, 2022
1 parent 4f223fe commit 28f5087
Show file tree
Hide file tree
Showing 21 changed files with 219 additions and 90 deletions.
25 changes: 23 additions & 2 deletions rest_framework_simplejwt/backends.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from datetime import timedelta
from typing import Union

import jwt
from django.utils.translation import gettext_lazy as _
from jwt import InvalidAlgorithmError, InvalidTokenError, algorithms
Expand Down Expand Up @@ -34,7 +37,7 @@ def __init__(
audience=None,
issuer=None,
jwk_url: str = None,
leeway=0,
leeway: Union[float, int, timedelta] = None,
):
self._validate_algorithm(algorithm)

Expand All @@ -48,6 +51,7 @@ def __init__(
self.jwks_client = PyJWKClient(jwk_url) if jwk_url else None
else:
self.jwks_client = None

self.leeway = leeway

def _validate_algorithm(self, algorithm):
Expand All @@ -67,6 +71,23 @@ def _validate_algorithm(self, algorithm):
)
)

def get_leeway(self) -> timedelta:
if self.leeway is None:
return timedelta(seconds=0)
elif isinstance(self.leeway, (int, float)):
return timedelta(seconds=self.leeway)
elif isinstance(self.leeway, timedelta):
return self.leeway
else:
raise TokenBackendError(
format_lazy(
_(
"Unrecognized type '{}', 'leeway' must be of type int, float or timedelta."
),
type(self.leeway),
)
)

def get_verifying_key(self, token):
if self.algorithm.startswith("HS"):
return self.signing_key
Expand Down Expand Up @@ -108,7 +129,7 @@ def decode(self, token, verify=True):
algorithms=[self.algorithm],
audience=self.audience,
issuer=self.issuer,
leeway=self.leeway,
leeway=self.get_leeway(),
options={
"verify_aud": self.audience is not None,
"verify_signature": verify,
Expand Down
15 changes: 10 additions & 5 deletions rest_framework_simplejwt/locale/cs/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ msgstr "Autorizační hlavička musí obsahovat dvě hodnoty oddělené mezerou"
msgid "Given token not valid for any token type"
msgstr "Daný token není validní pro žádný typ tokenu"

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

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

#: backends.py:60
#: backends.py:64
msgid "Unrecognized algorithm type '{}'"
msgstr "Nerozpoznaný typ algoritmu '{}'"

#: backends.py:66
#: backends.py:70
msgid "You must have cryptography installed to use {}."
msgstr ""

#: backends.py:118
#: backends.py:85
msgid ""
"Unrecognized type '{}', 'leeway' must be of type int, float or timedelta."
msgstr ""

#: backends.py:139
msgid "Invalid algorithm specified"
msgstr ""

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

Expand Down
15 changes: 10 additions & 5 deletions rest_framework_simplejwt/locale/de_CH/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ msgstr ""
msgid "Given token not valid for any token type"
msgstr "Der Token ist für keinen Tokentyp gültig"

#: authentication.py:116 authentication.py:138
#: authentication.py:116 authentication.py:143
msgid "Token contained no recognizable user identification"
msgstr "Token enthält keine erkennbare Benutzeridentifikation"

Expand All @@ -33,19 +33,24 @@ msgstr "Benutzer nicht gefunden"
msgid "User is inactive"
msgstr "Inaktiver Benutzer"

#: backends.py:60
#: backends.py:64
msgid "Unrecognized algorithm type '{}'"
msgstr "Unerkannter Algorithmustyp '{}'"

#: backends.py:66
#: backends.py:70
msgid "You must have cryptography installed to use {}."
msgstr ""

#: backends.py:118
#: backends.py:85
msgid ""
"Unrecognized type '{}', 'leeway' must be of type int, float or timedelta."
msgstr ""

#: backends.py:139
msgid "Invalid algorithm specified"
msgstr ""

#: backends.py:120 exceptions.py:38 tokens.py:44
#: backends.py:141 exceptions.py:38 tokens.py:44
msgid "Token is invalid or expired"
msgstr "Ungültiger oder abgelaufener Token"

Expand Down
15 changes: 10 additions & 5 deletions rest_framework_simplejwt/locale/es/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ msgstr ""
msgid "Given token not valid for any token type"
msgstr "El token dado no es valido para ningun tipo de token"

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

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

#: backends.py:60
#: backends.py:64
msgid "Unrecognized algorithm type '{}'"
msgstr "Tipo de algoritmo no reconocido '{}'"

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

#: backends.py:118
#: backends.py:85
msgid ""
"Unrecognized type '{}', 'leeway' must be of type int, float or timedelta."
msgstr ""

#: backends.py:139
msgid "Invalid algorithm specified"
msgstr "Algoritmo especificado no válido"

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

Expand Down
15 changes: 10 additions & 5 deletions rest_framework_simplejwt/locale/es_AR/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ msgstr ""
msgid "Given token not valid for any token type"
msgstr "El token dado no es válido para ningún tipo de token"

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

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

#: backends.py:60
#: backends.py:64
msgid "Unrecognized algorithm type '{}'"
msgstr "Tipo de algoritmo no reconocido '{}'"

#: backends.py:66
#: backends.py:70
msgid "You must have cryptography installed to use {}."
msgstr ""

#: backends.py:118
#: backends.py:85
msgid ""
"Unrecognized type '{}', 'leeway' must be of type int, float or timedelta."
msgstr ""

#: backends.py:139
msgid "Invalid algorithm specified"
msgstr ""

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

Expand Down
15 changes: 10 additions & 5 deletions rest_framework_simplejwt/locale/es_CL/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ msgstr ""
msgid "Given token not valid for any token type"
msgstr "El token provisto no es válido para ningún tipo de token"

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

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

#: backends.py:60
#: backends.py:64
msgid "Unrecognized algorithm type '{}'"
msgstr "Tipo de algoritmo no reconocido '{}'"

#: backends.py:66
#: backends.py:70
msgid "You must have cryptography installed to use {}."
msgstr ""

#: backends.py:118
#: backends.py:85
msgid ""
"Unrecognized type '{}', 'leeway' must be of type int, float or timedelta."
msgstr ""

#: backends.py:139
msgid "Invalid algorithm specified"
msgstr ""

#: backends.py:120 exceptions.py:38 tokens.py:44
#: backends.py:141 exceptions.py:38 tokens.py:44
msgid "Token is invalid or expired"
msgstr "Token inválido o expirado"

Expand Down
15 changes: 10 additions & 5 deletions rest_framework_simplejwt/locale/fa_IR/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ msgstr "هدر اعتبارسنجی باید شامل دو مقدار جدا ش
msgid "Given token not valid for any token type"
msgstr "توکن داده شده برای هیچ نوع توکنی معتبر نمی‌باشد"

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

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

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

#: backends.py:66
#: backends.py:70
msgid "You must have cryptography installed to use {}."
msgstr ""

#: backends.py:118
#: backends.py:85
msgid ""
"Unrecognized type '{}', 'leeway' must be of type int, float or timedelta."
msgstr ""

#: backends.py:139
msgid "Invalid algorithm specified"
msgstr ""

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

Expand Down
15 changes: 10 additions & 5 deletions rest_framework_simplejwt/locale/fr/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ msgstr ""
msgid "Given token not valid for any token type"
msgstr "Le type de jeton fourni n'est pas valide"

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

#: backends.py:60
#: backends.py:64
msgid "Unrecognized algorithm type '{}'"
msgstr "Type d'algorithme non reconnu '{}'"

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

#: backends.py:118
#: backends.py:85
msgid ""
"Unrecognized type '{}', 'leeway' must be of type int, float or timedelta."
msgstr ""

#: backends.py:139
msgid "Invalid algorithm specified"
msgstr "L'algorithme spécifié est invalide"

#: backends.py:120 exceptions.py:38 tokens.py:44
#: backends.py:141 exceptions.py:38 tokens.py:44
msgid "Token is invalid or expired"
msgstr "Le jeton est invalide ou expiré"

Expand Down
15 changes: 10 additions & 5 deletions rest_framework_simplejwt/locale/id_ID/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ msgstr "Header otorisasi harus berisi dua nilai yang dipisahkan spasi"
msgid "Given token not valid for any token type"
msgstr "Token yang diberikan tidak valid untuk semua jenis token"

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

Expand All @@ -32,19 +32,24 @@ msgstr "Pengguna tidak ditemukan"
msgid "User is inactive"
msgstr "Pengguna tidak aktif"

#: backends.py:60
#: backends.py:64
msgid "Unrecognized algorithm type '{}'"
msgstr "Jenis algoritma tidak dikenal '{}'"

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

#: backends.py:118
#: backends.py:85
msgid ""
"Unrecognized type '{}', 'leeway' must be of type int, float or timedelta."
msgstr ""

#: backends.py:139
msgid "Invalid algorithm specified"
msgstr "Algoritma yang ditentukan tidak valid"

#: backends.py:120 exceptions.py:38 tokens.py:44
#: backends.py:141 exceptions.py:38 tokens.py:44
msgid "Token is invalid or expired"
msgstr "Token tidak valid atau kedaluwarsa"

Expand Down
15 changes: 10 additions & 5 deletions rest_framework_simplejwt/locale/it_IT/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ msgstr ""
msgid "Given token not valid for any token type"
msgstr "Il token dato non è valido per qualsiasi tipo di token"

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

Expand All @@ -36,19 +36,24 @@ msgstr "Utente non trovato"
msgid "User is inactive"
msgstr "Utente non attivo"

#: backends.py:60
#: backends.py:64
msgid "Unrecognized algorithm type '{}'"
msgstr "Algoritmo di tipo '{}' non riconosciuto"

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

#: backends.py:118
#: backends.py:85
msgid ""
"Unrecognized type '{}', 'leeway' must be of type int, float or timedelta."
msgstr ""

#: backends.py:139
msgid "Invalid algorithm specified"
msgstr "L'algoritmo specificato non è valido"

#: backends.py:120 exceptions.py:38 tokens.py:44
#: backends.py:141 exceptions.py:38 tokens.py:44
msgid "Token is invalid or expired"
msgstr "Il token non è valido o è scaduto"

Expand Down
Binary file modified rest_framework_simplejwt/locale/ko_KR/LC_MESSAGES/django.mo
Binary file not shown.
Loading

0 comments on commit 28f5087

Please sign in to comment.