Skip to content

Add opt-in delegated Entra token exchange - #10

Merged
appflowy merged 8 commits into
appflowy4from
feat/delegated-token-exchange
Nov 25, 2026
Merged

appflowy merged 8 commits into
appflowy4from
feat/delegated-token-exchange

Conversation

@appflowy

@appflowy appflowy commented Nov 24, 2026 •

Copy link
Copy Markdown

Delegated dashboard access

A self-hosted dashboard can exchange a signed-in user's Entra v2 access token for a five-minute, resource-scoped bearer token. The grant is off until an installation configures it. It authenticates a registered confidential client and resolves the user's existing OIDC identity by tenant and object ID. Signing keys and sessions are specific to delegated access. Access to each Cloud endpoint requires a corresponding resource-server implementation.

Turning it on

There is no enable flag. Exchange is on when GOTRUE_TOKEN_EXCHANGE_TENANT_ID, _PROVIDER, _CLIENTS or _SIGNING_KEYS has a value, and off when all four are empty, which is what a deployment file forwards to an installation that never configured it. The two limits do not count, because deployment files forward their defaults everywhere. A partly configured exchange stops startup and names the missing setting, so it cannot run silently off. A flag defaulting to true was not an option: an installation with nothing configured would fail validation.

Registering a backend

Exchange authenticates backends against the OAuth server's client registry. GoTrue now serves /admin/oauth/clients whenever exchange is enabled, so the OAuth server itself stays off. Its authorization endpoints issue ordinary login tokens, which a resource API that checks only the login signature and session accepts as a full session. /oauth/authorize, /oauth/token and /oauth/clients/register still answer 404 unless GOTRUE_OAUTH_SERVER_ENABLED is set.

An exchange with no clients is valid and refuses every exchange with invalid_client. The policy names a client ID that only registration creates, so setup needs that state. An empty GOTRUE_TOKEN_EXCHANGE_CLIENTS means no clients, because deployment files forward an unset value as an empty string.

Setup order: set the tenant, provider and signing keys; register the backend; name its client ID in the policy. Emptying the client list pauses issuing without removing the configuration. Operator guide

Verification

Exercised the HTTP router with PostgreSQL and signed Entra fixtures, including scope and audience isolation between dashboards, session revocation, and ordinary login and refresh during a signing-key outage. TestTokenExchangeAdministersClientsWithoutTheOAuthServer registers a backend through the admin API with the OAuth server off, exchanges with the secret it returned, and checks that the authorization endpoints stay closed.

Run on 512e13b with Go 1.25.14 and PostgreSQL 15:

Command Result
go test ./internal/api -run 'TestTokenExchange|TestOAuthServer|TestDisabledFeatureReturnsJSON' -race Passed
go test ./internal/conf Passed
go test ./internal/api/oauthserver/... and go test ./internal/api -run 'Admin|OAuth' Passed on 2d94850; the commit after it changes only internal/conf
go build ./..., go vet ./internal/api ./internal/conf, gofmt -l on the changed files Clean

In CI, on 03a4a52. The fork's checks never ran before 2026-11-25: its workflows asked for runners AppFlowy-IO does not have. They run now (#11).

Check Result
Check pull requests Passed
Test: gofmt, migrations, go vet, staticcheck, gosec, govulncheck Passed. The first run reported three findings in this code (two gosec G101 false positives on the RFC 8693 identifier constants, one capitalised error string), fixed in a2de885
Test: test suite Passed: 30 packages and 1,765 tests, including all 23 token-exchange tests

Test was red until 03a4a52 because five password tests failed on appflowy4 itself. #12 fixes them, and its branch is merged into this one so that this pull request can be judged on its own code. Until #12 merges, its two commits (34ba15f, 28edc01) show in this diff; they touch only the password feature and the notes under appflowy/. The two pull requests can merge in either order.

