python-siseli 0.1.2__tar.gz → 0.2.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.
Files changed (39) hide show
  1. {python_siseli-0.1.2 → python_siseli-0.2.1}/PKG-INFO +2 -1
  2. {python_siseli-0.1.2 → python_siseli-0.2.1}/pyproject.toml +2 -1
  3. {python_siseli-0.1.2 → python_siseli-0.2.1}/python_siseli.egg-info/PKG-INFO +2 -1
  4. {python_siseli-0.1.2 → python_siseli-0.2.1}/python_siseli.egg-info/SOURCES.txt +2 -0
  5. {python_siseli-0.1.2 → python_siseli-0.2.1}/python_siseli.egg-info/requires.txt +1 -0
  6. {python_siseli-0.1.2 → python_siseli-0.2.1}/siseli/__init__.py +2 -0
  7. python_siseli-0.2.1/siseli/const.py +9 -0
  8. python_siseli-0.2.1/siseli/open_auth.py +168 -0
  9. python_siseli-0.2.1/tests/test_open_auth.py +166 -0
  10. python_siseli-0.1.2/siseli/const.py +0 -5
  11. {python_siseli-0.1.2 → python_siseli-0.2.1}/README.md +0 -0
  12. {python_siseli-0.1.2 → python_siseli-0.2.1}/python_siseli.egg-info/dependency_links.txt +0 -0
  13. {python_siseli-0.1.2 → python_siseli-0.2.1}/python_siseli.egg-info/top_level.txt +0 -0
  14. {python_siseli-0.1.2 → python_siseli-0.2.1}/setup.cfg +0 -0
  15. {python_siseli-0.1.2 → python_siseli-0.2.1}/siseli/_utils.py +0 -0
  16. {python_siseli-0.1.2 → python_siseli-0.2.1}/siseli/alarms.py +0 -0
  17. {python_siseli-0.1.2 → python_siseli-0.2.1}/siseli/auth.py +0 -0
  18. {python_siseli-0.1.2 → python_siseli-0.2.1}/siseli/client.py +0 -0
  19. {python_siseli-0.1.2 → python_siseli-0.2.1}/siseli/config.py +0 -0
  20. {python_siseli-0.1.2 → python_siseli-0.2.1}/siseli/dashboard.py +0 -0
  21. {python_siseli-0.1.2 → python_siseli-0.2.1}/siseli/device.py +0 -0
  22. {python_siseli-0.1.2 → python_siseli-0.2.1}/siseli/dictionary.py +0 -0
  23. {python_siseli-0.1.2 → python_siseli-0.2.1}/siseli/exceptions.py +0 -0
  24. {python_siseli-0.1.2 → python_siseli-0.2.1}/siseli/history.py +0 -0
  25. {python_siseli-0.1.2 → python_siseli-0.2.1}/siseli/models/__init__.py +0 -0
  26. {python_siseli-0.1.2 → python_siseli-0.2.1}/siseli/models/alarm.py +0 -0
  27. {python_siseli-0.1.2 → python_siseli-0.2.1}/siseli/models/auth.py +0 -0
  28. {python_siseli-0.1.2 → python_siseli-0.2.1}/siseli/models/common.py +0 -0
  29. {python_siseli-0.1.2 → python_siseli-0.2.1}/siseli/models/config.py +0 -0
  30. {python_siseli-0.1.2 → python_siseli-0.2.1}/siseli/models/dashboard.py +0 -0
  31. {python_siseli-0.1.2 → python_siseli-0.2.1}/siseli/models/device.py +0 -0
  32. {python_siseli-0.1.2 → python_siseli-0.2.1}/siseli/models/dictionary.py +0 -0
  33. {python_siseli-0.1.2 → python_siseli-0.2.1}/siseli/models/history.py +0 -0
  34. {python_siseli-0.1.2 → python_siseli-0.2.1}/siseli/models/state.py +0 -0
  35. {python_siseli-0.1.2 → python_siseli-0.2.1}/siseli/models/station.py +0 -0
  36. {python_siseli-0.1.2 → python_siseli-0.2.1}/siseli/state.py +0 -0
  37. {python_siseli-0.1.2 → python_siseli-0.2.1}/siseli/station.py +0 -0
  38. {python_siseli-0.1.2 → python_siseli-0.2.1}/tests/test_auth.py +0 -0
  39. {python_siseli-0.1.2 → python_siseli-0.2.1}/tests/test_phase3.py +0 -0
