Skip to content

Commit

Permalink
Refresh xsrf token on connect if last fetch was over a day ago
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Aug 3, 2023
1 parent 1384552 commit fce1ce9
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions maugclib/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import os
import random
import re
import time

from google.protobuf import message as proto
from yarl import URL
Expand Down Expand Up @@ -52,6 +53,7 @@ class Client:
_session: http_utils.Session | None
_channel: channel.Channel | None
_listen_future: asyncio.Future | None
_last_token_refresh: float

def __init__(
self,
Expand All @@ -62,6 +64,7 @@ def __init__(
) -> None:
self._max_retries = max_retries
self._retry_backoff_base = retry_backoff_base
self._last_token_refresh = -86400

self.on_connect = event.Event("Client.on_connect")
"""
Expand Down Expand Up @@ -141,6 +144,9 @@ async def connect(self, max_age: float) -> None:
called.
"""
self._api_reqid = 0
if self._last_token_refresh + 86400 < time.monotonic():
logger.info("Refreshing xsrf token before connecting")
await self.refresh_tokens()

self._channel = channel.Channel(self._session, self._max_retries, self._retry_backoff_base)

Expand Down Expand Up @@ -530,6 +536,7 @@ async def refresh_tokens(self):
if wiz_data["qwAQke"] == "AccountsSignInUi":
raise exceptions.NotLoggedInError("Provided tokens aren't valid")
self.xsrf_token = wiz_data["SMqcke"]
self._last_token_refresh = time.monotonic()

##########################################################################
# Private methods
Expand Down

0 comments on commit fce1ce9

Please sign in to comment.