@yielded/auth-persistence 0.1.0-beta.10
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 +27 -0
- package/dist/Adapter.d.mts +30 -0
- package/dist/Adapter.mjs +25 -0
- package/dist/index.d.mts +7 -0
- package/dist/index.mjs +7 -0
- package/dist/internal/PasswordPreparedPostconditions.d.mts +22 -0
- package/dist/internal/PasswordPreparedPostconditions.mjs +12 -0
- package/dist/internal/PasswordPreparedPostconditions.mjs.map +1 -0
- package/dist/internal/configuration.d.mts +174 -0
- package/dist/internal/configuration.mjs +7 -0
- package/dist/internal/configuration.mjs.map +1 -0
- package/dist/internal/email-kernel.d.mts +275 -0
- package/dist/internal/email-kernel.mjs +487 -0
- package/dist/internal/email-kernel.mjs.map +1 -0
- package/dist/internal/mapping-error.d.mts +10 -0
- package/dist/internal/mapping-error.mjs +30 -0
- package/dist/internal/mapping-error.mjs.map +1 -0
- package/dist/internal/models/common.d.mts +12 -0
- package/dist/internal/models/email-model.d.mts +353 -0
- package/dist/internal/models/email-model.mjs +21 -0
- package/dist/internal/models/email-model.mjs.map +1 -0
- package/dist/internal/models/passkey-model.d.mts +266 -0
- package/dist/internal/models/passkey-model.mjs +34 -0
- package/dist/internal/models/passkey-model.mjs.map +1 -0
- package/dist/internal/models/passkey-registration-ceremony-model.d.mts +49 -0
- package/dist/internal/models/passkey-registration-ceremony-model.mjs +18 -0
- package/dist/internal/models/passkey-registration-ceremony-model.mjs.map +1 -0
- package/dist/internal/models/passkey-write-model.d.mts +189 -0
- package/dist/internal/models/passkey-write-model.mjs +13 -0
- package/dist/internal/models/passkey-write-model.mjs.map +1 -0
- package/dist/internal/models/password-model.d.mts +328 -0
- package/dist/internal/models/password-model.mjs +23 -0
- package/dist/internal/models/password-model.mjs.map +1 -0
- package/dist/internal/models/proof-model.d.mts +285 -0
- package/dist/internal/models/proof-model.mjs +17 -0
- package/dist/internal/models/proof-model.mjs.map +1 -0
- package/dist/internal/models/session-model.d.mts +199 -0
- package/dist/internal/models/session-model.mjs +20 -0
- package/dist/internal/models/session-model.mjs.map +1 -0
- package/dist/internal/models/step-up-model.d.mts +66 -0
- package/dist/internal/models/step-up-model.mjs +9 -0
- package/dist/internal/models/step-up-model.mjs.map +1 -0
- package/dist/internal/oauth-app.d.mts +11 -0
- package/dist/internal/oauth-app.mjs +57 -0
- package/dist/internal/oauth-app.mjs.map +1 -0
- package/dist/internal/oauth-server.d.mts +10 -0
- package/dist/internal/oauth-server.mjs +66 -0
- package/dist/internal/oauth-server.mjs.map +1 -0
- package/dist/internal/passkey/admission.d.mts +6 -0
- package/dist/internal/passkey/admission.mjs +216 -0
- package/dist/internal/passkey/admission.mjs.map +1 -0
- package/dist/internal/passkey/credentials.d.mts +14 -0
- package/dist/internal/passkey/credentials.mjs +249 -0
- package/dist/internal/passkey/credentials.mjs.map +1 -0
- package/dist/internal/passkey/enrollment.mjs +138 -0
- package/dist/internal/passkey/enrollment.mjs.map +1 -0
- package/dist/internal/passkey/flow.d.mts +21 -0
- package/dist/internal/passkey/flow.mjs +452 -0
- package/dist/internal/passkey/flow.mjs.map +1 -0
- package/dist/internal/passkey/management.mjs +231 -0
- package/dist/internal/passkey/management.mjs.map +1 -0
- package/dist/internal/passkey/registration-ceremony.d.mts +9 -0
- package/dist/internal/passkey/registration-ceremony.mjs +129 -0
- package/dist/internal/passkey/registration-ceremony.mjs.map +1 -0
- package/dist/internal/passkey/registration-custody.d.mts +2 -0
- package/dist/internal/passkey/registration-custody.mjs +52 -0
- package/dist/internal/passkey/registration-custody.mjs.map +1 -0
- package/dist/internal/passkey/registration-write.mjs +211 -0
- package/dist/internal/passkey/registration-write.mjs.map +1 -0
- package/dist/internal/passkey/state.d.mts +10 -0
- package/dist/internal/passkey/state.mjs +234 -0
- package/dist/internal/passkey/state.mjs.map +1 -0
- package/dist/internal/passkey/target.d.mts +19 -0
- package/dist/internal/passkey/target.mjs +190 -0
- package/dist/internal/passkey/target.mjs.map +1 -0
- package/dist/internal/passkey/write-state.d.mts +17 -0
- package/dist/internal/passkey/write-state.mjs +345 -0
- package/dist/internal/passkey/write-state.mjs.map +1 -0
- package/dist/internal/passkey/write-target.mjs +192 -0
- package/dist/internal/passkey/write-target.mjs.map +1 -0
- package/dist/internal/passkey-kernel.d.mts +5592 -0
- package/dist/internal/passkey-kernel.mjs +49 -0
- package/dist/internal/passkey-kernel.mjs.map +1 -0
- package/dist/internal/passkeys.d.mts +7 -0
- package/dist/internal/passkeys.mjs +368 -0
- package/dist/internal/passkeys.mjs.map +1 -0
- package/dist/internal/password-kernel.d.mts +290 -0
- package/dist/internal/password-kernel.mjs +704 -0
- package/dist/internal/password-kernel.mjs.map +1 -0
- package/dist/internal/persistence.d.mts +25 -0
- package/dist/internal/persistence.mjs +291 -0
- package/dist/internal/persistence.mjs.map +1 -0
- package/dist/internal/phone-kernel.d.mts +162 -0
- package/dist/internal/phone-kernel.mjs +434 -0
- package/dist/internal/phone-kernel.mjs.map +1 -0
- package/dist/internal/proof-kernel.d.mts +93 -0
- package/dist/internal/proof-kernel.mjs +619 -0
- package/dist/internal/proof-kernel.mjs.map +1 -0
- package/dist/internal/query-operations.d.mts +61 -0
- package/dist/internal/registration-contract.d.mts +16 -0
- package/dist/internal/registration.mjs +66 -0
- package/dist/internal/registration.mjs.map +1 -0
- package/dist/internal/session-kernel.d.mts +48 -0
- package/dist/internal/session-kernel.mjs +737 -0
- package/dist/internal/session-kernel.mjs.map +1 -0
- package/dist/internal/sql-persistence.d.mts +10 -0
- package/dist/internal/sql-persistence.mjs +37 -0
- package/dist/internal/sql-persistence.mjs.map +1 -0
- package/dist/internal/sql-query.mjs +207 -0
- package/dist/internal/sql-query.mjs.map +1 -0
- package/dist/internal/sql-table.d.mts +36 -0
- package/dist/internal/sql-table.mjs +81 -0
- package/dist/internal/sql-table.mjs.map +1 -0
- package/dist/internal/standalone.d.mts +9 -0
- package/dist/internal/standalone.mjs +13 -0
- package/dist/internal/standalone.mjs.map +1 -0
- package/dist/internal/step-up-state.d.mts +49 -0
- package/dist/internal/step-up-state.mjs +32 -0
- package/dist/internal/step-up-state.mjs.map +1 -0
- package/dist/internal/storage-mapping.mjs +686 -0
- package/dist/internal/storage-mapping.mjs.map +1 -0
- package/dist/internal/storage-tables.d.mts +913 -0
- package/dist/internal/storage-tables.mjs +387 -0
- package/dist/internal/storage-tables.mjs.map +1 -0
- package/dist/internal/storage-validation.mjs +58 -0
- package/dist/internal/storage-validation.mjs.map +1 -0
- package/dist/internal/transaction-execution-kernel.d.mts +38 -0
- package/dist/internal/transaction-execution-kernel.mjs +116 -0
- package/dist/internal/transaction-execution-kernel.mjs.map +1 -0
- package/dist/internal/transaction-kernel.d.mts +77 -0
- package/dist/internal/transaction-kernel.mjs +260 -0
- package/dist/internal/transaction-kernel.mjs.map +1 -0
- package/package.json +1 -0
- package/src/Adapter.ts +291 -0
- package/src/index.ts +6 -0
- package/src/internal/PasswordPreparedPostconditions.ts +30 -0
- package/src/internal/configuration.ts +282 -0
- package/src/internal/email-kernel.ts +1535 -0
- package/src/internal/mapping-error.ts +36 -0
- package/src/internal/models/common.ts +11 -0
- package/src/internal/models/email-model.ts +559 -0
- package/src/internal/models/passkey-model.ts +390 -0
- package/src/internal/models/passkey-registration-ceremony-model.ts +72 -0
- package/src/internal/models/passkey-write-model.ts +297 -0
- package/src/internal/models/password-model.ts +513 -0
- package/src/internal/models/proof-model.ts +424 -0
- package/src/internal/models/session-model.ts +375 -0
- package/src/internal/models/step-up-model.ts +135 -0
- package/src/internal/oauth-app.ts +82 -0
- package/src/internal/oauth-server.ts +89 -0
- package/src/internal/passkey/admission.ts +361 -0
- package/src/internal/passkey/credentials.ts +511 -0
- package/src/internal/passkey/enrollment.ts +326 -0
- package/src/internal/passkey/flow.ts +1062 -0
- package/src/internal/passkey/management.ts +421 -0
- package/src/internal/passkey/registration-ceremony.ts +283 -0
- package/src/internal/passkey/registration-custody.ts +91 -0
- package/src/internal/passkey/registration-write.ts +426 -0
- package/src/internal/passkey/state.ts +353 -0
- package/src/internal/passkey/target.ts +586 -0
- package/src/internal/passkey/write-state.ts +681 -0
- package/src/internal/passkey/write-target.ts +452 -0
- package/src/internal/passkey-kernel.ts +104 -0
- package/src/internal/passkeys.ts +606 -0
- package/src/internal/password-kernel.ts +2124 -0
- package/src/internal/persistence.ts +588 -0
- package/src/internal/phone-kernel.ts +846 -0
- package/src/internal/proof-kernel.ts +1954 -0
- package/src/internal/query-operations.ts +81 -0
- package/src/internal/registration-contract.ts +22 -0
- package/src/internal/registration.ts +161 -0
- package/src/internal/session-kernel.ts +2217 -0
- package/src/internal/sql-persistence.ts +45 -0
- package/src/internal/sql-query.ts +298 -0
- package/src/internal/sql-table.ts +104 -0
- package/src/internal/standalone.ts +30 -0
- package/src/internal/step-up-state.ts +139 -0
- package/src/internal/storage-mapping.ts +708 -0
- package/src/internal/storage-tables.ts +408 -0
- package/src/internal/storage-validation.ts +117 -0
- package/src/internal/transaction-execution-kernel.ts +313 -0
- package/src/internal/transaction-kernel.ts +593 -0
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
/* oxlint-disable no-explicit-any -- existing mapped-row bridge; public adapters preserve table, ID, and Effect types. */
|
|
2
|
+
|
|
3
|
+
import type { PasskeyCeremony, PasskeyMethodPolicy } from "@yielded/auth/Passkey";
|
|
4
|
+
import { Effect } from "effect";
|
|
5
|
+
|
|
6
|
+
import type { PasskeyChargeKind } from "../models/passkey-model";
|
|
7
|
+
import type { SqlExpression as SQL, QueryOperations } from "../query-operations";
|
|
8
|
+
import type { makeTransactionKernel } from "../transaction-kernel";
|
|
9
|
+
import { CurrentPasskeyTransaction } from "./state";
|
|
10
|
+
import type { makePasskeyStateKernel } from "./state";
|
|
11
|
+
|
|
12
|
+
export const makePasskeyAdmissionKernel = (
|
|
13
|
+
operations: QueryOperations,
|
|
14
|
+
state: Pick<
|
|
15
|
+
ReturnType<typeof makePasskeyStateKernel>,
|
|
16
|
+
| "col"
|
|
17
|
+
| "equal"
|
|
18
|
+
| "existsExact"
|
|
19
|
+
| "invariant"
|
|
20
|
+
| "key"
|
|
21
|
+
| "mappedColumns"
|
|
22
|
+
| "subjectScope"
|
|
23
|
+
| "targetScope"
|
|
24
|
+
>,
|
|
25
|
+
transactions: Pick<ReturnType<typeof makeTransactionKernel>, "both">,
|
|
26
|
+
) => {
|
|
27
|
+
const { sql } = operations;
|
|
28
|
+
const { col, equal, existsExact, key, mappedColumns, subjectScope, targetScope } = state;
|
|
29
|
+
const invariant: (value: unknown) => asserts value = state.invariant;
|
|
30
|
+
const { both } = transactions;
|
|
31
|
+
|
|
32
|
+
const chargeRetentionMillis = 86_400_000;
|
|
33
|
+
|
|
34
|
+
const chargeScopes = (ceremony: PasskeyCeremony, subjectId?: string) => {
|
|
35
|
+
const scopes: { kind: PasskeyChargeKind; scope: string }[] = [
|
|
36
|
+
{ kind: "global", scope: key("global", [ceremony.moduleId]) },
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
if (subjectId !== undefined) scopes.push({ kind: "subject", scope: subjectScope(subjectId) });
|
|
40
|
+
const target = targetScope(ceremony);
|
|
41
|
+
|
|
42
|
+
if (target !== null) scopes.push({ kind: "target", scope: target });
|
|
43
|
+
|
|
44
|
+
return scopes;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const lockAdmission = Effect.fn("passkey.lockAdmission")(function* (mapping: any) {
|
|
48
|
+
const owner = yield* CurrentPasskeyTransaction;
|
|
49
|
+
const table = mapping.admission;
|
|
50
|
+
|
|
51
|
+
const identity = {
|
|
52
|
+
[table.authorityScope]: mapping.authorityScope,
|
|
53
|
+
[table.moduleId]: mapping.moduleId,
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// The previous marker/time is telemetry, not an authentication CAS. Actual
|
|
57
|
+
// engine predicates serialize admission, including separately planned D1 batches.
|
|
58
|
+
const found = yield* owner.read(table.table, equal(table.table, identity), {
|
|
59
|
+
limit: 1,
|
|
60
|
+
observe: false,
|
|
61
|
+
columns: [table.authorityScope, table.moduleId],
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
invariant(found.rows.length === 1);
|
|
65
|
+
invariant(
|
|
66
|
+
found.rows[0]![table.authorityScope] === mapping.authorityScope &&
|
|
67
|
+
found.rows[0]![table.moduleId] === mapping.moduleId,
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
const marked = {
|
|
71
|
+
...identity,
|
|
72
|
+
[table.version]: owner.marker,
|
|
73
|
+
[table.ownerMarker]: owner.marker,
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
yield* owner.updateGuarded(
|
|
77
|
+
table.table,
|
|
78
|
+
owner.exact(table.table, identity),
|
|
79
|
+
{
|
|
80
|
+
[table.version]: owner.marker,
|
|
81
|
+
[table.ownerMarker]: owner.marker,
|
|
82
|
+
[table.admittedAt]: null,
|
|
83
|
+
},
|
|
84
|
+
{ rows: 1, postcondition: yield* existsExact(table.table, marked) },
|
|
85
|
+
);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
const pendingCount = (mapping: any, scope?: string) => {
|
|
89
|
+
const table = mapping.flow;
|
|
90
|
+
|
|
91
|
+
return sql`(select count(*) from ${table.table} where ${both(
|
|
92
|
+
equal(table.table, { [table.moduleId]: mapping.moduleId }),
|
|
93
|
+
sql`${col(table.table, table.state)} in (${table.states.Pending}, ${table.states.Claimed})`,
|
|
94
|
+
scope === undefined ? undefined : equal(table.table, { [table.subjectScope]: scope }),
|
|
95
|
+
)})`;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const chargedCount = (mapping: any, kind: PasskeyChargeKind, scope: string, window: number) => {
|
|
99
|
+
const table = mapping.charge;
|
|
100
|
+
const clock = mapping.clock;
|
|
101
|
+
|
|
102
|
+
return sql`(select count(*) from ${table.table} where ${both(
|
|
103
|
+
equal(table.table, { [table.moduleId]: mapping.moduleId, [table.kind]: kind }),
|
|
104
|
+
kind === "global" ? undefined : equal(table.table, { [table.scope]: scope }),
|
|
105
|
+
sql`${col(table.table, table.admittedAt)} is not null`,
|
|
106
|
+
sql`${clock.toMillis(sql`${col(table.table, table.admittedAt)}`)} > ${clock.engineNowMillis} - ${window}`,
|
|
107
|
+
)})`;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const admissionCondition = (
|
|
111
|
+
mapping: any,
|
|
112
|
+
policies: ReadonlyArray<PasskeyMethodPolicy>,
|
|
113
|
+
ceremony: PasskeyCeremony,
|
|
114
|
+
subjectId: string | undefined,
|
|
115
|
+
added: ReadonlyArray<PasskeyChargeKind>,
|
|
116
|
+
newFlow: boolean,
|
|
117
|
+
resolvedSubject: boolean,
|
|
118
|
+
) =>
|
|
119
|
+
both(
|
|
120
|
+
...policies.flatMap((policy) => [
|
|
121
|
+
sql`${pendingCount(mapping)} + ${newFlow ? 1 : 0} <= ${policy.maximumPending}`,
|
|
122
|
+
subjectId === undefined
|
|
123
|
+
? undefined
|
|
124
|
+
: sql`${pendingCount(mapping, subjectScope(subjectId))} + ${newFlow || resolvedSubject ? 1 : 0} <= ${policy.maximumPendingPerSubject}`,
|
|
125
|
+
...chargeScopes(ceremony, subjectId).map(
|
|
126
|
+
({ kind, scope }) =>
|
|
127
|
+
sql`${chargedCount(mapping, kind, scope, policy.admission[kind].windowMillis)} + ${added.includes(kind) ? 1 : 0} <= ${policy.admission[kind].limit}`,
|
|
128
|
+
),
|
|
129
|
+
]),
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
const guardAdmission = (
|
|
133
|
+
mapping: any,
|
|
134
|
+
policies: ReadonlyArray<PasskeyMethodPolicy>,
|
|
135
|
+
ceremony: PasskeyCeremony,
|
|
136
|
+
subjectId?: string,
|
|
137
|
+
) =>
|
|
138
|
+
Effect.map(CurrentPasskeyTransaction, (owner) => {
|
|
139
|
+
owner.postconditions.push(
|
|
140
|
+
admissionCondition(mapping, policies, ceremony, subjectId, [], false, false),
|
|
141
|
+
);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
const readCharges = Effect.fn("passkey.readCharges")(function* (
|
|
145
|
+
mapping: any,
|
|
146
|
+
ceremony: PasskeyCeremony,
|
|
147
|
+
policy: PasskeyMethodPolicy,
|
|
148
|
+
subjectId?: string,
|
|
149
|
+
) {
|
|
150
|
+
const owner = yield* CurrentPasskeyTransaction;
|
|
151
|
+
const table = mapping.charge;
|
|
152
|
+
|
|
153
|
+
const where = equal(table.table, {
|
|
154
|
+
[table.moduleId]: ceremony.moduleId,
|
|
155
|
+
[table.flowId]: ceremony.flowId,
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
const found = yield* owner.read(table.table, where, {
|
|
159
|
+
limit: 3,
|
|
160
|
+
observe: false,
|
|
161
|
+
columns: mappedColumns(table),
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
const scopes = chargeScopes(ceremony, subjectId);
|
|
165
|
+
|
|
166
|
+
invariant(found.rows.length === scopes.length);
|
|
167
|
+
for (const { kind, scope } of scopes) {
|
|
168
|
+
const rows = found.rows.filter((row) => row[table.kind] === kind);
|
|
169
|
+
|
|
170
|
+
invariant(rows.length === 1);
|
|
171
|
+
const row = rows[0]!;
|
|
172
|
+
const admitted = mapping.clock.decodeInstant(row[table.admittedAt]);
|
|
173
|
+
const retained = mapping.clock.decodeInstant(row[table.retainUntil]);
|
|
174
|
+
|
|
175
|
+
invariant(
|
|
176
|
+
Number.isSafeInteger(admitted) &&
|
|
177
|
+
admitted >= 0 &&
|
|
178
|
+
retained === admitted + chargeRetentionMillis,
|
|
179
|
+
);
|
|
180
|
+
invariant(
|
|
181
|
+
row[table.moduleId] === ceremony.moduleId &&
|
|
182
|
+
row[table.flowId] === ceremony.flowId &&
|
|
183
|
+
row[table.purpose] === ceremony.purpose,
|
|
184
|
+
);
|
|
185
|
+
invariant(
|
|
186
|
+
row[table.scope] === scope &&
|
|
187
|
+
row[table.originalWindowMillis] === policy.admission[kind].windowMillis,
|
|
188
|
+
);
|
|
189
|
+
owner.observations.push({
|
|
190
|
+
table: table.table,
|
|
191
|
+
where: equal(table.table, {
|
|
192
|
+
[table.moduleId]: ceremony.moduleId,
|
|
193
|
+
[table.flowId]: ceremony.flowId,
|
|
194
|
+
[table.kind]: kind,
|
|
195
|
+
}),
|
|
196
|
+
rows: [row],
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
const guardChargeSet = (mapping: any, ceremony: PasskeyCeremony, subjectId?: string) =>
|
|
202
|
+
Effect.flatMap(CurrentPasskeyTransaction, (owner) =>
|
|
203
|
+
Effect.gen(function* () {
|
|
204
|
+
const table = mapping.charge;
|
|
205
|
+
const scopes = chargeScopes(ceremony, subjectId);
|
|
206
|
+
|
|
207
|
+
const where = equal(table.table, {
|
|
208
|
+
[table.moduleId]: ceremony.moduleId,
|
|
209
|
+
[table.flowId]: ceremony.flowId,
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
owner.postconditions.push(
|
|
213
|
+
sql`(select count(*) from ${table.table} where ${where}) = ${scopes.length}`,
|
|
214
|
+
);
|
|
215
|
+
for (const { kind, scope } of scopes)
|
|
216
|
+
owner.postconditions.push(
|
|
217
|
+
yield* existsExact(table.table, {
|
|
218
|
+
[table.moduleId]: ceremony.moduleId,
|
|
219
|
+
[table.flowId]: ceremony.flowId,
|
|
220
|
+
[table.purpose]: ceremony.purpose,
|
|
221
|
+
[table.kind]: kind,
|
|
222
|
+
[table.scope]: scope,
|
|
223
|
+
}),
|
|
224
|
+
);
|
|
225
|
+
}),
|
|
226
|
+
);
|
|
227
|
+
|
|
228
|
+
const insertCharges = Effect.fn("passkey.insertCharges")(function* (
|
|
229
|
+
mapping: any,
|
|
230
|
+
ceremony: PasskeyCeremony,
|
|
231
|
+
policy: PasskeyMethodPolicy,
|
|
232
|
+
kinds: ReadonlyArray<PasskeyChargeKind>,
|
|
233
|
+
subjectId?: string,
|
|
234
|
+
) {
|
|
235
|
+
const owner = yield* CurrentPasskeyTransaction;
|
|
236
|
+
|
|
237
|
+
if (kinds.length === 0) return;
|
|
238
|
+
const table = mapping.charge;
|
|
239
|
+
const anchor = mapping.admission;
|
|
240
|
+
const clock = mapping.clock;
|
|
241
|
+
const earliest = yield* owner.now(clock);
|
|
242
|
+
const identities: Record<string, unknown>[] = [];
|
|
243
|
+
|
|
244
|
+
for (const { kind, scope } of chargeScopes(ceremony, subjectId).filter((item) =>
|
|
245
|
+
kinds.includes(item.kind),
|
|
246
|
+
)) {
|
|
247
|
+
const identity = {
|
|
248
|
+
[table.moduleId]: ceremony.moduleId,
|
|
249
|
+
[table.flowId]: ceremony.flowId,
|
|
250
|
+
[table.kind]: kind,
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
const values = {
|
|
254
|
+
...table.encodeInsert({
|
|
255
|
+
moduleId: ceremony.moduleId,
|
|
256
|
+
flowId: ceremony.flowId,
|
|
257
|
+
purpose: ceremony.purpose,
|
|
258
|
+
kind,
|
|
259
|
+
scope,
|
|
260
|
+
originalWindowMillis: policy.admission[kind].windowMillis,
|
|
261
|
+
marker: owner.marker,
|
|
262
|
+
}),
|
|
263
|
+
...identity,
|
|
264
|
+
[table.purpose]: ceremony.purpose,
|
|
265
|
+
[table.scope]: scope,
|
|
266
|
+
[table.originalWindowMillis]: policy.admission[kind].windowMillis,
|
|
267
|
+
[table.admittedAt]: null,
|
|
268
|
+
[table.retainUntil]: null,
|
|
269
|
+
[table.version]: owner.marker,
|
|
270
|
+
[table.ownerMarker]: owner.marker,
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
const observation = yield* owner.insert(table.table, values, identity);
|
|
274
|
+
|
|
275
|
+
observation.rows = observation.rows.map((row) =>
|
|
276
|
+
Object.fromEntries(
|
|
277
|
+
Object.entries(row).filter(
|
|
278
|
+
([name]) => name !== table.admittedAt && name !== table.retainUntil,
|
|
279
|
+
),
|
|
280
|
+
),
|
|
281
|
+
);
|
|
282
|
+
identities.push(identity);
|
|
283
|
+
}
|
|
284
|
+
invariant(identities.length === kinds.length);
|
|
285
|
+
|
|
286
|
+
const anchorIdentity = {
|
|
287
|
+
[anchor.authorityScope]: mapping.authorityScope,
|
|
288
|
+
[anchor.moduleId]: mapping.moduleId,
|
|
289
|
+
[anchor.version]: owner.marker,
|
|
290
|
+
[anchor.ownerMarker]: owner.marker,
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
const stamp = sql`(select ${col(anchor.table, anchor.admittedAt)} from ${anchor.table} where ${owner.exact(anchor.table, anchorIdentity)})`;
|
|
294
|
+
const stampMillis = clock.toMillis(stamp);
|
|
295
|
+
|
|
296
|
+
const validStamp: SQL = both(
|
|
297
|
+
yield* existsExact(anchor.table, anchorIdentity),
|
|
298
|
+
sql`${stamp} is not null`,
|
|
299
|
+
sql`${stampMillis} >= ${earliest}`,
|
|
300
|
+
sql`${stampMillis} <= ${clock.engineNowMillis}`,
|
|
301
|
+
sql`${stampMillis} <= ${8640000000000000 - chargeRetentionMillis}`,
|
|
302
|
+
);
|
|
303
|
+
|
|
304
|
+
yield* owner.finalUpdate(
|
|
305
|
+
anchor.table,
|
|
306
|
+
owner.exact(anchor.table, { ...anchorIdentity, [anchor.admittedAt]: null }),
|
|
307
|
+
{
|
|
308
|
+
[anchor.admittedAt]: clock.fromMillis(clock.engineNowMillis),
|
|
309
|
+
},
|
|
310
|
+
{ rows: 1, postcondition: validStamp },
|
|
311
|
+
);
|
|
312
|
+
for (const identity of identities) {
|
|
313
|
+
const expected = {
|
|
314
|
+
...identity,
|
|
315
|
+
[table.version]: owner.marker,
|
|
316
|
+
[table.ownerMarker]: owner.marker,
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
const postcondition = yield* existsExact(
|
|
320
|
+
table.table,
|
|
321
|
+
expected,
|
|
322
|
+
both(
|
|
323
|
+
sql`${clock.toMillis(sql`${col(table.table, table.admittedAt)}`)} = ${stampMillis}`,
|
|
324
|
+
sql`${clock.toMillis(sql`${col(table.table, table.retainUntil)}`)} = ${stampMillis} + ${chargeRetentionMillis}`,
|
|
325
|
+
),
|
|
326
|
+
);
|
|
327
|
+
|
|
328
|
+
yield* owner.finalUpdate(
|
|
329
|
+
table.table,
|
|
330
|
+
owner.exact(table.table, {
|
|
331
|
+
...expected,
|
|
332
|
+
[table.admittedAt]: null,
|
|
333
|
+
[table.retainUntil]: null,
|
|
334
|
+
}),
|
|
335
|
+
{
|
|
336
|
+
[table.admittedAt]: stamp,
|
|
337
|
+
[table.retainUntil]: clock.fromMillis(sql`${stampMillis} + ${chargeRetentionMillis}`),
|
|
338
|
+
},
|
|
339
|
+
{ rows: 1, postcondition },
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
owner.postconditions.push(
|
|
343
|
+
sql`(select count(*) from ${table.table} where ${owner.exact(table.table, {
|
|
344
|
+
[table.moduleId]: ceremony.moduleId,
|
|
345
|
+
[table.flowId]: ceremony.flowId,
|
|
346
|
+
[table.ownerMarker]: owner.marker,
|
|
347
|
+
})}) = ${identities.length}`,
|
|
348
|
+
);
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
return {
|
|
352
|
+
chargeRetentionMillis,
|
|
353
|
+
chargeScopes,
|
|
354
|
+
lockAdmission,
|
|
355
|
+
admissionCondition,
|
|
356
|
+
guardAdmission,
|
|
357
|
+
readCharges,
|
|
358
|
+
guardChargeSet,
|
|
359
|
+
insertCharges,
|
|
360
|
+
};
|
|
361
|
+
};
|