@@ -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"
@@ -4,13 +4,14 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "python-siseli"
7
- version = "0.1.2"
7
+ version = "0.2.1"
8
8
  description = "Python SDK for Siseli Cloud"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.11"
11
11
  license = { text = "MIT" }
12
12
  dependencies = [
13
13
  "httpx>=0.27",
14
+ "cryptography>=42",
14
15
  ]
15
16
 
16
17
  [project.optional-dependencies]
@@ -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"
@@ -17,6 +17,7 @@ siseli/device.py
17
17
  siseli/dictionary.py
18
18
  siseli/exceptions.py
19
19
  siseli/history.py
20
+ siseli/open_auth.py
20
21
  siseli/state.py
21
22
  siseli/station.py
22
23
  siseli/models/__init__.py
@@ -31,4 +32,5 @@ siseli/models/history.py
31
32
  siseli/models/state.py
32
33
  siseli/models/station.py
33
34
  tests/test_auth.py
35
+ tests/test_open_auth.py
34
36
  tests/test_phase3.py
@@ -1,4 +1,5 @@
1
1
  httpx>=0.27
2
+ cryptography>=42
2
3
 
3
4
  [dev]
4
5
  pytest>=8
@@ -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",
@@ -0,0 +1,9 @@
1
+ BASE_URL = "https://solar.siseli.com"
2
+ DEFAULT_TIMEOUT = 30.0
3
+ DEFAULT_PAGE_SIZE = 20
4
+ # dataSource=1 observed in all telemetry requests in HAR captures
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="
@@ -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)
@@ -0,0 +1,166 @@
1
+ """Tests for Siseli Open API signing helpers."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import httpx
6
+ import pytest
7
+
8
+ from siseli.const import SISELI_APP_ID, SISELI_APP_SECRET_ENCRYPTED
9
+ from siseli.open_auth import (
10
+ attach_open_auth,
11
+ build_open_headers,
12
+ decrypt_open_secret,
13
+ )
14
+
15
+
16
+ class _MockHttpClient:
17
+ def __init__(self) -> None:
18
+ self.event_hooks = {"request": []}
19
+
20
+
21
+ class _MockSiseliClient:
22
+ _timezone = "Europe/Moscow"
23
+
24
+ def __init__(self) -> None:
25
+ self._http = _MockHttpClient()
26
+
27
+
28
+ def test_decrypt_open_secret_with_app_id() -> None:
29
+ """Encrypted Open secret is decrypted with AppID-derived key/IV."""
30
+ assert (
31
+ decrypt_open_secret(SISELI_APP_ID, SISELI_APP_SECRET_ENCRYPTED)
32
+ == "CJbrtLtqFES62bJ3ZW7c"
33
+ )
34
+
35
+
36
+ def test_get_request_signing_uses_empty_body_hash() -> None:
37
+ """GET requests must sign with an empty body hash."""
38
+ secret = decrypt_open_secret(SISELI_APP_ID, SISELI_APP_SECRET_ENCRYPTED)
39
+ headers = build_open_headers(
40
+ method="GET",
41
+ query="b=2&a=1",
42
+ body=None,
43
+ app_id=SISELI_APP_ID,
44
+ app_secret=secret,
45
+ timezone="UTC",
46
+ nonce="nonce-123",
47
+ )
48
+ assert "IOT-Open-Body-Hash" not in headers
49
+ assert headers["IOT-Open-Sign"] == "a325bd9e648a5cd75ca803ccb801906e"
50
+
51
+
52
+ def test_post_request_signing_with_json_body() -> None:
53
+ """POST requests must hash and sign the raw JSON body bytes."""
54
+ secret = decrypt_open_secret(SISELI_APP_ID, SISELI_APP_SECRET_ENCRYPTED)
55
+ body = b'{"name":"abc","value":123}'
56
+ headers = build_open_headers(
57
+ method="POST",
58
+ query="z=9",
59
+ body=body,
60
+ app_id=SISELI_APP_ID,
61
+ app_secret=secret,
62
+ timezone="UTC",
63
+ nonce="nonce-123",
64
+ )
65
+ assert headers["IOT-Open-Sign"] == "6fa65e7127b26486b752cdfdf6e60793"
66
+
67
+
68
+ def test_unicode_json_body_signing() -> None:
69
+ """Unicode body content must be hashed/signatured as UTF-8 bytes."""
70
+ secret = decrypt_open_secret(SISELI_APP_ID, SISELI_APP_SECRET_ENCRYPTED)
71
+ body = '{"text":"Привет 🌞"}'.encode()
72
+ headers = build_open_headers(
73
+ method="POST",
74
+ query="",
75
+ body=body,
76
+ app_id=SISELI_APP_ID,
77
+ app_secret=secret,
78
+ timezone="UTC",
79
+ nonce="nonce-123",
80
+ )
81
+ assert headers["IOT-Open-Sign"] == "ef3d8c0d1834f59ae76312fcb43356aa"
82
+
83
+
84
+ def test_empty_post_body_signing() -> None:
85
+ """Empty POST body must use SHA256 of empty bytes."""
86
+ secret = decrypt_open_secret(SISELI_APP_ID, SISELI_APP_SECRET_ENCRYPTED)
87
+ headers = build_open_headers(
88
+ method="POST",
89
+ query="x=",
90
+ body=b"",
91
+ app_id=SISELI_APP_ID,
92
+ app_secret=secret,
93
+ timezone="UTC",
94
+ nonce="nonce-123",
95
+ )
96
+ assert headers["IOT-Open-Sign"] == "c9fc185f18a1082b3499aa2d74b08240"
97
+
98
+
99
+ @pytest.mark.asyncio
100
+ async def test_header_generation_includes_required_iot_open_values() -> None:
101
+ """Request hook injects required IOT-Open-* and timezone headers."""
102
+ secret = decrypt_open_secret(SISELI_APP_ID, SISELI_APP_SECRET_ENCRYPTED)
103
+
104
+ client = _MockSiseliClient()
105
+ attach_open_auth(client)
106
+ assert len(client._http.event_hooks["request"]) == 1
107
+
108
+ request = httpx.Request(
109
+ "POST",
110
+ "https://solar.siseli.com/apis/login/account",
111
+ content=b'{"account":"u","password":"p"}',
112
+ )
113
+ await client._http.event_hooks["request"][0](request)
114
+
115
+ for key in (
116
+ "IOT-Time-Zone",
117
+ "IOT-Open-AppID",
118
+ "IOT-Open-Nonce",
119
+ "IOT-Open-Sign",
120
+ ):
121
+ assert key in request.headers
122
+ assert "IOT-Open-Body-Hash" not in request.headers
123
+ assert request.headers["IOT-Time-Zone"] == "Europe/Moscow"
124
+ assert request.headers["IOT-Open-AppID"] == SISELI_APP_ID
125
+ expected_sign = build_open_headers(
126
+ method="POST",
127
+ query="",
128
+ body=request.content,
129
+ app_id=SISELI_APP_ID,
130
+ app_secret=secret,
131
+ timezone="Europe/Moscow",
132
+ nonce=request.headers["IOT-Open-Nonce"],
133
+ )["IOT-Open-Sign"]
134
+ assert request.headers["IOT-Open-Sign"] == expected_sign
135
+ assert request.headers["IOT-Open-Sign"] != secret
136
+
137
+
138
+ def test_body_change_changes_signature_and_query_is_ignored() -> None:
139
+ """Body bytes affect the signature; query string does not."""
140
+ secret = decrypt_open_secret(SISELI_APP_ID, SISELI_APP_SECRET_ENCRYPTED)
141
+
142
+ def sign(body: bytes, query: str = "") -> str:
143
+ return build_open_headers(
144
+ method="POST",
145
+ query=query,
146
+ body=body,
147
+ app_id=SISELI_APP_ID,
148
+ app_secret=secret,
149
+ timezone="UTC",
150
+ nonce="n",
151
+ )["IOT-Open-Sign"]
152
+
153
+ assert sign(b"{}") != sign(b"{ }")
154
+ assert sign(b"{}") == sign(b"{}", "a=1")
155
+
156
+
157
+ @pytest.mark.asyncio
158
+ async def test_each_request_gets_new_nonce() -> None:
159
+ client = _MockSiseliClient()
160
+ attach_open_auth(client)
161
+ hook = client._http.event_hooks["request"][0]
162
+ r1 = httpx.Request("GET", "https://solar.siseli.com/apis/x")
163
+ r2 = httpx.Request("GET", "https://solar.siseli.com/apis/x")
164
+ await hook(r1)
165
+ await hook(r2)
166
+ assert r1.headers["IOT-Open-Nonce"] != r2.headers["IOT-Open-Nonce"]
@@ -1,5 +0,0 @@
1
- BASE_URL = "https://solar.siseli.com"
2
- DEFAULT_TIMEOUT = 30.0
3
- DEFAULT_PAGE_SIZE = 20
4
- # dataSource=1 observed in all telemetry requests in HAR captures
5
- DEFAULT_DATA_SOURCE = 1
File without changes
File without changes