A GoTrue image built from 03a4a52 also answered 21 password-flow checks over HTTP (listed in #12). The token-exchange checks against real services (108, in the Cloud pull request) ran on 512e13b; the commits since change one error string and two comments in that code.

A live Entra pilot is pending.

Related PRs

Project PR Role
Server (AppFlowy-Cloud-Premium) https://github.com/AppFlowy-IO/AppFlowy-Cloud-Premium/pull/1653 Verifies these tokens on the database row API
Self-host (AppFlowy-SelfHost-Commercial) AppFlowy-IO/AppFlowy-SelfHost-Commercial#106 Forwards the settings through Compose, Swarm and Helm, with an operator walkthrough

appflowy and others added 2 commits November 24, 2026 15:02
Token exchange authenticates backends against the OAuth server's client
registry. Administering that registry required GOTRUE_OAUTH_SERVER_ENABLED,
which also opens the authorization-code endpoints. Those issue ordinary login
tokens, which a resource API that checks only the login signature and session
accepts as a full session.

- Serve /admin/oauth/clients whenever the registry exists: with the OAuth
  server or with token exchange. The public /oauth endpoints still require
  the OAuth server.
- Accept an enabled exchange policy with no clients. It refuses every
  exchange, so the first backend can be registered before the policy names
  its client ID. An empty GOTRUE_TOKEN_EXCHANGE_CLIENTS means no clients.
- Document the order of setup and why the OAuth server stays off.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
GOTRUE_TOKEN_EXCHANGE_ENABLED is removed. Exchange is on when the tenant,
provider, client or signing-key setting has a value, and off when all four
are empty, which is what a deployment file forwards to an installation that
never configured it. The two limits do not count, because deployment files
forward their defaults to every installation.

A partly configured exchange stops startup and names the missing setting, so
it cannot run silently off. An empty signing-key value now reads as no keys
and is reported by validation.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
appflowy and others added 4 commits November 25, 2026 12:10
…code

This code had never been linted: the fork's checks only started running
with the workflows now on appflowy4.

- gosec G101 read the two RFC 8693 identifier constants as hardcoded
  credentials because their names contain "token". They are marked as
  not secrets.
- staticcheck ST1005: an error string began with a capital letter.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The flag says whether a password is one the user chose. POST
/user/change-password asks for the current password only when it is set,
and GET /user/auth-info reports it so that Web can ask for one.

Since the port to appflowy2 (2025-11) nothing stored it. The appflowy
branch wrote the column in UpdatePassword's column list; the port kept the
assignments in memory and dropped the column. The endpoint therefore never
asked for a current password that was set after the port, and auth-info
kept reporting such passwords as not chosen.

- User.SetIsNonDefaultPassword stores the flag. Each place that decides it
  calls this in the transaction of the change it belongs to, so the flag
  no longer depends on an upstream column list.
- POST /user/change-password and PUT /user with a password set it. Web and
  Desktop use PUT /user after a recovery, and Desktop for a first password.
- A verified password recovery resets it. The appflowy branch reset it
  when a recovery was requested, which let anyone reset it for any
  address; the port left that as an assignment that was never saved. A
  magic-link sign-in and an email change do not reset it.

Tests. The five that failed on appflowy4:

- TestUserChangePasswordNoCurrentPasswordForDefaultPassword failed on the
  missing write and passes unchanged.
- TestUserUpdatePasswordReauthentication and
  TestUserUpdatePasswordLogoutOtherSessions passed alone and failed in the
  suite: TestUserChangePasswordWeakPassword left the shared minimum length
  at 10, which refused their seven-character password. It restores it now.
- TestPasswordRecoveryResetsIsNonDefaultPassword also required the
  recovery to delete the password, which no version of the fork did. It
  now requires the reset on verification only, and that the password
  still signs the user in.
- TestEmailChangeResetsIsNonDefaultPassword required an email change to
  reset the flag and delete the password, and expected the change to
  complete after one of its two confirmations. It is replaced by a test
  that a completed change keeps both.

New tests read the flag back through each endpoint that sets it.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…no test

The testing guide ran suite methods by their own name, for example
`-run TestUserChangePassword`. Those tests are methods of the TestUser and
TestVerify suites, so the pattern matched nothing and `go test` printed
`ok ... [no tests to run]`. The migration notes recorded that as passing,
for tests that could not pass. The commands now name the suite first.

The notes also said the flag is reset on an email change, which the code
never did and should not do, and did not say where the flag is stored.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@appflowy
appflowy merged commit 11ea72a into appflowy4 Nov 25, 2026
4 checks passed
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.

1 participant