Fix token auth inconsistency and add native Bearer token support#508
Open
ulbi wants to merge 4 commits intospacemanspiff2007:masterfrom
Open
Fix token auth inconsistency and add native Bearer token support#508ulbi wants to merge 4 commits intospacemanspiff2007:masterfrom
ulbi wants to merge 4 commits intospacemanspiff2007:masterfrom
Conversation
Co-authored-by: ulbi <11133910+ulbi@users.noreply.github.com> Agent-Logs-Url: https://github.com/ulbi/HABApp/sessions/ef8a9084-1164-46b2-9607-4b51d63051df
…h warning Co-authored-by: ulbi <11133910+ulbi@users.noreply.github.com> Agent-Logs-Url: https://github.com/ulbi/HABApp/sessions/5557c952-97fb-4ed3-b13b-d607b47cce66
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
This PR addresses two related authentication issues that made it difficult to use openHAB API tokens with HABApp.
Issue – Bug: Token authentication failed when the token was placed in the
passwordfield. The detection logic (oh.prefix) was correct, butaiohttp.BasicAuthexpects the token as the username with a blank password — not the other way around. The result: REST calls returned 401, while the WebSocket stack (with its own_build_token) worked fine.Issue – Feature: For new deployments and security-conscious setups (token rotation, fine-grained access control), native support for the
Authorization: Bearer <token>header was missing — even though the openHAB REST API and WebSocket interface officially support it.What changed
New config field
tokenThe field was added to the Pydantic model (
config/models/openhab.py) and the generated default config snapshot was updated accordingly.Handler (
handler.py) — two layersExplicit Bearer path: If
tokenis set,on_setupbuilds theaiohttp.ClientSessionwithheaders={'Authorization': 'Bearer <token>'}instead ofBasicAuth. REST and WebSocket then share the same token channel.Automatic migration of legacy configs: If
tokenis empty butuserorpasswordcontain anoh.token, HABApp automatically promotes it to Bearer auth and emits a deprecation warning:This fully resolves Issue Dev #1 — without any breaking change for existing configurations.
WebSocket plugin (
websockets.py)The token is now derived directly from the
Authorizationheader of the active session, rather than being read separately from the config. This eliminates the inconsistency between session state and configuration.Tests (
tests/test_openhab/test_plugins/test_handler_auth.py)The test file was restructured from scratch:
TestBuildTokenWebsocketPlugin._build_token: token in login, token in password, BasicAuth fallbackTestTokenResolutionon_setup: explicit token takes precedence, migration fromuserfield, migration frompasswordfield, plain credentials without tokenBehaviour overview
token: oh.xyztokenempty,user: oh.xyztokenempty,password: oh.xyztoken: oh.xyz+user/passwordsetuser: admin,password: secretChecklist
passwordfield) fixed