Repository navigation
Description
Why
This is the core of the epic: a person proves who they are with Google, and we either recognise them or hand them to the first-sign-in router (#1400).
The design was settled on 2026-10-02 in frontend-design.md §2.4:
- direct OIDC in the BFF, using
openid-clientv6; - no broker, so no Auth0, Cognito or Clerk;
- the session layer we already have (
createSession,er_session, memberships, impersonation, audit) unchanged.
What the user sees
- On
/login: a "Continue with Google" button. After Google's account picker, a returning user lands exactly where password login would have taken them. - On
/invite/[token]: "Continue with Google" accepts that invite without setting a password, through First-sign-in router: join an existing workspace vs found a new one #1400's/join. - When anything goes wrong:
/loginwith a plain message saying what to do.
How
-
Start route,
GET /api/auth/sso/google/start. Use GET, not a form POST: the CSP'sform-action 'self'would report a POST that redirects off-site.- Generate
state,nonceand a PKCE verifier. - Read an optional
next, passed throughsafeNextPath(frontend/src/lib/auth/redirect.ts), and an optionalinvitetoken. - Store all of it in one short-lived cookie: httpOnly, Secure, SameSite=Lax, path
/api/auth/sso, 10-minute max-age. Sign or encrypt it withERWEB_SESSION_SECRET. - Redirect 302 to Google's authorization endpoint, found by OIDC discovery and cached in-process. Request
scope=openid email profileandprompt=select_account.
- Generate
-
Callback route,
GET /api/auth/sso/google/callback.- Read the cookie and delete it straight away, so it is single-use.
- Check that
statematches, then exchange the code using the PKCE verifier. - Validate the ID token:
issis Google,audis our client id,exphasn't passed, andnoncematches. Requireemail_verified === true. - Look up
identitiesby(google, sub).- Found, and the user isn't disabled: update
last_login_at, callcreateSession, and land the user with the same rules as password login. Pull those landing rules out oflogin/route.tsinto a shared helper, so both routes use one rule. - Not found: hand the verified claims (
sub,email,hd,name) and any invite token to the first-sign-in router (First-sign-in router: join an existing workspace vs found a new one #1400). The callback never decides membership itself.
- Found, and the user isn't disabled: update
-
Account linking, in Settings → Profile.
- "Link Google account" runs the same start and callback with a
linkintent. It needs an existing session, and re-checks the session at the callback. - If that Google
subis already linked to a different user, refuse. - "Unlink" is refused when the user would be left with no password and no identity.
- "Link Google account" runs the same start and callback with a
-
Plumbing.
frontend/src/proxy.ts: add/api/auth/ssotoPUBLIC_PATHS.check-bff-authz.mjsalready exemptsapi/auth/**; confirm it does.- Add a rate-based WAF rule for
/api/auth/sso/*ininfra/terraform/envs/prod/edge.tf, mirroring the login rule. A first flood never blocks during warm-up (see memorywaf-rate-rule-needs-warmup), so test it with a control path.
-
Audit.
auth.logingainsmethod: google|password.- Add
auth.identity.linkandauth.identity.unlink. - Failures write
auth.sso.failedwith a reason code, and never a token or an email.
-
Errors. Every failure redirects to
/login?error=<code>:sso_state: the attempt expired or was tampered with.sso_unverified: the Google email isn't verified.sso_disabled: the account is disabled.sso_unavailable: Google is unreachable, or the flag is off.
Each code gets one sentence of copy that names the next action.
-
Feature flag.
ERWEB_GOOGLE_SSO_ENABLED. When it's off, the button doesn't render and both routes return 404. -
Mock IdP for tests. Add a small OIDC provider next to
tests/mock-erserver/, with discovery, authorize, token and JWKS. It must issue tokens with configurable claims (sub,email,email_verified,hd). Point Playwright's env at it. Every later ticket in this epic tests through it.
Acceptance criteria
- With the mock IdP, a returning user signs in through Google and lands where password login would put them.
- Each of these is rejected and lands on the right
?error=:- a tampered
state; - a reused callback, where the cookie is already consumed;
- a wrong
aud; - an expired token;
- a wrong
nonce; email_verified=false.
- a tampered
-
next=https://evil.examplecan't redirect off-site. - No takeover: with a password user
[email protected]and no identity, a Google sign-in as[email protected]does not create a session for that user. The router test in First-sign-in router: join an existing workspace vs found a new one #1400 covers the copy; this ticket asserts that no session is created. - Linking from Profile works. Linking a
subthat belongs to someone else is refused. An unlink that would leave the account with no way in is refused. - With the flag off, both routes return 404 and the button is gone.
- No CSP violation reports come from the flow in a Playwright run.
- A review against the security skill's authn and session reference is recorded in the PR body.
-
make frontendpasses.
Out of scope
- The decision of who becomes a member, a founder or staff (First-sign-in router: join an existing workspace vs found a new one #1400 and Staff rule: a dupezero.com Google Workspace account becomes super admin #1401).
- SAML and SCIM.
- Other identity providers.
Depends on
#1397 (client and secrets), #1398 (schema). This ticket blocks #1400.
Where to start
frontend/src/app/api/auth/login/route.ts: the landing rules at L42-59.frontend/src/lib/auth/session.ts,redirect.ts.frontend/src/proxy.tsL10-39.frontend/next.config.ts, for the CSP..claude/skills/security/references/authn-session.md.- The earlier ticket SSO implementation #560 is superseded by this one.
Estimate: 1.5 engineer-weeks.
Part of the E31 epic (#1396) (linked as parent). Decisions recorded 2026-11-28 with the owner.
Activity