Skip to content

Commit

Permalink
Добавлена поддержка куков с id.yandex.ru
Browse files Browse the repository at this point in the history
  • Loading branch information
dext0r committed Apr 23, 2024
1 parent 347d89e commit 14cecee
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
6 changes: 4 additions & 2 deletions custom_components/yandex_station_intents/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
22 changes: 11 additions & 11 deletions custom_components/yandex_station_intents/yandex_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":"(.+?)"')


Expand Down Expand Up @@ -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:
Expand All @@ -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()
Expand Down

0 comments on commit 14cecee

Please sign in to comment.