@udibo/oauth2 0.5.1 → 0.7.0

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.
@@ -260,6 +260,23 @@ export interface HonoBffBackchannelOptions {
260
260
  * `{ sub, sid }` to log out. Throw or return `null` to reject the request.
261
261
  */
262
262
  verifyLogoutToken: (logoutToken: string) => Promise<BackchannelLogoutSubject | null> | BackchannelLogoutSubject | null;
263
+ /**
264
+ * The largest request body, in bytes, the receiver reads. The body is read
265
+ * before the `logout_token` is verified, so the cap bounds what an
266
+ * anonymous caller can make the BFF buffer. Bytes are counted as they
267
+ * arrive, so a chunked body or a `Content-Length` that understates the body
268
+ * cannot get past it. A larger body is refused with `400 invalid_request` —
269
+ * the status OIDC Back-Channel Logout 1.0 §2.8 requires for a failed
270
+ * request — without calling
271
+ * {@linkcode HonoBffBackchannelOptions.verifyLogoutToken}.
272
+ *
273
+ * The default is far above a `logout_token`, a JWT of a few KiB. When an
274
+ * earlier middleware has already read the body, the receiver uses Hono's
275
+ * parsed copy and the cap does not apply; bound the body in that middleware.
276
+ *
277
+ * @default 65536
278
+ */
279
+ maxBodyBytes?: number;
263
280
  }
264
281
  /**
265
282
  * Why `/auth/callback` could not complete a sign-in. Passed to
@@ -609,7 +626,9 @@ export declare class HonoBff {
609
626
  * {@linkcode HonoBffPaths.basePath}, when the session cookie would die
610
627
  * before the session behind it (see
611
628
  * {@linkcode HonoBffOptions.sessionMaxAgeMs}), when `sessionMaxAgeMs` or a
612
- * numeric `cookie.maxAge` is not positive or exceeds 400 days, or when
629
+ * numeric `cookie.maxAge` is not positive or exceeds 400 days, when
630
+ * {@linkcode HonoBffBackchannelOptions.maxBodyBytes} is not a positive
631
+ * integer (a `RangeError`), or when
613
632
  * {@linkcode HonoBffOptions.extraParams} or
614
633
  * {@linkcode HonoBffOptions.forwardedParams} names a reserved authorize
615
634
  * parameter, gives one a name that is empty or not equal to its trimmed
@@ -717,7 +736,9 @@ export declare class HonoBff {
717
736
  * carrying a signed `logout_token`; the configured verifier validates it (and
718
737
  * returns the subject / OP session id), then the matching server-side
719
738
  * session(s) are destroyed. Responds `200` on success and `400` on a
720
- * missing/invalid token, with `Cache-Control: no-store`, per the spec.
739
+ * missing/invalid token or a body over
740
+ * {@linkcode HonoBffBackchannelOptions.maxBodyBytes}, with
741
+ * `Cache-Control: no-store`, per the spec.
721
742
  */
722
743
  backchannelHandler(): Handler;
723
744
  /**
@@ -49,6 +49,7 @@
49
49
  */
50
50
  import { Hono } from "hono";
51
51
  import { deleteCookie, getCookie, setCookie } from "hono/cookie";
52
+ import { assertMaxBodyBytes, DEFAULT_MAX_BODY_BYTES, readBoundedBody, } from "../../../utils/_body.js";
52
53
  import { InvalidRequestError, InvalidTokenError, isOAuth2Error, TemporarilyUnavailableError, } from "../../../errors.js";
53
54
  import { timingSafeMatchIndex } from "../../../utils/_timing-safe.js";
54
55
  import { sha256Hash } from "../../../server/utils/hash.js";
@@ -303,7 +304,9 @@ export class HonoBff {
303
304
  * {@linkcode HonoBffPaths.basePath}, when the session cookie would die
304
305
  * before the session behind it (see
305
306
  * {@linkcode HonoBffOptions.sessionMaxAgeMs}), when `sessionMaxAgeMs` or a
306
- * numeric `cookie.maxAge` is not positive or exceeds 400 days, or when
307
+ * numeric `cookie.maxAge` is not positive or exceeds 400 days, when
308
+ * {@linkcode HonoBffBackchannelOptions.maxBodyBytes} is not a positive
309
+ * integer (a `RangeError`), or when
307
310
  * {@linkcode HonoBffOptions.extraParams} or
308
311
  * {@linkcode HonoBffOptions.forwardedParams} names a reserved authorize
309
312
  * parameter, gives one a name that is empty or not equal to its trimmed
@@ -363,6 +366,9 @@ export class HonoBff {
363
366
  }
364
367
  }
