Skip to content

Commit

Permalink
fix(fusionsolar): use pycryptodome, re-login on error
Browse files Browse the repository at this point in the history
  • Loading branch information
fhempy committed Nov 26, 2022
1 parent f1a49f0 commit 1427342
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions FHEM/bindings/python/fhempy/lib/fusionsolar/fusionsolar_api.py
Expand Up @@ -7,9 +7,9 @@
from datetime import datetime

import aiohttp
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import padding
from Crypto.Cipher import PKCS1_OAEP
from Crypto.Hash import SHA384
from Crypto.PublicKey import RSA


@dataclass
Expand Down Expand Up @@ -176,18 +176,11 @@ async def login(self):
self._validate_user_ver = "v2"
if fusion_pubkey_json["enableEncrypt"]:
self._validate_user_ver = "v3"
self.public_key = serialization.load_pem_public_key(
fusion_pubkey_json["pubKey"].encode("UTF-8"),
backend=default_backend(),
)
hex_pwd = self.public_key.encrypt(
self._password.encode("UTF-8"),
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA384()),
algorithm=hashes.SHA384(),
label=None,
),
self.public_key = RSA.import_key(
fusion_pubkey_json["pubKey"].encode("UTF-8")
)
cipher = PKCS1_OAEP.new(self.public_key, SHA384)
hex_pwd = cipher.encrypt(self._password.encode("UTF-8"))
self._password_encrypted = (
codecs.encode(hex_pwd, "base64").decode().replace("\n", "")
+ fusion_pubkey_json["version"]
Expand Down Expand Up @@ -301,9 +294,6 @@ async def _get_rest_data(self, path, params={}):
"User-Agent": "Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36",
}
resp = await self._get(url, headers, params=params)
if len(resp) == 0:
# try again
resp = await self._get(url, headers, params=params)
return resp

async def send_idle(self):
Expand Down Expand Up @@ -348,8 +338,12 @@ async def _get(self, url, headers, max_retries=5, params={}):
await self.login()
continue
else:
response = await resp.json()
break
if resp.headers["Content-Type"].startswith("text/html"):
await self.login()
continue
else:
response = await resp.json()
break

if "success" in response and response["success"] is True:
if "data" in response:
Expand Down

0 comments on commit 1427342

Please sign in to comment.