@tearleads/client-sdk 0.1.11 → 0.1.14
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 +50 -9
- package/client/Tearleads.d.ts +4 -0
- package/client/Tearleads.js +5 -0
- package/client/Tearleads.js.map +1 -1
- package/client/index.d.ts +1 -0
- package/client/index.js.map +1 -1
- package/client/principalHistoryProtection.d.ts +19 -0
- package/client/principalHistoryProtection.js +76 -0
- package/client/principalHistoryProtection.js.map +1 -0
- package/client/workflowRuntime.d.ts +5 -0
- package/client/workflowRuntime.js +34 -5
- package/client/workflowRuntime.js.map +1 -1
- package/data/containers/shared/authorAccess.d.ts +2 -2
- package/data/containers/shared/authorAccess.js.map +1 -1
- package/data/containers/shared/heldContainerHeads.js.map +1 -1
- package/data/containers/shared/principalPolicies.js.map +1 -1
- package/data/containers/shared/types.d.ts +3 -2
- package/data/containers/shared/types.js.map +1 -1
- package/data/keyingProjectionVerification/accessManifestCheckpointEnforcement.js.map +1 -1
- package/data/keyingProjectionVerification/checkpointContext.d.ts +4 -3
- package/data/keyingProjectionVerification/checkpointContext.js.map +1 -1
- package/data/keyingProjectionVerification/containerManifestVerification.js.map +1 -1
- package/data/keyingProjectionVerification/containerPathVerification.js.map +1 -1
- package/data/keyingProjectionVerification/containerProjectionVerification.d.ts +4 -3
- package/data/keyingProjectionVerification/containerProjectionVerification.js.map +1 -1
- package/data/keyingProjectionVerification/documentManifestPolicies.js.map +1 -1
- package/data/keyingProjectionVerification/documentManifestVerification.js.map +1 -1
- package/data/keyingProjectionVerification/documentProjectionVerification.d.ts +3 -2
- package/data/keyingProjectionVerification/documentProjectionVerification.js.map +1 -1
- package/data/keyingProjectionVerification/organizationFounderBinding.js +9 -4
- package/data/keyingProjectionVerification/organizationFounderBinding.js.map +1 -1
- package/data/keyingProjectionVerification/principalPolicyCache.js +3 -4
- package/data/keyingProjectionVerification/principalPolicyCache.js.map +1 -1
- package/data/keyingProjectionVerification/principalPolicyVerification.js +12 -2
- package/data/keyingProjectionVerification/principalPolicyVerification.js.map +1 -1
- package/data/keyingProjectionVerification/projectionAuthorizationEvidence.js.map +1 -1
- package/data/keyingProjectionVerification/resolvedPrincipalPolicy.js +70 -0
- package/data/keyingProjectionVerification/resolvedPrincipalPolicy.js.map +1 -0
- package/data/keyingProjectionVerification/types.d.ts +17 -2
- package/data/keyingProjectionVerification/types.js +12 -0
- package/data/keyingProjectionVerification/types.js.map +1 -1
- package/data/persistence/organizationFounderPersistence.js +3 -2
- package/data/persistence/organizationFounderPersistence.js.map +1 -1
- package/data/persistence/principalHistoryEvidencePersistence.js +91 -0
- package/data/persistence/principalHistoryEvidencePersistence.js.map +1 -0
- package/data/persistence/principalHistoryPrefixPersistence.js +52 -0
- package/data/persistence/principalHistoryPrefixPersistence.js.map +1 -0
- package/data/persistence/principalHistoryStagePersistence.js +9 -2
- package/data/persistence/principalHistoryStagePersistence.js.map +1 -1
- package/data/persistence/principalKeyEnvelopeCandidates.js +67 -0
- package/data/persistence/principalKeyEnvelopeCandidates.js.map +1 -0
- package/data/principals/principalHistoryPrefixProtection.js +29 -0
- package/data/principals/principalHistoryPrefixProtection.js.map +1 -0
- package/data/principals/principalHistoryProtection.d.ts +10 -0
- package/data/principals/principalHistoryProtection.js +14 -0
- package/data/principals/principalHistoryProtection.js.map +1 -0
- package/data/principals/principalHistoryStageProtection.js.map +1 -1
- package/data/principals/principalPolicyCrypto.js +11 -21
- package/data/principals/principalPolicyCrypto.js.map +1 -1
- package/data/principals/principalPolicyEvidence.d.ts +4 -0
- package/data/principals/principalPolicyEvidence.js +12 -0
- package/data/principals/principalPolicyEvidence.js.map +1 -0
- package/data/sqlite/principalHistoryEvidenceSchema.js +46 -0
- package/data/sqlite/principalHistoryEvidenceSchema.js.map +1 -0
- package/data/sqlite/principalKeyFingerprintJson.js +9 -0
- package/data/sqlite/principalKeyFingerprintJson.js.map +1 -0
- package/data/sqlite/principalPolicySchema.js +4 -0
- package/data/sqlite/principalPolicySchema.js.map +1 -1
- package/data/sqlite/schema.d.ts +304 -0
- package/data/sqlite/schema.js +5 -0
- package/data/sqlite/schema.js.map +1 -1
- package/data/sqlite/sqlTableSchema.js +1 -1
- package/data/sqlite/sqlTableSchema.js.map +1 -1
- package/index.d.ts +3 -3
- package/index.js +1 -1
- package/index.js.map +1 -1
- package/internal/api-client/principalPolicyPages.d.ts +2 -0
- package/internal/api-client/principalPolicyPages.js +11 -2
- package/internal/api-client/principalPolicyPages.js.map +1 -1
- package/internal/crypto/index.d.ts +1 -1
- package/internal/crypto/index.js +1 -1
- package/internal/crypto/index.js.map +1 -1
- package/internal/crypto/keying/index.d.ts +1 -1
- package/internal/crypto/keying/index.js +1 -1
- package/internal/crypto/keying/index.js.map +1 -1
- package/internal/crypto/keying/principalPolicyHistoryIndex.d.ts +6 -2
- package/internal/crypto/keying/principalPolicyHistoryIndex.js +11 -5
- package/internal/crypto/keying/principalPolicyHistoryIndex.js.map +1 -1
- package/internal/crypto/keying/principalPolicyHistoryReferences.d.ts +2 -0
- package/internal/crypto/keying/principalPolicyHistoryReferences.js +19 -2
- package/internal/crypto/keying/principalPolicyHistoryReferences.js.map +1 -1
- package/package.json +4 -1
- package/sqlite/sqlite3-licenses.md +38 -0
- package/sqlite/sqlite3.wasm +0 -0
- package/sqlite/worker.js +4 -0
- package/workflows/containers/child/create.js.map +1 -1
- package/workflows/containers/child/move.js.map +1 -1
- package/workflows/containers/child/moveWraps.js.map +1 -1
- package/workflows/containers/child/mutationRequestCore.js.map +1 -1
- package/workflows/containers/child/recitePlan.js.map +1 -1
- package/workflows/containers/child/rekey.d.ts +3 -2
- package/workflows/containers/child/rekey.js.map +1 -1
- package/workflows/containers/child/revoke.d.ts +2 -2
- package/workflows/containers/child/revoke.js.map +1 -1
- package/workflows/containers/child/rotationPrincipalPolicies.js.map +1 -1
- package/workflows/containers/child/rotationWraps.js.map +1 -1
- package/workflows/containers/child/sharePlanCore.d.ts +11 -10
- package/workflows/containers/child/sharePlanCore.js.map +1 -1
- package/workflows/documents/linkSetAuthority.js.map +1 -1
- package/workflows/principals/index.d.ts +1 -0
- package/workflows/principals/index.js +1 -0
- package/workflows/principals/index.js.map +1 -1
- package/workflows/principals/principalHistoryRecoveryPrefix.js +65 -0
- package/workflows/principals/principalHistoryRecoveryPrefix.js.map +1 -0
- package/workflows/principals/principalHistoryRecoveryReferences.js +60 -0
- package/workflows/principals/principalHistoryRecoveryReferences.js.map +1 -0
- package/workflows/principals/principalHistoryRecoveryStage.js +38 -2
- package/workflows/principals/principalHistoryRecoveryStage.js.map +1 -1
- package/workflows/principals/principalHistoryRecoveryTypes.d.ts +9 -1
- package/workflows/principals/principalHistoryRecoveryTypes.js +7 -0
- package/workflows/principals/principalHistoryRecoveryTypes.js.map +1 -1
- package/workflows/principals/principalHistoryRecoveryVerification.js +24 -0
- package/workflows/principals/principalHistoryRecoveryVerification.js.map +1 -0
- package/workflows/principals/principalRecoveryDirectory.d.ts +10 -0
- package/workflows/principals/principalRecoveryDirectory.js +107 -0
- package/workflows/principals/principalRecoveryDirectory.js.map +1 -0
- package/workflows/principals/principalRecoveryMemo.js +46 -0
- package/workflows/principals/principalRecoveryMemo.js.map +1 -0
- package/workflows/principals/principalRecoveryOutage.js +40 -0
- package/workflows/principals/principalRecoveryOutage.js.map +1 -0
- package/workflows/principals/principalRecoveryQueue.js +22 -0
- package/workflows/principals/principalRecoveryQueue.js.map +1 -0
- package/workflows/principals/recoverPrincipalPolicyHistory.d.ts +1 -0
- package/workflows/principals/recoverPrincipalPolicyHistory.js +108 -36
- package/workflows/principals/recoverPrincipalPolicyHistory.js.map +1 -1
- package/workflows/principals/recoverScopedPrincipalPolicyHistory.d.ts +13 -0
- package/workflows/principals/recoverScopedPrincipalPolicyHistory.js +150 -0
- package/workflows/principals/recoverScopedPrincipalPolicyHistory.js.map +1 -0
- package/workflows/principals/runtimePolicyRecovery.js +65 -0
- package/workflows/principals/runtimePolicyRecovery.js.map +1 -0
- package/workflows/principals/runtimePolicyWarmer.js +5 -1
- package/workflows/principals/runtimePolicyWarmer.js.map +1 -1
- package/workflows/sync/remoteResetPrincipalScope.js +10 -0
- package/workflows/sync/remoteResetPrincipalScope.js.map +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"principalHistoryStagePersistence.js","sourceRoot":"","sources":["principalHistoryStagePersistence.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AACtC,OAAO,EAAE,mCAAmC,EAAE,MAAM,uCAAuC,CAAC;AAC5F,OAAO,EACL,sBAAsB,EACtB,2BAA2B,GAC5B,MAAM,uCAAuC,CAAC;AAC/C,OAAO,EAAE,iCAAiC,EAAE,MAAM,oCAAoC,CAAC;AACvF,OAAO,EAAgB,eAAe,EAAE,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"principalHistoryStagePersistence.js","sourceRoot":"","sources":["principalHistoryStagePersistence.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AACtC,OAAO,EAAE,mCAAmC,EAAE,MAAM,uCAAuC,CAAC;AAC5F,OAAO,EAAE,8BAA8B,EAAE,MAAM,0CAA0C,CAAC;AAC1F,OAAO,EACL,sBAAsB,EACtB,2BAA2B,GAC5B,MAAM,uCAAuC,CAAC;AAC/C,OAAO,EAAE,iCAAiC,EAAE,MAAM,oCAAoC,CAAC;AACvF,OAAO,EAAgB,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACpE,OAAO,EAEL,iCAAiC,GAClC,MAAM,uCAAuC,CAAC;AAI/C,MAAM,iCAAkC,SAAQ,KAAK;IAC1C,IAAI,GAAG,iCAAiC,CAAC;IAClD;QACE,KAAK,CACH,sFAAsF,CACvF,CAAC;QACF,IAAI,CAAC,IAAI,GAAG,mCAAmC,CAAC;IAClD,CAAC;CACF;AAED,MAAM,CAAC,KAAK,UAAU,yBAAyB,CA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ACzD,MAAM,IAAI,iCAAiC,EAAE,CAAC;QAChD,MAAM,iCAAiC,CAAC,EAAE,EAAE,QAAQ,CAAC,CAAC;QACtD,MAAM,EAAE;aACL,MAAM,CAAC,sBAAsB,CAAC;aAC9B,MAAM,CAAC,KAAK,CAAC;aACb,kBAAkB,CAAC,EAAE,MAAM,EAAE,sBAAsB,CAAC,EAAE,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC;aACrE,GAAG,EAAE,CAAC;IACX,CAAC,EACD,KAAK,CAAC,YAAY,EAClB,EAAE,QAAQ,EAAE,WAAW,EAAE,CAC1B,CAAC;IACF,mCAAmC,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;AAC7D,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,4BAA4B,CAChD,OAAgB,EAChB,KAA4B,EAC5B,YAA2B;IAE3B,MAAM,OAAO,GAAG,iCAAiC,CAAC,OAAO,CAAC,CAAC;IAC3D,MAAM,SAAS,GAAG,MAAM,OAAO,CAAC,kBAAkB,CAChD,KAAK,EAAE,EAAE,EAAE,EAAE;QACX,MAAM,EAAE;aACL,MAAM,CAAC,sBAAsB,CAAC;aAC9B,KAAK,CACJ,GAAG,CACD,EAAE,CAAC,sBAAsB,CAAC,EAAE,EAAE,KAAK,CAAC,EAAE,CAAC,EACvC,EAAE,CAAC,sBAAsB,CAAC,QAAQ,EAAE,KAAK,CAAC,QAAQ,CAAC,CACpD,CACF;aACA,GAAG,EAAE,CAAC;IACX,CAAC,EACD,YAAY,EACZ,EAAE,QAAQ,EAAE,WAAW,EAAE,CAC1B,CAAC;IACF,mCAAmC,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC;AACjE,CAAC","sourcesContent":["import { and, eq } from \"drizzle-orm\";\nimport { assertProjectionVerificationCurrent } from \"../keyingProjectionVerification/types\";\nimport { principalHistoryEvidenceTables } from \"../sqlite/principalHistoryEvidenceSchema\";\nimport {\n principalHistoryStages,\n principalHistoryStageTables,\n} from \"../sqlite/principalHistoryStageSchema\";\nimport { getClientSQLitePersistenceRuntime } from \"../sqlite/sqlitePersistenceRuntime\";\nimport { type ExecSql, ensureSqlTables } from \"../sqlite/sqlSchema\";\nimport {\n type PrincipalHistoryEvidencePage,\n writePrincipalHistoryEvidencePage,\n} from \"./principalHistoryEvidencePersistence\";\n\nexport type PrincipalHistoryStage = typeof principalHistoryStages.$inferSelect;\n\nclass PrincipalHistoryStageChangedError extends Error {\n readonly code = \"principal_history_stage_changed\";\n constructor() {\n super(\n \"Another operation changed the saved principal history; resume from its current state\",\n );\n this.name = \"PrincipalHistoryStageChangedError\";\n }\n}\n\nexport async function loadPrincipalHistoryStage(execSql: ExecSql, id: string) {\n await ensureSqlTables(execSql, principalHistoryStageTables);\n const { db } = getClientSQLitePersistenceRuntime(execSql);\n const [row] = await db\n .select()\n .from(principalHistoryStages)\n .where(eq(principalHistoryStages.id, id))\n .limit(1);\n return row ?? null;\n}\n\n/** Publish accepted entries and index nodes atomically with authenticated progress. */\nexport async function savePrincipalHistoryStage(input: {\n readonly execSql: ExecSql;\n readonly stage: PrincipalHistoryStage;\n readonly evidence: PrincipalHistoryEvidencePage;\n readonly previousProgress: string | null;\n readonly stillCurrent: () => boolean;\n}): Promise<void> {\n const stage = { ...input.stage };\n const evidence = structuredClone(input.evidence);\n await ensureSqlTables(input.execSql, [\n ...principalHistoryStageTables,\n ...principalHistoryEvidenceTables,\n ]);\n const runtime = getClientSQLitePersistenceRuntime(input.execSql);\n const saved = await runtime.guardedTransaction(\n async (tx) => {\n const [previous] = await tx\n .select({ progress: principalHistoryStages.progress })\n .from(principalHistoryStages)\n .where(eq(principalHistoryStages.id, stage.id))\n .limit(1);\n if ((previous?.progress ?? null) !== input.previousProgress)\n throw new PrincipalHistoryStageChangedError();\n await writePrincipalHistoryEvidencePage(tx, evidence);\n await tx\n .insert(principalHistoryStages)\n .values(stage)\n .onConflictDoUpdate({ target: principalHistoryStages.id, set: stage })\n .run();\n },\n input.stillCurrent,\n { behavior: \"immediate\" },\n );\n assertProjectionVerificationCurrent(() => saved.committed);\n}\n\nexport async function discardPrincipalHistoryStage(\n execSql: ExecSql,\n stage: PrincipalHistoryStage,\n stillCurrent: () => boolean,\n) {\n const runtime = getClientSQLitePersistenceRuntime(execSql);\n const discarded = await runtime.guardedTransaction(\n async (db) => {\n await db\n .delete(principalHistoryStages)\n .where(\n and(\n eq(principalHistoryStages.id, stage.id),\n eq(principalHistoryStages.progress, stage.progress),\n ),\n )\n .run();\n },\n stillCurrent,\n { behavior: \"immediate\" },\n );\n assertProjectionVerificationCurrent(() => discarded.committed);\n}\n"]}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { isCurrentPrincipalMemberEnvelopesResponse, } from "../../internal/validators/response/index.js";
|
|
2
|
+
import { desc, eq, sql } from "drizzle-orm";
|
|
3
|
+
import { principalHistoryEvidenceTables, principalHistoryPrefixes, } from "../sqlite/principalHistoryEvidenceSchema.js";
|
|
4
|
+
import { principalCurrentFingerprintJson, principalStateFingerprintJson, } from "../sqlite/principalKeyFingerprintJson.js";
|
|
5
|
+
import { principalPolicies, principalPolicyBundleHistory, } from "../sqlite/principalPolicySchema.js";
|
|
6
|
+
import { principalPolicyTables } from "../sqlite/schema.js";
|
|
7
|
+
import { getClientSQLitePersistenceRuntime } from "../sqlite/sqlitePersistenceRuntime.js";
|
|
8
|
+
import { ensureSqlTables } from "../sqlite/sqlSchema.js";
|
|
9
|
+
/**
|
|
10
|
+
* Untrusted encrypted key candidates, never authorization evidence. The actual
|
|
11
|
+
* private key must open the requested recipient wrap; the projection verifier
|
|
12
|
+
* separately checks the signed KEK material identity and all policy authority.
|
|
13
|
+
* Read only envelopes for the requested fingerprints, never signed histories.
|
|
14
|
+
*/
|
|
15
|
+
export async function loadPrincipalKeyEnvelopeCandidates(execSql, fingerprints) {
|
|
16
|
+
await ensureSqlTables(execSql, [
|
|
17
|
+
...principalPolicyTables,
|
|
18
|
+
...principalHistoryEvidenceTables,
|
|
19
|
+
]);
|
|
20
|
+
const { db } = getClientSQLitePersistenceRuntime(execSql);
|
|
21
|
+
const candidates = [];
|
|
22
|
+
for (const fingerprint of new Set(fingerprints)) {
|
|
23
|
+
const encoded = [];
|
|
24
|
+
for (const table of [principalPolicies, principalPolicyBundleHistory]) {
|
|
25
|
+
const [row] = await db
|
|
26
|
+
.select({ envelopes: table.currentMemberEnvelopesJson })
|
|
27
|
+
.from(table)
|
|
28
|
+
.where(eq(principalStateFingerprintJson(table.currentStateJson), fingerprint))
|
|
29
|
+
.orderBy(desc(table.updatedAt))
|
|
30
|
+
.limit(1);
|
|
31
|
+
if (row)
|
|
32
|
+
encoded.push(row.envelopes);
|
|
33
|
+
}
|
|
34
|
+
const [prefix] = await db
|
|
35
|
+
.select({
|
|
36
|
+
envelopes: sql `json_extract(${principalHistoryPrefixes.currentJson}, '$.currentMemberEnvelopes')`,
|
|
37
|
+
})
|
|
38
|
+
.from(principalHistoryPrefixes)
|
|
39
|
+
.where(eq(principalCurrentFingerprintJson(principalHistoryPrefixes.currentJson), fingerprint))
|
|
40
|
+
.orderBy(desc(principalHistoryPrefixes.version))
|
|
41
|
+
.limit(1);
|
|
42
|
+
if (prefix)
|
|
43
|
+
encoded.push(prefix.envelopes);
|
|
44
|
+
for (const json of encoded) {
|
|
45
|
+
let envelopes;
|
|
46
|
+
try {
|
|
47
|
+
envelopes = JSON.parse(json);
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (!isCurrentPrincipalMemberEnvelopesResponse(envelopes))
|
|
53
|
+
continue;
|
|
54
|
+
candidates.push({
|
|
55
|
+
currentState: {
|
|
56
|
+
principalType: envelopes.principalType,
|
|
57
|
+
principalId: envelopes.principalId,
|
|
58
|
+
stateHash: envelopes.stateHash,
|
|
59
|
+
keyFingerprint: fingerprint,
|
|
60
|
+
},
|
|
61
|
+
currentMemberEnvelopes: envelopes,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return candidates;
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=principalKeyEnvelopeCandidates.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"principalKeyEnvelopeCandidates.js","sourceRoot":"","sources":["principalKeyEnvelopeCandidates.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,yCAAyC,GAC1C,MAAM,gCAAgC,CAAC;AACxC,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EACL,8BAA8B,EAC9B,wBAAwB,GACzB,MAAM,0CAA0C,CAAC;AAClD,OAAO,EACL,+BAA+B,EAC/B,6BAA6B,GAC9B,MAAM,uCAAuC,CAAC;AAC/C,OAAO,EACL,iBAAiB,EACjB,4BAA4B,GAC7B,MAAM,iCAAiC,CAAC;AACzC,OAAO,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AACzD,OAAO,EAAE,iCAAiC,EAAE,MAAM,oCAAoC,CAAC;AACvF,OAAO,EAAgB,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAYpE;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,kCAAkC,CACtD,OAAgB,EAChB,YAA+B;IAE/B,MAAM,eAAe,CAAC,OAAO,EAAE;QAC7B,GAAG,qBAAqB;QACxB,GAAG,8BAA8B;KAClC,CAAC,CAAC;IACH,MAAM,EAAE,EAAE,EAAE,GAAG,iCAAiC,CAAC,OAAO,CAAC,CAAC;IAC1D,MAAM,UAAU,GAAoC,EAAE,CAAC;IACvD,KAAK,MAAM,WAAW,IAAI,IAAI,GAAG,CAAC,YAAY,CAAC,EAAE,CAAC;QAChD,MAAM,OAAO,GAAa,EAAE,CAAC;QAC7B,KAAK,MAAM,KAAK,IAAI,CAAC,iBAAiB,EAAE,4BAA4B,CAAC,EAAE,CAAC;YACtE,MAAM,CAAC,GAAG,CAAC,GAAG,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BAClC,SAAS,EAAE,SAAS,CAAC,SAAS;oBAC9B,cAAc,EAAE,WAAW;iBAC5B;gBACD,sBAAsB,EAAE,SAAS;aAClC,CAAC,CAAC;QACL,CAAC;IACH,CAAC;IACD,OAAO,UAAU,CAAC;AACpB,CAAC","sourcesContent":["import {\n type CurrentPrincipalMemberEnvelopesResponse,\n isCurrentPrincipalMemberEnvelopesResponse,\n} from \"@tearleads/validators/response\";\nimport { desc, eq, sql } from \"drizzle-orm\";\nimport {\n principalHistoryEvidenceTables,\n principalHistoryPrefixes,\n} from \"../sqlite/principalHistoryEvidenceSchema\";\nimport {\n principalCurrentFingerprintJson,\n principalStateFingerprintJson,\n} from \"../sqlite/principalKeyFingerprintJson\";\nimport {\n principalPolicies,\n principalPolicyBundleHistory,\n} from \"../sqlite/principalPolicySchema\";\nimport { principalPolicyTables } from \"../sqlite/schema\";\nimport { getClientSQLitePersistenceRuntime } from \"../sqlite/sqlitePersistenceRuntime\";\nimport { type ExecSql, ensureSqlTables } from \"../sqlite/sqlSchema\";\n\nexport interface PrincipalKeyEnvelopeCandidate {\n readonly currentState: {\n readonly principalType: \"group\" | \"organization\";\n readonly principalId: string;\n readonly stateHash: string;\n readonly keyFingerprint: string;\n };\n readonly currentMemberEnvelopes: CurrentPrincipalMemberEnvelopesResponse;\n}\n\n/**\n * Untrusted encrypted key candidates, never authorization evidence. The actual\n * private key must open the requested recipient wrap; the projection verifier\n * separately checks the signed KEK material identity and all policy authority.\n * Read only envelopes for the requested fingerprints, never signed histories.\n */\nexport async function loadPrincipalKeyEnvelopeCandidates(\n execSql: ExecSql,\n fingerprints: readonly string[],\n): Promise<PrincipalKeyEnvelopeCandidate[]> {\n await ensureSqlTables(execSql, [\n ...principalPolicyTables,\n ...principalHistoryEvidenceTables,\n ]);\n const { db } = getClientSQLitePersistenceRuntime(execSql);\n const candidates: PrincipalKeyEnvelopeCandidate[] = [];\n for (const fingerprint of new Set(fingerprints)) {\n const encoded: string[] = [];\n for (const table of [principalPolicies, principalPolicyBundleHistory]) {\n const [row] = await db\n .select({ envelopes: table.currentMemberEnvelopesJson })\n .from(table)\n .where(\n eq(\n principalStateFingerprintJson(table.currentStateJson),\n fingerprint,\n ),\n )\n .orderBy(desc(table.updatedAt))\n .limit(1);\n if (row) encoded.push(row.envelopes);\n }\n const [prefix] = await db\n .select({\n envelopes: sql<string>`json_extract(${principalHistoryPrefixes.currentJson}, '$.currentMemberEnvelopes')`,\n })\n .from(principalHistoryPrefixes)\n .where(\n eq(\n principalCurrentFingerprintJson(principalHistoryPrefixes.currentJson),\n fingerprint,\n ),\n )\n .orderBy(desc(principalHistoryPrefixes.version))\n .limit(1);\n if (prefix) encoded.push(prefix.envelopes);\n for (const json of encoded) {\n let envelopes: unknown;\n try {\n envelopes = JSON.parse(json);\n } catch {\n continue;\n }\n if (!isCurrentPrincipalMemberEnvelopesResponse(envelopes)) continue;\n candidates.push({\n currentState: {\n principalType: envelopes.principalType,\n principalId: envelopes.principalId,\n stateHash: envelopes.stateHash,\n keyFingerprint: fingerprint,\n },\n currentMemberEnvelopes: envelopes,\n });\n }\n }\n return candidates;\n}\n"]}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { serializeKeyingCanonicalJson, toFingerprint, } from "../../internal/crypto/index.js";
|
|
2
|
+
// Signed entries and index nodes are content-addressed public evidence. Keep
|
|
3
|
+
// their storage namespace stable across local-key replacement; authenticating a
|
|
4
|
+
// prefix still requires the current private key. An unreadable prefix is discarded
|
|
5
|
+
// and rebuilt from signatures, reusing rows only after checking the new root.
|
|
6
|
+
export async function principalHistoryEvidenceScopeId(input) {
|
|
7
|
+
return toFingerprint(new TextEncoder().encode(serializeKeyingCanonicalJson([
|
|
8
|
+
"tearleads.sdk.principal-history-evidence.v2",
|
|
9
|
+
input.organizationId,
|
|
10
|
+
input.head.principalType,
|
|
11
|
+
input.head.principalId,
|
|
12
|
+
input.protection.context,
|
|
13
|
+
])));
|
|
14
|
+
}
|
|
15
|
+
export async function principalHistoryPrefixProtection(protection, prefix) {
|
|
16
|
+
return {
|
|
17
|
+
localKey: protection.localKey,
|
|
18
|
+
context: serializeKeyingCanonicalJson({
|
|
19
|
+
domain: "tearleads.sdk.principal-history-prefix.v2",
|
|
20
|
+
context: protection.context,
|
|
21
|
+
scopeId: prefix.scopeId,
|
|
22
|
+
organizationId: prefix.organizationId,
|
|
23
|
+
version: prefix.version,
|
|
24
|
+
headJson: prefix.headJson,
|
|
25
|
+
currentDigest: await toFingerprint(new TextEncoder().encode(prefix.currentJson)),
|
|
26
|
+
}),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=principalHistoryPrefixProtection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"principalHistoryPrefixProtection.js","sourceRoot":"","sources":["principalHistoryPrefixProtection.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,4BAA4B,EAC5B,aAAa,GACd,MAAM,mBAAmB,CAAC;AAG3B,6EAA6E;AAC7E,gFAAgF;AAChF,mFAAmF;AACnF,8EAA8E;AAC9E,MAAM,CAAC,KAAK,UAAU,+BAA+B,CAAC,KAIrD;IACC,OAAO,aAAa,CAClB,IAAI,WAAW,EAAE,CAAC,MAAM,CACtB,4BAA4B,CAAC;QAC3B,6CAA6C;QAC7C,KAAK,CAAC,cAAc;QACpB,KAAK,CAAC,IAAI,CAAC,aAAa;QACxB,KAAK,CAAC,IAAI,CAAC,WAAW;QACtB,KAAK,CAAC,UAAU,CAAC,OAAO;KACzB,CAAC,CACH,CACF,CAAC;AACJ,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,gCAAgC,CACpD,UAAiD,EACjD,MAAgD;IAEhD,OAAO;QACL,QAAQ,EAAE,UAAU,CAAC,QAAQ;QAC7B,OAAO,EAAE,4BAA4B,CAAC;YACpC,MAAM,EAAE,2CAA2C;YACnD,OAAO,EAAE,UAAU,CAAC,OAAO;YAC3B,OAAO,EAAE,MAAM,CAAC,OAAO;YACvB,cAAc,EAAE,MAAM,CAAC,cAAc;YACrC,OAAO,EAAE,MAAM,CAAC,OAAO;YACvB,QAAQ,EAAE,MAAM,CAAC,QAAQ;YACzB,aAAa,EAAE,MAAM,aAAa,CAChC,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,CAC7C;SACF,CAAC;KACH,CAAC;AACJ,CAAC","sourcesContent":["import {\n type PrincipalPolicyHistoryProgressOptions,\n type ReferencedPrincipalHead,\n serializeKeyingCanonicalJson,\n toFingerprint,\n} from \"@tearleads/crypto\";\nimport type { PrincipalHistoryPrefix } from \"../persistence/principalHistoryPrefixPersistence\";\n\n// Signed entries and index nodes are content-addressed public evidence. Keep\n// their storage namespace stable across local-key replacement; authenticating a\n// prefix still requires the current private key. An unreadable prefix is discarded\n// and rebuilt from signatures, reusing rows only after checking the new root.\nexport async function principalHistoryEvidenceScopeId(input: {\n readonly organizationId: string;\n readonly head: Pick<ReferencedPrincipalHead, \"principalType\" | \"principalId\">;\n readonly protection: PrincipalPolicyHistoryProgressOptions;\n}): Promise<string> {\n return toFingerprint(\n new TextEncoder().encode(\n serializeKeyingCanonicalJson([\n \"tearleads.sdk.principal-history-evidence.v2\",\n input.organizationId,\n input.head.principalType,\n input.head.principalId,\n input.protection.context,\n ]),\n ),\n );\n}\n\nexport async function principalHistoryPrefixProtection(\n protection: PrincipalPolicyHistoryProgressOptions,\n prefix: Omit<PrincipalHistoryPrefix, \"progress\">,\n): Promise<PrincipalPolicyHistoryProgressOptions> {\n return {\n localKey: protection.localKey,\n context: serializeKeyingCanonicalJson({\n domain: \"tearleads.sdk.principal-history-prefix.v2\",\n context: protection.context,\n scopeId: prefix.scopeId,\n organizationId: prefix.organizationId,\n version: prefix.version,\n headJson: prefix.headJson,\n currentDigest: await toFingerprint(\n new TextEncoder().encode(prefix.currentJson),\n ),\n }),\n };\n}\n"]}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type PrincipalPolicyHistoryProgressOptions } from "../../internal/crypto/index.js";
|
|
2
|
+
export type PrincipalHistoryProtectionLease = <T>(operation: (input: {
|
|
3
|
+
readonly protection: PrincipalPolicyHistoryProgressOptions;
|
|
4
|
+
readonly stillCurrent: () => boolean;
|
|
5
|
+
}) => Promise<T>) => Promise<T>;
|
|
6
|
+
/** Own private key bytes before any asynchronous discovery or verification. */
|
|
7
|
+
export declare function ownPrincipalHistoryProtection(protection: PrincipalPolicyHistoryProgressOptions): {
|
|
8
|
+
context: string;
|
|
9
|
+
localKey: Uint8Array<ArrayBuffer>;
|
|
10
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { KeyingVerificationError, } from "../../internal/crypto/index.js";
|
|
2
|
+
/** Own private key bytes before any asynchronous discovery or verification. */
|
|
3
|
+
export function ownPrincipalHistoryProtection(protection) {
|
|
4
|
+
if (!(protection.localKey instanceof Uint8Array) ||
|
|
5
|
+
protection.localKey.byteLength !== 32 ||
|
|
6
|
+
typeof protection.context !== "string" ||
|
|
7
|
+
protection.context.length === 0)
|
|
8
|
+
throw new KeyingVerificationError("invalid_shape", "Principal history recovery requires a private 32-byte key and trust context");
|
|
9
|
+
return {
|
|
10
|
+
context: protection.context,
|
|
11
|
+
localKey: new Uint8Array(protection.localKey),
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=principalHistoryProtection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"principalHistoryProtection.js","sourceRoot":"","sources":["principalHistoryProtection.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,GAExB,MAAM,mBAAmB,CAAC;AAS3B,+EAA+E;AAC/E,MAAM,UAAU,6BAA6B,CAC3C,UAAiD;IAEjD,IACE,CAAC,CAAC,UAAU,CAAC,QAAQ,YAAY,UAAU,CAAC;QAC5C,UAAU,CAAC,QAAQ,CAAC,UAAU,KAAK,EAAE;QACrC,OAAO,UAAU,CAAC,OAAO,KAAK,QAAQ;QACtC,UAAU,CAAC,OAAO,CAAC,MAAM,KAAK,CAAC;QAE/B,MAAM,IAAI,uBAAuB,CAC/B,eAAe,EACf,6EAA6E,CAC9E,CAAC;IACJ,OAAO;QACL,OAAO,EAAE,UAAU,CAAC,OAAO;QAC3B,QAAQ,EAAE,IAAI,UAAU,CAAC,UAAU,CAAC,QAAQ,CAAC;KAC9C,CAAC;AACJ,CAAC","sourcesContent":["import {\n KeyingVerificationError,\n type PrincipalPolicyHistoryProgressOptions,\n} from \"@tearleads/crypto\";\n\nexport type PrincipalHistoryProtectionLease = <T>(\n operation: (input: {\n readonly protection: PrincipalPolicyHistoryProgressOptions;\n readonly stillCurrent: () => boolean;\n }) => Promise<T>,\n) => Promise<T>;\n\n/** Own private key bytes before any asynchronous discovery or verification. */\nexport function ownPrincipalHistoryProtection(\n protection: PrincipalPolicyHistoryProgressOptions,\n) {\n if (\n !(protection.localKey instanceof Uint8Array) ||\n protection.localKey.byteLength !== 32 ||\n typeof protection.context !== \"string\" ||\n protection.context.length === 0\n )\n throw new KeyingVerificationError(\n \"invalid_shape\",\n \"Principal history recovery requires a private 32-byte key and trust context\",\n );\n return {\n context: protection.context,\n localKey: new Uint8Array(protection.localKey),\n };\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"principalHistoryStageProtection.js","sourceRoot":"","sources":["principalHistoryStageProtection.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,4BAA4B,EAC5B,aAAa,GACd,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,6BAA6B,EAAE,MAAM,gCAAgC,CAAC;AAG/E,MAAM,UAAU,uBAAuB,CACrC,cAAsB,EACtB,IAA6B,EAC7B,OAAe,EACf,kBAAsD;IAEtD,OAAO,aAAa,CAClB,IAAI,WAAW,EAAE,CAAC,MAAM,CACtB,4BAA4B,CAAC;QAC3B,0CAA0C;QAC1C,cAAc;QACd,IAAI,CAAC,aAAa;QAClB,IAAI,CAAC,WAAW;QAChB,IAAI,CAAC,OAAO;QACZ,IAAI,CAAC,QAAQ;QACb,IAAI,CAAC,SAAS;QACd,IAAI,CAAC,cAAc;QACnB,OAAO;QACP,CAAC,GAAG,kBAAkB,CAAC;aACpB,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,GAAG,CAAC,CAAC,OAAO,CAAC;aACrC,GAAG,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC;YAClB,SAAS,CAAC,aAAa;YACvB,SAAS,CAAC,WAAW;YACrB,SAAS,CAAC,OAAO;YACjB,SAAS,CAAC,QAAQ;YAClB,SAAS,CAAC,SAAS;YACnB,SAAS,CAAC,cAAc;SACzB,CAAC;KACL,CAAC,CACH,CACF,CAAC;AACJ,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,+BAA+B,CACnD,UAAiD,EACjD,YAAqC,EACrC,KAA8C;IAE9C,OAAO;QACL,QAAQ,EAAE,UAAU,CAAC,QAAQ;QAC7B,OAAO,EAAE,4BAA4B,CAAC;YACpC,MAAM,EAAE,0CAA0C;YAClD,OAAO,EAAE,UAAU,CAAC,OAAO;YAC3B,EAAE,EAAE,KAAK,CAAC,EAAE;YACZ,cAAc,EAAE,KAAK,CAAC,cAAc;YACpC,YAAY,EAAE,EAAE,GAAG,YAAY,EAAE;YACjC,YAAY,EAAE,KAAK,CAAC,YAAY;YAChC,QAAQ,EAAE,KAAK,CAAC,QAAQ;YACxB,aAAa,EAAE,MAAM,aAAa,CAChC,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,WAAW,CAAC,CAC5C;SACF,CAAC;KACH,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,iCAAiC,CAC/C,
|
|
1
|
+
{"version":3,"file":"principalHistoryStageProtection.js","sourceRoot":"","sources":["principalHistoryStageProtection.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,4BAA4B,EAC5B,aAAa,GACd,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EA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AC,MAAM,CAAC,KAAK,CAAC,WAAW,CAAC,CAC5C;SACF,CAAC;KACH,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,iCAAiC,CAC/C,KAAkE;IAElE,IAAI,OAAgB,CAAC;IACrB,IAAI,CAAC;QACH,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;IAC1C,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;IACD,IAAI,CAAC,OAAO,IAAI,OAAO,OAAO,KAAK,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC;QACnE,OAAO,IAAI,CAAC;IACd,MAAM,IAAI,GAAG;QACX,GAAG,OAAO;QACV,cAAc,EAAE,EAAE;QAClB,WAAW,EAAE,EAAE,YAAY,EAAE,KAAK,CAAC,YAAY,EAAE,gBAAgB,EAAE,IAAI,EAAE;KAC1E,CAAC;IACF,IACE,CAAC,6BAA6B,CAAC,IAAI,CAAC;QACpC,KAAK,CAAC,YAAY,IAAI,IAAI,CAAC,YAAY,CAAC,OAAO;QAE/C,OAAO,IAAI,CAAC;IACd,OAAO;QACL,YAAY,EAAE,IAAI,CAAC,YAAY;QAC/B,cAAc,EAAE,IAAI,CAAC,cAAc;QACnC,iBAAiB,EAAE,IAAI,CAAC,iBAAiB;QACzC,aAAa,EAAE,IAAI,CAAC,aAAa;QACjC,sBAAsB,EAAE,IAAI,CAAC,sBAAsB;KACpD,CAAC;AACJ,CAAC","sourcesContent":["import type { PrincipalPolicyPageCurrent } from \"@tearleads/api-client\";\nimport {\n type PrincipalPolicyHistoryProgressOptions,\n type ReferencedPrincipalHead,\n serializeKeyingCanonicalJson,\n toFingerprint,\n} from \"@tearleads/crypto\";\nimport { isPrincipalPolicyPageResponse } from \"@tearleads/validators/response\";\nimport type { PrincipalHistoryStage } from \"../persistence/principalHistoryStagePersistence\";\n\nexport function principalHistoryStageId(\n organizationId: string,\n head: ReferencedPrincipalHead,\n context: string,\n retainedReferences: readonly ReferencedPrincipalHead[],\n) {\n return toFingerprint(\n new TextEncoder().encode(\n serializeKeyingCanonicalJson([\n \"tearleads.sdk.principal-history-stage.v1\",\n organizationId,\n head.principalType,\n head.principalId,\n head.version,\n head.keyEpoch,\n head.stateHash,\n head.keyFingerprint,\n context,\n [...retainedReferences]\n .sort((a, b) => a.version - b.version)\n .map((reference) => [\n reference.principalType,\n reference.principalId,\n reference.version,\n reference.keyEpoch,\n reference.stateHash,\n reference.keyFingerprint,\n ]),\n ]),\n ),\n );\n}\n\nexport async function principalHistoryStageProtection(\n protection: PrincipalPolicyHistoryProgressOptions,\n expectedHead: ReferencedPrincipalHead,\n stage: Omit<PrincipalHistoryStage, \"progress\">,\n): Promise<PrincipalPolicyHistoryProgressOptions> {\n return {\n localKey: protection.localKey,\n context: serializeKeyingCanonicalJson({\n domain: \"tearleads.sdk.principal-history-stage.v1\",\n context: protection.context,\n id: stage.id,\n organizationId: stage.organizationId,\n expectedHead: { ...expectedHead },\n afterVersion: stage.afterVersion,\n complete: stage.complete,\n currentDigest: await toFingerprint(\n new TextEncoder().encode(stage.currentJson),\n ),\n }),\n };\n}\n\nexport function parsePrincipalHistoryStageCurrent(\n stage: Pick<PrincipalHistoryStage, \"currentJson\" | \"afterVersion\">,\n): PrincipalPolicyPageCurrent | null {\n let current: unknown;\n try {\n current = JSON.parse(stage.currentJson);\n } catch {\n return null;\n }\n if (!current || typeof current !== \"object\" || Array.isArray(current))\n return null;\n const page = {\n ...current,\n previousStates: [],\n historyPage: { afterVersion: stage.afterVersion, nextAfterVersion: null },\n };\n if (\n !isPrincipalPolicyPageResponse(page) ||\n stage.afterVersion >= page.currentState.version\n )\n return null;\n return {\n currentState: page.currentState,\n currentPayload: page.currentPayload,\n currentProjection: page.currentProjection,\n currentGrants: page.currentGrants,\n currentMemberEnvelopes: page.currentMemberEnvelopes,\n };\n}\n"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { unwrapDek } from "../../internal/crypto/index.js";
|
|
2
2
|
import { base64ToBytes } from "../../internal/encoding/index.js";
|
|
3
|
-
import {
|
|
3
|
+
import { loadPrincipalKeyEnvelopeCandidates, } from "../persistence/principalKeyEnvelopeCandidates.js";
|
|
4
4
|
function toKeyEnvelopeEntries(envelopes) {
|
|
5
5
|
return envelopes.map((envelope) => ({
|
|
6
6
|
keyFingerprint: envelope.keyFingerprint,
|
|
@@ -18,8 +18,8 @@ function toMemberEnvelopeEntries(envelopes) {
|
|
|
18
18
|
function principalBundleKey(bundle) {
|
|
19
19
|
return `${bundle.principalType}:${bundle.principalId}`;
|
|
20
20
|
}
|
|
21
|
-
async function createPrincipalPolicyResolutionContext(execSql) {
|
|
22
|
-
const bundles = await
|
|
21
|
+
async function createPrincipalPolicyResolutionContext(execSql, fingerprints) {
|
|
22
|
+
const bundles = await loadPrincipalKeyEnvelopeCandidates(execSql, fingerprints);
|
|
23
23
|
const bundlesByKeyFingerprint = new Map();
|
|
24
24
|
for (const bundle of bundles) {
|
|
25
25
|
const matchingBundles = bundlesByKeyFingerprint.get(bundle.currentState.keyFingerprint) ?? [];
|
|
@@ -28,21 +28,13 @@ async function createPrincipalPolicyResolutionContext(execSql) {
|
|
|
28
28
|
}
|
|
29
29
|
return {
|
|
30
30
|
bundlesByKeyFingerprint,
|
|
31
|
-
resolvedPrincipalSecretKeys: new Map(),
|
|
32
31
|
};
|
|
33
32
|
}
|
|
34
|
-
async function unwrapPrincipalSecretKey(bundle, secretKey
|
|
33
|
+
async function unwrapPrincipalSecretKey(bundle, secretKey) {
|
|
35
34
|
const principalKey = principalBundleKey(bundle.currentState);
|
|
36
|
-
const policyEpochKey = `${principalKey}:${bundle.currentState.stateHash}`;
|
|
37
|
-
const cachedSecretKey = context.resolvedPrincipalSecretKeys.get(policyEpochKey) ?? null;
|
|
38
|
-
if (cachedSecretKey) {
|
|
39
|
-
return cachedSecretKey;
|
|
40
|
-
}
|
|
41
35
|
const memberEnvelopeEntries = toMemberEnvelopeEntries(bundle.currentMemberEnvelopes.envelopes);
|
|
42
36
|
try {
|
|
43
|
-
|
|
44
|
-
context.resolvedPrincipalSecretKeys.set(policyEpochKey, resolvedSecretKey);
|
|
45
|
-
return resolvedSecretKey;
|
|
37
|
+
return await unwrapDek(memberEnvelopeEntries, secretKey);
|
|
46
38
|
}
|
|
47
39
|
catch {
|
|
48
40
|
// No transitive fallback: a principal's envelopes are all addressed to
|
|
@@ -61,18 +53,16 @@ export async function unwrapKeyEnvelopesWithPrincipalPolicies(input) {
|
|
|
61
53
|
throw new Error("No matching key envelope entry found for this secret key and no principal policy cache is available");
|
|
62
54
|
}
|
|
63
55
|
}
|
|
64
|
-
const context = await createPrincipalPolicyResolutionContext(input.execSql);
|
|
65
|
-
const
|
|
56
|
+
const context = await createPrincipalPolicyResolutionContext(input.execSql, input.envelopes.map((envelope) => envelope.keyFingerprint));
|
|
57
|
+
const attemptedFingerprints = new Set();
|
|
66
58
|
for (const envelope of input.envelopes) {
|
|
59
|
+
if (attemptedFingerprints.has(envelope.keyFingerprint))
|
|
60
|
+
continue;
|
|
61
|
+
attemptedFingerprints.add(envelope.keyFingerprint);
|
|
67
62
|
const candidateBundles = context.bundlesByKeyFingerprint.get(envelope.keyFingerprint) ?? [];
|
|
68
63
|
for (const bundle of candidateBundles) {
|
|
69
|
-
const principalKey = principalBundleKey(bundle.currentState);
|
|
70
|
-
if (attemptedPrincipalKeys.has(principalKey)) {
|
|
71
|
-
continue;
|
|
72
|
-
}
|
|
73
|
-
attemptedPrincipalKeys.add(principalKey);
|
|
74
64
|
try {
|
|
75
|
-
const principalSecretKey = await unwrapPrincipalSecretKey(bundle, input.secretKey
|
|
65
|
+
const principalSecretKey = await unwrapPrincipalSecretKey(bundle, input.secretKey);
|
|
76
66
|
return await unwrapDek(keyEntries, principalSecretKey);
|
|
77
67
|
}
|
|
78
68
|
catch { }
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"principalPolicyCrypto.js","sourceRoot":"","sources":["principalPolicyCrypto.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,SAAS,EAAE,MAAM,mBAAmB,CAAC;AACnE,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"principalPolicyCrypto.js","sourceRoot":"","sources":["principalPolicyCrypto.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,SAAS,EAAE,MAAM,mBAAmB,CAAC;AACnE,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAGpD,OAAO,EACL,kCAAkC,GAEnC,MAAM,+CAA+C,CAAC;AAUvD,SAAS,oBAAoB,CAC3B,SAA+C;IAE/C,OAAO,SAAS,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;QAClC,cAAc,EAAE,QAAQ,CAAC,cAAc;QACvC,aAAa,EAAE,aAAa,CAAC,QAAQ,CAAC,aAAa,CAAC;QACpD,UAAU,EAAE,aAAa,CAAC,QAAQ,CAAC,UAAU,CAAC;KAC/C,CAAC,CAAC,CAAC;AACN,CAAC;AAED,SAAS,uBAAuB,CAC9B,SAAyD;IAEzD,OAAO,SAAS,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;QAClC,cAAc,EAAE,QAAQ,CAAC,oBAAoB;QAC7C,aAAa,EAAE,aAAa,CAAC,QAAQ,CAAC,aAAa,CAAC;QACpD,UAAU,EAAE,aAAa,CAAC,QAAQ,CAAC,UAAU,CAAC;KAC/C,CAAC,CAAC,CAAC;AACN,CAAC;AAED,SAAS,kBAAkB,CACzB,MAGC;IAED,OAAO,GAAG,MAAM,CAAC,aAAa,IAAI,MAAM,CAAC,WAAW,EAAE,CAAC;AACzD,CAAC;AAED,KAAK,UAAU,sCAAsC,CACnD,OAAgB,EAChB,YAA+B;IAE/B,MAAM,OAAO,GAAG,MAAM,kCAAkC,CACtD,OAAO,EACP,YAAY,CACb,CAAC;IACF,MAAM,uBAAuB,G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AAK,CAAC,OAAO,EACb,KAAK,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,cAAc,CAAC,CAC3D,CAAC;IACF,MAAM,qBAAqB,GAAG,IAAI,GAAG,EAAU,CAAC;IAEhD,KAAK,MAAM,QAAQ,IAAI,KAAK,CAAC,SAAS,EAAE,CAAC;QACvC,IAAI,qBAAqB,CAAC,GAAG,CAAC,QAAQ,CAAC,cAAc,CAAC;YAAE,SAAS;QACjE,qBAAqB,CAAC,GAAG,CAAC,QAAQ,CAAC,cAAc,CAAC,CAAC;QACnD,MAAM,gBAAgB,GACpB,OAAO,CAAC,uBAAuB,CAAC,GAAG,CAAC,QAAQ,CAAC,cAAc,CAAC,IAAI,EAAE,CAAC;QAErE,KAAK,MAAM,MAAM,IAAI,gBAAgB,EAAE,CAAC;YACtC,IAAI,CAAC;gBACH,MAAM,kBAAkB,GAAG,MAAM,wBAAwB,CACvD,MAAM,EACN,KAAK,CAAC,SAAS,CAChB,CAAC;gBAEF,OAAO,MAAM,SAAS,CAAC,UAAU,EAAE,kBAAkB,CAAC,CAAC;YACzD,CAAC;YAAC,MAAM,CAAC,CAAA,CAAC;QACZ,CAAC;IACH,CAAC;IAED,MAAM,IAAI,KAAK,CACb,uFAAuF,CACxF,CAAC;AACJ,CAAC","sourcesContent":["import { type RecipientEntry, unwrapDek } from \"@tearleads/crypto\";\nimport { base64ToBytes } from \"@tearleads/encoding\";\nimport type { PrincipalMemberEnvelopeResponse } from \"@tearleads/validators/response\";\nimport type { SerializedKeyEnvelope } from \"@tearleads/validators/util\";\nimport {\n loadPrincipalKeyEnvelopeCandidates,\n type PrincipalKeyEnvelopeCandidate,\n} from \"../persistence/principalKeyEnvelopeCandidates\";\nimport type { ExecSql } from \"../sqlite/sqlSchema\";\n\ninterface PrincipalPolicyResolutionContext {\n bundlesByKeyFingerprint: ReadonlyMap<\n string,\n ReadonlyArray<PrincipalKeyEnvelopeCandidate>\n >;\n}\n\nfunction toKeyEnvelopeEntries(\n envelopes: ReadonlyArray<SerializedKeyEnvelope>,\n): RecipientEntry[] {\n return envelopes.map((envelope) => ({\n keyFingerprint: envelope.keyFingerprint,\n kemCipherText: base64ToBytes(envelope.kemCipherText),\n wrappedKey: base64ToBytes(envelope.wrappedKey),\n }));\n}\n\nfunction toMemberEnvelopeEntries(\n envelopes: ReadonlyArray<PrincipalMemberEnvelopeResponse>,\n): RecipientEntry[] {\n return envelopes.map((envelope) => ({\n keyFingerprint: envelope.memberKeyFingerprint,\n kemCipherText: base64ToBytes(envelope.kemCipherText),\n wrappedKey: base64ToBytes(envelope.wrappedKey),\n }));\n}\n\nfunction principalBundleKey(\n bundle: Pick<\n PrincipalKeyEnvelopeCandidate[\"currentState\"],\n \"principalType\" | \"principalId\"\n >,\n): string {\n return `${bundle.principalType}:${bundle.principalId}`;\n}\n\nasync function createPrincipalPolicyResolutionContext(\n execSql: ExecSql,\n fingerprints: readonly string[],\n): Promise<PrincipalPolicyResolutionContext> {\n const bundles = await loadPrincipalKeyEnvelopeCandidates(\n execSql,\n fingerprints,\n );\n const bundlesByKeyFingerprint = new Map<\n string,\n PrincipalKeyEnvelopeCandidate[]\n >();\n\n for (const bundle of bundles) {\n const matchingBundles =\n bundlesByKeyFingerprint.get(bundle.currentState.keyFingerprint) ?? [];\n matchingBundles.push(bundle);\n bundlesByKeyFingerprint.set(\n bundle.currentState.keyFingerprint,\n matchingBundles,\n );\n }\n\n return {\n bundlesByKeyFingerprint,\n };\n}\n\nasync function unwrapPrincipalSecretKey(\n bundle: PrincipalKeyEnvelopeCandidate,\n secretKey: Uint8Array,\n): Promise<Uint8Array> {\n const principalKey = principalBundleKey(bundle.currentState);\n const memberEnvelopeEntries = toMemberEnvelopeEntries(\n bundle.currentMemberEnvelopes.envelopes,\n );\n\n try {\n return await unwrapDek(memberEnvelopeEntries, secretKey);\n } catch {\n // No transitive fallback: a principal's envelopes are all addressed to\n // users directly, so if this identity key opens none of them the requester\n // is not a member.\n }\n\n throw new Error(`No matching principal member envelope for ${principalKey}`);\n}\n\nexport async function unwrapKeyEnvelopesWithPrincipalPolicies(input: {\n envelopes: ReadonlyArray<SerializedKeyEnvelope>;\n execSql?: ExecSql | undefined;\n secretKey: Uint8Array;\n}): Promise<Uint8Array> {\n const keyEntries = toKeyEnvelopeEntries(input.envelopes);\n\n try {\n return await unwrapDek(keyEntries, input.secretKey);\n } catch {\n if (!input.execSql) {\n throw new Error(\n \"No matching key envelope entry found for this secret key and no principal policy cache is available\",\n );\n }\n }\n\n const context = await createPrincipalPolicyResolutionContext(\n input.execSql,\n input.envelopes.map((envelope) => envelope.keyFingerprint),\n );\n const attemptedFingerprints = new Set<string>();\n\n for (const envelope of input.envelopes) {\n if (attemptedFingerprints.has(envelope.keyFingerprint)) continue;\n attemptedFingerprints.add(envelope.keyFingerprint);\n const candidateBundles =\n context.bundlesByKeyFingerprint.get(envelope.keyFingerprint) ?? [];\n\n for (const bundle of candidateBundles) {\n try {\n const principalSecretKey = await unwrapPrincipalSecretKey(\n bundle,\n input.secretKey,\n );\n\n return await unwrapDek(keyEntries, principalSecretKey);\n } catch {}\n }\n }\n\n throw new Error(\n \"No matching key envelope entry found for this secret key or cached principal policies\",\n );\n}\n"]}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { AnyVerifiedPrincipalPolicy, VerifiedPrincipalPolicy, VerifiedPrincipalPolicyCurrent } from "../../internal/crypto/index.js";
|
|
2
|
+
export type PrincipalPolicyCurrentEvidence = VerifiedPrincipalPolicy | VerifiedPrincipalPolicyCurrent;
|
|
3
|
+
export type PrincipalPolicyCheckpointEvidence = AnyVerifiedPrincipalPolicy | VerifiedPrincipalPolicyCurrent;
|
|
4
|
+
export declare function principalPolicyEvidenceEntries(policy: PrincipalPolicyCheckpointEvidence): NonNullable<VerifiedPrincipalPolicy["history"]>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export function principalPolicyEvidenceEntries(policy) {
|
|
2
|
+
return "retainedHistory" in policy
|
|
3
|
+
? policy.retainedHistory
|
|
4
|
+
: (policy.history ?? [
|
|
5
|
+
{
|
|
6
|
+
state: policy.state,
|
|
7
|
+
projection: policy.projection,
|
|
8
|
+
grants: policy.grants,
|
|
9
|
+
},
|
|
10
|
+
]);
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=principalPolicyEvidence.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"principalPolicyEvidence.js","sourceRoot":"","sources":["principalPolicyEvidence.ts"],"names":[],"mappings":"AAaA,MAAM,UAAU,8BAA8B,CAC5C,MAAyC;IAEzC,OAAO,iBAAiB,IAAI,MAAM;QAChC,CAAC,CAAC,MAAM,CAAC,eAAe;QACxB,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,IAAI;YACjB;gBACE,KAAK,EAAE,MAAM,CAAC,KAAK;gBACnB,UAAU,EAAE,MAAM,CAAC,UAAU;gBAC7B,MAAM,EAAE,MAAM,CAAC,MAAM;aACtB;SACF,CAAC,CAAC;AACT,CAAC","sourcesContent":["import type {\n AnyVerifiedPrincipalPolicy,\n VerifiedPrincipalPolicy,\n VerifiedPrincipalPolicyCurrent,\n} from \"@tearleads/crypto\";\n\nexport type PrincipalPolicyCurrentEvidence =\n | VerifiedPrincipalPolicy\n | VerifiedPrincipalPolicyCurrent;\nexport type PrincipalPolicyCheckpointEvidence =\n | AnyVerifiedPrincipalPolicy\n | VerifiedPrincipalPolicyCurrent;\n\nexport function principalPolicyEvidenceEntries(\n policy: PrincipalPolicyCheckpointEvidence,\n): NonNullable<VerifiedPrincipalPolicy[\"history\"]> {\n return \"retainedHistory\" in policy\n ? policy.retainedHistory\n : (policy.history ?? [\n {\n state: policy.state,\n projection: policy.projection,\n grants: policy.grants,\n },\n ]);\n}\n"]}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { desc } from "drizzle-orm";
|
|
2
|
+
import { index, integer, primaryKey, sqliteTable, text, } from "drizzle-orm/sqlite-core";
|
|
3
|
+
import { principalCurrentFingerprintJson } from "./principalKeyFingerprintJson.js";
|
|
4
|
+
import { defineSqlTableSchema } from "./sqlTableSchema.js";
|
|
5
|
+
/** A completed, locally authenticated prefix; never an application trust pin. */
|
|
6
|
+
export const principalHistoryPrefixes = sqliteTable("principal_history_prefixes", {
|
|
7
|
+
scopeId: text("scope_id").primaryKey(),
|
|
8
|
+
organizationId: text("organization_id").notNull(),
|
|
9
|
+
version: integer("version").notNull(),
|
|
10
|
+
headJson: text("head_json").notNull(),
|
|
11
|
+
currentJson: text("current_json").notNull(),
|
|
12
|
+
progress: text("progress").notNull(),
|
|
13
|
+
}, (table) => [
|
|
14
|
+
index("principal_history_prefixes_key_fingerprint_idx").on(principalCurrentFingerprintJson(table.currentJson), desc(table.version)),
|
|
15
|
+
index("principal_history_prefixes_organization_idx").on(table.organizationId),
|
|
16
|
+
]);
|
|
17
|
+
/** Entries are selected by their exact signed leaf, including signature bytes. */
|
|
18
|
+
export const principalHistoryEntries = sqliteTable("principal_history_entries", {
|
|
19
|
+
scopeId: text("scope_id").notNull(),
|
|
20
|
+
organizationId: text("organization_id").notNull(),
|
|
21
|
+
leafHash: text("leaf_hash").notNull(),
|
|
22
|
+
entryJson: text("entry_json").notNull(),
|
|
23
|
+
}, (table) => [
|
|
24
|
+
primaryKey({ columns: [table.scopeId, table.leafHash] }),
|
|
25
|
+
index("principal_history_entries_organization_idx").on(table.organizationId),
|
|
26
|
+
]);
|
|
27
|
+
/** Untrusted proof material, checked against a locally restored private root. */
|
|
28
|
+
export const principalHistoryNodes = sqliteTable("principal_history_nodes", {
|
|
29
|
+
scopeId: text("scope_id").notNull(),
|
|
30
|
+
organizationId: text("organization_id").notNull(),
|
|
31
|
+
hash: text("hash").notNull(),
|
|
32
|
+
leftHash: text("left_hash").notNull(),
|
|
33
|
+
rightHash: text("right_hash").notNull(),
|
|
34
|
+
}, (table) => [
|
|
35
|
+
primaryKey({ columns: [table.scopeId, table.hash] }),
|
|
36
|
+
index("principal_history_nodes_organization_idx").on(table.organizationId),
|
|
37
|
+
]);
|
|
38
|
+
export const principalHistoryEvidenceTables = [
|
|
39
|
+
{
|
|
40
|
+
...defineSqlTableSchema(principalHistoryPrefixes),
|
|
41
|
+
requiredColumns: ["current_json"],
|
|
42
|
+
},
|
|
43
|
+
defineSqlTableSchema(principalHistoryEntries),
|
|
44
|
+
defineSqlTableSchema(principalHistoryNodes),
|
|
45
|
+
];
|
|
46
|
+
//# sourceMappingURL=principalHistoryEvidenceSchema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"principalHistoryEvidenceSchema.js","sourceRoot":"","sources":["principalHistoryEvidenceSchema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AACnC,OAAO,EACL,KAAK,EACL,OAAO,EACP,UAAU,EACV,WAAW,EACX,IAAI,GACL,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,+BAA+B,EAAE,MAAM,+BAA+B,CAAC;AAChF,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AAExD,iFAAiF;AACjF,MAAM,CAAC,MAAM,wBAAwB,GAAG,WAAW,CACjD,4BAA4B,EAC5B;IACE,OAAO,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC,UAAU,EAAE;IACtC,cAAc,EAAE,IAAI,CAAC,iBAAiB,CAAC,CAAC,OAAO,EAAE;IACjD,OAAO,EAAE,OAAO,CAAC,SAAS,CAAC,CAAC,OAAO,EAAE;IACrC,QAAQ,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC,OAAO,EAAE;IACrC,WAAW,EAAE,IAAI,CAAC,cAAc,CAAC,CAAC,OAAO,EAAE;IAC3C,QAAQ,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC,OAAO,EAAE;CACrC,EACD,CAAC,KAAK,EAAE,EAAE,CAAC;IACT,KAAK,CAAC,gDAAgD,CAAC,CAAC,EAAE,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AK,CAAC,IAAI,CAAC,EAAE,CAAC;IACpD,KAAK,CAAC,0CAA0C,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,cAAc,CAAC;CAC3E,CACF,CAAC;AAEF,MAAM,CAAC,MAAM,8BAA8B,GAAG;IAC5C;QACE,GAAG,oBAAoB,CAAC,wBAAwB,CAAC;QACjD,eAAe,EAAE,CAAC,cAAc,CAAC;KAClC;IACD,oBAAoB,CAAC,uBAAuB,CAAC;IAC7C,oBAAoB,CAAC,qBAAqB,CAAC;CAC5C,CAAC","sourcesContent":["import { desc } from \"drizzle-orm\";\nimport {\n index,\n integer,\n primaryKey,\n sqliteTable,\n text,\n} from \"drizzle-orm/sqlite-core\";\nimport { principalCurrentFingerprintJson } from \"./principalKeyFingerprintJson\";\nimport { defineSqlTableSchema } from \"./sqlTableSchema\";\n\n/** A completed, locally authenticated prefix; never an application trust pin. */\nexport const principalHistoryPrefixes = sqliteTable(\n \"principal_history_prefixes\",\n {\n scopeId: text(\"scope_id\").primaryKey(),\n organizationId: text(\"organization_id\").notNull(),\n version: integer(\"version\").notNull(),\n headJson: text(\"head_json\").notNull(),\n currentJson: text(\"current_json\").notNull(),\n progress: text(\"progress\").notNull(),\n },\n (table) => [\n index(\"principal_history_prefixes_key_fingerprint_idx\").on(\n principalCurrentFingerprintJson(table.currentJson),\n desc(table.version),\n ),\n index(\"principal_history_prefixes_organization_idx\").on(\n table.organizationId,\n ),\n ],\n);\n\n/** Entries are selected by their exact signed leaf, including signature bytes. */\nexport const principalHistoryEntries = sqliteTable(\n \"principal_history_entries\",\n {\n scopeId: text(\"scope_id\").notNull(),\n organizationId: text(\"organization_id\").notNull(),\n leafHash: text(\"leaf_hash\").notNull(),\n entryJson: text(\"entry_json\").notNull(),\n },\n (table) => [\n primaryKey({ columns: [table.scopeId, table.leafHash] }),\n index(\"principal_history_entries_organization_idx\").on(\n table.organizationId,\n ),\n ],\n);\n\n/** Untrusted proof material, checked against a locally restored private root. */\nexport const principalHistoryNodes = sqliteTable(\n \"principal_history_nodes\",\n {\n scopeId: text(\"scope_id\").notNull(),\n organizationId: text(\"organization_id\").notNull(),\n hash: text(\"hash\").notNull(),\n leftHash: text(\"left_hash\").notNull(),\n rightHash: text(\"right_hash\").notNull(),\n },\n (table) => [\n primaryKey({ columns: [table.scopeId, table.hash] }),\n index(\"principal_history_nodes_organization_idx\").on(table.organizationId),\n ],\n);\n\nexport const principalHistoryEvidenceTables = [\n {\n ...defineSqlTableSchema(principalHistoryPrefixes),\n requiredColumns: [\"current_json\"],\n },\n defineSqlTableSchema(principalHistoryEntries),\n defineSqlTableSchema(principalHistoryNodes),\n];\n"]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { sql } from "drizzle-orm";
|
|
2
|
+
/** Match the candidate query to its expression index, including corrupt caches. */
|
|
3
|
+
export function principalStateFingerprintJson(column) {
|
|
4
|
+
return sql `json_extract(CASE WHEN json_valid(${column}) THEN ${column} END, '$.keyFingerprint')`;
|
|
5
|
+
}
|
|
6
|
+
export function principalCurrentFingerprintJson(column) {
|
|
7
|
+
return sql `json_extract(CASE WHEN json_valid(${column}) THEN ${column} END, '$.currentState.keyFingerprint')`;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=principalKeyFingerprintJson.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"principalKeyFingerprintJson.js","sourceRoot":"","sources":["principalKeyFingerprintJson.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,GAAG,EAAE,MAAM,aAAa,CAAC;AAEnD,mFAAmF;AACnF,MAAM,UAAU,6BAA6B,CAAC,MAAkB;IAC9D,OAAO,GAAG,CAAQ,qCAAqC,MAAM,UAAU,MAAM,2BAA2B,CAAC;AAC3G,CAAC;AAED,MAAM,UAAU,+BAA+B,CAAC,MAAkB;IAChE,OAAO,GAAG,CAAQ,qCAAqC,MAAM,UAAU,MAAM,wCAAwC,CAAC;AACxH,CAAC","sourcesContent":["import { type SQLWrapper, sql } from \"drizzle-orm\";\n\n/** Match the candidate query to its expression index, including corrupt caches. */\nexport function principalStateFingerprintJson(column: SQLWrapper) {\n return sql<string>`json_extract(CASE WHEN json_valid(${column}) THEN ${column} END, '$.keyFingerprint')`;\n}\n\nexport function principalCurrentFingerprintJson(column: SQLWrapper) {\n return sql<string>`json_extract(CASE WHEN json_valid(${column}) THEN ${column} END, '$.currentState.keyFingerprint')`;\n}\n"]}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import { desc } from "drizzle-orm";
|
|
1
2
|
import { index, integer, primaryKey, sqliteTable, text, } from "drizzle-orm/sqlite-core";
|
|
3
|
+
import { principalStateFingerprintJson } from "./principalKeyFingerprintJson.js";
|
|
2
4
|
/** Durable organization ownership for every managed-principal cache. */
|
|
3
5
|
export const principalPolicyOrganizations = sqliteTable("principal_policy_organizations", {
|
|
4
6
|
principalType: text("principal_type").notNull(),
|
|
@@ -25,12 +27,14 @@ function principalPolicyBundleColumns() {
|
|
|
25
27
|
/** Latest full policy bundle per managed principal. */
|
|
26
28
|
export const principalPolicies = sqliteTable("principal_policies", principalPolicyBundleColumns(), (table) => [
|
|
27
29
|
primaryKey({ columns: [table.principalType, table.principalId] }),
|
|
30
|
+
index("principal_policies_key_fingerprint_idx").on(principalStateFingerprintJson(table.currentStateJson), desc(table.updatedAt)),
|
|
28
31
|
]);
|
|
29
32
|
/** Older verified bundles retained for exact-reference verification and epoch key unwrapping. */
|
|
30
33
|
export const principalPolicyBundleHistory = sqliteTable("principal_policy_bundle_history", principalPolicyBundleColumns(), (table) => [
|
|
31
34
|
primaryKey({
|
|
32
35
|
columns: [table.principalType, table.principalId, table.stateHash],
|
|
33
36
|
}),
|
|
37
|
+
index("principal_policy_history_key_fingerprint_idx").on(principalStateFingerprintJson(table.currentStateJson), desc(table.updatedAt)),
|
|
34
38
|
]);
|
|
35
39
|
/** Exact policy states mapped to every retained bundle chain that contains them. */
|
|
36
40
|
export const principalPolicyBundleReferences = sqliteTable("principal_policy_bundle_references", {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"principalPolicySchema.js","sourceRoot":"","sources":["principalPolicySchema.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,EACL,OAAO,EACP,UAAU,EACV,WAAW,EACX,IAAI,GACL,MAAM,yBAAyB,CAAC;AAEjC,wEAAwE;AACxE,MAAM,CAAC,MAAM,4BAA4B,GAAG,WAAW,CACrD,gCAAgC,EAChC;IACE,aAAa,EAAE,IAAI,CAAC,gBAAgB,CAAC,CAAC,OAAO,EAAE;IAC/C,WAAW,EAAE,IAAI,CAAC,cAAc,CAAC,CAAC,OAAO,EAAE;IAC3C,cAAc,EAAE,IAAI,CAAC,iBAAiB,CAAC,CAAC,OAAO,EAAE;CAClD,EACD,CAAC,KAAK,EAAE,EAAE,CAAC;IACT,UAAU,CAAC,EAAE,OAAO,EAAE,CAAC,KAAK,CAAC,aAAa,EAAE,KAAK,CAAC,WAAW,CAAC,EAAE,CAAC;IACjE,KAAK,CAAC,iDAAiD,CAAC,CAAC,EAAE,CACzD,KAAK,CAAC,cAAc,CACrB;CACF,CACF,CAAC;AAEF,SAAS,4BAA4B;IACnC,OAAO;QACL,aAAa,EAAE,IAAI,CAAC,gBAAgB,CAAC,CAAC,OAAO,EAAE;QAC/C,WAAW,EAAE,IAAI,CAAC,cAAc,CAAC,CAAC,OAAO,EAAE;QAC3C,SAAS,EAAE,IAAI,CAAC,YAAY,CAAC,CAAC,OAAO,EAAE;QACvC,gBAAgB,EAAE,IAAI,CAAC,oBAAoB,CAAC,CAAC,OAAO,EAAE;QACtD,kBAAkB,EAAE,IAAI,CAAC,sBAAsB,CAAC,CAAC,OAAO,EAAE;QAC1D,qBAAqB,EAAE,IAAI,CAAC,yBAAyB,CAAC,CAAC,OAAO,EAAE;QAChE,iBAAiB,EAAE,IAAI,CAAC,qBAAqB,CAAC,CAAC,OAAO,EAAE;QACxD,0BAA0B,EAAE,IAAI,CAAC,+BAA+B,CAAC,CAA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AAC,OAAO,EAAE;IAC3C,OAAO,EAAE,OAAO,CAAC,SAAS,CAAC,CAAC,OAAO,EAAE;IACrC,SAAS,EAAE,IAAI,CAAC,YAAY,CAAC,CAAC,OAAO,EAAE;IACvC,SAAS,EAAE,IAAI,CAAC,YAAY,CAAC,CAAC,OAAO,EAAE;CACxC,EACD,CAAC,KAAK,EAAE,EAAE,CAAC;IACT,UAAU,CAAC,EAAE,OAAO,EAAE,CAAC,KAAK,CAAC,aAAa,EAAE,KAAK,CAAC,WAAW,CAAC,EAAE,CAAC;CAClE,CACF,CAAC","sourcesContent":["import { desc } from \"drizzle-orm\";\nimport {\n index,\n integer,\n primaryKey,\n sqliteTable,\n text,\n} from \"drizzle-orm/sqlite-core\";\n\nimport { principalStateFingerprintJson } from \"./principalKeyFingerprintJson\";\n\n/** Durable organization ownership for every managed-principal cache. */\nexport const principalPolicyOrganizations = sqliteTable(\n \"principal_policy_organizations\",\n {\n principalType: text(\"principal_type\").notNull(),\n principalId: text(\"principal_id\").notNull(),\n organizationId: text(\"organization_id\").notNull(),\n },\n (table) => [\n primaryKey({ columns: [table.principalType, table.principalId] }),\n index(\"principal_policy_organizations_organization_idx\").on(\n table.organizationId,\n ),\n ],\n);\n\nfunction principalPolicyBundleColumns() {\n return {\n principalType: text(\"principal_type\").notNull(),\n principalId: text(\"principal_id\").notNull(),\n stateHash: text(\"state_hash\").notNull(),\n currentStateJson: text(\"current_state_json\").notNull(),\n currentPayloadJson: text(\"current_payload_json\").notNull(),\n currentProjectionJson: text(\"current_projection_json\").notNull(),\n currentGrantsJson: text(\"current_grants_json\").notNull(),\n currentMemberEnvelopesJson: text(\"current_member_envelopes_json\").notNull(),\n previousStatesJson: text(\"previous_states_json\").notNull(),\n updatedAt: text(\"updated_at\").notNull(),\n };\n}\n\n/** Latest full policy bundle per managed principal. */\nexport const principalPolicies = sqliteTable(\n \"principal_policies\",\n principalPolicyBundleColumns(),\n (table) => [\n primaryKey({ columns: [table.principalType, table.principalId] }),\n index(\"principal_policies_key_fingerprint_idx\").on(\n principalStateFingerprintJson(table.currentStateJson),\n desc(table.updatedAt),\n ),\n ],\n);\n\n/** Older verified bundles retained for exact-reference verification and epoch key unwrapping. */\nexport const principalPolicyBundleHistory = sqliteTable(\n \"principal_policy_bundle_history\",\n principalPolicyBundleColumns(),\n (table) => [\n primaryKey({\n columns: [table.principalType, table.principalId, table.stateHash],\n }),\n index(\"principal_policy_history_key_fingerprint_idx\").on(\n principalStateFingerprintJson(table.currentStateJson),\n desc(table.updatedAt),\n ),\n ],\n);\n\n/** Exact policy states mapped to every retained bundle chain that contains them. */\nexport const principalPolicyBundleReferences = sqliteTable(\n \"principal_policy_bundle_references\",\n {\n principalType: text(\"principal_type\").notNull(),\n principalId: text(\"principal_id\").notNull(),\n version: integer(\"version\").notNull(),\n stateHash: text(\"state_hash\").notNull(),\n keyEpoch: integer(\"key_epoch\").notNull(),\n keyFingerprint: text(\"key_fingerprint\").notNull(),\n bundleVersion: integer(\"bundle_version\").notNull(),\n bundleStateHash: text(\"bundle_state_hash\").notNull(),\n },\n (table) => [\n primaryKey({\n columns: [\n table.principalType,\n table.principalId,\n table.version,\n table.stateHash,\n table.keyEpoch,\n table.keyFingerprint,\n table.bundleStateHash,\n ],\n }),\n ],\n);\n\n/** Monotonic anti-rollback pin for each verified managed principal. */\nexport const principalPolicyCheckpoints = sqliteTable(\n \"principal_policy_checkpoints\",\n {\n principalType: text(\"principal_type\").notNull(),\n principalId: text(\"principal_id\").notNull(),\n version: integer(\"version\").notNull(),\n stateHash: text(\"state_hash\").notNull(),\n updatedAt: text(\"updated_at\").notNull(),\n },\n (table) => [\n primaryKey({ columns: [table.principalType, table.principalId] }),\n ],\n);\n"]}
|