python-siseli 0.1.2__py3-none-any.whl → 0.2.1__py3-none-any.whl

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.
@@ -1,11 +1,12 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-siseli
3
- Version: 0.1.2
3
+ Version: 0.2.1
4
4
  Summary: Python SDK for Siseli Cloud
5
5
  License: MIT
6
6
  Requires-Python: >=3.11
7
7
  Description-Content-Type: text/markdown
8
8
  Requires-Dist: httpx>=0.27
9
+ Requires-Dist: cryptography>=42
9
10
  Provides-Extra: dev
10
11
  Requires-Dist: pytest>=8; extra == "dev"
11
12
  Requires-Dist: pytest-asyncio>=0.23; extra == "dev"
@@ -1,15 +1,16 @@
1
- siseli/__init__.py,sha256=_sHO0M0medXnJz_ZTtHKyy8DP6e40wuf5ubEGLC1kBs,1035
1
+ siseli/__init__.py,sha256=F4eW-gk3BGfcBhVgDCyqotYgCcibac6iSEgyp_px8Ug,1099
2
2
  siseli/_utils.py,sha256=QT-ooemKKAIEHICNI2QErPvRkFIkUGo6ncUFCJM1kGc,727
3
3
  siseli/alarms.py,sha256=Mc_CV8kUBFTXu8c48NstX8Lc6Q0bPTnhjwehx9sbCkQ,4853
4
4
  siseli/auth.py,sha256=I8WwJgy1go1MGxjowkVx-lvCtneh5rRtbs9DV9yO4T8,6470
5
5
  siseli/client.py,sha256=Eap7MTrnctdskt6kOzbns7SqUt27ZDjMlIKVshfcfN8,14200
6
6
  siseli/config.py,sha256=J8raq7Yf4gCyjO9ISNoTrk_EYNEGrESV56O-aHfDvBU,3278
7
- siseli/const.py,sha256=M8bBrc8cH0OyEGb9158_emux_dfknvikGQqm09iQ8CA,174
7
+ siseli/const.py,sha256=9qk2YEaDELKmn7xRsvxlnHh9nUGFymN1DWnulfQG4w4,364
8
8
  siseli/dashboard.py,sha256=JQHfi8orKs5uWsGGfoTvA14A9M_R9yIAYuGCxb4zTsM,3325
9
9
  siseli/device.py,sha256=crix14pYakrDaFmomPC3Uwgdq-1boZ-ZaEg2HgzXibs,2762
10
10
  siseli/dictionary.py,sha256=VSO83uXPaGLPzKGRmpROuWQKivXN7DNOmXN8QQJ-Y-w,1120
11
11
  siseli/exceptions.py,sha256=y9ws9c2vr5Q3bZxLwkXybUqQooq57YEEXAN0wO3_fxk,1420
12
12
  siseli/history.py,sha256=CgBvDfZBIrrewu7PeRv08-Zr4boEVOY8Xevruu-ge-I,3869
13
+ siseli/open_auth.py,sha256=w3Jq7ZA2m3d-A_ZR_O8G1bODHvcUJ4Rc1eOS4uq69l0,5570
13
14
  siseli/state.py,sha256=d7Exlf7FzF8O_CcF-NbAt6kq8qaR4fFhjGW8028kbrM,5800
14
15
  siseli/station.py,sha256=e1OK4OIry9WUjT8KggQI5b4Acypm9D31mWAMTjDGFew,5531
15
16
  siseli/models/__init__.py,sha256=WSIXRM_JRKNhB_hDwxrpeQgdqKUHdbT3KiFvdrLrrzY,1242
@@ -23,7 +24,7 @@ siseli/models/dictionary.py,sha256=d6KCu77UMlPcIDON8G3sjuytkSfbI5go1y3I_jAGTX8,4
23
24
  siseli/models/history.py,sha256=dx26VWLJosucMvwL36vojvAguvkMiv5GEKTllUtZV0Y,791
24
25
  siseli/models/state.py,sha256=VK5CNqMLRkY1w9HrcUhvaO_6J-rjqP7VwvbppCFpucI,2393
25
26
  siseli/models/station.py,sha256=02OLINAVblnKkF73ZwKxYvPtUFROsu4-g544oDg8ApA,1783
