init-data-py 0.2.2__tar.gz → 0.2.5__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.
Files changed (22) hide show
  1. {init_data_py-0.2.2 → init_data_py-0.2.5}/PKG-INFO +2 -3
  2. {init_data_py-0.2.2 → init_data_py-0.2.5}/pyproject.toml +1 -1
  3. {init_data_py-0.2.2 → init_data_py-0.2.5}/src/init_data_py/__init__.py +1 -1
  4. {init_data_py-0.2.2 → init_data_py-0.2.5}/src/init_data_py/errors/errors.py +9 -5
  5. {init_data_py-0.2.2 → init_data_py-0.2.5}/src/init_data_py/init_data.py +8 -6
  6. {init_data_py-0.2.2 → init_data_py-0.2.5}/tests/test_validate.py +6 -0
  7. {init_data_py-0.2.2 → init_data_py-0.2.5}/.github/workflows/publish.yml +0 -0
  8. {init_data_py-0.2.2 → init_data_py-0.2.5}/.gitignore +0 -0
  9. {init_data_py-0.2.2 → init_data_py-0.2.5}/.python-version +0 -0
  10. {init_data_py-0.2.2 → init_data_py-0.2.5}/LICENCE +0 -0
  11. {init_data_py-0.2.2 → init_data_py-0.2.5}/README.md +0 -0
  12. {init_data_py-0.2.2 → init_data_py-0.2.5}/requirements-dev.lock +0 -0
  13. {init_data_py-0.2.2 → init_data_py-0.2.5}/requirements.lock +0 -0
  14. {init_data_py-0.2.2 → init_data_py-0.2.5}/src/init_data_py/errors/__init__.py +0 -0
  15. {init_data_py-0.2.2 → init_data_py-0.2.5}/src/init_data_py/types/__init__.py +0 -0
  16. {init_data_py-0.2.2 → init_data_py-0.2.5}/src/init_data_py/types/chat.py +0 -0
  17. {init_data_py-0.2.2 → init_data_py-0.2.5}/src/init_data_py/types/object.py +0 -0
  18. {init_data_py-0.2.2 → init_data_py-0.2.5}/src/init_data_py/types/user.py +0 -0
  19. {init_data_py-0.2.2 → init_data_py-0.2.5}/tests/__init__.py +0 -0
  20. {init_data_py-0.2.2 → init_data_py-0.2.5}/tests/test_parse.py +0 -0
  21. {init_data_py-0.2.2 → init_data_py-0.2.5}/tests/test_sign.py +0 -0
  22. {init_data_py-0.2.2 → init_data_py-0.2.5}/tests/test_to_query_string.py +0 -0
@@ -1,10 +1,9 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: init-data-py
3
- Version: 0.2.2
3
+ Version: 0.2.5
4
4
  Summary: A Python library that provides tools for using and validating Telegram web app init data.
5
5
  Author-email: nimaxin <nimaxin@proton.me>
6
- License-Expression: MIT
7
- License-File: LICENCE
6
+ License: MIT
8
7
  Requires-Python: >=3.8
9
8
  Description-Content-Type: text/markdown
10
9
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "init-data-py"
3
- version = "0.2.2"
3
+ version = "0.2.5"
4
4
  description = "A Python library that provides tools for using and validating Telegram web app init data."
5
5
  authors = [
6
6
  { name = "nimaxin", email = "nimaxin@proton.me" }
@@ -1,5 +1,5 @@
1
1
  from .init_data import InitData
2
2
 
3
- __version__ = "0.2.2"
3
+ __version__ = "0.2.5"
4
4
 
5
5
  __all__ = ["InitData"]
@@ -1,23 +1,27 @@
1
- class AuthDateMissingError(Exception):
1
+ class InitDataPyError(Exception):
2
+ pass
3
+
4
+
5
+ class AuthDateMissingError(InitDataPyError):
2
6
  def __init__(self):
3
7
  super().__init__("auth_date is missing.")
4
8
 
5
9
 
6
- class ExpiredError(Exception):
10
+ class ExpiredError(InitDataPyError):
7
11
  def __init__(self):
8
12
  super().__init__("init data is expired.")
9
13
 
10
14
 
11
- class SignInvalidError(Exception):
15
+ class SignInvalidError(InitDataPyError):
12
16
  def __init__(self):
13
17
  super().__init__("signature (hash) is invalid.")
14
18
 
15
19
 
16
- class SignMissingError(Exception):
20
+ class SignMissingError(InitDataPyError):
17
21
  def __init__(self):
18
22
  super().__init__("signature (hash) is missing.")
19
23
 
20
24
 
21
- class UnexpectedFormatError(Exception):
25
+ class UnexpectedFormatError(InitDataPyError):
22
26
  def __init__(self):
23
27
  super().__init__("the init data query string has unexpected format.")
@@ -140,9 +140,9 @@ class InitData:
140
140
  `InitData`:
141
141
  This current updated init data by setting the `auth_date` and generated signature (`hash`) attributes.
142
142
  """
143
- if auth_date is None:
144
- self.auth_date = int(time.time())
145
- self.auth_date = auth_date
143
+ self.auth_date = (
144
+ auth_date if auth_date is not None else int(time.time())
145
+ )
146
146
  self.hash = self.calculate_hash(bot_token)
147
147
 
148
148
  return self
@@ -164,9 +164,11 @@ class InitData:
164
164
  init_data = self.to_dict(nested=False)
165
165
  init_data.pop("hash", None)
166
166
  sorted_attrs = sorted(init_data.items())
167
- data_check_string = "\n".join(
168
- f"{k}={v}" for k, v in sorted_attrs
169
- ).encode()
167
+ data_check_string = (
168
+ "\n".join(f"{k}={v}" for k, v in sorted_attrs)
169
+ .replace("/", "\/")
170
+ .encode()
171
+ )
170
172
  secret_key = hmac.new(
171
173
  b"WebAppData", bot_token.encode(), hashlib.sha256
172
174
  ).digest()
@@ -29,6 +29,12 @@ class TestValidateInitData(unittest.TestCase):
29
29
  init_data = InitData.parse(query_string)
30
30
  self.assertTrue(init_data.validate(bot_token))
31
31
 
32
+ def test_escape_character(self):
33
+ query_string = "query_id=AAF03wc0AgAAAHTfBzTtHPDB&user=%7B%22id%22%3A5167898484%2C%22first_name%22%3A%22xin%22%2C%22last_name%22%3A%22%22%2C%22username%22%3A%22pvnimaxin%22%2C%22language_code%22%3A%22en%22%2C%22allows_write_to_pm%22%3Atrue%2C%22photo_url%22%3A%22https%3A%5C%2F%5C%2Ft.me%5C%2Fi%5C%2Fuserpic%5C%2F320%5C%2FYpcdHFmoxukmQ537mOZhe-Woot_k2xrmbdAIrGK1zFgIVth6Wzacz7P2nGNCcp9j.svg%22%7D&auth_date=1731441609&hash=f4bac82fe8f20abdc03126af489751752e830227b58241ec2f9dd67913909ee0"
34
+ bot_token = "7244657541:AAFMjYH4kc3U9zG0GWnxYfW-QKICVzDwvEw"
35
+ init_data = InitData.parse(query_string)
36
+ self.assertTrue(init_data.validate(bot_token))
37
+
32
38
  def test_valid_init_data(self):
33
39
  self.assertTrue(self.init_data.validate(self.bot_token))
34
40
 
File without changes
File without changes
File without changes