Skip to content

feat(api): keep an OAuth client connected with refresh tokens, and serve clients that hold no secret - #3495

Merged
fforootd merged 10 commits into
claude/oauth-person-sign-infrom
claude/oauth-refresh-tokens
Dec 1, 2026
Merged

fforootd merged 10 commits into
claude/oauth-person-sign-infrom
claude/oauth-refresh-tokens

Conversation

@fforootd

@fforootd fforootd commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Context

Summary

A client of the platform project's issuer keeps its grant alive with refresh tokens, and the endpoints serve a client that holds no secret. Nothing changes for a server that leaves oauth.enabled off, which is the default.

Refresh tokens

  • Asked for with offline_access. A client whose grant_types list refresh_token asks for the scope, the consent step shows it ("Keep access until your session ends"), and the code is exchanged for a refresh token beside the access token. The metadata lists the scope and the grant, since clients ask for it only then.
  • Rotated on every use. grant_type=refresh_token answers a new access token and the refresh token that replaces the one presented. The token that was just replaced is answered once more for a minute, with the token that replaced it: clients repeat a refresh after a lost response, and processes that share a grant refresh at the same moment. Any older token ends the grant for every holder.
  • One row per grant. refresh_grants holds the id of the current token and of the one it replaced. What the grant is for is sealed in the token with the project's token key, and read as a refresh token only when that key sealed it as one: a session token is none, and a refresh token is no bearer for the API.
  • A grant ends with the session it was given in and with its user. While its issuer's authorization server is off, no refresh is answered; the grant is not deleted then, so it is answered again if the server is turned back on before the session ends. Whatever refused it, the client reads invalid_grant.
  • A refresh resolves the newest deployment to the issuer of its token (ADR 081 §10), which is where a declared client's secret is checked.
  • resource is checked at the token endpoint. A token request may name the resource of its grant again and no other (invalid_target).

Clients that hold no secret

  • A public client (type: public) authenticates with none: it exchanges its code and refreshes with its client ID alone, and the metadata lists none among the authentication methods.
  • PKCE is required of it, and it returns to a loopback address on any port (RFC 8252).
  • The token endpoint tells nobody which client IDs exist. A request without a secret gets the same answer for an unknown client and for one that must authenticate.

Validation

Run in apps/api unless noted:

  • go vet ./..., and with -tags sqlite_integration on the integration packages
  • go test ./... -count=1
  • go test -tags sqlite_integration ./internal/api/integration_test/... ./internal/storage/stmttest/... -count=1
  • Against a local PostgreSQL 16 (ZITADEL_TEST_POSTGRES_URL): go test -tags postgres_integration ./internal/api/integration_test/ ./internal/storage/stmttest/ -count=1
  • node scripts/go-lint.mjs, turbo run check-generate openapi --filter=@zitadel/server, node scripts/check-adrs-index.mjs and pnpm run check-openapi-rules (repo root)
  • turbo run lint typecheck --filter=@zitadel/components, and the components' unit project (repo root)
  • CI's full-pr passed on this PR's last commit while it was part of feat(api): serve an OAuth client that presents its client ID metadata document #3488.

TestOAuthRefreshKeepsAGrantAlive runs it over HTTP from end to end: rotation, a repeated token, a reused one, a session that ends.

The browser run that exercised refresh tokens used a client with a metadata document; it is described in #3488.

Not run: the Spanner lane locally (it has the new refresh_grants migration and statements), and the e2e lanes.

After an automated review, one more commit, which changes ADR 092 only: it said a grant ends when its issuer is turned off, and the code refuses the refresh without deleting the grant.

Release notes / changeset

  • Changeset: .changeset/oauth-refresh-tokens.md — the OAuth server issues and rotates refresh tokens (@zitadel/server, @zitadel/api, @zitadel/components)

