From 1575df8a58a48252e86a83aa13de02a79cd562cd Mon Sep 17 00:00:00 2001 From: Alvaro Tinoco Date: Thu, 6 Oct 2022 10:53:40 +0200 Subject: [PATCH] fix authentication --- custom_components/yi_hack/views.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/custom_components/yi_hack/views.py b/custom_components/yi_hack/views.py index 9231114..b926e3d 100644 --- a/custom_components/yi_hack/views.py +++ b/custom_components/yi_hack/views.py @@ -8,12 +8,10 @@ from typing import Any import aiohttp -from aiohttp import hdrs, web +from aiohttp import hdrs, web, BasicAuth from aiohttp.web_exceptions import HTTPBadGateway, HTTPUnauthorized from multidict import CIMultiDict -from requests.auth import HTTPBasicAuth - from homeassistant.const import ( CONF_HOST, CONF_NAME, @@ -100,7 +98,11 @@ async def _handle_request( url = "http://" + host + ":" + str(port) + "/" + full_path data = await request.read() - source_header = _init_header(request, user, password) + source_header = _init_header(request) + + auth = None + if user or password: + auth = BasicAuth(user, password) async with self._websession.request( request.method, @@ -109,6 +111,7 @@ async def _handle_request( params=request.query, allow_redirects=False, data=data, + auth=auth, ) as result: headers = _response_header(result) @@ -130,11 +133,7 @@ async def _handle_request( return response -def _init_header( - request: web.Request, - user: str, - password: str, -) -> CIMultiDict | dict[str, str]: +def _init_header(request: web.Request) -> CIMultiDict | dict[str, str]: """Create initial header.""" headers = {} @@ -173,9 +172,6 @@ def _init_header( if not forward_proto: forward_proto = request.url.scheme headers[hdrs.X_FORWARDED_PROTO] = forward_proto - - if user or password: - headers[hdrs.AUTHORIZATION] = HTTPBasicAuth(user, password) return headers