fix(oauth): use dynamic HA instance URL for redirect_uri (closes #19)#37
Open
daboynb wants to merge 1 commit into
Open
fix(oauth): use dynamic HA instance URL for redirect_uri (closes #19)#37daboynb wants to merge 1 commit into
daboynb wants to merge 1 commit into
Conversation
OAUTH2_CALLBACK_URL was hardcoded to http://homeassistant.local:8123 which only resolves on HAOS via mDNS. HA Container, HAOS without mDNS, and instances behind a reverse proxy / Tailscale Funnel all break during OAuth setup with DNS_PROBE_POSSIBLE / 404 on the callback URL. Users currently work around it by manually pinning homeassistant.local in /etc/hosts on the OAuth-initiating client. Compute the redirect at runtime via homeassistant.helpers.network.get_url(), which is the upstream-recommended way to obtain the instance URL. Fall back to the previous hardcoded value if get_url() raises NoURLAvailableError (e.g. very early in startup). Closes Connectlife-LLC#19.
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.
Summary
Replace the hardcoded OAuth redirect URL with a runtime-computed value based on the actual Home Assistant instance URL.
Currently
oauth2.py:17hardcodes:homeassistant.localonly resolves on HAOS via mDNS. On HA Container, HAOS without mDNS, or instances behind a reverse proxy / Tailscale Funnel the OAuth setup flow lands on a callback URL the browser can't reach (DNS_PROBE_POSSIBLE/ 404). Users currently work around it by manually pinninghomeassistant.localin/etc/hostson the OAuth-initiating client.This PR derives the redirect from
homeassistant.helpers.network.get_url(), the upstream-recommended way to obtain the HA instance URL with proper internal/external precedence. Falls back to the previous hardcoded value only ifget_url()raisesNoURLAvailableError(e.g. very early in startup).Closes
Lemonezzz, 2025-12-07) — same symptom verbatim: "When using Google authorization, it transfers to an address like: http://homeassistant.local:8123/auth/external/callback?code=... if you manually remove the .local from the link, then it says Completed".Compatibility test (existing tokens)
The Hisense OAuth backend was tested live against
https://oauth.hijuconn.com/oauth/tokenwith an existing user's refresh_token (issued previously with the hardcodedhomeassistant.localredirect_uri), to verify the change does not break already-authenticated users:grant_type=refresh_tokenrequestredirect_uri=http://homeassistant.local:8123/...redirect_uriredirect_uri(http://192.168.1.4:8123/...)Additionally, after a successful refresh the original refresh_token remained valid on subsequent calls (server is not single-use), so existing installations whose tokens were issued under the old hardcoded URL keep working as
redirect_urichanges value across refreshes — the Hisense backend does not enforce a match against the authorize-time URL on the refresh grant.Diff
Test plan
ast.parse) passeshttps://oauth.hijuconn.com/oauth/token(see table above)homeassistant/helpers/network.py/etc/hostsworkaround) — to be verified by maintainer / community on next release