@wtfalch/keys 0.7.0 → 0.12.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/README.md +349 -7
- package/dist/held/index.d.ts +116 -6
- package/dist/held/index.js +247 -25
- package/dist/held/schema.d.ts +17 -0
- package/dist/held/schema.js +12 -0
- package/dist/held/shred.d.ts +6 -0
- package/dist/held/shred.js +6 -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 +106 -0
- package/dist/host/company-registrar-keys.js +211 -0
- package/dist/host/events.d.ts +19 -3
- package/dist/host/events.js +55 -20
- package/dist/host/index.d.ts +15 -1
- package/dist/host/index.js +15 -1
- package/dist/host/native-build.d.ts +126 -0
- package/dist/host/native-build.js +322 -0
- package/dist/host/registrar.d.ts +87 -0
- package/dist/host/registrar.js +284 -0
- 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/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/testing/index.d.ts +3 -0
- package/dist/testing/index.js +3 -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 -3
package/dist/issued/index.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import type { PgDatabase, PgQueryResultHKT } from 'drizzle-orm/pg-core';
|
|
2
2
|
import type { SerializedCredential, VerifyKeys, WorkerClient, WorkerErrorCode } from '../worker-contract.js';
|
|
3
3
|
import type { RateLimiter } from './ratelimit.js';
|
|
4
|
+
export type { KeysIssuedCredentialRow } from './tables.js';
|
|
4
5
|
export { ID_PATTERN, SIGNED_PREFIX, type RequestMeta, type SerializedCredential, type SignableRow, type SignedKind, type SignedRow, type SignRowRequest, type SignRowResult, type SignRowSuccess, type VerifyKeys, type WorkerClient, type WorkerError, type WorkerErrorCode, type WorkerFailure, } from '../worker-contract.js';
|
|
5
6
|
export { createPostgresRateLimiter, type CreatePostgresRateLimiterOptions, type RateLimiter, type RateLimitResult, } from './ratelimit.js';
|
|
6
|
-
export { createSigningGenerationMigrator, MAX_RESIGN_BATCH_SIZE, type CreateSigningGenerationMigratorOptions, type ResignGenerationBatchRequest, type ResignGenerationBatchResult, type ResignGenerationRefusalReason, type SigningGenerationCursor, type SigningGenerationMigrator, } from './resign.js';
|
|
7
|
+
export { createSigningGenerationMigrator, MAX_RESIGN_BATCH_SIZE, RESIGN_GENERATION_REFUSAL_REASONS, type CreateSigningGenerationMigratorOptions, type ResignGenerationBatchRequest, type ResignGenerationBatchResult, type ResignGenerationRefusalReason, type SigningGenerationCursor, type SigningGenerationMigrator, } from './resign.js';
|
|
7
8
|
export type Db = PgDatabase<PgQueryResultHKT, any, any>;
|
|
8
9
|
/**
|
|
9
10
|
* #47: a built-in sandbox/live split, so a stamped host does not invent its
|
|
@@ -12,6 +13,15 @@ export type Db = PgDatabase<PgQueryResultHKT, any, any>;
|
|
|
12
13
|
* a presented secret is, same as before this existed.
|
|
13
14
|
*/
|
|
14
15
|
export type KeyEnvironment = 'live' | 'test';
|
|
16
|
+
/**
|
|
17
|
+
* What a credential is for. `ordinary` is an app or user key a company may
|
|
18
|
+
* revoke. `company_root` is a company's service root: it can be rolled with
|
|
19
|
+
* `rotate()` but not removed through company key management. `platform_master`
|
|
20
|
+
* stays under the operator. A row of unknown purpose (older than this field)
|
|
21
|
+
* reads as `null`; nothing guesses it.
|
|
22
|
+
*/
|
|
23
|
+
export type CredentialPurpose = 'ordinary' | 'company_root' | 'platform_master';
|
|
24
|
+
export declare const CREDENTIAL_PURPOSES: readonly CredentialPurpose[];
|
|
15
25
|
export interface AuditEvent {
|
|
16
26
|
readonly name: 'credential.minted' | 'credential.rotated' | 'credential.revoked';
|
|
17
27
|
readonly credentialId: string;
|
|
@@ -28,8 +38,38 @@ export interface CreateCredentialIssuerOptions<TGrant> {
|
|
|
28
38
|
readonly credential: SerializedCredential;
|
|
29
39
|
/** The Worker's current Ed25519 public key, and its previous one during a Worker key-rotation window. */
|
|
30
40
|
readonly verifyKeys: VerifyKeys;
|
|
31
|
-
/**
|
|
41
|
+
/**
|
|
42
|
+
* The signing generation `verifyKeys[1]` (`previous`) belongs to. When set,
|
|
43
|
+
* `check()` accepts the `previous` key only for a row labelled with this
|
|
44
|
+
* generation; any other row that verifies only under `previous` is
|
|
45
|
+
* `signature_invalid`. Unset, both keys are tried for every row. That is
|
|
46
|
+
* deprecated: 1.0.0 requires this whenever `previous` is passed.
|
|
47
|
+
*
|
|
48
|
+
* The label is not signed. Rollback is not prevented: someone who can write
|
|
49
|
+
* the database can restore an older row with its older signature.
|
|
50
|
+
*/
|
|
51
|
+
readonly previousGenerationId?: string;
|
|
52
|
+
/**
|
|
53
|
+
* `credential.minted` / `credential.rotated` / `credential.revoked`, for the
|
|
54
|
+
* host's own audit chain. Called after the write has committed. A throw does
|
|
55
|
+
* not reach the caller: it goes to `onAuditError`, and the one-time secret is
|
|
56
|
+
* still returned and every remaining revoke event is still emitted.
|
|
57
|
+
*/
|
|
32
58
|
readonly audit?: AuditCallback;
|
|
59
|
+
/**
|
|
60
|
+
* Called with the event and the error when `audit` throws. Unset, one fixed
|
|
61
|
+
* line naming the event and the error's name is logged. Never receives a
|
|
62
|
+
* secret: events carry ids only.
|
|
63
|
+
*/
|
|
64
|
+
readonly onAuditError?: (event: AuditEvent, error: unknown) => void;
|
|
65
|
+
/**
|
|
66
|
+
* Runs inside the transaction that makes the change, for hosts that keep
|
|
67
|
+
* events in the same database as `db`. The event and the change land
|
|
68
|
+
* together. A throw aborts the write: nothing is stored, and `issue()`,
|
|
69
|
+
* `rotate()` or `revoke()` throws. Pass `tx` to the same writer the host
|
|
70
|
+
* uses elsewhere, e.g. `recordEventInTransaction` from `./host`.
|
|
71
|
+
*/
|
|
72
|
+
readonly auditInTransaction?: (tx: Db, event: AuditEvent) => Promise<void> | void;
|
|
33
73
|
/** This host's split-secret prefix, e.g. `ai_live_`. Fixed per issuer; never inferred from a row. */
|
|
34
74
|
readonly prefix: string;
|
|
35
75
|
/**
|
|
@@ -46,15 +86,54 @@ export interface IssueRequest<TGrant> {
|
|
|
46
86
|
readonly grants: readonly TGrant[];
|
|
47
87
|
/** The id of the credential minting this one, or `null` for a root mint. */
|
|
48
88
|
readonly minter: string | null;
|
|
49
|
-
/** Unix milliseconds. Required: refused when missing
|
|
89
|
+
/** Unix milliseconds. Required: refused when missing, already past, or later than the minter's own -- never clamped. */
|
|
50
90
|
readonly expiresAt: number;
|
|
51
91
|
readonly idempotencyKey?: string;
|
|
52
92
|
/** Default `'live'`. A child must match its minter's environment -- see `IssueRefusalReason`. */
|
|
53
93
|
readonly environment?: KeyEnvironment;
|
|
94
|
+
/**
|
|
95
|
+
* Default `'ordinary'`. For trusted server code only: a host never fills it
|
|
96
|
+
* from a customer's request. A child (`minter` set) can only be `'ordinary'`.
|
|
97
|
+
*/
|
|
98
|
+
readonly purpose?: CredentialPurpose;
|
|
99
|
+
/**
|
|
100
|
+
* The organisation a root belongs to. For trusted server code only. A child
|
|
101
|
+
* takes its minter's organisation and may not name a different one.
|
|
102
|
+
*/
|
|
103
|
+
readonly organisationId?: string;
|
|
104
|
+
/**
|
|
105
|
+
* For trusted server code only. When the key is held by a stale row of this
|
|
106
|
+
* same minter (revoked or expired), `issue()` frees the key inside its own
|
|
107
|
+
* transaction, keeping the old row, and mints a new credential under it.
|
|
108
|
+
* A live row still answers as a replay.
|
|
109
|
+
*/
|
|
110
|
+
readonly replaceStale?: boolean;
|
|
54
111
|
}
|
|
112
|
+
/**
|
|
113
|
+
* Closed unions: each has a constant listing its members, pinned by `src/exports.test.ts`.
|
|
114
|
+
* Adding a member is a major change after 1.0.0. A union that embeds `WorkerErrorCode`
|
|
115
|
+
* spreads `WORKER_ERROR_CODES` into its list.
|
|
116
|
+
*/
|
|
117
|
+
export { WORKER_ERROR_CODES } from '../worker-contract.js';
|
|
55
118
|
export type IssueRefusalReason = 'expires_at_required' | 'expires_at_past' | 'minter_not_found' | 'minter_revoked' | 'minter_expired'
|
|
119
|
+
/** A child cannot outlive its minter: `request.expiresAt` is later than the minter row's own. Equal is allowed. */
|
|
120
|
+
| 'expires_after_minter'
|
|
121
|
+
/** A child asked for a purpose other than `'ordinary'`; nothing was signed. */
|
|
122
|
+
| 'purpose_not_allowed'
|
|
123
|
+
/** A child named an organisation that is not its minter's; nothing was signed. */
|
|
124
|
+
| 'organisation_mismatch'
|
|
125
|
+
/** The `idempotencyKey` is held by a row of this minter with other grants, environment or purpose. */
|
|
126
|
+
| 'idempotency_conflict'
|
|
127
|
+
/** The `idempotencyKey` is held by a row that has been revoked. Nothing was minted. */
|
|
128
|
+
| 'idempotent_row_revoked'
|
|
129
|
+
/** The `idempotencyKey` is held by a row that has expired. Nothing was minted. */
|
|
130
|
+
| 'idempotent_row_expired'
|
|
56
131
|
/** #47: `request.environment` (or its `'live'` default) does not match the minter row's own. */
|
|
57
|
-
| 'environment_mismatch'
|
|
132
|
+
| 'environment_mismatch'
|
|
133
|
+
/** The Worker's signature does not verify against `verifyKeys`; nothing was stored. */
|
|
134
|
+
| 'bad_worker_signature' | WorkerErrorCode;
|
|
135
|
+
/** Every member of `IssueRefusalReason`. */
|
|
136
|
+
export declare const ISSUE_REFUSAL_REASONS: readonly ["expires_at_required", "expires_at_past", "minter_not_found", "minter_revoked", "minter_expired", "expires_after_minter", "purpose_not_allowed", "organisation_mismatch", "idempotency_conflict", "idempotent_row_revoked", "idempotent_row_expired", "environment_mismatch", "bad_worker_signature", "credential_invalid", "credential_expired", "credential_revoked", "grant_missing", "rate_limited", "bad_request", "aad_mismatch", "unknown_generation", "bad_minter_signature", "minter_expired", "minter_revoked", "minter_exceeded", "lineage_mismatch", "bad_old_signature", "unlogged_row", "not_in_lineage", "unreachable"];
|
|
58
137
|
export type IssueResult = {
|
|
59
138
|
readonly ok: true;
|
|
60
139
|
readonly id: string;
|
|
@@ -64,29 +143,42 @@ export type IssueResult = {
|
|
|
64
143
|
} | {
|
|
65
144
|
readonly ok: false;
|
|
66
145
|
readonly reason: IssueRefusalReason;
|
|
146
|
+
/** Only with `reason: 'rate_limited'`, when the Worker said how long to wait. */
|
|
147
|
+
readonly retryAfterMs?: number;
|
|
67
148
|
};
|
|
68
149
|
export type CheckRefusalReason = 'not_found' | 'signature_invalid' | 'revoked' | 'expired' | 'secret_mismatch'
|
|
69
150
|
/** #45: `rateLimiter` refused this credential id for the current window. */
|
|
70
151
|
| 'rate_limited';
|
|
152
|
+
/** Every member of `CheckRefusalReason`. */
|
|
153
|
+
export declare const CHECK_REFUSAL_REASONS: readonly ["not_found", "signature_invalid", "revoked", "expired", "secret_mismatch", "rate_limited"];
|
|
71
154
|
export type CheckResult<TGrant> = {
|
|
72
155
|
readonly ok: true;
|
|
73
156
|
readonly id: string;
|
|
74
157
|
readonly issuedById: string | null;
|
|
75
158
|
readonly grants: readonly TGrant[];
|
|
76
159
|
readonly environment: KeyEnvironment;
|
|
160
|
+
/** `null` on a row older than the purpose field. */
|
|
161
|
+
readonly purpose: CredentialPurpose | null;
|
|
162
|
+
readonly organisationId: string | null;
|
|
77
163
|
} | {
|
|
78
164
|
readonly ok: false;
|
|
79
165
|
readonly reason: CheckRefusalReason;
|
|
80
166
|
/** Only meaningful with `reason: 'rate_limited'`. */
|
|
81
167
|
readonly retryAfterMs?: number;
|
|
82
168
|
};
|
|
83
|
-
export type RotateRefusalReason = 'signature_invalid' | 'conflict' | 'not_found' | 'revoked' | 'minter_not_found' | 'minter_revoked' | 'minter_expired'
|
|
169
|
+
export type RotateRefusalReason = 'signature_invalid' | 'conflict' | 'not_found' | 'revoked' | 'minter_not_found' | 'minter_revoked' | 'minter_expired'
|
|
170
|
+
/** The Worker's signature does not verify against `verifyKeys`; the old secret still works. */
|
|
171
|
+
| 'bad_worker_signature' | WorkerErrorCode;
|
|
172
|
+
/** Every member of `RotateRefusalReason`. */
|
|
173
|
+
export declare const ROTATE_REFUSAL_REASONS: readonly ["signature_invalid", "conflict", "not_found", "revoked", "minter_not_found", "minter_revoked", "minter_expired", "bad_worker_signature", "credential_invalid", "credential_expired", "credential_revoked", "grant_missing", "rate_limited", "bad_request", "aad_mismatch", "unknown_generation", "bad_minter_signature", "minter_expired", "minter_revoked", "minter_exceeded", "lineage_mismatch", "bad_old_signature", "unlogged_row", "not_in_lineage", "unreachable"];
|
|
84
174
|
export type RotateResult = {
|
|
85
175
|
readonly ok: true;
|
|
86
176
|
readonly secret: string;
|
|
87
177
|
} | {
|
|
88
178
|
readonly ok: false;
|
|
89
179
|
readonly reason: RotateRefusalReason;
|
|
180
|
+
/** Only with `reason: 'rate_limited'`, when the Worker said how long to wait. */
|
|
181
|
+
readonly retryAfterMs?: number;
|
|
90
182
|
};
|
|
91
183
|
export interface CredentialLink<TGrant> {
|
|
92
184
|
readonly id: string;
|
|
@@ -95,12 +187,38 @@ export interface CredentialLink<TGrant> {
|
|
|
95
187
|
readonly expiresAt: number;
|
|
96
188
|
readonly revokedAt: number | null;
|
|
97
189
|
readonly environment: KeyEnvironment;
|
|
190
|
+
readonly purpose: CredentialPurpose | null;
|
|
191
|
+
readonly organisationId: string | null;
|
|
98
192
|
}
|
|
193
|
+
/** Every reason `classify` refuses. Adding one is a major change after 1.0.0. */
|
|
194
|
+
export declare const CLASSIFY_REFUSAL_REASONS: readonly ["bad_request", "not_found", "already_classified"];
|
|
195
|
+
export type ClassifyResult = {
|
|
196
|
+
readonly ok: true;
|
|
197
|
+
} | {
|
|
198
|
+
readonly ok: false;
|
|
199
|
+
readonly reason: (typeof CLASSIFY_REFUSAL_REASONS)[number];
|
|
200
|
+
};
|
|
201
|
+
/** Every reason `revokeForCompany` refuses. Adding one is a major change after 1.0.0. */
|
|
202
|
+
export declare const REVOKE_FOR_COMPANY_REFUSAL_REASONS: readonly ["protected_credential", "purpose_unknown"];
|
|
203
|
+
export type RevokeForCompanyResult = {
|
|
204
|
+
readonly ok: true;
|
|
205
|
+
readonly revoked: readonly string[];
|
|
206
|
+
} | {
|
|
207
|
+
readonly ok: false;
|
|
208
|
+
readonly reason: (typeof REVOKE_FOR_COMPANY_REFUSAL_REASONS)[number];
|
|
209
|
+
};
|
|
99
210
|
export interface CredentialIssuer<TGrant> {
|
|
100
211
|
/** Calls `worker.signRow`. */
|
|
101
212
|
issue(request: IssueRequest<TGrant>): Promise<IssueResult>;
|
|
102
|
-
/**
|
|
103
|
-
|
|
213
|
+
/**
|
|
214
|
+
* Fully local — never calls `worker`. With `options.organisationId`, a row of
|
|
215
|
+
* any other organisation, or with none, answers exactly as a missing row does,
|
|
216
|
+
* before the signature, the secret, the state or the per-credential limit is
|
|
217
|
+
* looked at.
|
|
218
|
+
*/
|
|
219
|
+
check(secret: string, options?: {
|
|
220
|
+
readonly organisationId?: string;
|
|
221
|
+
}): Promise<CheckResult<TGrant>>;
|
|
104
222
|
/** Calls `worker.signRow`. */
|
|
105
223
|
rotate(id: string, options: {
|
|
106
224
|
readonly graceMs: number;
|
|
@@ -111,5 +229,28 @@ export interface CredentialIssuer<TGrant> {
|
|
|
111
229
|
}): Promise<readonly string[]>;
|
|
112
230
|
/** The chain from `id` to its root, `id` first. Fully local. */
|
|
113
231
|
lineageOf(id: string): Promise<readonly CredentialLink<TGrant>[]>;
|
|
232
|
+
/**
|
|
233
|
+
* What company key management calls. Revokes `id` and, by default, its
|
|
234
|
+
* descendants, under the lifecycle lock. Refuses, and changes nothing, when
|
|
235
|
+
* the target or any live row the cascade would reach is `company_root`,
|
|
236
|
+
* `platform_master` or of unknown purpose. With `organisationId`, it also
|
|
237
|
+
* refuses (`protected_credential`) when any such live row belongs to another
|
|
238
|
+
* organisation or to none. Fully local.
|
|
239
|
+
*/
|
|
240
|
+
revokeForCompany(id: string, options?: {
|
|
241
|
+
readonly cascade?: boolean;
|
|
242
|
+
readonly organisationId?: string;
|
|
243
|
+
}): Promise<RevokeForCompanyResult>;
|
|
244
|
+
/**
|
|
245
|
+
* For operator code: gives a row of unknown purpose its purpose, once. A row
|
|
246
|
+
* that already has one is refused. The Worker is not called and no
|
|
247
|
+
* signature changes.
|
|
248
|
+
*/
|
|
249
|
+
classify(id: string, classification: {
|
|
250
|
+
readonly purpose: CredentialPurpose;
|
|
251
|
+
readonly organisationId?: string;
|
|
252
|
+
}): Promise<ClassifyResult>;
|
|
114
253
|
}
|
|
254
|
+
/** The rate-limit id every `not_found` key is charged to. With an organisation, the id is `<this>:<organisation>`. */
|
|
255
|
+
export declare const UNKNOWN_KEY_BUCKET = "unknown-key";
|
|
115
256
|
export declare function createCredentialIssuer<TGrant>(options: CreateCredentialIssuerOptions<TGrant>): CredentialIssuer<TGrant>;
|