vk-mini-app-auth 1.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- vk_mini_app_auth-1.0.1/LICENSE +21 -0
- vk_mini_app_auth-1.0.1/PKG-INFO +101 -0
- vk_mini_app_auth-1.0.1/README.md +77 -0
- vk_mini_app_auth-1.0.1/pyproject.toml +68 -0
- vk_mini_app_auth-1.0.1/vk_miniapp_auth/__init__.py +7 -0
- vk_mini_app_auth-1.0.1/vk_miniapp_auth/auth.py +134 -0
- vk_mini_app_auth-1.0.1/vk_miniapp_auth/data.py +128 -0
- vk_mini_app_auth-1.0.1/vk_miniapp_auth/errors.py +10 -0
- vk_mini_app_auth-1.0.1/vk_miniapp_auth/types.py +5 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Dmitry Vasiliev
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: vk-mini-app-auth
|
|
3
|
+
Version: 1.0.1
|
|
4
|
+
Summary: Python package that implements VK Mini Apps authentication algorithms.
|
|
5
|
+
Home-page: https://pypi.org/project/vk-mini-app-auth/
|
|
6
|
+
License: MIT
|
|
7
|
+
Keywords: VK,VK Mini Apps,VK Bot,Poetry
|
|
8
|
+
Author: Dmitry Vasiliev
|
|
9
|
+
Author-email: contact.vasiliev.dmitry@gmail.com
|
|
10
|
+
Requires-Python: >=3.9,<4.0
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Topic :: Security
|
|
20
|
+
Project-URL: Documentation, https://swimmwatch.github.io/vk-mini-app-auth/
|
|
21
|
+
Project-URL: Repository, https://github.com/swimmwatch/vk-mini-app-auth
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
|
|
24
|
+
# vk-mini-app-auth
|
|
25
|
+
|
|
26
|
+
<!-- markdownlint-disable -->
|
|
27
|
+

