py.lib.aw_web_tools

Yohn Y. 2024-11-03 Parent:abfbff81c1fb Child:9c734271dcf2

8:b9fd029be707 Browse Files

.. 1.202411.1 . Переделываем под новые веяния SDK * Рефакторинг

pyproject.toml setup.py src/aw_web_tools/id_helper.py src/aw_web_tools/jwt.py src/aw_web_tools/jwt_helper.py

     1.1 --- a/pyproject.toml	Sun May 05 14:53:52 2024 +0300
     1.2 +++ b/pyproject.toml	Sun Nov 03 19:25:24 2024 +0300
     1.3 @@ -8,11 +8,24 @@
     1.4  [project]
     1.5  name = "aw_web_tools"
     1.6  dynamic = ["version", "description"]
     1.7 -requires-python = ">=3.8"
     1.8 +requires-python = ">=3.9"
     1.9  dependencies = [
    1.10      "PyJWT>=2.8.0",
    1.11      "bottle>=0.12.25",
    1.12  ]
    1.13  
    1.14  [project.urls]
    1.15 -src = "https://devel.a0fs.ru/py.lib.aw_web_tools/"
    1.16 \ No newline at end of file
    1.17 +documentation = "https://devel.a0fs.ru/lib.py/aw_web_tools/root"
    1.18 +source = "https://repo.devel.a0fs.ru/py.lib.aw_web_tools/"
    1.19 +download = "https://py-repo.devel.a0fs.ru"
    1.20 +
    1.21 +[project.license]
    1.22 +text = """
    1.23 +Данное ПО распространяется на условиях BSD-2-Clause
    1.24 +
    1.25 +URL: https://opensource.org/license/BSD-2-Clause
    1.26 +"""
    1.27 +
    1.28 +[[project.authors]]
    1.29 +name = 'awgur'
    1.30 +email = 'devel@a0fs.ru'
    1.31 \ No newline at end of file
     2.1 --- a/setup.py	Sun May 05 14:53:52 2024 +0300
     2.2 +++ b/setup.py	Sun Nov 03 19:25:24 2024 +0300
     2.3 @@ -2,14 +2,8 @@
     2.4  
     2.5  setup(
     2.6      name='aw_web_tools',
     2.7 -    version='0.202405.1',
     2.8 +    version='1.202411.1',
     2.9      packages=['aw_web_tools'],
    2.10 -    install_requires=[
    2.11 -        "PyJWT>=2.8.0",
    2.12 -        "bottle>=0.12.25",
    2.13 -    ],
    2.14      package_dir={'aw_web_tools': 'src/aw_web_tools'},
    2.15 -    url='https://devel.a0fs.ru/py.lib.aw_web_tools/',
    2.16 -    author_email='',
    2.17 -    description=''
    2.18 +    description='Инструментарий для Web-разработки'
    2.19  )
     3.1 --- a/src/aw_web_tools/id_helper.py	Sun May 05 14:53:52 2024 +0300
     3.2 +++ b/src/aw_web_tools/id_helper.py	Sun Nov 03 19:25:24 2024 +0300
     3.3 @@ -7,7 +7,7 @@
     3.4  from typing import Optional, Dict, Iterable, Any
     3.5  from . import btle_tools as tools
     3.6  from . import Error
     3.7 -from . import jwt
     3.8 +from . import jwt_helper
     3.9  from .cookie import Cookie
    3.10  
    3.11  
     4.1 --- a/src/aw_web_tools/jwt.py	Sun May 05 14:53:52 2024 +0300
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,59 +0,0 @@
     4.4 -# coding: utf-8
     4.5 -
     4.6 -import jwt
     4.7 -from datetime import datetime, timedelta
     4.8 -from typing import Optional
     4.9 -
    4.10 -from . import Error
    4.11 -
    4.12 -JWT_HASH_ALGO = 'HS512'
    4.13 -
    4.14 -
    4.15 -class JWTError(Error):
    4.16 -    pass
    4.17 -
    4.18 -
    4.19 -class JWTAuthError(JWTError):
    4.20 -    """\
    4.21 -    Провалена проверка токена на допустимость по подписи времени или прочее
    4.22 -    """
    4.23 -
    4.24 -
    4.25 -class JWTHelper(object):
    4.26 -    def __init__(self, key: str):
    4.27 -        self.key = key
    4.28 -
    4.29 -    def encode(self, data: dict, timeout: Optional[int] = None) -> str:
    4.30 -        if timeout is not None:
    4.31 -            data['exp'] = datetime.utcnow() + timedelta(seconds=timeout)
    4.32 -
    4.33 -        return jwt.encode(data, key=self.key, algorithm=JWT_HASH_ALGO)
    4.34 -
    4.35 -    def decode(self, token: str, check_timeout: bool = False) -> dict:
    4.36 -        opts = {
    4.37 -            'algorithms': [JWT_HASH_ALGO, ]
    4.38 -        }
    4.39 -
    4.40 -        if check_timeout:
    4.41 -            opts['options'] = {'require': {'exp'}}
    4.42 -
    4.43 -        if token is None:
    4.44 -            raise JWTAuthError('Ключ отсутствует')
    4.45 -        else:
    4.46 -            token = token.encode('utf-8')
    4.47 -
    4.48 -        try:
    4.49 -            return jwt.decode(jwt=token, key=self.key, **opts)
    4.50 -
    4.51 -        except (jwt.InvalidIssuerError, jwt.InvalidSignatureError, jwt.ExpiredSignatureError) as e:
    4.52 -            raise JWTAuthError(str(e))
    4.53 -
    4.54 -        except jwt.PyJWTError as e:
    4.55 -            raise JWTError(f'{type(e).__name__}: {e}')
    4.56 -
    4.57 -    @classmethod
    4.58 -    def make_fabric(cls, key: str):
    4.59 -        def f():
    4.60 -            return cls(key=key)
    4.61 -
    4.62 -        return f
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/aw_web_tools/jwt_helper.py	Sun Nov 03 19:25:24 2024 +0300
     5.3 @@ -0,0 +1,59 @@
     5.4 +# coding: utf-8
     5.5 +
     5.6 +import jwt
     5.7 +from datetime import datetime, timedelta
     5.8 +from typing import Optional
     5.9 +
    5.10 +from . import Error
    5.11 +
    5.12 +JWT_HASH_ALGO = 'HS512'
    5.13 +
    5.14 +
    5.15 +class JWTError(Error):
    5.16 +    pass
    5.17 +
    5.18 +
    5.19 +class JWTAuthError(JWTError):
    5.20 +    """\
    5.21 +    Провалена проверка токена на допустимость по подписи, времени действия или прочее
    5.22 +    """
    5.23 +
    5.24 +
    5.25 +class JWTHelper(object):
    5.26 +    def __init__(self, key: str):
    5.27 +        self.key = key
    5.28 +
    5.29 +    def encode(self, data: dict, timeout: Optional[int] = None) -> str:
    5.30 +        if timeout is not None:
    5.31 +            data['exp'] = datetime.utcnow() + timedelta(seconds=timeout)
    5.32 +
    5.33 +        return jwt.encode(data, key=self.key, algorithm=JWT_HASH_ALGO)
    5.34 +
    5.35 +    def decode(self, token: str, check_timeout: bool = False) -> dict:
    5.36 +        opts = {
    5.37 +            'algorithms': [JWT_HASH_ALGO, ]
    5.38 +        }
    5.39 +
    5.40 +        if check_timeout:
    5.41 +            opts['options'] = {'require': {'exp'}}
    5.42 +
    5.43 +        if token is None:
    5.44 +            raise JWTAuthError('Ключ отсутствует')
    5.45 +        else:
    5.46 +            token = token.encode('utf-8')
    5.47 +
    5.48 +        try:
    5.49 +            return jwt.decode(jwt=token, key=self.key, **opts)
    5.50 +
    5.51 +        except (jwt.InvalidIssuerError, jwt.InvalidSignatureError, jwt.ExpiredSignatureError) as e:
    5.52 +            raise JWTAuthError(str(e))
    5.53 +
    5.54 +        except jwt.PyJWTError as e:
    5.55 +            raise JWTError(f'{type(e).__name__}: {e}')
    5.56 +
    5.57 +    @classmethod
    5.58 +    def make_fabric(cls, key: str):
    5.59 +        def f():
    5.60 +            return cls(key=key)
    5.61 +
    5.62 +        return f