Skip to content

Commit

Permalink
Merge pull request #96 from MrMarble/main
Browse files Browse the repository at this point in the history
Fix authentication
  • Loading branch information
roleoroleo authored Oct 7, 2022
2 parents 201bd1b + 1575df8 commit f7a245b
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions custom_components/yi_hack/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -109,6 +111,7 @@ async def _handle_request(
params=request.query,
allow_redirects=False,
data=data,
auth=auth,
) as result:
headers = _response_header(result)

Expand All @@ -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 = {}

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit f7a245b

Please sign in to comment.