@yielded/auth-persistence 0.1.0-beta.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -0
- package/dist/Adapter.d.mts +30 -0
- package/dist/Adapter.mjs +25 -0
- package/dist/index.d.mts +7 -0
- package/dist/index.mjs +7 -0
- package/dist/internal/PasswordPreparedPostconditions.d.mts +22 -0
- package/dist/internal/PasswordPreparedPostconditions.mjs +12 -0
- package/dist/internal/PasswordPreparedPostconditions.mjs.map +1 -0
- package/dist/internal/configuration.d.mts +174 -0
- package/dist/internal/configuration.mjs +7 -0
- package/dist/internal/configuration.mjs.map +1 -0
- package/dist/internal/email-kernel.d.mts +275 -0
- package/dist/internal/email-kernel.mjs +487 -0
- package/dist/internal/email-kernel.mjs.map +1 -0
- package/dist/internal/mapping-error.d.mts +10 -0
- package/dist/internal/mapping-error.mjs +30 -0
- package/dist/internal/mapping-error.mjs.map +1 -0
- package/dist/internal/models/common.d.mts +12 -0
- package/dist/internal/models/email-model.d.mts +353 -0
- package/dist/internal/models/email-model.mjs +21 -0
- package/dist/internal/models/email-model.mjs.map +1 -0
- package/dist/internal/models/passkey-model.d.mts +266 -0
- package/dist/internal/models/passkey-model.mjs +34 -0
- package/dist/internal/models/passkey-model.mjs.map +1 -0
- package/dist/internal/models/passkey-registration-ceremony-model.d.mts +49 -0
- package/dist/internal/models/passkey-registration-ceremony-model.mjs +18 -0
- package/dist/internal/models/passkey-registration-ceremony-model.mjs.map +1 -0
- package/dist/internal/models/passkey-write-model.d.mts +189 -0
- package/dist/internal/models/passkey-write-model.mjs +13 -0
- package/dist/internal/models/passkey-write-model.mjs.map +1 -0
- package/dist/internal/models/password-model.d.mts +328 -0
- package/dist/internal/models/password-model.mjs +23 -0
- package/dist/internal/models/password-model.mjs.map +1 -0
- package/dist/internal/models/proof-model.d.mts +285 -0
- package/dist/internal/models/proof-model.mjs +17 -0
- package/dist/internal/models/proof-model.mjs.map +1 -0
- package/dist/internal/models/session-model.d.mts +199 -0
- package/dist/internal/models/session-model.mjs +20 -0
- package/dist/internal/models/session-model.mjs.map +1 -0
- package/dist/internal/models/step-up-model.d.mts +66 -0
- package/dist/internal/models/step-up-model.mjs +9 -0
- package/dist/internal/models/step-up-model.mjs.map +1 -0
- package/dist/internal/oauth-app.d.mts +11 -0
- package/dist/internal/oauth-app.mjs +57 -0
- package/dist/internal/oauth-app.mjs.map +1 -0
- package/dist/internal/oauth-server.d.mts +10 -0
- package/dist/internal/oauth-server.mjs +66 -0
- package/dist/internal/oauth-server.mjs.map +1 -0
- package/dist/internal/passkey/admission.d.mts +6 -0
- package/dist/internal/passkey/admission.mjs +216 -0
- package/dist/internal/passkey/admission.mjs.map +1 -0
- package/dist/internal/passkey/credentials.d.mts +14 -0
- package/dist/internal/passkey/credentials.mjs +249 -0
- package/dist/internal/passkey/credentials.mjs.map +1 -0
- package/dist/internal/passkey/enrollment.mjs +138 -0
- package/dist/internal/passkey/enrollment.mjs.map +1 -0
- package/dist/internal/passkey/flow.d.mts +21 -0
- package/dist/internal/passkey/flow.mjs +452 -0
- package/dist/internal/passkey/flow.mjs.map +1 -0
- package/dist/internal/passkey/management.mjs +231 -0
- package/dist/internal/passkey/management.mjs.map +1 -0
- package/dist/internal/passkey/registration-ceremony.d.mts +9 -0
- package/dist/internal/passkey/registration-ceremony.mjs +129 -0
- package/dist/internal/passkey/registration-ceremony.mjs.map +1 -0
- package/dist/internal/passkey/registration-custody.d.mts +2 -0
- package/dist/internal/passkey/registration-custody.mjs +52 -0
- package/dist/internal/passkey/registration-custody.mjs.map +1 -0
- package/dist/internal/passkey/registration-write.mjs +211 -0
- package/dist/internal/passkey/registration-write.mjs.map +1 -0
- package/dist/internal/passkey/state.d.mts +10 -0
- package/dist/internal/passkey/state.mjs +234 -0
- package/dist/internal/passkey/state.mjs.map +1 -0
- package/dist/internal/passkey/target.d.mts +19 -0
- package/dist/internal/passkey/target.mjs +190 -0
- package/dist/internal/passkey/target.mjs.map +1 -0
- package/dist/internal/passkey/write-state.d.mts +17 -0
- package/dist/internal/passkey/write-state.mjs +345 -0
- package/dist/internal/passkey/write-state.mjs.map +1 -0
- package/dist/internal/passkey/write-target.mjs +192 -0
- package/dist/internal/passkey/write-target.mjs.map +1 -0
- package/dist/internal/passkey-kernel.d.mts +5592 -0
- package/dist/internal/passkey-kernel.mjs +49 -0
- package/dist/internal/passkey-kernel.mjs.map +1 -0
- package/dist/internal/passkeys.d.mts +7 -0
- package/dist/internal/passkeys.mjs +368 -0
- package/dist/internal/passkeys.mjs.map +1 -0
- package/dist/internal/password-kernel.d.mts +290 -0
- package/dist/internal/password-kernel.mjs +704 -0
- package/dist/internal/password-kernel.mjs.map +1 -0
- package/dist/internal/persistence.d.mts +25 -0
- package/dist/internal/persistence.mjs +291 -0
- package/dist/internal/persistence.mjs.map +1 -0
- package/dist/internal/phone-kernel.d.mts +162 -0
- package/dist/internal/phone-kernel.mjs +434 -0
- package/dist/internal/phone-kernel.mjs.map +1 -0
- package/dist/internal/proof-kernel.d.mts +93 -0
- package/dist/internal/proof-kernel.mjs +619 -0
- package/dist/internal/proof-kernel.mjs.map +1 -0
- package/dist/internal/query-operations.d.mts +61 -0
- package/dist/internal/registration-contract.d.mts +16 -0
- package/dist/internal/registration.mjs +66 -0
- package/dist/internal/registration.mjs.map +1 -0
- package/dist/internal/session-kernel.d.mts +48 -0
- package/dist/internal/session-kernel.mjs +737 -0
- package/dist/internal/session-kernel.mjs.map +1 -0
- package/dist/internal/sql-persistence.d.mts +10 -0
- package/dist/internal/sql-persistence.mjs +37 -0
- package/dist/internal/sql-persistence.mjs.map +1 -0
- package/dist/internal/sql-query.mjs +207 -0
- package/dist/internal/sql-query.mjs.map +1 -0
- package/dist/internal/sql-table.d.mts +36 -0
- package/dist/internal/sql-table.mjs +81 -0
- package/dist/internal/sql-table.mjs.map +1 -0
- package/dist/internal/standalone.d.mts +9 -0
- package/dist/internal/standalone.mjs +13 -0
- package/dist/internal/standalone.mjs.map +1 -0
- package/dist/internal/step-up-state.d.mts +49 -0
- package/dist/internal/step-up-state.mjs +32 -0
- package/dist/internal/step-up-state.mjs.map +1 -0
- package/dist/internal/storage-mapping.mjs +686 -0
- package/dist/internal/storage-mapping.mjs.map +1 -0
- package/dist/internal/storage-tables.d.mts +913 -0
- package/dist/internal/storage-tables.mjs +387 -0
- package/dist/internal/storage-tables.mjs.map +1 -0
- package/dist/internal/storage-validation.mjs +58 -0
- package/dist/internal/storage-validation.mjs.map +1 -0
- package/dist/internal/transaction-execution-kernel.d.mts +38 -0
- package/dist/internal/transaction-execution-kernel.mjs +116 -0
- package/dist/internal/transaction-execution-kernel.mjs.map +1 -0
- package/dist/internal/transaction-kernel.d.mts +77 -0
- package/dist/internal/transaction-kernel.mjs +260 -0
- package/dist/internal/transaction-kernel.mjs.map +1 -0
- package/package.json +1 -0
- package/src/Adapter.ts +291 -0
- package/src/index.ts +6 -0
- package/src/internal/PasswordPreparedPostconditions.ts +30 -0
- package/src/internal/configuration.ts +282 -0
- package/src/internal/email-kernel.ts +1535 -0
- package/src/internal/mapping-error.ts +36 -0
- package/src/internal/models/common.ts +11 -0
- package/src/internal/models/email-model.ts +559 -0
- package/src/internal/models/passkey-model.ts +390 -0
- package/src/internal/models/passkey-registration-ceremony-model.ts +72 -0
- package/src/internal/models/passkey-write-model.ts +297 -0
- package/src/internal/models/password-model.ts +513 -0
- package/src/internal/models/proof-model.ts +424 -0
- package/src/internal/models/session-model.ts +375 -0
- package/src/internal/models/step-up-model.ts +135 -0
- package/src/internal/oauth-app.ts +82 -0
- package/src/internal/oauth-server.ts +89 -0
- package/src/internal/passkey/admission.ts +361 -0
- package/src/internal/passkey/credentials.ts +511 -0
- package/src/internal/passkey/enrollment.ts +326 -0
- package/src/internal/passkey/flow.ts +1062 -0
- package/src/internal/passkey/management.ts +421 -0
- package/src/internal/passkey/registration-ceremony.ts +283 -0
- package/src/internal/passkey/registration-custody.ts +91 -0
- package/src/internal/passkey/registration-write.ts +426 -0
- package/src/internal/passkey/state.ts +353 -0
- package/src/internal/passkey/target.ts +586 -0
- package/src/internal/passkey/write-state.ts +681 -0
- package/src/internal/passkey/write-target.ts +452 -0
- package/src/internal/passkey-kernel.ts +104 -0
- package/src/internal/passkeys.ts +606 -0
- package/src/internal/password-kernel.ts +2124 -0
- package/src/internal/persistence.ts +588 -0
- package/src/internal/phone-kernel.ts +846 -0
- package/src/internal/proof-kernel.ts +1954 -0
- package/src/internal/query-operations.ts +81 -0
- package/src/internal/registration-contract.ts +22 -0
- package/src/internal/registration.ts +161 -0
- package/src/internal/session-kernel.ts +2217 -0
- package/src/internal/sql-persistence.ts +45 -0
- package/src/internal/sql-query.ts +298 -0
- package/src/internal/sql-table.ts +104 -0
- package/src/internal/standalone.ts +30 -0
- package/src/internal/step-up-state.ts +139 -0
- package/src/internal/storage-mapping.ts +708 -0
- package/src/internal/storage-tables.ts +408 -0
- package/src/internal/storage-validation.ts +117 -0
- package/src/internal/transaction-execution-kernel.ts +313 -0
- package/src/internal/transaction-kernel.ts +593 -0
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
import { PersistenceMappingError, isMappedConstraintConflict } from "./mapping-error.mjs";
|
|
2
|
+
import { requiredEmailAddressConstraints, requiredEmailSignInConstraints } from "./models/email-model.mjs";
|
|
3
|
+
import { CurrentProofSql } from "./proof-kernel.mjs";
|
|
4
|
+
import { Cause, Context, DateTime, Effect, Option, Schema } from "effect";
|
|
5
|
+
import { CurrentCommitJournal, HookConfigurationError, LifecycleHooks, coordinateCommit, hasCommitScope } from "@yielded/auth/Hooks";
|
|
6
|
+
import { assessAuthentication, snapshotAuthenticationEvidence } from "@yielded/auth/Sessions";
|
|
7
|
+
import { reportPersistenceFailure } from "@yielded/auth/Persistence";
|
|
8
|
+
import { EmailAddressPersistence, EmailSignInTargets, EmailUnavailable, snapshotEmailCredential, snapshotEmailRequirement, snapshotEmailRevision } from "@yielded/auth/Email";
|
|
9
|
+
import { ProofUnavailable } from "@yielded/auth/Proofs";
|
|
10
|
+
//#region src/internal/email-kernel.ts
|
|
11
|
+
var CurrentEmailSql = class extends Context.Service()("effect-auth/CurrentEmailSql") {};
|
|
12
|
+
const makeEmailKernel = (operations, proofs) => {
|
|
13
|
+
const { and, eq, inArray, lte, column, updateValues } = operations;
|
|
14
|
+
const { completeProofPlanIn } = proofs;
|
|
15
|
+
const unavailable = () => EmailUnavailable.make({});
|
|
16
|
+
const translateFailure = (effect) => reportPersistenceFailure(effect, (error) => Schema.is(EmailUnavailable)(error) || Schema.is(ProofUnavailable)(error) || Schema.is(HookConfigurationError)(error)).pipe(Effect.catchCause((cause) => Effect.failCause(Cause.map(cause, () => unavailable()))));
|
|
17
|
+
const nowMillis = Effect.map(DateTime.now, DateTime.toEpochMillis);
|
|
18
|
+
const selectRows = (query, locking) => locking && typeof query.for === "function" ? query.for("update") : query;
|
|
19
|
+
const sameIdentifier = (left, right) => left.namespace === right.namespace && left.value === right.value;
|
|
20
|
+
const validEmailSignInConstraints = (mapping) => Object.entries(requiredEmailSignInConstraints).every(([key, value]) => mapping.constraints[key] === value);
|
|
21
|
+
const validAddressConstraints = (mapping) => Object.entries(requiredEmailAddressConstraints).every(([key, value]) => mapping.constraints[key] === value);
|
|
22
|
+
const subjectColumns = (mapping) => ({
|
|
23
|
+
id: column(mapping.subject.table, mapping.subject.id),
|
|
24
|
+
status: column(mapping.subject.table, mapping.subject.status),
|
|
25
|
+
securityRevision: column(mapping.subject.table, mapping.subject.securityRevision)
|
|
26
|
+
});
|
|
27
|
+
const identifierColumns = (mapping) => ({
|
|
28
|
+
namespace: column(mapping.identifier.table, mapping.identifier.namespace),
|
|
29
|
+
value: column(mapping.identifier.table, mapping.identifier.value),
|
|
30
|
+
subjectId: column(mapping.identifier.table, mapping.identifier.subjectId),
|
|
31
|
+
verifiedAt: column(mapping.identifier.table, mapping.identifier.verifiedAt),
|
|
32
|
+
bindingRevision: column(mapping.identifier.table, mapping.identifier.bindingRevision)
|
|
33
|
+
});
|
|
34
|
+
const credentialColumns = (mapping) => ({
|
|
35
|
+
moduleId: column(mapping.credential.table, mapping.credential.moduleId),
|
|
36
|
+
subjectId: column(mapping.credential.table, mapping.credential.subjectId),
|
|
37
|
+
credentialId: column(mapping.credential.table, mapping.credential.credentialId),
|
|
38
|
+
identifierNamespace: column(mapping.credential.table, mapping.credential.identifierNamespace),
|
|
39
|
+
identifierValue: column(mapping.credential.table, mapping.credential.identifierValue),
|
|
40
|
+
credentialRevision: column(mapping.credential.table, mapping.credential.credentialRevision),
|
|
41
|
+
status: column(mapping.credential.table, mapping.credential.status)
|
|
42
|
+
});
|
|
43
|
+
const authorityColumns = (mapping) => ({
|
|
44
|
+
subjectId: column(mapping.authorityCredential.table, mapping.authorityCredential.subjectId),
|
|
45
|
+
credentialId: column(mapping.authorityCredential.table, mapping.authorityCredential.credentialId),
|
|
46
|
+
revision: column(mapping.authorityCredential.table, mapping.authorityCredential.revision),
|
|
47
|
+
status: column(mapping.authorityCredential.table, mapping.authorityCredential.status)
|
|
48
|
+
});
|
|
49
|
+
const commandColumns = (mapping) => ({
|
|
50
|
+
moduleId: column(mapping.command.table, mapping.command.moduleId),
|
|
51
|
+
commandId: column(mapping.command.table, mapping.command.commandId),
|
|
52
|
+
action: column(mapping.command.table, mapping.command.action),
|
|
53
|
+
bindingDigest: column(mapping.command.table, mapping.command.bindingDigest),
|
|
54
|
+
retentionUntil: column(mapping.command.table, mapping.command.retentionUntil)
|
|
55
|
+
});
|
|
56
|
+
const allocate = (mode, asynchronous, synchronous) => {
|
|
57
|
+
if (mode === "synchronous") return synchronous === void 0 ? Effect.fail(unavailable()) : Effect.try({
|
|
58
|
+
try: synchronous,
|
|
59
|
+
catch: (cause) => new PersistenceMappingError({
|
|
60
|
+
operation: "mapping",
|
|
61
|
+
cause
|
|
62
|
+
})
|
|
63
|
+
});
|
|
64
|
+
if (asynchronous !== void 0) return asynchronous;
|
|
65
|
+
return synchronous === void 0 ? Effect.fail(unavailable()) : Effect.try({
|
|
66
|
+
try: synchronous,
|
|
67
|
+
catch: (cause) => new PersistenceMappingError({
|
|
68
|
+
operation: "mapping",
|
|
69
|
+
cause
|
|
70
|
+
})
|
|
71
|
+
});
|
|
72
|
+
};
|
|
73
|
+
const nextSecurityRevision = (mapping, mode, current) => {
|
|
74
|
+
if (mode === "synchronous") return mapping.subject.nextSecurityRevisionSync === void 0 ? Effect.fail(unavailable()) : Effect.try({
|
|
75
|
+
try: () => mapping.subject.nextSecurityRevisionSync(current),
|
|
76
|
+
catch: (cause) => new PersistenceMappingError({
|
|
77
|
+
operation: "mapping",
|
|
78
|
+
cause
|
|
79
|
+
})
|
|
80
|
+
});
|
|
81
|
+
if (mapping.subject.nextSecurityRevision !== void 0) return mapping.subject.nextSecurityRevision(current);
|
|
82
|
+
if (mapping.subject.nextSecurityRevisionSync !== void 0) return Effect.try({
|
|
83
|
+
try: () => mapping.subject.nextSecurityRevisionSync(current),
|
|
84
|
+
catch: (cause) => new PersistenceMappingError({
|
|
85
|
+
operation: "mapping",
|
|
86
|
+
cause
|
|
87
|
+
})
|
|
88
|
+
});
|
|
89
|
+
return Effect.fail(unavailable());
|
|
90
|
+
};
|
|
91
|
+
const safeRead = (database, configuration, body) => Effect.gen(function* () {
|
|
92
|
+
if (configuration.coordinated !== true) {
|
|
93
|
+
if (yield* hasCommitScope) return yield* unavailable();
|
|
94
|
+
yield* configuration.standaloneGuard;
|
|
95
|
+
}
|
|
96
|
+
return yield* database.transaction((transaction) => body.pipe(Effect.provideService(CurrentEmailSql, transaction), Effect.provideService(CurrentProofSql, transaction)));
|
|
97
|
+
});
|
|
98
|
+
const owned = (database, configuration, body) => {
|
|
99
|
+
const run = coordinateCommit(() => database.transaction((transaction) => body.pipe(Effect.provideService(CurrentEmailSql, transaction), Effect.provideService(CurrentProofSql, transaction))), { mode: configuration.mode }).pipe(Effect.map((result) => result.value));
|
|
100
|
+
return configuration.coordinated !== true ? Effect.gen(function* () {
|
|
101
|
+
if (yield* hasCommitScope) return yield* unavailable();
|
|
102
|
+
yield* configuration.standaloneGuard;
|
|
103
|
+
return yield* run;
|
|
104
|
+
}) : run;
|
|
105
|
+
};
|
|
106
|
+
const emailLookupQuery = (mapping, moduleId, identifier) => {
|
|
107
|
+
const subject = subjectColumns(mapping), binding = identifierColumns(mapping), credential = credentialColumns(mapping);
|
|
108
|
+
return {
|
|
109
|
+
fields: {
|
|
110
|
+
subject: mapping.subject.table,
|
|
111
|
+
identifier: mapping.identifier.table,
|
|
112
|
+
credential: mapping.credential.table
|
|
113
|
+
},
|
|
114
|
+
from: mapping.identifier.table,
|
|
115
|
+
subjectTable: mapping.subject.table,
|
|
116
|
+
subjectJoin: eq(subject.id, binding.subjectId),
|
|
117
|
+
credentialTable: mapping.credential.table,
|
|
118
|
+
credentialJoin: and(eq(credential.moduleId, moduleId), eq(credential.subjectId, binding.subjectId), eq(credential.identifierNamespace, binding.namespace), eq(credential.identifierValue, binding.value)),
|
|
119
|
+
predicate: and(eq(binding.namespace, identifier.namespace), eq(binding.value, identifier.value)),
|
|
120
|
+
limit: 1
|
|
121
|
+
};
|
|
122
|
+
};
|
|
123
|
+
const emailLookupRows = Effect.fn("Drizzle.emailLookupRows")(function* (mapping, moduleId, identifier) {
|
|
124
|
+
const database = yield* CurrentEmailSql;
|
|
125
|
+
const query = emailLookupQuery(mapping, moduleId, identifier);
|
|
126
|
+
return yield* database.select(query.fields).from(query.from).innerJoin(query.subjectTable, query.subjectJoin).innerJoin(query.credentialTable, query.credentialJoin).where(query.predicate).limit(query.limit);
|
|
127
|
+
});
|
|
128
|
+
const decodeEmailSnapshot = Effect.fn("DrizzleEmail.decodeSnapshot")(function* (mapping, moduleId, requested, row) {
|
|
129
|
+
if (!mapping.subject.isActiveStatus(row.subject[mapping.subject.status]) || !mapping.identifier.isCurrent(row.identifier) || !mapping.credential.isActiveStatus(row.credential[mapping.credential.status])) return void 0;
|
|
130
|
+
const snapshot = yield* mapping.credential.decode({
|
|
131
|
+
moduleId,
|
|
132
|
+
subject: row.subject,
|
|
133
|
+
identifier: row.identifier,
|
|
134
|
+
credential: row.credential
|
|
135
|
+
}).pipe(Effect.flatMap(snapshotEmailCredential));
|
|
136
|
+
const subjectId = yield* mapping.subjectId.toSubject(row.subject[mapping.subject.id]);
|
|
137
|
+
const verifiedAtMillis = yield* mapping.decodeInstant(row.identifier[mapping.identifier.verifiedAt]);
|
|
138
|
+
if (snapshot.moduleId !== moduleId || !sameIdentifier(snapshot.identifier, requested) || snapshot.revision.subjectId !== subjectId || snapshot.revision.securityRevision !== row.subject[mapping.subject.securityRevision] || snapshot.identifierRevision !== row.identifier[mapping.identifier.bindingRevision] || snapshot.verifiedAtMillis !== verifiedAtMillis || snapshot.credentialId !== row.credential[mapping.credential.credentialId] || snapshot.credentialRevision !== row.credential[mapping.credential.credentialRevision]) return void 0;
|
|
139
|
+
return snapshot;
|
|
140
|
+
});
|
|
141
|
+
const makeSqlEmailSignInTargets = Effect.fn("makeSqlEmailSignInTargets")(function(database, mapping, configuration) {
|
|
142
|
+
return Effect.succeed(EmailSignInTargets.of({ lookup: (input) => (validEmailSignInConstraints(mapping) ? safeRead(database, configuration, Effect.gen(function* () {
|
|
143
|
+
const row = (yield* emailLookupRows(mapping, input.moduleId, input.identifier))[0];
|
|
144
|
+
if (row === void 0) return Option.none();
|
|
145
|
+
const snapshot = yield* decodeEmailSnapshot(mapping, input.moduleId, input.identifier, row);
|
|
146
|
+
return snapshot === void 0 ? Option.none() : Option.some(snapshot);
|
|
147
|
+
})) : Effect.fail(unavailable())).pipe(Effect.provideService(CurrentEmailSql, database), translateFailure) }));
|
|
148
|
+
});
|
|
149
|
+
const readSubject = Effect.fn("DrizzleEmail.readSubject")(function* (mapping, subjectId, locking) {
|
|
150
|
+
const database = yield* CurrentEmailSql;
|
|
151
|
+
const nativeSubjectId = yield* mapping.subjectId.toNative(subjectId);
|
|
152
|
+
const s = subjectColumns(mapping);
|
|
153
|
+
return {
|
|
154
|
+
nativeSubjectId,
|
|
155
|
+
subject: (yield* selectRows(database.select().from(mapping.subject.table).where(eq(s.id, nativeSubjectId)).limit(1), locking))[0]
|
|
156
|
+
};
|
|
157
|
+
});
|
|
158
|
+
const identifierRow = Effect.fn("Drizzle.identifierRow")(function* (mapping, identifier, locking) {
|
|
159
|
+
const database = yield* CurrentEmailSql;
|
|
160
|
+
const i = identifierColumns(mapping);
|
|
161
|
+
return yield* selectRows(database.select().from(mapping.identifier.table).where(and(eq(i.namespace, identifier.namespace), eq(i.value, identifier.value))).limit(1), locking);
|
|
162
|
+
});
|
|
163
|
+
const credentialById = Effect.fn("Drizzle.credentialById")(function* (mapping, moduleId, credentialId, locking) {
|
|
164
|
+
const database = yield* CurrentEmailSql;
|
|
165
|
+
const c = credentialColumns(mapping);
|
|
166
|
+
return yield* selectRows(database.select().from(mapping.credential.table).where(and(eq(c.moduleId, moduleId), eq(c.credentialId, credentialId))).limit(1), locking);
|
|
167
|
+
});
|
|
168
|
+
const credentialByIdentifier = Effect.fn("Drizzle.credentialByIdentifier")(function* (mapping, moduleId, identifier, locking) {
|
|
169
|
+
const database = yield* CurrentEmailSql;
|
|
170
|
+
const c = credentialColumns(mapping);
|
|
171
|
+
return yield* selectRows(database.select().from(mapping.credential.table).where(and(eq(c.moduleId, moduleId), eq(c.identifierNamespace, identifier.namespace), eq(c.identifierValue, identifier.value))).limit(1), locking);
|
|
172
|
+
});
|
|
173
|
+
const currentAddress = Effect.fn("DrizzleEmail.currentAddress")(function* (mapping, input, locking) {
|
|
174
|
+
const database = yield* CurrentEmailSql;
|
|
175
|
+
const locked = yield* readSubject(mapping, input.subjectId, locking);
|
|
176
|
+
if (locked.subject === void 0 || !mapping.subject.isActiveStatus(locked.subject[mapping.subject.status])) return void 0;
|
|
177
|
+
const discoveredSource = input.sourceCredentialId === void 0 ? void 0 : (yield* credentialById(mapping, input.moduleId, input.sourceCredentialId, false))[0];
|
|
178
|
+
const sourceIdentifier = discoveredSource === void 0 ? void 0 : {
|
|
179
|
+
namespace: discoveredSource[mapping.credential.identifierNamespace],
|
|
180
|
+
value: discoveredSource[mapping.credential.identifierValue]
|
|
181
|
+
};
|
|
182
|
+
const identifiers = [input.target, ...sourceIdentifier === void 0 ? [] : [sourceIdentifier]].filter((value, index, values) => values.findIndex((candidate) => sameIdentifier(candidate, value)) === index).sort((left, right) => `${left.namespace}\u0000${left.value}`.localeCompare(`${right.namespace}\u0000${right.value}`));
|
|
183
|
+
const lockedIdentifiers = /* @__PURE__ */ new Map();
|
|
184
|
+
for (const identifier of identifiers) {
|
|
185
|
+
const row = (yield* identifierRow(mapping, identifier, locking))[0];
|
|
186
|
+
if (row !== void 0) lockedIdentifiers.set(`${identifier.namespace}\u0000${identifier.value}`, row);
|
|
187
|
+
}
|
|
188
|
+
const sourceCredential = input.sourceCredentialId === void 0 ? void 0 : (yield* credentialById(mapping, input.moduleId, input.sourceCredentialId, locking))[0];
|
|
189
|
+
const targetCredential = (yield* credentialByIdentifier(mapping, input.moduleId, input.target, locking))[0];
|
|
190
|
+
const a = authorityColumns(mapping);
|
|
191
|
+
const activeAuthority = (yield* selectRows(database.select().from(mapping.authorityCredential.table).where(eq(a.subjectId, locked.nativeSubjectId)).orderBy(a.credentialId), locking)).filter((row) => mapping.authorityCredential.isActiveStatus(row[mapping.authorityCredential.status]));
|
|
192
|
+
const subjectId = yield* mapping.subjectId.toSubject(locked.nativeSubjectId);
|
|
193
|
+
const revision = Object.freeze({
|
|
194
|
+
subjectId,
|
|
195
|
+
securityRevision: locked.subject[mapping.subject.securityRevision],
|
|
196
|
+
credentials: Object.freeze(activeAuthority.map((row) => Object.freeze({
|
|
197
|
+
credentialId: row[mapping.authorityCredential.credentialId],
|
|
198
|
+
revision: row[mapping.authorityCredential.revision]
|
|
199
|
+
})))
|
|
200
|
+
});
|
|
201
|
+
const targetIdentifier = lockedIdentifiers.get(`${input.target.namespace}\u0000${input.target.value}`);
|
|
202
|
+
const targetMutable = targetIdentifier !== void 0 && mapping.subjectId.equals(targetIdentifier[mapping.identifier.subjectId], locked.nativeSubjectId) && mapping.identifier.isMutableTarget(targetIdentifier) && (targetCredential === void 0 || !mapping.credential.isActiveStatus(targetCredential[mapping.credential.status]) && mapping.subjectId.equals(targetCredential[mapping.credential.subjectId], locked.nativeSubjectId));
|
|
203
|
+
let source;
|
|
204
|
+
if (input.sourceCredentialId !== void 0) {
|
|
205
|
+
if (sourceCredential === void 0 || sourceIdentifier === void 0 || !mapping.subjectId.equals(sourceCredential[mapping.credential.subjectId], locked.nativeSubjectId) || !mapping.credential.isActiveStatus(sourceCredential[mapping.credential.status])) return void 0;
|
|
206
|
+
const sourceIdentifierRow = lockedIdentifiers.get(`${sourceIdentifier.namespace}\u0000${sourceIdentifier.value}`);
|
|
207
|
+
if (sourceIdentifierRow === void 0 || !mapping.identifier.isCurrent(sourceIdentifierRow) || !mapping.subjectId.equals(sourceIdentifierRow[mapping.identifier.subjectId], locked.nativeSubjectId)) return void 0;
|
|
208
|
+
source = {
|
|
209
|
+
credential: sourceCredential,
|
|
210
|
+
identifier: sourceIdentifierRow,
|
|
211
|
+
snapshot: yield* mapping.credential.decode({
|
|
212
|
+
moduleId: input.moduleId,
|
|
213
|
+
subject: locked.subject,
|
|
214
|
+
identifier: sourceIdentifierRow,
|
|
215
|
+
credential: sourceCredential
|
|
216
|
+
}).pipe(Effect.flatMap(snapshotEmailCredential))
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
let eligible = targetIdentifier === void 0 ? targetCredential === void 0 : targetMutable;
|
|
220
|
+
if (input.sourceCredentialId !== void 0 && source === void 0) eligible = false;
|
|
221
|
+
if (source !== void 0 && sameIdentifier(source.snapshot.identifier, input.target)) eligible = false;
|
|
222
|
+
if (mapping.addressCardinality === "single" && input.sourceCredentialId === void 0) {
|
|
223
|
+
const c = credentialColumns(mapping);
|
|
224
|
+
if ((yield* selectRows(database.select().from(mapping.credential.table).where(and(eq(c.moduleId, input.moduleId), eq(c.subjectId, locked.nativeSubjectId))), locking)).some((row) => mapping.credential.isActiveStatus(row[mapping.credential.status]))) eligible = false;
|
|
225
|
+
}
|
|
226
|
+
return {
|
|
227
|
+
nativeSubjectId: locked.nativeSubjectId,
|
|
228
|
+
subject: locked.subject,
|
|
229
|
+
revision,
|
|
230
|
+
...source === void 0 ? {} : { source },
|
|
231
|
+
...targetIdentifier === void 0 ? {} : { targetIdentifier },
|
|
232
|
+
...targetCredential === void 0 ? {} : { targetCredential },
|
|
233
|
+
...targetMutable ? { targetIdentifierRevision: targetIdentifier[mapping.identifier.bindingRevision] } : {},
|
|
234
|
+
eligible
|
|
235
|
+
};
|
|
236
|
+
});
|
|
237
|
+
const sameEmailRevision = (left, right) => {
|
|
238
|
+
if (left.subjectId !== right.subjectId || left.securityRevision !== right.securityRevision || left.credentials.length !== right.credentials.length) return false;
|
|
239
|
+
const expected = [...left.credentials].sort((a, b) => a.credentialId.localeCompare(b.credentialId));
|
|
240
|
+
const actual = [...right.credentials].sort((a, b) => a.credentialId.localeCompare(b.credentialId));
|
|
241
|
+
return expected.every((item, index) => item.credentialId === actual[index].credentialId && item.revision === actual[index].revision);
|
|
242
|
+
};
|
|
243
|
+
const actionModule = (moduleId, action) => `${moduleId}/${action === "verify-address" ? "verify-address" : "change-address"}`;
|
|
244
|
+
const actionPurpose = (action) => action === "verify-address" ? "email-address-verification" : "email-address-change";
|
|
245
|
+
const completionMatches = (input, action) => {
|
|
246
|
+
const completion = input.completion.input;
|
|
247
|
+
const binding = completion.binding;
|
|
248
|
+
return completion.moduleId === actionModule(input.moduleId, action) && completion.purpose === actionPurpose(action) && binding._tag === "IdentifierChange" && sameIdentifier(binding.identifier, input.target) && sameEmailRevision(binding.revision, input.captured.revision);
|
|
249
|
+
};
|
|
250
|
+
const snapshotEmailMutation = Effect.fn("DrizzleEmail.snapshotMutation")(function* (input) {
|
|
251
|
+
const evidence = yield* snapshotAuthenticationEvidence(input.authorization.evidence).pipe(Effect.mapError(unavailable));
|
|
252
|
+
const requirement = yield* snapshotEmailRequirement(input.authorization.requirement);
|
|
253
|
+
const source = input.captured.source === void 0 ? void 0 : yield* snapshotEmailCredential(input.captured.source);
|
|
254
|
+
return Object.freeze({
|
|
255
|
+
...input,
|
|
256
|
+
target: Object.freeze({ ...input.target }),
|
|
257
|
+
...input.invalidation === void 0 ? {} : { invalidation: Object.freeze({ ...input.invalidation }) },
|
|
258
|
+
captured: Object.freeze({
|
|
259
|
+
eligible: input.captured.eligible,
|
|
260
|
+
revision: snapshotEmailRevision(input.captured.revision),
|
|
261
|
+
...input.captured.targetIdentifierRevision === void 0 ? {} : { targetIdentifierRevision: input.captured.targetIdentifierRevision },
|
|
262
|
+
...source === void 0 ? {} : { source }
|
|
263
|
+
}),
|
|
264
|
+
authorization: Object.freeze({
|
|
265
|
+
challenge: Object.freeze({
|
|
266
|
+
...input.authorization.challenge,
|
|
267
|
+
target: Object.freeze({ ...input.authorization.challenge.target }),
|
|
268
|
+
revision: snapshotEmailRevision(input.authorization.challenge.revision)
|
|
269
|
+
}),
|
|
270
|
+
evidence,
|
|
271
|
+
requirement
|
|
272
|
+
})
|
|
273
|
+
});
|
|
274
|
+
});
|
|
275
|
+
const validateEmailAuthority = Effect.fn("DrizzleEmail.validateAuthority")(function* (mapping, configuration, input, action) {
|
|
276
|
+
const confirmsExisting = action === "verify-address" && input.captured.targetIdentifierRevision !== void 0;
|
|
277
|
+
if (!input.captured.eligible || !completionMatches(input, action) || input.authorization.challenge.moduleId !== input.moduleId || input.authorization.challenge.action !== action || input.authorization.challenge.commandId !== input.commandId || !sameIdentifier(input.authorization.challenge.target, input.target) || input.authorization.challenge.sourceCredentialId !== input.captured.source?.credentialId || !sameEmailRevision(input.authorization.challenge.revision, input.captured.revision) || input.authorization.challenge.targetIdentifierRevision !== input.captured.targetIdentifierRevision || input.authorization.evidence.flowId !== input.commandId || input.authorization.evidence.bindingDigest !== input.authorization.challenge.bindingDigest || !sameEmailRevision(input.authorization.evidence.revision, input.captured.revision) || action === "verify-address" && input.captured.source !== void 0 || action === "change-address" && input.captured.source === void 0 || confirmsExisting !== (input.invalidation === void 0) || input.invalidation?.existingSessions === "immediate" && mapping.sessionInvalidation !== "same-authority-immediate") return void 0;
|
|
278
|
+
const current = yield* currentAddress(mapping, {
|
|
279
|
+
moduleId: input.moduleId,
|
|
280
|
+
subjectId: input.captured.revision.subjectId,
|
|
281
|
+
target: input.target,
|
|
282
|
+
...input.captured.source === void 0 ? {} : { sourceCredentialId: input.captured.source.credentialId }
|
|
283
|
+
}, configuration.locking);
|
|
284
|
+
if (current === void 0 || !current.eligible || !sameEmailRevision(current.revision, input.captured.revision) || current.targetIdentifierRevision !== input.captured.targetIdentifierRevision || input.captured.source !== void 0 && (current.source === void 0 || current.source.snapshot.identifierRevision !== input.captured.source.identifierRevision || current.source.snapshot.credentialRevision !== input.captured.source.credentialRevision || !sameIdentifier(current.source.snapshot.identifier, input.captured.source.identifier))) return void 0;
|
|
285
|
+
const currentRequirement = yield* mapping.subject.decodeActionRequirement(current.subject, action);
|
|
286
|
+
const original = yield* assessAuthentication(input.authorization.evidence, input.authorization.requirement).pipe(Effect.mapError(unavailable));
|
|
287
|
+
const configured = yield* assessAuthentication(input.authorization.evidence, currentRequirement).pipe(Effect.mapError(unavailable));
|
|
288
|
+
return original.satisfied && configured.satisfied ? current : void 0;
|
|
289
|
+
});
|
|
290
|
+
const commandExists = Effect.fn("Drizzle.commandExists")(function* (mapping, moduleId, commandId, locking) {
|
|
291
|
+
const database = yield* CurrentEmailSql;
|
|
292
|
+
const c = commandColumns(mapping);
|
|
293
|
+
return yield* selectRows(database.select().from(mapping.command.table).where(and(eq(c.moduleId, moduleId), eq(c.commandId, commandId))).limit(1), locking);
|
|
294
|
+
});
|
|
295
|
+
const applyMutation = Effect.fn("DrizzleEmail.applyMutation")(function* (mapping, input, action, current, allocated, now) {
|
|
296
|
+
const database = yield* CurrentEmailSql;
|
|
297
|
+
const i = identifierColumns(mapping);
|
|
298
|
+
const c = credentialColumns(mapping);
|
|
299
|
+
const a = authorityColumns(mapping);
|
|
300
|
+
const s = subjectColumns(mapping);
|
|
301
|
+
const targetCredentialId = current.targetCredential?.[mapping.credential.credentialId] ?? allocated.targetCredentialId;
|
|
302
|
+
if (action === "change-address") {
|
|
303
|
+
const source = current.source;
|
|
304
|
+
yield* database.update(mapping.identifier.table).set(mapping.identifier.encodeRetirement({
|
|
305
|
+
source: source.snapshot.identifier,
|
|
306
|
+
bindingRevision: allocated.sourceIdentifierRevision
|
|
307
|
+
})).where(and(eq(i.namespace, source.snapshot.identifier.namespace), eq(i.value, source.snapshot.identifier.value), eq(i.subjectId, current.nativeSubjectId), eq(i.bindingRevision, source.snapshot.identifierRevision)));
|
|
308
|
+
yield* database.update(mapping.credential.table).set(mapping.credential.encodeRetirement({
|
|
309
|
+
source: source.snapshot.identifier,
|
|
310
|
+
credentialRevision: allocated.sourceCredentialRevision
|
|
311
|
+
})).where(and(eq(c.moduleId, input.moduleId), eq(c.credentialId, source.snapshot.credentialId), eq(c.subjectId, current.nativeSubjectId), eq(c.credentialRevision, source.snapshot.credentialRevision)));
|
|
312
|
+
yield* database.update(mapping.authorityCredential.table).set(mapping.authorityCredential.encodeRetirement(allocated.sourceCredentialRevision)).where(and(eq(a.subjectId, current.nativeSubjectId), eq(a.credentialId, source.snapshot.credentialId), eq(a.revision, source.snapshot.credentialRevision)));
|
|
313
|
+
}
|
|
314
|
+
if (current.targetIdentifier === void 0) yield* database.insert(mapping.identifier.table).values(mapping.identifier.encodeVerifiedInsert({
|
|
315
|
+
identifier: input.target,
|
|
316
|
+
subjectId: current.nativeSubjectId,
|
|
317
|
+
verifiedAtMillis: now,
|
|
318
|
+
bindingRevision: allocated.targetIdentifierRevision
|
|
319
|
+
}));
|
|
320
|
+
else yield* database.update(mapping.identifier.table).set(mapping.identifier.encodeVerification({
|
|
321
|
+
verifiedAtMillis: now,
|
|
322
|
+
bindingRevision: allocated.targetIdentifierRevision
|
|
323
|
+
})).where(and(eq(i.namespace, input.target.namespace), eq(i.value, input.target.value), eq(i.subjectId, current.nativeSubjectId), eq(i.bindingRevision, current.targetIdentifier[mapping.identifier.bindingRevision])));
|
|
324
|
+
if (current.targetCredential === void 0) {
|
|
325
|
+
yield* database.insert(mapping.credential.table).values(mapping.credential.encodeVerifiedInsert({
|
|
326
|
+
moduleId: input.moduleId,
|
|
327
|
+
subjectId: current.nativeSubjectId,
|
|
328
|
+
credentialId: targetCredentialId,
|
|
329
|
+
identifier: input.target,
|
|
330
|
+
credentialRevision: allocated.targetCredentialRevision
|
|
331
|
+
}));
|
|
332
|
+
yield* database.insert(mapping.authorityCredential.table).values(mapping.authorityCredential.encodeInsert({
|
|
333
|
+
subjectId: current.nativeSubjectId,
|
|
334
|
+
credentialId: targetCredentialId,
|
|
335
|
+
revision: allocated.targetCredentialRevision
|
|
336
|
+
}));
|
|
337
|
+
} else {
|
|
338
|
+
const oldRevision = current.targetCredential[mapping.credential.credentialRevision];
|
|
339
|
+
yield* database.update(mapping.credential.table).set(mapping.credential.encodeActivation({
|
|
340
|
+
identifier: input.target,
|
|
341
|
+
credentialRevision: allocated.targetCredentialRevision
|
|
342
|
+
})).where(and(eq(c.moduleId, input.moduleId), eq(c.credentialId, targetCredentialId), eq(c.subjectId, current.nativeSubjectId), eq(c.credentialRevision, oldRevision)));
|
|
343
|
+
yield* database.update(mapping.authorityCredential.table).set(mapping.authorityCredential.encodeActivation(allocated.targetCredentialRevision)).where(and(eq(a.subjectId, current.nativeSubjectId), eq(a.credentialId, targetCredentialId), eq(a.revision, oldRevision)));
|
|
344
|
+
}
|
|
345
|
+
yield* database.update(mapping.subject.table).set(updateValues([[mapping.subject.securityRevision, allocated.nextSecurityRevision]])).where(and(eq(s.id, current.nativeSubjectId), eq(s.securityRevision, input.captured.revision.securityRevision)));
|
|
346
|
+
yield* database.insert(mapping.command.table).values(mapping.command.encodeInsert({
|
|
347
|
+
moduleId: input.moduleId,
|
|
348
|
+
commandId: input.commandId,
|
|
349
|
+
action,
|
|
350
|
+
bindingDigest: input.authorization.challenge.bindingDigest,
|
|
351
|
+
retentionUntilMillis: now + mapping.commandRetentionMillis
|
|
352
|
+
}));
|
|
353
|
+
const sourceStillActive = action === "verify-address" ? [] : yield* selectRows(database.select().from(mapping.credential.table).where(and(eq(c.moduleId, input.moduleId), eq(c.credentialId, current.source.snapshot.credentialId), eq(c.subjectId, current.nativeSubjectId))), false);
|
|
354
|
+
const target = (yield* emailLookupRows(mapping, input.moduleId, input.target))[0];
|
|
355
|
+
const targetAuthority = yield* selectRows(database.select().from(mapping.authorityCredential.table).where(and(eq(a.subjectId, current.nativeSubjectId), eq(a.credentialId, targetCredentialId))).limit(1), false);
|
|
356
|
+
const command = yield* selectRows(database.select().from(mapping.command.table).where(and(eq(commandColumns(mapping).moduleId, input.moduleId), eq(commandColumns(mapping).commandId, input.commandId), eq(commandColumns(mapping).action, action), eq(commandColumns(mapping).bindingDigest, input.authorization.challenge.bindingDigest), eq(commandColumns(mapping).retentionUntil, mapping.encodeInstant(now + mapping.commandRetentionMillis)))).limit(1), false);
|
|
357
|
+
const targetVerifiedAt = target === void 0 ? void 0 : yield* mapping.decodeInstant(target.identifier[mapping.identifier.verifiedAt]);
|
|
358
|
+
const sourceIdentifier = action === "verify-address" ? [] : yield* identifierRow(mapping, current.source.snapshot.identifier, false);
|
|
359
|
+
const sourceAuthority = action === "verify-address" ? [] : yield* selectRows(database.select().from(mapping.authorityCredential.table).where(and(eq(a.subjectId, current.nativeSubjectId), eq(a.credentialId, current.source.snapshot.credentialId))).limit(1), false);
|
|
360
|
+
return target !== void 0 && mapping.subjectId.equals(target.subject[mapping.subject.id], current.nativeSubjectId) && mapping.subject.isActiveStatus(target.subject[mapping.subject.status]) && target.subject[mapping.subject.securityRevision] === allocated.nextSecurityRevision && target.identifier[mapping.identifier.bindingRevision] === allocated.targetIdentifierRevision && targetVerifiedAt === now && target.credential[mapping.credential.credentialRevision] === allocated.targetCredentialRevision && target.credential[mapping.credential.credentialId] === targetCredentialId && mapping.subjectId.equals(target.credential[mapping.credential.subjectId], current.nativeSubjectId) && mapping.identifier.isCurrent(target.identifier) && mapping.credential.isActiveStatus(target.credential[mapping.credential.status]) && targetAuthority.length === 1 && targetAuthority[0][mapping.authorityCredential.revision] === allocated.targetCredentialRevision && mapping.authorityCredential.isActiveStatus(targetAuthority[0][mapping.authorityCredential.status]) && command.length === 1 && (action === "verify-address" || sourceStillActive.length === 1 && sourceStillActive[0][mapping.credential.credentialRevision] === allocated.sourceCredentialRevision && !mapping.credential.isActiveStatus(sourceStillActive[0][mapping.credential.status]) && sourceIdentifier.length === 1 && mapping.subjectId.equals(sourceIdentifier[0][mapping.identifier.subjectId], current.nativeSubjectId) && sourceIdentifier[0][mapping.identifier.bindingRevision] === allocated.sourceIdentifierRevision && !mapping.identifier.isCurrent(sourceIdentifier[0]) && sourceAuthority.length === 1 && sourceAuthority[0][mapping.authorityCredential.revision] === allocated.sourceCredentialRevision && !mapping.authorityCredential.isActiveStatus(sourceAuthority[0][mapping.authorityCredential.status]));
|
|
361
|
+
});
|
|
362
|
+
const probeProofCompletion = Effect.fn("Drizzle.probeProofCompletion")(function* (configuration, input) {
|
|
363
|
+
const database = yield* CurrentEmailSql;
|
|
364
|
+
if (configuration.proof === void 0) return yield* Effect.fail(unavailable());
|
|
365
|
+
const validProbe = { _tag: "EmailProofCompletionValid" };
|
|
366
|
+
const plan = {
|
|
367
|
+
input,
|
|
368
|
+
prepare: (decision, journal, project) => journal.prepare(project(decision))
|
|
369
|
+
};
|
|
370
|
+
return yield* coordinateCommit(() => database.transaction((transaction) => completeProofPlanIn(configuration.proof.mapping, configuration.proof.configuration, plan, Effect.fail(validProbe), () => false).pipe(Effect.provideService(CurrentProofSql, transaction))), { mode: configuration.mode }).pipe(Effect.as(false), Effect.catchCause((cause) => {
|
|
371
|
+
const reason = cause.reasons[0];
|
|
372
|
+
return cause.reasons.length === 1 && reason !== void 0 && Cause.isFailReason(reason) && reason.error === validProbe ? Effect.succeed(true) : Effect.failCause(cause);
|
|
373
|
+
}));
|
|
374
|
+
});
|
|
375
|
+
const prepareRejected = Effect.fn("Drizzle.prepareRejected")(function* (configuration, prepare) {
|
|
376
|
+
const database = yield* CurrentEmailSql;
|
|
377
|
+
return yield* owned(database, configuration, Effect.gen(function* () {
|
|
378
|
+
const journal = yield* CurrentCommitJournal;
|
|
379
|
+
return yield* Effect.succeed(prepare("rejected", journal));
|
|
380
|
+
}));
|
|
381
|
+
});
|
|
382
|
+
const makeSqlEmailAddressPersistence = Effect.fn("makeSqlEmailAddressPersistence")(function* (database, mapping, configuration) {
|
|
383
|
+
const hooks = yield* LifecycleHooks;
|
|
384
|
+
return EmailAddressPersistence.of({
|
|
385
|
+
target: (input) => (validAddressConstraints(mapping) ? safeRead(database, configuration, Effect.gen(function* () {
|
|
386
|
+
const current = yield* currentAddress(mapping, input, configuration.locking);
|
|
387
|
+
if (current === void 0) return yield* unavailable();
|
|
388
|
+
return Object.freeze({
|
|
389
|
+
revision: snapshotEmailRevision(current.revision),
|
|
390
|
+
eligible: current.eligible,
|
|
391
|
+
...current.targetIdentifierRevision === void 0 ? {} : { targetIdentifierRevision: current.targetIdentifierRevision },
|
|
392
|
+
...current.source === void 0 ? {} : { source: yield* snapshotEmailCredential(current.source.snapshot) }
|
|
393
|
+
});
|
|
394
|
+
})) : Effect.fail(unavailable())).pipe(Effect.provideService(CurrentEmailSql, database), Effect.provideService(LifecycleHooks, hooks), translateFailure),
|
|
395
|
+
checkCompletion: (input) => {
|
|
396
|
+
if (configuration.proof === void 0 || input.binding._tag !== "IdentifierChange") return Effect.succeed(false).pipe(Effect.provideService(CurrentEmailSql, database), Effect.provideService(LifecycleHooks, hooks), translateFailure);
|
|
397
|
+
const binding = input.binding;
|
|
398
|
+
return safeRead(database, configuration, Effect.gen(function* () {
|
|
399
|
+
const action = input.moduleId.endsWith("/verify-address") && input.purpose === "email-address-verification" ? "verify-address" : input.moduleId.endsWith("/change-address") && input.purpose === "email-address-change" ? "change-address" : void 0;
|
|
400
|
+
if (action === void 0) return false;
|
|
401
|
+
const moduleId = input.moduleId.slice(0, -`/${action}`.length);
|
|
402
|
+
const current = yield* currentAddress(mapping, {
|
|
403
|
+
moduleId,
|
|
404
|
+
subjectId: binding.revision.subjectId,
|
|
405
|
+
target: binding.identifier
|
|
406
|
+
}, false);
|
|
407
|
+
return current !== void 0 && sameEmailRevision(current.revision, binding.revision);
|
|
408
|
+
})).pipe(Effect.flatMap((current) => current ? probeProofCompletion(configuration, input) : Effect.succeed(false))).pipe(Effect.provideService(CurrentEmailSql, database), Effect.provideService(LifecycleHooks, hooks), translateFailure);
|
|
409
|
+
},
|
|
410
|
+
verifyWithProof: (uncaptured, prepare) => mutateAddress(mapping, configuration, uncaptured, "verify-address", prepare).pipe(Effect.provideService(CurrentEmailSql, database), Effect.provideService(LifecycleHooks, hooks), translateFailure),
|
|
411
|
+
changeWithProof: (uncaptured, prepare) => mutateAddress(mapping, configuration, uncaptured, "change-address", prepare).pipe(Effect.provideService(CurrentEmailSql, database), Effect.provideService(LifecycleHooks, hooks), translateFailure),
|
|
412
|
+
cleanup: (input, prepare) => owned(database, configuration, Effect.gen(function* () {
|
|
413
|
+
const transaction = yield* CurrentEmailSql;
|
|
414
|
+
const journal = yield* CurrentCommitJournal;
|
|
415
|
+
const c = commandColumns(mapping);
|
|
416
|
+
const now = yield* nowMillis;
|
|
417
|
+
const rows = yield* selectRows(transaction.select({ commandId: c.commandId }).from(mapping.command.table).where(and(eq(c.moduleId, input.moduleId), lte(c.retentionUntil, mapping.encodeInstant(now)))).limit(input.limit + 1), configuration.locking);
|
|
418
|
+
const selected = rows.slice(0, input.limit);
|
|
419
|
+
const receipt = prepare({
|
|
420
|
+
removed: selected.length,
|
|
421
|
+
hasMore: rows.length > input.limit
|
|
422
|
+
}, journal);
|
|
423
|
+
if (selected.length > 0) yield* transaction.delete(mapping.command.table).where(and(eq(c.moduleId, input.moduleId), inArray(c.commandId, selected.map((row) => row.commandId)), lte(c.retentionUntil, mapping.encodeInstant(now))));
|
|
424
|
+
return receipt;
|
|
425
|
+
})).pipe(Effect.provideService(CurrentEmailSql, database), Effect.provideService(LifecycleHooks, hooks), translateFailure)
|
|
426
|
+
});
|
|
427
|
+
});
|
|
428
|
+
const mutateAddress = Effect.fn("Drizzle.mutateAddress")(function* (mapping, configuration, uncaptured, action, prepare) {
|
|
429
|
+
const database = yield* CurrentEmailSql;
|
|
430
|
+
if (!validAddressConstraints(mapping) || configuration.proof === void 0) return yield* unavailable();
|
|
431
|
+
const input = yield* snapshotEmailMutation(uncaptured);
|
|
432
|
+
const targetCredentialId = yield* allocate(configuration.mode, mapping.allocateCredentialId, mapping.allocateCredentialIdSync);
|
|
433
|
+
const targetIdentifierRevision = yield* allocate(configuration.mode, mapping.allocateRevision, mapping.allocateRevisionSync);
|
|
434
|
+
const targetCredentialRevision = yield* allocate(configuration.mode, mapping.allocateRevision, mapping.allocateRevisionSync);
|
|
435
|
+
const sourceIdentifierRevision = yield* allocate(configuration.mode, mapping.allocateRevision, mapping.allocateRevisionSync);
|
|
436
|
+
const sourceCredentialRevision = yield* allocate(configuration.mode, mapping.allocateRevision, mapping.allocateRevisionSync);
|
|
437
|
+
const confirmsExisting = action === "verify-address" && input.captured.targetIdentifierRevision !== void 0;
|
|
438
|
+
const nextRevision = confirmsExisting ? input.captured.revision.securityRevision : yield* nextSecurityRevision(mapping, configuration.mode, input.captured.revision.securityRevision);
|
|
439
|
+
if (!confirmsExisting && nextRevision === input.captured.revision.securityRevision || [
|
|
440
|
+
targetIdentifierRevision,
|
|
441
|
+
targetCredentialRevision,
|
|
442
|
+
sourceIdentifierRevision,
|
|
443
|
+
sourceCredentialRevision
|
|
444
|
+
].some((value) => input.captured.revision.credentials.some((item) => item.revision === value))) return yield* unavailable();
|
|
445
|
+
return yield* owned(database, configuration, Effect.gen(function* () {
|
|
446
|
+
const journal = yield* CurrentCommitJournal;
|
|
447
|
+
const current = yield* validateEmailAuthority(mapping, configuration, input, action);
|
|
448
|
+
if (current === void 0) return prepare("rejected", journal);
|
|
449
|
+
if ((yield* commandExists(mapping, input.moduleId, input.commandId, configuration.locking)).length > 0) return prepare("rejected", journal);
|
|
450
|
+
let receipt;
|
|
451
|
+
yield* completeProofPlanIn(configuration.proof.mapping, configuration.proof.configuration, input.completion, Effect.gen(function* () {
|
|
452
|
+
const currentRequirement = yield* mapping.subject.decodeActionRequirement(current.subject, action);
|
|
453
|
+
const original = yield* assessAuthentication(input.authorization.evidence, input.authorization.requirement).pipe(Effect.mapError(unavailable));
|
|
454
|
+
const configured = yield* assessAuthentication(input.authorization.evidence, currentRequirement).pipe(Effect.mapError(unavailable));
|
|
455
|
+
if (!original.satisfied || !configured.satisfied) return false;
|
|
456
|
+
const now = yield* nowMillis;
|
|
457
|
+
return yield* applyMutation(mapping, input, action, current, {
|
|
458
|
+
targetCredentialId,
|
|
459
|
+
targetIdentifierRevision,
|
|
460
|
+
targetCredentialRevision,
|
|
461
|
+
sourceIdentifierRevision,
|
|
462
|
+
sourceCredentialRevision,
|
|
463
|
+
nextSecurityRevision: nextRevision
|
|
464
|
+
}, now);
|
|
465
|
+
}), (decision) => {
|
|
466
|
+
receipt = prepare(decision === "completed" ? "changed" : "rejected", journal);
|
|
467
|
+
return decision;
|
|
468
|
+
});
|
|
469
|
+
return receipt ?? prepare("rejected", journal);
|
|
470
|
+
})).pipe(Effect.catchCause((cause) => cause.reasons.every(Cause.isFailReason) && isMappedConstraintConflict(mapping.isCommandConflict, cause) || cause.reasons.every(Cause.isFailReason) && isMappedConstraintConflict(mapping.isIdentifierConflict, cause) || cause.reasons.every(Cause.isFailReason) && isMappedConstraintConflict(mapping.isCredentialConflict, cause) ? prepareRejected(configuration, prepare) : Effect.failCause(cause)));
|
|
471
|
+
});
|
|
472
|
+
return {
|
|
473
|
+
validEmailSignInConstraints,
|
|
474
|
+
emailLookupQuery,
|
|
475
|
+
decodeEmailSnapshot,
|
|
476
|
+
makeSqlEmailSignInTargets,
|
|
477
|
+
currentAddress,
|
|
478
|
+
sameEmailRevision,
|
|
479
|
+
snapshotEmailMutation,
|
|
480
|
+
validateEmailAuthority,
|
|
481
|
+
makeSqlEmailAddressPersistence
|
|
482
|
+
};
|
|
483
|
+
};
|
|
484
|
+
//#endregion
|
|
485
|
+
export { CurrentEmailSql, makeEmailKernel };
|
|
486
|
+
|
|
487
|
+
//# sourceMappingURL=email-kernel.mjs.map
|