26
- python_siseli-0.1.2.dist-info/METADATA,sha256=xnZJLpywI8nzDQEqwUgUsN9Pbx5iL7bnSqV3v326MFs,1407
27
- python_siseli-0.1.2.dist-info/WHEEL,sha256=K260EYznzXsJYBQGqmI8VTxEdiZYNvDZwW9cBh9-_MA,91
28
- python_siseli-0.1.2.dist-info/top_level.txt,sha256=Dofr9c7hGvRRV6ij6aoPuAZqxEjkgtsBeN4KtFHbGgo,7
29
- python_siseli-0.1.2.dist-info/RECORD,,
27
+ python_siseli-0.2.1.dist-info/METADATA,sha256=wzl4nryFI-fsKCerJ7V60RUJggiQTAqX1DAwAnAfba8,1439
28
+ python_siseli-0.2.1.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
29
+ python_siseli-0.2.1.dist-info/top_level.txt,sha256=Dofr9c7hGvRRV6ij6aoPuAZqxEjkgtsBeN4KtFHbGgo,7
30
+ python_siseli-0.2.1.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (83.0.0)
2
+ Generator: setuptools (84.0.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
siseli/__init__.py CHANGED
@@ -1,6 +1,7 @@
1
1
  """python-siseli — Python SDK for Siseli Cloud."""
2
2
 
3
3
  from .client import SiseliClient
4
+ from .open_auth import attach_open_auth
4
5
  from .exceptions import ApiError, AuthenticationError, NetworkError, SiseliError, TokenExpiredError
5
6
  from .models import (
6
7
  Alarm,
@@ -25,6 +26,7 @@ from .models import (
25
26
 
26
27
  __all__ = [
27
28
  "SiseliClient",
29
+ "attach_open_auth",
28
30
  "SiseliError",
29
31
  "AuthenticationError",
30
32
  "TokenExpiredError",
siseli/const.py CHANGED
@@ -3,3 +3,7 @@ DEFAULT_TIMEOUT = 30.0
3
3
  DEFAULT_PAGE_SIZE = 20
4
4
  # dataSource=1 observed in all telemetry requests in HAR captures
5
5
  DEFAULT_DATA_SOURCE = 1
6
+
7
+ # Production Siseli Open API application credentials from the official web client.
8
+ SISELI_APP_ID = "rBrTRfAPXz"
9
+ SISELI_APP_SECRET_ENCRYPTED = "I4D0KRr2339z3pQ/at91V9BpFAOe54DaTafwSm6suIQ="
siseli/open_auth.py ADDED
@@ -0,0 +1,168 @@
1
+ """Siseli Open API request signing helpers.
2
+
3
+ Every API request must carry IOT-Open-* headers that prove the caller holds a
4
+ valid application credential. The signing algorithm mirrors the official web
5
+ client (CryptoJS-based) exactly:
6
+
7
+ 1. Decrypt the base64-encoded app secret with AES-128-CBC.
8
+ Key and IV are derived from MD5(app_id) as ASCII hex chars.
9
+ 2. Compute a body hash: empty string for GET; SHA-256 hex for everything else.
10
+ 3. Sign input: ``IOT-Open-AppID``, ``IOT-Open-Body-Hash`` and
11
+ ``IOT-Open-Nonce`` only, sorted by key, joined as ``k=v&k=v`` without
12
+ URL-encoding, then base64-encoded (UTF-8).
13
+ 4. Sign: HMAC-SHA256(base64_string, secret) raw digest → MD5 → lowercase hex.
14
+ 5. The body hash is part of the signature but is NOT sent as an HTTP header.
15
+ """
16
+
17
+ from __future__ import annotations
18
+
19
+ import base64
20
+ import hashlib
21
+ import hmac
22
+ import uuid
23
+ from typing import TYPE_CHECKING, Any
24
+
25
+ from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
26
+
27
+ from .const import SISELI_APP_ID, SISELI_APP_SECRET_ENCRYPTED
28
+
29
+ if TYPE_CHECKING:
30
+ import httpx
31
+
32
+
33
+ def decrypt_open_secret(app_id: str, encrypted_secret_b64: str) -> str:
34
+ """Decrypt the base64-encoded app secret using AES-128-CBC.
35
+
36
+ The AES key and IV are derived from MD5(app_id).lower():
37
+ - first 16 hex chars → 16 ASCII bytes → AES key
38
+ - last 16 hex chars → 16 ASCII bytes → IV
39
+
40
+ ZeroPadding is assumed: trailing null bytes are stripped from the output.
41
+ """
42
+ md5_hex = hashlib.md5(app_id.encode()).hexdigest() # noqa: S324
43
+ key = md5_hex[:16].encode("ascii")
44
+ iv = md5_hex[16:].encode("ascii")
45
+
46
+ encrypted_bytes = base64.b64decode(encrypted_secret_b64)
47
+ cipher = Cipher(algorithms.AES(key), modes.CBC(iv))
48
+ decryptor = cipher.decryptor()
49
+ plaintext = decryptor.update(encrypted_bytes) + decryptor.finalize()
50
+ return plaintext.rstrip(b"\x00").decode("utf-8")
51
+
52
+
53
+ def _sha256_hex(data: bytes) -> str:
54
+ """Return the lowercase hex SHA-256 digest of *data*."""
55
+ return hashlib.sha256(data).hexdigest()
56
+
57
+
58
+ def build_open_headers(
59
+ *,
60
+ method: str,
61
+ query: str,
62
+ body: bytes | None,
63
+ app_id: str,
64
+ app_secret: str,
65
+ timezone: str,
66
+ nonce: str,
67
+ ) -> dict[str, str]:
68
+ """Build the IOT-Open-* headers (and IOT-Time-Zone) for a single request.
69
+
70
+ Parameters
71
+ ----------
72
+ method:
73
+ HTTP method ("GET", "POST", …).
74
+ query:
75
+ The raw query string portion of the URL. Accepted for API
76
+ compatibility; it does not take part in the signature.
77
+ body:
78
+ Raw request body bytes, or *None* / empty for GET requests.
79
+ app_id:
80
+ The application ID (``IOT-Open-AppID``).
81
+ app_secret:
82
+ The *decrypted* application secret used as the HMAC key.
83
+ timezone:
84
+ The client timezone string, e.g. "UTC" or "Europe/Moscow".
85
+ nonce:
86
+ A unique string per request (UUID hex recommended).
87
+ """
88
+ # --- body hash -----------------------------------------------------------
89
+ if method.upper() == "GET":
90
+ body_hash = ""
91
+ else:
92
+ body_hash = _sha256_hex(body if body else b"")
93
+
94
+ # --- sign input: exactly three params, sorted, no URL-encoding -----------
95
+ params = {
96
+ "IOT-Open-AppID": app_id,
97
+ "IOT-Open-Body-Hash": body_hash,
98
+ "IOT-Open-Nonce": nonce,
99
+ }
100
+ signing_string = "&".join(f"{k}={v}" for k, v in sorted(params.items()))
101
+
102
+ # --- sign: base64 -> HMAC-SHA256 (raw digest) -> MD5 hex -----------------
103
+ base64_input = base64.b64encode(signing_string.encode("utf-8")).decode("ascii")
104
+ hmac_digest = hmac.new(
105
+ app_secret.encode("utf-8"),
106
+ base64_input.encode("ascii"),
107
+ hashlib.sha256,
108
+ ).digest()
109
+ sign = hashlib.md5(hmac_digest).hexdigest() # noqa: S324
110
+
111
+ # The body hash takes part in the signature but is not sent as a header.
112
+ return {
113
+ "IOT-Time-Zone": timezone,
114
+ "IOT-Open-AppID": app_id,
115
+ "IOT-Open-Nonce": nonce,
116
+ "IOT-Open-Sign": sign,
117
+ }
118
+
119
+
120
+ def attach_open_auth(
121
+ client: Any,
122
+ *,
123
+ app_id: str = SISELI_APP_ID,
124
+ encrypted_secret: str = SISELI_APP_SECRET_ENCRYPTED,
125
+ ) -> None:
126
+ """Attach an httpx request hook that adds Open API signing headers.
127
+
128
+ The hook is appended to ``client._http.event_hooks["request"]`` and runs
129
+ before every outgoing request, injecting:
130
+
131
+ - ``IOT-Time-Zone``
132
+ - ``IOT-Open-AppID``
133
+ - ``IOT-Open-Nonce``
134
+ - ``IOT-Open-Sign``
135
+
136
+ ``IOT-Open-Body-Hash`` is part of the signature but is not sent.
137
+
138
+ Parameters
139
+ ----------
140
+ client:
141
+ A :class:`~siseli.SiseliClient` instance (or any object with
142
+ ``._http.event_hooks`` and ``._timezone``).
143
+ app_id:
144
+ Override the default production App ID.
145
+ encrypted_secret:
146
+ Override the default production encrypted secret.
147
+ """
148
+ secret = decrypt_open_secret(app_id, encrypted_secret)
149
+ timezone = getattr(client, "_timezone", "UTC")
150
+
151
+ async def _sign_request(request: "httpx.Request") -> None:
152
+ nonce = uuid.uuid4().hex
153
+ query = request.url.query
154
+ if isinstance(query, bytes):
155
+ query = query.decode("utf-8")
156
+ body = request.content if request.method.upper() != "GET" else None
157
+ headers = build_open_headers(
158
+ method=request.method,
159
+ query=query,
160
+ body=body,
161
+ app_id=app_id,
162
+ app_secret=secret,
163
+ timezone=timezone,
164
+ nonce=nonce,
165
+ )
166
+ request.headers.update(headers)
167
+
168
+ client._http.event_hooks["request"].append(_sign_request)