From 291b502edef2ee389f98493b11a95b1521f811e4 Mon Sep 17 00:00:00 2001 From: Divided by Zer0 Date: Wed, 17 Apr 2024 10:16:52 +0200 Subject: [PATCH] feat: allows to refresh auth token with relog_in (#94) --- pythorhead/lemmy.py | 3 +++ pythorhead/requestor.py | 14 +++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/pythorhead/lemmy.py b/pythorhead/lemmy.py index 523d26c..ece647e 100644 --- a/pythorhead/lemmy.py +++ b/pythorhead/lemmy.py @@ -44,6 +44,9 @@ def username(self): def log_in(self, username_or_email: str, password: str, totp: Optional[str] = None) -> bool: return self._requestor.log_in(username_or_email, password, totp) + def relog_in(self) -> bool: + return self._requestor._log_in() + def discover_community(self, community_name: str, search=SearchOption.Retry) -> Optional[int]: if community_name in self._known_communities: return self._known_communities[community_name] diff --git a/pythorhead/requestor.py b/pythorhead/requestor.py index 8818bb5..5c65206 100644 --- a/pythorhead/requestor.py +++ b/pythorhead/requestor.py @@ -30,6 +30,7 @@ class Requestor: raise_exceptions: Optional[bool] = False request_timeout: Optional[int] = 3 logged_in_username: Optional[str] = None + current_password: Optional[str] = None def __init__(self, raise_exceptions = False, request_timeout = 3): self._auth = Authentication() @@ -122,14 +123,21 @@ def image_del(self, method: Request, image_delete_url:str, **kwargs) -> Optional return r def log_in(self, username_or_email: str, password: str, totp: Optional[str] = None) -> bool: + self.logged_in_username = username_or_email + self.current_password = password + return self._log_in(totp) + + def _log_in(self, totp: Optional[str] = None) -> bool: payload = { - "username_or_email": username_or_email, - "password": password, + "username_or_email": self.logged_in_username, + "password": self.current_password, "totp_2fa_token": totp, } if data := self.api(Request.POST, "/user/login", json=payload): self._auth.set_token(data["jwt"]) - self.logged_in_username = username_or_email + else: + self.logged_in_username = None + self.current_password = None return self._auth.token is not None def log_out(self) -> None: