|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#25224 cut 11] The WebContainer password hasher of `AuthManager` — the |
| 5 | + * pure-JS scrypt hasher handed to better-auth when the runtime is StackBlitz's |
| 6 | + * WebContainer, whose `node:crypto.scrypt` polyfill fails sign-up — moved out |
| 7 | + * of `auth-manager.ts` whole. |
| 8 | + * |
| 9 | + * A MOVE, not a redesign. The function below is the body of the |
| 10 | + * `AuthManager` method of the same name as it stood, re-indented one level |
| 11 | + * and otherwise unedited: the same runtime test, the same scrypt parameters |
| 12 | + * and `{saltHex}:{keyHex}` storage format, the same lazy load of |
| 13 | + * `@noble/hashes` (only inside WebContainer), and the same warning and |
| 14 | + * fallback when that load fails. |
| 15 | + * |
| 16 | + * ## How the moved body reaches the class |
| 17 | + * |
| 18 | + * The shape #23050 set for `rest-server.ts` (its `data-action-routes.ts` |
| 19 | + * header carries the full reasoning), which the ten `auth-manager-*.ts` |
| 20 | + * modules split out before this one follow. The class keeps the method — the |
| 21 | + * published `dist/*.d.ts` lists it — and its body is a plain call: it reads |
| 22 | + * no member of the class, so it is handed none, and there is no host type. |
| 23 | + * It is no longer `async`: it returns the moved function's own promise, so a |
| 24 | + * caller awaits the same promise the body settles, with no extra wrapper. |
| 25 | + * |
| 26 | + * `isWebContainerRuntime` stays: the request-state polyfill in |
| 27 | + * `auth-manager.ts`, which the constructor installs, reads it too. So that |
| 28 | + * file exports it and this module imports it by VALUE, for one read inside |
| 29 | + * `resolvePasswordHasher`, when the better-auth instance is built. |
| 30 | + * `index.ts` lists `auth-manager.ts`'s published names one by one, so that |
| 31 | + * export is not published. The two modules import each other, and that is |
| 32 | + * safe because nothing reads the binding while they are first evaluated: this |
| 33 | + * module's top level is one import and one function declaration. |
| 34 | + * |
| 35 | + * Nothing re-exports this module, so the published surface does not move. |
| 36 | + */ |
| 37 | + |
| 38 | +import { isWebContainerRuntime } from './auth-manager.js'; |
| 39 | + |
| 40 | +/** The body of `AuthManager#resolvePasswordHasher`; the method's docblock there is its contract. */ |
| 41 | +export async function resolvePasswordHasher(): Promise< |
| 42 | + { hash: (password: string) => Promise<string>; verify: (args: { hash: string; password: string }) => Promise<boolean> } | undefined |
| 43 | +> { |
| 44 | + if (!isWebContainerRuntime()) return undefined; |
| 45 | + try { |
| 46 | + const { scryptAsync } = await import('@noble/hashes/scrypt.js'); |
| 47 | + const PARAMS = { N: 16384, r: 16, p: 1, dkLen: 64, maxmem: 128 * 16384 * 16 * 2 } as const; |
| 48 | + const toHex = (b: Uint8Array): string => { |
| 49 | + let s = ''; |
| 50 | + for (let i = 0; i < b.length; i++) s += b[i]!.toString(16).padStart(2, '0'); |
| 51 | + return s; |
| 52 | + }; |
| 53 | + const generateKey = (password: string, saltHex: string): Promise<Uint8Array> => |
| 54 | + scryptAsync(password.normalize('NFKC'), saltHex, PARAMS); |
| 55 | + return { |
| 56 | + hash: async (password: string) => { |
| 57 | + const saltBytes = (globalThis as any).crypto.getRandomValues(new Uint8Array(16)); |
| 58 | + const saltHex = toHex(saltBytes); |
| 59 | + const key = await generateKey(password, saltHex); |
| 60 | + return `${saltHex}:${toHex(key)}`; |
| 61 | + }, |
| 62 | + verify: async ({ hash, password }) => { |
| 63 | + const [saltHex, keyHex] = hash.split(':'); |
| 64 | + if (!saltHex || !keyHex) throw new Error('Invalid password hash'); |
| 65 | + const target = await generateKey(password, saltHex); |
| 66 | + return toHex(target) === keyHex; |
| 67 | + }, |
| 68 | + }; |
| 69 | + } catch (err: any) { |
| 70 | + console.warn( |
| 71 | + `[AuthManager] WebContainer detected but pure-JS scrypt unavailable: ${err?.message ?? err}. Falling back to default.`, |
| 72 | + ); |
| 73 | + return undefined; |
| 74 | + } |
| 75 | +} |
0 commit comments