@opengeni/db 3.4.0-canary.2 → 3.6.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/dist/canonical-human-identities.d.ts +20 -0
- package/dist/canonical-human-identities.js +7 -3
- package/dist/{chunk-ZWRIPCTR.js → chunk-2NT476HD.js} +2 -2
- package/dist/{chunk-PYMY3AJW.js → chunk-744DFC65.js} +202 -7
- package/dist/chunk-744DFC65.js.map +1 -0
- package/dist/{chunk-O6FTELKH.js → chunk-CDMMSUWJ.js} +3 -3
- package/dist/chunk-G3732M3T.js +299 -0
- package/dist/chunk-G3732M3T.js.map +1 -0
- package/dist/{chunk-TRBOWUG3.js → chunk-IW6O6G7R.js} +2 -2
- package/dist/{chunk-HY3F5Q4E.js → chunk-Q5YUGQVK.js} +3 -3
- package/dist/{chunk-V3TRUXFJ.js → chunk-UGDVSQ6U.js} +72 -19
- package/dist/chunk-UGDVSQ6U.js.map +1 -0
- package/dist/{chunk-C2AFP7SB.js → chunk-UJ2EW3QI.js} +6664 -6051
- package/dist/chunk-UJ2EW3QI.js.map +1 -0
- package/dist/{chunk-H7MXYG7R.js → chunk-WW7AJ5IC.js} +2 -2
- package/dist/editable-artifact-durable-export.js +2 -2
- package/dist/editable-artifacts.js +3 -3
- package/dist/index.d.ts +44 -6
- package/dist/index.js +2914 -1535
- package/dist/index.js.map +1 -1
- package/dist/managed-auth-session-set-schema.d.ts +1399 -0
- package/dist/managed-auth-session-sets.d.ts +145 -0
- package/dist/managed-auth-session-sets.js +50 -0
- package/dist/managed-auth-session-sets.js.map +1 -0
- package/dist/organization-recovery-schema.d.ts +2144 -0
- package/dist/organization-recovery.d.ts +186 -0
- package/dist/provision-roles.js +1 -1
- package/dist/runtime-posture.d.ts +7 -4
- package/dist/schema.d.ts +9 -6
- package/dist/schema.js +43 -1
- package/dist/session-background-commands.js +3 -3
- package/dist/session-control.d.ts +5 -3
- package/dist/session-queue-commands.d.ts +2 -0
- package/dist/session-tenancy.js +3 -3
- package/dist/video-generation.js +3 -3
- package/dist/work-claims-schema.d.ts +975 -0
- package/dist/work-claims.d.ts +83 -0
- package/drizzle/0362_managed_auth_session_sets.sql +2347 -0
- package/drizzle/0363_organization_recovery_custody.sql +2418 -0
- package/drizzle/0364_workspace_learning_policy_snapshot_lock_order.sql +221 -0
- package/drizzle/0365_permission_scoped_work_claims.sql +1306 -0
- package/drizzle/0366_session_discovery_title_search_index.sql +5 -0
- package/drizzle/0367_session_discovery_goal_search_index.sql +5 -0
- package/drizzle/0368_session_discovery_claim_search_index.sql +6 -0
- package/package.json +10 -6
- package/src/canonical-human-identities.ts +105 -27
- package/src/index.ts +1471 -433
- package/src/managed-auth-session-set-schema.ts +176 -0
- package/src/managed-auth-session-sets.ts +430 -0
- package/src/organization-recovery-schema.ts +277 -0
- package/src/organization-recovery.ts +424 -0
- package/src/provision-roles.ts +67 -0
- package/src/runtime-posture.ts +144 -6
- package/src/schema.ts +3 -0
- package/src/session-control.ts +255 -16
- package/src/session-queue-commands.ts +10 -1
- package/src/session-tool-call-settlement.ts +6 -1
- package/src/work-claims-schema.ts +148 -0
- package/src/work-claims.ts +200 -0
- package/dist/chunk-C2AFP7SB.js.map +0 -1
- package/dist/chunk-PYMY3AJW.js.map +0 -1
- package/dist/chunk-V3TRUXFJ.js.map +0 -1
- /package/dist/{chunk-ZWRIPCTR.js.map → chunk-2NT476HD.js.map} +0 -0
- /package/dist/{chunk-O6FTELKH.js.map → chunk-CDMMSUWJ.js.map} +0 -0
- /package/dist/{chunk-TRBOWUG3.js.map → chunk-IW6O6G7R.js.map} +0 -0
- /package/dist/{chunk-HY3F5Q4E.js.map → chunk-Q5YUGQVK.js.map} +0 -0
- /package/dist/{chunk-H7MXYG7R.js.map → chunk-WW7AJ5IC.js.map} +0 -0
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import {
|
|
2
2
|
withRlsContext
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-WW7AJ5IC.js";
|
|
4
4
|
import {
|
|
5
5
|
sandboxRetainedProcesses,
|
|
6
6
|
sessionBackgroundCommands,
|
|
7
7
|
sessions
|
|
8
|
-
} from "./chunk-
|
|
8
|
+
} from "./chunk-UJ2EW3QI.js";
|
|
9
9
|
|
|
10
10
|
// src/session-background-commands.ts
|
|
11
11
|
import { and, desc, eq, inArray, sql } from "drizzle-orm";
|
|
@@ -513,4 +513,4 @@ export {
|
|
|
513
513
|
settleSessionBackgroundCommandForRetainedProcess,
|
|
514
514
|
settleConnectedMachineSessionBackgroundCommand
|
|
515
515
|
};
|
|
516
|
-
//# sourceMappingURL=chunk-
|
|
516
|
+
//# sourceMappingURL=chunk-CDMMSUWJ.js.map
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import {
|
|
2
|
+
nestedPostgresSqlState,
|
|
3
|
+
rawRows
|
|
4
|
+
} from "./chunk-WW7AJ5IC.js";
|
|
5
|
+
|
|
6
|
+
// src/managed-auth-session-sets.ts
|
|
7
|
+
import {
|
|
8
|
+
ManagedAuthLoginTransaction,
|
|
9
|
+
ManagedAuthLogoutAllReceipt,
|
|
10
|
+
ManagedAuthSessionSetProjection
|
|
11
|
+
} from "@opengeni/contracts/managed-auth-session-sets";
|
|
12
|
+
import { sql } from "drizzle-orm";
|
|
13
|
+
var DatabaseProjection = ManagedAuthSessionSetProjection.omit({ csrfToken: true });
|
|
14
|
+
var ManagedAuthSessionSetGenerationConflictError = class extends Error {
|
|
15
|
+
name = "ManagedAuthSessionSetGenerationConflictError";
|
|
16
|
+
code = "generation_conflict";
|
|
17
|
+
constructor() {
|
|
18
|
+
super("The browser session set changed in another request");
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
var ManagedAuthSessionSetOperationReuseError = class extends Error {
|
|
22
|
+
name = "ManagedAuthSessionSetOperationReuseError";
|
|
23
|
+
constructor() {
|
|
24
|
+
super("The browser session-set operation id was reused for different input");
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
var ManagedAuthSessionSetAuthorityError = class extends Error {
|
|
28
|
+
name = "ManagedAuthSessionSetAuthorityError";
|
|
29
|
+
};
|
|
30
|
+
var ManagedAuthLoginSlotUnavailableError = class extends Error {
|
|
31
|
+
name = "ManagedAuthLoginSlotUnavailableError";
|
|
32
|
+
};
|
|
33
|
+
var ManagedAuthLoginSlotLimitError = class extends Error {
|
|
34
|
+
name = "ManagedAuthLoginSlotLimitError";
|
|
35
|
+
};
|
|
36
|
+
var ManagedAuthLoginSlotAlreadyExistsError = class extends Error {
|
|
37
|
+
name = "ManagedAuthLoginSlotAlreadyExistsError";
|
|
38
|
+
code = "slot_already_exists";
|
|
39
|
+
};
|
|
40
|
+
var ManagedAuthActorMutationInFlightError = class extends Error {
|
|
41
|
+
name = "ManagedAuthActorMutationInFlightError";
|
|
42
|
+
code = "actor_mutation_in_flight";
|
|
43
|
+
};
|
|
44
|
+
var ManagedAuthLoginTransactionRateLimitError = class extends Error {
|
|
45
|
+
name = "ManagedAuthLoginTransactionRateLimitError";
|
|
46
|
+
code = "login_transaction_rate_limited";
|
|
47
|
+
};
|
|
48
|
+
function mapSqlError(error) {
|
|
49
|
+
const state = nestedPostgresSqlState(error);
|
|
50
|
+
if (state === "40001") throw new ManagedAuthSessionSetGenerationConflictError();
|
|
51
|
+
if (state === "23505") throw new ManagedAuthSessionSetOperationReuseError();
|
|
52
|
+
if (state === "P0002") throw new ManagedAuthLoginSlotUnavailableError();
|
|
53
|
+
if (state === "P0003") throw new ManagedAuthLoginSlotAlreadyExistsError();
|
|
54
|
+
if (state === "54000") throw new ManagedAuthLoginSlotLimitError();
|
|
55
|
+
if (state === "55P03") throw new ManagedAuthActorMutationInFlightError();
|
|
56
|
+
if (state === "P0004") throw new ManagedAuthLoginTransactionRateLimitError();
|
|
57
|
+
if (state === "42501") {
|
|
58
|
+
throw new ManagedAuthSessionSetAuthorityError(
|
|
59
|
+
error instanceof Error ? error.message : "Browser session-set authority denied"
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
throw error;
|
|
63
|
+
}
|
|
64
|
+
function jsonValue(value) {
|
|
65
|
+
return typeof value === "string" ? JSON.parse(value) : value;
|
|
66
|
+
}
|
|
67
|
+
async function oneJson(db, query) {
|
|
68
|
+
const [row] = await rawRows(db, query);
|
|
69
|
+
return row ? jsonValue(row.result) : null;
|
|
70
|
+
}
|
|
71
|
+
function projectionFromResult(value) {
|
|
72
|
+
const projection = value?.projection;
|
|
73
|
+
return DatabaseProjection.parse(projection);
|
|
74
|
+
}
|
|
75
|
+
async function getManagedAuthSessionSetAuthorityState(db, authorityHash) {
|
|
76
|
+
try {
|
|
77
|
+
const [row] = await rawRows(
|
|
78
|
+
db,
|
|
79
|
+
sql`select managed_auth_session_set_authority_state(${authorityHash}) as state`
|
|
80
|
+
);
|
|
81
|
+
const value = row?.state;
|
|
82
|
+
if (value === "absent" || value === "active" || value === "retired") return value;
|
|
83
|
+
throw new ManagedAuthSessionSetAuthorityError("Invalid browser session-set authority state");
|
|
84
|
+
} catch (error) {
|
|
85
|
+
mapSqlError(error);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
async function getManagedAuthSessionSetSnapshot(db, input) {
|
|
89
|
+
try {
|
|
90
|
+
const value = await oneJson(
|
|
91
|
+
db,
|
|
92
|
+
sql`select managed_auth_session_set_snapshot(
|
|
93
|
+
${input.authorityHash}, ${input.mode}, ${input.includeInternal ?? false},
|
|
94
|
+
${input.allowRecovery ?? false}, ${input.readOnly ?? false}
|
|
95
|
+
) as result`
|
|
96
|
+
);
|
|
97
|
+
if (value === null) return null;
|
|
98
|
+
const snapshot = value;
|
|
99
|
+
return {
|
|
100
|
+
projection: DatabaseProjection.parse(snapshot.projection),
|
|
101
|
+
selected: snapshot.selected ?? null,
|
|
102
|
+
internalSlots: snapshot.internalSlots ?? []
|
|
103
|
+
};
|
|
104
|
+
} catch (error) {
|
|
105
|
+
mapSqlError(error);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
async function bootstrapManagedAuthSessionSet(db, input) {
|
|
109
|
+
try {
|
|
110
|
+
return projectionFromResult(
|
|
111
|
+
await oneJson(
|
|
112
|
+
db,
|
|
113
|
+
sql`select managed_auth_session_set_bootstrap(
|
|
114
|
+
${input.authorityHash}, ${input.csrfHash}, ${input.authSessionId}, ${input.mode},
|
|
115
|
+
${input.operationId}::uuid, ${input.requestDigest},
|
|
116
|
+
${input.expectedGeneration}::bigint, ${input.expectedActorEpoch}::bigint
|
|
117
|
+
) as result`
|
|
118
|
+
)
|
|
119
|
+
);
|
|
120
|
+
} catch (error) {
|
|
121
|
+
mapSqlError(error);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
async function beginManagedAuthLoginTransaction(db, input) {
|
|
125
|
+
try {
|
|
126
|
+
const value = await oneJson(
|
|
127
|
+
db,
|
|
128
|
+
sql`select managed_auth_session_set_begin_transaction(
|
|
129
|
+
${input.authorityHash}, ${input.csrfHash},
|
|
130
|
+
${input.rateScopeHash ?? input.authorityHash}, ${input.operationId}::uuid,
|
|
131
|
+
${input.requestDigest}, ${input.expectedGeneration}::bigint,
|
|
132
|
+
${input.transactionId}::uuid, ${input.expectedActorEpoch}::bigint,
|
|
133
|
+
${input.transactionSecretHash}, ${input.kind},
|
|
134
|
+
${input.targetSlotId}::uuid, ${input.returnIntentId}::uuid,
|
|
135
|
+
${input.returnPath}, ${input.expiresAt.toISOString()}::timestamptz
|
|
136
|
+
) as result`
|
|
137
|
+
);
|
|
138
|
+
return ManagedAuthLoginTransaction.parse({
|
|
139
|
+
...value,
|
|
140
|
+
expiresAt: typeof value?.expiresAt === "string" || value?.expiresAt instanceof Date ? new Date(value.expiresAt).toISOString() : value?.expiresAt
|
|
141
|
+
});
|
|
142
|
+
} catch (error) {
|
|
143
|
+
mapSqlError(error);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
async function completeManagedAuthLoginTransaction(db, input) {
|
|
147
|
+
try {
|
|
148
|
+
const result = await oneJson(
|
|
149
|
+
db,
|
|
150
|
+
sql`select managed_auth_session_set_complete_transaction(
|
|
151
|
+
${input.authorityHash}, ${input.csrfHash}, ${input.operationId}::uuid,
|
|
152
|
+
${input.requestDigest}, ${input.expectedGeneration}::bigint,
|
|
153
|
+
${input.expectedActorEpoch}::bigint,
|
|
154
|
+
${input.transactionId}::uuid, ${input.transactionSecretHash},
|
|
155
|
+
${input.authSessionId}, ${input.mode}
|
|
156
|
+
) as result`
|
|
157
|
+
);
|
|
158
|
+
return {
|
|
159
|
+
projection: DatabaseProjection.parse(result.projection),
|
|
160
|
+
returnIntent: result.returnIntent ?? null
|
|
161
|
+
};
|
|
162
|
+
} catch (error) {
|
|
163
|
+
mapSqlError(error);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
async function mutateManagedAuthSessionSet(db, input) {
|
|
167
|
+
try {
|
|
168
|
+
const result = await oneJson(
|
|
169
|
+
db,
|
|
170
|
+
sql`select managed_auth_session_set_mutate(
|
|
171
|
+
${input.authorityHash}, ${input.csrfHash}, ${input.operationId}::uuid,
|
|
172
|
+
${input.requestDigest}, ${input.expectedGeneration}::bigint,
|
|
173
|
+
${input.expectedActorEpoch}::bigint, ${input.operationType},
|
|
174
|
+
${input.targetSlotId ?? null}::uuid, ${input.replacementSlotId ?? null}::uuid,
|
|
175
|
+
${input.transactionId ?? null}::uuid, ${input.transactionSecretHash ?? null}, ${input.mode}
|
|
176
|
+
) as result`
|
|
177
|
+
);
|
|
178
|
+
if (input.operationType === "logout_all") return ManagedAuthLogoutAllReceipt.parse(result);
|
|
179
|
+
return projectionFromResult(result);
|
|
180
|
+
} catch (error) {
|
|
181
|
+
mapSqlError(error);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
async function getManagedAuthSessionSetOperationReceipt(db, input) {
|
|
185
|
+
const result = await oneJson(
|
|
186
|
+
db,
|
|
187
|
+
sql`select managed_auth_session_set_operation_receipt(
|
|
188
|
+
${input.authorityHash}, ${input.operationId}::uuid, ${input.requestDigest}, 'complete'
|
|
189
|
+
) as result`
|
|
190
|
+
);
|
|
191
|
+
if (!result?.projection) return null;
|
|
192
|
+
return {
|
|
193
|
+
projection: DatabaseProjection.parse(result.projection),
|
|
194
|
+
returnIntent: result.returnIntent ?? null
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
async function acquireManagedAuthActorMutationLease(db, input) {
|
|
198
|
+
try {
|
|
199
|
+
const [row] = await rawRows(
|
|
200
|
+
db,
|
|
201
|
+
sql`select managed_auth_actor_mutation_lease_acquire(
|
|
202
|
+
${input.authorityHash}, ${input.actorEpoch}::bigint,
|
|
203
|
+
${input.requestId}::uuid, ${input.leaseSeconds}::integer
|
|
204
|
+
) as "expiresAt"`
|
|
205
|
+
);
|
|
206
|
+
if (!row) throw new ManagedAuthSessionSetAuthorityError("Mutation lease was not acquired");
|
|
207
|
+
return row.expiresAt instanceof Date ? row.expiresAt : new Date(row.expiresAt);
|
|
208
|
+
} catch (error) {
|
|
209
|
+
mapSqlError(error);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
async function releaseManagedAuthActorMutationLease(db, input) {
|
|
213
|
+
try {
|
|
214
|
+
const [row] = await rawRows(
|
|
215
|
+
db,
|
|
216
|
+
sql`select managed_auth_actor_mutation_lease_release(
|
|
217
|
+
${input.authorityHash}, ${input.requestId}::uuid
|
|
218
|
+
) as released`
|
|
219
|
+
);
|
|
220
|
+
return row?.released === true;
|
|
221
|
+
} catch (error) {
|
|
222
|
+
mapSqlError(error);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
async function getManagedAuthAdoptedSessionSnapshot(db, authSessionId) {
|
|
226
|
+
try {
|
|
227
|
+
const value = await oneJson(
|
|
228
|
+
db,
|
|
229
|
+
sql`select managed_auth_adopted_session_snapshot(${authSessionId}) as result`
|
|
230
|
+
);
|
|
231
|
+
if (value === null) return null;
|
|
232
|
+
if (!/^[0-9a-f]{64}$/.test(value.authorityHash) || !/^[1-9][0-9]*$/.test(value.actorEpoch)) {
|
|
233
|
+
throw new ManagedAuthSessionSetAuthorityError("Invalid adopted-session snapshot");
|
|
234
|
+
}
|
|
235
|
+
return value;
|
|
236
|
+
} catch (error) {
|
|
237
|
+
mapSqlError(error);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
async function validateManagedAuthActorMutationLease(db, input) {
|
|
241
|
+
try {
|
|
242
|
+
const [row] = await rawRows(
|
|
243
|
+
db,
|
|
244
|
+
sql`select managed_auth_actor_mutation_lease_validate(
|
|
245
|
+
${input.authorityHash}, ${input.actorEpoch}::bigint, ${input.requestId}::uuid
|
|
246
|
+
) as valid`
|
|
247
|
+
);
|
|
248
|
+
return row?.valid === true;
|
|
249
|
+
} catch (error) {
|
|
250
|
+
mapSqlError(error);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
async function reapManagedAuthIsolatedSessions(db, limit = 100) {
|
|
254
|
+
try {
|
|
255
|
+
const [row] = await rawRows(
|
|
256
|
+
db,
|
|
257
|
+
sql`select managed_auth_isolated_session_reap(${limit}::integer) as reaped`
|
|
258
|
+
);
|
|
259
|
+
return row?.reaped ?? 0;
|
|
260
|
+
} catch (error) {
|
|
261
|
+
mapSqlError(error);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
async function reapExpiredManagedAuthSessionSets(db, limit = 100) {
|
|
265
|
+
try {
|
|
266
|
+
const [row] = await rawRows(
|
|
267
|
+
db,
|
|
268
|
+
sql`select managed_auth_expired_session_set_reap(${limit}::integer) as retired`
|
|
269
|
+
);
|
|
270
|
+
return row?.retired ?? 0;
|
|
271
|
+
} catch (error) {
|
|
272
|
+
mapSqlError(error);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
export {
|
|
277
|
+
ManagedAuthSessionSetGenerationConflictError,
|
|
278
|
+
ManagedAuthSessionSetOperationReuseError,
|
|
279
|
+
ManagedAuthSessionSetAuthorityError,
|
|
280
|
+
ManagedAuthLoginSlotUnavailableError,
|
|
281
|
+
ManagedAuthLoginSlotLimitError,
|
|
282
|
+
ManagedAuthLoginSlotAlreadyExistsError,
|
|
283
|
+
ManagedAuthActorMutationInFlightError,
|
|
284
|
+
ManagedAuthLoginTransactionRateLimitError,
|
|
285
|
+
getManagedAuthSessionSetAuthorityState,
|
|
286
|
+
getManagedAuthSessionSetSnapshot,
|
|
287
|
+
bootstrapManagedAuthSessionSet,
|
|
288
|
+
beginManagedAuthLoginTransaction,
|
|
289
|
+
completeManagedAuthLoginTransaction,
|
|
290
|
+
mutateManagedAuthSessionSet,
|
|
291
|
+
getManagedAuthSessionSetOperationReceipt,
|
|
292
|
+
acquireManagedAuthActorMutationLease,
|
|
293
|
+
releaseManagedAuthActorMutationLease,
|
|
294
|
+
getManagedAuthAdoptedSessionSnapshot,
|
|
295
|
+
validateManagedAuthActorMutationLease,
|
|
296
|
+
reapManagedAuthIsolatedSessions,
|
|
297
|
+
reapExpiredManagedAuthSessionSets
|
|
298
|
+
};
|
|
299
|
+
//# sourceMappingURL=chunk-G3732M3T.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/managed-auth-session-sets.ts"],"sourcesContent":["import {\n ManagedAuthLoginTransaction,\n ManagedAuthLogoutAllReceipt,\n ManagedAuthSessionSetProjection,\n type ManagedAuthLoginTransaction as ManagedAuthLoginTransactionType,\n type ManagedAuthLogoutAllReceipt as ManagedAuthLogoutAllReceiptType,\n type ManagedAuthSessionSetMode,\n type ManagedAuthSessionSetProjection as ManagedAuthSessionSetProjectionType,\n} from \"@opengeni/contracts/managed-auth-session-sets\";\nimport { sql, type SQL } from \"drizzle-orm\";\nimport type { Database } from \"./database\";\nimport { rawRows } from \"./database\";\nimport { nestedPostgresSqlState } from \"./persistence-errors\";\n\nconst DatabaseProjection = ManagedAuthSessionSetProjection.omit({ csrfToken: true });\nexport type ManagedAuthDatabaseProjection = Omit<ManagedAuthSessionSetProjectionType, \"csrfToken\">;\n\nexport type ManagedAuthSelectedSession = {\n slotId: string;\n authSessionId: string;\n authUserId: string;\n token: string;\n email: string;\n name: string;\n emailVerified: boolean;\n};\n\nexport type ManagedAuthInternalSlot = ManagedAuthSelectedSession;\n\nexport type ManagedAuthAdoptedSessionSnapshot = {\n authorityHash: string;\n actorEpoch: string;\n selected: ManagedAuthSelectedSession | null;\n};\n\nexport class ManagedAuthSessionSetGenerationConflictError extends Error {\n readonly name = \"ManagedAuthSessionSetGenerationConflictError\";\n readonly code = \"generation_conflict\";\n constructor() {\n super(\"The browser session set changed in another request\");\n }\n}\n\nexport class ManagedAuthSessionSetOperationReuseError extends Error {\n readonly name = \"ManagedAuthSessionSetOperationReuseError\";\n constructor() {\n super(\"The browser session-set operation id was reused for different input\");\n }\n}\n\nexport class ManagedAuthSessionSetAuthorityError extends Error {\n readonly name = \"ManagedAuthSessionSetAuthorityError\";\n}\n\nexport class ManagedAuthLoginSlotUnavailableError extends Error {\n readonly name = \"ManagedAuthLoginSlotUnavailableError\";\n}\n\nexport class ManagedAuthLoginSlotLimitError extends Error {\n readonly name = \"ManagedAuthLoginSlotLimitError\";\n}\n\nexport class ManagedAuthLoginSlotAlreadyExistsError extends Error {\n readonly name = \"ManagedAuthLoginSlotAlreadyExistsError\";\n readonly code = \"slot_already_exists\";\n}\n\nexport class ManagedAuthActorMutationInFlightError extends Error {\n readonly name = \"ManagedAuthActorMutationInFlightError\";\n readonly code = \"actor_mutation_in_flight\";\n}\n\nexport class ManagedAuthLoginTransactionRateLimitError extends Error {\n readonly name = \"ManagedAuthLoginTransactionRateLimitError\";\n readonly code = \"login_transaction_rate_limited\";\n}\n\nfunction mapSqlError(error: unknown): never {\n const state = nestedPostgresSqlState(error);\n if (state === \"40001\") throw new ManagedAuthSessionSetGenerationConflictError();\n if (state === \"23505\") throw new ManagedAuthSessionSetOperationReuseError();\n if (state === \"P0002\") throw new ManagedAuthLoginSlotUnavailableError();\n if (state === \"P0003\") throw new ManagedAuthLoginSlotAlreadyExistsError();\n if (state === \"54000\") throw new ManagedAuthLoginSlotLimitError();\n if (state === \"55P03\") throw new ManagedAuthActorMutationInFlightError();\n if (state === \"P0004\") throw new ManagedAuthLoginTransactionRateLimitError();\n if (state === \"42501\") {\n throw new ManagedAuthSessionSetAuthorityError(\n error instanceof Error ? error.message : \"Browser session-set authority denied\",\n );\n }\n throw error;\n}\n\nfunction jsonValue(value: unknown): unknown {\n return typeof value === \"string\" ? JSON.parse(value) : value;\n}\n\nasync function oneJson(db: Database, query: SQL): Promise<unknown> {\n const [row] = await rawRows<{ result: unknown }>(db, query);\n return row ? jsonValue(row.result) : null;\n}\n\nfunction projectionFromResult(value: unknown): ManagedAuthDatabaseProjection {\n const projection = (value as { projection?: unknown } | null)?.projection;\n return DatabaseProjection.parse(projection);\n}\n\nexport async function getManagedAuthSessionSetAuthorityState(\n db: Database,\n authorityHash: string,\n): Promise<\"absent\" | \"active\" | \"retired\"> {\n try {\n const [row] = await rawRows<{ state: string }>(\n db,\n sql`select managed_auth_session_set_authority_state(${authorityHash}) as state`,\n );\n const value = row?.state;\n if (value === \"absent\" || value === \"active\" || value === \"retired\") return value;\n throw new ManagedAuthSessionSetAuthorityError(\"Invalid browser session-set authority state\");\n } catch (error) {\n mapSqlError(error);\n }\n}\n\nexport async function getManagedAuthSessionSetSnapshot(\n db: Database,\n input: {\n authorityHash: string;\n mode: ManagedAuthSessionSetMode;\n includeInternal?: boolean;\n allowRecovery?: boolean;\n readOnly?: boolean;\n },\n): Promise<{\n projection: ManagedAuthDatabaseProjection;\n selected: ManagedAuthSelectedSession | null;\n internalSlots: ManagedAuthInternalSlot[];\n} | null> {\n try {\n const value = await oneJson(\n db,\n sql`select managed_auth_session_set_snapshot(\n ${input.authorityHash}, ${input.mode}, ${input.includeInternal ?? false},\n ${input.allowRecovery ?? false}, ${input.readOnly ?? false}\n ) as result`,\n );\n if (value === null) return null;\n const snapshot = value as {\n projection: unknown;\n selected?: ManagedAuthSelectedSession | null;\n internalSlots?: ManagedAuthInternalSlot[];\n };\n return {\n projection: DatabaseProjection.parse(snapshot.projection),\n selected: snapshot.selected ?? null,\n internalSlots: snapshot.internalSlots ?? [],\n };\n } catch (error) {\n mapSqlError(error);\n }\n}\n\nexport async function bootstrapManagedAuthSessionSet(\n db: Database,\n input: {\n authorityHash: string;\n csrfHash: string;\n authSessionId: string | null;\n mode: ManagedAuthSessionSetMode;\n operationId: string;\n requestDigest: string;\n expectedGeneration: string;\n expectedActorEpoch: string;\n },\n): Promise<ManagedAuthDatabaseProjection> {\n try {\n return projectionFromResult(\n await oneJson(\n db,\n sql`select managed_auth_session_set_bootstrap(\n ${input.authorityHash}, ${input.csrfHash}, ${input.authSessionId}, ${input.mode},\n ${input.operationId}::uuid, ${input.requestDigest},\n ${input.expectedGeneration}::bigint, ${input.expectedActorEpoch}::bigint\n ) as result`,\n ),\n );\n } catch (error) {\n mapSqlError(error);\n }\n}\n\nexport async function beginManagedAuthLoginTransaction(\n db: Database,\n input: {\n authorityHash: string;\n csrfHash: string;\n rateScopeHash?: string;\n operationId: string;\n requestDigest: string;\n expectedGeneration: string;\n expectedActorEpoch: string;\n transactionId: string;\n transactionSecretHash: string;\n kind: \"add\" | \"reauth\";\n targetSlotId: string | null;\n returnIntentId: string | null;\n returnPath: string | null;\n expiresAt: Date;\n },\n): Promise<ManagedAuthLoginTransactionType> {\n try {\n const value = (await oneJson(\n db,\n sql`select managed_auth_session_set_begin_transaction(\n ${input.authorityHash}, ${input.csrfHash},\n ${input.rateScopeHash ?? input.authorityHash}, ${input.operationId}::uuid,\n ${input.requestDigest}, ${input.expectedGeneration}::bigint,\n ${input.transactionId}::uuid, ${input.expectedActorEpoch}::bigint,\n ${input.transactionSecretHash}, ${input.kind},\n ${input.targetSlotId}::uuid, ${input.returnIntentId}::uuid,\n ${input.returnPath}, ${input.expiresAt.toISOString()}::timestamptz\n ) as result`,\n )) as { expiresAt?: unknown } | null;\n return ManagedAuthLoginTransaction.parse({\n ...value,\n expiresAt:\n typeof value?.expiresAt === \"string\" || value?.expiresAt instanceof Date\n ? new Date(value.expiresAt).toISOString()\n : value?.expiresAt,\n });\n } catch (error) {\n mapSqlError(error);\n }\n}\n\nexport async function completeManagedAuthLoginTransaction(\n db: Database,\n input: {\n authorityHash: string;\n csrfHash: string;\n operationId: string;\n requestDigest: string;\n expectedGeneration: string;\n expectedActorEpoch: string;\n transactionId: string;\n transactionSecretHash: string;\n authSessionId: string;\n mode: ManagedAuthSessionSetMode;\n },\n): Promise<{ projection: ManagedAuthDatabaseProjection; returnIntent: string | null }> {\n try {\n const result = (await oneJson(\n db,\n sql`select managed_auth_session_set_complete_transaction(\n ${input.authorityHash}, ${input.csrfHash}, ${input.operationId}::uuid,\n ${input.requestDigest}, ${input.expectedGeneration}::bigint,\n ${input.expectedActorEpoch}::bigint,\n ${input.transactionId}::uuid, ${input.transactionSecretHash},\n ${input.authSessionId}, ${input.mode}\n ) as result`,\n )) as { projection: unknown; returnIntent?: string | null };\n return {\n projection: DatabaseProjection.parse(result.projection),\n returnIntent: result.returnIntent ?? null,\n };\n } catch (error) {\n mapSqlError(error);\n }\n}\n\nexport async function mutateManagedAuthSessionSet(\n db: Database,\n input: {\n authorityHash: string;\n csrfHash: string;\n operationId: string;\n requestDigest: string;\n expectedGeneration: string;\n expectedActorEpoch: string;\n operationType: \"cancel_transaction\" | \"select\" | \"logout_one\" | \"logout_all\";\n targetSlotId?: string | null;\n replacementSlotId?: string | null;\n transactionId?: string | null;\n transactionSecretHash?: string | null;\n mode: ManagedAuthSessionSetMode;\n },\n): Promise<ManagedAuthDatabaseProjection | ManagedAuthLogoutAllReceiptType> {\n try {\n const result = await oneJson(\n db,\n sql`select managed_auth_session_set_mutate(\n ${input.authorityHash}, ${input.csrfHash}, ${input.operationId}::uuid,\n ${input.requestDigest}, ${input.expectedGeneration}::bigint,\n ${input.expectedActorEpoch}::bigint, ${input.operationType},\n ${input.targetSlotId ?? null}::uuid, ${input.replacementSlotId ?? null}::uuid,\n ${input.transactionId ?? null}::uuid, ${input.transactionSecretHash ?? null}, ${input.mode}\n ) as result`,\n );\n if (input.operationType === \"logout_all\") return ManagedAuthLogoutAllReceipt.parse(result);\n return projectionFromResult(result);\n } catch (error) {\n mapSqlError(error);\n }\n}\n\nexport async function getManagedAuthSessionSetOperationReceipt(\n db: Database,\n input: { authorityHash: string; operationId: string; requestDigest: string },\n): Promise<{ projection: ManagedAuthDatabaseProjection; returnIntent: string | null } | null> {\n const result = (await oneJson(\n db,\n sql`select managed_auth_session_set_operation_receipt(\n ${input.authorityHash}, ${input.operationId}::uuid, ${input.requestDigest}, 'complete'\n ) as result`,\n )) as { projection?: unknown; returnIntent?: string | null } | null;\n if (!result?.projection) return null;\n return {\n projection: DatabaseProjection.parse(result.projection),\n returnIntent: result.returnIntent ?? null,\n };\n}\n\nexport async function acquireManagedAuthActorMutationLease(\n db: Database,\n input: {\n authorityHash: string;\n actorEpoch: string;\n requestId: string;\n leaseSeconds: number;\n },\n): Promise<Date> {\n try {\n const [row] = await rawRows<{ expiresAt: Date | string }>(\n db,\n sql`select managed_auth_actor_mutation_lease_acquire(\n ${input.authorityHash}, ${input.actorEpoch}::bigint,\n ${input.requestId}::uuid, ${input.leaseSeconds}::integer\n ) as \"expiresAt\"`,\n );\n if (!row) throw new ManagedAuthSessionSetAuthorityError(\"Mutation lease was not acquired\");\n return row.expiresAt instanceof Date ? row.expiresAt : new Date(row.expiresAt);\n } catch (error) {\n mapSqlError(error);\n }\n}\n\nexport async function releaseManagedAuthActorMutationLease(\n db: Database,\n input: { authorityHash: string; requestId: string },\n): Promise<boolean> {\n try {\n const [row] = await rawRows<{ released: boolean }>(\n db,\n sql`select managed_auth_actor_mutation_lease_release(\n ${input.authorityHash}, ${input.requestId}::uuid\n ) as released`,\n );\n return row?.released === true;\n } catch (error) {\n mapSqlError(error);\n }\n}\n\n/** Classify a legacy ambient session once it has entered session-set authority. */\nexport async function getManagedAuthAdoptedSessionSnapshot(\n db: Database,\n authSessionId: string,\n): Promise<ManagedAuthAdoptedSessionSnapshot | null> {\n try {\n const value = (await oneJson(\n db,\n sql`select managed_auth_adopted_session_snapshot(${authSessionId}) as result`,\n )) as ManagedAuthAdoptedSessionSnapshot | null;\n if (value === null) return null;\n if (!/^[0-9a-f]{64}$/.test(value.authorityHash) || !/^[1-9][0-9]*$/.test(value.actorEpoch)) {\n throw new ManagedAuthSessionSetAuthorityError(\"Invalid adopted-session snapshot\");\n }\n return value;\n } catch (error) {\n mapSqlError(error);\n }\n}\n\n/** Exact post-handler proof that the request-owned actor lease is still live. */\nexport async function validateManagedAuthActorMutationLease(\n db: Database,\n input: { authorityHash: string; actorEpoch: string; requestId: string },\n): Promise<boolean> {\n try {\n const [row] = await rawRows<{ valid: boolean }>(\n db,\n sql`select managed_auth_actor_mutation_lease_validate(\n ${input.authorityHash}, ${input.actorEpoch}::bigint, ${input.requestId}::uuid\n ) as valid`,\n );\n return row?.valid === true;\n } catch (error) {\n mapSqlError(error);\n }\n}\n\n/** Bounded cleanup for provider sessions left by a process death before adoption. */\nexport async function reapManagedAuthIsolatedSessions(db: Database, limit = 100): Promise<number> {\n try {\n const [row] = await rawRows<{ reaped: number }>(\n db,\n sql`select managed_auth_isolated_session_reap(${limit}::integer) as reaped`,\n );\n return row?.reaped ?? 0;\n } catch (error) {\n mapSqlError(error);\n }\n}\n\n/** Bounded, lease-aware retirement of expired installation/session-set authority. */\nexport async function reapExpiredManagedAuthSessionSets(\n db: Database,\n limit = 100,\n): Promise<number> {\n try {\n const [row] = await rawRows<{ retired: number }>(\n db,\n sql`select managed_auth_expired_session_set_reap(${limit}::integer) as retired`,\n );\n return row?.retired ?? 0;\n } catch (error) {\n mapSqlError(error);\n }\n}\n"],"mappings":";;;;;;AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAKK;AACP,SAAS,WAAqB;AAK9B,IAAM,qBAAqB,gCAAgC,KAAK,EAAE,WAAW,KAAK,CAAC;AAqB5E,IAAM,+CAAN,cAA2D,MAAM;AAAA,EAC7D,OAAO;AAAA,E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KAAK;AAAA,EACnB;AACF;","names":[]}
|
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
withWorkspaceRls,
|
|
6
6
|
withWorkspaceSubjectRls,
|
|
7
7
|
withWorkspaceSubjectSessionActivityRls
|
|
8
|
-
} from "./chunk-
|
|
8
|
+
} from "./chunk-WW7AJ5IC.js";
|
|
9
9
|
|
|
10
10
|
// src/session-tenancy.ts
|
|
11
11
|
import { createHash } from "crypto";
|
|
@@ -481,4 +481,4 @@ export {
|
|
|
481
481
|
forkSessionContent,
|
|
482
482
|
replayAppliedSessionFork
|
|
483
483
|
};
|
|
484
|
-
//# sourceMappingURL=chunk-
|
|
484
|
+
//# sourceMappingURL=chunk-IW6O6G7R.js.map
|
|
@@ -2,7 +2,7 @@ import {
|
|
|
2
2
|
rawRows,
|
|
3
3
|
withRlsContext,
|
|
4
4
|
withWorkspaceRls
|
|
5
|
-
} from "./chunk-
|
|
5
|
+
} from "./chunk-WW7AJ5IC.js";
|
|
6
6
|
import {
|
|
7
7
|
connections,
|
|
8
8
|
creditLedgerEntries,
|
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
workspaceVideoGenerationPolicies,
|
|
15
15
|
workspaceVideoGenerationQuotas,
|
|
16
16
|
workspaces
|
|
17
|
-
} from "./chunk-
|
|
17
|
+
} from "./chunk-UJ2EW3QI.js";
|
|
18
18
|
|
|
19
19
|
// src/video-generation.ts
|
|
20
20
|
import {
|
|
@@ -992,4 +992,4 @@ export {
|
|
|
992
992
|
mediaGenerationResultForStoredOperation,
|
|
993
993
|
getVideoGenerationOperationSummary
|
|
994
994
|
};
|
|
995
|
-
//# sourceMappingURL=chunk-
|
|
995
|
+
//# sourceMappingURL=chunk-Q5YUGQVK.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
nestedPostgresSqlState,
|
|
3
3
|
rawRows
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-WW7AJ5IC.js";
|
|
5
5
|
|
|
6
6
|
// src/canonical-human-identities.ts
|
|
7
7
|
import { createHash } from "crypto";
|
|
@@ -10,6 +10,22 @@ import {
|
|
|
10
10
|
CanonicalHumanIdentityProjection
|
|
11
11
|
} from "@opengeni/contracts/canonical-human-identities";
|
|
12
12
|
import { sql } from "drizzle-orm";
|
|
13
|
+
async function getCanonicalHumanExactLoginBindingForAuthUser(db, input) {
|
|
14
|
+
const [row] = await rawRows(
|
|
15
|
+
db,
|
|
16
|
+
sql`
|
|
17
|
+
select get_canonical_human_exact_login_binding(
|
|
18
|
+
${input.authUserId}, ${input.providerId}
|
|
19
|
+
) as result
|
|
20
|
+
`
|
|
21
|
+
);
|
|
22
|
+
if (!row?.result) {
|
|
23
|
+
throw new CanonicalHumanIdentityAuthorityError(
|
|
24
|
+
"Authentication did not prove exactly one canonical login binding"
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
return { ...row.result, revision: Number(row.result.revision) };
|
|
28
|
+
}
|
|
13
29
|
var CanonicalHumanIdentityConflictError = class extends Error {
|
|
14
30
|
name = "CanonicalHumanIdentityConflictError";
|
|
15
31
|
code = "CANONICAL_HUMAN_IDENTITY_CONFLICT";
|
|
@@ -24,6 +40,13 @@ var CanonicalHumanIdentityOperationReuseError = class extends Error {
|
|
|
24
40
|
super("The canonical human identity operation id was already used for another request");
|
|
25
41
|
}
|
|
26
42
|
};
|
|
43
|
+
var CanonicalHumanIdentityMutationInFlightError = class extends Error {
|
|
44
|
+
name = "CanonicalHumanIdentityMutationInFlightError";
|
|
45
|
+
code = "CANONICAL_HUMAN_IDENTITY_MUTATION_IN_FLIGHT";
|
|
46
|
+
constructor() {
|
|
47
|
+
super("Another request is still using this browser actor");
|
|
48
|
+
}
|
|
49
|
+
};
|
|
27
50
|
var CanonicalHumanIdentityNotFoundError = class extends Error {
|
|
28
51
|
name = "CanonicalHumanIdentityNotFoundError";
|
|
29
52
|
constructor(message = "Canonical human identity was not found") {
|
|
@@ -48,6 +71,7 @@ function mapSqlError(error) {
|
|
|
48
71
|
const state = nestedPostgresSqlState(error);
|
|
49
72
|
if (state === "40001") throw new CanonicalHumanIdentityConflictError();
|
|
50
73
|
if (state === "23505") throw new CanonicalHumanIdentityOperationReuseError();
|
|
74
|
+
if (state === "55P03") throw new CanonicalHumanIdentityMutationInFlightError();
|
|
51
75
|
if (state === "P0002") throw new CanonicalHumanIdentityNotFoundError();
|
|
52
76
|
if (state === "42501") {
|
|
53
77
|
throw new CanonicalHumanIdentityAuthorityError(
|
|
@@ -81,6 +105,17 @@ async function ensureCanonicalHumanIdentity(db, input) {
|
|
|
81
105
|
}
|
|
82
106
|
}
|
|
83
107
|
async function ensureCanonicalHumanIdentityForAuthUser(db, authUserId) {
|
|
108
|
+
try {
|
|
109
|
+
const existing = await getCanonicalHumanIdentityProjection(db, authUserId);
|
|
110
|
+
return {
|
|
111
|
+
identityId: existing.activeIdentity.id,
|
|
112
|
+
identityRevision: existing.activeIdentity.identityRevision,
|
|
113
|
+
authRevision: existing.activeIdentity.authRevision,
|
|
114
|
+
identityStatus: existing.activeIdentity.status
|
|
115
|
+
};
|
|
116
|
+
} catch (error) {
|
|
117
|
+
if (!(error instanceof CanonicalHumanIdentityNotFoundError)) throw error;
|
|
118
|
+
}
|
|
84
119
|
const [user] = await rawRows(
|
|
85
120
|
db,
|
|
86
121
|
sql`
|
|
@@ -209,9 +244,31 @@ async function getCanonicalHumanIdentityProjection(db, authUserId) {
|
|
|
209
244
|
}
|
|
210
245
|
async function applyCanonicalHumanIdentityOperation(db, input) {
|
|
211
246
|
try {
|
|
212
|
-
|
|
213
|
-
db
|
|
214
|
-
|
|
247
|
+
if (input.actorFence) {
|
|
248
|
+
return await db.transaction(async (tx) => {
|
|
249
|
+
const txDb = tx;
|
|
250
|
+
await rawRows(
|
|
251
|
+
txDb,
|
|
252
|
+
sql`select managed_auth_actor_mutation_fence(
|
|
253
|
+
${input.actorFence.authorityHash}, ${input.actorFence.actorEpoch}::bigint,
|
|
254
|
+
${input.actorFence.requestId}::uuid
|
|
255
|
+
)`
|
|
256
|
+
);
|
|
257
|
+
return await applyCanonicalHumanIdentityOperationInner(txDb, input);
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
return await applyCanonicalHumanIdentityOperationInner(db, input);
|
|
261
|
+
} catch (error) {
|
|
262
|
+
if (error instanceof CanonicalHumanIdentityConflictError || error instanceof CanonicalHumanIdentityOperationReuseError || error instanceof CanonicalHumanIdentityMutationInFlightError || error instanceof CanonicalHumanIdentityNotFoundError || error instanceof CanonicalHumanIdentityAuthorityError) {
|
|
263
|
+
throw error;
|
|
264
|
+
}
|
|
265
|
+
mapSqlError(error);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
async function applyCanonicalHumanIdentityOperationInner(db, input) {
|
|
269
|
+
const [row] = await rawRows(
|
|
270
|
+
db,
|
|
271
|
+
sql`
|
|
215
272
|
select
|
|
216
273
|
outcome,
|
|
217
274
|
identity_id as "identityId",
|
|
@@ -228,25 +285,21 @@ async function applyCanonicalHumanIdentityOperation(db, input) {
|
|
|
228
285
|
${input.reason}
|
|
229
286
|
)
|
|
230
287
|
`
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
} catch (error) {
|
|
240
|
-
if (error instanceof CanonicalHumanIdentityConflictError || error instanceof CanonicalHumanIdentityOperationReuseError || error instanceof CanonicalHumanIdentityNotFoundError || error instanceof CanonicalHumanIdentityAuthorityError) {
|
|
241
|
-
throw error;
|
|
242
|
-
}
|
|
243
|
-
mapSqlError(error);
|
|
244
|
-
}
|
|
288
|
+
);
|
|
289
|
+
if (!row) throw new CanonicalHumanIdentityNotFoundError();
|
|
290
|
+
const identity = await getCanonicalHumanIdentityProjection(db, input.authUserId);
|
|
291
|
+
return CanonicalHumanIdentityMutationResponse.parse({
|
|
292
|
+
outcome: row.outcome,
|
|
293
|
+
operationId: input.operationId,
|
|
294
|
+
identity
|
|
295
|
+
});
|
|
245
296
|
}
|
|
246
297
|
|
|
247
298
|
export {
|
|
299
|
+
getCanonicalHumanExactLoginBindingForAuthUser,
|
|
248
300
|
CanonicalHumanIdentityConflictError,
|
|
249
301
|
CanonicalHumanIdentityOperationReuseError,
|
|
302
|
+
CanonicalHumanIdentityMutationInFlightError,
|
|
250
303
|
CanonicalHumanIdentityNotFoundError,
|
|
251
304
|
CanonicalHumanIdentityAuthorityError,
|
|
252
305
|
ensureCanonicalHumanIdentity,
|
|
@@ -256,4 +309,4 @@ export {
|
|
|
256
309
|
getCanonicalHumanIdentityProjection,
|
|
257
310
|
applyCanonicalHumanIdentityOperation
|
|
258
311
|
};
|
|
259
|
-
//# sourceMappingURL=chunk-
|
|
312
|
+
//# sourceMappingURL=chunk-UGDVSQ6U.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/canonical-human-identities.ts"],"sourcesContent":["import { createHash } from \"node:crypto\";\nimport {\n CanonicalHumanIdentityMutationResponse,\n CanonicalHumanIdentityProjection,\n type CanonicalHumanIdentityMutationOutcome,\n type CanonicalHumanIdentityMutationResponse as CanonicalHumanIdentityMutationResponseType,\n type CanonicalHumanIdentityProjection as CanonicalHumanIdentityProjectionType,\n} from \"@opengeni/contracts/canonical-human-identities\";\nimport { sql } from \"drizzle-orm\";\nimport type { Database } from \"./database\";\nimport { rawRows } from \"./database\";\nimport { nestedPostgresSqlState } from \"./persistence-errors\";\n\nexport type CanonicalHumanSessionAuthority = {\n identityId: string;\n identityRevision: number;\n authRevision: number;\n identityStatus: \"active\" | \"recovery_required\" | \"disputed\" | \"disabled\";\n};\n\nexport type CanonicalHumanExactLoginBinding = {\n id: string;\n identityId: string;\n revision: number;\n status: \"active\" | \"recovery_pending\" | \"stale\" | \"disputed\" | \"revoked\";\n};\n\nexport async function getCanonicalHumanExactLoginBindingForAuthUser(\n db: Database,\n input: { authUserId: string; providerId: string },\n): Promise<CanonicalHumanExactLoginBinding> {\n const [row] = await rawRows<{\n result:\n | (Omit<CanonicalHumanExactLoginBinding, \"revision\"> & { revision: number | string })\n | null;\n }>(\n db,\n sql`\n select get_canonical_human_exact_login_binding(\n ${input.authUserId}, ${input.providerId}\n ) as result\n `,\n );\n if (!row?.result) {\n throw new CanonicalHumanIdentityAuthorityError(\n \"Authentication did not prove exactly one canonical login binding\",\n );\n }\n return { ...row.result, revision: Number(row.result.revision) };\n}\n\nexport class CanonicalHumanIdentityConflictError extends Error {\n readonly name = \"CanonicalHumanIdentityConflictError\";\n readonly code = \"CANONICAL_HUMAN_IDENTITY_CONFLICT\";\n\n constructor() {\n super(\"The canonical human identity changed in another request\");\n }\n}\n\nexport class CanonicalHumanIdentityOperationReuseError extends Error {\n readonly name = \"CanonicalHumanIdentityOperationReuseError\";\n readonly code = \"CANONICAL_HUMAN_IDENTITY_OPERATION_REUSED\";\n\n constructor() {\n super(\"The canonical human identity operation id was already used for another request\");\n }\n}\n\nexport class CanonicalHumanIdentityMutationInFlightError extends Error {\n readonly name = \"CanonicalHumanIdentityMutationInFlightError\";\n readonly code = \"CANONICAL_HUMAN_IDENTITY_MUTATION_IN_FLIGHT\";\n\n constructor() {\n super(\"Another request is still using this browser actor\");\n }\n}\n\nexport class CanonicalHumanIdentityNotFoundError extends Error {\n readonly name = \"CanonicalHumanIdentityNotFoundError\";\n\n constructor(message = \"Canonical human identity was not found\") {\n super(message);\n }\n}\n\nexport class CanonicalHumanIdentityAuthorityError extends Error {\n readonly name = \"CanonicalHumanIdentityAuthorityError\";\n}\n\nfunction deterministicOperationId(seed: string): string {\n const bytes = createHash(\"sha256\").update(seed, \"utf8\").digest().subarray(0, 16);\n bytes[6] = (bytes[6]! & 0x0f) | 0x50;\n bytes[8] = (bytes[8]! & 0x3f) | 0x80;\n const hex = bytes.toString(\"hex\");\n return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;\n}\n\nfunction projectionValue(value: unknown): unknown {\n if (typeof value === \"string\") return JSON.parse(value);\n return value;\n}\n\nfunction mapSqlError(error: unknown): never {\n const state = nestedPostgresSqlState(error);\n if (state === \"40001\") throw new CanonicalHumanIdentityConflictError();\n if (state === \"23505\") throw new CanonicalHumanIdentityOperationReuseError();\n if (state === \"55P03\") throw new CanonicalHumanIdentityMutationInFlightError();\n if (state === \"P0002\") throw new CanonicalHumanIdentityNotFoundError();\n if (state === \"42501\") {\n throw new CanonicalHumanIdentityAuthorityError(\n error instanceof Error ? error.message : \"Canonical human identity authority denied\",\n );\n }\n throw error;\n}\n\nexport async function ensureCanonicalHumanIdentity(\n db: Database,\n input: { authUserId: string; displayName: string },\n): Promise<CanonicalHumanSessionAuthority> {\n try {\n const [row] = await rawRows<{\n identityId: string;\n identityRevision: number | string;\n authRevision: number | string;\n identityStatus: CanonicalHumanSessionAuthority[\"identityStatus\"];\n }>(\n db,\n sql`\n select\n identity_id as \"identityId\",\n identity_revision as \"identityRevision\",\n auth_revision as \"authRevision\",\n identity_status as \"identityStatus\"\n from ensure_canonical_human_identity(${input.authUserId}, ${input.displayName})\n `,\n );\n if (!row) throw new CanonicalHumanIdentityNotFoundError();\n return {\n identityId: row.identityId,\n identityRevision: Number(row.identityRevision),\n authRevision: Number(row.authRevision),\n identityStatus: row.identityStatus,\n };\n } catch (error) {\n mapSqlError(error);\n }\n}\n\nexport async function ensureCanonicalHumanIdentityForAuthUser(\n db: Database,\n authUserId: string,\n): Promise<CanonicalHumanSessionAuthority> {\n try {\n const existing = await getCanonicalHumanIdentityProjection(db, authUserId);\n return {\n identityId: existing.activeIdentity.id,\n identityRevision: existing.activeIdentity.identityRevision,\n authRevision: existing.activeIdentity.authRevision,\n identityStatus: existing.activeIdentity.status,\n };\n } catch (error) {\n if (!(error instanceof CanonicalHumanIdentityNotFoundError)) throw error;\n }\n const [user] = await rawRows<{ displayName: string }>(\n db,\n sql`\n select coalesce(nullif(btrim(name), ''), email) as \"displayName\"\n from auth_users\n where id = ${authUserId}\n limit 1\n `,\n );\n if (!user) throw new CanonicalHumanIdentityNotFoundError(\"Authentication user was not found\");\n return await ensureCanonicalHumanIdentity(db, {\n authUserId,\n displayName: user.displayName,\n });\n}\n\nexport async function synchronizeCanonicalHumanLoginBindings(\n db: Database,\n authUserId: string,\n): Promise<CanonicalHumanSessionAuthority> {\n let authority = await ensureCanonicalHumanIdentityForAuthUser(db, authUserId);\n const accounts = await rawRows<{\n id: string;\n providerId: string;\n providerAccountId: string;\n }>(\n db,\n sql`\n select\n id,\n lower(provider_id) as \"providerId\",\n account_id as \"providerAccountId\"\n from auth_identities\n where user_id = ${authUserId}\n order by created_at, id\n `,\n );\n\n for (const account of accounts) {\n for (let attempt = 0; attempt < 4; attempt += 1) {\n const projection = await getCanonicalHumanIdentityProjection(db, authUserId);\n if (projection.activeIdentity.status !== \"active\") {\n return {\n identityId: projection.activeIdentity.id,\n identityRevision: projection.activeIdentity.identityRevision,\n authRevision: projection.activeIdentity.authRevision,\n identityStatus: projection.activeIdentity.status,\n };\n }\n const existing = projection.loginBindings.find(\n (binding) =>\n binding.providerId === account.providerId &&\n binding.providerAccountId === account.providerAccountId,\n );\n if (existing?.status === \"active\") {\n authority = {\n identityId: projection.activeIdentity.id,\n identityRevision: projection.activeIdentity.identityRevision,\n authRevision: projection.activeIdentity.authRevision,\n identityStatus: projection.activeIdentity.status,\n };\n break;\n }\n try {\n const result = await applyCanonicalHumanIdentityOperation(db, {\n operationId: deterministicOperationId(\n `canonical-human-account-link\\n${account.id}\\n${projection.activeIdentity.identityRevision}`,\n ),\n authUserId,\n expectedIdentityRevision: projection.activeIdentity.identityRevision,\n operationType: \"link\",\n providerId: account.providerId,\n providerAccountId: account.providerAccountId,\n reason: \"Synchronize verified authentication account\",\n });\n authority = {\n identityId: result.identity.activeIdentity.id,\n identityRevision: result.identity.activeIdentity.identityRevision,\n authRevision: result.identity.activeIdentity.authRevision,\n identityStatus: result.identity.activeIdentity.status,\n };\n break;\n } catch (error) {\n if (error instanceof CanonicalHumanIdentityConflictError && attempt < 3) continue;\n if (error instanceof CanonicalHumanIdentityAuthorityError) {\n const currentProjection = await getCanonicalHumanIdentityProjection(db, authUserId);\n if (currentProjection.activeIdentity.status !== \"active\") {\n return {\n identityId: currentProjection.activeIdentity.id,\n identityRevision: currentProjection.activeIdentity.identityRevision,\n authRevision: currentProjection.activeIdentity.authRevision,\n identityStatus: currentProjection.activeIdentity.status,\n };\n }\n }\n throw error;\n }\n }\n }\n return authority;\n}\n\nexport async function validateCanonicalHumanSession(\n db: Database,\n input: { authSessionId: string; authUserId: string; allowRecovery?: boolean },\n): Promise<boolean> {\n try {\n const [row] = await rawRows<{ valid: boolean }>(\n db,\n sql`\n select validate_canonical_human_session(\n ${input.authSessionId},\n ${input.authUserId},\n ${input.allowRecovery ?? false}\n ) as valid\n `,\n );\n return row?.valid === true;\n } catch (error) {\n mapSqlError(error);\n }\n}\n\nexport async function getCanonicalHumanIdentityProjection(\n db: Database,\n authUserId: string,\n): Promise<CanonicalHumanIdentityProjectionType> {\n try {\n const [row] = await rawRows<{ projection: unknown }>(\n db,\n sql`\n select get_canonical_human_identity_projection(${authUserId}) as projection\n `,\n );\n if (row?.projection === null || row?.projection === undefined) {\n throw new CanonicalHumanIdentityNotFoundError();\n }\n return CanonicalHumanIdentityProjection.parse(projectionValue(row.projection));\n } catch (error) {\n if (error instanceof CanonicalHumanIdentityNotFoundError) throw error;\n mapSqlError(error);\n }\n}\n\nexport type ApplyCanonicalHumanIdentityOperationInput = {\n operationId: string;\n authUserId: string;\n expectedIdentityRevision: number;\n operationType: \"link\" | \"unlink\" | \"begin_recovery\" | \"recover\";\n bindingId?: string | null;\n providerId?: string | null;\n providerAccountId?: string | null;\n reason: string;\n actorFence?: {\n authorityHash: string;\n actorEpoch: string;\n requestId: string;\n };\n};\n\nexport async function applyCanonicalHumanIdentityOperation(\n db: Database,\n input: ApplyCanonicalHumanIdentityOperationInput,\n): Promise<CanonicalHumanIdentityMutationResponseType> {\n try {\n if (input.actorFence) {\n return await db.transaction(async (tx) => {\n const txDb = tx as unknown as Database;\n await rawRows(\n txDb,\n sql`select managed_auth_actor_mutation_fence(\n ${input.actorFence!.authorityHash}, ${input.actorFence!.actorEpoch}::bigint,\n ${input.actorFence!.requestId}::uuid\n )`,\n );\n return await applyCanonicalHumanIdentityOperationInner(txDb, input);\n });\n }\n return await applyCanonicalHumanIdentityOperationInner(db, input);\n } catch (error) {\n if (\n error instanceof CanonicalHumanIdentityConflictError ||\n error instanceof CanonicalHumanIdentityOperationReuseError ||\n error instanceof CanonicalHumanIdentityMutationInFlightError ||\n error instanceof CanonicalHumanIdentityNotFoundError ||\n error instanceof CanonicalHumanIdentityAuthorityError\n ) {\n throw error;\n }\n mapSqlError(error);\n }\n}\n\nasync function applyCanonicalHumanIdentityOperationInner(\n db: Database,\n input: ApplyCanonicalHumanIdentityOperationInput,\n): Promise<CanonicalHumanIdentityMutationResponseType> {\n const [row] = await rawRows<{\n outcome: CanonicalHumanIdentityMutationOutcome;\n identityId: string;\n identityRevision: number | string;\n authRevision: number | string;\n }>(\n db,\n sql`\n select\n outcome,\n identity_id as \"identityId\",\n identity_revision as \"identityRevision\",\n auth_revision as \"authRevision\"\n from apply_canonical_human_identity_operation(\n ${input.operationId},\n ${input.authUserId},\n ${input.expectedIdentityRevision},\n ${input.operationType},\n ${input.bindingId ?? null},\n ${input.providerId ?? null},\n ${input.providerAccountId ?? null},\n ${input.reason}\n )\n `,\n );\n if (!row) throw new CanonicalHumanIdentityNotFoundError();\n const identity = await getCanonicalHumanIdentityProjection(db, input.authUserId);\n return CanonicalHumanIdentityMutationResponse.parse({\n outcome: row.outcome,\n operationId: input.operationId,\n identity,\n });\n}\n"],"mappings":";;;;;;AAAA,SAAS,kBAAkB;AAC3B;AAAA,EACE;AAAA,EACA;AAAA,OAIK;AACP,SAAS,WAAW;AAmBpB,eAAsB,8CACpB,IACA,OAC0C;AAC1C,QAAM,CAAC,GAAG,IAAI,MAAM;AA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