py.lib

Yohn Y. 2022-08-05 Child:84b54a8a6d4c

31:4186c3b229fa Go to Latest

py.lib/webapp/jwt_util.py

+ Модуль работы с датаклассами и их наполнения из ORM + Утилиты Bottle + Утилиты JWT + Помошник в парсинге конфигурационных файлов

History
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/webapp/jwt_util.py	Fri Aug 05 23:58:12 2022 +0300
     1.3 @@ -0,0 +1,58 @@
     1.4 +# coding: utf-8
     1.5 +
     1.6 +import jwt
     1.7 +from datetime import datetime, timedelta
     1.8 +from typing import Optional
     1.9 +
    1.10 +
    1.11 +JWT_HASH_ALGO = 'HS512'
    1.12 +
    1.13 +
    1.14 +class JWTError(Exception):
    1.15 +    pass
    1.16 +
    1.17 +
    1.18 +class JWTAuthError(JWTError):
    1.19 +    """\
    1.20 +    Провалена проверка токена на допустимость по подписи времени или прочее
    1.21 +    """
    1.22 +
    1.23 +
    1.24 +class JWTHelper(object):
    1.25 +    def __init__(self, key: str):
    1.26 +        self.key = key
    1.27 +
    1.28 +    def encode(self, data: dict, timeout: Optional[int] = None) -> str:
    1.29 +        if timeout is not None:
    1.30 +            data['exp'] = datetime.utcnow() + timedelta(seconds=timeout)
    1.31 +
    1.32 +        return jwt.encode(data, key=self.key, algorithm=JWT_HASH_ALGO)
    1.33 +
    1.34 +    def decode(self, token: str, check_timeout: bool = False) -> dict:
    1.35 +        opts = {
    1.36 +            'algorithms': [JWT_HASH_ALGO, ]
    1.37 +        }
    1.38 +
    1.39 +        if check_timeout:
    1.40 +            opts['options'] = {'require': {'exp'}}
    1.41 +
    1.42 +        if token is None:
    1.43 +            raise JWTAuthError('Ключ отсутствует')
    1.44 +        else:
    1.45 +            token = token.encode('utf-8')
    1.46 +
    1.47 +        try:
    1.48 +            return jwt.decode(jwt=token, key=self.key, **opts)
    1.49 +
    1.50 +        except (jwt.InvalidIssuerError, jwt.InvalidSignatureError, jwt.ExpiredSignatureError) as e:
    1.51 +            raise JWTAuthError(str(e))
    1.52 +
    1.53 +        except jwt.PyJWTError as e:
    1.54 +            raise JWTError(f'{type(e).__name__}: {e}')
    1.55 +
    1.56 +    @classmethod
    1.57 +    def make_cls_fabric(cls, key: str):
    1.58 +        def f():
    1.59 +            return cls(key=key)
    1.60 +
    1.61 +        return f
    1.62 \ No newline at end of file