@emilia-protocol/gate 0.14.0 → 0.15.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 +60 -1
- package/README.md +32 -1
- package/aeb-consumption-store.js +3 -0
- package/dist/aeb-consumption-store.d.ts +88 -0
- package/dist/aeb-consumption-store.d.ts.map +1 -0
- package/dist/aeb-consumption-store.js +471 -0
- package/dist/aeb-consumption-store.js.map +1 -0
- package/dist/capability-receipt.d.ts +8 -0
- package/dist/capability-receipt.d.ts.map +1 -1
- package/dist/capability-receipt.js +17 -0
- package/dist/capability-receipt.js.map +1 -1
- package/dist/index.d.ts +13 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +16 -4
- package/dist/index.js.map +1 -1
- package/dist/proposal-to-effect-postgres.d.ts +144 -0
- package/dist/proposal-to-effect-postgres.d.ts.map +1 -0
- package/dist/proposal-to-effect-postgres.js +1578 -0
- package/dist/proposal-to-effect-postgres.js.map +1 -0
- package/dist/proposal-to-effect-status-head-store.d.ts +72 -0
- package/dist/proposal-to-effect-status-head-store.d.ts.map +1 -0
- package/dist/proposal-to-effect-status-head-store.js +355 -0
- package/dist/proposal-to-effect-status-head-store.js.map +1 -0
- package/dist/proposal-to-effect-status.d.ts +55 -0
- package/dist/proposal-to-effect-status.d.ts.map +1 -0
- package/dist/proposal-to-effect-status.js +294 -0
- package/dist/proposal-to-effect-status.js.map +1 -0
- package/dist/proposal-to-effect.d.ts +151 -3
- package/dist/proposal-to-effect.d.ts.map +1 -1
- package/dist/proposal-to-effect.js +589 -46
- package/dist/proposal-to-effect.js.map +1 -1
- package/dist/remedy-case-set-postgres.d.ts +73 -0
- package/dist/remedy-case-set-postgres.d.ts.map +1 -0
- package/dist/remedy-case-set-postgres.js +846 -0
- package/dist/remedy-case-set-postgres.js.map +1 -0
- package/dist/remedy-case-set.d.ts +53 -0
- package/dist/remedy-case-set.d.ts.map +1 -0
- package/dist/remedy-case-set.js +571 -0
- package/dist/remedy-case-set.js.map +1 -0
- package/dist/remedy-program-adapters.d.ts +137 -0
- package/dist/remedy-program-adapters.d.ts.map +1 -0
- package/dist/remedy-program-adapters.js +590 -0
- package/dist/remedy-program-adapters.js.map +1 -0
- package/dist/trust-program-revocation.js.map +1 -1
- package/package.json +283 -63
- package/proposal-to-effect-postgres.js +3 -0
- package/proposal-to-effect-status-head-store.js +2 -0
- package/proposal-to-effect-status.js +2 -0
- package/remedy-case-set-postgres.js +3 -0
- package/remedy-case-set.js +3 -0
- package/remedy-program-adapters.js +3 -0
- package/src/aeb-consumption-store.ts +568 -0
- package/src/capability-receipt.ts +17 -0
- package/src/index.ts +66 -3
- package/src/proposal-to-effect-postgres.ts +1833 -0
- package/src/proposal-to-effect-status-head-store.ts +449 -0
- package/src/proposal-to-effect-status.ts +388 -0
- package/src/proposal-to-effect.ts +755 -50
- package/src/remedy-case-set-postgres.ts +1008 -0
- package/src/remedy-case-set.ts +603 -0
- package/src/remedy-program-adapters.ts +657 -0
- package/src/trust-program-revocation.ts +1 -1
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
/**
|
|
3
|
+
* Durable relying-party custody for accepted EP-STATUS-v1 heads.
|
|
4
|
+
*
|
|
5
|
+
* The presenter supplies only a candidate status artifact. The store loads the
|
|
6
|
+
* authenticated predecessor, passes that predecessor to trusted verification
|
|
7
|
+
* code, and advances the exact tenant/relying-party/target head only when one
|
|
8
|
+
* database-side compare-and-advance still observes that predecessor. This
|
|
9
|
+
* keeps cryptographic work outside database transactions without opening a
|
|
10
|
+
* time-of-check/time-of-use acceptance race.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
statusArtifactDigest,
|
|
15
|
+
type StatusTarget,
|
|
16
|
+
type StatusVerification,
|
|
17
|
+
} from '@emilia-protocol/verify/status';
|
|
18
|
+
|
|
19
|
+
export const PROPOSAL_TO_EFFECT_STATUS_HEAD_STORE_VERSION =
|
|
20
|
+
'EP-GATE-PTE-STATUS-HEAD-PG-v1';
|
|
21
|
+
export const PROPOSAL_TO_EFFECT_STATUS_HEAD_TABLE = 'ep_aeb_status_heads';
|
|
22
|
+
|
|
23
|
+
export const PROPOSAL_TO_EFFECT_STATUS_HEAD_SQL = Object.freeze({
|
|
24
|
+
get: `SELECT status_digest, sequence, status_state, previous_status_digest,
|
|
25
|
+
issued_at, next_update, status_json, predecessor_status_json
|
|
26
|
+
FROM ep_aeb_private.get_status_head(
|
|
27
|
+
$1::text, $2::text, $3::text, $4::text, $5::text, $6::text
|
|
28
|
+
)`,
|
|
29
|
+
compareAndAdvance: `SELECT accepted, reason
|
|
30
|
+
FROM ep_aeb_private.compare_and_advance_status_head(
|
|
31
|
+
$1::text, $2::text, $3::text, $4::text, $5::text, $6::text,
|
|
32
|
+
$7::text, $8::text, $9::bigint, $10::text, $11::text,
|
|
33
|
+
$12::timestamptz, $13::timestamptz, $14::text
|
|
34
|
+
)`,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
38
|
+
|
|
39
|
+
type QueryResult = {
|
|
40
|
+
rowCount: number | null;
|
|
41
|
+
rows?: Record<string, unknown>[];
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export type ProposalToEffectStatusHeadPgClient = {
|
|
45
|
+
query: (text: string, params?: any[]) => Promise<QueryResult>;
|
|
46
|
+
release: () => void;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export type ProposalToEffectStatusHeadPgPool = {
|
|
50
|
+
connect: () => Promise<ProposalToEffectStatusHeadPgClient>;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export interface ProposalToEffectStatusHeadAcceptance {
|
|
54
|
+
accepted: boolean;
|
|
55
|
+
source: 'advanced' | 'existing' | null;
|
|
56
|
+
reason: string | null;
|
|
57
|
+
verification: StatusVerification;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface ProposalToEffectStatusHeadAcceptanceInput {
|
|
61
|
+
target: Readonly<StatusTarget>;
|
|
62
|
+
status: unknown;
|
|
63
|
+
/**
|
|
64
|
+
* Trusted verification callback. Its argument is loaded from durable
|
|
65
|
+
* relying-party custody; no presenter-provided predecessor is accepted.
|
|
66
|
+
*/
|
|
67
|
+
verify(previousStatus: unknown | undefined): MaybePromise<StatusVerification>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface ProposalToEffectStatusHeadStore {
|
|
71
|
+
durable: true;
|
|
72
|
+
readonly tenantId: string;
|
|
73
|
+
readonly relyingPartyId: string;
|
|
74
|
+
accept(
|
|
75
|
+
input: ProposalToEffectStatusHeadAcceptanceInput,
|
|
76
|
+
): Promise<ProposalToEffectStatusHeadAcceptance>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface PostgresProposalToEffectStatusHeadStoreOptions {
|
|
80
|
+
/** Pool authenticated as a tenant-bound member of ep_aeb_executor. */
|
|
81
|
+
pool?: ProposalToEffectStatusHeadPgPool;
|
|
82
|
+
tenantId?: string;
|
|
83
|
+
relyingPartyId?: string;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
interface StoredHead {
|
|
87
|
+
statusDigest: string;
|
|
88
|
+
sequence: number;
|
|
89
|
+
statusState: 'not_revoked' | 'revoked';
|
|
90
|
+
previousStatusDigest: string | null;
|
|
91
|
+
issuedAt: string;
|
|
92
|
+
nextUpdate: string | null;
|
|
93
|
+
status: unknown;
|
|
94
|
+
predecessorStatus: unknown | undefined;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const DIGEST_PATTERN = /^sha256:[0-9a-f]{64}$/;
|
|
98
|
+
const TARGET_TYPES = new Set(['receipt', 'commit', 'delegation']);
|
|
99
|
+
const TARGET_USAGES = new Set(['authorization', 'execution', 'delegation']);
|
|
100
|
+
|
|
101
|
+
function dataRecord(value: unknown): value is Record<string, unknown> {
|
|
102
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) return false;
|
|
103
|
+
const prototype = Object.getPrototypeOf(value);
|
|
104
|
+
if (prototype !== Object.prototype && prototype !== null) return false;
|
|
105
|
+
return Reflect.ownKeys(value).every((key) => {
|
|
106
|
+
if (typeof key !== 'string') return false;
|
|
107
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
108
|
+
return Boolean(descriptor?.enumerable && Object.hasOwn(descriptor, 'value'));
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function denseDataArray(value: unknown): value is unknown[] {
|
|
113
|
+
if (!Array.isArray(value) || Object.getPrototypeOf(value) !== Array.prototype) return false;
|
|
114
|
+
const keys = Reflect.ownKeys(value);
|
|
115
|
+
if (keys.length !== value.length + 1 || !keys.includes('length')) return false;
|
|
116
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
117
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, String(index));
|
|
118
|
+
if (!descriptor?.enumerable || !Object.hasOwn(descriptor, 'value')) return false;
|
|
119
|
+
}
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function snapshotJson(value: unknown, seen = new WeakSet<object>()): unknown {
|
|
124
|
+
if (value === null || typeof value === 'string' || typeof value === 'boolean') return value;
|
|
125
|
+
if (typeof value === 'number') {
|
|
126
|
+
if (Number.isFinite(value)) return value;
|
|
127
|
+
throw new TypeError('status head contains a non-finite JSON number');
|
|
128
|
+
}
|
|
129
|
+
if (typeof value !== 'object' || seen.has(value)) {
|
|
130
|
+
throw new TypeError('status head is outside the JSON data model');
|
|
131
|
+
}
|
|
132
|
+
seen.add(value);
|
|
133
|
+
if (denseDataArray(value)) {
|
|
134
|
+
return value.map((member) => snapshotJson(member, seen));
|
|
135
|
+
}
|
|
136
|
+
if (!dataRecord(value)) throw new TypeError('status head is not a plain data object');
|
|
137
|
+
const snapshot: Record<string, unknown> = {};
|
|
138
|
+
for (const key of Object.keys(value)) snapshot[key] = snapshotJson(value[key], seen);
|
|
139
|
+
return snapshot;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function assertText(
|
|
143
|
+
value: unknown,
|
|
144
|
+
label: string,
|
|
145
|
+
maximumBytes: number,
|
|
146
|
+
): asserts value is string {
|
|
147
|
+
if (typeof value !== 'string'
|
|
148
|
+
|| Buffer.byteLength(value, 'utf8') < 1
|
|
149
|
+
|| Buffer.byteLength(value, 'utf8') > maximumBytes
|
|
150
|
+
|| /[\u0000-\u001f\u007f]/.test(value)) {
|
|
151
|
+
throw new TypeError(`proposal-to-effect status head ${label} is invalid`);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function digest(value: unknown): value is string {
|
|
156
|
+
return typeof value === 'string' && DIGEST_PATTERN.test(value);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function normalizedInstant(value: unknown, nullable = false): string | null {
|
|
160
|
+
if (nullable && value === null) return null;
|
|
161
|
+
if (typeof value !== 'string') return null;
|
|
162
|
+
const milliseconds = Date.parse(value);
|
|
163
|
+
if (!Number.isFinite(milliseconds)) return null;
|
|
164
|
+
return new Date(milliseconds).toISOString();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function safeInteger(value: unknown): number | null {
|
|
168
|
+
const parsed = typeof value === 'string' && /^[0-9]+$/.test(value)
|
|
169
|
+
? Number(value) : value;
|
|
170
|
+
return Number.isSafeInteger(parsed) && (parsed as number) >= 0
|
|
171
|
+
? parsed as number : null;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function targetSnapshot(value: unknown): Readonly<StatusTarget> {
|
|
175
|
+
if (!dataRecord(value)
|
|
176
|
+
|| Reflect.ownKeys(value).length !== 4
|
|
177
|
+
|| !Object.hasOwn(value, 'type')
|
|
178
|
+
|| !Object.hasOwn(value, 'id')
|
|
179
|
+
|| !Object.hasOwn(value, 'digest')
|
|
180
|
+
|| !Object.hasOwn(value, 'usage')
|
|
181
|
+
|| !TARGET_TYPES.has(value.type as string)
|
|
182
|
+
|| !TARGET_USAGES.has(value.usage as string)
|
|
183
|
+
|| typeof value.id !== 'string'
|
|
184
|
+
|| Buffer.byteLength(value.id, 'utf8') < 1
|
|
185
|
+
|| Buffer.byteLength(value.id, 'utf8') > 512
|
|
186
|
+
|| /[\u0000-\u001f\u007f]/.test(value.id)
|
|
187
|
+
|| !digest(value.digest)) {
|
|
188
|
+
throw new TypeError('proposal-to-effect status head target is invalid');
|
|
189
|
+
}
|
|
190
|
+
return Object.freeze({
|
|
191
|
+
type: value.type as StatusTarget['type'],
|
|
192
|
+
id: value.id,
|
|
193
|
+
digest: value.digest,
|
|
194
|
+
usage: value.usage as StatusTarget['usage'],
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function exactRowCount(result: QueryResult, operation: string): number {
|
|
199
|
+
if (!result || !Number.isSafeInteger(result.rowCount) || (result.rowCount as number) < 0) {
|
|
200
|
+
throw new Error(`${operation}: malformed PostgreSQL result`);
|
|
201
|
+
}
|
|
202
|
+
return result.rowCount as number;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function parseJsonText(value: unknown, label: string): unknown {
|
|
206
|
+
if (typeof value !== 'string'
|
|
207
|
+
|| Buffer.byteLength(value, 'utf8') < 2
|
|
208
|
+
|| Buffer.byteLength(value, 'utf8') > 1_048_576) {
|
|
209
|
+
throw new Error(`malformed PostgreSQL status head: ${label}`);
|
|
210
|
+
}
|
|
211
|
+
try {
|
|
212
|
+
return snapshotJson(JSON.parse(value));
|
|
213
|
+
} catch {
|
|
214
|
+
throw new Error(`malformed PostgreSQL status head: ${label}`);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function storedHead(result: QueryResult): StoredHead | null {
|
|
219
|
+
const rows = exactRowCount(result, 'load status head');
|
|
220
|
+
if (rows === 0 && (!result.rows || result.rows.length === 0)) return null;
|
|
221
|
+
if (rows !== 1 || result.rows?.length !== 1 || !dataRecord(result.rows[0])) {
|
|
222
|
+
throw new Error('malformed PostgreSQL status head: row cardinality');
|
|
223
|
+
}
|
|
224
|
+
const row = result.rows[0];
|
|
225
|
+
const sequence = safeInteger(row.sequence);
|
|
226
|
+
const issuedAt = normalizedInstant(row.issued_at);
|
|
227
|
+
const nextUpdate = normalizedInstant(row.next_update, true);
|
|
228
|
+
const statusState = row.status_state;
|
|
229
|
+
const previousStatusDigest = row.previous_status_digest;
|
|
230
|
+
if (!digest(row.status_digest)
|
|
231
|
+
|| sequence === null
|
|
232
|
+
|| (statusState !== 'not_revoked' && statusState !== 'revoked')
|
|
233
|
+
|| (previousStatusDigest !== null && !digest(previousStatusDigest))
|
|
234
|
+
|| !issuedAt
|
|
235
|
+
|| (statusState === 'not_revoked' && !nextUpdate)
|
|
236
|
+
|| (statusState === 'revoked' && nextUpdate !== null)) {
|
|
237
|
+
throw new Error('malformed PostgreSQL status head: metadata');
|
|
238
|
+
}
|
|
239
|
+
const status = parseJsonText(row.status_json, 'status_json');
|
|
240
|
+
const predecessorStatus = row.predecessor_status_json === null
|
|
241
|
+
? undefined : parseJsonText(row.predecessor_status_json, 'predecessor_status_json');
|
|
242
|
+
if (statusArtifactDigest(status) !== row.status_digest
|
|
243
|
+
|| (sequence === 0
|
|
244
|
+
&& (previousStatusDigest !== null || predecessorStatus !== undefined))
|
|
245
|
+
|| (sequence > 0
|
|
246
|
+
&& (previousStatusDigest === null
|
|
247
|
+
|| predecessorStatus === undefined
|
|
248
|
+
|| statusArtifactDigest(predecessorStatus) !== previousStatusDigest))) {
|
|
249
|
+
throw new Error('malformed PostgreSQL status head: chain');
|
|
250
|
+
}
|
|
251
|
+
return {
|
|
252
|
+
statusDigest: row.status_digest,
|
|
253
|
+
sequence,
|
|
254
|
+
statusState,
|
|
255
|
+
previousStatusDigest,
|
|
256
|
+
issuedAt,
|
|
257
|
+
nextUpdate,
|
|
258
|
+
status,
|
|
259
|
+
predecessorStatus,
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function candidateMetadata(
|
|
264
|
+
status: unknown,
|
|
265
|
+
verification: StatusVerification,
|
|
266
|
+
) {
|
|
267
|
+
if (!dataRecord(status)
|
|
268
|
+
|| !digest(verification.status_digest)
|
|
269
|
+
|| !Number.isSafeInteger(verification.sequence)
|
|
270
|
+
|| verification.sequence === null
|
|
271
|
+
|| verification.sequence < 0
|
|
272
|
+
|| verification.status_digest !== statusArtifactDigest(status)
|
|
273
|
+
|| status.sequence !== verification.sequence
|
|
274
|
+
|| (status.status !== 'not_revoked' && status.status !== 'revoked')
|
|
275
|
+
|| (status.previous_status_digest !== null
|
|
276
|
+
&& !digest(status.previous_status_digest))) {
|
|
277
|
+
throw new Error('verified status head metadata is inconsistent');
|
|
278
|
+
}
|
|
279
|
+
const issuedAt = normalizedInstant(status.issued_at);
|
|
280
|
+
const nextUpdate = normalizedInstant(status.next_update, true);
|
|
281
|
+
const verificationNextUpdate = normalizedInstant(verification.next_update, true);
|
|
282
|
+
if (!issuedAt
|
|
283
|
+
|| nextUpdate !== verificationNextUpdate
|
|
284
|
+
|| (status.status === 'not_revoked' && !nextUpdate)
|
|
285
|
+
|| (status.status === 'revoked' && nextUpdate !== null)) {
|
|
286
|
+
throw new Error('verified status head timing is inconsistent');
|
|
287
|
+
}
|
|
288
|
+
return {
|
|
289
|
+
statusDigest: verification.status_digest,
|
|
290
|
+
sequence: verification.sequence,
|
|
291
|
+
statusState: status.status,
|
|
292
|
+
previousStatusDigest: status.previous_status_digest,
|
|
293
|
+
issuedAt,
|
|
294
|
+
nextUpdate,
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function verificationRefusal(
|
|
299
|
+
verification: StatusVerification,
|
|
300
|
+
): ProposalToEffectStatusHeadAcceptance {
|
|
301
|
+
return {
|
|
302
|
+
accepted: false,
|
|
303
|
+
source: null,
|
|
304
|
+
reason: verification.reasons[0] ?? 'status_verification_failed',
|
|
305
|
+
verification,
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Create a durable accepted-head store. The PostgreSQL principal must be bound
|
|
311
|
+
* to tenantId in ep_aeb_private.tenant_principals and inherit ep_aeb_executor.
|
|
312
|
+
*/
|
|
313
|
+
export function createPostgresProposalToEffectStatusHeadStore({
|
|
314
|
+
pool,
|
|
315
|
+
tenantId,
|
|
316
|
+
relyingPartyId,
|
|
317
|
+
}: PostgresProposalToEffectStatusHeadStoreOptions = {}): ProposalToEffectStatusHeadStore {
|
|
318
|
+
if (!pool || typeof pool.connect !== 'function') {
|
|
319
|
+
throw new TypeError(
|
|
320
|
+
'createPostgresProposalToEffectStatusHeadStore requires an ep_aeb_executor pg pool',
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
assertText(tenantId, 'tenantId', 512);
|
|
324
|
+
assertText(relyingPartyId, 'relyingPartyId', 512);
|
|
325
|
+
|
|
326
|
+
const store: ProposalToEffectStatusHeadStore = {
|
|
327
|
+
durable: true,
|
|
328
|
+
tenantId,
|
|
329
|
+
relyingPartyId,
|
|
330
|
+
|
|
331
|
+
async accept(input): Promise<ProposalToEffectStatusHeadAcceptance> {
|
|
332
|
+
const target = targetSnapshot(input?.target);
|
|
333
|
+
const candidate = snapshotJson(input?.status);
|
|
334
|
+
if (typeof input?.verify !== 'function') {
|
|
335
|
+
throw new TypeError('proposal-to-effect status head verify callback is required');
|
|
336
|
+
}
|
|
337
|
+
const candidateDigest = statusArtifactDigest(candidate);
|
|
338
|
+
if (!digest(candidateDigest)) {
|
|
339
|
+
throw new TypeError('proposal-to-effect status head digest is invalid');
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
const client = await pool.connect();
|
|
343
|
+
if (!client || typeof client.query !== 'function' || typeof client.release !== 'function') {
|
|
344
|
+
throw new TypeError('status head pg pool returned an invalid client');
|
|
345
|
+
}
|
|
346
|
+
try {
|
|
347
|
+
const current = storedHead(await client.query(
|
|
348
|
+
PROPOSAL_TO_EFFECT_STATUS_HEAD_SQL.get,
|
|
349
|
+
[tenantId, relyingPartyId, target.type, target.id, target.digest, target.usage],
|
|
350
|
+
));
|
|
351
|
+
const exactReplay = current?.statusDigest === candidateDigest;
|
|
352
|
+
const statusForVerification = exactReplay ? current.status : candidate;
|
|
353
|
+
const previousForVerification = exactReplay
|
|
354
|
+
? current.predecessorStatus
|
|
355
|
+
: current?.status;
|
|
356
|
+
const verification = await input.verify(previousForVerification);
|
|
357
|
+
if (!dataRecord(verification)
|
|
358
|
+
|| typeof verification.valid !== 'boolean'
|
|
359
|
+
|| !Array.isArray(verification.reasons)) {
|
|
360
|
+
throw new Error('status head verifier returned a malformed result');
|
|
361
|
+
}
|
|
362
|
+
if (!verification.valid || verification.outcome === 'indeterminate') {
|
|
363
|
+
return verificationRefusal(verification);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
const metadata = candidateMetadata(statusForVerification, verification);
|
|
367
|
+
if (exactReplay) {
|
|
368
|
+
if (metadata.statusDigest !== current.statusDigest
|
|
369
|
+
|| metadata.sequence !== current.sequence
|
|
370
|
+
|| metadata.statusState !== current.statusState
|
|
371
|
+
|| metadata.previousStatusDigest !== current.previousStatusDigest
|
|
372
|
+
|| metadata.issuedAt !== current.issuedAt
|
|
373
|
+
|| metadata.nextUpdate !== current.nextUpdate) {
|
|
374
|
+
throw new Error('authenticated status head replay metadata mismatch');
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
if (!exactReplay && ((!current
|
|
379
|
+
&& (metadata.sequence !== 0 || metadata.previousStatusDigest !== null))
|
|
380
|
+
|| (current
|
|
381
|
+
&& (current.statusState === 'revoked'
|
|
382
|
+
|| metadata.sequence !== current.sequence + 1
|
|
383
|
+
|| metadata.previousStatusDigest !== current.statusDigest)))) {
|
|
384
|
+
return {
|
|
385
|
+
accepted: false,
|
|
386
|
+
source: null,
|
|
387
|
+
reason: 'status_head_conflict',
|
|
388
|
+
verification,
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
const result = await client.query(
|
|
393
|
+
PROPOSAL_TO_EFFECT_STATUS_HEAD_SQL.compareAndAdvance,
|
|
394
|
+
[
|
|
395
|
+
tenantId,
|
|
396
|
+
relyingPartyId,
|
|
397
|
+
target.type,
|
|
398
|
+
target.id,
|
|
399
|
+
target.digest,
|
|
400
|
+
target.usage,
|
|
401
|
+
current?.statusDigest ?? null,
|
|
402
|
+
metadata.statusDigest,
|
|
403
|
+
metadata.sequence,
|
|
404
|
+
metadata.statusState,
|
|
405
|
+
metadata.previousStatusDigest,
|
|
406
|
+
metadata.issuedAt,
|
|
407
|
+
metadata.nextUpdate,
|
|
408
|
+
JSON.stringify(exactReplay ? current.status : candidate),
|
|
409
|
+
],
|
|
410
|
+
);
|
|
411
|
+
const rows = exactRowCount(result, 'advance status head');
|
|
412
|
+
const accepted = result.rows?.[0]?.accepted;
|
|
413
|
+
const reason = result.rows?.[0]?.reason;
|
|
414
|
+
if (rows !== 1
|
|
415
|
+
|| result.rows?.length !== 1
|
|
416
|
+
|| typeof accepted !== 'boolean'
|
|
417
|
+
|| (reason !== null && typeof reason !== 'string')
|
|
418
|
+
|| (accepted && reason !== null)) {
|
|
419
|
+
throw new Error('advance status head: malformed PostgreSQL result');
|
|
420
|
+
}
|
|
421
|
+
if (!accepted) {
|
|
422
|
+
return {
|
|
423
|
+
accepted: false,
|
|
424
|
+
source: null,
|
|
425
|
+
reason: 'status_head_conflict',
|
|
426
|
+
verification,
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
return {
|
|
430
|
+
accepted: true,
|
|
431
|
+
source: exactReplay ? 'existing' : 'advanced',
|
|
432
|
+
reason: null,
|
|
433
|
+
verification,
|
|
434
|
+
};
|
|
435
|
+
} finally {
|
|
436
|
+
client.release();
|
|
437
|
+
}
|
|
438
|
+
},
|
|
439
|
+
};
|
|
440
|
+
|
|
441
|
+
return Object.freeze(store);
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
export default {
|
|
445
|
+
PROPOSAL_TO_EFFECT_STATUS_HEAD_STORE_VERSION,
|
|
446
|
+
PROPOSAL_TO_EFFECT_STATUS_HEAD_TABLE,
|
|
447
|
+
PROPOSAL_TO_EFFECT_STATUS_HEAD_SQL,
|
|
448
|
+
createPostgresProposalToEffectStatusHeadStore,
|
|
449
|
+
};
|