Skip to content

Commit 6ce5996

Browse files
committed
addd 'bcrypt' support (simplified) v2
1 parent 1e00ed4 commit 6ce5996

File tree

1 file changed

+18
-9
lines changed
  • custom_components/goecharger_api2/pygoecharger_ha

1 file changed

+18
-9
lines changed

custom_components/goecharger_api2/pygoecharger_ha/__init__.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ async def _ws_debounce_coordinator_update(self):
464464

465465
def _ws_compute_hashed_password(self, hash_type: str, password: str, serial: str) -> bytes:
466466
if hash_type == "pbkdf2":
467-
"""Compute PBKDF2-SHA512 hashed password for WebSocket authentication"""
467+
# Compute PBKDF2-SHA512 hashed password for WebSocket authentication
468468
hashed = hashlib.pbkdf2_hmac(
469469
'sha512',
470470
password.encode('utf-8'),
@@ -579,7 +579,7 @@ async def ws_connect(self):
579579
headers = None
580580

581581
try:
582-
async with self.web_session.ws_connect(url=self.ws_url, headers=headers) as ws:
582+
async with (self.web_session.ws_connect(url=self.ws_url, headers=headers) as ws):
583583
self._ws_connection = ws
584584
_LOGGER.info(f"ws_connect(): Connected to WebSocket: {self.ws_url}")
585585

@@ -598,9 +598,6 @@ async def ws_connect(self):
598598
self._ws_proto = normalized_hello.get('proto', -1)
599599
self._ws_protocol = normalized_hello.get('protocol', -1)
600600

601-
# 'devicetype': 'go-eCharger_V4'
602-
# 'devicetype': 'go-eCharger_Phoenix', 'devicesubtype': 'core_cable',
603-
604601
_LOGGER.debug(f"ws_connect(): Extracted the device serial: {self._ws_serial} [secured: {self._ws_secured}, proto: {self._ws_proto}, protocol: {self._ws_protocol}]")
605602
self._ws_device_info = {k: v for k, v in normalized_hello.items()}
606603
if 'type' in self._ws_device_info:
@@ -613,8 +610,20 @@ async def ws_connect(self):
613610
auth_data = self._ws_decode_message(auth_req_msg.data)
614611
normalized_auth = self._ws_normalize_dict(auth_data)
615612

616-
hash_type = normalized_auth.get('hash', 'pbkdf2').lower()
617-
if hash_type not in ['pbkdf2', 'bcrypt']:
613+
hash_type = normalized_auth.get('hash', '').lower()
614+
_LOGGER.debug(f"ws_connect(): Extracted the auth type: '{hash_type}' - source was: '{normalized_auth.get('hash')}'")
615+
if hash_type is None or len(hash_type) == 0:
616+
_LOGGER.info("ws_connect(): No authentication hash type in AUTH REQUIRED message found...")
617+
# the default fallback is always 'pbkdf2' for the older devices...
618+
# 'devicetype': 'go-eCharger_V4'
619+
hash_type = "pbkdf2"
620+
621+
# 'devicetype': 'go-eCharger_Phoenix', 'devicesubtype': 'core_cable',
622+
if ("_phoenix" in self._ws_device_info.get("devicetype", "").lower() or
623+
"core" in self._ws_device_info.get("devicesubtype", "").lower()):
624+
hash_type = "bcrypt"
625+
626+
if hash_type not in ["pbkdf2", "bcrypt"]:
618627
_LOGGER.info(f"ws_connect(): Unsupported authentication hash type: {hash_type}")
619628
return None
620629

@@ -625,12 +634,12 @@ async def ws_connect(self):
625634

626635
self._ws_hashed_password = self._ws_compute_hashed_password(hash_type, self.access_password, serial)
627636
_logging_pwd = self._ws_hashed_password.decode('utf-8')
628-
_LOGGER.debug(f"ws_connect(): Computed hashed password {_logging_pwd[:6]}...{_logging_pwd[-6:]}")
637+
_LOGGER.debug(f"ws_connect(): Computed hashed password {hash_type}: {_logging_pwd[:6]}...{_logging_pwd[-6:]}")
629638

630639
token1 = normalized_auth.get('token1')
631640
token2 = normalized_auth.get('token2')
632641
if not token1 or not token2:
633-
_LOGGER.warning("ws_connect(): Missing authentication tokens")
642+
_LOGGER.warning("ws_connect(): Missing authentication tokens in AUTH REQUIRED message!")
634643
return None
635644

636645
# Step 4: Generate token3 and compute auth hash

0 commit comments

Comments
 (0)