feat(auth): read email from the userinfo endpoint when the access token has none #139

Merged
amiryahaya merged 3 commits from feat/oidc-userinfo-email into main 2026-10-31 04:59:56 +01:00
Owner

What

Zitadel's JWT access tokens carry no email or email_verified, but KWS authorises the bootstrap Super Admin and staff/organization invitations only on a verified email read from the token. Against a real Zitadel the first admin signed in and got no role.

OIDCVerifier.Verify now asks the issuer's userinfo endpoint when the token has no email:

  • the bearer token itself is used; the userinfo sub must equal the token's sub (else ErrInvalidToken)
  • cached per subject: 5 min, but 30 s when the email is missing or unverified, so verifying and then accepting an invitation is not held up
  • the issuer refusing the token (401/403: signed out, deactivated, no openid scope) is ErrInvalidToken, i.e. a 401 so the web client signs the user out; any other failure is ErrUnavailable (503) and is not cached, so a blank email never overwrites a stored one
  • no userinfo endpoint advertised: behaviour unchanged. Tokens that already carry an email never call userinfo.
  • It adds no revocation check (a cached entry does not extend a token).

Also: x/oauth2 is now a direct dependency; authtest gained an opt-in userinfo endpoint; CLAUDE.md auth conventions updated. Needed IdP settings: the app issues JWT access tokens and the web apps request the email scope.

Checked

  • Real Zitadel v4.20.0 (local compose, separate PR/branch): the bootstrap admin became Super Admin on first sign-in; a verified customer got no role.
  • Coverage: make cover 91.6% total (min 80%), internal/auth 96.3%. make lint 0 issues; auth, app, rbac, contract tests pass with -race.
  • Mutation check: removing the subject check makes the mismatch test fail.

Review

Dual-pass (Ponytail complexity, then correctness reviewer). Pass 2 said "fix first" with two Important findings (401 reported as 503; unverified answer cached 5 min). Both fixed with tests, plus per-subject cache, name-precedence and short-TTL tests; Pass 1 re-check clear.

Not done / risks

  • Matching "401"/"403" at the start of go-oidc's error text is a shortcut (go-oidc has no typed error); a test pins it.
  • No singleflight: concurrent first requests of one user each call userinfo.
  • go-oidc puts the userinfo response body in the error, which the middleware logs at Warn (Zitadel bodies are generic).
  • Not tried: the ticket site end to end (needs a mail server for the email code), prompt=create, a real invitation.

🤖 Generated with Claude Code

## What Zitadel's JWT access tokens carry no `email` or `email_verified`, but KWS authorises the bootstrap Super Admin and staff/organization invitations only on a **verified** email read from the token. Against a real Zitadel the first admin signed in and got no role. `OIDCVerifier.Verify` now asks the issuer's userinfo endpoint when the token has no `email`: - the bearer token itself is used; the userinfo `sub` must equal the token's `sub` (else `ErrInvalidToken`) - cached per subject: 5 min, but 30 s when the email is missing or unverified, so verifying and then accepting an invitation is not held up - the issuer refusing the token (401/403: signed out, deactivated, no `openid` scope) is `ErrInvalidToken`, i.e. a 401 so the web client signs the user out; any other failure is `ErrUnavailable` (503) and is not cached, so a blank email never overwrites a stored one - no userinfo endpoint advertised: behaviour unchanged. Tokens that already carry an email never call userinfo. - It adds no revocation check (a cached entry does not extend a token). Also: `x/oauth2` is now a direct dependency; `authtest` gained an opt-in userinfo endpoint; CLAUDE.md auth conventions updated. Needed IdP settings: the app issues JWT access tokens and the web apps request the `email` scope. ## Checked - Real Zitadel v4.20.0 (local compose, separate PR/branch): the bootstrap admin became Super Admin on first sign-in; a verified customer got no role. - Coverage: `make cover` **91.6%** total (min 80%), `internal/auth` 96.3%. `make lint` 0 issues; auth, app, rbac, contract tests pass with `-race`. - Mutation check: removing the subject check makes the mismatch test fail. ## Review Dual-pass (Ponytail complexity, then correctness reviewer). Pass 2 said "fix first" with two Important findings (401 reported as 503; unverified answer cached 5 min). Both fixed with tests, plus per-subject cache, name-precedence and short-TTL tests; Pass 1 re-check clear. ## Not done / risks - Matching "401"/"403" at the start of go-oidc's error text is a shortcut (go-oidc has no typed error); a test pins it. - No singleflight: concurrent first requests of one user each call userinfo. - go-oidc puts the userinfo response body in the error, which the middleware logs at Warn (Zitadel bodies are generic). - Not tried: the ticket site end to end (needs a mail server for the email code), `prompt=create`, a real invitation. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Zitadel JWT access tokens carry no email or email_verified, so the bootstrap
admin and invitation matches never fired. The verifier now asks userinfo once
per subject per 5 minutes; a failure is ErrUnavailable, never a blank email.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Review findings: a 401/403 from userinfo (signed out or deactivated at the issuer)
was reported as a 503, so the web client never signed the user out; and an
unverified answer was cached for 5 minutes, delaying verify-then-accept.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
docs: userinfo email rules in CLAUDE.md
All checks were successful
ci / changes (pull_request) Successful in 16s
ci / clients (pull_request) Has been skipped
ci / web (pull_request) Has been skipped
ci / api (pull_request) Successful in 16m20s
ci / ci-gate (pull_request) Successful in 3s
b588db785c
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
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
primatekuntech/kws!139
No description provided.