1
mirror of https://github.com/home-assistant/core synced 2024-07-27 18:58:57 +02:00

Fix oauth2 error (#86634)

This commit is contained in:
Paulus Schoutsen 2023-01-25 14:50:16 -05:00 committed by GitHub
parent 1b97a51b5e
commit df0fc30695
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 3 deletions

View File

@ -13,7 +13,7 @@ from collections.abc import Awaitable, Callable
import logging import logging
import secrets import secrets
import time import time
from typing import Any, cast from typing import Any, Optional, cast
from aiohttp import client, web from aiohttp import client, web
import async_timeout import async_timeout
@ -437,7 +437,10 @@ class OAuth2AuthorizeCallbackView(http.HomeAssistantView):
state = _decode_jwt(hass, request.query["state"]) state = _decode_jwt(hass, request.query["state"])
if state is None: if state is None:
return web.Response(text="Invalid state") return web.Response(
text="Invalid state. Is My Home Assistant configured to go to the right instance?",
status=400,
)
user_input: dict[str, Any] = {"state": state} user_input: dict[str, Any] = {"state": state}
@ -538,7 +541,10 @@ def _encode_jwt(hass: HomeAssistant, data: dict) -> str:
@callback @callback
def _decode_jwt(hass: HomeAssistant, encoded: str) -> dict | None: def _decode_jwt(hass: HomeAssistant, encoded: str) -> dict | None:
"""JWT encode data.""" """JWT encode data."""
secret = cast(str, hass.data.get(DATA_JWT_SECRET)) secret = cast(Optional[str], hass.data.get(DATA_JWT_SECRET))
if secret is None:
return None
try: try:
return jwt.decode(encoded, secret, algorithms=["HS256"]) return jwt.decode(encoded, secret, algorithms=["HS256"])

View File

@ -726,3 +726,10 @@ async def test_oauth_session_refresh_failure(
session = config_entry_oauth2_flow.OAuth2Session(hass, config_entry, local_impl) session = config_entry_oauth2_flow.OAuth2Session(hass, config_entry, local_impl)
with pytest.raises(aiohttp.client_exceptions.ClientResponseError): with pytest.raises(aiohttp.client_exceptions.ClientResponseError):
await session.async_request("post", "https://example.com") await session.async_request("post", "https://example.com")
async def test_oauth2_without_secret_init(local_impl, hass_client_no_auth):
"""Check authorize callback without secret initalizated."""
client = await hass_client_no_auth()
resp = await client.get("/auth/external/callback?code=abcd&state=qwer")
assert resp.status == 400