Skip to content

Commit 7c64369

Browse files
committed
Improve v6 XM login
1 parent dc85ca5 commit 7c64369

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

airos/base.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ def _get_authenticated_headers(
202202
headers["Content-Type"] = "application/x-www-form-urlencoded"
203203

204204
if self._csrf_id: # pragma: no cover
205+
_LOGGER.error("TESTv6 - CSRF ID found %s", self._csrf_id)
205206
headers["X-CSRF-ID"] = self._csrf_id
206207

207208
if self._auth_cookie: # pragma: no cover
@@ -217,9 +218,12 @@ def _store_auth_data(self, response: aiohttp.ClientResponse) -> None:
217218
# Parse all Set-Cookie headers to ensure we don't miss AIROS_* cookie
218219
cookie = SimpleCookie()
219220
for set_cookie in response.headers.getall("Set-Cookie", []):
221+
_LOGGER.error("TESTv6 - regular cookie handling: %s", set_cookie)
220222
cookie.load(set_cookie)
221223
for key, morsel in cookie.items():
222-
_LOGGER.error("TESTv6 - cookie handling: %s with %s", key, morsel.value)
224+
_LOGGER.error(
225+
"TESTv6 - AIROS_cookie handling: %s with %s", key, morsel.value
226+
)
223227
if key.startswith("AIROS_"):
224228
self._auth_cookie = morsel.key[6:] + "=" + morsel.value
225229
break
@@ -246,6 +250,12 @@ async def _request_json(
246250
if headers:
247251
request_headers.update(headers)
248252

253+
# Potential XM fix - not sure, might have been login issue
254+
if url == self._status_cgi_url:
255+
request_headers["Referrer"] = f"{self.base_url}/login.cgi"
256+
request_headers["Accept"] = "application/json, text/javascript, */*; q=0.01"
257+
request_headers["X-Requested-With"] = "XMLHttpRequest"
258+
249259
try:
250260
if (
251261
url not in self._login_urls.values()
@@ -268,19 +278,21 @@ async def _request_json(
268278

269279
# v6 responds with a 302 redirect and empty body
270280
if url != self._login_urls["v6_login"]:
271-
_LOGGER.error("TESTv6 - we are in v8, raising for status")
272281
response.raise_for_status()
273282

274283
response_text = await response.text()
275-
_LOGGER.debug("Successfully fetched JSON from %s", url)
284+
_LOGGER.error("Successfully fetched %s from %s", response_text, url)
276285

277286
# If this is the login request, we need to store the new auth data
278287
if url in self._login_urls.values():
279288
self._store_auth_data(response)
280289
self.connected = True
281290

282-
# V6 responsds with empty body on login, not JSON
283-
if url != self._login_urls["v6_login"] and not response_text:
291+
_LOGGER.error("TESTv6 - response: %s", response_text)
292+
# V6 responds with empty body on login, not JSON
293+
if url == self._login_urls["v6_login"]:
294+
self._store_auth_data(response)
295+
self.connected = True
284296
return {}
285297

286298
return json.loads(response_text)
@@ -325,7 +337,7 @@ async def login(self) -> None:
325337
_LOGGER.error("TESTv6 - Trying to get / first for cookies")
326338
with contextlib.suppress(Exception):
327339
cookieresponse = await self._request_json(
328-
"GET", f"{self.base_url}/", authenticated=False
340+
"GET", f"{self.base_url}/", authenticated=True
329341
)
330342
_LOGGER.error("TESTv6 - Cookie response: %s", cookieresponse)
331343

@@ -351,7 +363,7 @@ async def login(self) -> None:
351363
headers=login_headers,
352364
form_data=v6_simple_multipart_form_data,
353365
authenticated=True,
354-
allow_redirects=False,
366+
allow_redirects=True,
355367
)
356368
except (AirOSUrlNotFoundError, AirOSConnectionSetupError) as err:
357369
_LOGGER.error(
@@ -369,8 +381,12 @@ async def login(self) -> None:
369381

370382
async def status(self) -> AirOSDataModel:
371383
"""Retrieve status from the device."""
384+
status_headers = {
385+
"Accept": "application/json, text/javascript, */*; q=0.01",
386+
"X-Requested-With": "XMLHttpRequest",
387+
}
372388
response = await self._request_json(
373-
"GET", self._status_cgi_url, authenticated=True
389+
"GET", self._status_cgi_url, authenticated=True, headers=status_headers
374390
)
375391

376392
try:

0 commit comments

Comments
 (0)