Commit eec7c7b
feat: auth flow via same-origin ory elements (#438)
## Summary
Replaces the NextAuth (Auth.js) layer with a first-party, same-origin
auth flow built directly on Ory. The dashboard now acts as Hydra's
OAuth2 client **and** its consent/logout provider, drives an
authorization-code + PKCE flow, and carries the resulting OIDC tokens in
a single JWE-encrypted `e2b_session` cookie. Kratos owns the session;
the cookie is just a token cache, never the auth gate.
This removes `next-auth` / `@auth/core` entirely and the Auth.js
request-wrapping that had been fighting our sign-out and tRPC code
paths.
## Why
- Auth.js wrapped every request and re-issued a JWT session cookie on
the way out, which clobbered our sign-out cookie deletion and forced
awkward boundary shims (`authjs-boundary`, `authjs-session-boundary`,
`authjs-callbacks`).
- The OAuth2 redirect/PKCE/state handling was opaque inside the Auth.js
provider, making the Hydra ⇄ Kratos consent/logout dance hard to reason
about.
## What changed
**Auth flow (new routes)**
- `GET /api/auth/oauth/start` — builds the Hydra authorization request
(PKCE S256, state, nonce), stashes the verifier/state/nonce in a
short-lived httpOnly flow cookie, and 307-redirects to `/oauth2/auth`.
- `GET /api/auth/oauth/callback/ory` — validates state/nonce/PKCE,
exchanges the code, bootstraps the dashboard user from the id_token,
seals tokens into `e2b_session`. Failures route to the one-shot
`recover` guard so a stale callback can't loop.
- `GET /consent` — dashboard is Hydra's consent provider; accepts the
consent challenge and folds the identity's profile traits into the
id_token (without this, tokens carry `sub`/`iss` but no email/name and
bootstrap rejects the login).
- `GET /logout` — dashboard is Hydra's logout provider; accepts the
logout challenge, routes through Kratos' own logout first so
`ory_kratos_session` is cleared before Hydra finalizes.
- `sign-out` route now reads the id_token from `e2b_session` to build
the Hydra RP-logout URL and clears the cookie on the redirect.
**Session / tokens**
- New `e2b_session` cookie: JWE (`dir` + `A256GCM`) via `jose`, carrying
access/refresh/id tokens. Keyed by new `E2B_SESSION_SECRET` (replaces
`AUTH_SECRET`); domain-scoped so it works across the cluster's
subdomains.
- New `token-refresh` module refreshes the Hydra access token edge-side
(direct token-endpoint call, refresh skew, dead/unchanged/refreshed
states).
- New `kratos-session-edge` helper does an edge-safe Kratos `whoami` for
the middleware redirect gate; authoritative enforcement stays in
`getAuthContext`.
**UI**
- Ory Elements auth pages (`/login`, `/registration`, `/recovery`,
`/verification`) are now always same-origin — the
`NEXT_PUBLIC_ORY_CUSTOM_UI` flag and the legacy redirect fallbacks are
gone.
- Auth footer links switched from `next/link` to plain `<a>` so the
cross-origin start redirect stays a top-level document navigation (a
soft nav would turn it into a CORS request Hydra rejects).
**Plumbing**
- tRPC handler no longer wrapped by `auth()`; auth is resolved
per-procedure by the auth middleware (Kratos whoami + `e2b_session`).
- Deleted: `src/auth.ts`, the Auth.js boundary/callback shims,
`next-auth.d.ts`, and the `[...nextauth]` / `bootstrap-failed` routes.
**Dependencies**
- Removed `next-auth` (+ `@auth/core` and its transitive `@panva/hkdf`,
`preact*`).
- Added `jose` and `oauth4webapi` (direct, version-pinned).
**Env**
- Added `E2B_SESSION_SECRET`, optional `ORY_HYDRA_PUBLIC_URL`
(self-hosted Hydra issuer).
- Removed `AUTH_SECRET`, `AUTH_TRUST_HOST`, `NEXT_PUBLIC_ORY_CUSTOM_UI`.
## Testing
New unit + integration coverage for the rewritten flow: `oauth-client`,
`session-cookie`, `token-refresh`, `kratos-session-edge` (unit) and
`auth-ory-callback`, `auth-ory-consent`, `auth-ory-logout`, plus the
retained entrypoints/account-security suites (integration). The Auth.js
session-boundary test was removed with its module.
## Notes for reviewers
- **Env / deploy:** set `E2B_SESSION_SECRET` (`openssl rand -hex 32`) in
every environment before merge — `AUTH_SECRET` is no longer read.
- This is auth-critical; worth a careful pass on the callback
failure/recover paths and the logout ordering (Kratos before Hydra
finalize).
---------
Co-authored-by: ben-fornefeld <ben.fornefeld@gmail.com>
Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>1 parent eec42cd commit eec7c7b
81 files changed
Lines changed: 3711 additions & 1560 deletions
File tree
- .github/workflows
- src
- app
- (auth)/forgot-password
- api
- auth
- oauth
- [...nextauth]
- bootstrap-failed
- callback/ory
- logout-relay
- recover
- relay
- start
- sign-out
- switch-account
- trpc/[trpc]
- consent
- login
- components
- logout
- recovery
- registration
- verification
- configs
- core
- modules/auth
- server
- api
- middlewares
- routers
- auth
- ory
- proxy
- trpc
- lib
- types
- tests
- integration
- unit
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
9 | | - | |
10 | | - | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
14 | 17 | | |
15 | 18 | | |
16 | 19 | | |
17 | 20 | | |
18 | 21 | | |
19 | 22 | | |
20 | 23 | | |
21 | | - | |
22 | | - | |
23 | | - | |
| 24 | + | |
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
| |||
41 | 42 | | |
42 | 43 | | |
43 | 44 | | |
44 | | - | |
45 | | - | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
46 | 52 | | |
47 | 53 | | |
48 | 54 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
48 | | - | |
| 48 | + | |
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
| |||
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
110 | 110 | | |
111 | 111 | | |
112 | 112 | | |
| 113 | + | |
113 | 114 | | |
114 | 115 | | |
115 | 116 | | |
116 | 117 | | |
117 | | - | |
118 | 118 | | |
119 | 119 | | |
120 | 120 | | |
| 121 | + | |
121 | 122 | | |
122 | 123 | | |
123 | 124 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | 2 | | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
| 3 | + | |
| 4 | + | |
11 | 5 | | |
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
| 18 | + | |
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| |||
0 commit comments