Skip to content

Commit

Permalink
Avoid infinite recursion when client is banned
Browse files Browse the repository at this point in the history
  • Loading branch information
rmartin16 committed Nov 16, 2024
1 parent 60b4914 commit 3aa75fb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/qbittorrentapi/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@

from requests import Response

from qbittorrentapi import Version
from qbittorrentapi._version_support import Version
from qbittorrentapi.definitions import APIKwargsT, APINames, ClientCache
from qbittorrentapi.exceptions import LoginFailed, UnsupportedQbittorrentVersion
from qbittorrentapi.exceptions import (
LoginFailed,
UnsupportedQbittorrentVersion,
)
from qbittorrentapi.request import Request

if TYPE_CHECKING:
Expand Down Expand Up @@ -132,7 +135,11 @@ def _session_cookie(self, cookie_name: str = "SID") -> str | None:

def auth_log_out(self, **kwargs: APIKwargsT) -> None:
"""End session with qBittorrent."""
self._post(_name=APINames.Authorization, _method="logout", **kwargs)
# Originally, if log out failed authentication, the client would re-authenticate
# and then log out of that session. With the change to avoid retrying failed
# auth calls, only attempt to log out if the current authentication is valid.
if self.is_logged_in:
self._post(_name=APINames.Authorization, _method="logout", **kwargs)

def __enter__(self) -> Client:
self.auth_log_in()
Expand Down
5 changes: 5 additions & 0 deletions src/qbittorrentapi/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,11 @@ def _auth_request(
**kwargs,
)
except HTTP403Error:
# Do not retry auth endpoints for 403. If an auth endpoint is returning
# 403, then trying again won't work because it is likely the credentials
# are no longer valid. Furthermore, it leads to infinite recursion.
if api_namespace == APINames.Authorization:
raise
logger.debug("Login may have expired...attempting new login")
self.auth_log_in( # type: ignore[attr-defined]
requests_args=requests_args,
Expand Down

0 comments on commit 3aa75fb

Please sign in to comment.