init-data-py 0.2.4__tar.gz → 0.2.6__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.4 → init_data_py-0.2.6}/PKG-INFO +2 -3
  2. {init_data_py-0.2.4 → init_data_py-0.2.6}/pyproject.toml +1 -1
  3. {init_data_py-0.2.4 → init_data_py-0.2.6}/src/init_data_py/__init__.py +1 -1
  4. {init_data_py-0.2.4 → init_data_py-0.2.6}/src/init_data_py/init_data.py +14 -4
  5. {init_data_py-0.2.4 → init_data_py-0.2.6}/tests/test_validate.py +6 -0
  6. {init_data_py-0.2.4 → init_data_py-0.2.6}/.github/workflows/publish.yml +0 -0
  7. {init_data_py-0.2.4 → init_data_py-0.2.6}/.gitignore +0 -0
  8. {init_data_py-0.2.4 → init_data_py-0.2.6}/.python-version +0 -0
  9. {init_data_py-0.2.4 → init_data_py-0.2.6}/LICENCE +0 -0
  10. {init_data_py-0.2.4 → init_data_py-0.2.6}/README.md +0 -0
  11. {init_data_py-0.2.4 → init_data_py-0.2.6}/requirements-dev.lock +0 -0
  12. {init_data_py-0.2.4 → init_data_py-0.2.6}/requirements.lock +0 -0
  13. {init_data_py-0.2.4 → init_data_py-0.2.6}/src/init_data_py/errors/__init__.py +0 -0
  14. {init_data_py-0.2.4 → init_data_py-0.2.6}/src/init_data_py/errors/errors.py +0 -0
  15. {init_data_py-0.2.4 → init_data_py-0.2.6}/src/init_data_py/types/__init__.py +0 -0
  16. {init_data_py-0.2.4 → init_data_py-0.2.6}/src/init_data_py/types/chat.py +0 -0
  17. {init_data_py-0.2.4 → init_data_py-0.2.6}/src/init_data_py/types/object.py +0 -0
  18. {init_data_py-0.2.4 → init_data_py-0.2.6}/src/init_data_py/types/user.py +0 -0
  19. {init_data_py-0.2.4 → init_data_py-0.2.6}/tests/__init__.py +0 -0
  20. {init_data_py-0.2.4 → init_data_py-0.2.6}/tests/test_parse.py +0 -0
  21. {init_data_py-0.2.4 → init_data_py-0.2.6}/tests/test_sign.py +0 -0
  22. {init_data_py-0.2.4 → init_data_py-0.2.6}/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.4
3
+ Version: 0.2.6
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.4"
3
+ version = "0.2.6"
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.4"
3
+ __version__ = "0.2.6"
4
4
 
5
5
  __all__ = ["InitData"]
@@ -43,6 +43,9 @@ class InitData:
43
43
 
44
44
  hash (`str`, optional):
45
45
  A hash of all passed parameters, which the bot server can use to check their validity.
46
+
47
+ signature (`str`, optional):
48
+ A signature of all passed parameters (except hash), which the third party can use to check their validity.
46
49
  """
47
50
 
48
51
  def __init__(
@@ -60,6 +63,7 @@ class InitData:
60
63
  can_send_after: Optional[int] = None,
61
64
  auth_date: Optional[int] = None,
62
65
  hash: Optional[str] = None,
66
+ signature: Optional[str] = None,
63
67
  ) -> None:
64
68
  self.query_id = query_id
65
69
  self.user = user
@@ -71,6 +75,7 @@ class InitData:
71
75
  self.can_send_after = can_send_after
72
76
  self.auth_date = auth_date
73
77
  self.hash = hash
78
+ self.signature = signature
74
79
 
75
80
  def validate(
76
81
  self,
@@ -140,7 +145,9 @@ class InitData:
140
145
  `InitData`:
141
146
  This current updated init data by setting the `auth_date` and generated signature (`hash`) attributes.
142
147
  """
143
- self.auth_date = auth_date if auth_date is not None else int(time.time())
148
+ self.auth_date = (
149
+ auth_date if auth_date is not None else int(time.time())
150
+ )
144
151
  self.hash = self.calculate_hash(bot_token)
145
152
 
146
153
  return self
@@ -162,9 +169,11 @@ class InitData:
162
169
  init_data = self.to_dict(nested=False)
163
170
  init_data.pop("hash", None)
164
171
  sorted_attrs = sorted(init_data.items())
165
- data_check_string = "\n".join(
166
- f"{k}={v}" for k, v in sorted_attrs
167
- ).encode()
172
+ data_check_string = (
173
+ "\n".join(f"{k}={v}" for k, v in sorted_attrs)
174
+ .replace("/", "\/")
175
+ .encode()
176
+ )
168
177
  secret_key = hmac.new(
169
178
  b"WebAppData", bot_token.encode(), hashlib.sha256
170
179
  ).digest()
@@ -210,6 +219,7 @@ class InitData:
210
219
  for k, v in parsed_qs.items():
211
220
  if k in {
212
221
  "hash",
222
+ "signature",
213
223
  "query_id",
214
224
  "chat_type",
215
225
  "chat_instance",
@@ -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