Background credential refresher + RFC 8707 resource on token requests (0.1.1-dev.2) #106

Merged
john merged 1 commit from feat/credential-refresher into main 2026-11-26 01:14:56 +00:00
Owner

v0.1.1-dev.2

Linked MCP accounts stay signed in, and public OAuth clients such as PocketSmith's hosted MCP work end to end.

  • Background credential refresher (generic). A new corn.refresher.CredentialRefresher sweeps registered credential sources on an interval and renews grants before they expire. Linked MCP accounts (CORN_UPSTREAM_MCP_SERVERS) are the first source; other credential families can register their own sweep(min_validity). Only one replica sweeps at a time (database lease credential-refresher). On-demand refresh at call time is unchanged and remains the correctness path.
    • CORN_REFRESH_INTERVAL_SECONDS (default 1800, 0 disables)
    • CORN_REFRESH_MIN_VALIDITY_SECONDS (default 2400): grants with less remaining lifetime are refreshed. With the defaults, a one-hour token is renewed before it can expire between two sweeps.
  • RFC 8707 resource on every token request. When an upstream sets resource, Corn now sends it on the authorization-code exchange and on refreshes too, not only on the authorization request, as the MCP authorization spec requires.
  • Transient refresh failures keep the grant. Network errors, 429 and 5xx from the token endpoint no longer delete the user's linked account; the current token is used while still valid and the tool error says to retry. Only a rejected refresh token (4xx) disconnects.
  • Grants without a refresh token are used until they expire instead of being deleted early, and the Connections page shows whether each linked account renews automatically.

No configuration change is required; the refresher starts with the defaults.

Why

Linking PocketSmith's hosted MCP (read-write) per user through Corn. PocketSmith is a public OAuth client (DCR, token_endpoint_auth_method: none), requires PKCE S256 and uses one-hour access tokens. Without a background refresh, an idle user's grant could lapse; and a temporary token-endpoint 5xx used to delete the grant.

Tests

tests/test_credential_refresher.py: resource on authorize/code/refresh, public-client refresh body, sweep window selection across users, upstream down keeps grant, rejected refresh disconnects, no-refresh-token grant used until expiry, refresher source isolation + cross-replica lease skip, disabled modes. Full suite: 537 passed.

# v0.1.1-dev.2 Linked MCP accounts stay signed in, and public OAuth clients such as PocketSmith's hosted MCP work end to end. - **Background credential refresher (generic).** A new `corn.refresher.CredentialRefresher` sweeps registered credential sources on an interval and renews grants before they expire. Linked MCP accounts (`CORN_UPSTREAM_MCP_SERVERS`) are the first source; other credential families can register their own `sweep(min_validity)`. Only one replica sweeps at a time (database lease `credential-refresher`). On-demand refresh at call time is unchanged and remains the correctness path. - `CORN_REFRESH_INTERVAL_SECONDS` (default `1800`, `0` disables) - `CORN_REFRESH_MIN_VALIDITY_SECONDS` (default `2400`): grants with less remaining lifetime are refreshed. With the defaults, a one-hour token is renewed before it can expire between two sweeps. - **RFC 8707 `resource` on every token request.** When an upstream sets `resource`, Corn now sends it on the authorization-code exchange and on refreshes too, not only on the authorization request, as the MCP authorization spec requires. - **Transient refresh failures keep the grant.** Network errors, `429` and `5xx` from the token endpoint no longer delete the user's linked account; the current token is used while still valid and the tool error says to retry. Only a rejected refresh token (`4xx`) disconnects. - **Grants without a refresh token** are used until they expire instead of being deleted early, and the Connections page shows whether each linked account renews automatically. No configuration change is required; the refresher starts with the defaults. ## Why Linking PocketSmith's hosted MCP (read-write) per user through Corn. PocketSmith is a public OAuth client (DCR, `token_endpoint_auth_method: none`), requires PKCE S256 and uses one-hour access tokens. Without a background refresh, an idle user's grant could lapse; and a temporary token-endpoint 5xx used to delete the grant. ## Tests `tests/test_credential_refresher.py`: resource on authorize/code/refresh, public-client refresh body, sweep window selection across users, upstream down keeps grant, rejected refresh disconnects, no-refresh-token grant used until expiry, refresher source isolation + cross-replica lease skip, disabled modes. Full suite: 537 passed.
feat(upstream): generic background credential refresher; RFC 8707 resource on token requests (0.1.1-dev.2)
All checks were successful
Corn CI / host (pull_request) Successful in 14s
Corn CI / release-check-1 (pull_request) Successful in 14s
Corn CI / release-check (pull_request) Successful in 0s
Security / scan (pull_request) Successful in 32s
Security / security (pull_request) Successful in 0s
Corn CI / test (pull_request) Successful in 2m54s
Corn CI / release (pull_request) Has been skipped
Review / change impact (pull_request_target) Successful in 3m26s
e6a443911c
Add corn.refresher.CredentialRefresher: sweeps registered sources every
CORN_REFRESH_INTERVAL_SECONDS (default 1800, 0 disables) and renews grants with
less than CORN_REFRESH_MIN_VALIDITY_SECONDS (default 2400) left. A database
lease keeps it to one replica per sweep. Linked MCP accounts are the first
source (UpstreamMcpService.sweep).