Notes

  • Breaking. The token endpoint refuses a resource that is not its grant's, where it ignored the parameter.
  • Migration. refresh_grants is new (Postgres 000062, SQLite 000049, Spanner 000066). feat: add public OAuth clients with origins and private-use redirects #3409 may claim the same numbers; whichever merges later renumbers.
  • The first commit amends ADR 082 for the whole stack. It also records that the platform's issuer admits any client document by default and that a policy narrows it, which feat(api): serve an OAuth client that presents its client ID metadata document #3488 builds. ADR 092 §8 lists what is built at each step.
  • A grant lasts as long as its session. A session lasts a day by default and using it does not extend it, so a connected agent asks the person to sign in again every day. session.default_ttl moves that for every session.
  • Open from the review, not changed: a retry within the grace minute narrows the scopes of the token it presents, not of the one that replaced it, because the grant's row holds no scopes. A scope that left the API's defined scopes and returned within that minute would come back. Storing the scopes on the row is a schema change in three dialects.
  • Decided: turning the issuer off pauses its grants and does not delete them. A server started once without the setting would otherwise sign every client out for good. Signing out ends a grant, as before.
  • A replayed code still revokes nothing. A refresh grant does not record the code it came from.
  • Public clients for applications are in feat: add public OAuth clients with origins and private-use redirects #3409. This PR serves a client without a secret at the endpoints, with the same shape (type: public, none, PKCE required), and leaves application.json at confidential. The two will conflict in oauth/client.go, oauth/token.go and domain/application.go, in lines that say the same.
  • New error codes that reach no API operation: authreq.resource_mismatch, refresh.token_invalid, refresh.scope_not_granted. The first is also a new reason of auth.request.exchange.failed.
  • Events. auth.token.issued and auth.token.revoked also record a refresh, a grant that starts and a grant that ends, the last two with the entity refresh_grant (docs/design/api/events-catalog.md).

https://claude.ai/code/session_0137eGmVSt2DgYJj2CjZbi1U


Generated by Claude Code

…and narrow it by policy

Decided on 2026-12-01. ADR 082 §9 kept both registration paths off by
default and asked for a login-only issuer. For the platform project's
issuer, which is what an agent connects to, a client that presents a client
ID metadata document is now admitted by default, and an operation policy
narrows which. §9 lists what no policy switches off for a client nobody
vouched for: consent on every request, both hosts on the consent screen,
tokens and refresh tokens that end with the session, and a fetch held to
public addresses. A customer project stays opt-in, and dynamic registration
still needs a login-only issuer.

ADR 091 §8 lists what a client still needs, from what today's clients
require: client documents, clients without a secret, refresh tokens, iss on
every answer, and the cloud's routes.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0137eGmVSt2DgYJj2CjZbi1U
…ld no secret

A request that is granted offline_access is answered with a refresh token
beside the access token. The scope is granted to a client whose grant_types
list refresh_token, and the issuer's metadata lists it, with the refresh
token grant and the `none` authentication method.

A grant keeps one row for all its refresh tokens: the id of the current
token and of the one it replaced. The token a rotation replaced is answered
once more for a minute, with the token that replaced it; any older one ends
the grant. A grant ends with the session it was given in, with its user,
and when its issuer's authorization server is off. What the grant is for is
sealed in the token with the project's token key, and read as a refresh
token only when that key sealed it as one.

A refresh resolves the newest deployment to the issuer of its token, which
is where a declared client's secret is checked. A public client must send a
PKCE challenge, returns to a loopback address on any port, and exchanges
its code and refreshes with its client ID alone. A token request may name
the resource of its grant again and no other.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0137eGmVSt2DgYJj2CjZbi1U
…r changeset

ADR 037 gains the minute in which a replaced refresh token is answered
again, and the one row a grant keeps. ADR 082 §7 says what a refresh
checks. ADR 091 §8 moves refresh tokens and secretless clients to what is
built, and names what still keeps a client out.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0137eGmVSt2DgYJj2CjZbi1U
@vercel

vercel Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
cloud Ready Ready Preview Dec 1, 2026 6:03pm UTC

Request Review

@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

🦋 Changeset detected

Latest commit: 645b980

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 27 packages
Name Type
@zitadel/server Minor
@zitadel/api Minor
@zitadel/components Minor
@zitadel/cli Minor
@zitadel/config Minor
@zitadel/sdk-angular Minor
@zitadel/sdk-core Minor
@zitadel/sdk-next Minor
@zitadel/sdk-nuxt Minor
@zitadel/sdk-qwik Minor
@zitadel/sdk-react Minor
@zitadel/sdk-solid Minor
@zitadel/sdk-svelte Minor
@zitadel/sdk-tanstack-start Minor
@zitadel/sdk-vue Minor
@zitadel/testing Minor
@zitadel/cloud Patch
@zitadel/client-sdk-contract Patch
@zitadel/sdk-qwik-router Minor
@zitadel/sdk-solid-start Minor
@zitadel/sdk-sveltekit Minor
@zitadel/server-sdk-contract Patch
@zitadel/server-darwin-arm64 Minor
@zitadel/server-darwin-x64 Minor
@zitadel/server-linux-arm64 Minor
@zitadel/server-linux-x64 Minor
@zitadel/server-win32-x64 Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
🔒 Security Review ✅ Completed 2026-12-01T06:46:45.826348Z 78e065f Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Grace retries can restore scopes dropped during refresh-token rotation, while disabled-issuer refresh rejection leaves grants reusable after re-enablement instead of revoking them.

2 open issues
What changed in this PR

