Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Authorization header to media player requests #94

Merged
merged 2 commits into from Sep 2, 2022

Conversation

KarolKsionek
Copy link
Contributor

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.

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
Copy link
Owner

Thank you for your code.

@roleoroleo roleoroleo merged commit da5fc0f into roleoroleo:main Sep 2, 2022
@dieideeistgut
Copy link

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
Copy link
Owner

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

@MrMarble
Copy link
Contributor

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

Trying to play a file I get a MIMEType error
imagen

@roleoroleo
Copy link
Owner

Which version of ha?

@aming0
Copy link

aming0 commented Oct 4, 2022

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
Copy link

HectorHumanes commented Oct 5, 2022

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
Copy link
Owner

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
Copy link

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
Copy link
Contributor

MrMarble commented Oct 6, 2022

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
Copy link
Contributor

MrMarble commented Oct 6, 2022

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

@MrMarble MrMarble mentioned this pull request Oct 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants