Skip to content

Commit 470c012

Browse files
committed
Rework manual following redirects
1 parent 1d724a6 commit 470c012

File tree

1 file changed

+45
-4
lines changed

1 file changed

+45
-4
lines changed

airos/base.py

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import aiohttp
1717
from mashumaro.exceptions import InvalidFieldValue, MissingField
18+
from yarl import URL
1819

1920
from .data import (
2021
AirOSDataBaseClass,
@@ -214,7 +215,8 @@ def _get_authenticated_headers(
214215
self.api_version,
215216
self._auth_cookie,
216217
)
217-
headers["Cookie"] = f"AIROS_{self._auth_cookie}"
218+
# headers["Cookie"] = f"AIROS_{self._auth_cookie}"
219+
headers["Cookie"] = self._auth_cookie
218220

219221
return headers
220222

@@ -339,6 +341,11 @@ async def _request_json(
339341
_LOGGER.error(
340342
"TESTv%s - Response history: %s", self.api_version, response.history
341343
)
344+
_LOGGER.error(
345+
"TESTv%s - Session cookies: %s",
346+
self.api_version,
347+
self.session.cookie_jar.filter_cookies(URL(url)),
348+
)
342349

343350
# v6 responds with a 302 redirect and empty body
344351
if not url.startswith(self._login_urls["v6_login"]):
@@ -361,10 +368,32 @@ async def _request_json(
361368
self.connected = True
362369

363370
_LOGGER.error("TESTv%s - response: %s", self.api_version, response_text)
371+
372+
location = response.headers.get("Location")
373+
if location and isinstance(location, str) and location.startswith("/"):
374+
_LOGGER.error(
375+
"TESTv%s - Following redirect to: %s",
376+
self.api_version,
377+
location,
378+
)
379+
await self._request_json(
380+
"GET",
381+
f"{self.base_url}{location}",
382+
headers={
383+
"Referer": self._login_urls["v6_login"],
384+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36",
385+
},
386+
authenticated=True,
387+
allow_redirects=False,
388+
)
389+
else:
390+
_LOGGER.error(
391+
"TESTv%s - no location header found to follow in response to %s",
392+
self.api_version,
393+
url,
394+
)
364395
# V6 responds with empty body on login, not JSON
365396
if url.startswith(self._login_urls["v6_login"]):
366-
self._store_auth_data(response)
367-
self.connected = True
368397
return {}
369398

370399
return json.loads(response_text)
@@ -425,6 +454,18 @@ async def login(self) -> None:
425454
_LOGGER.error(
426455
"TESTv%s - Cookie response: %s", self.api_version, cookieresponse
427456
)
457+
if isinstance(cookieresponse, aiohttp.ClientResponse):
458+
_LOGGER.debug(
459+
"TESTv%s - Finalization redirect chain: %s",
460+
self.api_version,
461+
cookieresponse.history,
462+
)
463+
else:
464+
_LOGGER.debug(
465+
"TESTv%s - Finalization response is not a ClientResponse: %s",
466+
self.api_version,
467+
type(cookieresponse),
468+
)
428469

429470
v6_simple_multipart_form_data = aiohttp.FormData()
430471
v6_simple_multipart_form_data.add_field("uri", "/index.cgi")
@@ -457,7 +498,7 @@ async def login(self) -> None:
457498
ct_form=False,
458499
ct_json=False,
459500
authenticated=False,
460-
allow_redirects=True,
501+
allow_redirects=False,
461502
)
462503
except (AirOSUrlNotFoundError, AirOSConnectionSetupError) as err:
463504
_LOGGER.error(

0 commit comments

Comments
 (0)