mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-04-19 18:57:16 +00:00
Fix new auth system (#633)
* Fix new auth system * Update exceptions.py * Update exceptions.py * Update homeassistant.py * Update homeassistant.py * Update homeassistant.py * Fix some API Errors * fix lint
This commit is contained in:
parent
398815efd8
commit
00e7d96472
@ -57,7 +57,7 @@ class APIProxy(CoreSysAttributes):
|
||||
yield resp
|
||||
return
|
||||
|
||||
except HomeAssistantAuthError:
|
||||
except HomeAssistantAPIError:
|
||||
_LOGGER.error("Authenticate error on API for request %s", path)
|
||||
except aiohttp.ClientError as err:
|
||||
_LOGGER.error("Client error on API %s request %s", path, err)
|
||||
@ -151,7 +151,7 @@ class APIProxy(CoreSysAttributes):
|
||||
_LOGGER.error(
|
||||
"Failed authentication to Home-Assistant websocket: %s", data)
|
||||
|
||||
except (RuntimeError, HomeAssistantAPIError) as err:
|
||||
except (RuntimeError, HomeAssistantAuthError, ValueError) as err:
|
||||
_LOGGER.error("Client error on websocket API %s.", err)
|
||||
|
||||
raise HTTPBadGateway()
|
||||
|
@ -1,7 +1,4 @@
|
||||
"""Core Exceptions."""
|
||||
import asyncio
|
||||
|
||||
import aiohttp
|
||||
|
||||
|
||||
class HassioError(Exception):
|
||||
@ -26,14 +23,13 @@ class HomeAssistantUpdateError(HomeAssistantError):
|
||||
pass
|
||||
|
||||
|
||||
class HomeAssistantAuthError(HomeAssistantError):
|
||||
"""Home Assistant Auth API exception."""
|
||||
class HomeAssistantAPIError(HomeAssistantError):
|
||||
"""Home Assistant API exception."""
|
||||
pass
|
||||
|
||||
|
||||
class HomeAssistantAPIError(
|
||||
HomeAssistantAuthError, asyncio.TimeoutError, aiohttp.ClientError):
|
||||
"""Home Assistant API exception."""
|
||||
class HomeAssistantAuthError(HomeAssistantAPIError):
|
||||
"""Home Assistant Auth API exception."""
|
||||
pass
|
||||
|
||||
|
||||
|
@ -355,7 +355,7 @@ class HomeAssistant(JsonConfig, CoreSysAttributes):
|
||||
return
|
||||
|
||||
with suppress(asyncio.TimeoutError, aiohttp.ClientError):
|
||||
async with self.sys_websession_ssl.get(
|
||||
async with self.sys_websession_ssl.post(
|
||||
f"{self.api_url}/auth/token",
|
||||
timeout=30,
|
||||
data={
|
||||
@ -363,15 +363,14 @@ class HomeAssistant(JsonConfig, CoreSysAttributes):
|
||||
"refresh_token": self.refresh_token
|
||||
}
|
||||
) as resp:
|
||||
if resp.status != 200:
|
||||
_LOGGER.error("Authenticate problem with HomeAssistant!")
|
||||
raise HomeAssistantAuthError()
|
||||
tokens = await resp.json()
|
||||
self.access_token = tokens['access_token']
|
||||
return
|
||||
if resp.status == 200:
|
||||
_LOGGER.info("Updated HomeAssistant API token")
|
||||
tokens = await resp.json()
|
||||
self.access_token = tokens['access_token']
|
||||
return
|
||||
|
||||
_LOGGER.error("Can't update HomeAssistant access token!")
|
||||
raise HomeAssistantAPIError()
|
||||
raise HomeAssistantAuthError()
|
||||
|
||||
@asynccontextmanager
|
||||
async def make_request(self, method, path, json=None, content_type=None,
|
||||
@ -394,15 +393,20 @@ class HomeAssistant(JsonConfig, CoreSysAttributes):
|
||||
await self.ensure_access_token()
|
||||
headers[hdrs.AUTHORIZATION] = f'Bearer {self.access_token}'
|
||||
|
||||
async with getattr(self.sys_websession_ssl, method)(
|
||||
url, data=data, timeout=timeout, json=json, headers=headers
|
||||
) as resp:
|
||||
# Access token expired
|
||||
if resp.status == 401 and self.refresh_token:
|
||||
self.access_token = None
|
||||
continue
|
||||
yield resp
|
||||
return
|
||||
try:
|
||||
async with getattr(self.sys_websession_ssl, method)(
|
||||
url, data=data, timeout=timeout, json=json,
|
||||
headers=headers
|
||||
) as resp:
|
||||
# Access token expired
|
||||
if resp.status == 401 and self.refresh_token:
|
||||
self.access_token = None
|
||||
continue
|
||||
yield resp
|
||||
return
|
||||
except (asyncio.TimeoutError, aiohttp.ClientError) as err:
|
||||
_LOGGER.error("Error on call %s: %s", url, err)
|
||||
break
|
||||
|
||||
raise HomeAssistantAPIError()
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
attr==0.3.1
|
||||
async_timeout==3.0.0
|
||||
aiohttp==3.3.2
|
||||
docker==3.4.0
|
||||
docker==3.4.1
|
||||
colorlog==3.1.2
|
||||
voluptuous==0.11.1
|
||||
gitpython==2.1.10
|
||||
|
Loading…
x
Reference in New Issue
Block a user