@fluixi/oauth2 1.0.0-alpha.10 → 1.0.0-alpha.101

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.
Files changed (69) hide show
  1. package/LICENSE +1 -1
  2. package/dist/api-guard.cjs +1 -1
  3. package/dist/api-guard.mjs +1 -1
  4. package/dist/backchannel.cjs +462 -0
  5. package/dist/backchannel.d.ts +39 -0
  6. package/dist/backchannel.d.ts.map +1 -0
  7. package/dist/backchannel.js +97 -0
  8. package/dist/backchannel.mjs +307 -0
  9. package/dist/{browser-36FHB6KG.mjs → browser-3AN2ABKG.mjs} +2 -2
  10. package/dist/browser.cjs +6 -1
  11. package/dist/browser.mjs +6 -1
  12. package/dist/callback.cjs +2 -2
  13. package/dist/callback.d.ts +2 -2
  14. package/dist/callback.js +3 -3
  15. package/dist/callback.mjs +3 -3
  16. package/dist/{chunk-OGS6MQ3D.mjs → chunk-AYQ2UUAK.mjs} +1 -1
  17. package/dist/chunk-LJV5WIJI.mjs +108 -0
  18. package/dist/{chunk-F52RUVDM.mjs → chunk-TJDXJSUS.mjs} +1 -1
  19. package/dist/discovery.cjs +6 -1
  20. package/dist/discovery.d.ts.map +1 -1
  21. package/dist/discovery.js +6 -1
  22. package/dist/discovery.mjs +6 -1
  23. package/dist/{dpop-GOWCCCFR.mjs → dpop-SSEBKRGF.mjs} +4 -4
  24. package/dist/dpop-SYK6ZHW4.mjs +22 -0
  25. package/dist/env-config.cjs +1 -1
  26. package/dist/env-config.mjs +1 -1
  27. package/dist/index.cjs +108 -8
  28. package/dist/index.d.ts +2 -0
  29. package/dist/index.d.ts.map +1 -1
  30. package/dist/index.js +1 -0
  31. package/dist/index.mjs +112 -12
  32. package/dist/module.cjs +1 -1
  33. package/dist/module.mjs +1 -1
  34. package/dist/presets.cjs +1 -1
  35. package/dist/presets.mjs +1 -1
  36. package/dist/roles.cjs +1 -1
  37. package/dist/roles.d.ts +1 -1
  38. package/dist/roles.js +1 -1
  39. package/dist/roles.mjs +1 -1
  40. package/dist/server.cjs +705 -143
  41. package/dist/server.d.ts +71 -4
  42. package/dist/server.d.ts.map +1 -1
  43. package/dist/server.js +456 -65
  44. package/dist/server.mjs +588 -145
  45. package/dist/service-client.cjs +7 -2
  46. package/dist/service-client.mjs +9 -4
  47. package/dist/service.cjs +104 -5
  48. package/dist/service.d.ts +1 -1
  49. package/dist/service.d.ts.map +1 -1
  50. package/dist/service.js +38 -6
  51. package/dist/service.mjs +105 -6
  52. package/dist/session-monitor.cjs +98 -0
  53. package/dist/session-monitor.d.ts +32 -0
  54. package/dist/session-monitor.d.ts.map +1 -0
  55. package/dist/session-monitor.js +104 -0
  56. package/dist/session-monitor.mjs +77 -0
  57. package/dist/tokens.cjs +1 -1
  58. package/dist/tokens.d.ts +2 -2
  59. package/dist/tokens.d.ts.map +1 -1
  60. package/dist/tokens.js +1 -1
  61. package/dist/tokens.mjs +1 -1
  62. package/dist/tsconfig.lib.tsbuildinfo +1 -1
  63. package/dist/types.d.ts +8 -0
  64. package/dist/types.d.ts.map +1 -1
  65. package/dist/verify.cjs +8 -3
  66. package/dist/verify.d.ts +2 -2
  67. package/dist/verify.js +2 -2
  68. package/dist/verify.mjs +13 -8
  69. package/package.json +22 -16
package/dist/server.js CHANGED
@@ -21,7 +21,7 @@
21
21
  * cookieSecret: process.env.SESSION_SECRET!,
22
22
  * });
23
23
  *
