From 14cecee49d87256e632bf221a68c1474899dc7ec Mon Sep 17 00:00:00 2001 From: Artem Sorokin Date: Tue, 23 Apr 2024 16:52:40 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BF=D0=BE=D0=B4=D0=B4=D0=B5=D1=80=D0=B6=D0=BA?= =?UTF-8?q?=D0=B0=20=D0=BA=D1=83=D0=BA=D0=BE=D0=B2=20=D1=81=20id.yandex.ru?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yandex_station_intents/config_flow.py | 6 +++-- .../translations/en.json | 2 +- .../yandex_station_intents/yandex_session.py | 22 +++++++++---------- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/custom_components/yandex_station_intents/config_flow.py b/custom_components/yandex_station_intents/config_flow.py index 1e183f3..12ea4bb 100644 --- a/custom_components/yandex_station_intents/config_flow.py +++ b/custom_components/yandex_station_intents/config_flow.py @@ -72,12 +72,14 @@ async def async_step_yandex_station(self, user_input: ConfigType | None = None) async def async_step_cookies(self, user_input: ConfigType) -> FlowResult: try: - cookies = {p["name"]: p["value"] for p in json.loads(user_input[AuthMethod.COOKIES])} + raw = json.loads(user_input[AuthMethod.COOKIES]) + host = next(p["domain"] for p in raw if p["domain"].startswith(".yandex.")) + cookies = {p["name"]: p["value"] for p in raw} except (TypeError, KeyError, json.decoder.JSONDecodeError): return await self._show_form(AuthMethod.COOKIES, errors={"base": "cookies.invalid_format"}) try: - response = await self._session.login_cookies(cookies) + response = await self._session.login_cookies(host, cookies) except AuthException as e: _LOGGER.error(f"Ошибка авторизации: {e}") return await self._show_form(AuthMethod.COOKIES, errors={"base": "auth.error"}) diff --git a/custom_components/yandex_station_intents/translations/en.json b/custom_components/yandex_station_intents/translations/en.json index 05995a1..f2abdd5 100644 --- a/custom_components/yandex_station_intents/translations/en.json +++ b/custom_components/yandex_station_intents/translations/en.json @@ -16,7 +16,7 @@ }, "cookies": { "title": "Авторизация через cookies", - "description": "* Установите расширение [Copy Cookies](https://chrome.google.com/webstore/detail/copy-cookies/jcbpglbplpblnagieibnemmkiamekcdg) в Google Chrome или Yandex Browser\n* Авторизуйтесь на [passport.yandex.com](https://passport.yandex.com/profile) (обязательно **.com**)\n* Скопируйте сюда cookies с помощью расширения", + "description": "* Установите расширение [Copy Cookies](https://chrome.google.com/webstore/detail/copy-cookies/jcbpglbplpblnagieibnemmkiamekcdg) в Google Chrome или Yandex Browser\n* Авторизуйтесь на [id.yandex.ru](https://id.yandex.ru)\n* Скопируйте сюда cookies с помощью расширения", "data": { "cookies": "Cookies JSON" } diff --git a/custom_components/yandex_station_intents/yandex_session.py b/custom_components/yandex_station_intents/yandex_session.py index b705b8b..57f0ad6 100644 --- a/custom_components/yandex_station_intents/yandex_session.py +++ b/custom_components/yandex_station_intents/yandex_session.py @@ -13,8 +13,6 @@ _LOGGER = logging.getLogger(__name__) -HEADERS = {"User-Agent": "com.yandex.mobile.auth.sdk/7.15.0.715001762"} - RE_CSRF = re.compile('"csrfToken2":"(.+?)"') @@ -87,15 +85,17 @@ def __init__(self, hass: HomeAssistant, entry: ConfigEntry | None = None) -> Non raw = base64.b64decode(cookie) cast(CookieJar, self._session.cookie_jar)._cookies = pickle.loads(raw) - async def login_cookies(self, cookies: dict[str, str]) -> LoginResponse: - payload = { - "grant_type": "sessionid", - "client_id": "c0ebe342af7d48fbbbfcf2d2eedb8f9e", - "client_secret": "ad0a908f0aa341a182a37ecd75bc319e", - "host": "passport.yandex.com", - } + async def login_cookies(self, host: str, cookies: dict[str, str]) -> LoginResponse: r = await self._session.post( - "https://mobileproxy.passport.yandex.net/1/token", data=payload, headers=HEADERS, cookies=cookies + "https://mobileproxy.passport.yandex.net/1/bundle/oauth/token_by_sessionid", + data={ + "client_id": "c0ebe342af7d48fbbbfcf2d2eedb8f9e", + "client_secret": "ad0a908f0aa341a182a37ecd75bc319e", + }, + headers={ + "Ya-Client-Host": host, + "Ya-Client-Cookie": "; ".join([f"{k}={v}" for k, v in cookies.items()]), + }, ) resp = await r.json() if "error" in resp: @@ -110,7 +110,7 @@ async def login_cookies(self, cookies: dict[str, str]) -> LoginResponse: async def validate_token(self, x_token: str) -> LoginResponse: headers = {"Authorization": f"OAuth {x_token}"} r = await self._session.get( - "https://mobileproxy.passport.yandex.net/1/bundle/account/" "short_info/?avatar_size=islands-300", + "https://mobileproxy.passport.yandex.net/1/bundle/account/short_info/?avatar_size=islands-300", headers=headers, ) resp = await r.json()