365
368
  }
369
+ if (options.backchannelLogout?.maxBodyBytes !== undefined) {
370
+ assertMaxBodyBytes(options.backchannelLogout.maxBodyBytes, "backchannelLogout.maxBodyBytes");
371
+ }
366
372
  if (options.backchannelLogout && !supportsBackchannelLogout(this.#store)) {
367
373
  throw new Error("backchannelLogout requires a SessionStore that implements " +
368
374
  "destroyByLogout() (a stateful store). The stateless " +
@@ -775,7 +781,9 @@ export class HonoBff {
775
781
  * carrying a signed `logout_token`; the configured verifier validates it (and
776
782
  * returns the subject / OP session id), then the matching server-side
777
783
  * session(s) are destroyed. Responds `200` on success and `400` on a
778
- * missing/invalid token, with `Cache-Control: no-store`, per the spec.
784
+ * missing/invalid token or a body over
785
+ * {@linkcode HonoBffBackchannelOptions.maxBodyBytes}, with
786
+ * `Cache-Control: no-store`, per the spec.
779
787
  */
780
788
  backchannelHandler() {
781
789
  return async (c) => {
@@ -785,7 +793,16 @@ export class HonoBff {
785
793
  return c.json({ error: "not_supported" }, 400);
786
794
  let logoutToken;
787
795
  try {
788
- logoutToken = (await c.req.formData()).get("logout_token")?.toString();
796
+ const form = c.req.raw.bodyUsed
797
+ ? await c.req.formData()
798
+ : await this.#readBackchannelForm(c, opts);
799
+ if (!form) {
800
+ return c.json({
801
+ error: "invalid_request",
802
+ error_description: "request body is too large",
803
+ }, 400);
804
+ }
805
+ logoutToken = form.get("logout_token")?.toString();
789
806
  }
790
807
  catch {
791
808
  logoutToken = undefined;
@@ -816,6 +833,14 @@ export class HonoBff {
816
833
  return c.body(null, 200);
817
834
  };
818
835
  }
836
+ async #readBackchannelForm(c, opts) {
837
+ const bytes = await readBoundedBody(c.req.raw, opts.maxBodyBytes ?? DEFAULT_MAX_BODY_BYTES);
838
+ if (!bytes)
839
+ return null;
840
+ return await new Response(bytes, {
841
+ headers: { "content-type": c.req.header("content-type") ?? "" },
842
+ }).formData();
843
+ }
819
844
  async #resolveSessionEntry(c) {
820
845
  const cookieValue = getCookie(c, this.#cookieName);
821
846
  if (!cookieValue)
@@ -32,8 +32,11 @@ export type RedirectPolicy = "refuse" | "follow";
32
32
  * @param what The endpoint's name, for error messages.
33
33
  * @param options `redirect` policy and `timeoutMs` deadline.
34
34
  * @returns The response, which may still be a non-OK status.
35
- * @throws {ServerError} when the request cannot be made, times out, or (under
36
- * `"refuse"`) answers with a redirect.
35
+ * @throws {TemporarilyUnavailableError} when the request cannot be made or its
36
+ * response headers do not arrive before the deadline; `cause` is the
37
+ * transport failure.
38
+ * @throws {ServerError} when (under `"refuse"`) the endpoint answers with a
39
+ * redirect.
37
40
  */
38
41
  export declare function sendGuarded(fetchImpl: typeof fetch, url: string, init: RequestInit, what: Endpoint, options?: {
39
42
  redirect?: RedirectPolicy;
@@ -48,6 +51,8 @@ export declare function sendGuarded(fetchImpl: typeof fetch, url: string, init:
48
51
  * @param res The response from {@link sendGuarded}.
49
52
  * @param what The endpoint's name, for error messages.
50
53
  * @returns The parsed JSON object.
54
+ * @throws {TemporarilyUnavailableError} on a `2xx` whose body stops arriving
55
+ * within the size cap — the deadline fires or the connection drops mid-body.
51
56
  * @throws {OAuth2Error} on a non-OK status, an oversized body, a body that is
52
57
  * not valid JSON, or JSON that is not an object.
53
58
  */
@@ -47,8 +47,11 @@ const ERROR_CLASS_BY_CODE = {
47
47
  * @param what The endpoint's name, for error messages.
48
48
  * @param options `redirect` policy and `timeoutMs` deadline.
49
49
  * @returns The response, which may still be a non-OK status.
50
- * @throws {ServerError} when the request cannot be made, times out, or (under
51
- * `"refuse"`) answers with a redirect.
50
+ * @throws {TemporarilyUnavailableError} when the request cannot be made or its
51
+ * response headers do not arrive before the deadline; `cause` is the
52
+ * transport failure.
53
+ * @throws {ServerError} when (under `"refuse"`) the endpoint answers with a
54
+ * redirect.
52
55
  */
53
56
  export async function sendGuarded(fetchImpl, url, init, what, options = {}) {
54
57
  const redirect = options.redirect ?? "refuse";
@@ -62,7 +65,7 @@ export async function sendGuarded(fetchImpl, url, init, what, options = {}) {
62
65
  });
63
66
  }
64
67
  catch (error) {
65
- throw new ServerError(`could not reach the ${what} (${describeError(error)})`, { cause: error });
68
+ throw new TemporarilyUnavailableError(`could not reach the ${what} (${describeError(error)})`, { cause: error });
66
69
  }
67
70
  if (redirect === "refuse" && res.status >= 300 && res.status < 400) {
68
71
  await res.body?.cancel();
@@ -81,6 +84,8 @@ export async function sendGuarded(fetchImpl, url, init, what, options = {}) {
81
84
  * @param res The response from {@link sendGuarded}.
82
85
  * @param what The endpoint's name, for error messages.
83
86
  * @returns The parsed JSON object.
87
+ * @throws {TemporarilyUnavailableError} on a `2xx` whose body stops arriving
88
+ * within the size cap — the deadline fires or the connection drops mid-body.
84
89
  * @throws {OAuth2Error} on a non-OK status, an oversized body, a body that is
85
90
  * not valid JSON, or JSON that is not an object.
86
91
  */
@@ -92,6 +97,10 @@ export async function receiveJson(res, what) {
92
97
  throw new ServerError(`the ${what} response exceeded ${MAX_RESPONSE_BYTES} bytes — refusing ` +
93
98
  `to parse a response this large`);
94
99
  }
100
+ if (body.interrupted) {
101
+ throw new TemporarilyUnavailableError(`the ${what} response body did not arrive in full ` +
102
+ `(${describeError(body.interrupted.cause)})`, { cause: body.interrupted.cause });
103
+ }
95
104
  let parsed;
96
105
  try {
97
106
  parsed = JSON.parse(body.text);
@@ -184,8 +193,8 @@ async function readBounded(res) {
184
193
  size += value.byteLength;
185
194
  }
186
195
  }
187
- catch {
188
- return { text: decode(chunks, size), truncated: true };
196
+ catch (cause) {
197
+ return { text: decode(chunks, size), truncated, interrupted: { cause } };
189
198
  }
190
199
  finally {
191
200
  await reader.cancel().catch(() => { });
@@ -96,9 +96,10 @@ export interface CheckPermissionsResult {
96
96
  * deleted after the signature, so catch it on that path rather than reading a
97
97
  * throw as a bug in your own setup. A redirect, or a `2xx` whose body is not a
98
98
  * JSON object or exceeds the response-size cap, also throws
99
- * {@linkcode ServerError}, and a request whose response headers do not
100
- * arrive before the deadline throws {@linkcode TemporarilyUnavailableError}.
101
- * The body's shape is not validated beyond being a JSON object.
99
+ * {@linkcode ServerError}. A response that does not arrive in full before the
100
+ * deadline — whether its headers or its body stall — or whose connection
101
+ * drops mid-body throws {@linkcode TemporarilyUnavailableError}. The body's
102
+ * shape is not validated beyond being a JSON object.
102
103
  *
103
104
  * @example
104
105
  * ```ts
@@ -50,9 +50,10 @@ const CHECK_ENDPOINT = "check endpoint";
50
50
  * deleted after the signature, so catch it on that path rather than reading a
51
51
  * throw as a bug in your own setup. A redirect, or a `2xx` whose body is not a
52
52
  * JSON object or exceeds the response-size cap, also throws
53
- * {@linkcode ServerError}, and a request whose response headers do not
54
- * arrive before the deadline throws {@linkcode TemporarilyUnavailableError}.
55
- * The body's shape is not validated beyond being a JSON object.
53
+ * {@linkcode ServerError}. A response that does not arrive in full before the
54
+ * deadline — whether its headers or its body stall — or whose connection
55
+ * drops mid-body throws {@linkcode TemporarilyUnavailableError}. The body's
56
+ * shape is not validated beyond being a JSON object.
56
57
  *
57
58
  * @example
58
59
  * ```ts
@@ -73,38 +74,17 @@ const CHECK_ENDPOINT = "check endpoint";
73
74
  * ```
74
75
  */
75
76
  export async function checkPermissions(options) {
76
- const fetchImpl = options.fetch ?? fetch;
77
- const reportUnreachable = async (input, init) => {
78
- try {
79
- return await fetchImpl(input, init);
80
- }
81
- catch (cause) {
82
- throw new TemporarilyUnavailableError("check endpoint unreachable", {
83
- cause,
84
- });
85
- }
86
- };
87
- let response;
88
- try {
89
- response = await sendGuarded(reportUnreachable, options.endpoint, {
90
- method: "POST",
91
- headers: {
92
- authorization: `Bearer ${options.accessToken}`,
93
- "content-type": "application/json",
94
- },
95
- body: JSON.stringify({
96
- permissions: options.permissions,
97
- ...(options.resource ? { resource: options.resource } : {}),
98
- }),
99
- }, CHECK_ENDPOINT);
100
- }
101
- catch (error) {
102
- if (error instanceof Error &&
103
- error.cause instanceof TemporarilyUnavailableError) {
104
- throw error.cause;
105
- }
106
- throw error;
107
- }
77
+ const response = await sendGuarded(options.fetch ?? fetch, options.endpoint, {
78
+ method: "POST",
79
+ headers: {
80
+ authorization: `Bearer ${options.accessToken}`,
81
+ "content-type": "application/json",
82
+ },
83
+ body: JSON.stringify({
84
+ permissions: options.permissions,
85
+ ...(options.resource ? { resource: options.resource } : {}),
86
+ }),
87
+ }, CHECK_ENDPOINT);
108
88
  if (response.status >= 500) {
109
89
  await response.body?.cancel();
110
90
  throw new TemporarilyUnavailableError(`check endpoint answered ${response.status}`);
@@ -290,9 +290,13 @@ export declare class DirectClient extends OAuth2ClientBase {
290
290
  * @returns The server's metadata document.
291
291
  * @throws {Error} when the client was configured with explicit `endpoints`
292
292
  * rather than an `issuer` — there is nothing to discover.
293
+ * @throws {TemporarilyUnavailableError} the last attempt's error when both
294
+ * well-known paths fail because the server cannot be reached or its
295
+ * response does not arrive in full before the deadline; `cause` is the
296
+ * transport failure.
293
297
  * @throws {OAuth2Error} the last attempt's error when both well-known paths
294
- * fail: the server is unreachable, answers non-OK or with something other
295
- * than a JSON object, or reports an `issuer` other than the configured one.
298
+ * fail otherwise: the server answers non-OK or with something other than a
299
+ * JSON object, or reports an `issuer` other than the configured one.
296
300
  *
297
301
  * @example
298
302
  * ```ts
@@ -377,6 +381,9 @@ export declare class DirectClient extends OAuth2ClientBase {
377
381
  *
378
382
  * @param input The callback URL, its query string, or the parsed params.
379
383
  * @returns The persisted tokens and the `returnTo` recorded at login.
384
+ * @throws {TemporarilyUnavailableError} when the token endpoint cannot be
385
+ * reached or its response does not arrive in full before the deadline;
386
+ * `cause` is the transport failure.
380
387
  * @throws {OAuth2Error} when the callback carries `?error=…`, or the token
381
388
  * exchange fails.
382
389
  * @throws {TypeError} when `code` or `state` is missing.
@@ -422,6 +429,9 @@ export declare class DirectClient extends OAuth2ClientBase {
422
429
  * `returnTo`, plus the raw response.
423
430
  * @throws {InvalidGrantError} when `state` is unknown or older than
424
431
  * `authRequestTtlMs`.
432
+ * @throws {TemporarilyUnavailableError} when the token endpoint cannot be
433
+ * reached or its response does not arrive in full before the deadline;
434
+ * `cause` is the transport failure.
425
435
  */
426
436
  exchangeAuthorizationCode(code: string, state: string, options?: ExchangeOptions): Promise<ExchangeResult>;
427
437
  /**
@@ -440,6 +450,9 @@ export declare class DirectClient extends OAuth2ClientBase {
440
450
  *
441
451
  * @param refreshToken The refresh token to redeem.
442
452
  * @returns The new tokens, the rotated refresh token, and the raw response.
453
+ * @throws {TemporarilyUnavailableError} when the token endpoint cannot be
454
+ * reached or its response does not arrive in full before the deadline;
455
+ * `cause` is the transport failure.
443
456
  * @throws {OAuth2Error} when the token endpoint rejects the grant.
444
457
  */
445
458
  exchangeRefreshToken(refreshToken: string): Promise<ExchangeResult>;
@@ -451,6 +464,9 @@ export declare class DirectClient extends OAuth2ClientBase {
451
464
  * @returns The persisted token bundle.
452
465
  * @throws {Error} when the client has no `clientSecret` — RFC 6749 §4.4
453
466
  * restricts this grant to confidential clients.
467
+ * @throws {TemporarilyUnavailableError} when the token endpoint cannot be
468
+ * reached or its response does not arrive in full before the deadline;
469
+ * `cause` is the transport failure.
454
470
  * @throws {OAuth2Error} when the token endpoint rejects the grant.
455
471
  *
456
472
  * @example
@@ -491,6 +507,11 @@ export declare class DirectClient extends OAuth2ClientBase {
491
507
  * one is dead.
492
508
  * @throws {Error} whatever the configured `RefreshTokenStorage` threw when
493
509
  * the read itself failed.
510
+ * @throws {TemporarilyUnavailableError} when the token endpoint cannot be
511
+ * reached or its response does not arrive in full before the deadline;
512
+ * `cause` is the transport failure.
513
+ * @throws {OAuth2Error} when the token endpoint rejects the refresh for
514
+ * another reason.
494
515
  */
495
516
  refresh(): Promise<string>;
496
517
  /**
@@ -502,6 +523,9 @@ export declare class DirectClient extends OAuth2ClientBase {
502
523
  * @returns The server's device authorization response.
503
524
  * @throws {Error} when no device authorization endpoint is configured or
504
525
  * discoverable.
526
+ * @throws {TemporarilyUnavailableError} when the device authorization
527
+ * endpoint cannot be reached or its response does not arrive in full before
528
+ * the deadline; `cause` is the transport failure.
505
529
  * @throws {OAuth2Error} when the server rejects the request.
506
530
  *
507
531
  * @example
@@ -534,6 +558,9 @@ export declare class DirectClient extends OAuth2ClientBase {
534
558
  * @throws {InvalidGrantError} when `expiresAt` passes before approval. A
535
559
  * server that reports the code expired first throws `ExpiredTokenError`
536
560
  * instead.
561
+ * @throws {TemporarilyUnavailableError} when the token endpoint cannot be
562
+ * reached or its response does not arrive in full before the deadline;
563
+ * `cause` is the transport failure. Polling stops on it.
537
564
  * @throws {OAuth2Error} on any other terminal error from the server.
538
565
  */
539
566
  pollDeviceToken(deviceCode: string, options?: PollDeviceTokenOptions): Promise<TokenBundle>;
@@ -547,6 +574,9 @@ export declare class DirectClient extends OAuth2ClientBase {
547
574
  * @returns The introspection response; check `active`.
548
575
  * @throws {Error} when the client has no `clientSecret` — RFC 7662 requires
549
576
  * an authenticated caller.
577
+ * @throws {TemporarilyUnavailableError} when the introspection endpoint
578
+ * cannot be reached or its response does not arrive in full before the
579
+ * deadline; `cause` is the transport failure.
550
580
  * @throws {OAuth2Error} when the endpoint rejects the request.
551
581
  */
552
582
  introspect(token: string, options?: {
@@ -560,6 +590,9 @@ export declare class DirectClient extends OAuth2ClientBase {
560
590
  * @param options `tokenTypeHint` tells the server which kind to look up
561
591
  * first.
562
592
  * @throws {Error} when no revocation endpoint is configured or discoverable.
593
+ * @throws {TemporarilyUnavailableError} when the revocation endpoint cannot
594
+ * be reached or its response does not arrive in full before the deadline;
595
+ * `cause` is the transport failure.
563
596
  * @throws {OAuth2Error} when the endpoint rejects the request.
564
597
  */
565
598
  revoke(token: string, options?: {
@@ -573,6 +606,9 @@ export declare class DirectClient extends OAuth2ClientBase {
573
606
  * @throws {Error} when no userinfo endpoint is configured or discoverable.
574
607
  * @throws {InvalidGrantError} when no usable token is stored and there is no
575
608
  * refresh token to mint one.
609
+ * @throws {TemporarilyUnavailableError} when the token or userinfo endpoint
610
+ * cannot be reached or its response does not arrive in full before the
611
+ * deadline; `cause` is the transport failure.
576
612
  * @throws {OAuth2Error} when the refresh or the userinfo endpoint fails.
577
613
  */
578
614
  getUserInfo(): Promise<UserInfoClaims>;
@@ -721,6 +757,9 @@ export declare class DirectClient extends OAuth2ClientBase {
721
757
  * than 30 seconds left, otherwise a freshly refreshed one.
722
758
  * @throws {InvalidGrantError} when a refresh is needed and no refresh token
723
759
  * is stored, or the stored one is dead.
760
+ * @throws {TemporarilyUnavailableError} when the token endpoint cannot be
761
+ * reached or its response does not arrive in full before the deadline;
762
+ * `cause` is the transport failure.
724
763
  * @throws {OAuth2Error} when a needed refresh fails for another reason.
725
764
  */
726
765
  getAccessToken(): Promise<string>;
@@ -159,9 +159,13 @@ export class DirectClient extends OAuth2ClientBase {
159
159
  * @returns The server's metadata document.
160
160
  * @throws {Error} when the client was configured with explicit `endpoints`
161
161
  * rather than an `issuer` — there is nothing to discover.
162
+ * @throws {TemporarilyUnavailableError} the last attempt's error when both
163
+ * well-known paths fail because the server cannot be reached or its
164
+ * response does not arrive in full before the deadline; `cause` is the
165
+ * transport failure.
162
166
  * @throws {OAuth2Error} the last attempt's error when both well-known paths
163
- * fail: the server is unreachable, answers non-OK or with something other
164
- * than a JSON object, or reports an `issuer` other than the configured one.
167
+ * fail otherwise: the server answers non-OK or with something other than a
168
+ * JSON object, or reports an `issuer` other than the configured one.
165
169
  *
166
170
  * @example
167
171
  * ```ts
@@ -368,6 +372,9 @@ export class DirectClient extends OAuth2ClientBase {
368
372
  *
369
373
  * @param input The callback URL, its query string, or the parsed params.
370
374
  * @returns The persisted tokens and the `returnTo` recorded at login.
375
+ * @throws {TemporarilyUnavailableError} when the token endpoint cannot be
376
+ * reached or its response does not arrive in full before the deadline;
377
+ * `cause` is the transport failure.
371
378
  * @throws {OAuth2Error} when the callback carries `?error=…`, or the token
372
379
  * exchange fails.
373
380
  * @throws {TypeError} when `code` or `state` is missing.
@@ -451,6 +458,9 @@ export class DirectClient extends OAuth2ClientBase {
451
458
  * `returnTo`, plus the raw response.
452
459
  * @throws {InvalidGrantError} when `state` is unknown or older than
453
460
  * `authRequestTtlMs`.
461
+ * @throws {TemporarilyUnavailableError} when the token endpoint cannot be
462
+ * reached or its response does not arrive in full before the deadline;
463
+ * `cause` is the transport failure.
454
464
  */
455
465
  async exchangeAuthorizationCode(code, state, options = {}) {
456
466
  const authRequestStorage = options.authRequestStorage ??
@@ -494,6 +504,9 @@ export class DirectClient extends OAuth2ClientBase {
494
504
  *
495
505
  * @param refreshToken The refresh token to redeem.
496
506
  * @returns The new tokens, the rotated refresh token, and the raw response.
507
+ * @throws {TemporarilyUnavailableError} when the token endpoint cannot be
508
+ * reached or its response does not arrive in full before the deadline;
509
+ * `cause` is the transport failure.
497
510
  * @throws {OAuth2Error} when the token endpoint rejects the grant.
498
511
  */
499
512
  async exchangeRefreshToken(refreshToken) {
@@ -515,6 +528,9 @@ export class DirectClient extends OAuth2ClientBase {
515
528
  * @returns The persisted token bundle.
516
529
  * @throws {Error} when the client has no `clientSecret` — RFC 6749 §4.4
517
530
  * restricts this grant to confidential clients.
531
+ * @throws {TemporarilyUnavailableError} when the token endpoint cannot be
532
+ * reached or its response does not arrive in full before the deadline;
533
+ * `cause` is the transport failure.
518
534
  * @throws {OAuth2Error} when the token endpoint rejects the grant.
519
535
  *
520
536
  * @example
@@ -566,6 +582,11 @@ export class DirectClient extends OAuth2ClientBase {
566
582
  * one is dead.
567
583
  * @throws {Error} whatever the configured `RefreshTokenStorage` threw when
568
584
  * the read itself failed.
585
+ * @throws {TemporarilyUnavailableError} when the token endpoint cannot be
586
+ * reached or its response does not arrive in full before the deadline;
587
+ * `cause` is the transport failure.
588
+ * @throws {OAuth2Error} when the token endpoint rejects the refresh for
589
+ * another reason.
569
590
  */
570
591
  refresh() {
571
592
  return this.#sharedRefresh(true);
@@ -642,6 +663,9 @@ export class DirectClient extends OAuth2ClientBase {
642
663
  * @returns The server's device authorization response.
643
664
  * @throws {Error} when no device authorization endpoint is configured or
644
665
  * discoverable.
666
+ * @throws {TemporarilyUnavailableError} when the device authorization
667
+ * endpoint cannot be reached or its response does not arrive in full before
668
+ * the deadline; `cause` is the transport failure.
645
669
  * @throws {OAuth2Error} when the server rejects the request.
646
670
  *
647
671
  * @example
@@ -687,6 +711,9 @@ export class DirectClient extends OAuth2ClientBase {
687
711
  * @throws {InvalidGrantError} when `expiresAt` passes before approval. A
688
712
  * server that reports the code expired first throws `ExpiredTokenError`
689
713
  * instead.
714
+ * @throws {TemporarilyUnavailableError} when the token endpoint cannot be
715
+ * reached or its response does not arrive in full before the deadline;
716
+ * `cause` is the transport failure. Polling stops on it.
690
717
  * @throws {OAuth2Error} on any other terminal error from the server.
691
718
  */
692
719
  async pollDeviceToken(deviceCode, options = {}) {
@@ -734,6 +761,9 @@ export class DirectClient extends OAuth2ClientBase {
734
761
  * @returns The introspection response; check `active`.
735
762
  * @throws {Error} when the client has no `clientSecret` — RFC 7662 requires
736
763
  * an authenticated caller.
764
+ * @throws {TemporarilyUnavailableError} when the introspection endpoint
765
+ * cannot be reached or its response does not arrive in full before the
766
+ * deadline; `cause` is the transport failure.
737
767
  * @throws {OAuth2Error} when the endpoint rejects the request.
738
768
  */
739
769
  async introspect(token, options = {}) {
@@ -760,6 +790,9 @@ export class DirectClient extends OAuth2ClientBase {
760
790
  * @param options `tokenTypeHint` tells the server which kind to look up
761
791
  * first.
762
792
  * @throws {Error} when no revocation endpoint is configured or discoverable.
793
+ * @throws {TemporarilyUnavailableError} when the revocation endpoint cannot
794
+ * be reached or its response does not arrive in full before the deadline;
795
+ * `cause` is the transport failure.
763
796
  * @throws {OAuth2Error} when the endpoint rejects the request.
764
797
  */
765
798
  async revoke(token, options = {}) {
@@ -785,6 +818,9 @@ export class DirectClient extends OAuth2ClientBase {
785
818
  * @throws {Error} when no userinfo endpoint is configured or discoverable.
786
819
  * @throws {InvalidGrantError} when no usable token is stored and there is no
787
820
  * refresh token to mint one.
821
+ * @throws {TemporarilyUnavailableError} when the token or userinfo endpoint
822
+ * cannot be reached or its response does not arrive in full before the
823
+ * deadline; `cause` is the transport failure.
788
824
  * @throws {OAuth2Error} when the refresh or the userinfo endpoint fails.
789
825
  */
790
826
  async getUserInfo() {
@@ -1053,6 +1089,9 @@ export class DirectClient extends OAuth2ClientBase {
1053
1089
  * than 30 seconds left, otherwise a freshly refreshed one.
1054
1090
  * @throws {InvalidGrantError} when a refresh is needed and no refresh token
1055
1091
  * is stored, or the stored one is dead.
1092
+ * @throws {TemporarilyUnavailableError} when the token endpoint cannot be
1093
+ * reached or its response does not arrive in full before the deadline;
1094
+ * `cause` is the transport failure.
1056
1095
  * @throws {OAuth2Error} when a needed refresh fails for another reason.
1057
1096
  */
1058
1097
  async getAccessToken() {
@@ -89,12 +89,20 @@ export interface AuthRequestStorage {
89
89
  * before the token call but lets concurrent callers that both read it
90
90
  * before either deletes it redeem it twice. Implement it with the backing
91
91
  * store's own atomic primitive (`GETDEL`, `DELETE … RETURNING`) whenever
92
- * the store is shared between processes or requests.
92
+ * the store is shared between processes or requests, and check it with
93
+ * `runAuthRequestStorageContractTests` from `@udibo/oauth2/testing/contract`.
93
94
  */
94
95
  take?(state: string): Promise<AuthRequestRecord | null> | AuthRequestRecord | null;
95
96
  /** Removes one entry — a claimed or stale one. */
96
97
  delete(state: string): Promise<void> | void;
97
- /** Removes every entry. Called when the client clears its session. */
98
+ /**
99
+ * Removes every entry. `DirectClient` calls it whenever it clears its
100
+ * session — on sign-out, and when a refresh finds the grant dead. A store
101
+ * shared between users may scope it instead, removing only expired records,
102
+ * so that one user's sign-out does not cancel another user's sign-in still
103
+ * in progress; register such a store with the contract suite's
104
+ * `clear: "scoped"`.
105
+ */
98
106
  clear(): Promise<void> | void;
99
107
  }
100
108
  /** Default in-memory {@link TokenStorage}. Data is lost when the process exits. */
@@ -248,6 +248,26 @@ export interface AuthorizationServerOptions<Client extends ClientInterface, User
248
248
  * bundle.
249
249
  */
250
250
  isPublicSuffix?: IsPublicSuffix;
251
+ /**
252
+ * The largest request body, in bytes, the server reads at the token,
253
+ * revocation, introspection, device authorization and end-session
254
+ * endpoints. Each reads its form body before it has authenticated the
255
+ * caller, so the cap bounds what an anonymous request can make the server
256
+ * buffer. Bytes are counted as they arrive, so a chunked body or a
257
+ * `Content-Length` that understates the body cannot get past it. A larger
258
+ * body is refused with `413` and `invalid_request` before the request's
259
+ * context is resolved or any client is authenticated.
260
+ *
261
+ * The default fits every body these endpoints legitimately receive: the
262
+ * largest field is a JWT (`id_token_hint`, or a token presented for
263
+ * introspection or revocation), a few KiB even with a large claim set.
264
+ * Raise it only if your clients send larger ones.
265
+ *
266
+ * @default 65536
267
+ * @throws {RangeError} From the constructor when it is not a positive
268
+ * integer.
269
+ */
270
+ maxBodyBytes?: number;
251
271
  }
252
272
  /**
253
273
  * Function to authenticate the user during authorization.
@@ -407,6 +427,7 @@ export declare class AuthorizationServer<Client extends ClientInterface, User, S
407
427
  *
408
428
  * @throws {Error} If any grant's map key does not equal its `grantType`.
409
429
  * @throws {Error} If `requireOidc` is set without `signingKeys`.
430
+ * @throws {RangeError} If `maxBodyBytes` is not a positive integer.
410
431
  */
411
432
  constructor(options: AuthorizationServerOptions<Client, User, S>);
412
433
  /**
@@ -453,7 +474,9 @@ export declare class AuthorizationServer<Client extends ClientInterface, User, S
453
474
  * `application/x-www-form-urlencoded`, or no `grant_type` — is a 400
454
475
  * `invalid_request`. A `grant_type` with no registered grant is
455
476
  * `unsupported_grant_type`, checked before client authentication; a client
456
- * not registered for the grant is `unauthorized_client`.
477
+ * not registered for the grant is `unauthorized_client`. A body over
478
+ * {@linkcode AuthorizationServerOptions.maxBodyBytes} is a 413
479
+ * `invalid_request`, refused before the request context is resolved.
457
480
  */
458
481
  handleTokenRequest(request: Request): Promise<Response>;
459
482
  /**
@@ -522,7 +545,9 @@ export declare class AuthorizationServer<Client extends ClientInterface, User, S
522
545
  * when sent, names the client ahead of the hint's audience, and when both
523
546
  * are sent `client_id` must be one of the hint's audiences — a mismatch is
524
547
  * a 400 `invalid_request` (OIDC RP-Initiated Logout 1.0 §2). `state` rides
525
- * along only to a URI that passed authorization.
548
+ * along only to a URI that passed authorization. A POST body over
549
+ * {@linkcode AuthorizationServerOptions.maxBodyBytes} is a 413
550
+ * `invalid_request`, refused before `endSession` runs.
526
551
  *
527
552
  * Wire the app's session teardown through the `endSession` **constructor
528
553
  * option**; this method answers 404 without it, and the discovery document