From c4e8038652bce57cf01535bf15a0661232ce4c64 Mon Sep 17 00:00:00 2001 From: Andre Basche Date: Sun, 11 Feb 2024 02:02:47 +0100 Subject: [PATCH] Fix problems with refresh token --- pyhon/command_loader.py | 2 +- pyhon/connection/api.py | 2 +- pyhon/connection/auth.py | 8 ++++---- pyhon/connection/handler/hon.py | 5 +++-- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/pyhon/command_loader.py b/pyhon/command_loader.py index 94b0277..fd615ec 100644 --- a/pyhon/command_loader.py +++ b/pyhon/command_loader.py @@ -109,7 +109,7 @@ class HonCommandLoader: categories: Optional[Dict[str, "HonCommand"]] = None, category_name: str = "", ) -> Optional[HonCommand]: - """Try to crate HonCommand object""" + """Try to create HonCommand object""" if not isinstance(data, dict): self._additional_data[command_name] = data return None diff --git a/pyhon/connection/api.py b/pyhon/connection/api.py index 1e17e4c..30a9cd2 100644 --- a/pyhon/connection/api.py +++ b/pyhon/connection/api.py @@ -75,7 +75,7 @@ class HonAPI: self._hon_handler = await HonConnectionHandler( self._email, self._password, - self._session, + session=self._session, mobile_id=self._mobile_id, ).create() return self diff --git a/pyhon/connection/auth.py b/pyhon/connection/auth.py index 5012bbf..c6f7c4f 100644 --- a/pyhon/connection/auth.py +++ b/pyhon/connection/auth.py @@ -48,7 +48,6 @@ class HonAuth: email: str, password: str, device: HonDevice, - refresh_token: str = "", ) -> None: self._session = session self._request = HonAuthConnectionHandler(session) @@ -58,7 +57,6 @@ class HonAuth: self._device = device self._expires: datetime = datetime.utcnow() self._auth = HonAuthData() - self._auth.refresh_token = refresh_token @property def cognito_token(self) -> str: @@ -201,7 +199,7 @@ class HonAuth: if access_token := re.findall("access_token=(.*?)&", text): self._auth.access_token = access_token[0] if refresh_token := re.findall("refresh_token=(.*?)&", text): - self._auth.refresh_token = refresh_token[0] + self._auth.refresh_token = parse.unquote(refresh_token[0]) if id_token := re.findall("id_token=(.*?)&", text): self._auth.id_token = id_token[0] return bool(access_token and refresh_token and id_token) @@ -266,7 +264,9 @@ class HonAuth: except exceptions.HonNoAuthenticationNeeded: return - async def refresh(self) -> bool: + async def refresh(self, refresh_token="") -> bool: + if refresh_token: + self._auth.refresh_token = refresh_token params = { "client_id": const.CLIENT_ID, "refresh_token": self._auth.refresh_token, diff --git a/pyhon/connection/handler/hon.py b/pyhon/connection/handler/hon.py index b7d808e..988c8f4 100644 --- a/pyhon/connection/handler/hon.py +++ b/pyhon/connection/handler/hon.py @@ -22,9 +22,9 @@ class HonConnectionHandler(ConnectionHandler): self, email: str, password: str, + session: Optional[aiohttp.ClientSession] = None, mobile_id: str = "", refresh_token: str = "", - session: Optional[aiohttp.ClientSession] = None, ) -> None: super().__init__(session=session) self._device: HonDevice = HonDevice(mobile_id) @@ -54,11 +54,12 @@ class HonConnectionHandler(ConnectionHandler): self._email, self._password, self._device, - refresh_token=self._refresh_token, ) return self async def _check_headers(self, headers: Dict[str, str]) -> Dict[str, str]: + if self._refresh_token: + await self.auth.refresh(self._refresh_token) if not (self.auth.cognito_token and self.auth.id_token): await self.auth.authenticate() headers["cognito-token"] = self.auth.cognito_token