micro509 0.16.0 → 0.17.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.
- package/CHANGELOG.md +120 -1
- package/README.md +3 -0
- package/dist/internal/jose/base64url.js +40 -0
- package/dist/internal/jose/base64url.js.map +1 -0
- package/dist/internal/jose/strict-json.d.ts +9 -0
- package/dist/internal/jose/strict-json.js +155 -0
- package/dist/internal/jose/strict-json.js.map +1 -0
- package/dist/jose/errors.d.ts +44 -0
- package/dist/jose/errors.js +11 -0
- package/dist/jose/errors.js.map +1 -0
- package/dist/jose/jwks.d.ts +93 -0
- package/dist/jose/jwks.js +193 -0
- package/dist/jose/jwks.js.map +1 -0
- package/dist/jose/jws.d.ts +158 -0
- package/dist/jose/jws.js +362 -0
- package/dist/jose/jws.js.map +1 -0
- package/dist/jose/jwt.d.ts +91 -0
- package/dist/jose/jwt.js +187 -0
- package/dist/jose/jwt.js.map +1 -0
- package/dist/jose/remote-jwks.d.ts +95 -0
- package/dist/jose/remote-jwks.js +298 -0
- package/dist/jose/remote-jwks.js.map +1 -0
- package/dist/jose.d.ts +7 -0
- package/dist/jose.js +5 -0
- package/dist/kdf/argon2-arithmetic.js +15 -0
- package/dist/kdf/argon2-arithmetic.js.map +1 -0
- package/dist/kdf/argon2-core.js +486 -0
- package/dist/kdf/argon2-core.js.map +1 -0
- package/dist/kdf/argon2-memory.js +20 -0
- package/dist/kdf/argon2-memory.js.map +1 -0
- package/dist/kdf/argon2-portable.js +18 -0
- package/dist/kdf/argon2-portable.js.map +1 -0
- package/dist/kdf/argon2.d.ts +84 -0
- package/dist/kdf/argon2.js +186 -0
- package/dist/kdf/argon2.js.map +1 -0
- package/dist/kdf.d.ts +2 -0
- package/dist/kdf.js +1 -0
- package/dist/ssh/ssh.d.ts +200 -0
- package/dist/ssh/ssh.js +648 -0
- package/dist/ssh/ssh.js.map +1 -0
- package/dist/ssh/wire.d.ts +3 -0
- package/dist/ssh/wire.js +105 -0
- package/dist/ssh/wire.js.map +1 -0
- package/dist/ssh.d.ts +3 -0
- package/dist/ssh.js +1 -0
- package/package.json +24 -16
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"remote-jwks.js","names":["ABORTED","DEFAULTS","REDIRECT_STATUSES","LOOPBACK_HOSTS","url","resolved","value","fallback","throwMicro509Error","name","config","maxRedirects","maxTtlMs","cooldownMs","target","init","destination","from","joseFailure","message","status","headers","directives","directive","date","apparentAge","receivedAt","reportedAge","transit","requestedAt","age","maxAge","lifetime","match","expires","tooLarge","maxBytes","declared","response","successResult","reader","chunks","total","signal","done","body","offset","chunk","location","redirects","jwks","parseJwks","hop","entry","imported","fetched","carried","previous","TIMED_OUT","settings","snapshot","refresh","lastAttemptAt","controller","previousAttemptAt","timer","deadline","resolve","work","tracked","outcome","joined","settled","key","cached","current","pending","importJwkEntry","loaded","source","selected","selectJwk","selector","reloaded","request"],"sources":["../../src/jose/remote-jwks.ts"],"sourcesContent":["/**\n * Opt-in remote JWK Set resolution. A resolver is bound to one trusted JWKS URL from caller\n * configuration and keeps its own cache, so two issuers never share keys. Importing this module\n * performs no request; only a verification that reaches the resolver can.\n *\n * @module\n */\n\nimport type { JoseErrorResult, JoseResult, KeyResolutionErrorCode } from '#micro509/jose/errors';\nimport { joseFailure } from '#micro509/jose/errors';\nimport type { JwkSelector, Jwks, RsaJwkEntry } from '#micro509/jose/jwks';\nimport { importJwkEntry, parseJwks, selectJwk } from '#micro509/jose/jwks';\nimport type { JwsKeyRequest, JwsKeyResolver } from '#micro509/jose/jws';\nimport { successResult, throwMicro509Error } from '#micro509/result/result';\n\n/** The request {@linkcode JwksTransport} must issue. */\nexport interface JwksRequestInit {\n\t/** Always `GET`. */\n\treadonly method: 'GET';\n\t/** Always `manual`: the resolver validates every redirect destination itself. */\n\treadonly redirect: 'manual';\n\t/** Always `omit`. */\n\treadonly credentials: 'omit';\n\t/** Always `no-store`: each resolver refresh must reach the issuer, bypassing the HTTP cache. */\n\treadonly cache: 'no-store';\n\t/** Configured request headers. Withheld from any origin other than the configured URL's. */\n\treadonly headers: Readonly<Record<string, string>>;\n\t/** Aborts on timeout, cancellation or when no verification still waits for the response. */\n\treadonly signal: AbortSignal;\n}\n\n/**\n * Performs one HTTP request. `fetch` satisfies this type. Supply another to route through a proxy,\n * apply egress or DNS controls, or serve fixtures; the transport sees only URLs the resolver\n * already validated.\n */\nexport type JwksTransport = (url: string, init: JwksRequestInit) => Promise<Response>;\n\n/** Redirect handling for a remote JWK Set. By default redirects are refused. */\nexport interface RemoteJwksRedirectPolicy {\n\t/** Maximum redirects followed per fetch. */\n\treadonly maxRedirects: number;\n\t/**\n\t * Approves a redirect destination. Default: same origin as the hop it came from. The scheme\n\t * rule applies first: `https`, or `http` for a loopback host.\n\t */\n\treadonly allow?: (destination: URL, from: URL) => boolean;\n}\n\n/** Configuration of {@linkcode createRemoteJwksKeyResolver}. */\nexport interface RemoteJwksConfig {\n\t/** The trusted JWKS URL. `https`, or `http` for a loopback host. Never taken from a token. */\n\treadonly url: string | URL;\n\t/** Request transport. Default: `globalThis.fetch`. */\n\treadonly transport?: JwksTransport;\n\t/** Request headers, sent only to the configured URL's origin. */\n\treadonly headers?: Readonly<Record<string, string>>;\n\t/** Whole-request deadline in milliseconds, response body included. Default `5000`. */\n\treadonly timeoutMs?: number;\n\t/** Maximum response body in bytes, enforced while streaming. Default `65536`. */\n\treadonly maxResponseBytes?: number;\n\t/** Maximum keys in the set. Default `32`. */\n\treadonly maxKeys?: number;\n\t/** Redirect handling. Default: refuse every redirect. */\n\treadonly redirects?: RemoteJwksRedirectPolicy;\n\t/** Freshness applied when the response states none, in milliseconds. Default `600000`. */\n\treadonly defaultTtlMs?: number;\n\t/** Longest freshness the resolver will keep a response for, in milliseconds. Default `86400000`. */\n\treadonly maxTtlMs?: number;\n\t/**\n\t * Minimum interval between the starts of two fetches, in milliseconds. It bounds how often\n\t * unknown `kid` values, expired responses and failures can cause a request, and does not\n\t * lengthen how long a response stays fresh. Default `30000`.\n\t */\n\treadonly cooldownMs?: number;\n\t/** The clock in milliseconds since the epoch. Default `Date.now`. */\n\treadonly now?: () => number;\n}\n\n/** Codes {@linkcode createRemoteJwksKeyResolver} throws for unusable configuration. */\nexport type RemoteJwksConfigErrorCode = 'invalid_remote_jwks_config';\n\ntype FetchFailureCode = 'jwks_unavailable' | 'jwks_malformed';\n\ninterface Snapshot {\n\treadonly jwks: Jwks;\n\treadonly expiresAt: number;\n\treadonly imported: Map<string, Promise<JoseResult<CryptoKey, 'key_unsuitable'>>>;\n}\n\ntype RefreshOutcome =\n\t| { readonly kind: 'fresh'; readonly snapshot: Snapshot }\n\t| { readonly kind: 'failed'; readonly failure: JoseErrorResult<FetchFailureCode> }\n\t| { readonly kind: 'abandoned' };\n\ninterface Refresh {\n\treadonly promise: Promise<RefreshOutcome>;\n\treadonly controller: AbortController;\n\treadonly startedAt: number;\n\treadonly previousAttemptAt: number | undefined;\n\twaiters: number;\n}\n\nconst ABORTED: unique symbol = Symbol('micro509.jose.aborted');\n\nconst DEFAULTS = {\n\ttimeoutMs: 5000,\n\tmaxResponseBytes: 65_536,\n\tmaxKeys: 32,\n\tdefaultTtlMs: 600_000,\n\tmaxTtlMs: 86_400_000,\n\tcooldownMs: 30_000,\n} as const;\n\nconst REDIRECT_STATUSES: ReadonlySet<number> = new Set([301, 302, 303, 307, 308]);\n\nconst LOOPBACK_HOSTS: ReadonlySet<string> = new Set(['localhost', '127.0.0.1', '[::1]']);\n\nfunction isPermittedUrl(url: URL): boolean {\n\treturn (\n\t\turl.protocol === 'https:' || (url.protocol === 'http:' && LOOPBACK_HOSTS.has(url.hostname))\n\t);\n}\n\nfunction positiveInteger(value: number | undefined, fallback: number, name: string): number {\n\tconst resolved = value ?? fallback;\n\tif (!Number.isSafeInteger(resolved) || resolved <= 0) {\n\t\treturn throwMicro509Error('invalid_remote_jwks_config', `${name} must be a positive integer`);\n\t}\n\treturn resolved;\n}\n\ninterface ResolvedConfig {\n\treadonly url: URL;\n\treadonly transport: JwksTransport;\n\treadonly headers: Readonly<Record<string, string>>;\n\treadonly timeoutMs: number;\n\treadonly maxResponseBytes: number;\n\treadonly maxKeys: number;\n\treadonly maxRedirects: number;\n\treadonly allowRedirect: (destination: URL, from: URL) => boolean;\n\treadonly defaultTtlMs: number;\n\treadonly maxTtlMs: number;\n\treadonly cooldownMs: number;\n\treadonly now: () => number;\n}\n\nfunction resolveConfig(config: RemoteJwksConfig): ResolvedConfig {\n\tlet url: URL;\n\ttry {\n\t\turl = new URL(config.url);\n\t} catch {\n\t\treturn throwMicro509Error('invalid_remote_jwks_config', 'url is not an absolute URL');\n\t}\n\tif (!isPermittedUrl(url)) {\n\t\treturn throwMicro509Error(\n\t\t\t'invalid_remote_jwks_config',\n\t\t\t'url must use https, or http on a loopback host',\n\t\t);\n\t}\n\tif (url.username !== '' || url.password !== '') {\n\t\treturn throwMicro509Error('invalid_remote_jwks_config', 'url must not embed credentials');\n\t}\n\tconst maxRedirects = config.redirects?.maxRedirects ?? 0;\n\tif (!Number.isSafeInteger(maxRedirects) || maxRedirects < 0 || maxRedirects > 10) {\n\t\treturn throwMicro509Error(\n\t\t\t'invalid_remote_jwks_config',\n\t\t\t'maxRedirects must be an integer from 0 to 10',\n\t\t);\n\t}\n\tconst maxTtlMs = positiveInteger(config.maxTtlMs, DEFAULTS.maxTtlMs, 'maxTtlMs');\n\tconst cooldownMs = positiveInteger(config.cooldownMs, DEFAULTS.cooldownMs, 'cooldownMs');\n\treturn {\n\t\turl,\n\t\ttransport: config.transport ?? ((target, init) => globalThis.fetch(target, init)),\n\t\theaders: { ...config.headers },\n\t\ttimeoutMs: positiveInteger(config.timeoutMs, DEFAULTS.timeoutMs, 'timeoutMs'),\n\t\tmaxResponseBytes: positiveInteger(\n\t\t\tconfig.maxResponseBytes,\n\t\t\tDEFAULTS.maxResponseBytes,\n\t\t\t'maxResponseBytes',\n\t\t),\n\t\tmaxKeys: positiveInteger(config.maxKeys, DEFAULTS.maxKeys, 'maxKeys'),\n\t\tmaxRedirects,\n\t\tallowRedirect:\n\t\t\tconfig.redirects?.allow ?? ((destination, from) => destination.origin === from.origin),\n\t\tdefaultTtlMs: positiveInteger(config.defaultTtlMs, DEFAULTS.defaultTtlMs, 'defaultTtlMs'),\n\t\tmaxTtlMs,\n\t\tcooldownMs,\n\t\tnow: config.now ?? Date.now,\n\t};\n}\n\nfunction unavailable(message: string, status?: number): JoseErrorResult<'jwks_unavailable'> {\n\treturn joseFailure('jwks_unavailable', message, status === undefined ? undefined : { status });\n}\n\nconst NOT_A_NUMBER = Number.NaN;\n\nfunction secondsHeader(headers: Headers, name: string): number {\n\tconst value = headers.get(name);\n\treturn value !== null && /^\\d{1,10}$/.test(value) ? Number(value) * 1000 : NOT_A_NUMBER;\n}\n\n/**\n * Remaining freshness in milliseconds, never below zero and never above `maxTtlMs`.\n *\n * The lifetime is `Cache-Control: max-age`, else `Expires` less `Date`, else `defaultTtlMs`\n * (RFC 9111 §4.2.1); `no-store`, `no-cache` and unusable values give none. The response age is the\n * larger of the apparent age from `Date` and the `Age` header plus the transit delay\n * (RFC 9111 §4.2.3).\n */\nfunction freshnessMs(\n\theaders: Headers,\n\trequestedAt: number,\n\treceivedAt: number,\n\tconfig: ResolvedConfig,\n): number {\n\tconst directives = (headers.get('cache-control') ?? '')\n\t\t.split(',')\n\t\t.map((directive) => directive.trim().toLowerCase())\n\t\t.filter((directive) => directive.length > 0);\n\tif (directives.includes('no-store') || directives.includes('no-cache')) return 0;\n\tconst date = Date.parse(headers.get('date') ?? '');\n\tconst apparentAge = Number.isNaN(date) ? 0 : Math.max(0, receivedAt - date);\n\tconst reportedAge = secondsHeader(headers, 'age');\n\tconst transit = Math.max(0, receivedAt - requestedAt);\n\tconst age = Math.max(apparentAge, (Number.isNaN(reportedAge) ? 0 : reportedAge) + transit);\n\tconst maxAge = directives.find((directive) => directive.startsWith('max-age'));\n\tlet lifetime = config.defaultTtlMs;\n\tif (maxAge !== undefined) {\n\t\tconst match = /^max-age=(\"?)(\\d{1,10})\\1$/.exec(maxAge);\n\t\tlifetime = match?.[2] === undefined ? 0 : Number(match[2]) * 1000;\n\t} else if (headers.has('expires')) {\n\t\tconst expires = Date.parse(headers.get('expires') ?? '');\n\t\tlifetime = Number.isNaN(expires) ? 0 : expires - (Number.isNaN(date) ? receivedAt : date);\n\t}\n\treturn Math.min(config.maxTtlMs, Math.max(0, lifetime - age));\n}\n\ntype BodyResult = JoseResult<Uint8Array, 'jwks_unavailable' | 'jwks_malformed'>;\n\nasync function readBounded(\n\tresponse: Response,\n\tmaxBytes: number,\n\tsignal: AbortSignal,\n): Promise<BodyResult> {\n\tconst tooLarge = joseFailure('jwks_malformed', `The JWKS response exceeds ${maxBytes} bytes`);\n\tconst declared = response.headers.get('content-length');\n\tif (declared !== null && /^\\d+$/.test(declared) && Number(declared) > maxBytes) {\n\t\tawait response.body?.cancel();\n\t\treturn tooLarge;\n\t}\n\tif (response.body === null) return successResult(new Uint8Array(0));\n\tconst reader = response.body.getReader();\n\tconst release = (): void => {\n\t\treader.cancel().catch(() => undefined);\n\t};\n\tsignal.addEventListener('abort', release, { once: true });\n\ttry {\n\t\tconst chunks: Uint8Array[] = [];\n\t\tlet total = 0;\n\t\tfor (;;) {\n\t\t\tconst { done, value } = await reader.read();\n\t\t\tif (signal.aborted) return unavailable('The JWKS request was cancelled');\n\t\t\tif (done) break;\n\t\t\ttotal += value.byteLength;\n\t\t\tif (total > maxBytes) {\n\t\t\t\trelease();\n\t\t\t\treturn tooLarge;\n\t\t\t}\n\t\t\tchunks.push(value);\n\t\t}\n\t\tconst body = new Uint8Array(total);\n\t\tlet offset = 0;\n\t\tfor (const chunk of chunks) {\n\t\t\tbody.set(chunk, offset);\n\t\t\toffset += chunk.byteLength;\n\t\t}\n\t\treturn successResult(body);\n\t} finally {\n\t\tsignal.removeEventListener('abort', release);\n\t}\n}\n\ninterface Fetched {\n\treadonly jwks: Jwks;\n\treadonly expiresAt: number;\n}\n\nfunction nextHop(\n\tconfig: ResolvedConfig,\n\tfrom: URL,\n\tresponse: Response,\n\tredirects: number,\n): JoseResult<URL, 'jwks_unavailable'> {\n\tconst location = response.headers.get('location');\n\tif (redirects >= config.maxRedirects || location === null) {\n\t\treturn unavailable('The JWKS endpoint redirected', response.status);\n\t}\n\tlet destination: URL;\n\ttry {\n\t\tdestination = new URL(location, from);\n\t} catch {\n\t\treturn unavailable('The JWKS redirect target is not a URL', response.status);\n\t}\n\tif (\n\t\t!isPermittedUrl(destination) ||\n\t\tdestination.username !== '' ||\n\t\tdestination.password !== '' ||\n\t\t!config.allowRedirect(destination, from)\n\t) {\n\t\treturn unavailable('The JWKS redirect target is not permitted', response.status);\n\t}\n\treturn successResult(destination);\n}\n\nasync function readJwks(\n\tconfig: ResolvedConfig,\n\tresponse: Response,\n\trequestedAt: number,\n\tsignal: AbortSignal,\n): Promise<JoseResult<Fetched, FetchFailureCode>> {\n\tif (response.status !== 200) {\n\t\tawait response.body?.cancel();\n\t\treturn unavailable('The JWKS endpoint did not answer 200', response.status);\n\t}\n\tconst receivedAt = config.now();\n\tconst body = await readBounded(response, config.maxResponseBytes, signal);\n\tif (!body.ok) return body;\n\tconst jwks = parseJwks(body.value, {\n\t\tmaxBytes: config.maxResponseBytes,\n\t\tmaxKeys: config.maxKeys,\n\t});\n\tif (!jwks.ok) return joseFailure('jwks_malformed', jwks.message);\n\treturn successResult({\n\t\tjwks: jwks.value,\n\t\texpiresAt: receivedAt + freshnessMs(response.headers, requestedAt, receivedAt, config),\n\t});\n}\n\nasync function fetchJwks(\n\tconfig: ResolvedConfig,\n\tsignal: AbortSignal,\n\trequestedAt: number,\n): Promise<JoseResult<Fetched, FetchFailureCode>> {\n\tlet url = config.url;\n\tfor (let redirects = 0; ; redirects++) {\n\t\tconst response = await config.transport(url.href, {\n\t\t\tmethod: 'GET',\n\t\t\tredirect: 'manual',\n\t\t\tcredentials: 'omit',\n\t\t\tcache: 'no-store',\n\t\t\theaders: url.origin === config.url.origin ? config.headers : {},\n\t\t\tsignal,\n\t\t});\n\t\tif (signal.aborted) {\n\t\t\tawait response.body?.cancel();\n\t\t\treturn unavailable('The JWKS request was cancelled');\n\t\t}\n\t\tif (response.type === 'opaqueredirect') {\n\t\t\treturn unavailable('The runtime hides redirects, so they cannot be validated');\n\t\t}\n\t\tif (!REDIRECT_STATUSES.has(response.status)) {\n\t\t\treturn await readJwks(config, response, requestedAt, signal);\n\t\t}\n\t\tawait response.body?.cancel();\n\t\tconst hop = nextHop(config, url, response, redirects);\n\t\tif (!hop.ok) return hop;\n\t\turl = hop.value;\n\t}\n}\n\nfunction entryKey(entry: RsaJwkEntry): string {\n\treturn `${entry.n}.${entry.e}`;\n}\n\nfunction carryOver(fetched: Fetched, previous: Snapshot | undefined): Snapshot {\n\tconst imported = new Map<string, Promise<JoseResult<CryptoKey, 'key_unsuitable'>>>();\n\tfor (const entry of fetched.jwks.keys) {\n\t\tconst carried = entry.kind === 'rsa' ? previous?.imported.get(entryKey(entry)) : undefined;\n\t\tif (entry.kind === 'rsa' && carried !== undefined) imported.set(entryKey(entry), carried);\n\t}\n\treturn { jwks: fetched.jwks, expiresAt: fetched.expiresAt, imported };\n}\n\nconst TIMED_OUT: unique symbol = Symbol('micro509.jose.timedOut');\n\n/**\n * Creates a key resolver that reads a JWK Set from one trusted URL.\n *\n * Behaviour, per resolver:\n *\n * - The first verification fetches the set. Later ones reuse it while fresh. Freshness is the\n * remaining lifetime from `Cache-Control: max-age`, else `Expires`, else `defaultTtlMs`, less the\n * response age (the larger of the apparent age from `Date` and `Age` plus the transit delay),\n * capped at `maxTtlMs`. `no-store`, `no-cache` and a lifetime already spent leave none: the set\n * serves the verification that fetched it and nothing after. No heuristic freshness is applied.\n * - A set that is no longer fresh is never used, whether or not a refresh succeeds. There is no\n * stale-if-error. A refresh that fails while the cached set is still fresh keeps that set until\n * it expires.\n * - Refreshes are separate from freshness: after any attempt the next one waits `cooldownMs`.\n * Within that interval a token whose `kid` is absent gets `key_not_found`, and a token that\n * needs a set that is no longer fresh gets `jwks_unavailable`. Concurrent verifications share\n * a single request, and unknown `kid` values are never cached.\n * - A refresh replaces the whole set, so a rotated-out key stops verifying as soon as the new\n * set is read. Rotation must therefore introduce a new `kid`.\n * - The whole request, response body included, is bounded by `timeoutMs` and enforced even when\n * the transport ignores the abort signal. Redirects, bytes and keys are bounded by `redirects`,\n * `maxResponseBytes` and `maxKeys`. No retry happens inside a fetch. Cancelling the last waiting\n * verification aborts the request, and a response that finishes afterwards is discarded.\n * - Request headers go only to the configured origin. `jku`, `x5u` and `jwk` headers are never\n * used.\n *\n * Throws a `ResultError` coded {@linkcode RemoteJwksConfigErrorCode} for unusable configuration.\n * Redirect destinations, and the addresses a hostname resolves to, are limited by `redirects` and\n * the transport; this module cannot see DNS answers, and browsers hide redirect targets.\n */\nexport function createRemoteJwksKeyResolver(config: RemoteJwksConfig): JwsKeyResolver {\n\tconst settings = resolveConfig(config);\n\tlet snapshot: Snapshot | undefined;\n\tlet refresh: Refresh | undefined;\n\tlet lastAttemptAt: number | undefined;\n\n\tfunction startRefresh(): Refresh {\n\t\tconst controller = new AbortController();\n\t\tconst requestedAt = settings.now();\n\t\tconst previousAttemptAt = lastAttemptAt;\n\t\tconst previous = snapshot;\n\t\tlastAttemptAt = requestedAt;\n\n\t\tasync function run(): Promise<RefreshOutcome> {\n\t\t\tlet timer: ReturnType<typeof setTimeout> | undefined;\n\t\t\tconst deadline = new Promise<typeof TIMED_OUT>((resolve) => {\n\t\t\t\ttimer = setTimeout(() => resolve(TIMED_OUT), settings.timeoutMs);\n\t\t\t});\n\t\t\tconst work = fetchJwks(settings, controller.signal, requestedAt);\n\t\t\twork.catch(() => undefined);\n\t\t\ttry {\n\t\t\t\tconst fetched = await Promise.race([work, deadline]);\n\t\t\t\tif (fetched === TIMED_OUT) {\n\t\t\t\t\tcontroller.abort();\n\t\t\t\t\treturn { kind: 'failed', failure: unavailable('The JWKS request timed out') };\n\t\t\t\t}\n\t\t\t\tif (!fetched.ok) return { kind: 'failed', failure: fetched };\n\t\t\t\treturn { kind: 'fresh', snapshot: carryOver(fetched.value, previous) };\n\t\t\t} catch {\n\t\t\t\treturn controller.signal.aborted\n\t\t\t\t\t? { kind: 'abandoned' }\n\t\t\t\t\t: { kind: 'failed', failure: unavailable('The JWKS request failed') };\n\t\t\t} finally {\n\t\t\t\tclearTimeout(timer);\n\t\t\t}\n\t\t}\n\n\t\tconst tracked: Promise<RefreshOutcome> = run()\n\t\t\t.then((outcome): RefreshOutcome => {\n\t\t\t\tif (refresh?.promise !== tracked) return { kind: 'abandoned' };\n\t\t\t\tif (outcome.kind === 'fresh') snapshot = outcome.snapshot;\n\t\t\t\treturn outcome;\n\t\t\t})\n\t\t\t.finally(() => {\n\t\t\t\tif (refresh?.promise === tracked) refresh = undefined;\n\t\t\t});\n\t\treturn {\n\t\t\tpromise: tracked,\n\t\t\tcontroller,\n\t\t\tstartedAt: requestedAt,\n\t\t\tpreviousAttemptAt,\n\t\t\twaiters: 0,\n\t\t};\n\t}\n\n\tasync function awaitRefresh(\n\t\tsignal: AbortSignal | undefined,\n\t): Promise<RefreshOutcome | typeof ABORTED> {\n\t\tif (signal?.aborted === true) return ABORTED;\n\t\trefresh ??= startRefresh();\n\t\tconst joined = refresh;\n\t\tjoined.waiters++;\n\t\tif (signal === undefined) return await joined.promise;\n\t\treturn await new Promise<RefreshOutcome | typeof ABORTED>((resolve) => {\n\t\t\tlet settled = false;\n\t\t\tconst finish = (): void => {\n\t\t\t\tsettled = true;\n\t\t\t\tsignal.removeEventListener('abort', onAbort);\n\t\t\t};\n\t\t\tconst onAbort = (): void => {\n\t\t\t\tif (settled) return;\n\t\t\t\tfinish();\n\t\t\t\tjoined.waiters--;\n\t\t\t\tif (joined.waiters === 0) {\n\t\t\t\t\tif (refresh === joined) refresh = undefined;\n\t\t\t\t\tif (lastAttemptAt === joined.startedAt) lastAttemptAt = joined.previousAttemptAt;\n\t\t\t\t\tjoined.controller.abort();\n\t\t\t\t}\n\t\t\t\tresolve(ABORTED);\n\t\t\t};\n\t\t\tsignal.addEventListener('abort', onAbort, { once: true });\n\t\t\tif (signal.aborted) onAbort();\n\t\t\tvoid joined.promise.then((outcome) => {\n\t\t\t\tif (settled) return;\n\t\t\t\tfinish();\n\t\t\t\tresolve(outcome);\n\t\t\t});\n\t\t});\n\t}\n\n\tfunction refreshAllowed(): boolean {\n\t\tif (refresh !== undefined || lastAttemptAt === undefined) return true;\n\t\treturn settings.now() - lastAttemptAt >= settings.cooldownMs;\n\t}\n\n\tasync function obtain(\n\t\tsignal: AbortSignal | undefined,\n\t): Promise<JoseResult<Snapshot | undefined, FetchFailureCode | 'aborted'>> {\n\t\tif (!refreshAllowed()) return successResult(undefined);\n\t\tconst outcome = await awaitRefresh(signal);\n\t\tif (outcome === ABORTED || outcome.kind === 'abandoned') {\n\t\t\treturn joseFailure('aborted', 'Key resolution was cancelled');\n\t\t}\n\t\tif (outcome.kind === 'failed') return outcome.failure;\n\t\treturn successResult(outcome.snapshot);\n\t}\n\n\tasync function importFrom(\n\t\tcurrent: Snapshot,\n\t\tentry: RsaJwkEntry,\n\t): Promise<JoseResult<CryptoKey, 'key_unsuitable'>> {\n\t\tconst key = entryKey(entry);\n\t\tconst cached = current.imported.get(key);\n\t\tif (cached !== undefined) return await cached;\n\t\tconst pending = importJwkEntry(entry);\n\t\tcurrent.imported.set(key, pending);\n\t\treturn await pending;\n\t}\n\n\tasync function currentSnapshot(\n\t\tsignal: AbortSignal | undefined,\n\t): Promise<JoseResult<Snapshot, FetchFailureCode | 'aborted'>> {\n\t\tif (snapshot !== undefined && settings.now() < snapshot.expiresAt) {\n\t\t\treturn successResult(snapshot);\n\t\t}\n\t\tsnapshot = undefined;\n\t\tconst loaded = await obtain(signal);\n\t\tif (!loaded.ok) return loaded;\n\t\treturn loaded.value === undefined\n\t\t\t? unavailable('The JWKS endpoint is in cooldown')\n\t\t\t: successResult(loaded.value);\n\t}\n\n\tasync function select(\n\t\tcurrent: Snapshot,\n\t\tselector: JwkSelector,\n\t\tsignal: AbortSignal | undefined,\n\t): Promise<JoseResult<CryptoKey, KeyResolutionErrorCode>> {\n\t\tlet source = current;\n\t\tlet selected = selectJwk(source.jwks, selector);\n\t\tif (!selected.ok && selected.code === 'key_not_found') {\n\t\t\tconst reloaded = await obtain(signal);\n\t\t\tif (!reloaded.ok) return reloaded;\n\t\t\tif (reloaded.value !== undefined) {\n\t\t\t\tsource = reloaded.value;\n\t\t\t\tselected = selectJwk(source.jwks, selector);\n\t\t\t}\n\t\t}\n\t\tif (!selected.ok) return selected;\n\t\tconst key = await importFrom(source, selected.value);\n\t\treturn signal?.aborted === true ? joseFailure('aborted', 'Key resolution was cancelled') : key;\n\t}\n\n\treturn async (request: JwsKeyRequest) => {\n\t\tif (request.signal?.aborted === true) {\n\t\t\treturn joseFailure('aborted', 'Key resolution was cancelled');\n\t\t}\n\t\tconst current = await currentSnapshot(request.signal);\n\t\tif (!current.ok) return current;\n\t\treturn await select(\n\t\t\tcurrent.value,\n\t\t\t{ algorithm: request.algorithm, ...(request.kid === undefined ? {} : { kid: request.kid }) },\n\t\t\trequest.signal,\n\t\t);\n\t};\n}\n"],"mappings":";;;;AAuGA,MAAMA,IAAyB,OAAO,uBAAuB,GAEvDC,IAAW;CAChB,WAAW;CACX,kBAAkB;CAClB,SAAS;CACT,cAAc;CACd,UAAU;CACV,YAAY;AACb,GAEMC,oBAAyC,IAAI,IAAI;CAAC;CAAK;CAAK;CAAK;CAAK;AAAG,CAAC,GAE1EC,oBAAsC,IAAI,IAAI;CAAC;CAAa;CAAa;AAAO,CAAC;AAEvF,SAAS,eAAe,GAAmB;CAC1C,OACCC,EAAI,aAAa,YAAaA,EAAI,aAAa,WAAWD,EAAe,IAAIC,EAAI,QAAQ;AAE3F;AAEA,SAAS,gBAAgB,GAA2B,GAAkB,GAAsB;CAC3F,IAAMC,IAAWC,KAASC;CAI1B,OAHI,CAAC,OAAO,cAAcF,CAAQ,KAAKA,KAAY,IAC3CG,EA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EACA,IAAI,CAACF,EAAS,IAAI,OAAOA;EACzB,IAAMP,IAAM,MAAM,WAAWM,GAAQC,EAAS,KAAK;EACnD,OAAOrC,GAAQ,YAAY,KAAOzB,EAAY,WAAW,8BAA8B,IAAIuD;CAC5F;CAEA,OAAO,OAAO,MAA2B;EACxC,IAAIW,EAAQ,QAAQ,YAAY,IAC/B,OAAOlE,EAAY,WAAW,8BAA8B;EAE7D,IAAMyD,IAAU,MAAM,gBAAgBS,EAAQ,MAAM;EAEpD,OADKT,EAAQ,KACN,MAAM,OACZA,EAAQ,OACR;GAAE,WAAWS,EAAQ;GAAW,GAAIA,EAAQ,QAAQ,KAAA,IAAY,CAAC,IAAI,EAAE,KAAKA,EAAQ,IAAI;EAAG,GAC3FA,EAAQ,MACT,IALwBT;CAMzB;AACD"}
|
package/dist/jose.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { JsonObject, JsonValue } from "./internal/jose/strict-json.js";
|
|
2
|
+
import { JoseErrorCode, JoseErrorDetails, JoseErrorResult, JoseFailure, JoseResult, JwsParseErrorCode, JwsPolicyErrorCode, JwtClaimsErrorCode, KeyResolutionErrorCode, VerifyJwsErrorCode, VerifyJwtErrorCode } from "./jose/errors.js";
|
|
3
|
+
import { DEFAULT_JOSE_LIMITS, JWS_ALGORITHMS, JoseLimits, JwsAlgorithm, JwsHeader, JwsHeaderInput, JwsKeyRequest, JwsKeyResolver, MIN_RS256_MODULUS_BITS, SignJoseErrorCode, SignJwsInput, TokenTypePolicy, Unchecked, UnverifiedJws, VerifiedJws, VerifyJwsOptions, parseCompactJws, signCompactJws, verifyCompactJws } from "./jose/jws.js";
|
|
4
|
+
import { DEFAULT_JWKS_LIMITS, JwkEntry, JwkEntryBase, JwkMetadata, JwkSelector, Jwks, JwksLimits, ParseJwksErrorCode, RsaJwkEntry, UnsupportedJwkEntry, createJwksKeyResolver, importJwkEntry, parseJwks, selectJwk } from "./jose/jwks.js";
|
|
5
|
+
import { FutureIssuedAtPolicy, JwtClaimsPolicy, MAX_CLOCK_TOLERANCE_SECONDS, RegisteredClaims, SignJwtInput, UnverifiedJwt, VerifiedJwt, VerifyJwtOptions, decodeJwtUnverified, signJwt, verifyJwt } from "./jose/jwt.js";
|
|
6
|
+
import { JwksRequestInit, JwksTransport, RemoteJwksConfig, RemoteJwksConfigErrorCode, RemoteJwksRedirectPolicy, createRemoteJwksKeyResolver } from "./jose/remote-jwks.js";
|
|
7
|
+
export { DEFAULT_JOSE_LIMITS, DEFAULT_JWKS_LIMITS, type FutureIssuedAtPolicy, JWS_ALGORITHMS, type JoseErrorCode, type JoseErrorDetails, type JoseErrorResult, type JoseFailure, type JoseLimits, type JoseResult, type JsonObject, type JsonValue, type JwkEntry, type JwkEntryBase, type JwkMetadata, type JwkSelector, type Jwks, type JwksLimits, type JwksRequestInit, type JwksTransport, type JwsAlgorithm, type JwsHeader, type JwsHeaderInput, type JwsKeyRequest, type JwsKeyResolver, type JwsParseErrorCode, type JwsPolicyErrorCode, type JwtClaimsErrorCode, type JwtClaimsPolicy, type KeyResolutionErrorCode, MAX_CLOCK_TOLERANCE_SECONDS, MIN_RS256_MODULUS_BITS, type ParseJwksErrorCode, type RegisteredClaims, type RemoteJwksConfig, type RemoteJwksConfigErrorCode, type RemoteJwksRedirectPolicy, type RsaJwkEntry, type SignJoseErrorCode, type SignJwsInput, type SignJwtInput, type TokenTypePolicy, type Unchecked, type UnsupportedJwkEntry, type UnverifiedJws, type UnverifiedJwt, type VerifiedJws, type VerifiedJwt, type VerifyJwsErrorCode, type VerifyJwsOptions, type VerifyJwtErrorCode, type VerifyJwtOptions, createJwksKeyResolver, createRemoteJwksKeyResolver, decodeJwtUnverified, importJwkEntry, parseCompactJws, parseJwks, selectJwk, signCompactJws, signJwt, verifyCompactJws, verifyJwt };
|
package/dist/jose.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { DEFAULT_JOSE_LIMITS as e, JWS_ALGORITHMS as t, MIN_RS256_MODULUS_BITS as n, parseCompactJws as r, signCompactJws as i, verifyCompactJws as a } from "./jose/jws.js";
|
|
2
|
+
import { DEFAULT_JWKS_LIMITS as o, createJwksKeyResolver as s, importJwkEntry as c, parseJwks as l, selectJwk as u } from "./jose/jwks.js";
|
|
3
|
+
import { MAX_CLOCK_TOLERANCE_SECONDS as d, decodeJwtUnverified as f, signJwt as p, verifyJwt as m } from "./jose/jwt.js";
|
|
4
|
+
import { createRemoteJwksKeyResolver as h } from "./jose/remote-jwks.js";
|
|
5
|
+
export { e as DEFAULT_JOSE_LIMITS, o as DEFAULT_JWKS_LIMITS, t as JWS_ALGORITHMS, d as MAX_CLOCK_TOLERANCE_SECONDS, n as MIN_RS256_MODULUS_BITS, s as createJwksKeyResolver, h as createRemoteJwksKeyResolver, f as decodeJwtUnverified, c as importJwkEntry, r as parseCompactJws, l as parseJwks, u as selectJwk, i as signCompactJws, p as signJwt, a as verifyCompactJws, m as verifyJwt };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// src/kdf/argon2-arithmetic.ts
|
|
2
|
+
/**
|
|
3
|
+
* Exact high word of an unsigned 32×32 product; `low` is Math.imul(a, b).
|
|
4
|
+
*
|
|
5
|
+
* Subtracting the exact low word leaves a multiple of 2^32 with at most 2048
|
|
6
|
+
* units of floating-point error. Scaling by 2^32 reduces that error below
|
|
7
|
+
* 2^-20, so adding 0.5 and truncating recovers the exact integer high word.
|
|
8
|
+
* BlaMka already computes the low word and passes it here for reuse.
|
|
9
|
+
*/
|
|
10
|
+
function multiplyHigh(e, t, n) {
|
|
11
|
+
return (e * t - (n >>> 0)) / 4294967296 + .5 >>> 0;
|
|
12
|
+
}
|
|
13
|
+
export { multiplyHigh };
|
|
14
|
+
|
|
15
|
+
//# sourceMappingURL=argon2-arithmetic.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"argon2-arithmetic.js","names":["a","b","low"],"sources":["../../src/kdf/argon2-arithmetic.ts"],"sourcesContent":["/**\n * Exact high word of an unsigned 32×32 product; `low` is Math.imul(a, b).\n *\n * Subtracting the exact low word leaves a multiple of 2^32 with at most 2048\n * units of floating-point error. Scaling by 2^32 reduces that error below\n * 2^-20, so adding 0.5 and truncating recovers the exact integer high word.\n * BlaMka already computes the low word and passes it here for reuse.\n */\nexport function multiplyHigh(a: number, b: number, low: number): number {\n\treturn ((a * b - (low >>> 0)) / 0x1_0000_0000 + 0.5) >>> 0;\n}\n"],"mappings":";;;;;;;;;AAQA,SAAgB,aAAa,GAAW,GAAW,GAAqB;CACvE,QAASA,IAAIC,KAAKC,MAAQ,MAAM,aAAgB,OAAS;AAC1D"}
|
|
@@ -0,0 +1,486 @@
|
|
|
1
|
+
import { multiplyHigh as e } from "./argon2-arithmetic.js";
|
|
2
|
+
import { createMatrixPool as t } from "./argon2-memory.js";
|
|
3
|
+
// src/kdf/argon2-core.ts
|
|
4
|
+
const n = t(), r = 18446744073709551615n, i = [
|
|
5
|
+
7640891576956012808n,
|
|
6
|
+
13503953896175478587n,
|
|
7
|
+
4354685564936845355n,
|
|
8
|
+
11912009170470909681n,
|
|
9
|
+
5840696475078001361n,
|
|
10
|
+
11170449401992604703n,
|
|
11
|
+
2270897969802886507n,
|
|
12
|
+
6620516959819538809n
|
|
13
|
+
], a = [
|
|
14
|
+
[
|
|
15
|
+
0,
|
|
16
|
+
1,
|
|
17
|
+
2,
|
|
18
|
+
3,
|
|
19
|
+
4,
|
|
20
|
+
5,
|
|
21
|
+
6,
|
|
22
|
+
7,
|
|
23
|
+
8,
|
|
24
|
+
9,
|
|
25
|
+
10,
|
|
26
|
+
11,
|
|
27
|
+
12,
|
|
28
|
+
13,
|
|
29
|
+
14,
|
|
30
|
+
15
|
|
31
|
+
],
|
|
32
|
+
[
|
|
33
|
+
14,
|
|
34
|
+
10,
|
|
35
|
+
4,
|
|
36
|
+
8,
|
|
37
|
+
9,
|
|
38
|
+
15,
|
|
39
|
+
13,
|
|
40
|
+
6,
|
|
41
|
+
1,
|
|
42
|
+
12,
|
|
43
|
+
0,
|
|
44
|
+
2,
|
|
45
|
+
11,
|
|
46
|
+
7,
|
|
47
|
+
5,
|
|
48
|
+
3
|
|
49
|
+
],
|
|
50
|
+
[
|
|
51
|
+
11,
|
|
52
|
+
8,
|
|
53
|
+
12,
|
|
54
|
+
0,
|
|
55
|
+
5,
|
|
56
|
+
2,
|
|
57
|
+
15,
|
|
58
|
+
13,
|
|
59
|
+
10,
|
|
60
|
+
14,
|
|
61
|
+
3,
|
|
62
|
+
6,
|
|
63
|
+
7,
|
|
64
|
+
1,
|
|
65
|
+
9,
|
|
66
|
+
4
|
|
67
|
+
],
|
|
68
|
+
[
|
|
69
|
+
7,
|
|
70
|
+
9,
|
|
71
|
+
3,
|
|
72
|
+
1,
|
|
73
|
+
13,
|
|
74
|
+
12,
|
|
75
|
+
11,
|
|
76
|
+
14,
|
|
77
|
+
2,
|
|
78
|
+
6,
|
|
79
|
+
5,
|
|
80
|
+
10,
|
|
81
|
+
4,
|
|
82
|
+
0,
|
|
83
|
+
15,
|
|
84
|
+
8
|
|
85
|
+
],
|
|
86
|
+
[
|
|
87
|
+
9,
|
|
88
|
+
0,
|
|
89
|
+
5,
|
|
90
|
+
7,
|
|
91
|
+
2,
|
|
92
|
+
4,
|
|
93
|
+
10,
|
|
94
|
+
15,
|
|
95
|
+
14,
|
|
96
|
+
1,
|
|
97
|
+
11,
|
|
98
|
+
12,
|
|
99
|
+
6,
|
|
100
|
+
8,
|
|
101
|
+
3,
|
|
102
|
+
13
|
|
103
|
+
],
|
|
104
|
+
[
|
|
105
|
+
2,
|
|
106
|
+
12,
|
|
107
|
+
6,
|
|
108
|
+
10,
|
|
109
|
+
0,
|
|
110
|
+
11,
|
|
111
|
+
8,
|
|
112
|
+
3,
|
|
113
|
+
4,
|
|
114
|
+
13,
|
|
115
|
+
7,
|
|
116
|
+
5,
|
|
117
|
+
15,
|
|
118
|
+
14,
|
|
119
|
+
1,
|
|
120
|
+
9
|
|
121
|
+
],
|
|
122
|
+
[
|
|
123
|
+
12,
|
|
124
|
+
5,
|
|
125
|
+
1,
|
|
126
|
+
15,
|
|
127
|
+
14,
|
|
128
|
+
13,
|
|
129
|
+
4,
|
|
130
|
+
10,
|
|
131
|
+
0,
|
|
132
|
+
7,
|
|
133
|
+
6,
|
|
134
|
+
3,
|
|
135
|
+
9,
|
|
136
|
+
2,
|
|
137
|
+
8,
|
|
138
|
+
11
|
|
139
|
+
],
|
|
140
|
+
[
|
|
141
|
+
13,
|
|
142
|
+
11,
|
|
143
|
+
7,
|
|
144
|
+
14,
|
|
145
|
+
12,
|
|
146
|
+
1,
|
|
147
|
+
3,
|
|
148
|
+
9,
|
|
149
|
+
5,
|
|
150
|
+
0,
|
|
151
|
+
15,
|
|
152
|
+
4,
|
|
153
|
+
8,
|
|
154
|
+
6,
|
|
155
|
+
2,
|
|
156
|
+
10
|
|
157
|
+
],
|
|
158
|
+
[
|
|
159
|
+
6,
|
|
160
|
+
15,
|
|
161
|
+
14,
|
|
162
|
+
9,
|
|
163
|
+
11,
|
|
164
|
+
3,
|
|
165
|
+
0,
|
|
166
|
+
8,
|
|
167
|
+
12,
|
|
168
|
+
2,
|
|
169
|
+
13,
|
|
170
|
+
7,
|
|
171
|
+
1,
|
|
172
|
+
4,
|
|
173
|
+
10,
|
|
174
|
+
5
|
|
175
|
+
],
|
|
176
|
+
[
|
|
177
|
+
10,
|
|
178
|
+
2,
|
|
179
|
+
8,
|
|
180
|
+
4,
|
|
181
|
+
7,
|
|
182
|
+
6,
|
|
183
|
+
1,
|
|
184
|
+
5,
|
|
185
|
+
15,
|
|
186
|
+
11,
|
|
187
|
+
9,
|
|
188
|
+
14,
|
|
189
|
+
3,
|
|
190
|
+
12,
|
|
191
|
+
13,
|
|
192
|
+
0
|
|
193
|
+
],
|
|
194
|
+
[
|
|
195
|
+
0,
|
|
196
|
+
1,
|
|
197
|
+
2,
|
|
198
|
+
3,
|
|
199
|
+
4,
|
|
200
|
+
5,
|
|
201
|
+
6,
|
|
202
|
+
7,
|
|
203
|
+
8,
|
|
204
|
+
9,
|
|
205
|
+
10,
|
|
206
|
+
11,
|
|
207
|
+
12,
|
|
208
|
+
13,
|
|
209
|
+
14,
|
|
210
|
+
15
|
|
211
|
+
],
|
|
212
|
+
[
|
|
213
|
+
14,
|
|
214
|
+
10,
|
|
215
|
+
4,
|
|
216
|
+
8,
|
|
217
|
+
9,
|
|
218
|
+
15,
|
|
219
|
+
13,
|
|
220
|
+
6,
|
|
221
|
+
1,
|
|
222
|
+
12,
|
|
223
|
+
0,
|
|
224
|
+
2,
|
|
225
|
+
11,
|
|
226
|
+
7,
|
|
227
|
+
5,
|
|
228
|
+
3
|
|
229
|
+
]
|
|
230
|
+
], o = [
|
|
231
|
+
[
|
|
232
|
+
0,
|
|
233
|
+
4,
|
|
234
|
+
8,
|
|
235
|
+
12
|
|
236
|
+
],
|
|
237
|
+
[
|
|
238
|
+
1,
|
|
239
|
+
5,
|
|
240
|
+
9,
|
|
241
|
+
13
|
|
242
|
+
],
|
|
243
|
+
[
|
|
244
|
+
2,
|
|
245
|
+
6,
|
|
246
|
+
10,
|
|
247
|
+
14
|
|
248
|
+
],
|
|
249
|
+
[
|
|
250
|
+
3,
|
|
251
|
+
7,
|
|
252
|
+
11,
|
|
253
|
+
15
|
|
254
|
+
],
|
|
255
|
+
[
|
|
256
|
+
0,
|
|
257
|
+
5,
|
|
258
|
+
10,
|
|
259
|
+
15
|
|
260
|
+
],
|
|
261
|
+
[
|
|
262
|
+
1,
|
|
263
|
+
6,
|
|
264
|
+
11,
|
|
265
|
+
12
|
|
266
|
+
],
|
|
267
|
+
[
|
|
268
|
+
2,
|
|
269
|
+
7,
|
|
270
|
+
8,
|
|
271
|
+
13
|
|
272
|
+
],
|
|
273
|
+
[
|
|
274
|
+
3,
|
|
275
|
+
4,
|
|
276
|
+
9,
|
|
277
|
+
14
|
|
278
|
+
]
|
|
279
|
+
];
|
|
280
|
+
function read32(e, t) {
|
|
281
|
+
return ((e[t] ?? 0) | (e[t + 1] ?? 0) << 8 | (e[t + 2] ?? 0) << 16 | (e[t + 3] ?? 0) << 24) >>> 0;
|
|
282
|
+
}
|
|
283
|
+
function write32(e, t, n) {
|
|
284
|
+
e[t] = n & 255, e[t + 1] = n >>> 8 & 255, e[t + 2] = n >>> 16 & 255, e[t + 3] = n >>> 24;
|
|
285
|
+
}
|
|
286
|
+
function read64(e, t) {
|
|
287
|
+
let n = 0n;
|
|
288
|
+
for (let r = 7; r >= 0; --r) n = n << 8n | BigInt(e[t + r] ?? 0);
|
|
289
|
+
return n;
|
|
290
|
+
}
|
|
291
|
+
function concat(...e) {
|
|
292
|
+
let t = new Uint8Array(e.reduce((e, t) => e + t.length, 0)), n = 0;
|
|
293
|
+
for (let r of e) t.set(r, n), n += r.length;
|
|
294
|
+
return t;
|
|
295
|
+
}
|
|
296
|
+
function blakeMix(e, t, n, i, a, o, s) {
|
|
297
|
+
e[t] = (e[t] ?? 0n) + (e[n] ?? 0n) + o & r, e[a] = rotateRight((e[a] ?? 0n) ^ (e[t] ?? 0n), 32), e[i] = (e[i] ?? 0n) + (e[a] ?? 0n) & r, e[n] = rotateRight((e[n] ?? 0n) ^ (e[i] ?? 0n), 24), e[t] = (e[t] ?? 0n) + (e[n] ?? 0n) + s & r, e[a] = rotateRight((e[a] ?? 0n) ^ (e[t] ?? 0n), 16), e[i] = (e[i] ?? 0n) + (e[a] ?? 0n) & r, e[n] = rotateRight((e[n] ?? 0n) ^ (e[i] ?? 0n), 63);
|
|
298
|
+
}
|
|
299
|
+
function rotateRight(e, t) {
|
|
300
|
+
let n = BigInt(t);
|
|
301
|
+
return (e >> n | e << 64n - n) & r;
|
|
302
|
+
}
|
|
303
|
+
function blakeRound(e, t, n) {
|
|
304
|
+
for (let [r, [i, a, s, c]] of o.entries()) blakeMix(e, i, a, s, c, t[n[r * 2] ?? 0] ?? 0n, t[n[r * 2 + 1] ?? 0] ?? 0n);
|
|
305
|
+
}
|
|
306
|
+
function blake2bCompress(e, t, n, o) {
|
|
307
|
+
let s = Array.from({ length: 16 }, (e, n) => read64(t, n * 8)), c = [...e, ...i];
|
|
308
|
+
try {
|
|
309
|
+
c[12] = (c[12] ?? 0n) ^ n & r, c[13] = (c[13] ?? 0n) ^ n >> 64n, o && (c[14] = (c[14] ?? 0n) ^ r);
|
|
310
|
+
for (let e of a) blakeRound(c, s, e);
|
|
311
|
+
for (let t = 0; t < 8; t += 1) e[t] = (e[t] ?? 0n) ^ (c[t] ?? 0n) ^ (c[t + 8] ?? 0n);
|
|
312
|
+
} finally {
|
|
313
|
+
s.fill(0n), c.fill(0n);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
function blake2b(e, t) {
|
|
317
|
+
let n = [...i], r = /* @__PURE__ */ new Uint8Array(128);
|
|
318
|
+
try {
|
|
319
|
+
n[0] = (n[0] ?? 0n) ^ (16842752n | BigInt(t));
|
|
320
|
+
let i = 0;
|
|
321
|
+
for (; e.length - i > 128;) blake2bCompress(n, e.subarray(i, i + 128), BigInt(i + 128), !1), i += 128;
|
|
322
|
+
r.set(e.subarray(i)), blake2bCompress(n, r, BigInt(e.length), !0);
|
|
323
|
+
let a = new Uint8Array(t);
|
|
324
|
+
for (let e = 0; e < t; e += 1) a[e] = Number((n[Math.floor(e / 8)] ?? 0n) >> BigInt(e % 8 * 8) & 255n);
|
|
325
|
+
return a;
|
|
326
|
+
} finally {
|
|
327
|
+
n.fill(0n), r.fill(0);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
function blakeLong(e, t) {
|
|
331
|
+
let n = concat(Uint8Array.from([
|
|
332
|
+
t & 255,
|
|
333
|
+
t >>> 8 & 255,
|
|
334
|
+
t >>> 16 & 255,
|
|
335
|
+
t >>> 24
|
|
336
|
+
]), e), r = /* @__PURE__ */ new Uint8Array();
|
|
337
|
+
try {
|
|
338
|
+
if (t <= 64) return blake2b(n, t);
|
|
339
|
+
r = blake2b(n, 64);
|
|
340
|
+
let e = new Uint8Array(t), i = 0;
|
|
341
|
+
for (; t - i > 64;) if (e.set(r.subarray(0, 32), i), i += 32, t - i > 64) {
|
|
342
|
+
let e = blake2b(r, 64);
|
|
343
|
+
r.fill(0), r = e;
|
|
344
|
+
}
|
|
345
|
+
let a = blake2b(r, t - i);
|
|
346
|
+
try {
|
|
347
|
+
return e.set(a, i), e;
|
|
348
|
+
} finally {
|
|
349
|
+
a.fill(0);
|
|
350
|
+
}
|
|
351
|
+
} finally {
|
|
352
|
+
n.fill(0), r.fill(0);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
function initialHash(e, t) {
|
|
356
|
+
let n = e.password, r = e.salt, i = e.secret ?? /* @__PURE__ */ new Uint8Array(), a = e.associatedData ?? /* @__PURE__ */ new Uint8Array(), o = /* @__PURE__ */ new Uint8Array(28), s = [
|
|
357
|
+
e.parallelism,
|
|
358
|
+
e.outputBytes,
|
|
359
|
+
e.memoryKiB,
|
|
360
|
+
e.passes,
|
|
361
|
+
e.version,
|
|
362
|
+
t,
|
|
363
|
+
n.length
|
|
364
|
+
], c = 0;
|
|
365
|
+
for (let e of s) write32(o, c, e), c += 4;
|
|
366
|
+
let l = /* @__PURE__ */ new Uint8Array(12);
|
|
367
|
+
write32(l, 0, r.length), write32(l, 4, i.length), write32(l, 8, a.length);
|
|
368
|
+
let u = concat(o.subarray(0, 28), n, l.subarray(0, 4), r, l.subarray(4, 8), i, l.subarray(8, 12), a);
|
|
369
|
+
try {
|
|
370
|
+
return blake2b(u, 64);
|
|
371
|
+
} finally {
|
|
372
|
+
u.fill(0);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
function argonMix(t, n, r, i, a) {
|
|
376
|
+
let o = t[n] ?? 0, s = (t[n + 1] ?? 0) | 0, c = t[r] ?? 0, l = (t[r + 1] ?? 0) | 0, u = t[i] ?? 0, d = (t[i + 1] ?? 0) | 0, f = t[a] ?? 0, p = (t[a + 1] ?? 0) | 0, m, h, g, _;
|
|
377
|
+
m = Math.imul(o, c), h = o + c + (m << 1 >>> 0), s = s + l + (e(o, c, m) << 1 | m >>> 31) + (h / 4294967296 | 0) | 0, o = h >>> 0, g = f ^ o, _ = p ^ s, f = _ >>> 0, p = g | 0, m = Math.imul(u, f), h = u + f + (m << 1 >>> 0), d = d + p + (e(u, f, m) << 1 | m >>> 31) + (h / 4294967296 | 0) | 0, u = h >>> 0, g = c ^ u, _ = l ^ d, c = (g >>> 24 | _ << 8) >>> 0, l = _ >>> 24 | g << 8, m = Math.imul(o, c), h = o + c + (m << 1 >>> 0), s = s + l + (e(o, c, m) << 1 | m >>> 31) + (h / 4294967296 | 0) | 0, o = h >>> 0, g = f ^ o, _ = p ^ s, f = (g >>> 16 | _ << 16) >>> 0, p = _ >>> 16 | g << 16, m = Math.imul(u, f), h = u + f + (m << 1 >>> 0), d = d + p + (e(u, f, m) << 1 | m >>> 31) + (h / 4294967296 | 0) | 0, u = h >>> 0, g = c ^ u, _ = l ^ d, c = (g << 1 | _ >>> 31) >>> 0, l = _ << 1 | g >>> 31, t[n] = o, t[n + 1] = s, t[r] = c, t[r + 1] = l, t[i] = u, t[i + 1] = d, t[a] = f, t[a + 1] = p;
|
|
378
|
+
}
|
|
379
|
+
function argonRound(e, t, n, r) {
|
|
380
|
+
argonMix(e, t, t + 2 * n, t + 4 * n, t + 6 * n), argonMix(e, t + r, t + 2 * n + r, t + 4 * n + r, t + 6 * n + r), argonMix(e, t + n, t + 3 * n, t + 5 * n, t + 7 * n), argonMix(e, t + n + r, t + 3 * n + r, t + 5 * n + r, t + 7 * n + r), argonMix(e, t, t + 2 * n + r, t + 5 * n, t + 7 * n + r), argonMix(e, t + r, t + 3 * n, t + 5 * n + r, t + 6 * n), argonMix(e, t + n, t + 3 * n + r, t + 4 * n, t + 6 * n + r), argonMix(e, t + n + r, t + 2 * n, t + 4 * n + r, t + 7 * n);
|
|
381
|
+
}
|
|
382
|
+
function permuteBlock(e) {
|
|
383
|
+
for (let t = 0; t < 256; t += 32) argonRound(e, t, 4, 2);
|
|
384
|
+
for (let t = 0; t < 32; t += 4) argonRound(e, t, 32, 2);
|
|
385
|
+
}
|
|
386
|
+
function fillBlock(e, t, n, r, i, a, o) {
|
|
387
|
+
for (let r = 0; r < 256; r += 1) {
|
|
388
|
+
let i = (e[t + r] ?? 0) ^ (e[n + r] ?? 0);
|
|
389
|
+
a[r] = i, o[r] = i;
|
|
390
|
+
}
|
|
391
|
+
if (permuteBlock(o), i) for (let t = 0; t < 256; t += 1) e[r + t] = (a[t] ?? 0) ^ (o[t] ?? 0) ^ (e[r + t] ?? 0);
|
|
392
|
+
else for (let t = 0; t < 256; t += 1) e[r + t] = (a[t] ?? 0) ^ (o[t] ?? 0);
|
|
393
|
+
}
|
|
394
|
+
function indexAlpha(t, n, r, i, a, o, s) {
|
|
395
|
+
let c;
|
|
396
|
+
c = t === 0 ? n === 0 ? r - 1 : n * a + (i ? r - 1 : r === 0 ? -1 : 0) : o - a + (i ? r - 1 : r === 0 ? -1 : 0);
|
|
397
|
+
let l = e(s, s, Math.imul(s, s)), u = c - 1 - e(c, l, Math.imul(c, l));
|
|
398
|
+
return ((t === 0 || n === 3 ? 0 : (n + 1) * a) + u) % o;
|
|
399
|
+
}
|
|
400
|
+
function dataIndependent(e, t, n) {
|
|
401
|
+
return e === 1 || e === 2 && t === 0 && n < 2;
|
|
402
|
+
}
|
|
403
|
+
function compressBlocks(e, t, n, r, i) {
|
|
404
|
+
for (let n = 0; n < 256; n += 1) {
|
|
405
|
+
let a = (e[n] ?? 0) ^ (t[n] ?? 0);
|
|
406
|
+
r[n] = a, i[n] = a;
|
|
407
|
+
}
|
|
408
|
+
permuteBlock(i);
|
|
409
|
+
for (let e = 0; e < 256; e += 1) n[e] = (r[e] ?? 0) ^ (i[e] ?? 0);
|
|
410
|
+
}
|
|
411
|
+
function generateAddressBlock(e, t, n, r, i, a) {
|
|
412
|
+
e[12] = (e[12] ?? 0) + 1 >>> 0, compressBlocks(n, e, a, r, i), compressBlocks(n, a, t, r, i);
|
|
413
|
+
}
|
|
414
|
+
function toWords(e) {
|
|
415
|
+
let t = /* @__PURE__ */ new Uint32Array(256);
|
|
416
|
+
for (let n = 0; n < 256; n += 1) t[n] = read32(e, n * 4);
|
|
417
|
+
return t;
|
|
418
|
+
}
|
|
419
|
+
function fillIndexedBlock(e, t, n, r, i) {
|
|
420
|
+
let { pass: a, slice: o, lane: s } = n, { memory: c, laneLength: l, segmentLength: u, original: d, permuted: f } = t, p = s * l + o * u + r, m = (p % l === 0 ? p + l - 1 : p - 1) * 256, h = i === void 0 ? m : r % 128 * 2, g = i ?? c, _ = g[h] ?? 0, v = (g[h + 1] ?? 0) % e.parallelism;
|
|
421
|
+
a === 0 && o === 0 && (v = s);
|
|
422
|
+
let y = indexAlpha(a, o, r, v === s, u, l, _), b = p * 256;
|
|
423
|
+
fillBlock(c, m, (v * l + y) * 256, b, e.version !== 16 && a !== 0, d, f);
|
|
424
|
+
}
|
|
425
|
+
async function fillSegment(e, t, n, r) {
|
|
426
|
+
let { pass: i, slice: a, lane: o } = r, { memoryBlocks: s, segmentLength: c, original: l, permuted: u, zero: d, addressTemporary: f } = n, p = dataIndependent(t, i, a), m = /* @__PURE__ */ new Uint32Array(256), h = /* @__PURE__ */ new Uint32Array(256);
|
|
427
|
+
try {
|
|
428
|
+
p && (m[0] = i, m[2] = o, m[4] = a, m[6] = s, m[8] = e.passes, m[10] = t);
|
|
429
|
+
let g = i === 0 && a === 0 ? 2 : 0;
|
|
430
|
+
p && g !== 0 && generateAddressBlock(m, h, d, l, u, f);
|
|
431
|
+
for (let t = g; t < c; t += 512) {
|
|
432
|
+
let i = new Promise((e) => setTimeout(e, 0)), a = Math.min(t + 512, c);
|
|
433
|
+
for (let i = t; i < a; i += 1) p && i % 128 == 0 && generateAddressBlock(m, h, d, l, u, f), fillIndexedBlock(e, n, r, i, p ? h : void 0);
|
|
434
|
+
await i;
|
|
435
|
+
}
|
|
436
|
+
} finally {
|
|
437
|
+
m.fill(0), h.fill(0), f.fill(0);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
async function deriveArgon2TypeScript(e) {
|
|
441
|
+
let t = {
|
|
442
|
+
argon2d: 0,
|
|
443
|
+
argon2i: 1,
|
|
444
|
+
argon2id: 2
|
|
445
|
+
}[e.algorithm], r = Math.floor(e.memoryKiB / (4 * e.parallelism)) * 4 * e.parallelism, i = r / e.parallelism, a = i / 4, o = /* @__PURE__ */ new Uint32Array(), s = /* @__PURE__ */ new Uint8Array(), c = /* @__PURE__ */ new Uint32Array(), l = /* @__PURE__ */ new Uint32Array(), u = /* @__PURE__ */ new Uint32Array(), d = /* @__PURE__ */ new Uint32Array(), f = /* @__PURE__ */ new Uint8Array(), p = /* @__PURE__ */ new Uint32Array();
|
|
446
|
+
try {
|
|
447
|
+
o = n.take(r * 256), s = initialHash(e, t), c = /* @__PURE__ */ new Uint32Array(256), l = /* @__PURE__ */ new Uint32Array(256), u = /* @__PURE__ */ new Uint32Array(256), d = /* @__PURE__ */ new Uint32Array(256);
|
|
448
|
+
for (let t = 0; t < e.parallelism; t += 1) for (let e = 0; e < 2; e += 1) {
|
|
449
|
+
let n = /* @__PURE__ */ new Uint8Array(8);
|
|
450
|
+
write32(n, 0, e), write32(n, 4, t);
|
|
451
|
+
let r = concat(s, n), a = /* @__PURE__ */ new Uint8Array(), c = /* @__PURE__ */ new Uint32Array();
|
|
452
|
+
try {
|
|
453
|
+
a = blakeLong(r, 1024), c = toWords(a), o.set(c, (t * i + e) * 256);
|
|
454
|
+
} finally {
|
|
455
|
+
r.fill(0), a.fill(0), c.fill(0);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
let m = {
|
|
459
|
+
memory: o,
|
|
460
|
+
memoryBlocks: r,
|
|
461
|
+
laneLength: i,
|
|
462
|
+
segmentLength: a,
|
|
463
|
+
original: c,
|
|
464
|
+
permuted: l,
|
|
465
|
+
zero: u,
|
|
466
|
+
addressTemporary: d
|
|
467
|
+
};
|
|
468
|
+
for (let n = 0; n < e.passes; n += 1) for (let r = 0; r < 4; r += 1) for (let i = 0; i < e.parallelism; i += 1) await fillSegment(e, t, m, {
|
|
469
|
+
pass: n,
|
|
470
|
+
slice: r,
|
|
471
|
+
lane: i
|
|
472
|
+
});
|
|
473
|
+
f = /* @__PURE__ */ new Uint8Array(1024), p = toWords(f);
|
|
474
|
+
for (let t = 0; t < e.parallelism; t += 1) {
|
|
475
|
+
let e = (t * i + i - 1) * 256;
|
|
476
|
+
for (let t = 0; t < 256; t += 1) p[t] = (p[t] ?? 0) ^ (o[e + t] ?? 0);
|
|
477
|
+
}
|
|
478
|
+
for (let e = 0; e < 256; e += 1) write32(f, e * 4, p[e] ?? 0);
|
|
479
|
+
return blakeLong(f, e.outputBytes);
|
|
480
|
+
} finally {
|
|
481
|
+
s.fill(0), c.fill(0), l.fill(0), u.fill(0), d.fill(0), f.fill(0), p.fill(0), n.release(o);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
export { deriveArgon2TypeScript };
|
|
485
|
+
|
|
486
|
+
//# sourceMappingURL=argon2-core.js.map
|