This PR extends the OAuth sign-in flow with session-bound refresh tokens and endpoint support for clients without secrets, preparing the platform’s MCP authorization flow for persistent connections.

Changes:

  • Adds refresh-token issuance, rotation, retry grace, revocation, and storage across PostgreSQL, SQLite, and Spanner.
  • Supports public clients with mandatory PKCE, advertises OAuth capabilities, and validates token-request resources.
  • Updates consent translations, audit contracts, generated clients, documentation, and tests.
File Description
packages/​go/​api/​oas_validators_gen.go Validates the new resource-mismatch reason.
packages/​go/​api/​oas_schemas_gen.go Updates generated event types and comments.
packages/​go/​api/​oas_json_gen.go Decodes the new event reason.
packages/​components/​src/​orchestrator/​locales/​it.ts Adds Italian offline-access consent text.
packages/​components/​src/​orchestrator/​locales/​en.ts Adds English offline-access consent text.
packages/​components/​src/​orchestrator/​locales/​de.ts Adds German offline-access consent text.
docs/​design/​api/​events-catalog.md Documents refresh-grant audit events.
docs/​adrs/​092-mcp-server.md Updates MCP authorization implementation status.
docs/​adrs/​082-oauth-and-oidc-server.md Records refresh and client-admission decisions.
docs/​adrs/​047-dialect-id-generation.md Registers the refresh-grant ID prefix.
docs/​adrs/​037-token-lifecycle.md Defines rotation grace and grant storage.
apps/​docs/​content/​docs/​guides/​enable-oauth.mdx Updates the discovery document example.
apps/​api/​internal/​storage/​stmttest/​schema_nullability_test.go Checks refresh-grant column nullability.
apps/​api/​internal/​storage/​stmttest/​refresh_grant_test.go Tests grant persistence across dialects.
apps/​api/​internal/​storage/​dialect/​sqlite/​statement.go Registers SQLite grant statements.
apps/​api/​internal/​storage/​dialect/​sqlite/​refresh_grant.go Implements SQLite grant persistence.
apps/​api/​internal/​storage/​dialect/​sqlite/​migration/​sql/​000049_refresh_grants.sql Creates SQLite grant storage.
apps/​api/​internal/​storage/​dialect/​spanner/​statement.go Registers Spanner grant statements.
apps/​api/​internal/​storage/​dialect/​spanner/​refresh_grant.go Implements Spanner grant persistence.
apps/​api/​internal/​storage/​dialect/​spanner/​migration/​sql/​000066_refresh_grants.sql Creates Spanner grant storage.
apps/​api/​internal/​storage/​dialect/​postgres/​statement.go Registers PostgreSQL grant statements.
apps/​api/​internal/​storage/​dialect/​postgres/​refresh_grant.go Implements PostgreSQL grant persistence.
apps/​api/​internal/​storage/​dialect/​postgres/​migration/​sql/​000062_refresh_grants.sql Creates PostgreSQL grant storage.
apps/​api/​internal/​service/​statement.go Defines refresh-grant storage operations.
apps/​api/​internal/​service/​session.go Deletes refresh grants on session revocation.
apps/​api/​internal/​service/​refresh_grant.go Implements refresh lifecycle and token issuance.
apps/​api/​internal/​service/​mocks/​service.mock.go Adds generated grant-storage mocks.
apps/​api/​internal/​service/​auth_request.go Issues refresh tokens during code redemption.
apps/​api/​internal/​service/​auth_request_test.go Tests resource validation and updated redemption results.
apps/​api/​internal/​oauth/​token.go Adds refresh exchange and public-client authentication.
apps/​api/​internal/​oauth/​token_test.go Tests token responses and public clients.
apps/​api/​internal/​oauth/​server.go Wires refresh storage and metadata scopes.
apps/​api/​internal/​oauth/​refresh_test.go Tests refresh endpoint behavior and errors.
apps/​api/​internal/​oauth/​metadata.go Advertises refresh grants, scopes, and none authentication.
apps/​api/​internal/​oauth/​metadata_test.go Tests expanded discovery metadata.
apps/​api/​internal/​oauth/​client.go Adds public-client behavior and refresh context.
apps/​api/​internal/​oauth/​authorize.go Grants offline access and requires public-client PKCE.
apps/​api/​internal/​oauth/​authorize_test.go Tests offline access and public-client authorization.
apps/​api/​internal/​domain/​refresh_grant.go Defines grants, sealed tokens, and reuse rules.
apps/​api/​internal/​domain/​refresh_grant_test.go Tests token sealing and rotation-grace classification.
apps/​api/​internal/​domain/​event_payload.go Adds resource mismatch and updates client-ID comments.
apps/​api/​internal/​domain/​auth_request.go Defines the resource-mismatch error.
apps/​api/​internal/​domain/​application.go Adds public-client identification.
apps/​api/​internal/​api/​integration_test/​oauth_sign_in_test.go Adds a refresh-capable client fixture.
apps/​api/​internal/​api/​integration_test/​oauth_refresh_test.go Exercises refresh lifecycle over HTTP.
apps/​api/​internal/​api/​integration_test/​oauth_metadata_test.go Checks expanded discovery capabilities.
apps/​api/​internal/​api/​integration_test/​oauth_code_test.go Adapts code-redemption assertions.
apps/​api/​internal/​api/​integration_test/​helpers/​oauth.go Wires the refresh service into integration tests.
apps/​api/​internal/​api/​integration_test/​access_token_test.go Adapts access-token issuance helpers.
apps/​api/​internal/​api/​generated/​oas_validators_gen.go Validates the new event reason.
apps/​api/​internal/​api/​generated/​oas_schemas_gen.go Updates generated server event types and comments.
apps/​api/​internal/​api/​generated/​oas_json_gen.go Decodes the resource-mismatch reason.
apps/​api/​internal/​api/​error_handler_test.go Registers OAuth-only error exclusions.
apps/​api/​cmd/​server/​server.go Wires the production refresh service.
apps/​api/​api/​openapi/​endpoints/​events/​payloads/​auth-token-issued-payload.yaml Documents client IDs on refresh-issued tokens.
apps/​api/​api/​openapi/​endpoints/​events/​payloads/​auth-request-exchange-failed-payload.yaml Adds the resource-mismatch event reason.
apps/​api/​api/​openapi/​components/​schemas/​errors/​refresh-token_invalid.yaml Adds the generated invalid-refresh schema.
apps/​api/​api/​openapi/​components/​schemas/​errors/​refresh-scope_not_granted.yaml Adds the generated refresh-scope error schema.
apps/​api/​api/​openapi/​components/​schemas/​errors/​authreq-resource_mismatch.yaml Adds the generated resource-mismatch schema.
apps/​api/​api/​openapi/​components/​examples/​errors/​refresh-token_invalid.yaml Adds the generated invalid-refresh example.
apps/​api/​api/​openapi/​components/​examples/​errors/​refresh-scope_not_granted.yaml Adds the generated refresh-scope error example.
apps/​api/​api/​openapi/​components/​examples/​errors/​authreq-resource_mismatch.yaml Adds the generated resource-mismatch example.
apps/​api/​api/​openapi/​bundled/​openapi.json Refreshes the bundled event contract.
.changeset/​oauth-refresh-tokens.md Records release notes and compatibility changes.

