Skip to content

Commit

Permalink
fix: fix token parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
juancarlospaco committed Dec 11, 2024
1 parent aee5050 commit c3fdfad
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions realtime/_async/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit c3fdfad

Please sign in to comment.