Send resource (RFC 8707) on the code exchange and refreshes, not only on the
authorization request. Treat network errors, 429 and 5xx from the token
endpoint as temporary: keep the grant instead of disconnecting. Use grants
without a refresh token until they expire. Show 'renews automatically' on the
Connections page. Document a PocketSmith hosted-MCP example.

Change review · 56e682199485 → e6a443911c59

Call paths changed by this PR

Caller → changed symbol → callee. Each edge labels the line in the caller. One detected entry path (up to 3 hops) is included when found. Static evidence, not a runtime stack trace.

After

flowchart LR
    n0["Settings.from_env - modified"]
    n1["_csv"]
    n2["_env"]
    n3["_seconds"]
    n4["ConnectionService.user_ids_with_provider - added"]
    n5["RefreshSource.sweep - added"]
    n6["SweepReport.as_dict - added"]
    n7["CredentialRefresher.__init__ - added"]
    n8["CredentialRefresher.register - added"]
    n0 -->|L321| n1
    n0 -->|L297| n2
    n0 -->|L324| n3

Changed symbols:

  • modified Settings.from_env — src/corn/config.py:296
  • added ConnectionService.user_ids_with_provider — src/corn/credentials.py:132
  • added RefreshSource.sweep — src/corn/refresher.py:32
  • added SweepReport.as_dict — src/corn/refresher.py:42
  • added CredentialRefresher.__init__ — src/corn/refresher.py:54
  • added CredentialRefresher.register — src/corn/refresher.py:73
    Bounded view: 45 more changed symbols and 1 neighbour references omitted.
Overall architecture after this change

Source: e6a443911c59 · code-review 0.1.0-dev.1.

Generated from resolved static calls between reviewed code areas. Numbers are call sites, not request counts.
Framework registration, dynamic dispatch, and unresolved calls are not shown. This is not a deployment map.
Showing 6 of 6 inter-area edges. Edit the reviewed source instead of this generated page.

flowchart LR
    n1["Console"]
    n2["Identity"]
    n3["NSW integrations"]
    n4["Persistence"]
    n5["REST handlers"]
    n6["Server composition"]
    n7["Shared contracts"]
    n8["Shopping"]
    n4 -->|"9"| n2
    n6 -->|"5"| n5
    n6 -->|"2"| n7
    n1 -->|"1"| n3
    n1 -->|"1"| n8
    n6 -->|"1"| n2

This diagram is for the linked commit. It refreshes after required checks pass for a new head.

<!-- john/release:architecture-review:v1 --> # Change review · `56e682199485` → [`e6a443911c59`](https://code.yawn.dev/john/corn/commit/e6a443911c5985f4284fad8dc1b1598cba9d43f6) ## Call paths changed by this PR Caller → changed symbol → callee. Each edge labels the line in the caller. One detected entry path (up to 3 hops) is included when found. Static evidence, not a runtime stack trace. ### After ```mermaid flowchart LR n0["Settings.from_env - modified"] n1["_csv"] n2["_env"] n3["_seconds"] n4["ConnectionService.user_ids_with_provider - added"] n5["RefreshSource.sweep - added"] n6["SweepReport.as_dict - added"] n7["CredentialRefresher.__init__ - added"] n8["CredentialRefresher.register - added"] n0 -->|L321| n1 n0 -->|L297| n2 n0 -->|L324| n3 ``` Changed symbols: - **modified** `Settings.from_env` — `src/corn/config.py:296` - **added** `ConnectionService.user_ids_with_provider` — `src/corn/credentials.py:132` - **added** `RefreshSource.sweep` — `src/corn/refresher.py:32` - **added** `SweepReport.as_dict` — `src/corn/refresher.py:42` - **added** `CredentialRefresher.__init__` — `src/corn/refresher.py:54` - **added** `CredentialRefresher.register` — `src/corn/refresher.py:73` Bounded view: 45 more changed symbols and 1 neighbour references omitted. <details><summary>Overall architecture after this change</summary> Source: [e6a443911c59](https://code.yawn.dev/john/corn/commit/e6a443911c5985f4284fad8dc1b1598cba9d43f6) · `code-review 0.1.0-dev.1`. Generated from resolved static calls between reviewed code areas. Numbers are call sites, not request counts. Framework registration, dynamic dispatch, and unresolved calls are not shown. This is not a deployment map. Showing 6 of 6 inter-area edges. Edit the reviewed source instead of this generated page. ```mermaid flowchart LR n1["Console"] n2["Identity"] n3["NSW integrations"] n4["Persistence"] n5["REST handlers"] n6["Server composition"] n7["Shared contracts"] n8["Shopping"] n4 -->|"9"| n2 n6 -->|"5"| n5 n6 -->|"2"| n7 n1 -->|"1"| n3 n1 -->|"1"| n8 n6 -->|"1"| n2 ``` </details> This diagram is for the linked commit. It refreshes after required checks pass for a new head.
john merged commit 7993f82030 into main 2026-11-26 01:14:56 +00:00
john deleted branch feat/credential-refresher 2026-11-26 01:14:56 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
john/corn!106
No description provided.