@wtfalch/keys 0.8.0 → 0.13.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.
- package/README.md +244 -27
- package/dist/held/index.d.ts +116 -6
- package/dist/held/index.js +259 -37
- package/dist/held/schema.d.ts +17 -0
- package/dist/held/schema.js +12 -0
- package/dist/held/seal.d.ts +1 -1
- package/dist/held/seal.js +1 -1
- package/dist/held/shred.d.ts +6 -0
- package/dist/held/shred.js +6 -0
- package/dist/held/tenant.d.ts +2 -0
- package/dist/held/tenant.js +46 -0
- package/dist/held-edge/index.d.ts +17 -2
- package/dist/held-edge/index.js +51 -9
- package/dist/host/audit-endpoint.js +2 -8
- package/dist/host/company-registrar-keys.d.ts +13 -0
- package/dist/host/company-registrar-keys.js +6 -8
- package/dist/host/events.d.ts +19 -3
- package/dist/host/events.js +56 -20
- package/dist/host/index.d.ts +9 -2
- package/dist/host/index.js +9 -2
- package/dist/host/native-build.d.ts +126 -0
- package/dist/host/native-build.js +322 -0
- package/dist/host/registrar.d.ts +10 -4
- package/dist/host/registrar.js +64 -42
- package/dist/host/rotation-sweep.d.ts +56 -0
- package/dist/host/rotation-sweep.js +89 -0
- package/dist/issued/encoding.d.ts +4 -0
- package/dist/issued/encoding.js +22 -0
- package/dist/issued/index.d.ts +148 -7
- package/dist/issued/index.js +320 -34
- package/dist/issued/resign.d.ts +16 -0
- package/dist/issued/resign.js +55 -2
- package/dist/issued/tables.d.ts +38 -0
- package/dist/issued/tables.js +6 -0
- package/dist/issued/verify.d.ts +14 -3
- package/dist/issued/verify.js +10 -3
- package/dist/migrations/held/0004_keys_archived_publish_guard.sql +46 -0
- package/dist/migrations/held/0005_keys_held_migration_credential.sql +216 -0
- package/dist/migrations/held/0006_keys_held_entries_guard.sql +54 -0
- package/dist/migrations/host/0002_keys_host_held_events.sql +29 -0
- package/dist/migrations/issued/0007_keys_purpose.sql +68 -0
- package/dist/migrations/issued/0008_keys_idempotency_scope.sql +23 -0
- package/dist/migrations/tenant-rows/0001_keys_tenant_rows.sql +63 -0
- package/dist/migrations-dir.d.ts +9 -0
- package/dist/migrations-dir.js +9 -0
- package/dist/service/body.d.ts +21 -0
- package/dist/service/body.js +128 -0
- package/dist/service/errors.d.ts +24 -0
- package/dist/service/errors.js +89 -0
- package/dist/service/handler.d.ts +35 -0
- package/dist/service/handler.js +328 -0
- package/dist/service/index.d.ts +7 -0
- package/dist/service/index.js +7 -0
- package/dist/service/policy.d.ts +130 -0
- package/dist/service/policy.js +123 -0
- package/dist/service/timeout.d.ts +6 -0
- package/dist/service/timeout.js +34 -0
- package/dist/webhooks.d.ts +40 -17
- package/dist/webhooks.js +70 -26
- package/dist/worker-contract.d.ts +43 -6
- package/dist/worker-contract.js +37 -0
- package/dist/worker.d.ts +1 -1
- package/dist/worker.js +7 -28
- package/package.json +21 -7
- package/dist/testing/index.d.ts +0 -9
- package/dist/testing/index.js +0 -9
- package/dist/testing/worker-stub.d.ts +0 -55
- package/dist/testing/worker-stub.js +0 -180
package/dist/worker.js
CHANGED
|
@@ -1,34 +1,13 @@
|
|
|
1
|
-
import { ID_PATTERN, WORKER_HTTP, } from './worker-contract.js';
|
|
2
|
-
export { WORKER_HTTP } from './worker-contract.js';
|
|
1
|
+
import { ID_PATTERN, WORKER_ERROR_CODES, WORKER_HTTP, } from './worker-contract.js';
|
|
2
|
+
export { WORKER_ERROR_CODES, WORKER_HTTP } from './worker-contract.js';
|
|
3
3
|
const DEFAULT_TIMEOUT_MS = 10_000;
|
|
4
4
|
/**
|
|
5
|
-
* The
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* not exist at all -- both are `unreachable` from here.
|
|
10
|
-
*
|
|
11
|
-
* A value-level mirror, not a type import: `WorkerErrorCode` is a union of
|
|
12
|
-
* string literals, which has no runtime representation to check a decoded
|
|
13
|
-
* body's `error.code` against.
|
|
5
|
+
* The codes the Worker itself ever sends: `WORKER_ERROR_CODES` without
|
|
6
|
+
* `unreachable`. The contract says the Worker never sends `unreachable`, so a
|
|
7
|
+
* response body claiming it is exactly as untrustworthy as one claiming a code
|
|
8
|
+
* from a newer Worker that this client does not know: both become `unreachable`.
|
|
14
9
|
*/
|
|
15
|
-
const KNOWN_ERROR_CODES = new Set(
|
|
16
|
-
'credential_invalid',
|
|
17
|
-
'credential_expired',
|
|
18
|
-
'credential_revoked',
|
|
19
|
-
'grant_missing',
|
|
20
|
-
'rate_limited',
|
|
21
|
-
'bad_request',
|
|
22
|
-
'aad_mismatch',
|
|
23
|
-
'unknown_generation',
|
|
24
|
-
'bad_minter_signature',
|
|
25
|
-
'minter_expired',
|
|
26
|
-
'minter_revoked',
|
|
27
|
-
'lineage_mismatch',
|
|
28
|
-
'bad_old_signature',
|
|
29
|
-
'unlogged_row',
|
|
30
|
-
'not_in_lineage',
|
|
31
|
-
]);
|
|
10
|
+
const KNOWN_ERROR_CODES = new Set(WORKER_ERROR_CODES.filter((c) => c !== 'unreachable'));
|
|
32
11
|
/** The only statuses `errors.ts` ever sends for a refusal. Anything else -- 3xx, 5xx, 0 (an opaque redirect) -- is `unreachable`, whatever its body says. */
|
|
33
12
|
const KNOWN_FAILURE_STATUSES = new Set([400, 401, 403, 429]);
|
|
34
13
|
function assertHttpsOrigin(url) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wtfalch/keys",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"description": "The estate's own bearer keys, issued to callers and held on their behalf: two entries, issued and held, nothing stored in common.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -50,9 +50,9 @@
|
|
|
50
50
|
"types": "./dist/host/index.d.ts",
|
|
51
51
|
"default": "./dist/host/index.js"
|
|
52
52
|
},
|
|
53
|
-
"./
|
|
54
|
-
"types": "./dist/
|
|
55
|
-
"default": "./dist/
|
|
53
|
+
"./service": {
|
|
54
|
+
"types": "./dist/service/index.d.ts",
|
|
55
|
+
"default": "./dist/service/index.js"
|
|
56
56
|
}
|
|
57
57
|
},
|
|
58
58
|
"sideEffects": false,
|
|
@@ -63,21 +63,35 @@
|
|
|
63
63
|
"node": ">=22.0.0"
|
|
64
64
|
},
|
|
65
65
|
"scripts": {
|
|
66
|
-
"build": "rm -rf dist && tsc -p tsconfig.build.json && mkdir -p dist/migrations && cp -R src/migrations/issued src/migrations/held src/migrations/host dist/migrations/",
|
|
66
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json && mkdir -p dist/migrations && cp -R src/migrations/issued src/migrations/held src/migrations/host src/migrations/tenant-rows dist/migrations/",
|
|
67
67
|
"prepack": "pnpm build",
|
|
68
68
|
"typecheck": "tsc --noEmit",
|
|
69
69
|
"test": "vitest run"
|
|
70
70
|
},
|
|
71
71
|
"peerDependencies": {
|
|
72
|
-
"drizzle-orm": ">=0.45.2 <1.0.0"
|
|
72
|
+
"drizzle-orm": ">=0.45.2 <1.0.0",
|
|
73
|
+
"@wtfalch/authz": "0.18.0"
|
|
74
|
+
},
|
|
75
|
+
"dependencies": {
|
|
76
|
+
"@wtfalch/utils": "0.1.0",
|
|
77
|
+
"jose": "6.2.12",
|
|
78
|
+
"@wtfalch/contracts": "0.2.0"
|
|
73
79
|
},
|
|
74
80
|
"devDependencies": {
|
|
75
81
|
"@electric-sql/pglite": "^0.5.8",
|
|
76
82
|
"@types/node": "^22",
|
|
77
83
|
"@wtfalch/db": "0.5.2",
|
|
84
|
+
"@wtfalch/keys-client": "workspace:^",
|
|
78
85
|
"drizzle-orm": "0.45.2",
|
|
79
86
|
"postgres": "^3.4.5",
|
|
80
87
|
"typescript": "^5.9.0",
|
|
81
|
-
"vitest": "^4.1.6"
|
|
88
|
+
"vitest": "^4.1.6",
|
|
89
|
+
"@wtfalch/authz": "0.18.0",
|
|
90
|
+
"zod": "^4.1.13"
|
|
91
|
+
},
|
|
92
|
+
"peerDependenciesMeta": {
|
|
93
|
+
"@wtfalch/authz": {
|
|
94
|
+
"optional": true
|
|
95
|
+
}
|
|
82
96
|
}
|
|
83
97
|
}
|
package/dist/testing/index.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `./testing`: an in-process Worker stub built on the package's own signing
|
|
3
|
-
* and AAD code, for a host's tests that need a `WorkerClient` without a
|
|
4
|
-
* live Worker (issue #83). `canonicalJson`, `encodeSigned` and
|
|
5
|
-
* `pickSignable` are re-exported here -- not from `./issued` or the
|
|
6
|
-
* public root -- because they exist for building test fixtures, not for a
|
|
7
|
-
* host's runtime code, which never needs to construct signed bytes itself.
|
|
8
|
-
*/
|
|
9
|
-
export { createKeysWorkerStub, TEST_CREDENTIAL, canonicalJson, encodeSigned, pickSignable, type CreateKeysWorkerStubOptions, type KeysWorkerStub, } from './worker-stub.js';
|
package/dist/testing/index.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `./testing`: an in-process Worker stub built on the package's own signing
|
|
3
|
-
* and AAD code, for a host's tests that need a `WorkerClient` without a
|
|
4
|
-
* live Worker (issue #83). `canonicalJson`, `encodeSigned` and
|
|
5
|
-
* `pickSignable` are re-exported here -- not from `./issued` or the
|
|
6
|
-
* public root -- because they exist for building test fixtures, not for a
|
|
7
|
-
* host's runtime code, which never needs to construct signed bytes itself.
|
|
8
|
-
*/
|
|
9
|
-
export { createKeysWorkerStub, TEST_CREDENTIAL, canonicalJson, encodeSigned, pickSignable, } from './worker-stub.js';
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { canonicalJson, encodeSigned, pickSignable } from '../issued/encoding.js';
|
|
2
|
-
import type { VerifyKeys, WorkerClient, WorkerErrorCode } from '../worker-contract.js';
|
|
3
|
-
/**
|
|
4
|
-
* An in-process stand-in for the unwrap Worker (`packages/worker`), for
|
|
5
|
-
* every host test that needs a `WorkerClient<TGrant>` without a live
|
|
6
|
-
* Worker. Implements the full contract -- `wrap`/`unwrap`/`rewrap` for
|
|
7
|
-
* `./held`, `signRow`/`resign`/`recordEviction` for `./issued` -- with the
|
|
8
|
-
* package's own real signing, AAD and sealing code, not a reimplementation:
|
|
9
|
-
* it imports `encodeSigned`/`canonicalJson`/`pickSignable` from `../issued/
|
|
10
|
-
* encoding.js`, `encodeAad` from `../held/aad.js`, and `aesGcmSeal`/
|
|
11
|
-
* `aesGcmOpen` from `../held/seal.js` -- the same module
|
|
12
|
-
* `packages/worker/src/crypto.ts` imports for its own AES-256-GCM layout
|
|
13
|
-
* (#86: before this, the two copies could drift silently) -- rather than
|
|
14
|
-
* duplicating any of them, so a signature or a sealed key this stub
|
|
15
|
-
* produces is verified the same way a real Worker's would be -- proven by
|
|
16
|
-
* `worker-stub.test.ts`, which runs `check()` (`../issued`) and `open()`
|
|
17
|
-
* (`../held`) against rows this stub signed and sealed, and by
|
|
18
|
-
* `../../../worker/src/crypto.test.ts`, which opens a value sealed through
|
|
19
|
-
* this same module with the Worker's own re-exported `aesGcmOpen`, and the
|
|
20
|
-
* reverse.
|
|
21
|
-
*
|
|
22
|
-
* This is what every host's own `tests/keys-worker-stub.ts` re-implemented
|
|
23
|
-
* by hand (issue #83, "checked by hand against dist/issued/index.d.ts") --
|
|
24
|
-
* see the package README's "Adopting ./testing".
|
|
25
|
-
*
|
|
26
|
-
* Signing uses `node:crypto`'s classic, synchronous `generateKeyPairSync`/
|
|
27
|
-
* `sign`/`verify` rather than `webcrypto.subtle`: both produce and check
|
|
28
|
-
* the same RFC 8032 Ed25519 signatures -- `worker-stub.test.ts` proves it
|
|
29
|
-
* by running `../issued/verify.js`'s own `verifyIssuedRowSignature`
|
|
30
|
-
* (which uses `webcrypto.subtle.verify`) against a signature this stub
|
|
31
|
-
* produced -- but being synchronous, it lets `verifyKeys`/`verifyKeysEnv`
|
|
32
|
-
* be plain properties instead of a promise a caller would have to await
|
|
33
|
-
* before building `KEYS_VERIFY_KEYS`.
|
|
34
|
-
*/
|
|
35
|
-
/** Every credential this stub accepts. Anything else is `credential_invalid`, exactly as a real bad bearer token would be. */
|
|
36
|
-
export declare const TEST_CREDENTIAL = "stub-test-credential";
|
|
37
|
-
type Method = keyof WorkerClient<unknown>;
|
|
38
|
-
export interface CreateKeysWorkerStubOptions {
|
|
39
|
-
/** Unix milliseconds. Defaults to `Date.now`; a test passes a fake clock to check `minter_expired`. */
|
|
40
|
-
readonly now?: () => number;
|
|
41
|
-
}
|
|
42
|
-
export interface KeysWorkerStub<TGrant> extends WorkerClient<TGrant> {
|
|
43
|
-
/** This stub's one Ed25519 public key, for `createCredentialIssuer`'s `verifyKeys` option. */
|
|
44
|
-
readonly verifyKeys: VerifyKeys;
|
|
45
|
-
/** The same key, base64url, in the comma-separated form `KEYS_VERIFY_KEYS` takes. */
|
|
46
|
-
readonly verifyKeysEnv: string;
|
|
47
|
-
/** Calls received so far, per method -- for asserting a code path called the Worker exactly once, or not at all. */
|
|
48
|
-
readonly callCounts: Record<Method, number>;
|
|
49
|
-
/** Every call to `method` (or `'all'`) fails with `code` until `clearFailure()`. */
|
|
50
|
-
failAs(method: Method | 'all', code: WorkerErrorCode, retryAfterMs?: number): void;
|
|
51
|
-
clearFailure(): void;
|
|
52
|
-
}
|
|
53
|
-
/** `canonicalJson`, `encodeSigned` and `pickSignable` are the package's own -- re-exported by `./testing/index.js`, not reimplemented here. */
|
|
54
|
-
export declare function createKeysWorkerStub<TGrant = unknown>(options?: CreateKeysWorkerStubOptions): KeysWorkerStub<TGrant>;
|
|
55
|
-
export { canonicalJson, encodeSigned, pickSignable };
|
|
@@ -1,180 +0,0 @@
|
|
|
1
|
-
import { generateKeyPairSync, sign as signWithKey, verify as verifyWithKey, webcrypto, } from 'node:crypto';
|
|
2
|
-
import { encodeAad } from '../held/aad.js';
|
|
3
|
-
import { aesGcmOpen, aesGcmSeal } from '../held/seal.js';
|
|
4
|
-
import { canonicalJson, encodeSigned, pickSignable } from '../issued/encoding.js';
|
|
5
|
-
/**
|
|
6
|
-
* An in-process stand-in for the unwrap Worker (`packages/worker`), for
|
|
7
|
-
* every host test that needs a `WorkerClient<TGrant>` without a live
|
|
8
|
-
* Worker. Implements the full contract -- `wrap`/`unwrap`/`rewrap` for
|
|
9
|
-
* `./held`, `signRow`/`resign`/`recordEviction` for `./issued` -- with the
|
|
10
|
-
* package's own real signing, AAD and sealing code, not a reimplementation:
|
|
11
|
-
* it imports `encodeSigned`/`canonicalJson`/`pickSignable` from `../issued/
|
|
12
|
-
* encoding.js`, `encodeAad` from `../held/aad.js`, and `aesGcmSeal`/
|
|
13
|
-
* `aesGcmOpen` from `../held/seal.js` -- the same module
|
|
14
|
-
* `packages/worker/src/crypto.ts` imports for its own AES-256-GCM layout
|
|
15
|
-
* (#86: before this, the two copies could drift silently) -- rather than
|
|
16
|
-
* duplicating any of them, so a signature or a sealed key this stub
|
|
17
|
-
* produces is verified the same way a real Worker's would be -- proven by
|
|
18
|
-
* `worker-stub.test.ts`, which runs `check()` (`../issued`) and `open()`
|
|
19
|
-
* (`../held`) against rows this stub signed and sealed, and by
|
|
20
|
-
* `../../../worker/src/crypto.test.ts`, which opens a value sealed through
|
|
21
|
-
* this same module with the Worker's own re-exported `aesGcmOpen`, and the
|
|
22
|
-
* reverse.
|
|
23
|
-
*
|
|
24
|
-
* This is what every host's own `tests/keys-worker-stub.ts` re-implemented
|
|
25
|
-
* by hand (issue #83, "checked by hand against dist/issued/index.d.ts") --
|
|
26
|
-
* see the package README's "Adopting ./testing".
|
|
27
|
-
*
|
|
28
|
-
* Signing uses `node:crypto`'s classic, synchronous `generateKeyPairSync`/
|
|
29
|
-
* `sign`/`verify` rather than `webcrypto.subtle`: both produce and check
|
|
30
|
-
* the same RFC 8032 Ed25519 signatures -- `worker-stub.test.ts` proves it
|
|
31
|
-
* by running `../issued/verify.js`'s own `verifyIssuedRowSignature`
|
|
32
|
-
* (which uses `webcrypto.subtle.verify`) against a signature this stub
|
|
33
|
-
* produced -- but being synchronous, it lets `verifyKeys`/`verifyKeysEnv`
|
|
34
|
-
* be plain properties instead of a promise a caller would have to await
|
|
35
|
-
* before building `KEYS_VERIFY_KEYS`.
|
|
36
|
-
*/
|
|
37
|
-
// ---------------------------------------------------------------------------
|
|
38
|
-
// The stub
|
|
39
|
-
// ---------------------------------------------------------------------------
|
|
40
|
-
/** Every credential this stub accepts. Anything else is `credential_invalid`, exactly as a real bad bearer token would be. */
|
|
41
|
-
export const TEST_CREDENTIAL = 'stub-test-credential';
|
|
42
|
-
const KEK_ID = 'kek-test';
|
|
43
|
-
const SIGN_GENERATION_ID = 'sign-test';
|
|
44
|
-
function failure(code, retryAfterMs) {
|
|
45
|
-
return {
|
|
46
|
-
ok: false,
|
|
47
|
-
error: retryAfterMs !== undefined ? { code, retryAfterMs } : { code },
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
function publicKeyRawOf(key) {
|
|
51
|
-
const jwk = key.export({ format: 'jwk' });
|
|
52
|
-
if (!jwk.x) {
|
|
53
|
-
throw new Error('createKeysWorkerStub: Ed25519 public key export had no JWK x coordinate');
|
|
54
|
-
}
|
|
55
|
-
return new Uint8Array(Buffer.from(jwk.x, 'base64url'));
|
|
56
|
-
}
|
|
57
|
-
/** `canonicalJson`, `encodeSigned` and `pickSignable` are the package's own -- re-exported by `./testing/index.js`, not reimplemented here. */
|
|
58
|
-
export function createKeysWorkerStub(options = {}) {
|
|
59
|
-
const now = options.now ?? (() => Date.now());
|
|
60
|
-
const { publicKey, privateKey } = generateKeyPairSync('ed25519');
|
|
61
|
-
const publicKeyRaw = publicKeyRawOf(publicKey);
|
|
62
|
-
const kek = webcrypto.getRandomValues(new Uint8Array(32));
|
|
63
|
-
const callCounts = {
|
|
64
|
-
wrap: 0,
|
|
65
|
-
unwrap: 0,
|
|
66
|
-
rewrap: 0,
|
|
67
|
-
signRow: 0,
|
|
68
|
-
resign: 0,
|
|
69
|
-
recordEviction: 0,
|
|
70
|
-
};
|
|
71
|
-
let forced = null;
|
|
72
|
-
function record(method) {
|
|
73
|
-
callCounts[method] += 1;
|
|
74
|
-
if (forced && (forced.method === method || forced.method === 'all')) {
|
|
75
|
-
return failure(forced.code, forced.retryAfterMs);
|
|
76
|
-
}
|
|
77
|
-
return null;
|
|
78
|
-
}
|
|
79
|
-
function signRowBytes(row) {
|
|
80
|
-
return new Uint8Array(signWithKey(null, encodeSigned('issued-row', pickSignable(row)), privateKey));
|
|
81
|
-
}
|
|
82
|
-
function verifyRowSignature(row, signature) {
|
|
83
|
-
return verifyWithKey(null, encodeSigned('issued-row', pickSignable(row)), publicKey, signature);
|
|
84
|
-
}
|
|
85
|
-
const client = {
|
|
86
|
-
async wrap(req) {
|
|
87
|
-
const forcedFailure = record('wrap');
|
|
88
|
-
if (forcedFailure)
|
|
89
|
-
return forcedFailure;
|
|
90
|
-
if (req.credential !== TEST_CREDENTIAL)
|
|
91
|
-
return failure('credential_invalid');
|
|
92
|
-
const dataKey = webcrypto.getRandomValues(new Uint8Array(32));
|
|
93
|
-
const wrappedKey = await aesGcmSeal(kek, dataKey, encodeAad(req.binding));
|
|
94
|
-
return { ok: true, dataKey, wrappedKey, kekId: KEK_ID };
|
|
95
|
-
},
|
|
96
|
-
async unwrap(req) {
|
|
97
|
-
const forcedFailure = record('unwrap');
|
|
98
|
-
if (forcedFailure)
|
|
99
|
-
return forcedFailure;
|
|
100
|
-
if (req.credential !== TEST_CREDENTIAL)
|
|
101
|
-
return failure('credential_invalid');
|
|
102
|
-
if (req.kekId !== KEK_ID)
|
|
103
|
-
return failure('unknown_generation');
|
|
104
|
-
const dataKey = await aesGcmOpen(kek, req.wrappedKey, encodeAad(req.binding));
|
|
105
|
-
if (!dataKey)
|
|
106
|
-
return failure('aad_mismatch');
|
|
107
|
-
return { ok: true, dataKey };
|
|
108
|
-
},
|
|
109
|
-
async rewrap(req) {
|
|
110
|
-
const forcedFailure = record('rewrap');
|
|
111
|
-
if (forcedFailure)
|
|
112
|
-
return forcedFailure;
|
|
113
|
-
if (req.credential !== TEST_CREDENTIAL)
|
|
114
|
-
return failure('credential_invalid');
|
|
115
|
-
if (req.kekIdOld !== KEK_ID || req.kekIdNew !== KEK_ID)
|
|
116
|
-
return failure('unknown_generation');
|
|
117
|
-
const aad = encodeAad(req.binding);
|
|
118
|
-
const dataKey = await aesGcmOpen(kek, req.wrappedKey, aad);
|
|
119
|
-
if (!dataKey)
|
|
120
|
-
return failure('aad_mismatch');
|
|
121
|
-
const wrappedKey = await aesGcmSeal(kek, dataKey, aad);
|
|
122
|
-
return { ok: true, wrappedKey };
|
|
123
|
-
},
|
|
124
|
-
async signRow(req) {
|
|
125
|
-
const forcedFailure = record('signRow');
|
|
126
|
-
if (forcedFailure)
|
|
127
|
-
return forcedFailure;
|
|
128
|
-
if (req.credential !== TEST_CREDENTIAL)
|
|
129
|
-
return failure('credential_invalid');
|
|
130
|
-
const { candidate, minter } = req;
|
|
131
|
-
if (minter) {
|
|
132
|
-
if (!verifyRowSignature(minter.row, minter.signature)) {
|
|
133
|
-
return failure('bad_minter_signature');
|
|
134
|
-
}
|
|
135
|
-
if (minter.row.expiresAt <= now())
|
|
136
|
-
return failure('minter_expired');
|
|
137
|
-
}
|
|
138
|
-
// Minimal lineage check -- the mint log and resign/recordEviction
|
|
139
|
-
// sweeps a real Worker keeps are out of this stub's scope.
|
|
140
|
-
const expectedIssuedById = minter ? minter.row.id : null;
|
|
141
|
-
if (candidate.issuedById !== expectedIssuedById)
|
|
142
|
-
return failure('lineage_mismatch');
|
|
143
|
-
return { ok: true, signature: signRowBytes(candidate), generationId: SIGN_GENERATION_ID };
|
|
144
|
-
},
|
|
145
|
-
async resign(req) {
|
|
146
|
-
const forcedFailure = record('resign');
|
|
147
|
-
if (forcedFailure)
|
|
148
|
-
return forcedFailure;
|
|
149
|
-
if (req.credential !== TEST_CREDENTIAL)
|
|
150
|
-
return failure('credential_invalid');
|
|
151
|
-
if (req.oldGenerationId !== SIGN_GENERATION_ID)
|
|
152
|
-
return failure('unknown_generation');
|
|
153
|
-
if (!verifyRowSignature(req.row, req.oldSignature))
|
|
154
|
-
return failure('bad_old_signature');
|
|
155
|
-
return { ok: true, signature: signRowBytes(req.row), generationId: SIGN_GENERATION_ID };
|
|
156
|
-
},
|
|
157
|
-
async recordEviction(req) {
|
|
158
|
-
const forcedFailure = record('recordEviction');
|
|
159
|
-
if (forcedFailure)
|
|
160
|
-
return forcedFailure;
|
|
161
|
-
if (req.credential !== TEST_CREDENTIAL)
|
|
162
|
-
return failure('credential_invalid');
|
|
163
|
-
return { ok: true, receiptId: `receipt-${req.credentialId}-${req.revokedAt}` };
|
|
164
|
-
},
|
|
165
|
-
};
|
|
166
|
-
return Object.assign(client, {
|
|
167
|
-
verifyKeys: [publicKeyRaw],
|
|
168
|
-
verifyKeysEnv: Buffer.from(publicKeyRaw).toString('base64url'),
|
|
169
|
-
callCounts,
|
|
170
|
-
failAs(method, code, retryAfterMs) {
|
|
171
|
-
forced = { method, code, retryAfterMs };
|
|
172
|
-
},
|
|
173
|
-
clearFailure() {
|
|
174
|
-
forced = null;
|
|
175
|
-
},
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
// Re-exported by ./testing/index.js so a host's own fixture code can build
|
|
179
|
-
// custom signable bytes without depending on this file's internals.
|
|
180
|
-
export { canonicalJson, encodeSigned, pickSignable };
|