Automatically clear the jar and delete the cookie file for re-auth;

if the Client ID within the cookie does not match the Client ID currently used by the application (#107)
This commit is contained in:
DevilXD committed 2024-09-15 14:25:20 +02:00
1 parent 77e2ce0c28
commit df05fb2707
1 file changed
+35 -27
+35 -27
View File
@@ -374,34 +374,42 @@ class _AuthState:
login_form: LoginForm = self._twitch.gui.login login_form: LoginForm = self._twitch.gui.login
logger.info("Checking login") logger.info("Checking login")
login_form.update(_("gui", "login", "logging_in"), None) login_form.update(_("gui", "login", "logging_in"), None)
for attempt in range(2): for client_mismatch_attempt in range(2):
cookie = jar.filter_cookies(client_info.CLIENT_URL) for invalid_token_attempt in range(2):
if "auth-token" not in cookie: cookie = jar.filter_cookies(client_info.CLIENT_URL)
self.access_token = await self._oauth_login() if "auth-token" not in cookie:
cookie["auth-token"] = self.access_token self.access_token = await self._oauth_login()
elif not hasattr(self, "access_token"): cookie["auth-token"] = self.access_token
logger.info("Restoring session from cookie") elif not hasattr(self, "access_token"):
self.access_token = cookie["auth-token"].value logger.info("Restoring session from cookie")
# validate the auth token, by obtaining user_id self.access_token = cookie["auth-token"].value
async with self._twitch.request( # validate the auth token, by obtaining user_id
"GET", async with self._twitch.request(
"https://id.twitch.tv/oauth2/validate", "GET",
headers={"Authorization": f"OAuth {self.access_token}"} "https://id.twitch.tv/oauth2/validate",
) as response: headers={"Authorization": f"OAuth {self.access_token}"}
status = response.status ) as response:
if status == 401: status = response.status
# the access token we have is invalid - clear the cookie and reauth if status == 401:
logger.info("Restored session is invalid") # the access token we have is invalid - clear the cookie and reauth
assert client_info.CLIENT_URL.host is not None logger.info("Restored session is invalid")
jar.clear_domain(client_info.CLIENT_URL.host) assert client_info.CLIENT_URL.host is not None
continue jar.clear_domain(client_info.CLIENT_URL.host)
elif status == 200: continue
validate_response = await response.json() elif status == 200:
break validate_response = await response.json()
break
else:
raise RuntimeError("Login verification failure (step #2)")
# ensure the cookie's client ID matches the currently selected client
if validate_response["client_id"] == client_info.CLIENT_ID:
break
# otherwise, we need to delete the entire cookie file and clear the jar
logger.info("Cookie client ID mismatch")
jar.clear()
COOKIES_PATH.unlink(missing_ok=True)
else: else:
raise RuntimeError("Login verification failure") raise RuntimeError("Login verification failure (step #1)")
if validate_response["client_id"] != client_info.CLIENT_ID:
raise LoginException("You're using an old cookie file, please generate a new one.")
self.user_id = int(validate_response["user_id"]) self.user_id = int(validate_response["user_id"])
cookie["persistent"] = str(self.user_id) cookie["persistent"] = str(self.user_id)
logger.info(f"Login successful, user ID: {self.user_id}") logger.info(f"Login successful, user ID: {self.user_id}")