Skip to content

MyUplink: authorization-code OAuth + in-app consent + heat-pump dashboard - #497

Merged
frahlg merged 11 commits into
masterfrom
496-myuplink-oauth-heating
Jun 20, 2026
Merged

frahlg merged 11 commits into
masterfrom
496-myuplink-oauth-heating

Conversation

@frahlg

@frahlg frahlg commented Jun 20, 2026 •

Copy link
Copy Markdown
Member

Why

A Discord tester (and soon Fredrik) hit invalid_client when setting up the MyUplink/NIBE heat-pump driver (#496). Root cause: the driver used the client_credentials OAuth grant, but the MyUplink developer portal only issues authorization-code apps (Callback URL + Client Identifier + Client Secret, Azure B2C). client_credentials is simply not offered, so a normal portal app fails at the token request before any polling starts.

Separately, the heat-pump telemetry the driver already emits (hp_*) had no UI — it only showed as raw live metrics in the per-driver Diagnose modal. And the earlier troubleshooting guidance about a mode: hotwater | heating config was wrong: the driver has no mode field.

What

Auth (closes #496) — authorization-code + refresh-token, with in-app consent:

  • drivers/myuplink.lua now runs grant_type=refresh_token at runtime and persists Azure-B2C-rotated tokens. With no token yet it idles quietly ("awaiting OAuth connect") instead of erroring.
  • New Go bootstrap: GET /api/oauth/myuplink/start (mints state, returns the authorize URL + the exact callback URL to register) and GET /api/oauth/myuplink/callback (exchanges the code, stores the refresh token, restarts the driver, renders a result page).
  • New generic host.persist_secret(key, value) capability: drivers durably store a rotated secret in the unwatched state KV (not config.yaml, which would trigger a config-reload restart loop). SecretOverride layers it back over config at driver_init, so the freshest token survives a restart while config.yaml keeps the bootstrap seed the UI shows as "saved".
  • Settings → Devices gains a Callback-URL hint, a Connect to MyUplink button, and a Connected/Not-connected badge. The browser passes its own location.origin callback (validated server-side) so the flow works over a relay origin too.

Heating UI:

  • New Heat pump dashboard card: compressor power + hot-water/indoor/outdoor temperatures + a 24h power sparkline (reuses /api/series). Hidden until a driver reports hp_power_w.

Docs: docs/myuplink-oauth.md setup walkthrough (incl. the HTTPS-callback caveat and an explicit "there is no mode field" correction); host.persist_secret documented.

Testing

  • Unit tests: host.persist_secret (granted + not-granted), driver refresh-token flow + rotation persistence + not-connected idle, OAuth start/callback (state lifecycle, code exchange, config+KV persistence, redirect_uri validation/rejection). make verify green.
  • Manual end-to-end against a running instance with a stub heat pump: /api/drivers/{name} live metrics, /api/series sparkline data, /api/oauth/myuplink/start authorize URL + 400 on a bad redirect_uri, and the rendered Heat pump dashboard card (screenshot-verified, on-theme).

🤖 Generated with Claude Code

Onboarding reachability (added)

The automatic OAuth callback can't reach the Pi on every origin: the relay/home route refuses /api/* (owner data stays P2P), and the MyUplink portal may reject a non-HTTPS LAN callback. So consent also has a manual fallback: POST /api/oauth/myuplink/exchange — the operator pastes the redirected URL (carrying code + state) and the Pi exchanges the code over its own outbound HTTPS. No inbound callback is needed, so onboarding works on relay, headless, and http-LAN origins. Both paths share completeMyUplinkConsent; consent-state TTL is 15 min.

Verified live: /api/oauth/myuplink/exchange reaches the real token endpoint with a valid minted state (returns invalid_client only because the test creds are fake — the happy path is unit-tested against a stub).

frahlg and others added 8 commits June 20, 2026 15:57
…okens

Drivers can now durably persist a rotated secret (e.g. an OAuth
refresh_token) via host.persist_secret(key, value). The registry routes
it to the unwatched state KV — never config.yaml — so a token rotation
can't trigger a configreload restart loop. SecretOverride layers the KV
value back over config.yaml at driver_init so the freshest token
survives a restart while config.yaml keeps the bootstrap seed.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
The MyUplink developer portal issues authorization-code apps, not
client_credentials, so the old grant returned invalid_client before any
polling started. The driver now runs grant_type=refresh_token at runtime
using a refresh_token obtained via the in-app OAuth connect flow, and
persists the rotated token via host.persist_secret. With no refresh_token
configured it idles quietly (awaiting connect) instead of erroring.

There is no `mode` field — corrects earlier misinformation; this is
read-only telemetry for one physical pump.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
GET /api/oauth/myuplink/start mints a single-use state and returns the
MyUplink authorize URL plus the exact callback URL to register in the
portal. GET /api/oauth/myuplink/callback exchanges the code for a
refresh_token, persists it to the driver config (masked) and the
unwatched state KV (so SecretOverride supersedes any stale rotated
value), restarts the driver, and renders a standalone result page.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
On a remote/relay origin the request Host the server sees is not the
browser's origin, so a server-derived redirect_uri would be wrong. The
browser now passes location.origin's callback URL; the server validates
it (absolute http(s), exact callback path, no query/fragment) to prevent
turning the authorize state into an open redirect, and falls back to the
request origin for plain LAN access.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
The apicreds device card now shows the exact Callback URL to register in
the MyUplink portal, a Connect to MyUplink button, and a Connected/Not
connected badge driven by whether a refresh_token is saved. Connect calls
/api/oauth/myuplink/start (passing the browser's own callback URL) and
opens the consent in a new tab. Also corrects the stale client_credentials
copy to authorization-code.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Adds a Heat pump card to the dashboard that surfaces the MyUplink
driver's hp_* metrics (compressor power + hot-water/indoor/outdoor
temperatures) plus a 24h compressor-power sparkline from /api/series.
The section stays hidden until a driver reports hp_power_w; discovery
runs once and steady-state polling only touches heat-pump drivers.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

frahlg and others added 3 commits June 20, 2026 16:32
The auto-callback can't reach the Pi on every origin: the relay/home
route refuses /api/*, and the MyUplink portal may reject a non-HTTPS LAN
callback. Add POST /api/oauth/myuplink/exchange — the operator pastes the
redirected URL (carrying code + state) and the Pi exchanges the code over
its own outbound HTTPS, so onboarding works on relay, headless, and http
LAN origins with no inbound callback. Shared consent-completion logic
(completeMyUplinkConsent) backs both the GET callback and the POST
exchange; consent-state TTL bumped to 15 min for the paste path. Settings
→ Devices gains a 'Not redirected back? Paste the URL' fallback.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Always send a code_challenge on /authorize and the code_verifier on
token exchange (RFC 7636). Harmless if MyUplink ignores PKCE, required if
its Azure B2C app enforces it — defensive so the consent flow doesn't
fail at the authorize step. The verifier is bound to the pending state
and never leaves the Pi.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@frahlg
frahlg merged commit 48f972b into master Jun 20, 2026
2 checks passed
@frahlg
frahlg deleted the 496-myuplink-oauth-heating branch June 20, 2026 15:29
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.

MyUplink driver uses unsupported client_credentials OAuth flow

1 participant