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
218 stars 32 forks source link

Add Authorization header to media player requests #94

Closed KarolKsionek closed 2 years ago

KarolKsionek commented 2 years ago

When opening Media Player in HA we weren't passing Authorization data in requests for thumbnails and video itself, so requests couldn't be completed successfully.

roleoroleo commented 2 years ago

Thank you for your code.

dieideeistgut commented 2 years ago

FYI: This fix somehow breaks the ability to see thumbs and view/play videos. Reverting to views.py from 0.3.9. fixes this back to work for me.

roleoroleo commented 2 years ago

Do you mean thumb and video in Media section? It's working for me.

MrMarble commented 2 years ago

I get a 500 server error on the media folder: imagen

Trying to play a file I get a MIMEType error imagen

roleoroleo commented 2 years ago

Which version of ha?

aming0 commented 2 years ago

I get a 500 server error on the media folder: imagen

Trying to play a file I get a MIMEType error imagen

Same problem here Home Assistant 2022.9.7

HectorHumanes commented 2 years ago

I have the same error 500 and no file playing problem in HA 2022.9.6

As @dieideeistgut pointed, the problem is solved by restoring views.py from v0.3.9

roleoroleo commented 2 years ago

Tested now with my 2022.9.7 and I have no problems. Please add some details: cam (type and hack version), password/no password, etc...

dieideeistgut commented 2 years ago

I think this may not be directly related to yi-hack. It could as well be some reverse-proxy config. In my case i have HA behind a traefik proxy. So authentication params may not be routed to HA the way HA expect these. Just a thought. Will run some tests over the weekend.

MrMarble commented 2 years ago

Which version of ha?

Sorry, I was on vacation.

I just updated to HA 2022.10.0 and have the same problem. The camera is a Yi dome camera 1080p running https://github.com/roleoroleo/yi-hack-MStar

Hostname yi-hack
Firmware Version 0.4.7
Base Version 4.6.0.0A_201908271549
Model Suffix h201c
Serial Number BFUSY1EEAKQAD5200310

I think this may not be directly related to yi-hack. It could as well be some reverse-proxy config. In my case i have HA behind a traefik proxy. So authentication params may not be routed to HA the way HA expect these. Just a thought. Will run some tests over the weekend.

Hmm, that may be my case as well, I run HA on my Kubernetes cluster behind traefik, will look into that

MrMarble commented 2 years ago

Looking at the logs, I get this

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>

The HTTPBasicAuth is an object intended to be passed as .request(auth=HTTPBasicAuth(user,password) but is instead added to the headers of the request.

See https://requests.readthedocs.io/en/latest/user/authentication/ and https://docs.aiohttp.org/en/stable/client_reference.html#basic-api

aiohttp has its own authentication class aiohttp.BasicAuth, after that change everything was working.

imagen

I don't know how any of this worked in the first place, will open a PR with the change I made