roleoroleo / yi-hack_ha_integration

Home Assistant custom integration for Yi cameras: yi-hack-MStar, yi-hack-Allwinner, yi-hack-Allwinner-v2, yi-hack-v5 and sonoff-hack
GNU General Public License v3.0
205 stars 32 forks source link

Fix authentication #96

Closed MrMarble closed 1 year ago

MrMarble commented 1 year ago

PR #94 added Authorization headers to media player requests using requests.HTTPBasicAuth as a header, this class is intended to be used as a parameter to requests package https://requests.readthedocs.io/en/latest/user/authentication/#basic-authentication.

Looking at source, you can see it will not serialize to string automagically, it uses the __call__ method to mutate the requests object https://github.com/psf/requests/blob/main/requests/auth.py#L95

You could see this happening on HA:

Logger: aiohttp.server
Source: custom_components/yi_hack/views.py:105
Integration: Yi Cam con yi-hack (documentation, issues)
First occurred: 10:13:43 (64 occurrences)
Last logged: 10:22:31

Error handling request
Traceback (most recent call last):
  File "/usr/lib/python3.10/site-packages/aiohttp/web_protocol.py", line 435, in _handle_request
    resp = await request_handler(request)
  File "/usr/lib/python3.10/site-packages/aiohttp/web_app.py", line 504, in _handle
    resp = await handler(request)
  File "/usr/lib/python3.10/site-packages/aiohttp/web_middlewares.py", line 117, in impl
    return await handler(request)
  File "/usr/lib/python3.10/site-packages/homeassistant/components/http/security_filter.py", line 60, in security_filter_middleware
    return await handler(request)
  File "/usr/lib/python3.10/site-packages/homeassistant/components/http/forwarded.py", line 222, in forwarded_middleware
    return await handler(request)
  File "/usr/lib/python3.10/site-packages/homeassistant/components/http/request_context.py", line 28, in request_context_middleware
    return await handler(request)
  File "/usr/lib/python3.10/site-packages/homeassistant/components/http/ban.py", line 82, in ban_middleware
    return await handler(request)
  File "/usr/lib/python3.10/site-packages/homeassistant/components/http/auth.py", line 236, in auth_middleware
    return await handler(request)
  File "/usr/lib/python3.10/site-packages/homeassistant/components/http/view.py", line 136, in handle
    result = await result
  File "/config/custom_components/yi_hack/views.py", line 72, in get
    return await self._handle_request(request, **kwargs)
  File "/config/custom_components/yi_hack/views.py", line 105, in _handle_request
    async with self._websession.request(
  File "/usr/lib/python3.10/site-packages/aiohttp/client.py", line 1138, in __aenter__
    self._resp = await self._coro
  File "/usr/lib/python3.10/site-packages/aiohttp/client.py", line 557, in _request
    resp = await req.send(conn)
  File "/usr/lib/python3.10/site-packages/aiohttp/client_reqrep.py", line 669, in send
    await writer.write_headers(status_line, self.headers)
  File "/usr/lib/python3.10/site-packages/aiohttp/http_writer.py", line 130, in write_headers
    buf = _serialize_headers(status_line, headers)
  File "aiohttp/_http_writer.pyx", line 132, in aiohttp._http_writer._serialize_headers
  File "aiohttp/_http_writer.pyx", line 109, in aiohttp._http_writer.to_str
TypeError: Cannot serialize non-str key <requests.auth.HTTPBasicAuth object at 0x71ad4dd60bb0>

I changed this to use aiohttp.BasicAuth and pass the authentication to the request, instead of the headers https://docs.aiohttp.org/en/stable/client_reference.html#aiohttp.BasicAuth https://docs.aiohttp.org/en/stable/client_reference.html#aiohttp.ClientSession.request

Before this change, I was getting some errors as I commented here https://github.com/roleoroleo/yi-hack_ha_integration/pull/94#issuecomment-1257166096

dieideeistgut commented 1 year ago

I've been running this change since @MrMarble posted it. No hickups so far with HA behind traefik. Seems working sweetly. Goooood Job 🥇

roleoroleo commented 1 year ago

Thank you for your fix.