Skip to content

Sign in with Google: the core OIDC flow (start, callback, linking, mock IdP) #1399

Backlog

Description

@athvin

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-client v6;
  • 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: /login with a plain message saying what to do.

How

  1. Start route, GET /api/auth/sso/google/start. Use GET, not a form POST: the CSP's form-action 'self' would report a POST that redirects off-site.

    • Generate state, nonce and a PKCE verifier.
    • Read an optional next, passed through safeNextPath (frontend/src/lib/auth/redirect.ts), and an optional invite token.
    • 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 with ERWEB_SESSION_SECRET.
    • Redirect 302 to Google's authorization endpoint, found by OIDC discovery and cached in-process. Request scope=openid email profile and prompt=select_account.
  2. Callback route, GET /api/auth/sso/google/callback.

    • Read the cookie and delete it straight away, so it is single-use.
    • Check that state matches, then exchange the code using the PKCE verifier.
    • Validate the ID token: iss is Google, aud is our client id, exp hasn't passed, and nonce matches. Require email_verified === true.
    • Look up identities by (google, sub).
      • Found, and the user isn't disabled: update last_login_at, call createSession, and land the user with the same rules as password login. Pull those landing rules out of login/route.ts into 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.
  3. Account linking, in Settings → Profile.

    • "Link Google account" runs the same start and callback with a link intent. It needs an existing session, and re-checks the session at the callback.
    • If that Google sub is already linked to a different user, refuse.
    • "Unlink" is refused when the user would be left with no password and no identity.
  4. Plumbing.

    • frontend/src/proxy.ts: add /api/auth/sso to PUBLIC_PATHS.
    • check-bff-authz.mjs already exempts api/auth/**; confirm it does.
    • Add a rate-based WAF rule for /api/auth/sso/* in infra/terraform/envs/prod/edge.tf, mirroring the login rule. A first flood never blocks during warm-up (see memory waf-rate-rule-needs-warmup), so test it with a control path.
  5. Audit.

    • auth.login gains method: google|password.
    • Add auth.identity.link and auth.identity.unlink.
    • Failures write auth.sso.failed with a reason code, and never a token or an email.
  6. 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.

  7. Feature flag. ERWEB_GOOGLE_SSO_ENABLED. When it's off, the button doesn't render and both routes return 404.

  8. 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.
  • next=https://evil.example can'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 sub that 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 frontend passes.

Out of scope

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.ts L10-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

  1. added
    post-mvp
    on Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    post-mvp

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions