@softure-ai/auth 0.2.12 → 0.2.16

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 (49) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/README.md +6 -1
  3. package/dist/index.d.ts +12 -12
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +3 -2
  6. package/dist/index.js.map +1 -1
  7. package/dist/jobs/index.d.ts.map +1 -1
  8. package/dist/messages/en.d.ts.map +1 -1
  9. package/dist/next/actions.js.map +1 -1
  10. package/dist/next/context.js +1 -1
  11. package/dist/next/context.js.map +1 -1
  12. package/dist/next/current-user.js.map +1 -1
  13. package/dist/next/pages.d.ts.map +1 -1
  14. package/dist/next/pages.js +1 -1
  15. package/dist/next/pages.js.map +1 -1
  16. package/dist/options.d.ts +43 -29
  17. package/dist/options.d.ts.map +1 -1
  18. package/dist/options.js +25 -4
  19. package/dist/options.js.map +1 -1
  20. package/dist/schema.d.ts +6 -6
  21. package/dist/schema.d.ts.map +1 -1
  22. package/dist/scripts/create-account.js.map +1 -1
  23. package/dist/scripts/role-scripts.js.map +1 -1
  24. package/dist/scripts/temporary-password.js.map +1 -1
  25. package/dist/server/oidc/discovery.d.ts.map +1 -1
  26. package/dist/server/oidc/discovery.js.map +1 -1
  27. package/dist/server/oidc/id-token.d.ts.map +1 -1
  28. package/dist/server/options.d.ts +2 -1
  29. package/dist/server/options.d.ts.map +1 -1
  30. package/dist/server/options.js +17 -1
  31. package/dist/server/options.js.map +1 -1
  32. package/dist/server/rate-limits.d.ts.map +1 -1
  33. package/dist/server/sessions.js.map +1 -1
  34. package/dist/server/switches.js.map +1 -1
  35. package/dist/server/temporary-password.js.map +1 -1
  36. package/dist/testing/account.d.ts.map +1 -1
  37. package/dist/testing/account.js +2 -2
  38. package/dist/testing/account.js.map +1 -1
  39. package/dist/testing/github-provider.js.map +1 -1
  40. package/dist/testing/oidc-provider.js.map +1 -1
  41. package/dist/ui/auth-forms.js +1 -1
  42. package/module.json +2 -1
  43. package/package.json +13 -3
  44. package/src/index.ts +3 -1
  45. package/src/next/context.ts +1 -1
  46. package/src/options.ts +41 -7
  47. package/src/server/oidc/discovery.ts +2 -2
  48. package/src/server/options.ts +19 -3
  49. package/src/testing/account.ts +2 -2
package/src/options.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  // The options an app passes to `auth({ ... })` in softure.config.ts, parsed at startup.
2
- import type { FetchFunction, ModuleContext, SoftureConfig } from "@softure-ai/core";
2
+ import type { FetchFunction, ModuleContext, SoftureConfig, SoftureRuntime } from "@softure-ai/core";
3
3
  import type { Queryable } from "@softure-ai/db";
4
4
  import { z } from "zod";
5
5
  import type { RegisteredEvent } from "./contract.js";
@@ -13,6 +13,22 @@ import { ROLE_NAME_PATTERN } from "./roles.js";
13
13
  */
14
14
  export const DEFAULT_SCRYPT_COST = 2 ** 17;
15
15
 
16
+ /** scrypt N, r and p: memory per hash is 128 * cost * blockSize bytes. */
17
+ export interface ScryptParams {
18
+ readonly cost: number;
19
+ readonly blockSize: number;
20
+ readonly parallelization: number;
21
+ }
22
+
23
+ /**
24
+ * The hash cost each runtime uses when the app sets none. Node: OWASP's first profile (128 MiB). Workers: OWASP's
25
+ * equivalent 16 MiB profile (N = 2^14, r = 8, p = 5), because a Workers isolate has 128 MB in all.
26
+ */
27
+ export const SCRYPT_DEFAULTS: Readonly<Record<SoftureRuntime, ScryptParams>> = Object.freeze({
28
+ node: Object.freeze({ cost: DEFAULT_SCRYPT_COST, blockSize: 8, parallelization: 1 }),
29
+ workers: Object.freeze({ cost: 2 ** 14, blockSize: 8, parallelization: 5 }),
30
+ });
31
+
16
32
  const MAX_EMAIL_LENGTH = 254;
17
33
  const COOKIE_NAME = /^[A-Za-z0-9_-]{1,64}$/;
18
34
  /** A legacy cookie may carry its own prefix (`__Host-session`) or a dot. */
@@ -48,6 +64,7 @@ function isPowerOfTwo(value: number): boolean {
48
64
  return Number.isInteger(Math.log2(value));
49
65
  }
50
66
 
67
+ /** Each field left out takes the runtime's default (`SCRYPT_DEFAULTS`), resolved by `getAuthOptions`. */
51
68
  const scryptSchema = z.strictObject({
52
69
  /** N: CPU and memory cost, a power of two. */
53
70
  cost: z
@@ -56,11 +73,11 @@ const scryptSchema = z.strictObject({
56
73
  .min(2 ** 10)
57
74
  .max(2 ** 20)
58
75
  .refine(isPowerOfTwo, "must be a power of two")
59
- .default(DEFAULT_SCRYPT_COST),
76
+ .optional(),
60
77
  /** r: block size. */
61
- blockSize: z.number().int().min(1).max(32).default(8),
78
+ blockSize: z.number().int().min(1).max(32).optional(),
62
79
  /** p: parallelization. */
63
- parallelization: z.number().int().min(1).max(16).default(1),
80
+ parallelization: z.number().int().min(1).max(16).optional(),
64
81
  });
65
82
 
66
83
  const adminEmailSchema = z.string().trim().toLowerCase().max(MAX_EMAIL_LENGTH).pipe(z.email("must be an email address"));
@@ -199,7 +216,10 @@ export const authOptionsSchema = z.strictObject({
199
216
  enabled: z.boolean().default(true),
200
217
  /** Shortest password a user may set (characters). */
201
218
  minLength: z.number().int().min(8).max(128).default(10),
202
- /** Hash cost; memory per hash is 128 * cost * blockSize bytes. Raising it rehashes each password at its owner's next login. */
219
+ /**
220
+ * Hash cost; memory per hash is 128 * cost * blockSize bytes. A field left out takes the runtime's default
221
+ * (`SCRYPT_DEFAULTS`). Changing it rehashes each password at its owner's next login.
222
+ */
203
223
  scrypt: scryptSchema.prefault({}),
204
224
  })
205
225
  .prefault({}),
@@ -312,5 +332,19 @@ export const authOptionsSchema = z.strictObject({
312
332
  });
313
333
 
314
334
  export type AuthOptionsInput = z.input<typeof authOptionsSchema>;
315
- export type AuthOptions = z.output<typeof authOptionsSchema>;
316
- export type ScryptParams = z.output<typeof scryptSchema>;
335
+ /** The options as `auth({ ... })` parsed them, before the runtime is known. */
336
+ export type ParsedAuthOptions = z.output<typeof authOptionsSchema>;
337
+ /** The options of the running app (`getAuthOptions`): the hash cost resolved for its runtime. */
338
+ export type AuthOptions = Omit<ParsedAuthOptions, "password"> & {
339
+ readonly password: Omit<ParsedAuthOptions["password"], "scrypt"> & { readonly scrypt: ScryptParams };
340
+ };
341
+
342
+ /** `scrypt` with each unset field taken from the runtime's default. */
343
+ export function resolveScryptParams(scrypt: ParsedAuthOptions["password"]["scrypt"], runtime: SoftureRuntime): ScryptParams {
344
+ const defaults = SCRYPT_DEFAULTS[runtime];
345
+ return {
346
+ cost: scrypt.cost ?? defaults.cost,
347
+ blockSize: scrypt.blockSize ?? defaults.blockSize,
348
+ parallelization: scrypt.parallelization ?? defaults.parallelization,
349
+ };
350
+ }
@@ -1,7 +1,7 @@
1
1
  // A provider's discovery document (OpenID Connect Discovery 1.0) and its signing keys (JWKS), fetched through
2
2
  // core's `requestJson` (timeout, no redirect followed) and cached per app configuration. The document must name the
3
3
  // configured issuer (§4.3) and https endpoints, so a document cannot send the client secret to another scheme.
4
- import { createPublicKey, type JsonWebKey, type KeyObject } from "node:crypto";
4
+ import { createPublicKey, type KeyObject } from "node:crypto";
5
5
  import { requestJson, type SoftureConfig } from "@softure-ai/core";
6
6
  import { z } from "zod";
7
7
  import { isSecureEndpoint, type OpenIdConnectProvider } from "../../options.js";
@@ -118,7 +118,7 @@ export async function findSigningKey(
118
118
  function toSigningKey(jwk: z.output<typeof jwkSchema>): SigningKey[] {
119
119
  if (jwk.use !== undefined && jwk.use !== "sig") return [];
120
120
  try {
121
- return [{ kid: jwk.kid, key: createPublicKey({ key: jwk as JsonWebKey, format: "jwk" }) }];
121
+ return [{ kid: jwk.kid, key: createPublicKey({ key: jwk, format: "jwk" }) }];
122
122
  } catch {
123
123
  // A key node cannot read (another curve, a broken entry) cannot have signed a token we accept.
124
124
  return [];
@@ -1,6 +1,6 @@
1
1
  // The auth options of the running app, read from the configuration in the module context.
2
- import { getModule, type SoftureConfig } from "@softure-ai/core";
3
- import type { AuthOptions } from "../options.js";
2
+ import { getModule, type SoftureConfig, type SoftureRuntime } from "@softure-ai/core";
3
+ import { resolveScryptParams, type AuthOptions, type ParsedAuthOptions } from "../options.js";
4
4
 
5
5
  const MODULE_ID = "auth";
6
6
 
@@ -13,9 +13,25 @@ export function getAuthModule(config: SoftureConfig) {
13
13
  return module;
14
14
  }
15
15
 
16
+ /** Per parsed options object and runtime, so `getAuthOptions` returns the same object on every call. */
17
+ const resolvedOptions = new WeakMap<object, Map<SoftureRuntime, AuthOptions>>();
18
+
19
+ /** The auth options of the running app, with the hash cost resolved for its runtime (`config.platform`). */
16
20
  export function getAuthOptions(config: SoftureConfig): AuthOptions {
17
21
  // The module factory parsed these options with authOptionsSchema.
18
- return getAuthModule(config).options as AuthOptions;
22
+ const parsed = getAuthModule(config).options as ParsedAuthOptions;
23
+ const runtime = config.platform.runtime;
24
+ let byRuntime = resolvedOptions.get(parsed);
25
+ if (byRuntime === undefined) {
26
+ byRuntime = new Map();
27
+ resolvedOptions.set(parsed, byRuntime);
28
+ }
29
+ let resolved = byRuntime.get(runtime);
30
+ if (resolved === undefined) {
31
+ resolved = { ...parsed, password: { ...parsed.password, scrypt: resolveScryptParams(parsed.password.scrypt, runtime) } };
32
+ byRuntime.set(runtime, resolved);
33
+ }
34
+ return resolved;
19
35
  }
20
36
 
21
37
  const anchoredPatterns = new WeakMap<RegExp, RegExp>();
@@ -3,7 +3,7 @@
3
3
  // request (`runHooks`), so fixture rows the app's hooks write (a profile, an entitlement) exist too.
4
4
  import type { Queryable } from "@softure-ai/db";
5
5
  import type { AuthUser } from "../contract.js";
6
- import { DEFAULT_SCRYPT_COST, type ScryptParams } from "../options.js";
6
+ import { SCRYPT_DEFAULTS, type ScryptParams } from "../options.js";
7
7
  import { userRoles, users } from "../schema.js";
8
8
  import { getAuthOptions } from "../server/options.js";
9
9
  import { hashPassword, isPasswordHash } from "../server/password.js";
@@ -11,7 +11,7 @@ import type { AuthContext } from "../server/sessions.js";
11
11
  import { parseEmail } from "../server/validation.js";
12
12
 
13
13
  /** Auth's own hash parameters when the app sets none (`password.scrypt` in `auth({ ... })`). */
14
- const DEFAULT_SCRYPT: ScryptParams = { cost: DEFAULT_SCRYPT_COST, blockSize: 8, parallelization: 1 };
14
+ const DEFAULT_SCRYPT: ScryptParams = SCRYPT_DEFAULTS.node;
15
15
 
16
16
  /** One hash per password and parameters for the whole test run: scrypt is the slow part of a big suite's setup. */
17
17
  const hashCache = new Map<string, Promise<string>>();