24
- * export default defineMiddleware([auth, …]);
24
+ * export default defineMiddleware([auth, ...]);
25
25
  * ```
26
26
  *
27
27
  * The cookie itself is `@fluixi/session/server`, which knows nothing about OAuth: it
@@ -32,10 +32,86 @@ import { createSessionCookie } from '@fluixi/session/server';
32
32
  import { safeReturnTo } from './return-to.js';
33
33
  import { applyClientAuth, } from './client-auth.js';
34
34
  import { discover } from './discovery.js';
35
+ import { idTokenClaims, logoutTokenVerifier, memoryLogouts } from './backchannel.js';
35
36
  import { createPkcePair, createStateValue, expiryFrom, timingSafeEqual } from './pkce.js';
36
37
  import { OAuthError } from './types.js';
37
38
  import { createDpopKey, exportDpopKey, importDpopKey, nonceFrom, proofFor } from './dpop.js';
38
39
  import { defineModule } from '@fluixi/start/app';
40
+ /** Ends the state of a sign-in started again after an answer the browser had no flow for. */
41
+ const RETRY_MARK = '.retry';
42
+ const PROMPTS = new Set(['none', 'login', 'consent', 'select_account']);
43
+ /** What a `prompt=none` request ends with when the provider would have to ask the visitor. */
44
+ const SILENT_REFUSALS = new Set([
45
+ 'login_required',
46
+ 'consent_required',
47
+ 'interaction_required',
48
+ 'account_selection_required',
49
+ ]);
50
+ /**
51
+ * The cookie saying the provider was just asked silently and knew nobody. It holds the
52
+ * `session_state` that answer carried, which the page watches for a later sign-in. It
53
+ * expires soon on purpose: kept for the browser session, it hid a sign-in made at the
54
+ * provider while no page was open to notice.
55
+ */
56
+ function checkedCookie(config, sessionState) {
57
+ const name = `${config.cookieName ?? 'fx_session'}_checked`;
58
+ const secure = config.secureCookie ?? process.env.NODE_ENV === 'production';
59
+ const maxAge = Math.max(0, Math.floor(config.silentRecheck ?? 60));
60
+ const value = sessionState ?? '1';
61
+ return { name, header: `${name}=${value}; Path=/; Max-Age=${maxAge}; HttpOnly; SameSite=Lax${secure ? '; Secure' : ''}` };
62
+ }
63
+ /** A `session_state` as Session Management builds it, `<hash>.<salt>`; anything else is dropped. */
64
+ function sessionStateFrom(url) {
65
+ const value = url.searchParams.get('session_state');
66
+ return value && /^[\w-]{1,128}\.[\w-]{1,64}$/.test(value) ? value : undefined;
67
+ }
68
+ /** A document load, as opposed to a fetch, an asset or a form post. */
69
+ function isPageRequest(request) {
70
+ if (request.method !== 'GET')
71
+ return false;
72
+ const dest = request.headers.get('sec-fetch-dest');
73
+ if (dest)
74
+ return dest === 'document';
75
+ return (request.headers.get('accept') ?? '').includes('text/html');
76
+ }
77
+ function cookieValue(request, name) {
78
+ for (const part of (request.headers.get('cookie') ?? '').split(';')) {
79
+ const [key, ...rest] = part.trim().split('=');
80
+ if (key === name)
81
+ return rest.join('=');
82
+ }
83
+ return undefined;
84
+ }
85
+ function hasCookie(request, name) {
86
+ return cookieValue(request, name) !== undefined;
87
+ }
88
+ /** Whether `pathname` is `path` or below it. */
89
+ function under(pathname, path) {
90
+ return pathname === path || pathname.startsWith(path.endsWith('/') ? path : `${path}/`);
91
+ }
92
+ /** Where `signIn` sends a signed-out visitor's page request, or null to render it as is. */
93
+ function signInRedirect(request, config) {
94
+ if (!isPageRequest(request))
95
+ return null;
96
+ const url = new URL(request.url);
97
+ const base = (config.basePath ?? '/auth').replace(/\/$/, '');
98
+ const guarded = (config.protectedPaths ?? []).some((path) => under(url.pathname, path));
99
+ const mode = guarded ? 'required' : (config.signIn ?? 'manual');
100
+ if (mode === 'manual')
101
+ return null;
102
+ if ([base, ...(config.publicPaths ?? [])].some((path) => under(url.pathname, path)))
103
+ return null;
104
+ // Back from a refused sign-in: show the page and its error, or a required sign-in would
105
+ // send the visitor straight back to the provider, which refuses again, in a loop.
106
+ if (url.searchParams.has('auth_error'))
107
+ return null;
108
+ if (mode === 'silent' && hasCookie(request, checkedCookie(config).name))
109
+ return null;
110
+ const login = new URLSearchParams({ returnTo: url.pathname + url.search });
111
+ if (mode === 'silent')
112
+ login.set('prompt', 'none');
113
+ return new Response(null, { status: 302, headers: { location: `${base}/login?${login}` } });
114
+ }
39
115
  /**
40
116
  * Nonces providers asked for, per origin, shared by the routes and `sessionLocals`. A nonce
41
117
  * is the server's to set (RFC 9449 §8, §9), so the latest one it sent is the one to use.
@@ -56,38 +132,76 @@ function learnNonce(url, response) {
56
132
  return fresh && !response.ok;
57
133
  }
58
134
  /**
59
- * A request carrying the session's access token: the `DPoP` scheme and a proof bound to the
60
- * token when the provider bound it, `Bearer` otherwise.
135
+ * Sends a request with the session's access token: as a bearer token, or with a DPoP proof
136
+ * for this method and URL when the token is bound to the session's key. A nonce the server
137
+ * asks for is learned and the request sent once more, so the body must be one that can be
138
+ * sent twice (a string, not a stream).
61
139
  */
62
- async function withToken(url, session) {
140
+ async function sendAs(session, input, init = {}) {
141
+ const url = String(input);
142
+ const method = (init.method ?? 'GET').toUpperCase();
63
143
  const key = session.dpop && session.tokenType?.toLowerCase() === 'dpop' ? await importDpopKey(session.dpop) : null;
64
144
  const send = async () => {
65
- const headers = {
145
+ const credentials = {
66
146
  authorization: `${key ? 'DPoP' : 'Bearer'} ${session.accessToken}`,
67
147
  };
68
148
  if (key)
69
- await prove(headers, key, 'GET', url, session.accessToken);
70
- return fetch(url, { headers });
149
+ await prove(credentials, key, method, url, session.accessToken);
150
+ const headers = new Headers(init.headers);
151
+ for (const [name, value] of Object.entries(credentials))
152
+ headers.set(name, value);
153
+ return fetch(url, { ...init, method, headers });
71
154
  };
72
155
  let response = await send();
73
156
  if (key && learnNonce(url, response))
74
157
  response = await send();
75
158
  return response;
76
159
  }
160
+ const withToken = (url, session) => sendAs(session, url);
77
161
  /**
78
- * The middleware.
79
- *
80
- * Handles `<basePath>/login`, `/callback`, `/refresh`, `/me` and `/logout`, and passes
81
- * anything else along untouched.
162
+ * The provider did not process the token request: it answered 429 or 5xx, or the connection
163
+ * never opened. The same request may be sent again after `retryAfterMs`.
82
164
  */
83
- export function oauthRoutes(config) {
84
- const base = (config.basePath ?? '/auth').replace(/\/$/, '');
85
- const scopes = config.scopes ?? ['openid', 'profile', 'email'];
86
- const cookie = createSessionCookie({
87
- secret: config.cookieSecret,
88
- name: config.cookieName,
89
- secure: config.secureCookie,
90
- });
165
+ class ProviderUnavailable extends OAuthError {
166
+ retryAfterMs;
167
+ constructor(retryAfterMs) {
168
+ super('The authorization server is unavailable.', 'temporarily_unavailable');
169
+ this.retryAfterMs = retryAfterMs;
170
+ }
171
+ }
172
+ /**
173
+ * The request may have been processed, but its answer is unusable: it timed out, the
174
+ * connection broke after it was sent, or a success came back without an access token. A
175
+ * refresh token sent this way may already have been rotated.
176
+ */
177
+ class AnswerLost extends OAuthError {
178
+ constructor(what) {
179
+ super(`The token request's answer was lost (${what}).`, 'answer_lost');
180
+ }
181
+ }
182
+ /** Connection failures that happen before the request is sent. */
183
+ const NOT_SENT = new Set(['ECONNREFUSED', 'ENOTFOUND', 'EAI_AGAIN', 'ENETUNREACH', 'EHOSTUNREACH', 'UND_ERR_CONNECT_TIMEOUT']);
184
+ const RETRY_DEFAULT_MS = 5_000;
185
+ const RETRY_MAX_MS = 300_000;
186
+ /** The pause a 429 or 5xx asks for (`Retry-After`, seconds or a date), within sane bounds. */
187
+ function retryAfter(response) {
188
+ const header = response.headers.get('retry-after');
189
+ if (!header)
190
+ return RETRY_DEFAULT_MS;
191
+ const seconds = Number(header);
192
+ const ms = Number.isFinite(seconds) ? seconds * 1000 : Date.parse(header) - Date.now();
193
+ return Number.isFinite(ms) ? Math.min(Math.max(ms, 1_000), RETRY_MAX_MS) : RETRY_DEFAULT_MS;
194
+ }
195
+ /** What a failed `fetch` means for the request: never sent, or possibly processed. */
196
+ function unanswered(error) {
197
+ const code = error?.cause?.code;
198
+ if (code && NOT_SENT.has(code))
199
+ return new ProviderUnavailable(RETRY_DEFAULT_MS);
200
+ return new AnswerLost(code ?? (error instanceof Error ? error.name : 'no answer'));
201
+ }
202
+ /** Endpoints, client authentication and the token request, for the routes and `sessionLocals`. */
203
+ function tokenClient(config) {
204
+ const timeoutMs = config.tokenTimeoutMs ?? 10_000;
91
205
  let endpoints = config.endpoints ?? null;
92
206
  const resolve = async () => {
93
207
  if (endpoints)
@@ -98,10 +212,6 @@ export function oauthRoutes(config) {
98
212
  endpoints = await discover(config.issuer);
99
213
  return endpoints;
100
214
  };
101
- const json = (body, status = 200, headers = {}) => new Response(JSON.stringify(body), {
102
- status,
103
- headers: { 'content-type': 'application/json', ...headers },
104
- });
105
215
  const clientAuth = {
106
216
  clientId: config.clientId,
107
217
  // `client_secret_post` by default here, which is what this half has always sent, so
@@ -124,29 +234,186 @@ export function oauthRoutes(config) {
124
234
  await applyClientAuth(clientAuth, token, headers, form);
125
235
  if (key)
126
236
  await prove(headers, key, 'POST', token);
127
- return fetch(token, { method: 'POST', headers, body: form.toString() });
237
+ return fetch(token, { method: 'POST', headers, body: form.toString(), signal: AbortSignal.timeout(timeoutMs) });
128
238
  };
129
- let response = await attempt();
130
- if (key && learnNonce(token, response))
239
+ let response;
240
+ let payload = null;
241
+ try {
131
242
  response = await attempt();
132
- const payload = (await response.json().catch(() => ({})));
243
+ if (key && learnNonce(token, response))
244
+ response = await attempt();
245
+ if (response.status === 429 || response.status >= 500)
246
+ throw new ProviderUnavailable(retryAfter(response));
247
+ payload = (await response.json().catch(() => null));
248
+ }
249
+ catch (error) {
250
+ throw error instanceof OAuthError ? error : unanswered(error);
251
+ }
133
252
  if (!response.ok) {
134
- throw new OAuthError(String(payload.error ?? 'token_request_failed'), String(payload.error ?? 'token_request_failed'));
253
+ throw new OAuthError(String(payload?.error ?? 'token_request_failed'), String(payload?.error ?? 'token_request_failed'));
254
+ }
255
+ // A token endpoint's success always carries one (RFC 6749 5.1). Anything else (a proxy's
256
+ // page, a truncated body) is not a session, and the provider may have rotated the token.
257
+ if (typeof payload?.access_token !== 'string' || !payload.access_token) {
258
+ throw new AnswerLost('a success without an access token');
135
259
  }
136
260
  return payload;
137
261
  }
262
+ return { resolve, tokenRequest, clientAuth };
263
+ }
264
+ /**
265
+ * Renewals in flight or just finished, by the refresh token they spent. The provider rotates
266
+ * refresh tokens and treats a second use of one as theft, revoking the whole family: requests
267
+ * that arrive together with the same expired cookie (a page and its server functions) must
268
+ * share one renewal, and one that arrives a moment later with the old cookie still gets its
269
+ * result. One process only; replicas need the provider to allow a short reuse window.
270
+ */
271
+ const renewals = new Map();
272
+ const RENEWAL_REUSE_MS = 30_000;
273
+ /** How long a refused or possibly rotated refresh token stays recorded, so it is not sent again. */
274
+ const SPENT_MS = 5 * 60_000;
275
+ /** The session with fresh tokens, or null when the provider refuses to renew it. */
276
+ function renew(current, tokenRequest) {
277
+ const refreshToken = current.refreshToken;
278
+ const now = Date.now();
279
+ for (const [key, entry] of renewals)
280
+ if (entry.until <= now)
281
+ renewals.delete(key);
282
+ const known = renewals.get(refreshToken);
283
+ if (known)
284
+ return known.result;
285
+ const result = (async () => {
286
+ // The key that obtained the refresh token, or the provider refuses the refresh.
287
+ const key = current.dpop ? await importDpopKey(current.dpop) : null;
288
+ const payload = await tokenRequest({ grant_type: 'refresh_token', refresh_token: refreshToken }, key);
289
+ return {
290
+ accessToken: String(payload.access_token ?? ''),
291
+ // A provider that rotates refresh tokens sends a new one; one that does not
292
+ // expects the old one to keep working.
293
+ refreshToken: typeof payload.refresh_token === 'string' ? payload.refresh_token : refreshToken,
294
+ idToken: typeof payload.id_token === 'string' ? payload.id_token : current.idToken,
295
+ expiresAt: expiryFrom(payload.expires_in),
296
+ tokenType: typeof payload.token_type === 'string' ? payload.token_type : current.tokenType,
297
+ dpop: current.dpop,
298
+ sessionState: current.sessionState,
299
+ };
300
+ })().catch((error) => {
301
+ // Not processed (the provider down or busy): nothing was spent. Ask again after the pause.
302
+ if (error instanceof ProviderUnavailable) {
303
+ renewals.set(refreshToken, { until: Date.now() + error.retryAfterMs, result });
304
+ throw error;
305
+ }
306
+ // Refused, or answered in a way that was lost after the provider may have rotated the
307
+ // token: the session is over either way. Sending the token again would be a replay, which
308
+ // the provider answers by ending every session of this sign-in.
309
+ if (error instanceof OAuthError) {
310
+ renewals.set(refreshToken, { until: Date.now() + SPENT_MS, result });
311
+ return null;
312
+ }
313
+ renewals.delete(refreshToken);
314
+ throw error;
315
+ });
316
+ renewals.set(refreshToken, { until: now + RENEWAL_REUSE_MS, result });
317
+ return result;
318
+ }
319
+ /** Renewed a little before it runs out, so a request does not reach an API with a dying token. */
320
+ const EXPIRY_SKEW_MS = 30_000;
321
+ const expired = (session) => session.expiresAt !== undefined && session.expiresAt - EXPIRY_SKEW_MS <= Date.now();
322
+ /**
323
+ * The middleware.
324
+ *
325
+ * Handles `<basePath>/login`, `/callback`, `/refresh`, `/me`, `/session-check` and
326
+ * `/logout`, and passes anything else along untouched.
327
+ */
328
+ // One default store per configuration, so the routes and sessionLocals built from it share it.
329
+ const defaultLogouts = new WeakMap();
330
+ function logoutsOf(config) {
331
+ if (config.logouts)
332
+ return config.logouts;
333
+ let store = defaultLogouts.get(config);
334
+ if (!store)
335
+ defaultLogouts.set(config, (store = memoryLogouts()));
336
+ return store;
337
+ }
338
+ /** Whether the provider has ended this session over the back channel. */
339
+ async function endedAtProvider(config, session) {
340
+ const id = idTokenClaims(session.idToken);
341
+ if (!id.sid && !id.sub)
342
+ return false;
343
+ return logoutsOf(config).ended({ sid: id.sid, sub: id.sub, since: (id.iat ?? 0) * 1000 });
344
+ }
345
+ export function oauthRoutes(config) {
346
+ const { resolve, tokenRequest, clientAuth } = tokenClient(config);
347
+ const json = (body, status = 200, headers = {}) => new Response(JSON.stringify(body), {
348
+ status,
349
+ headers: { 'content-type': 'application/json', ...headers },
350
+ });
351
+ const base = (config.basePath ?? '/auth').replace(/\/$/, '');
352
+ const scopes = config.scopes ?? ['openid', 'profile', 'email'];
353
+ const cookie = createSessionCookie({
354
+ secret: config.cookieSecret,
355
+ name: config.cookieName,
356
+ secure: config.secureCookie,
357
+ });
138
358
  return async function middleware(request, next) {
139
359
  const url = new URL(request.url);
140
360
  if (!url.pathname.startsWith(`${base}/`))
141
361
  return next();
142
362
  const route = url.pathname.slice(base.length);
143
363
  const session = async () => {
144
- return cookie.read(request);
364
+ const current = await cookie.read(request);
365
+ return current && (await endedAtProvider(config, current)) ? null : current;
145
366
  };
367
+ if (route === '/backchannel-logout') {
368
+ // The provider, server to server: a session this app took part in has ended there.
369
+ const headers = { 'cache-control': 'no-store' };
370
+ if (request.method !== 'POST')
371
+ return json({ error: 'invalid_request' }, 405, headers);
372
+ try {
373
+ const form = await request.formData();
374
+ const token = form.get('logout_token');
375
+ if (typeof token !== 'string' || !token)
376
+ throw new Error('no logout_token');
377
+ const endpoints = await resolve();
378
+ const issuer = endpoints.issuer ?? config.issuer;
379
+ if (!issuer)
380
+ throw new Error('no issuer');
381
+ if (!endpoints.jwks)
382
+ throw new Error('no jwks_uri');
383
+ // Read fresh for each logout (they are rare), so a rotated key is always known.
384
+ const jwks = (await (await fetch(endpoints.jwks)).json());
385
+ const entry = await logoutTokenVerifier({ issuer, clientId: config.clientId, jwks })(token);
386
+ await logoutsOf(config).add(entry);
387
+ return new Response(null, { status: 200, headers });
388
+ }
389
+ catch {
390
+ return json({ error: 'invalid_request' }, 400, headers);
391
+ }
392
+ }
146
393
  if (route === '/login') {
147
- const { authorization } = await resolve();
394
+ let authorization;
395
+ try {
396
+ ({ authorization } = await resolve());
397
+ }
398
+ catch (error) {
399
+ if (!(error instanceof OAuthError) || error.code !== 'discovery_unreachable')
400
+ throw error;
401
+ // The provider is down: back to the app, which can say so, rather than an error page.
402
+ const back = safeReturnTo(url.searchParams.get('returnTo'), {
403
+ fallback: config.defaultReturnTo ?? '/',
404
+ origin: url.origin,
405
+ allowedOrigins: config.allowedReturnOrigins,
406
+ });
407
+ const target = new URL(back, url.origin);
408
+ target.searchParams.set('auth_error', 'provider_unreachable');
409
+ const location = target.origin === url.origin ? target.pathname + target.search : target.toString();
410
+ return new Response(null, { status: 302, headers: { location } });
411
+ }
148
412
  const { verifier, challenge } = await createPkcePair();
149
- const state = createStateValue();
413
+ // A retry carries its mark in the state itself: the one place that survives the trip
414
+ // when the cookie does not, so a second failed attempt is told apart from the first.
415
+ const retry = url.searchParams.get('retry') === '1';
416
+ const state = createStateValue() + (retry ? RETRY_MARK : '');
150
417
  const target = new URL(authorization);
151
418
  target.searchParams.set('response_type', 'code');
152
419
  target.searchParams.set('client_id', config.clientId);
@@ -155,6 +422,10 @@ export function oauthRoutes(config) {
155
422
  target.searchParams.set('state', state);
156
423
  target.searchParams.set('code_challenge', challenge);
157
424
  target.searchParams.set('code_challenge_method', 'S256');
425
+ // Only the values OpenID Connect defines; anything else from the query string is dropped.
426
+ const prompt = url.searchParams.get('prompt');
427
+ if (prompt && PROMPTS.has(prompt))
428
+ target.searchParams.set('prompt', prompt);
158
429
  // The verifier goes in the cookie, which is the only thing that survives the trip
159
430
  // and is not readable by the page.
160
431
  const pendingCookie = await cookie.write({
@@ -169,6 +440,7 @@ export function oauthRoutes(config) {
169
440
  origin: url.origin,
170
441
  allowedOrigins: config.allowedReturnOrigins,
171
442
  }) || undefined,
443
+ silent: prompt === 'none' || undefined,
172
444
  },
173
445
  },
174
446
  // Ten minutes: long enough to sign in, short enough that an abandoned attempt
@@ -186,14 +458,52 @@ export function oauthRoutes(config) {
186
458
  const current = await session();
187
459
  const pending = current?.pending;
188
460
  const error = url.searchParams.get('error');
189
- if (error)
190
- return json({ error }, 400);
461
+ if (error) {
462
+ // Back to the page the flow started from, never a JSON answer the visitor reads.
463
+ const ours = !!pending && timingSafeEqual(pending.state, url.searchParams.get('state') ?? '');
464
+ let location = safeReturnTo(ours ? pending.returnTo ?? config.defaultReturnTo : config.defaultReturnTo, {
465
+ fallback: '/',
466
+ origin: url.origin,
467
+ allowedOrigins: config.allowedReturnOrigins,
468
+ });
469
+ const headers = new Headers();
470
+ headers.append('set-cookie', cookie.clear());
471
+ if (ours && pending.silent && SILENT_REFUSALS.has(error)) {
472
+ // Nobody signed in at the provider: remember it was asked, and show the page as is.
473
+ headers.append('set-cookie', checkedCookie(config, sessionStateFrom(url)).header);
474
+ }
475
+ else {
476
+ const target = new URL(location, url.origin);
477
+ target.searchParams.set('auth_error', error);
478
+ location = target.origin === url.origin ? target.pathname + target.search : target.toString();
479
+ }
480
+ headers.set('location', location);
481
+ return new Response(null, { status: 302, headers });
482
+ }
191
483
  const code = url.searchParams.get('code');
192
484
  const state = url.searchParams.get('state');
193
485
  if (!code)
194
486
  return json({ error: 'no_code' }, 400);
195
487
  if (!pending || !state || !timingSafeEqual(pending.state, state)) {
196
- return json({ error: 'state_mismatch' }, 400);
488
+ // An answer this browser has no flow for: its cookie expired (a sign-in that took longer
489
+ // than ten minutes, a provider page refreshed long after), or another tab started a
490
+ // newer one. Already signed in, the answer is a leftover. Otherwise start again once,
491
+ // and only then give up, on a page rather than with JSON.
492
+ const home = safeReturnTo(config.defaultReturnTo, { fallback: '/', origin: url.origin, allowedOrigins: config.allowedReturnOrigins });
493
+ if (current?.accessToken)
494
+ return new Response(null, { status: 302, headers: { location: home } });
495
+ if (!state?.endsWith(RETRY_MARK)) {
496
+ return new Response(null, { status: 302, headers: { location: `${base}/login?retry=1` } });
497
+ }
498
+ const target = new URL(home, url.origin);
499
+ target.searchParams.set('auth_error', 'state_mismatch');
500
+ return new Response(null, {
501
+ status: 302,
502
+ headers: {
503
+ location: target.origin === url.origin ? target.pathname + target.search : target.toString(),
504
+ 'set-cookie': cookie.clear(),
505
+ },
506
+ });
197
507
  }
198
508
  // The token is bound here, at the exchange, so this is where the session's key is made.
199
509
  const key = config.dpop
@@ -212,6 +522,7 @@ export function oauthRoutes(config) {
212
522
  expiresAt: expiryFrom(payload.expires_in),
213
523
  tokenType: typeof payload.token_type === 'string' ? payload.token_type : undefined,
214
524
  dpop: key ? await exportDpopKey(key) : undefined,
525
+ sessionState: sessionStateFrom(url),
215
526
  });
216
527
  return new Response(null, {
217
528
  status: 302,
@@ -232,22 +543,10 @@ export function oauthRoutes(config) {
232
543
  const current = await session();
233
544
  if (!current?.refreshToken)
234
545
  return json({ error: 'no_session' }, 401);
235
- // The key that obtained the refresh token, or the provider refuses the refresh.
236
- const key = current.dpop ? await importDpopKey(current.dpop) : null;
237
- const payload = await tokenRequest({ grant_type: 'refresh_token', refresh_token: current.refreshToken }, key);
238
- const refreshed = await cookie.write({
239
- accessToken: String(payload.access_token ?? ''),
240
- // A provider that rotates refresh tokens sends a new one; one that does not
241
- // expects the old one to keep working.
242
- refreshToken: typeof payload.refresh_token === 'string'
243
- ? payload.refresh_token
244
- : current.refreshToken,
245
- idToken: current.idToken,
246
- expiresAt: expiryFrom(payload.expires_in),
247
- tokenType: typeof payload.token_type === 'string' ? payload.token_type : current.tokenType,
248
- dpop: current.dpop,
249
- });
250
- return json({ ok: true }, 200, { 'set-cookie': refreshed });
546
+ const renewed = await renew(current, tokenRequest);
547
+ if (!renewed)
548
+ return json({ error: 'refresh_refused' }, 401, { 'set-cookie': cookie.clear() });
549
+ return json({ ok: true }, 200, { 'set-cookie': await cookie.write(renewed) });
251
550
  }
252
551
  if (route === '/me') {
253
552
  const current = await session();
@@ -262,6 +561,16 @@ export function oauthRoutes(config) {
262
561
  const claims = (await response.json());
263
562
  return json(config.mapUser ? config.mapUser(claims, current.accessToken) : claims);
264
563
  }
564
+ if (route === '/session-check') {
565
+ // What the page needs to watch the provider's session: its check page, this client,
566
+ // and the last answer's session_state, signed in or not. Nothing secret: the state is
567
+ // a salted hash bound to this origin.
568
+ const { checkSession } = await resolve().catch(() => ({ checkSession: undefined }));
569
+ const current = await session();
570
+ const checked = cookieValue(request, checkedCookie(config).name);
571
+ const state = current?.accessToken ? current.sessionState : checked !== '1' ? checked : undefined;
572
+ return json({ frame: checkSession ?? null, clientId: config.clientId, state: state ?? null });
573
+ }
265
574
  if (route === '/revoke') {
266
575
  const current = await session();
267
576
  const hint = url.searchParams.get('hint') === 'access_token' ? 'access_token' : 'refresh_token';
@@ -302,18 +611,21 @@ export function oauthRoutes(config) {
302
611
  }
303
612
  // Max-Age=0 is what removes it. Clearing the value alone leaves a cookie behind.
304
613
  const cleared = cookie.clear();
614
+ // Signed out on purpose: `silent` must not sign them straight back in.
615
+ const headers = new Headers();
616
+ headers.append('set-cookie', cleared);
617
+ headers.append('set-cookie', checkedCookie(config).header);
305
618
  if (endSession && current?.idToken) {
306
619
  const target = new URL(endSession);
307
620
  target.searchParams.set('id_token_hint', current.idToken);
308
621
  if (config.defaultReturnTo) {
309
622
  target.searchParams.set('post_logout_redirect_uri', config.defaultReturnTo);
310
623
  }
311
- return new Response(null, {
312
- status: 302,
313
- headers: { location: target.toString(), 'set-cookie': cleared },
314
- });
624
+ headers.set('location', target.toString());
625
+ return new Response(null, { status: 302, headers });
315
626
  }
316
- return json({ ok: true }, 200, { 'set-cookie': cleared });
627
+ headers.set('content-type', 'application/json');
628
+ return new Response(JSON.stringify({ ok: true }), { status: 200, headers });
317
629
  }
318
630
  return next();
319
631
  };
@@ -349,6 +661,7 @@ export function sessionLocals(config) {
349
661
  name: config.cookieName,
350
662
  secure: config.secureCookie,
351
663
  });
664
+ const { tokenRequest } = tokenClient(config);
352
665
  const ttl = config.userCacheMs ?? 30_000;
353
666
  /**
354
667
  * Keyed by the access token, which this process already holds in the cookie it just
@@ -376,23 +689,78 @@ export function sessionLocals(config) {
376
689
  return async function middleware(request, next) {
377
690
  const { getRequestLocals } = await import('@fluixi/server');
378
691
  const locals = getRequestLocals(request);
379
- const session = await cookie.read(request);
692
+ let session = await cookie.read(request);
693
+ // What the browser's cookie must become: renewed tokens, or nothing.
694
+ let setCookie;
695
+ const respond = async (response) => {
696
+ if (!setCookie)
697
+ return response;
698
+ const copy = new Response(response.body, response);
699
+ copy.headers.append('set-cookie', setCookie);
700
+ return copy;
701
+ };
702
+ const renewOrDrop = async (current) => {
703
+ const renewed = current.refreshToken ? await renew(current, tokenRequest) : null;
704
+ setCookie = renewed ? await cookie.write(renewed) : cookie.clear();
705
+ return renewed;
706
+ };
707
+ // An access token lives minutes, the session days: renew it here, on the request that
708
+ // finds it expired, rather than rendering a signed-in visitor as nobody.
709
+ // Set when the provider could not renew now: the session stays, it is retried later.
710
+ let paused = false;
711
+ const keep = (current) => (error) => {
712
+ if (error instanceof ProviderUnavailable)
713
+ paused = true;
714
+ return current;
715
+ };
716
+ if (session?.accessToken && expired(session)) {
717
+ session = await renewOrDrop(session).catch(keep(session));
718
+ }
380
719
  // Written either way. A route that reads locals should find "nobody" rather than
381
720
  // nothing, so the absent case and the signed-out case look the same to it.
382
- if (!session?.accessToken) {
721
+ const signedOut = async () => {
722
+ const redirect = signInRedirect(request, config);
723
+ if (redirect)
724
+ return respond(redirect);
383
725
  locals.auth = { user: null, session: null };
384
- return next();
726
+ return respond(await next());
727
+ };
728
+ if (!session?.accessToken)
729
+ return signedOut();
730
+ // Ended at the provider (back-channel logout): this session is over too.
731
+ if (await endedAtProvider(config, session)) {
732
+ setCookie = cookie.clear();
733
+ session = null;
734
+ return signedOut();
385
735
  }
386
- const cached = cache.get(session.accessToken);
736
+ // Server code calls the app's APIs as this user with it; the token itself stays here.
737
+ const live = session;
738
+ const fetchAs = (input, init) => sendAs(live, input, init);
739
+ const cached = cache.get(live.accessToken);
387
740
  if (cached && cached.until > Date.now()) {
388
- locals.auth = { user: cached.user, session: { expiresAt: session.expiresAt } };
389
- return next();
741
+ locals.auth = { user: cached.user, session: { expiresAt: live.expiresAt }, fetch: fetchAs };
742
+ return respond(await next());
390
743
  }
391
744
  let user = null;
745
+ let unreachable = false;
392
746
  try {
393
747
  const endpoints = config.endpoints ?? (config.issuer ? await discover(config.issuer) : null);
394
748
  if (endpoints?.userinfo) {
395
- const response = await withToken(endpoints.userinfo, session);
749
+ let response = await withToken(endpoints.userinfo, session);
750
+ // Refused before its time (revoked, a clock apart): renew once, and when that is
751
+ // refused too the session is over, so the visitor goes the signed-out way.
752
+ if (response.status === 401 && !setCookie && !paused) {
753
+ session = await renewOrDrop(session).catch(keep(session));
754
+ if (!session)
755
+ return signedOut();
756
+ response = await withToken(endpoints.userinfo, session);
757
+ }
758
+ if (response.status === 401) {
759
+ // Not renewable just now: signed out for this request, renewed on a later one.
760
+ if (!paused)
761
+ setCookie = cookie.clear();
762
+ return signedOut();
763
+ }
396
764
  if (response.ok) {
397
765
  const claims = (await response.json());
398
766
  user = config.mapUser ? config.mapUser(claims, session.accessToken) : claims;
@@ -404,12 +772,34 @@ export function sessionLocals(config) {
404
772
  }
405
773
  catch {
406
774
  // A provider that is unreachable should not take the page down with it. The render
407
- // proceeds as signed out, and the client retries on its own.
775
+ // proceeds as signed out, and the client retries on its own; `unreachable` tells
776
+ // server code it is not the visitor who is signed out.
777
+ unreachable = true;
408
778
  }
409
- locals.auth = { user, session: user ? { expiresAt: session.expiresAt } : null };
410
- return next();
779
+ if (!session)
780
+ return signedOut();
781
+ const current = session;
782
+ const fetchCurrent = (input, init) => sendAs(current, input, init);
783
+ locals.auth = user
784
+ ? { user, session: { expiresAt: current.expiresAt }, fetch: fetchCurrent }
785
+ : { user: null, session: null, ...(unreachable ? { unreachable: true } : {}) };
786
+ return respond(await next());
411
787
  };
412
788
  }
789
+ /**
790
+ * The request's auth, for server code: a route's data loader, a server function, an API route.
791
+ * Reads the current request's locals, or `request`'s when one is given.
792
+ *
793
+ * ```ts
794
+ * const { fetch } = await requestAuth();
795
+ * const projects = await fetch?.(`${apiUrl}/projects`).then((r) => r.json());
796
+ * ```
797
+ */
798
+ export async function requestAuth(request) {
799
+ const { getLocals, getRequestLocals } = await import('@fluixi/server');
800
+ const locals = (request ? getRequestLocals(request) : getLocals());
801
+ return locals.auth ?? { user: null, session: null };
802
+ }
413
803
  /**
414
804
  * The server half of `authModule`, listed in `src/app.config.server.ts`: the routes and the
415
805
  * session middleware that `tokens: 'server'` needs, in the order they have to run.
@@ -420,3 +810,4 @@ export function sessionLocals(config) {
420
810
  export function authServerModule(config) {
421
811
  return defineModule({ name: 'oauth2-server', middleware: [oauthRoutes(config), sessionLocals(config)] });
422
812
  }
813
+ export { memoryLogouts } from './backchannel.js';