-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathauth.py
146 lines (118 loc) · 5.02 KB
/
auth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from logging import getLogger
from qbittorrentapi import Version
from qbittorrentapi.decorators import login_required
from qbittorrentapi.definitions import APINames
from qbittorrentapi.definitions import ClientCache
from qbittorrentapi.exceptions import HTTP403Error
from qbittorrentapi.exceptions import LoginFailed
from qbittorrentapi.exceptions import UnsupportedQbittorrentVersion
from qbittorrentapi.request import Request
logger = getLogger(__name__)
class Authorization(ClientCache):
"""
Allows interaction with the ``Authorization`` API endpoints.
:Usage:
>>> from qbittorrentapi import Client
>>> client = Client(host='localhost:8080', username='admin', password='adminadmin')
>>> is_logged_in = client.auth.is_logged_in
>>> client.auth.log_in(username='admin', password='adminadmin')
>>> client.auth.log_out()
"""
@property
def is_logged_in(self):
"""Implements :meth:`~AuthAPIMixIn.is_logged_in`"""
return self._client.is_logged_in
def log_in(self, username=None, password=None, **kwargs):
"""Implements :meth:`~AuthAPIMixIn.auth_log_in`"""
return self._client.auth_log_in(username=username, password=password, **kwargs)
def log_out(self, **kwargs):
"""Implements :meth:`~AuthAPIMixIn.auth_log_out`"""
return self._client.auth_log_out(**kwargs)
class AuthAPIMixIn(Request):
"""
Implementation of all ``Authorization`` API methods.
:Usage:
>>> from qbittorrentapi import Client
>>> client = Client(host='localhost:8080', username='admin', password='adminadmin')
>>> _ = client.is_logged_in
>>> client.auth_log_in(username='admin', password='adminadmin')
>>> client.auth_log_out()
"""
@property
def auth(self):
"""
Allows for transparent interaction with Authorization endpoints.
:return: Auth object
"""
if self._authorization is None:
self._authorization = Authorization(client=self)
return self._authorization
authorization = auth
@property
def is_logged_in(self):
"""
Returns True if low-overhead API call succeeds; False otherwise.
There isn't a reliable way to know if an existing session is still valid
without attempting to use it. qBittorrent invalidates cookies when they expire.
:returns: True/False if current auth cookie is accepted by qBittorrent.
"""
try:
self._post(_name=APINames.Application, _method="version")
except HTTP403Error:
return False
else:
return True
def auth_log_in(self, username=None, password=None, **kwargs):
"""
Log in to qBittorrent host.
:raises LoginFailed: if credentials failed to log in
:raises Forbidden403Error: if user is banned...or not logged in
:param username: username for qBittorrent client
:param password: password for qBittorrent client
:return: None
"""
if username:
self.username = username
self._password = password or ""
self._initialize_context()
creds = {"username": self.username, "password": self._password}
auth_response = self._post(
_name=APINames.Authorization, _method="login", data=creds, **kwargs
)
if auth_response.text != "Ok.":
logger.debug("Login failed")
raise LoginFailed()
logger.debug("Login successful")
# check if the connected qBittorrent is fully supported by this Client yet
if self._RAISE_UNSUPPORTEDVERSIONERROR:
app_version = self.app_version()
api_version = self.app_web_api_version()
if not (
Version.is_api_version_supported(api_version)
and Version.is_app_version_supported(app_version)
):
raise UnsupportedQbittorrentVersion(
"This version of qBittorrent is not fully supported => App Version: %s API Version: %s"
% (app_version, api_version)
)
@property
def _SID(self):
"""
Authorization session cookie from qBittorrent using default cookie name
`SID`. Backwards compatible for :meth:`~AuthAPIMixIn._session_cookie`.
:return: Auth cookie value from qBittorrent or None if one isn't already acquired
"""
return self._session_cookie()
def _session_cookie(self, cookie_name="SID"):
"""
Authorization session cookie from qBittorrent.
:param cookie_name: Name of the authorization cookie; configurable after v4.5.0.
:return: Auth cookie value from qBittorrent or None if one isn't already acquired
"""
if self._http_session:
return self._http_session.cookies.get(cookie_name, None)
return None
@login_required
def auth_log_out(self, **kwargs):
"""End session with qBittorrent."""
self._post(_name=APINames.Authorization, _method="logout", **kwargs)