py.lib
40:366c9fe26d76
Go to Latest
py.lib/webapp/jwt_util.py
. Не импортируем ненужное.
4 from datetime import datetime, timedelta
5 from typing import Optional
7 JWT_HASH_ALGO = 'HS512'
10 class JWTError(Exception):
14 class JWTAuthError(JWTError):
16 Провалена проверка токена на допустимость по подписи времени или прочее
20 class JWTHelper(object):
21 def __init__(self, key: str):
24 def encode(self, data: dict, timeout: Optional[int] = None) -> str:
25 if timeout is not None:
26 data['exp'] = datetime.utcnow() + timedelta(seconds=timeout)
28 return jwt.encode(data, key=self.key, algorithm=JWT_HASH_ALGO)
30 def decode(self, token: str, check_timeout: bool = False) -> dict:
32 'algorithms': [JWT_HASH_ALGO, ]
36 opts['options'] = {'require': {'exp'}}
39 raise JWTAuthError('Ключ отсутствует')
41 token = token.encode('utf-8')
44 return jwt.decode(jwt=token, key=self.key, **opts)
46 except (jwt.InvalidIssuerError, jwt.InvalidSignatureError, jwt.ExpiredSignatureError) as e:
47 raise JWTAuthError(str(e))
49 except jwt.PyJWTError as e:
50 raise JWTError(f'{type(e).__name__}: {e}')
53 def make_cls_fabric(cls, key: str):