@boost33/shell-data 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,252 @@
1
+ import { NextResponse, type NextRequest } from 'next/server.js';
2
+ export declare const CTX_COOKIE = "b33_ctx";
3
+ export declare const SID_COOKIE = "b33_sid";
4
+ /** Refresh this long before `exp` (§5.1.4): `jwtVerify` allows no clock skew. */
5
+ export declare const REFRESH_MARGIN_SECONDS = 30;
6
+ /** The ceiling the contract sets on the call (§5.1.5). */
7
+ export declare const REFRESH_TIMEOUT_MS = 1500;
8
+ /**
9
+ * Whether `b33_ctx` should be refreshed before this request renders.
10
+ *
11
+ * Reads `exp` with an UNVERIFIED decode (§5.1.4). That is safe only because the
12
+ * answer decides nothing but whether to call identity, and only while the page
13
+ * runs a full `jwtVerify` behind the gate on every path: a forged early `exp`
14
+ * buys a refresh that succeeds or fails on `b33_sid` alone, and a forged late
15
+ * one skips the refresh and meets the page's verify. It returns a boolean and
16
+ * nothing else, so no claim from the decode can leave the gate.
17
+ *
18
+ * A missing or undecodable cookie reads as expired: with `b33_sid` present a
19
+ * refresh is exactly how a person gets a fresh one.
20
+ *
21
+ * Known and accepted (§5.1.4): this detects an expired token, not a forged or
22
+ * retired-key one. During a key rotation a token signed by a retired key with a
23
+ * live `exp` passes here, fails at the page, and takes the sign-in redirect for
24
+ * up to the token's remaining ten minutes.
25
+ */
26
+ export declare function ctxLooksExpired(cookie: string | undefined, nowSeconds?: number): boolean;
27
+ export type RefreshOutcome =
28
+ /** Identity re-issued the session. Forward these verbatim (§5.1.3). */
29
+ {
30
+ kind: 'refreshed';
31
+ setCookies: string[];
32
+ }
33
+ /** 401: the session is genuinely dead. Clear both cookies, sign in (§5.1.7). */
34
+ | {
35
+ kind: 'dead';
36
+ }
37
+ /** Timeout, network error, 5xx or 429: identity is unwell, the session is
38
+ * probably fine. Fall through unchanged (§5.1.7). Never a 5xx of our own. */
39
+ | {
40
+ kind: 'unwell';
41
+ status: number | null;
42
+ };
43
+ export type RefreshOptions = {
44
+ identityUrl: string;
45
+ /** This request's own Cookie header, carrying `b33_sid`. */
46
+ cookieHeader: string;
47
+ /** This app's slug in identity's `APPS`, sent as `x-boost33-client` (§5.1.6). */
48
+ client: string;
49
+ fetchImpl?: typeof fetch;
50
+ timeoutMs?: number;
51
+ };
52
+ /**
53
+ * One server-to-server call to identity's `/auth/refresh`, relaying this
54
+ * request's `b33_sid`. Not a redirect through identity (§5.1, opening).
55
+ */
56
+ export declare function refreshSession({ identityUrl, cookieHeader, client, fetchImpl, timeoutMs, }: RefreshOptions): Promise<RefreshOutcome>;
57
+ /**
58
+ * The `name=value` pair at the front of a Set-Cookie header, for rewriting the
59
+ * inbound Cookie header (§5.1.2). Only the pair is read; the header itself is
60
+ * forwarded untouched (§5.1.3).
61
+ */
62
+ export declare function cookiePair(setCookie: string): {
63
+ name: string;
64
+ value: string;
65
+ } | null;
66
+ export type GateOptions = {
67
+ identityUrl: string;
68
+ /**
69
+ * This caller's slug in identity's `APPS` (boost33-identity src/config/apps.ts),
70
+ * sent as `x-boost33-client` (§5.1.6). These are identity's caller slugs, not
71
+ * product slugs: `outbound`, `passport`, `hub`, `boostscore`. A value not in
72
+ * that list still works; identity logs it as `unrecognised`.
73
+ */
74
+ client: string;
75
+ /**
76
+ * The app's own full verify of a `b33_ctx` (its `jwtVerify` against identity's
77
+ * JWKS, issuer and audience): true when it verifies. Only a yes or no crosses
78
+ * back; no claim leaves the gate.
79
+ */
80
+ verify: (token: string) => Promise<boolean>;
81
+ /** For tests: replaces the call to identity. */
82
+ refresh?: (cookieHeader: string) => Promise<RefreshOutcome>;
83
+ };
84
+ /**
85
+ * What the gate decided for one request, for an app that runs its own proxy
86
+ * logic around it (for instance, verifying `b33_ctx` and handing claims to the
87
+ * page in a header) and so cannot return `gateRequest`'s response as is.
88
+ *
89
+ * - `none`: no `b33_sid`. Nothing to refresh from; the page decides.
90
+ * - `live`: `b33_ctx` is not within 30s of `exp`, by an UNVERIFIED decode. This
91
+ * says nothing about whether it is genuine: verify it yourself, as always.
92
+ * - `refreshed`: identity re-issued the session and the new `b33_ctx` VERIFIED
93
+ * with your verifier. Forward `setCookies` verbatim on your response (§5.1.3),
94
+ * and render this request with `cookieHeader` (§5.1.2); `freshCtx` is the new
95
+ * token, already verified, for an app that reads its claims here.
96
+ * - `dead`: identity answered 401. Return `redirect`: both cookies cleared, and
97
+ * on to sign in with a `return` here (§5.1.7). Do not retry.
98
+ * - `unwell`: identity timed out, failed or rate-limited, or re-issued a token
99
+ * that does not verify. Change nothing and forward nothing; the request goes
100
+ * on with its expired `b33_ctx`, which your verify will then reject.
101
+ */
102
+ export type GateResult = {
103
+ outcome: 'none';
104
+ } | {
105
+ outcome: 'live';
106
+ } | {
107
+ outcome: 'refreshed';
108
+ freshCtx: string;
109
+ setCookies: string[];
110
+ cookieHeader: string;
111
+ } | {
112
+ outcome: 'dead';
113
+ redirect: Response;
114
+ } | {
115
+ outcome: 'unwell';
116
+ };
117
+ /**
118
+ * The gate's decision without building a response: §5.1 in full (one refresh,
119
+ * 1.5s, exp - 30s, `x-boost33-client`, 401 versus unwell, the re-issued token
120
+ * verified before anything is forwarded). `gateRequest` is this, turned into
121
+ * the response a plain proxy returns. Takes any `Request`.
122
+ */
123
+ export declare function gate(request: Request, options: GateOptions): Promise<GateResult>;
124
+ /**
125
+ * The request gate: step 2 of identity's contract §5, with §5.1's server-side
126
+ * refresh, as the response a proxy returns. Run it on navigations only
127
+ * (SESSION_GATE_MATCHER, or `isNavigation` in code behind a broad matcher).
128
+ *
129
+ * `b33_sid` present, `b33_ctx` absent or past `exp - 30s`
130
+ * → POST identity's /auth/refresh, relaying `b33_sid` (one call, 1.5s cap)
131
+ * → refreshed: verify the new `b33_ctx` with the app's verifier, forward
132
+ * identity's Set-Cookie headers verbatim on the response, AND rewrite
133
+ * this request's Cookie header so the page rendering it reads the new
134
+ * token (§5.1.2; without that the page still redirects)
135
+ * → 401: the session is dead. Clear both cookies and sign in.
136
+ * → anything else (timeout, network, 5xx, 429), or a re-issued token that
137
+ * does not verify: fall through unchanged, forwarding nothing. The page's
138
+ * own full verify then fails on the expired token and takes the path it
139
+ * always has. Never a pass: nothing here lets a request through on a
140
+ * token that was not verified.
141
+ *
142
+ * Everything else (no `b33_sid`, a live `b33_ctx`) passes through untouched,
143
+ * and the page decides from claims it verifies in full. This gate never tells
144
+ * the page who is signed in, and never reads `design`.
145
+ *
146
+ * The gate itself never redirects on the unwell path, so it cannot loop. An
147
+ * app whose page redirects to identity when it finds no claims needs the
148
+ * one-bounce guard §5.1.7 asks for: see `markBounce` and `isRepeatBounce`.
149
+ */
150
+ export declare function gateRequest(request: NextRequest, options: GateOptions): Promise<NextResponse>;
151
+ /**
152
+ * Whether this request is a navigation the gate should run on, decided in code
153
+ * the way SESSION_GATE_MATCHER decides it in config: a document request that
154
+ * accepts HTML, or an in-app navigation (`rsc: 1`); never a prefetch; never
155
+ * Next's own assets, `api/`, or a file with an extension. For an app whose
156
+ * proxy must run on every request for other reasons and so cannot narrow its
157
+ * matcher.
158
+ */
159
+ export declare function isNavigation(request: Request): boolean;
160
+ /**
161
+ * Whether the request says it is a prefetch: Next's router (`next-router-prefetch`),
162
+ * or a browser's speculative load (`purpose: prefetch`, `sec-purpose: prefetch`).
163
+ *
164
+ * THESE HEADERS ARE SET BY THE CLIENT. Use this only to decide whether to SPEND a
165
+ * refresh, never to decide whether to run your proxy at all. In an app whose
166
+ * pages trust a header the proxy sets, the proxy must run on every request
167
+ * (CLAIMS_PROXY_MATCHER) and call this to skip the refresh alone.
168
+ */
169
+ export declare function isPrefetch(request: Request): boolean;
170
+ export declare const BOUNCE_PARAM = "b33_bounced";
171
+ /** The return URL with the bounce mark added. Pass it as identity's `return`. */
172
+ export declare function markBounce(returnTo: string | URL): string;
173
+ /** Whether this arrival is already the bounce back from identity. */
174
+ export declare function isRepeatBounce(url: string | URL): boolean;
175
+ /**
176
+ * FOR APPS WHOSE PAGES VERIFY `b33_ctx` THEMSELVES, and only those. The Hub is
177
+ * one: every page runs its own `jwtVerify`, and the proxy sets no request
178
+ * header a page trusts. If your proxy hands claims to the page in a request
179
+ * header, use CLAIMS_PROXY_MATCHER instead. This matcher's prefetch exclusions
180
+ * read headers the CLIENT sets, so a document request marked as a prefetch
181
+ * skips your proxy entirely, and a page that trusts a proxy-set header would
182
+ * then trust whatever the client sent.
183
+ *
184
+ * The matcher the gate must run behind (§5.1.1): navigations only, meaning a
185
+ * document request that accepts HTML, or an in-app navigation, which Next sends
186
+ * as an RSC fetch marked `rsc: 1`. Never a prefetch, so hovering links cannot
187
+ * start a refresh storm; never Next's own assets or a file with an extension.
188
+ * API routes authenticate on their own (service keys), so `api/` is excluded.
189
+ *
190
+ * Copy this value into your proxy's `export const config` AS A LITERAL. Next
191
+ * reads the matcher at build time and ignores a value held in a variable or
192
+ * imported, silently, which would run the gate on every request.
193
+ */
194
+ export declare const SESSION_GATE_MATCHER: readonly [{
195
+ readonly source: "/((?!_next/|api/|.*\\..*).*)";
196
+ readonly has: readonly [{
197
+ readonly type: "header";
198
+ readonly key: "accept";
199
+ readonly value: "(.*)text/html(.*)";
200
+ }];
201
+ readonly missing: readonly [{
202
+ readonly type: "header";
203
+ readonly key: "next-router-prefetch";
204
+ }, {
205
+ readonly type: "header";
206
+ readonly key: "purpose";
207
+ readonly value: "prefetch";
208
+ }];
209
+ }, {
210
+ readonly source: "/((?!_next/|api/|.*\\..*).*)";
211
+ readonly has: readonly [{
212
+ readonly type: "header";
213
+ readonly key: "rsc";
214
+ readonly value: "1";
215
+ }];
216
+ readonly missing: readonly [{
217
+ readonly type: "header";
218
+ readonly key: "next-router-prefetch";
219
+ }, {
220
+ readonly type: "header";
221
+ readonly key: "purpose";
222
+ readonly value: "prefetch";
223
+ }];
224
+ }];
225
+ /**
226
+ * The matcher for an app whose proxy hands claims to its pages in a request
227
+ * header (Results, `x-results-claims`). It runs the proxy on EVERY request but
228
+ * Next's own static assets, with no `has`/`missing` conditions, because any
229
+ * condition on a client-set header is a way around the proxy. In code:
230
+ *
231
+ * headers.delete('x-<app>-claims') // always, first
232
+ * if (isNavigation(request)) result = await gate(request, options)
233
+ * // set the claims header only from claims your proxy verified
234
+ *
235
+ * `isNavigation` (and so `isPrefetch`) only decides whether to spend a refresh.
236
+ * A request that lies about being a prefetch skips the refresh, never the proxy.
237
+ *
238
+ * Copy it into `export const config` AS A LITERAL, as with SESSION_GATE_MATCHER.
239
+ */
240
+ export declare const CLAIMS_PROXY_MATCHER: readonly ["/((?!_next/static|_next/image).*)"];
241
+ /**
242
+ * For an app's tests: whether its literal matcher copy is SESSION_GATE_MATCHER,
243
+ * or narrows only its paths (Outbound scopes it to its Backdrop routes). A
244
+ * matcher that drops the prefetch exclusion or the `_next/`/`api/` exclusions
245
+ * fails.
246
+ *
247
+ * Passing says the matcher spends no refresh on a prefetch. It does NOT say the
248
+ * app is safe: this matcher is only safe where pages verify `b33_ctx`
249
+ * themselves. An app that passes claims in a request header must use
250
+ * CLAIMS_PROXY_MATCHER, and this check does not apply to it.
251
+ */
252
+ export declare function matcherIsNavigationOnly(matcher: unknown): boolean;
@@ -0,0 +1,376 @@
1
+ // No `server-only` here, unlike the package's root entry: this module runs in
2
+ // Next's request-interception layer (proxy/middleware), which is not React's
3
+ // server build, so `server-only` would throw on import there. It is still
4
+ // server code: it is only ever imported from proxy.ts.
5
+ import { decodeJwt } from 'jose';
6
+ import { NextResponse } from 'next/server.js';
7
+ /*
8
+ * The session gate: refreshing an expired `b33_ctx` before a page renders, as
9
+ * boost33-identity's docs/Boost33-Identity-Contract-v1.md §5.1 requires. That
10
+ * section is normative; its numbered requirements are cited below.
11
+ *
12
+ * Lifted from Passport's production gate (boost33-passport src/lib/ctx-refresh.ts
13
+ * and src/lib/session-gate.ts, B33-1751, itself a copy of Boost Voice's
14
+ * src/lib/identity/refresh.ts). The only changes are the parameters that were
15
+ * Passport's own: the caller slug, the verifier, and the fallback host. Every
16
+ * app calls this one gate, so the next fix lands once.
17
+ *
18
+ * Why every Backdrop app needs it: `b33_ctx` lives ten minutes. Without a
19
+ * refresh before the render, a page that reads the `design` claim from an
20
+ * expired cookie finds no claims and draws the classic design, so a person on
21
+ * the preview flips between designs every ten minutes or so.
22
+ *
23
+ * Use it from the request-interception layer, never a page (§5.1.1): a Server
24
+ * Component cannot set a cookie. In `src/proxy.ts` on Next 16 (or middleware):
25
+ *
26
+ * export function proxy(request: NextRequest) {
27
+ * return gateRequest(request, { identityUrl, client: 'boostscore', verify })
28
+ * }
29
+ *
30
+ * and copy SESSION_GATE_MATCHER's value into `export const config` there, as a
31
+ * literal: Next reads the matcher at build time and ignores an imported value.
32
+ * `matcherIsNavigationOnly` checks that copy in your tests.
33
+ */
34
+ export const CTX_COOKIE = 'b33_ctx';
35
+ export const SID_COOKIE = 'b33_sid';
36
+ /** Refresh this long before `exp` (§5.1.4): `jwtVerify` allows no clock skew. */
37
+ export const REFRESH_MARGIN_SECONDS = 30;
38
+ /** The ceiling the contract sets on the call (§5.1.5). */
39
+ export const REFRESH_TIMEOUT_MS = 1_500;
40
+ /**
41
+ * Whether `b33_ctx` should be refreshed before this request renders.
42
+ *
43
+ * Reads `exp` with an UNVERIFIED decode (§5.1.4). That is safe only because the
44
+ * answer decides nothing but whether to call identity, and only while the page
45
+ * runs a full `jwtVerify` behind the gate on every path: a forged early `exp`
46
+ * buys a refresh that succeeds or fails on `b33_sid` alone, and a forged late
47
+ * one skips the refresh and meets the page's verify. It returns a boolean and
48
+ * nothing else, so no claim from the decode can leave the gate.
49
+ *
50
+ * A missing or undecodable cookie reads as expired: with `b33_sid` present a
51
+ * refresh is exactly how a person gets a fresh one.
52
+ *
53
+ * Known and accepted (§5.1.4): this detects an expired token, not a forged or
54
+ * retired-key one. During a key rotation a token signed by a retired key with a
55
+ * live `exp` passes here, fails at the page, and takes the sign-in redirect for
56
+ * up to the token's remaining ten minutes.
57
+ */
58
+ export function ctxLooksExpired(cookie, nowSeconds = Date.now() / 1000) {
59
+ if (!cookie)
60
+ return true;
61
+ try {
62
+ const { exp } = decodeJwt(cookie);
63
+ if (typeof exp !== 'number')
64
+ return true;
65
+ return exp - REFRESH_MARGIN_SECONDS <= nowSeconds;
66
+ }
67
+ catch {
68
+ return true;
69
+ }
70
+ }
71
+ /**
72
+ * One server-to-server call to identity's `/auth/refresh`, relaying this
73
+ * request's `b33_sid`. Not a redirect through identity (§5.1, opening).
74
+ */
75
+ export async function refreshSession({ identityUrl, cookieHeader, client, fetchImpl = fetch, timeoutMs = REFRESH_TIMEOUT_MS, }) {
76
+ let res;
77
+ try {
78
+ res = await fetchImpl(`${identityUrl.replace(/\/+$/, '')}/api/v1/auth/refresh`, {
79
+ method: 'POST',
80
+ headers: {
81
+ cookie: cookieHeader,
82
+ // §5.1.6: the caller, unproven, so NOT x-boost33-app, which identity
83
+ // checks against a service key this request does not carry.
84
+ 'x-boost33-client': client,
85
+ },
86
+ signal: AbortSignal.timeout(timeoutMs),
87
+ cache: 'no-store',
88
+ });
89
+ }
90
+ catch {
91
+ return { kind: 'unwell', status: null };
92
+ }
93
+ // Only a 401 means the session is dead. Collapsing anything else into it would
94
+ // sign out everybody during an identity blip, silently (§5.1.7).
95
+ if (res.status === 401)
96
+ return { kind: 'dead' };
97
+ if (!res.ok)
98
+ return { kind: 'unwell', status: res.status };
99
+ // §5.1.3: both cookies come back (`b33_sid` slides forward too). Forward all.
100
+ return { kind: 'refreshed', setCookies: res.headers.getSetCookie() };
101
+ }
102
+ /**
103
+ * The `name=value` pair at the front of a Set-Cookie header, for rewriting the
104
+ * inbound Cookie header (§5.1.2). Only the pair is read; the header itself is
105
+ * forwarded untouched (§5.1.3).
106
+ */
107
+ export function cookiePair(setCookie) {
108
+ const pair = setCookie.split(';', 1)[0] ?? '';
109
+ const eq = pair.indexOf('=');
110
+ if (eq <= 0)
111
+ return null;
112
+ return { name: pair.slice(0, eq).trim(), value: pair.slice(eq + 1).trim() };
113
+ }
114
+ /** The cookies on a request's Cookie header, by name. Works on any `Request`. */
115
+ function cookiesOf(request) {
116
+ const jar = new Map();
117
+ for (const part of (request.headers.get('cookie') ?? '').split(';')) {
118
+ const eq = part.indexOf('=');
119
+ if (eq <= 0)
120
+ continue;
121
+ const name = part.slice(0, eq).trim();
122
+ if (name && !jar.has(name))
123
+ jar.set(name, part.slice(eq + 1).trim());
124
+ }
125
+ return jar;
126
+ }
127
+ /**
128
+ * The gate's decision without building a response: §5.1 in full (one refresh,
129
+ * 1.5s, exp - 30s, `x-boost33-client`, 401 versus unwell, the re-issued token
130
+ * verified before anything is forwarded). `gateRequest` is this, turned into
131
+ * the response a plain proxy returns. Takes any `Request`.
132
+ */
133
+ export async function gate(request, options) {
134
+ const jar = cookiesOf(request);
135
+ if (!jar.get(SID_COOKIE))
136
+ return { outcome: 'none' };
137
+ if (!ctxLooksExpired(jar.get(CTX_COOKIE)))
138
+ return { outcome: 'live' };
139
+ const refresh = options.refresh ??
140
+ ((cookieHeader) => refreshSession({ identityUrl: options.identityUrl, cookieHeader, client: options.client }));
141
+ const outcome = await refresh(request.headers.get('cookie') ?? '');
142
+ if (outcome.kind === 'dead') {
143
+ // §5.1.7: no retry; identity would only bounce them back.
144
+ const signin = new URL(`${options.identityUrl.replace(/\/+$/, '')}/signin`);
145
+ signin.searchParams.set('return', returnUrl(request));
146
+ const headers = new Headers({ location: signin.toString() });
147
+ for (const name of [SID_COOKIE, CTX_COOKIE])
148
+ headers.append('set-cookie', expiredCookie(name, request));
149
+ return { outcome: 'dead', redirect: new Response(null, { status: 307, headers }) };
150
+ }
151
+ if (outcome.kind === 'unwell')
152
+ return { outcome: 'unwell' };
153
+ // The re-issued token is verified here, not only by the page, so a refresh
154
+ // whose token would not verify (wrong key, keys unreachable) changes nothing:
155
+ // no cookie is forwarded, and the page takes the path it took before.
156
+ for (const raw of outcome.setCookies) {
157
+ const pair = cookiePair(raw);
158
+ if (pair)
159
+ jar.set(pair.name, pair.value);
160
+ }
161
+ const fresh = jar.get(CTX_COOKIE);
162
+ if (!fresh || !(await options.verify(fresh).catch(() => false)))
163
+ return { outcome: 'unwell' };
164
+ return {
165
+ outcome: 'refreshed',
166
+ freshCtx: fresh,
167
+ setCookies: outcome.setCookies,
168
+ cookieHeader: [...jar].map(([name, value]) => `${name}=${value}`).join('; '),
169
+ };
170
+ }
171
+ /**
172
+ * The request gate: step 2 of identity's contract §5, with §5.1's server-side
173
+ * refresh, as the response a proxy returns. Run it on navigations only
174
+ * (SESSION_GATE_MATCHER, or `isNavigation` in code behind a broad matcher).
175
+ *
176
+ * `b33_sid` present, `b33_ctx` absent or past `exp - 30s`
177
+ * → POST identity's /auth/refresh, relaying `b33_sid` (one call, 1.5s cap)
178
+ * → refreshed: verify the new `b33_ctx` with the app's verifier, forward
179
+ * identity's Set-Cookie headers verbatim on the response, AND rewrite
180
+ * this request's Cookie header so the page rendering it reads the new
181
+ * token (§5.1.2; without that the page still redirects)
182
+ * → 401: the session is dead. Clear both cookies and sign in.
183
+ * → anything else (timeout, network, 5xx, 429), or a re-issued token that
184
+ * does not verify: fall through unchanged, forwarding nothing. The page's
185
+ * own full verify then fails on the expired token and takes the path it
186
+ * always has. Never a pass: nothing here lets a request through on a
187
+ * token that was not verified.
188
+ *
189
+ * Everything else (no `b33_sid`, a live `b33_ctx`) passes through untouched,
190
+ * and the page decides from claims it verifies in full. This gate never tells
191
+ * the page who is signed in, and never reads `design`.
192
+ *
193
+ * The gate itself never redirects on the unwell path, so it cannot loop. An
194
+ * app whose page redirects to identity when it finds no claims needs the
195
+ * one-bounce guard §5.1.7 asks for: see `markBounce` and `isRepeatBounce`.
196
+ */
197
+ export async function gateRequest(request, options) {
198
+ const result = await gate(request, options);
199
+ if (result.outcome === 'dead') {
200
+ const response = NextResponse.redirect(result.redirect.headers.get('location'));
201
+ for (const raw of result.redirect.headers.getSetCookie())
202
+ response.headers.append('set-cookie', raw);
203
+ return response;
204
+ }
205
+ if (result.outcome !== 'refreshed')
206
+ return NextResponse.next();
207
+ const headers = new Headers(request.headers);
208
+ headers.set('cookie', result.cookieHeader);
209
+ const response = NextResponse.next({ request: { headers } });
210
+ // §5.1.3: verbatim, never rebuilt. Both cookies: `b33_sid` slides forward too.
211
+ for (const raw of result.setCookies)
212
+ response.headers.append('set-cookie', raw);
213
+ return response;
214
+ }
215
+ /**
216
+ * Whether this request is a navigation the gate should run on, decided in code
217
+ * the way SESSION_GATE_MATCHER decides it in config: a document request that
218
+ * accepts HTML, or an in-app navigation (`rsc: 1`); never a prefetch; never
219
+ * Next's own assets, `api/`, or a file with an extension. For an app whose
220
+ * proxy must run on every request for other reasons and so cannot narrow its
221
+ * matcher.
222
+ */
223
+ export function isNavigation(request) {
224
+ const { pathname } = new URL(request.url);
225
+ if (pathname.startsWith('/_next/') || pathname.startsWith('/api/'))
226
+ return false;
227
+ if (/\.[^/]*$/.test(pathname))
228
+ return false;
229
+ if (isPrefetch(request))
230
+ return false;
231
+ const h = request.headers;
232
+ return (h.get('accept') ?? '').includes('text/html') || h.get('rsc') === '1';
233
+ }
234
+ /**
235
+ * Whether the request says it is a prefetch: Next's router (`next-router-prefetch`),
236
+ * or a browser's speculative load (`purpose: prefetch`, `sec-purpose: prefetch`).
237
+ *
238
+ * THESE HEADERS ARE SET BY THE CLIENT. Use this only to decide whether to SPEND a
239
+ * refresh, never to decide whether to run your proxy at all. In an app whose
240
+ * pages trust a header the proxy sets, the proxy must run on every request
241
+ * (CLAIMS_PROXY_MATCHER) and call this to skip the refresh alone.
242
+ */
243
+ export function isPrefetch(request) {
244
+ const h = request.headers;
245
+ return (h.has('next-router-prefetch') ||
246
+ h.get('purpose') === 'prefetch' ||
247
+ (h.get('sec-purpose') ?? '').split(';')[0]?.trim() === 'prefetch');
248
+ }
249
+ /**
250
+ * Where identity sends them back to after sign-in: the forwarded host first, so
251
+ * it is the address the person used rather than the one the platform routed to.
252
+ */
253
+ function returnUrl(request) {
254
+ const { pathname, search, origin } = new URL(request.url);
255
+ const host = request.headers.get('x-forwarded-host') ?? request.headers.get('host');
256
+ if (!host)
257
+ return `${origin}${pathname}${search}`;
258
+ const local = host.startsWith('localhost') || host.startsWith('127.0.0.1');
259
+ const proto = request.headers.get('x-forwarded-proto') ?? (local ? 'http' : 'https');
260
+ return `${proto}://${host}${pathname}${search}`;
261
+ }
262
+ /**
263
+ * Expire one of identity's cookies. They are set on `.boost33.com`, so a
264
+ * host-only expiry would leave the shared cookie alive; the domain is used only
265
+ * when this host is under it, so local development still clears.
266
+ */
267
+ function expiredCookie(name, request) {
268
+ const url = new URL(request.url);
269
+ const host = (request.headers.get('x-forwarded-host') ?? url.hostname).split(':')[0] ?? '';
270
+ const shared = host === 'boost33.com' || host.endsWith('.boost33.com');
271
+ const domain = shared ? '; Domain=.boost33.com' : '';
272
+ const secure = shared || url.protocol === 'https:' ? '; Secure' : '';
273
+ return `${name}=; Path=/${domain}; Max-Age=0; HttpOnly; SameSite=Lax${secure}`;
274
+ }
275
+ /* ── The one-bounce guard (§5.1.7) ─────────────────────────────────────────
276
+ *
277
+ * For an app's own redirect to identity when a page finds no claims, which is
278
+ * the path an unwell identity takes. Mark the return URL on the way out; on the
279
+ * way back, a page that still finds no claims and sees the mark sends the
280
+ * person to a terminal page instead of to identity again. Without it, an
281
+ * unwell identity is an infinite redirect rather than a slow page.
282
+ */
283
+ export const BOUNCE_PARAM = 'b33_bounced';
284
+ /** The return URL with the bounce mark added. Pass it as identity's `return`. */
285
+ export function markBounce(returnTo) {
286
+ const url = new URL(returnTo);
287
+ url.searchParams.set(BOUNCE_PARAM, '1');
288
+ return url.toString();
289
+ }
290
+ /** Whether this arrival is already the bounce back from identity. */
291
+ export function isRepeatBounce(url) {
292
+ return new URL(url).searchParams.get(BOUNCE_PARAM) === '1';
293
+ }
294
+ /**
295
+ * FOR APPS WHOSE PAGES VERIFY `b33_ctx` THEMSELVES, and only those. The Hub is
296
+ * one: every page runs its own `jwtVerify`, and the proxy sets no request
297
+ * header a page trusts. If your proxy hands claims to the page in a request
298
+ * header, use CLAIMS_PROXY_MATCHER instead. This matcher's prefetch exclusions
299
+ * read headers the CLIENT sets, so a document request marked as a prefetch
300
+ * skips your proxy entirely, and a page that trusts a proxy-set header would
301
+ * then trust whatever the client sent.
302
+ *
303
+ * The matcher the gate must run behind (§5.1.1): navigations only, meaning a
304
+ * document request that accepts HTML, or an in-app navigation, which Next sends
305
+ * as an RSC fetch marked `rsc: 1`. Never a prefetch, so hovering links cannot
306
+ * start a refresh storm; never Next's own assets or a file with an extension.
307
+ * API routes authenticate on their own (service keys), so `api/` is excluded.
308
+ *
309
+ * Copy this value into your proxy's `export const config` AS A LITERAL. Next
310
+ * reads the matcher at build time and ignores a value held in a variable or
311
+ * imported, silently, which would run the gate on every request.
312
+ */
313
+ export const SESSION_GATE_MATCHER = [
314
+ {
315
+ source: '/((?!_next/|api/|.*\\..*).*)',
316
+ has: [{ type: 'header', key: 'accept', value: '(.*)text/html(.*)' }],
317
+ missing: [
318
+ { type: 'header', key: 'next-router-prefetch' },
319
+ { type: 'header', key: 'purpose', value: 'prefetch' },
320
+ ],
321
+ },
322
+ {
323
+ source: '/((?!_next/|api/|.*\\..*).*)',
324
+ has: [{ type: 'header', key: 'rsc', value: '1' }],
325
+ missing: [
326
+ { type: 'header', key: 'next-router-prefetch' },
327
+ { type: 'header', key: 'purpose', value: 'prefetch' },
328
+ ],
329
+ },
330
+ ];
331
+ /**
332
+ * The matcher for an app whose proxy hands claims to its pages in a request
333
+ * header (Results, `x-results-claims`). It runs the proxy on EVERY request but
334
+ * Next's own static assets, with no `has`/`missing` conditions, because any
335
+ * condition on a client-set header is a way around the proxy. In code:
336
+ *
337
+ * headers.delete('x-<app>-claims') // always, first
338
+ * if (isNavigation(request)) result = await gate(request, options)
339
+ * // set the claims header only from claims your proxy verified
340
+ *
341
+ * `isNavigation` (and so `isPrefetch`) only decides whether to spend a refresh.
342
+ * A request that lies about being a prefetch skips the refresh, never the proxy.
343
+ *
344
+ * Copy it into `export const config` AS A LITERAL, as with SESSION_GATE_MATCHER.
345
+ */
346
+ export const CLAIMS_PROXY_MATCHER = ['/((?!_next/static|_next/image).*)'];
347
+ /**
348
+ * For an app's tests: whether its literal matcher copy is SESSION_GATE_MATCHER,
349
+ * or narrows only its paths (Outbound scopes it to its Backdrop routes). A
350
+ * matcher that drops the prefetch exclusion or the `_next/`/`api/` exclusions
351
+ * fails.
352
+ *
353
+ * Passing says the matcher spends no refresh on a prefetch. It does NOT say the
354
+ * app is safe: this matcher is only safe where pages verify `b33_ctx`
355
+ * themselves. An app that passes claims in a request header must use
356
+ * CLAIMS_PROXY_MATCHER, and this check does not apply to it.
357
+ */
358
+ export function matcherIsNavigationOnly(matcher) {
359
+ if (!Array.isArray(matcher) || matcher.length === 0)
360
+ return false;
361
+ return matcher.every((entry) => {
362
+ if (!entry || typeof entry !== 'object')
363
+ return false;
364
+ const { source, has, missing } = entry;
365
+ if (typeof source !== 'string')
366
+ return false;
367
+ const scoped = !source.startsWith('/((?!');
368
+ if (!scoped && !(source.includes('_next/') && source.includes('api/')))
369
+ return false;
370
+ const json = JSON.stringify;
371
+ const navigation = SESSION_GATE_MATCHER.some((ref) => json(has) === json(ref.has));
372
+ const noPrefetch = json(missing) === json(SESSION_GATE_MATCHER[0].missing);
373
+ return navigation && noPrefetch;
374
+ });
375
+ }
376
+ //# sourceMappingURL=session.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session.js","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,6EAA6E;AAC7E,0EAA0E;AAC1E,uDAAuD;AACvD,OAAO,EAAE,SAAS,EAAE,MAAM,MAAM,CAAA;AAChC,OAAO,EAAE,YAAY,EAAoB,MAAM,gBAAgB,CAAA;AAE/D;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,MAAM,CAAC,MAAM,UAAU,GAAG,SAAS,CAAA;AACnC,MAAM,CAAC,MAAM,UAAU,GAAG,SAAS,CAAA;AAEnC,iFAAiF;AACjF,MAAM,CA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