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

[websocket] Support token authentication through header #4515

Merged
merged 2 commits into from
Dec 31, 2024

Conversation

florian-h05
Copy link
Contributor

@florian-h05 florian-h05 commented Dec 22, 2024

Closes #4490.

Browser clients do not have the ability to set an Authorization header programatically on websocket requests. All they have control over is the URL and the websocket subprotocols sent (see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket)

This PR adds support for specifying a bearer token via a websocket subprotocol, with the format org.openhab.ws.accessToken.base64.<encoded-token>.

The browser must specify at least one other subprotocol since the server must echo a selected subprotocol back.
For this, org.openhab.ws.protocol.default is used.

<encoded-token> must be base64-encoded without padding, since bearer tokens can contain characters a websocket protocol may not (/ and =).

…ol header

Signed-off-by: Florian Hotze <dev@florianhotze.com>
@florian-h05 florian-h05 force-pushed the websocket-auth-header branch from 19ec9f0 to 164291a Compare December 22, 2024 14:07
@florian-h05 florian-h05 marked this pull request as ready for review December 22, 2024 14:08
@florian-h05 florian-h05 requested a review from a team as a code owner December 22, 2024 14:08
@florian-h05
Copy link
Contributor Author

@ghys This was easier to implement than I expected 👍

@ghys
Copy link
Member

ghys commented Dec 22, 2024

@florian-h05 you're underestimating yourself ;)

Signed-off-by: Florian Hotze <dev@florianhotze.com>
Copy link
Member

@holgerfriedrich holgerfriedrich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@holgerfriedrich holgerfriedrich merged commit 25ca43d into openhab:main Dec 31, 2024
3 checks passed
@holgerfriedrich holgerfriedrich added the enhancement An enhancement or new feature of the Core label Dec 31, 2024
@holgerfriedrich holgerfriedrich added this to the 5.0 milestone Dec 31, 2024
@florian-h05 florian-h05 deleted the websocket-auth-header branch December 31, 2024 16:01
florian-h05 added a commit to openhab/openhab-webui that referenced this pull request Dec 31, 2024
…h header (#2907)

Depens on openhab/openhab-core#4515.

This refactors the WebSocket connection code from #2884 to the `$oh`
namespace, same as it is for the SSE logic.
It also passes the access token as WebSocket subprotocol so it is sent
with the `Sec-WebSocket-Protocol` header.

---------

Signed-off-by: Florian Hotze <dev@florianhotze.com>
florian-h05 added a commit to florian-h05/openhab-docs that referenced this pull request Jan 4, 2025
Refs openhab/openhab-core#4515.

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
florian-h05 added a commit to florian-h05/openhab-docs that referenced this pull request Jan 4, 2025
Refs openhab/openhab-core#4515.

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
florian-h05 added a commit to florian-h05/openhab-docs that referenced this pull request Jan 4, 2025
Refs openhab/openhab-core#4515.

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
stefan-hoehn pushed a commit to openhab/openhab-docs that referenced this pull request Jan 5, 2025
…2442)

* WebSocket API: Add sending access token through sub-protocols header

Refs openhab/openhab-core#4515.

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>

* Update configuration/websocket.md

Signed-off-by: Florian Hotze <dev@florianhotze.com>

---------

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
Signed-off-by: Florian Hotze <dev@florianhotze.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement An enhancement or new feature of the Core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow passing the access token in Sec-WebSocket-Protocol header to authorize WebSocket connections
3 participants