py.lib.aw_web_tools
16:c53a61e27af8
Go to Latest
py.lib.aw_web_tools/src/aw_web_tools/jwt_helper.py
* Ошибка в написании метода
4 from datetime import datetime, timedelta
5 from typing import Optional
9 JWT_HASH_ALGO = 'HS512'
12 class JWTError(Error):
16 class JWTAuthError(JWTError):
18 Провалена проверка токена на допустимость по подписи, времени действия или прочее
22 class JWTHelper(object):
23 def __init__(self, key: str):
26 def encode(self, data: dict, timeout: Optional[int] = None) -> str:
27 if timeout is not None:
28 data['exp'] = datetime.utcnow() + timedelta(seconds=timeout)
30 return jwt.encode(data, key=self.key, algorithm=JWT_HASH_ALGO)
32 def decode(self, token: str, check_timeout: bool = False) -> dict:
34 'algorithms': [JWT_HASH_ALGO, ]
38 opts['options'] = {'require': {'exp'}}
41 raise JWTAuthError('Ключ отсутствует')
43 token = token.encode('utf-8')
46 return jwt.decode(jwt=token, key=self.key, **opts)
48 except (jwt.InvalidIssuerError, jwt.InvalidSignatureError, jwt.ExpiredSignatureError) as e:
49 raise JWTAuthError(str(e))
51 except jwt.PyJWTError as e:
52 raise JWTError(f'{type(e).__name__}: {e}')
55 def make_fabric(cls, key: str):