|
|
28
|
+
|
|
29
|
+
<div align="center">
|
|
30
|
+
<p>
|
|
31
|
+
<a href="https://pypi.org/project/vk-mini-app-auth">
|
|
32
|
+
<img src="https://img.shields.io/pypi/v/vk-mini-app-auth.svg" alt="PyPI">
|
|
33
|
+
</a>
|
|
34
|
+
<a href="pyproject.toml">
|
|
35
|
+
<img src="https://img.shields.io/pypi/pyversions/vk-mini-app-auth" alt="Supported Python Versions">
|
|
36
|
+
</a>
|
|
37
|
+
<br/>
|
|
38
|
+
<a href="LICENSE">
|
|
39
|
+
<img src="https://img.shields.io/github/license/swimmwatch/vk-mini-app-auth" alt="License">
|
|
40
|
+
</a>
|
|
41
|
+
<a href="https://github.com/ambv/black">
|
|
42
|
+
<img src="https://img.shields.io/badge/code%20style-black-black" alt="Code style">
|
|
43
|
+
</a>
|
|
44
|
+
<a href="https://github.com/pycqa/flake8">
|
|
45
|
+
<img src="https://img.shields.io/badge/lint-flake8-black" alt="Linter">
|
|
46
|
+
</a>
|
|
47
|
+
<a href="https://github.com/python/mypy">
|
|
48
|
+
<img src="https://img.shields.io/badge/type%20checker-mypy-black" alt="Type checker">
|
|
49
|
+
</a>
|
|
50
|
+
<a href="https://snyk.io/advisor/python/vk-mini-app-auth">
|
|
51
|
+
<img src="https://snyk.io/advisor/python/vk-mini-app-auth/badge.svg" alt="Package health">
|
|
52
|
+
</a>
|
|
53
|
+
<br/>
|
|
54
|
+
<a href="https://github.com/swimmwatch/vk-mini-app-auth/actions/workflows/python-check.yml">
|
|
55
|
+
<img src="https://github.com/swimmwatch/vk-mini-app-auth/actions/workflows/python-check.yml/badge.svg" alt="Tests">
|
|
56
|
+
</a>
|
|
57
|
+
<a href="https://codecov.io/github/swimmwatch/vk-mini-app-auth" target="_blank">
|
|
58
|
+
<img src="https://codecov.io/github/swimmwatch/vk-mini-app-auth/graph/badge.svg?token=M638BMDY5V" alt="Coverage">
|
|
59
|
+
</a>
|
|
60
|
+
<a href="https://github.com/swimmwatch/vk-mini-app-auth/actions/workflows/release.yml">
|
|
61
|
+
<img src="https://github.com/swimmwatch/vk-mini-app-auth/actions/workflows/release.yml/badge.svg" alt="Release">
|
|
62
|
+
</a>
|
|
63
|
+
<a href="https://github.com/swimmwatch/vk-mini-app-auth/actions/workflows/docs.yml">
|
|
64
|
+
<img src="https://github.com/swimmwatch/vk-mini-app-auth/actions/workflows/docs.yml/badge.svg" alt="Docs">
|
|
65
|
+
</a>
|
|
66
|
+
</p>
|
|
67
|
+
</div>
|
|
68
|
+
<!-- markdownlint-enable -->
|
|
69
|
+
|
|
70
|
+
This Python package implements [VK Mini Apps authentication algorithms](https://dev.vk.com/en/mini-apps/development/launch-params-sign).
|
|
71
|
+
It is designed to be simple and easy to use, providing a straightforward way to authenticate users in VK Mini Apps.
|
|
72
|
+
|
|
73
|
+
## Features
|
|
74
|
+
- **Easy to use**: The package is designed to be simple and intuitive, making it easy to integrate into your VK Mini App.
|
|
75
|
+
- **Secure**: It implements the authentication algorithms as described in the VK Mini Apps documentation, ensuring that your app's authentication is secure.
|
|
76
|
+
- **Lightweight**: The package is small and does not have any external dependencies, making it easy to include in your project without adding unnecessary bloat.
|
|
77
|
+
- **Type hints**: The package is fully typed, providing better code completion and type checking in your IDE.
|
|
78
|
+
- **Well-documented**: The package comes with comprehensive documentation, making it easy to understand how to use it effectively.
|
|
79
|
+
- **Tested**: The package includes unit tests to ensure that it works correctly and reliably.
|
|
80
|
+
- **Supports Python 3.9+**: The package is compatible with Python 3.9 and later versions, ensuring that it works with modern Python environments.
|
|
81
|
+
- **Open source**: The package is open source and licensed under the MIT License, allowing you to use it freely in your projects.
|
|
82
|
+
|
|
83
|
+
## Requirements
|
|
84
|
+
- Python 3.9 or later
|
|
85
|
+
- No external dependencies
|
|
86
|
+
|
|
87
|
+
## Installation
|
|
88
|
+
```bash
|
|
89
|
+
pip install vk-mini-app-auth
|
|
90
|
+
# or
|
|
91
|
+
poetry add vk-mini-app-auth
|
|
92
|
+
# or
|
|
93
|
+
uv add vk-mini-app-auth
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## Documentation
|
|
97
|
+
For detailed documentation, please visit the [vk-mini-app-auth documentation](https://swimmwatch.github.io/vk-mini-app-auth/).
|
|
98
|
+
|
|
99
|
+
## License
|
|
100
|
+
`vk-mini-app-auth` is licensed under the [MIT License](LICENSE).
|
|
101
|
+
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# vk-mini-app-auth
|
|
2
|
+
|
|
3
|
+
<!-- markdownlint-disable -->
|
|
4
|
+

|
|
5
|
+
|
|
6
|
+
<div align="center">
|
|
7
|
+
<p>
|
|
8
|
+
<a href="https://pypi.org/project/vk-mini-app-auth">
|
|
9
|
+
<img src="https://img.shields.io/pypi/v/vk-mini-app-auth.svg" alt="PyPI">
|
|
10
|
+
</a>
|
|
11
|
+
<a href="pyproject.toml">
|
|
12
|
+
<img src="https://img.shields.io/pypi/pyversions/vk-mini-app-auth" alt="Supported Python Versions">
|
|
13
|
+
</a>
|
|
14
|
+
<br/>
|
|
15
|
+
<a href="LICENSE">
|
|
16
|
+
<img src="https://img.shields.io/github/license/swimmwatch/vk-mini-app-auth" alt="License">
|
|
17
|
+
</a>
|
|
18
|
+
<a href="https://github.com/ambv/black">
|
|
19
|
+
<img src="https://img.shields.io/badge/code%20style-black-black" alt="Code style">
|
|
20
|
+
</a>
|
|
21
|
+
<a href="https://github.com/pycqa/flake8">
|
|
22
|
+
<img src="https://img.shields.io/badge/lint-flake8-black" alt="Linter">
|
|
23
|
+
</a>
|
|
24
|
+
<a href="https://github.com/python/mypy">
|
|
25
|
+
<img src="https://img.shields.io/badge/type%20checker-mypy-black" alt="Type checker">
|
|
26
|
+
</a>
|
|
27
|
+
<a href="https://snyk.io/advisor/python/vk-mini-app-auth">
|
|
28
|
+
<img src="https://snyk.io/advisor/python/vk-mini-app-auth/badge.svg" alt="Package health">
|
|
29
|
+
</a>
|
|
30
|
+
<br/>
|
|
31
|
+
<a href="https://github.com/swimmwatch/vk-mini-app-auth/actions/workflows/python-check.yml">
|
|
32
|
+
<img src="https://github.com/swimmwatch/vk-mini-app-auth/actions/workflows/python-check.yml/badge.svg" alt="Tests">
|
|
33
|
+
</a>
|
|
34
|
+
<a href="https://codecov.io/github/swimmwatch/vk-mini-app-auth" target="_blank">
|
|
35
|
+
<img src="https://codecov.io/github/swimmwatch/vk-mini-app-auth/graph/badge.svg?token=M638BMDY5V" alt="Coverage">
|
|
36
|
+
</a>
|
|
37
|
+
<a href="https://github.com/swimmwatch/vk-mini-app-auth/actions/workflows/release.yml">
|
|
38
|
+
<img src="https://github.com/swimmwatch/vk-mini-app-auth/actions/workflows/release.yml/badge.svg" alt="Release">
|
|
39
|
+
</a>
|
|
40
|
+
<a href="https://github.com/swimmwatch/vk-mini-app-auth/actions/workflows/docs.yml">
|
|
41
|
+
<img src="https://github.com/swimmwatch/vk-mini-app-auth/actions/workflows/docs.yml/badge.svg" alt="Docs">
|
|
42
|
+
</a>
|
|
43
|
+
</p>
|
|
44
|
+
</div>
|
|
45
|
+
<!-- markdownlint-enable -->
|
|
46
|
+
|
|
47
|
+
This Python package implements [VK Mini Apps authentication algorithms](https://dev.vk.com/en/mini-apps/development/launch-params-sign).
|
|
48
|
+
It is designed to be simple and easy to use, providing a straightforward way to authenticate users in VK Mini Apps.
|
|
49
|
+
|
|
50
|
+
## Features
|
|
51
|
+
- **Easy to use**: The package is designed to be simple and intuitive, making it easy to integrate into your VK Mini App.
|
|
52
|
+
- **Secure**: It implements the authentication algorithms as described in the VK Mini Apps documentation, ensuring that your app's authentication is secure.
|
|
53
|
+
- **Lightweight**: The package is small and does not have any external dependencies, making it easy to include in your project without adding unnecessary bloat.
|
|
54
|
+
- **Type hints**: The package is fully typed, providing better code completion and type checking in your IDE.
|
|
55
|
+
- **Well-documented**: The package comes with comprehensive documentation, making it easy to understand how to use it effectively.
|
|
56
|
+
- **Tested**: The package includes unit tests to ensure that it works correctly and reliably.
|
|
57
|
+
- **Supports Python 3.9+**: The package is compatible with Python 3.9 and later versions, ensuring that it works with modern Python environments.
|
|
58
|
+
- **Open source**: The package is open source and licensed under the MIT License, allowing you to use it freely in your projects.
|
|
59
|
+
|
|
60
|
+
## Requirements
|
|
61
|
+
- Python 3.9 or later
|
|
62
|
+
- No external dependencies
|
|
63
|
+
|
|
64
|
+
## Installation
|
|
65
|
+
```bash
|
|
66
|
+
pip install vk-mini-app-auth
|
|
67
|
+
# or
|
|
68
|
+
poetry add vk-mini-app-auth
|
|
69
|
+
# or
|
|
70
|
+
uv add vk-mini-app-auth
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Documentation
|
|
74
|
+
For detailed documentation, please visit the [vk-mini-app-auth documentation](https://swimmwatch.github.io/vk-mini-app-auth/).
|
|
75
|
+
|
|
76
|
+
## License
|
|
77
|
+
`vk-mini-app-auth` is licensed under the [MIT License](LICENSE).
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "vk-mini-app-auth"
|
|
3
|
+
version = "1.0.1"
|
|
4
|
+
description = "Python package that implements VK Mini Apps authentication algorithms."
|
|
5
|
+
documentation = "https://swimmwatch.github.io/vk-mini-app-auth/"
|
|
6
|
+
authors = ["Dmitry Vasiliev <contact.vasiliev.dmitry@gmail.com>"]
|
|
7
|
+
license = "MIT"
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
homepage = "https://pypi.org/project/vk-mini-app-auth/"
|
|
10
|
+
repository = "https://github.com/swimmwatch/vk-mini-app-auth"
|
|
11
|
+
keywords = ["VK", "VK Mini Apps", "VK Bot", "Poetry"]
|
|
12
|
+
classifiers = [
|
|
13
|
+
"Programming Language :: Python :: 3",
|
|
14
|
+
"Programming Language :: Python :: 3.9",
|
|
15
|
+
"Programming Language :: Python :: 3.10",
|
|
16
|
+
"Programming Language :: Python :: 3.11",
|
|
17
|
+
"Programming Language :: Python :: 3.12",
|
|
18
|
+
"Programming Language :: Python :: 3.13",
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
"License :: OSI Approved :: MIT License",
|
|
21
|
+
"Operating System :: OS Independent",
|
|
22
|
+
"Topic :: Security",
|
|
23
|
+
]
|
|
24
|
+
packages = [{include = "vk_miniapp_auth"}]
|
|
25
|
+
|
|
26
|
+
[tool.poetry.dependencies]
|
|
27
|
+
python = "^3.9"
|
|
28
|
+
|
|
29
|
+
[tool.poetry.group.dev.dependencies]
|
|
30
|
+
mypy = "^1.0.1"
|
|
31
|
+
flake8 = "7.3.0"
|
|
32
|
+
black = ">=23.1,<26.0"
|
|
33
|
+
pydocstyle = "^6.3.0"
|
|
34
|
+
isort = ">=5.13.2,<7.0.0"
|
|
35
|
+
flake8-isort = "^6.1.1"
|
|
36
|
+
flake8-quotes = "^3.4.0"
|
|
37
|
+
flake8-string-format = "^0.3.0"
|
|
38
|
+
pep8-naming = ">=0.14.1,<0.16.0"
|
|
39
|
+
flake8-bandit = "^4.1.1"
|
|
40
|
+
flake8-comprehensions = "^3.15.0"
|
|
41
|
+
flake8-pyproject = "^1.2.3"
|
|
42
|
+
typing-extensions = "^4.12.2"
|
|
43
|
+
mkdocs-material = "^9.5.38"
|
|
44
|
+
mkdocstrings = {extras = ["python"], version = "^0.29.1"}
|
|
45
|
+
mkdocstrings-python = "^1.11.1"
|
|
46
|
+
mkdocs-coverage = "^1.1.0"
|
|
47
|
+
mkdocs-section-index = "^0.3.9"
|
|
48
|
+
mkdocs = "^1.6.1"
|
|
49
|
+
mkdocs-macros-plugin = "^1.2.0"
|
|
50
|
+
mkdocs-minify-plugin = "^0.8.0"
|
|
51
|
+
pytest = "^8.3.3"
|
|
52
|
+
pytest-xdist = "^3.6.1"
|
|
53
|
+
freezegun = "^1.5.1"
|
|
54
|
+
pytest-cov = "^6.0.0"
|
|
55
|
+
pytest-benchmark = "^5.1.0"
|
|
56
|
+
flake8-docstrings = "^1.7.0"
|
|
57
|
+
|
|
58
|
+
[build-system]
|
|
59
|
+
requires = ["poetry-core"]
|
|
60
|
+
build-backend = "poetry.core.masonry.api"
|
|
61
|
+
|
|
62
|
+
[tool.isort]
|
|
63
|
+
profile = "black"
|
|
64
|
+
default_section = "THIRDPARTY"
|
|
65
|
+
force_single_line = true
|
|
66
|
+
line_length = 120
|
|
67
|
+
use_parentheses = true
|
|
68
|
+
py_version=39
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"""VK Mini App Authenticator utilities."""
|
|
2
|
+
|
|
3
|
+
import base64
|
|
4
|
+
import hashlib
|
|
5
|
+
import hmac
|
|
6
|
+
import logging
|
|
7
|
+
import typing
|
|
8
|
+
from datetime import datetime
|
|
9
|
+
from datetime import timedelta
|
|
10
|
+
from datetime import timezone
|
|
11
|
+
from urllib.parse import parse_qs
|
|
12
|
+
from urllib.parse import urlencode
|
|
13
|
+
from urllib.parse import urlparse
|
|
14
|
+
|
|
15
|
+
from .data import VkLaunchParams
|
|
16
|
+
from .errors import InvalidInitDataError
|
|
17
|
+
from .types import QueryParams
|
|
18
|
+
|
|
19
|
+
logger = logging.getLogger(__name__)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class VKMiniAppAuthenticator:
|
|
23
|
+
"""VK Mini App Authenticator for validating launch parameters.
|
|
24
|
+
|
|
25
|
+
This class provides methods to extract, validate, and check the expiration of launch parameters.
|
|
26
|
+
It uses the VK application ID and secret key to verify the signature of the parameters.
|
|
27
|
+
The launch parameters are expected to be provided in the authorization header as a base64 encoded URL.
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
def __init__(self, app_id: int, app_secret: str, ttl: typing.Optional[timedelta] = None):
|
|
31
|
+
"""Initializes the VK Mini App Authenticator.
|
|
32
|
+
|
|
33
|
+
Args:
|
|
34
|
+
app_id (int): The VK application ID.
|
|
35
|
+
app_secret (str): The VK application secret key.
|
|
36
|
+
ttl (timedelta, optional): Time to live for the launch parameters. Defaults to 1 hour.
|
|
37
|
+
"""
|
|
38
|
+
self._app_id = app_id
|
|
39
|
+
self._app_secret = app_secret
|
|
40
|
+
self._ttl = ttl or timedelta(hours=1)
|
|
41
|
+
|
|
42
|
+
def get_launch_params(self, authorization_header: str) -> typing.Optional[VkLaunchParams]:
|
|
43
|
+
"""Extracts and validates launch parameters from the authorization header.
|
|
44
|
+
|
|
45
|
+
Args:
|
|
46
|
+
authorization_header (str): The authorization header containing the launch parameters.
|
|
47
|
+
|
|
48
|
+
Returns:
|
|
49
|
+
VkLaunchParams: An instance of VkLaunchParams if valid, otherwise None.
|
|
50
|
+
"""
|
|
51
|
+
query_params_url = self.extract_query_params_as_url(authorization_header)
|
|
52
|
+
query_params = self.extract_query_params_as_dict(query_params_url)
|
|
53
|
+
if not query_params:
|
|
54
|
+
return None
|
|
55
|
+
|
|
56
|
+
return VkLaunchParams(**query_params)
|
|
57
|
+
|
|
58
|
+
@staticmethod
|
|
59
|
+
def extract_query_params_as_url(authorization_header: str) -> str:
|
|
60
|
+
"""Extracts the query parameters from the authorization header.
|
|
61
|
+
|
|
62
|
+
Args:
|
|
63
|
+
authorization_header (str): The authorization header containing the base64 encoded query parameters.
|
|
64
|
+
|
|
65
|
+
Returns:
|
|
66
|
+
str: The decoded query parameters as a URL.
|
|
67
|
+
"""
|
|
68
|
+
if not authorization_header:
|
|
69
|
+
raise ValueError("Missing authorization header value")
|
|
70
|
+
|
|
71
|
+
authorization_header = authorization_header.strip()
|
|
72
|
+
|
|
73
|
+
try:
|
|
74
|
+
return base64.b64decode(authorization_header).decode("utf-8")
|
|
75
|
+
except ValueError as err:
|
|
76
|
+
logger.error("Failed to decode authorization header: %s", err)
|
|
77
|
+
raise InvalidInitDataError("Invalid authorization header format") from err
|
|
78
|
+
|
|
79
|
+
@staticmethod
|
|
80
|
+
def extract_query_params_as_dict(query_params_url: str) -> QueryParams:
|
|
81
|
+
"""Extracts query parameters from a URL and returns them as a dictionary.
|
|
82
|
+
|
|
83
|
+
Args:
|
|
84
|
+
query_params_url (str): The URL containing the query parameters.
|
|
85
|
+
|
|
86
|
+
Returns:
|
|
87
|
+
QueryParams: A dictionary containing the query parameters.
|
|
88
|
+
"""
|
|
89
|
+
query_string = urlparse(query_params_url).query
|
|
90
|
+
query_params = parse_qs(query_string, keep_blank_values=True)
|
|
91
|
+
return {k: v[0] if isinstance(v, list) else "" for k, v in query_params.items()}
|
|
92
|
+
|
|
93
|
+
def is_signed(self, launch_params: VkLaunchParams) -> bool:
|
|
94
|
+
"""Validates the signature of the launch parameters. Also checks if the parameters are not expired.
|
|
95
|
+
|
|
96
|
+
Args:
|
|
97
|
+
launch_params (VkLaunchParams): The launch parameters to validate.
|
|
98
|
+
|
|
99
|
+
Returns:
|
|
100
|
+
bool: True if the launch parameters are valid and signed correctly, False otherwise.
|
|
101
|
+
"""
|
|
102
|
+
if self._app_id != launch_params.vk_app_id:
|
|
103
|
+
logger.debug("Invalid VK app ID. Expected: %s, got: %s", self._app_id, launch_params.vk_app_id)
|
|
104
|
+
return False
|
|
105
|
+
|
|
106
|
+
vk_params = {k: v for k, v in launch_params.get_data().items() if k.startswith("vk_")}
|
|
107
|
+
sorted_vk_params = dict(sorted(vk_params.items()))
|
|
108
|
+
|
|
109
|
+
sign_params_query = urlencode(sorted_vk_params)
|
|
110
|
+
sign = (
|
|
111
|
+
base64.urlsafe_b64encode(
|
|
112
|
+
hmac.new(
|
|
113
|
+
self._app_secret.encode(),
|
|
114
|
+
sign_params_query.encode(),
|
|
115
|
+
hashlib.sha256,
|
|
116
|
+
).digest()
|
|
117
|
+
)
|
|
118
|
+
.decode()
|
|
119
|
+
.rstrip("=")
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
return sign == launch_params.sign and not self.is_expired(launch_params)
|
|
123
|
+
|
|
124
|
+
def is_expired(self, launch_params: VkLaunchParams) -> bool:
|
|
125
|
+
"""Checks if the launch parameters are expired based on the TTL.
|
|
126
|
+
|
|
127
|
+
Args:
|
|
128
|
+
launch_params (VkLaunchParams): The launch parameters to check.
|
|
129
|
+
|
|
130
|
+
Returns:
|
|
131
|
+
bool: True if the launch parameters are expired, False otherwise.
|
|
132
|
+
"""
|
|
133
|
+
now = datetime.now(timezone.utc)
|
|
134
|
+
return (now - launch_params.vk_ts) > self._ttl
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import dataclasses
|
|
2
|
+
import enum
|
|
3
|
+
import typing
|
|
4
|
+
from datetime import datetime
|
|
5
|
+
from datetime import timezone
|
|
6
|
+
|
|
7
|
+
from .errors import InvalidInitDataError
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclasses.dataclass
|
|
11
|
+
class VkLaunchParams:
|
|
12
|
+
"""Represents passed launch parameters from VK.
|
|
13
|
+
|
|
14
|
+
Links:
|
|
15
|
+
https://dev.vk.com/en/mini-apps/development/launch-params
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
sign: str
|
|
19
|
+
vk_access_token_settings: typing.List[str]
|
|
20
|
+
vk_app_id: int
|
|
21
|
+
vk_are_notifications_enabled: bool
|
|
22
|
+
vk_is_app_user: bool
|
|
23
|
+
vk_is_favorite: bool
|
|
24
|
+
vk_language: "LanguageEnum"
|
|
25
|
+
vk_platform: "PlatformEnum"
|
|
26
|
+
vk_ts: datetime
|
|
27
|
+
vk_user_id: int
|
|
28
|
+
vk_chat_id: typing.Optional[str] = None
|
|
29
|
+
vk_group_id: typing.Optional[int] = None
|
|
30
|
+
vk_has_profile_button: typing.Optional[bool] = None
|
|
31
|
+
vk_is_play_machine: typing.Optional[bool] = None
|
|
32
|
+
vk_is_recommended: typing.Optional[bool] = None
|
|
33
|
+
vk_is_widescreen: typing.Optional[bool] = None
|
|
34
|
+
vk_profile_id: typing.Optional[int] = None
|
|
35
|
+
vk_request_key: typing.Optional[str] = None
|
|
36
|
+
vk_testing_group_id: typing.Optional[int] = None
|
|
37
|
+
vk_ref: typing.Optional[str] = None
|
|
38
|
+
vk_viewer_group_role: typing.Optional["ViewerGroupRoleEnum"] = None
|
|
39
|
+
|
|
40
|
+
def __init__(self, **kwargs: typing.Any):
|
|
41
|
+
"""Initialize the VkLaunchParams with keyword arguments."""
|
|
42
|
+
self._data = kwargs
|
|
43
|
+
|
|
44
|
+
for key, value in kwargs.items():
|
|
45
|
+
setattr(self, key, value)
|
|
46
|
+
|
|
47
|
+
self.__post_init__()
|
|
48
|
+
|
|
49
|
+
def __post_init__(self):
|
|
50
|
+
"""Post-initialization processing to convert types and validate data."""
|
|
51
|
+
try:
|
|
52
|
+
self.vk_app_id = int(self.vk_app_id)
|
|
53
|
+
self.vk_user_id = int(self.vk_user_id)
|
|
54
|
+
self.vk_access_token_settings = self.vk_access_token_settings.split(",") # type: ignore[attr-defined]
|
|
55
|
+
self.vk_ts = datetime.fromtimestamp(float(self.vk_ts), timezone.utc) # type: ignore[arg-type]
|
|
56
|
+
self.vk_are_notifications_enabled = bool(int(self.vk_are_notifications_enabled))
|
|
57
|
+
self.vk_is_app_user = bool(int(self.vk_is_app_user))
|
|
58
|
+
self.vk_is_favorite = bool(int(self.vk_is_favorite))
|
|
59
|
+
self.vk_language = LanguageEnum(self.vk_language)
|
|
60
|
+
self.vk_platform = PlatformEnum(self.vk_platform)
|
|
61
|
+
|
|
62
|
+
if self.vk_has_profile_button is not None:
|
|
63
|
+
self.vk_has_profile_button = bool(int(self.vk_has_profile_button))
|
|
64
|
+
|
|
65
|
+
if self.vk_is_play_machine is not None:
|
|
66
|
+
self.vk_is_play_machine = bool(int(self.vk_is_play_machine))
|
|
67
|
+
|
|
68
|
+
if self.vk_is_recommended is not None:
|
|
69
|
+
self.vk_is_recommended = bool(int(self.vk_is_recommended))
|
|
70
|
+
|
|
71
|
+
if self.vk_is_widescreen is not None:
|
|
72
|
+
self.vk_is_widescreen = bool(int(self.vk_is_widescreen))
|
|
73
|
+
|
|
74
|
+
if self.vk_viewer_group_role is not None:
|
|
75
|
+
self.vk_viewer_group_role = ViewerGroupRoleEnum(self.vk_viewer_group_role)
|
|
76
|
+
except ValueError as err:
|
|
77
|
+
raise InvalidInitDataError("Invalid launch parameters") from err
|
|
78
|
+
|
|
79
|
+
def get_data(self) -> typing.Dict[str, typing.Any]:
|
|
80
|
+
"""Return the launch parameters as a dictionary."""
|
|
81
|
+
return self._data
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
class ViewerGroupRoleEnum(str, enum.Enum):
|
|
85
|
+
"""Represents the group role of the viewer in VK launch parameters."""
|
|
86
|
+
|
|
87
|
+
NONE = "none"
|
|
88
|
+
ADMIN = "admin"
|
|
89
|
+
EDITOR = "editor"
|
|
90
|
+
MEMBER = "member"
|
|
91
|
+
MODERATOR = "moder"
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
class PlatformEnum(str, enum.Enum):
|
|
95
|
+
"""Represents the platform codes used in VK launch parameters."""
|
|
96
|
+
|
|
97
|
+
# The platform on which the application was launched:
|
|
98
|
+
DESKTOP_WEB = "desktop_web"
|
|
99
|
+
MOBILE_WEB = "mobile_web"
|
|
100
|
+
MOBILE_ANDROID = "mobile_android"
|
|
101
|
+
MOBILE_IPAD = "mobile_ipad"
|
|
102
|
+
MOBILE_IPHONE = "mobile_iphone"
|
|
103
|
+
|
|
104
|
+
# If the mini app was launched from VK Messenger:
|
|
105
|
+
DESKTOP_APP_MESSENGER = "desktop_app_messenger"
|
|
106
|
+
DESKTOP_WEB_MESSENGER = "desktop_web_messenger"
|
|
107
|
+
MOBILE_ANDROID_MESSENGER = "mobile_android_messenger"
|
|
108
|
+
MOBILE_IPHONE_MESSENGER = "mobile_iphone_messenger"
|
|
109
|
+
|
|
110
|
+
# If the mini app was launched from outside VK or VK Messenger
|
|
111
|
+
ANDROID_EXTERNAL = "android_external"
|
|
112
|
+
IPHONE_EXTERNAL = "iphone_external"
|
|
113
|
+
IPAD_EXTERNAL = "ipad_external"
|
|
114
|
+
MVK_EXTERNAL = "mvk_external"
|
|
115
|
+
WEB_EXTERNAL = "web_external"
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
class LanguageEnum(str, enum.Enum):
|
|
119
|
+
"""Represents the language codes used in VK launch parameters."""
|
|
120
|
+
|
|
121
|
+
RU = "ru"
|
|
122
|
+
UK = "uk"
|
|
123
|
+
UA = "ua"
|
|
124
|
+
BE = "be"
|
|
125
|
+
KZ = "kz"
|
|
126
|
+
PT = "pt"
|
|
127
|
+
ES = "es"
|
|
128
|
+
EN = "en"
|