Skip to content

Commit

Permalink
feat: allows to refresh auth token with relog_in (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
db0 authored Apr 17, 2024
1 parent e9e94ab commit 291b502
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
3 changes: 3 additions & 0 deletions pythorhead/lemmy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
14 changes: 11 additions & 3 deletions pythorhead/requestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 291b502

Please sign in to comment.