Review effort: Balanced

Comment on lines +205 to +207
next := *token
next.Scopes = granted
next.TokenID = grant.TokenID

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Not changed in this PR. A grant's scopes travel in its sealed refresh token and the grant row holds no scope list, so a grace retry has only the previous token's scopes to narrow. Storing the ceiling on the row is a schema change in all three dialects, which is more than this review round should carry.

The sequence needs a scope to leave the API's defined scopes and return within the one-minute grace. For the platform API that list is built into the server, so it takes two server versions answering the same grant within that minute, for example during a rolling deploy of a release that removes a scope. The scope that returns is one the person consented to and the older version still defines. Noted as a follow-up.


Generated by Claude Code

Comment on lines +189 to +191
case !settings.Enabled:
return nil, "", oidc.ErrInvalidGrant().WithDescription("the refresh token is invalid, expired or already used").
WithParent(errors.New("the authorization server is off at the grant's issuer"))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Kept as it is, and the record corrected instead: ADR 092 now says that no refresh is answered while the issuer's authorization server is off, and that the grant is not deleted then.

Whether turning the switch off should end every grant for good is a product decision that I have left to the PR author: a server started once without the setting would otherwise sign every client out for good. Until then the grant still ends with its session and with its own lifetime.


Generated by Claude Code

…orization server is off

The MCP record said the grant ends. The token endpoint refuses the refresh
and deletes nothing, so the grant is answered again if the server is turned
back on before its session ends.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0137eGmVSt2DgYJj2CjZbi1U
…tokens

# Conflicts:
#	docs/design/api/events-catalog.md
#	packages/components/custom-elements.json
@fforootd
fforootd added this pull request to the merge queue Dec 1, 2026
Merged via the queue into main with commit 98496e9 Dec 1, 2026
18 of 19 checks passed
@fforootd
fforootd deleted the claude/oauth-refresh-tokens branch December 1, 2026 20:44
@github-project-automation github-project-automation Bot moved this from Inbox to Done in Engineering Kanban Dec 1, 2026

This branch was successfully deployed

1 active deployment
Preview — 645b9809 Deployed Dec 1, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants