From c3fdfad4dc12c480f8ff2915f0d8dccaba7a87a6 Mon Sep 17 00:00:00 2001 From: Juan Carlos Date: Wed, 11 Dec 2024 00:01:19 -0300 Subject: [PATCH] fix: fix token parsing --- realtime/_async/client.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/realtime/_async/client.py b/realtime/_async/client.py index 7e39e72..f222d42 100644 --- a/realtime/_async/client.py +++ b/realtime/_async/client.py @@ -259,26 +259,27 @@ async def set_auth(self, token: Optional[str]) -> None: Returns: None """ - # No empty string tokens. if isinstance(token, str) and len(token.strip()) == 0: - raise ValueError("InvalidJWTToken: Provide a valid jwt token") + raise ValueError("Provide a valid jwt token") if token: - payload = token.split(".")[1] + "==" parsed = None try: + payload = token.split(".")[1] + "==" parsed = json.loads(b64decode(payload).decode("utf-8")) except Exception: - raise ValueError("InvalidJWTToken: Provide a valid jwt token") - - # Handle expired token if any. - if parsed and "exp" in parsed: - now = floor(datetime.now().timestamp()) - valid = now - parsed["exp"] < 0 - if not valid: - raise ValueError( - f"InvalidJWTToken: Invalid value for JWT claim 'exp' with value { parsed['exp'] }" - ) + raise ValueError("InvalidJWTToken") + + if parsed: + if "exp" in parsed: + now = floor(datetime.now().timestamp()) + valid = now - parsed["exp"] < 0 + if not valid: + raise ValueError( + f"InvalidJWTToken: Invalid value for JWT claim 'exp' with value { parsed['exp'] }" + ) + else: + raise ValueError("InvalidJWTToken: expected claim 'exp'") self.access_token = token