@yielded/auth-persistence 0.1.0-beta.23 → 0.1.0-beta.25
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 +11 -0
- package/dist/Adapter.d.mts +12 -6
- package/dist/Adapter.mjs +8 -3
- package/dist/OAuthPersistence.d.mts +87 -0
- package/dist/OAuthPersistence.mjs +125 -0
- package/dist/OAuthPersistence.mjs.map +1 -0
- package/dist/_virtual/_rolldown/runtime.mjs +13 -0
- package/dist/index.d.mts +3 -1
- package/dist/index.mjs +3 -1
- package/dist/internal/configuration.d.mts +1 -1
- package/dist/internal/configuration.mjs.map +1 -1
- package/dist/internal/email-kernel.d.mts +1 -1
- package/dist/internal/email-kernel.mjs +1 -1
- package/dist/internal/email-kernel.mjs.map +1 -1
- package/dist/internal/models/oauth-connected-model.d.mts +294 -0
- package/dist/internal/models/oauth-connected-model.mjs +22 -0
- package/dist/internal/models/oauth-connected-model.mjs.map +1 -0
- package/dist/internal/models/oauth-model.d.mts +467 -0
- package/dist/internal/models/oauth-model.mjs +49 -0
- package/dist/internal/models/oauth-model.mjs.map +1 -0
- package/dist/internal/models/password-model.d.mts +6 -48
- package/dist/internal/models/password-model.mjs +0 -1
- package/dist/internal/models/password-model.mjs.map +1 -1
- package/dist/internal/native-sql-table.d.mts +4 -0
- package/dist/internal/native-sql-table.mjs +7 -0
- package/dist/internal/native-sql-table.mjs.map +1 -1
- package/dist/internal/oauth/accounts.d.mts +8 -0
- package/dist/internal/oauth/accounts.mjs +379 -0
- package/dist/internal/oauth/accounts.mjs.map +1 -0
- package/dist/internal/oauth/connected-access.d.mts +9 -0
- package/dist/internal/oauth/connected-access.mjs +229 -0
- package/dist/internal/oauth/connected-access.mjs.map +1 -0
- package/dist/internal/oauth/connected-collection.d.mts +7 -0
- package/dist/internal/oauth/connected-collection.mjs +214 -0
- package/dist/internal/oauth/connected-collection.mjs.map +1 -0
- package/dist/internal/oauth/connected-custody.d.mts +6 -0
- package/dist/internal/oauth/connected-custody.mjs +165 -0
- package/dist/internal/oauth/connected-custody.mjs.map +1 -0
- package/dist/internal/oauth/connected-flow.d.mts +7 -0
- package/dist/internal/oauth/connected-flow.mjs +324 -0
- package/dist/internal/oauth/connected-flow.mjs.map +1 -0
- package/dist/internal/oauth/connected-input.mjs +122 -0
- package/dist/internal/oauth/connected-input.mjs.map +1 -0
- package/dist/internal/oauth/connected-keys.d.mts +2 -0
- package/dist/internal/oauth/connected-keys.mjs +22 -0
- package/dist/internal/oauth/connected-keys.mjs.map +1 -0
- package/dist/internal/oauth/connected-maintenance.d.mts +9 -0
- package/dist/internal/oauth/connected-maintenance.mjs +226 -0
- package/dist/internal/oauth/connected-maintenance.mjs.map +1 -0
- package/dist/internal/oauth/connected-management.d.mts +9 -0
- package/dist/internal/oauth/connected-management.mjs +243 -0
- package/dist/internal/oauth/connected-management.mjs.map +1 -0
- package/dist/internal/oauth/connected-reference.mjs +63 -0
- package/dist/internal/oauth/connected-reference.mjs.map +1 -0
- package/dist/internal/oauth/connected-settlement.d.mts +8 -0
- package/dist/internal/oauth/connected-settlement.mjs +141 -0
- package/dist/internal/oauth/connected-settlement.mjs.map +1 -0
- package/dist/internal/oauth/connected-sign-in.d.mts +10 -0
- package/dist/internal/oauth/connected-sign-in.mjs +281 -0
- package/dist/internal/oauth/connected-sign-in.mjs.map +1 -0
- package/dist/internal/oauth/connected-state.d.mts +14 -0
- package/dist/internal/oauth/connected-state.mjs +469 -0
- package/dist/internal/oauth/connected-state.mjs.map +1 -0
- package/dist/internal/oauth/connected-target.d.mts +21 -0
- package/dist/internal/oauth/connected-target.mjs +117 -0
- package/dist/internal/oauth/connected-target.mjs.map +1 -0
- package/dist/internal/oauth/flow.d.mts +5 -0
- package/dist/internal/oauth/flow.mjs +279 -0
- package/dist/internal/oauth/flow.mjs.map +1 -0
- package/dist/internal/oauth/input.mjs +127 -0
- package/dist/internal/oauth/input.mjs.map +1 -0
- package/dist/internal/oauth/owner.d.mts +10 -0
- package/dist/internal/oauth/owner.mjs +10 -0
- package/dist/internal/oauth/owner.mjs.map +1 -0
- package/dist/internal/oauth/query-compiler.d.mts +7 -0
- package/dist/internal/oauth/query-compiler.mjs +9 -0
- package/dist/internal/oauth/query-compiler.mjs.map +1 -0
- package/dist/internal/oauth/registration.d.mts +7 -0
- package/dist/internal/oauth/registration.mjs +393 -0
- package/dist/internal/oauth/registration.mjs.map +1 -0
- package/dist/internal/oauth/state.mjs +107 -0
- package/dist/internal/oauth/state.mjs.map +1 -0
- package/dist/internal/oauth/target.d.mts +31 -0
- package/dist/internal/oauth/target.mjs +273 -0
- package/dist/internal/oauth/target.mjs.map +1 -0
- package/dist/internal/oauth-kernel.d.mts +1423 -0
- package/dist/internal/oauth-kernel.mjs +187 -0
- package/dist/internal/oauth-kernel.mjs.map +1 -0
- package/dist/internal/oauth-proxy.d.mts +31 -0
- package/dist/internal/oauth-proxy.mjs +129 -0
- package/dist/internal/oauth-proxy.mjs.map +1 -0
- package/dist/internal/passkey/write-state.mjs +9 -1
- package/dist/internal/passkey/write-state.mjs.map +1 -1
- package/dist/internal/passkey-kernel.d.mts +9 -9
- package/dist/internal/{password-attempts.d.mts → password-credentials.d.mts} +3 -4
- package/dist/internal/password-credentials.mjs +121 -0
- package/dist/internal/password-credentials.mjs.map +1 -0
- package/dist/internal/password-kernel.d.mts +8 -24
- package/dist/internal/password-kernel.mjs +5 -43
- package/dist/internal/password-kernel.mjs.map +1 -1
- package/dist/internal/persistence.d.mts +1 -1
- package/dist/internal/persistence.mjs +2 -2
- package/dist/internal/persistence.mjs.map +1 -1
- package/dist/internal/query-operations.d.mts +2 -0
- package/dist/internal/session-kernel.d.mts +2 -2
- package/dist/internal/session-kernel.mjs +74 -18
- package/dist/internal/session-kernel.mjs.map +1 -1
- package/dist/internal/sql-oauth-connected-model.d.mts +29 -0
- package/dist/internal/sql-oauth-connected-model.mjs +7 -0
- package/dist/internal/sql-oauth-connected-model.mjs.map +1 -0
- package/dist/internal/sql-oauth-model.d.mts +60 -0
- package/dist/internal/sql-oauth-model.mjs +10 -0
- package/dist/internal/sql-oauth-model.mjs.map +1 -0
- package/dist/internal/sql-query.d.mts +12 -0
- package/dist/internal/sql-query.mjs +15 -18
- package/dist/internal/sql-query.mjs.map +1 -1
- package/dist/internal/sql-storage-validation.mjs +54 -0
- package/dist/internal/sql-storage-validation.mjs.map +1 -0
- package/dist/internal/storage-mapping.mjs +1 -37
- package/dist/internal/storage-mapping.mjs.map +1 -1
- package/dist/internal/storage-plans.d.mts +7 -0
- package/dist/internal/storage-plans.mjs +189 -0
- package/dist/internal/storage-plans.mjs.map +1 -0
- package/dist/internal/storage-tables.d.mts +0 -56
- package/dist/internal/storage-tables.mjs +0 -17
- package/dist/internal/storage-tables.mjs.map +1 -1
- package/dist/internal/storage-validation.d.mts +6 -1
- package/dist/internal/storage-validation.mjs +42 -1
- package/dist/internal/storage-validation.mjs.map +1 -1
- package/dist/internal/transaction-kernel.mjs +3 -2
- package/dist/internal/transaction-kernel.mjs.map +1 -1
- package/package.json +1 -1
- package/src/Adapter.ts +89 -5
- package/src/OAuthPersistence.ts +427 -0
- package/src/index.ts +3 -0
- package/src/internal/configuration.ts +1 -3
- package/src/internal/email-kernel.ts +2 -1
- package/src/internal/models/oauth-connected-model.ts +396 -0
- package/src/internal/models/oauth-model.ts +708 -0
- package/src/internal/models/password-model.ts +2 -57
- package/src/internal/native-sql-table.ts +14 -0
- package/src/internal/oauth/accounts.ts +839 -0
- package/src/internal/oauth/connected-access.ts +388 -0
- package/src/internal/oauth/connected-collection.ts +345 -0
- package/src/internal/oauth/connected-custody.ts +308 -0
- package/src/internal/oauth/connected-flow.ts +562 -0
- package/src/internal/oauth/connected-input.ts +147 -0
- package/src/internal/oauth/connected-keys.ts +31 -0
- package/src/internal/oauth/connected-maintenance.ts +379 -0
- package/src/internal/oauth/connected-management.ts +391 -0
- package/src/internal/oauth/connected-reference.ts +135 -0
- package/src/internal/oauth/connected-settlement.ts +278 -0
- package/src/internal/oauth/connected-sign-in.ts +472 -0
- package/src/internal/oauth/connected-state.ts +924 -0
- package/src/internal/oauth/connected-target.ts +307 -0
- package/src/internal/oauth/flow.ts +505 -0
- package/src/internal/oauth/input.ts +201 -0
- package/src/internal/oauth/owner.ts +22 -0
- package/src/internal/oauth/query-compiler.ts +15 -0
- package/src/internal/oauth/registration.ts +676 -0
- package/src/internal/oauth/state.ts +218 -0
- package/src/internal/oauth/target.ts +803 -0
- package/src/internal/oauth-kernel.ts +294 -0
- package/src/internal/oauth-proxy.ts +176 -0
- package/src/internal/passkey/write-state.ts +18 -4
- package/src/internal/password-credentials.ts +245 -0
- package/src/internal/password-kernel.ts +7 -99
- package/src/internal/persistence.ts +1 -1
- package/src/internal/query-operations.ts +2 -0
- package/src/internal/session-kernel.ts +143 -55
- package/src/internal/sql-oauth-connected-model.ts +145 -0
- package/src/internal/sql-oauth-model.ts +263 -0
- package/src/internal/sql-query.ts +16 -13
- package/src/internal/sql-storage-validation.ts +64 -0
- package/src/internal/storage-mapping.ts +0 -37
- package/src/internal/storage-plans.ts +218 -0
- package/src/internal/storage-tables.ts +0 -20
- package/src/internal/storage-validation.ts +58 -0
- package/src/internal/transaction-kernel.ts +6 -2
- package/dist/internal/password-attempts.mjs +0 -197
- package/dist/internal/password-attempts.mjs.map +0 -1
- package/src/internal/password-attempts.ts +0 -466
|
@@ -0,0 +1,839 @@
|
|
|
1
|
+
/* oxlint-disable no-explicit-any -- compiler table shapes are erased; domain codecs and error channels remain typed. */
|
|
2
|
+
import {
|
|
3
|
+
OAuthAccountRevision,
|
|
4
|
+
OAuthActionAuthorization,
|
|
5
|
+
OAuthLinkAccess,
|
|
6
|
+
OAuthLinkClaim,
|
|
7
|
+
OAuthLinkPendingFlow,
|
|
8
|
+
OAuthLinkTransactionContext,
|
|
9
|
+
OAuthUnlinked,
|
|
10
|
+
OAuthCredentialSnapshot,
|
|
11
|
+
snapshotOAuthSync,
|
|
12
|
+
} from "@yielded/auth/OAuth";
|
|
13
|
+
import {
|
|
14
|
+
SessionInvalidationWindow,
|
|
15
|
+
AuthenticationRequirement,
|
|
16
|
+
SecurityRevision,
|
|
17
|
+
} from "@yielded/auth/Sessions";
|
|
18
|
+
import { DateTime, Effect, Context, Layer } from "effect";
|
|
19
|
+
|
|
20
|
+
import type { SubjectIdCodec } from "../models/common";
|
|
21
|
+
import type { OAuthReferenceGuardDescriptor, OAuthEligibilityFact } from "../models/oauth-model";
|
|
22
|
+
import { OAuthFlow } from "./flow";
|
|
23
|
+
import { CurrentOAuthTransaction } from "./owner";
|
|
24
|
+
import { OAuthQueryCompiler } from "./query-compiler";
|
|
25
|
+
import { OAuthRegistration } from "./registration";
|
|
26
|
+
import {
|
|
27
|
+
invariant,
|
|
28
|
+
oauthIdentityKey,
|
|
29
|
+
sameRevision,
|
|
30
|
+
satisfies,
|
|
31
|
+
storage,
|
|
32
|
+
validAction,
|
|
33
|
+
} from "./state";
|
|
34
|
+
|
|
35
|
+
const make = Effect.gen(function* () {
|
|
36
|
+
const operations = yield* OAuthQueryCompiler;
|
|
37
|
+
const flow = yield* OAuthFlow;
|
|
38
|
+
const registration = yield* OAuthRegistration;
|
|
39
|
+
|
|
40
|
+
const { eq, sql } = operations;
|
|
41
|
+
|
|
42
|
+
const {
|
|
43
|
+
claimFlow,
|
|
44
|
+
credentialStorage,
|
|
45
|
+
currentSubject,
|
|
46
|
+
exactClaim,
|
|
47
|
+
issueFlow,
|
|
48
|
+
linkStorage,
|
|
49
|
+
matchesAccess,
|
|
50
|
+
readFlow,
|
|
51
|
+
terminalFlow,
|
|
52
|
+
} = flow;
|
|
53
|
+
|
|
54
|
+
const { both, col, copiedRow, equal } = operations;
|
|
55
|
+
const { acquireTuple, readTuple } = registration;
|
|
56
|
+
|
|
57
|
+
const contextStorage = storage(OAuthLinkTransactionContext);
|
|
58
|
+
|
|
59
|
+
const unlinkedStorage = storage(OAuthUnlinked);
|
|
60
|
+
|
|
61
|
+
const accountCurrent = (mapping: any, subjectId: OAuthAccountRevision["subjectId"]) =>
|
|
62
|
+
Effect.gen(function* () {
|
|
63
|
+
const native = yield* (mapping.subjectId as SubjectIdCodec<unknown>).toNative(subjectId);
|
|
64
|
+
|
|
65
|
+
return yield* currentSubject(mapping, native);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const authorization = (
|
|
69
|
+
mapping: any,
|
|
70
|
+
current: any,
|
|
71
|
+
auth: OAuthActionAuthorization,
|
|
72
|
+
action: OAuthActionAuthorization["challenge"]["action"],
|
|
73
|
+
flowId: string,
|
|
74
|
+
commandId: string,
|
|
75
|
+
intent: string,
|
|
76
|
+
maximumAge: number,
|
|
77
|
+
) =>
|
|
78
|
+
Effect.flatMap(CurrentOAuthTransaction, (owner) =>
|
|
79
|
+
Effect.gen(function* () {
|
|
80
|
+
const now = yield* owner.now(mapping.clock);
|
|
81
|
+
|
|
82
|
+
const currentRequirement = snapshotOAuthSync(
|
|
83
|
+
AuthenticationRequirement,
|
|
84
|
+
mapping.subject.decodeActionRequirement(copiedRow(current.row), action),
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
const accepted = yield* validAction(
|
|
88
|
+
auth,
|
|
89
|
+
{
|
|
90
|
+
moduleId: auth.challenge.moduleId,
|
|
91
|
+
action,
|
|
92
|
+
flowId,
|
|
93
|
+
commandId,
|
|
94
|
+
revision: current.revision,
|
|
95
|
+
intent,
|
|
96
|
+
},
|
|
97
|
+
currentRequirement,
|
|
98
|
+
now,
|
|
99
|
+
maximumAge,
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
if (!accepted) return false;
|
|
103
|
+
|
|
104
|
+
const age = Math.min(
|
|
105
|
+
maximumAge,
|
|
106
|
+
auth.requirement.maximumAgeMillis,
|
|
107
|
+
currentRequirement.maximumAgeMillis,
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
const fresh = auth.evidence.proofs.filter(
|
|
111
|
+
(proof) => now - DateTime.toEpochMillis(proof.verifiedAt) < age,
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
invariant(fresh.length > 0);
|
|
115
|
+
|
|
116
|
+
const deadline = Math.min(
|
|
117
|
+
...fresh.map((proof) => DateTime.toEpochMillis(proof.verifiedAt) + age),
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
owner.postconditions.push(
|
|
121
|
+
sql`${mapping.clock.engineNowMillis} >= ${now} and ${mapping.clock.engineNowMillis} < ${deadline}`,
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
return true;
|
|
125
|
+
}),
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
const currentLink = (mapping: any, flow: typeof OAuthLinkPendingFlow.Type) =>
|
|
129
|
+
Effect.gen(function* () {
|
|
130
|
+
const current = yield* accountCurrent(mapping, flow.context.revision.subjectId);
|
|
131
|
+
|
|
132
|
+
return current !== undefined && sameRevision(current.revision, flow.context.revision)
|
|
133
|
+
? current
|
|
134
|
+
: undefined;
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
const issueLink = (mapping: any, input: any) =>
|
|
138
|
+
Effect.gen(function* () {
|
|
139
|
+
const flow = snapshotOAuthSync(OAuthLinkPendingFlow, input.flow),
|
|
140
|
+
c = flow.context;
|
|
141
|
+
|
|
142
|
+
const current = yield* currentLink(mapping, flow);
|
|
143
|
+
const auth = snapshotOAuthSync(OAuthActionAuthorization, input.authorization);
|
|
144
|
+
|
|
145
|
+
if (
|
|
146
|
+
current === undefined ||
|
|
147
|
+
auth.challenge.moduleId !== c.moduleId ||
|
|
148
|
+
!(yield* authorization(
|
|
149
|
+
mapping,
|
|
150
|
+
current,
|
|
151
|
+
auth,
|
|
152
|
+
"link-begin",
|
|
153
|
+
c.flowId,
|
|
154
|
+
c.commandId,
|
|
155
|
+
contextStorage.encode(c),
|
|
156
|
+
c.maximumEvidenceAgeMillis,
|
|
157
|
+
))
|
|
158
|
+
)
|
|
159
|
+
return { _tag: "Rejected" } as const;
|
|
160
|
+
|
|
161
|
+
return yield* issueFlow(mapping, flow, "link");
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
const preflightLink = (mapping: any, original: typeof OAuthLinkAccess.Type) =>
|
|
165
|
+
Effect.flatMap(CurrentOAuthTransaction, (owner) =>
|
|
166
|
+
Effect.gen(function* () {
|
|
167
|
+
const input = snapshotOAuthSync(OAuthLinkAccess, original);
|
|
168
|
+
// The authenticated subject is known before discovering/locking any flow.
|
|
169
|
+
const current = yield* accountCurrent(mapping, input.subjectId);
|
|
170
|
+
const found = yield* readFlow(mapping, input, "link");
|
|
171
|
+
const now = yield* owner.now(mapping.clock);
|
|
172
|
+
|
|
173
|
+
if (
|
|
174
|
+
current === undefined ||
|
|
175
|
+
found.flow === undefined ||
|
|
176
|
+
found.row?.[mapping.flow.state] !== "Pending" ||
|
|
177
|
+
!matchesAccess(found.flow, input)
|
|
178
|
+
)
|
|
179
|
+
return undefined;
|
|
180
|
+
|
|
181
|
+
const flow = snapshotOAuthSync(OAuthLinkPendingFlow, found.flow as never),
|
|
182
|
+
c = flow.context;
|
|
183
|
+
|
|
184
|
+
if (
|
|
185
|
+
c.revision.subjectId !== input.subjectId ||
|
|
186
|
+
!sameRevision(current.revision, c.revision) ||
|
|
187
|
+
now < c.issuedAtMillis ||
|
|
188
|
+
now >= c.expiresAtMillis
|
|
189
|
+
)
|
|
190
|
+
return undefined;
|
|
191
|
+
owner.postconditions.push(
|
|
192
|
+
sql`${mapping.clock.engineNowMillis} >= ${c.issuedAtMillis} and ${mapping.clock.engineNowMillis} < ${c.expiresAtMillis}`,
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
return { current, flow, row: found.row };
|
|
196
|
+
}),
|
|
197
|
+
);
|
|
198
|
+
|
|
199
|
+
const claimLink = (mapping: any, input: any) =>
|
|
200
|
+
Effect.gen(function* () {
|
|
201
|
+
const found = yield* preflightLink(mapping, input.access);
|
|
202
|
+
const flow = snapshotOAuthSync(OAuthLinkPendingFlow, input.flow);
|
|
203
|
+
|
|
204
|
+
const auth = snapshotOAuthSync(OAuthActionAuthorization, input.authorization),
|
|
205
|
+
c = flow.context;
|
|
206
|
+
|
|
207
|
+
if (
|
|
208
|
+
found === undefined ||
|
|
209
|
+
linkStorage.encode(found.flow) !== linkStorage.encode(flow) ||
|
|
210
|
+
auth.challenge.moduleId !== c.moduleId ||
|
|
211
|
+
!(yield* authorization(
|
|
212
|
+
mapping,
|
|
213
|
+
found.current,
|
|
214
|
+
auth,
|
|
215
|
+
"link-complete",
|
|
216
|
+
c.flowId,
|
|
217
|
+
c.commandId,
|
|
218
|
+
contextStorage.encode(c),
|
|
219
|
+
c.maximumEvidenceAgeMillis,
|
|
220
|
+
))
|
|
221
|
+
)
|
|
222
|
+
return { _tag: "Rejected" } as const;
|
|
223
|
+
const claim = yield* claimFlow(mapping, { row: found.row!, flow }, input.claimId);
|
|
224
|
+
|
|
225
|
+
return claim === undefined
|
|
226
|
+
? ({ _tag: "Rejected" } as const)
|
|
227
|
+
: ({ _tag: "Claimed", claim } as const);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
const checkInvalidation = (mapping: any, original: SessionInvalidationWindow) => {
|
|
231
|
+
const value = snapshotOAuthSync(SessionInvalidationWindow, original);
|
|
232
|
+
|
|
233
|
+
invariant(
|
|
234
|
+
value.trigger === "credential-change" &&
|
|
235
|
+
value.oldAuthenticationEvidence === "rejected" &&
|
|
236
|
+
(mapping.sessionInvalidation === "same-authority-immediate"
|
|
237
|
+
? (value.existingSessions === "immediate" && value.maximumExposureMillis === 0) ||
|
|
238
|
+
(value.existingSessions === "cache-expiry" && value.maximumExposureMillis > 0)
|
|
239
|
+
: value.existingSessions === "original-absolute-expiry"),
|
|
240
|
+
);
|
|
241
|
+
|
|
242
|
+
return value;
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
const advance = (mapping: any, current: any, removed?: string) =>
|
|
246
|
+
Effect.flatMap(CurrentOAuthTransaction, (owner) =>
|
|
247
|
+
Effect.gen(function* () {
|
|
248
|
+
const revision = mapping.subject.nextSecurityRevision(current.revision.securityRevision);
|
|
249
|
+
|
|
250
|
+
invariant(
|
|
251
|
+
typeof revision === "string" &&
|
|
252
|
+
revision.length > 0 &&
|
|
253
|
+
revision.length <= 256 &&
|
|
254
|
+
revision !== current.revision.securityRevision,
|
|
255
|
+
);
|
|
256
|
+
const subject = mapping.subject;
|
|
257
|
+
|
|
258
|
+
yield* owner.update(
|
|
259
|
+
subject.table,
|
|
260
|
+
{ [subject.id]: current.nativeId },
|
|
261
|
+
{ [subject.securityRevision]: revision },
|
|
262
|
+
);
|
|
263
|
+
for (const cleanup of mapping.cleanup) {
|
|
264
|
+
const condition = both(
|
|
265
|
+
eq(col(cleanup.table, cleanup.subjectId), current.nativeId),
|
|
266
|
+
cleanup.condition({
|
|
267
|
+
subjectId: current.nativeId,
|
|
268
|
+
revision: snapshotOAuthSync(OAuthAccountRevision, current.revision),
|
|
269
|
+
...(removed === undefined ? {} : { removedCredentialId: removed }),
|
|
270
|
+
}),
|
|
271
|
+
);
|
|
272
|
+
|
|
273
|
+
yield* owner.write(owner.database.delete(cleanup.table).where(condition));
|
|
274
|
+
owner.postconditions.push(
|
|
275
|
+
sql`not exists(select 1 from ${cleanup.table} where ${condition})`,
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
return SecurityRevision.make(revision);
|
|
280
|
+
}),
|
|
281
|
+
);
|
|
282
|
+
|
|
283
|
+
const settleLink = (
|
|
284
|
+
mapping: any,
|
|
285
|
+
input: any,
|
|
286
|
+
allocated: { readonly credentialId: string; readonly revision: SecurityRevision },
|
|
287
|
+
) =>
|
|
288
|
+
Effect.flatMap(CurrentOAuthTransaction, (owner) =>
|
|
289
|
+
Effect.gen(function* () {
|
|
290
|
+
const claim = snapshotOAuthSync(OAuthLinkClaim, input.claim),
|
|
291
|
+
flow = claim.flow,
|
|
292
|
+
c = flow.context;
|
|
293
|
+
|
|
294
|
+
const current = yield* currentLink(mapping, flow);
|
|
295
|
+
|
|
296
|
+
// Known subject is always locked before tuple/flow; even a rejected outcome
|
|
297
|
+
// settles its exact original claim without adopting a newer revision.
|
|
298
|
+
const identity =
|
|
299
|
+
input.outcome._tag === "Verified" ? input.outcome.identity.identity : undefined;
|
|
300
|
+
|
|
301
|
+
if (identity !== undefined)
|
|
302
|
+
invariant(identity.provider === c.provider && identity.issuer === c.issuer);
|
|
303
|
+
|
|
304
|
+
const tuple =
|
|
305
|
+
identity !== undefined && current !== undefined
|
|
306
|
+
? yield* readTuple(mapping.ownership, identity)
|
|
307
|
+
: undefined;
|
|
308
|
+
|
|
309
|
+
const exact = yield* exactClaim(mapping, claim, "link");
|
|
310
|
+
|
|
311
|
+
if (exact === undefined) return { _tag: "Rejected" } as const;
|
|
312
|
+
let decision: any = { _tag: input.outcome._tag };
|
|
313
|
+
|
|
314
|
+
if (input.outcome._tag === "Verified") {
|
|
315
|
+
decision = { _tag: "Rejected" };
|
|
316
|
+
const auth = snapshotOAuthSync(OAuthActionAuthorization, input.authorization);
|
|
317
|
+
|
|
318
|
+
if (
|
|
319
|
+
current !== undefined &&
|
|
320
|
+
tuple !== undefined &&
|
|
321
|
+
auth.challenge.moduleId === c.moduleId &&
|
|
322
|
+
(yield* authorization(
|
|
323
|
+
mapping,
|
|
324
|
+
current,
|
|
325
|
+
auth,
|
|
326
|
+
"link-complete",
|
|
327
|
+
c.flowId,
|
|
328
|
+
c.commandId,
|
|
329
|
+
contextStorage.encode(c),
|
|
330
|
+
c.maximumEvidenceAgeMillis,
|
|
331
|
+
))
|
|
332
|
+
) {
|
|
333
|
+
const t = mapping.ownership.tuple,
|
|
334
|
+
cr = mapping.credential,
|
|
335
|
+
a = mapping.authority;
|
|
336
|
+
|
|
337
|
+
const login = yield* owner.read(
|
|
338
|
+
cr.table,
|
|
339
|
+
eq(col(cr.table, cr.identityKey), tuple.key),
|
|
340
|
+
{
|
|
341
|
+
limit: 1,
|
|
342
|
+
},
|
|
343
|
+
);
|
|
344
|
+
|
|
345
|
+
const previous = login.rows[0];
|
|
346
|
+
|
|
347
|
+
if (
|
|
348
|
+
tuple.row[t.state] === "Reserved" ||
|
|
349
|
+
(tuple.row[t.state] === "Owned" &&
|
|
350
|
+
!mapping.subjectId.equals(tuple.row[t.subjectId], current.nativeId)) ||
|
|
351
|
+
(previous !== undefined &&
|
|
352
|
+
(previous[cr.moduleId] !== c.moduleId ||
|
|
353
|
+
!mapping.subjectId.equals(previous[cr.subjectId], current.nativeId) ||
|
|
354
|
+
!cr.isActiveStatus(previous[cr.status])))
|
|
355
|
+
)
|
|
356
|
+
decision = { _tag: "Conflict" };
|
|
357
|
+
else if (previous !== undefined) {
|
|
358
|
+
const shared = current.revision.credentials.find(
|
|
359
|
+
(item: any) => item.credentialId === previous[cr.credentialId],
|
|
360
|
+
);
|
|
361
|
+
|
|
362
|
+
if (
|
|
363
|
+
tuple.row[t.state] !== "Owned" ||
|
|
364
|
+
shared?.revision !== previous[cr.credentialRevision] ||
|
|
365
|
+
!(yield* owner.check(
|
|
366
|
+
sql`exists(select 1 from ${cr.table} where ${both(eq(col(cr.table, cr.identityKey), tuple.key), cr.activeCondition)})`,
|
|
367
|
+
))
|
|
368
|
+
)
|
|
369
|
+
decision = { _tag: "Conflict" };
|
|
370
|
+
else
|
|
371
|
+
decision = {
|
|
372
|
+
_tag: "Linked",
|
|
373
|
+
changed: false,
|
|
374
|
+
credential: snapshotOAuthSync(OAuthCredentialSnapshot, {
|
|
375
|
+
moduleId: c.moduleId,
|
|
376
|
+
identity,
|
|
377
|
+
credentialId: previous[cr.credentialId],
|
|
378
|
+
credentialRevision: previous[cr.credentialRevision],
|
|
379
|
+
revision: current.revision,
|
|
380
|
+
}),
|
|
381
|
+
};
|
|
382
|
+
} else {
|
|
383
|
+
checkInvalidation(mapping, input.invalidation);
|
|
384
|
+
|
|
385
|
+
const values = {
|
|
386
|
+
...cr.encodeInsert({
|
|
387
|
+
moduleId: c.moduleId,
|
|
388
|
+
subjectId: current.nativeId,
|
|
389
|
+
identityKey: tuple.key,
|
|
390
|
+
credentialId: allocated.credentialId,
|
|
391
|
+
credentialRevision: allocated.revision,
|
|
392
|
+
}),
|
|
393
|
+
[cr.moduleId]: c.moduleId,
|
|
394
|
+
[cr.subjectId]: current.nativeId,
|
|
395
|
+
[cr.identityKey]: tuple.key,
|
|
396
|
+
[cr.credentialId]: allocated.credentialId,
|
|
397
|
+
[cr.credentialRevision]: allocated.revision,
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
invariant(cr.isActiveStatus(values[cr.status]));
|
|
401
|
+
|
|
402
|
+
const inserted = yield* owner.insert(cr.table, values, {
|
|
403
|
+
[cr.credentialId]: allocated.credentialId,
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
login.rows = inserted.rows;
|
|
407
|
+
|
|
408
|
+
const sharedValues = {
|
|
409
|
+
...a.encodeInsert({
|
|
410
|
+
subjectId: current.nativeId,
|
|
411
|
+
credentialId: allocated.credentialId,
|
|
412
|
+
revision: allocated.revision,
|
|
413
|
+
}),
|
|
414
|
+
[a.subjectId]: current.nativeId,
|
|
415
|
+
[a.credentialId]: allocated.credentialId,
|
|
416
|
+
[a.revision]: allocated.revision,
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
invariant(a.isActiveStatus(sharedValues[a.status]));
|
|
420
|
+
|
|
421
|
+
const shared = yield* owner.insert(a.table, sharedValues, {
|
|
422
|
+
[a.subjectId]: current.nativeId,
|
|
423
|
+
[a.credentialId]: allocated.credentialId,
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
current.authorities.rows = [...current.authorities.rows, ...shared.rows];
|
|
427
|
+
owner.postconditions.push(
|
|
428
|
+
sql`exists(select 1 from ${cr.table} where ${both(
|
|
429
|
+
eq(col(cr.table, cr.credentialId), allocated.credentialId),
|
|
430
|
+
cr.activeCondition,
|
|
431
|
+
)})`,
|
|
432
|
+
sql`exists(select 1 from ${a.table} where ${both(
|
|
433
|
+
eq(col(a.table, a.subjectId), current.nativeId),
|
|
434
|
+
eq(col(a.table, a.credentialId), allocated.credentialId),
|
|
435
|
+
a.activeCondition,
|
|
436
|
+
)})`,
|
|
437
|
+
);
|
|
438
|
+
if (tuple.row[t.state] === "Unowned")
|
|
439
|
+
yield* acquireTuple(mapping.ownership, identity, tuple.key, current.nativeId);
|
|
440
|
+
const next = yield* advance(mapping, current);
|
|
441
|
+
|
|
442
|
+
const revision = snapshotOAuthSync(OAuthAccountRevision, {
|
|
443
|
+
...current.revision,
|
|
444
|
+
securityRevision: next,
|
|
445
|
+
credentials: [
|
|
446
|
+
...current.revision.credentials,
|
|
447
|
+
{ credentialId: allocated.credentialId, revision: allocated.revision },
|
|
448
|
+
],
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
decision = {
|
|
452
|
+
_tag: "Linked",
|
|
453
|
+
changed: true,
|
|
454
|
+
credential: snapshotOAuthSync(OAuthCredentialSnapshot, {
|
|
455
|
+
moduleId: c.moduleId,
|
|
456
|
+
identity,
|
|
457
|
+
credentialId: allocated.credentialId,
|
|
458
|
+
credentialRevision: allocated.revision as never,
|
|
459
|
+
revision,
|
|
460
|
+
}),
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
yield* terminalFlow(mapping, flow, decision._tag);
|
|
466
|
+
|
|
467
|
+
return decision;
|
|
468
|
+
}),
|
|
469
|
+
);
|
|
470
|
+
|
|
471
|
+
const targetCredential = (mapping: any, moduleId: string, credentialId: string, current: any) =>
|
|
472
|
+
Effect.flatMap(CurrentOAuthTransaction, (owner) =>
|
|
473
|
+
Effect.gen(function* () {
|
|
474
|
+
const cr = mapping.credential;
|
|
475
|
+
|
|
476
|
+
const found = yield* owner.read(
|
|
477
|
+
cr.table,
|
|
478
|
+
eq(col(cr.table, cr.credentialId), credentialId),
|
|
479
|
+
{
|
|
480
|
+
limit: 1,
|
|
481
|
+
},
|
|
482
|
+
);
|
|
483
|
+
|
|
484
|
+
const row = found.rows[0];
|
|
485
|
+
|
|
486
|
+
if (
|
|
487
|
+
row === undefined ||
|
|
488
|
+
row[cr.moduleId] !== moduleId ||
|
|
489
|
+
!mapping.subjectId.equals(row[cr.subjectId], current.nativeId) ||
|
|
490
|
+
!cr.isActiveStatus(row[cr.status])
|
|
491
|
+
)
|
|
492
|
+
return undefined;
|
|
493
|
+
const t = mapping.ownership.tuple;
|
|
494
|
+
|
|
495
|
+
const tuple = yield* owner.read(
|
|
496
|
+
t.table,
|
|
497
|
+
eq(col(t.table, t.identityKey), row[cr.identityKey]),
|
|
498
|
+
{
|
|
499
|
+
limit: 1,
|
|
500
|
+
},
|
|
501
|
+
);
|
|
502
|
+
|
|
503
|
+
const ownership = tuple.rows[0];
|
|
504
|
+
|
|
505
|
+
if (
|
|
506
|
+
ownership === undefined ||
|
|
507
|
+
ownership[t.state] !== "Owned" ||
|
|
508
|
+
!mapping.subjectId.equals(ownership[t.subjectId], current.nativeId)
|
|
509
|
+
)
|
|
510
|
+
return undefined;
|
|
511
|
+
|
|
512
|
+
const identity = {
|
|
513
|
+
provider: ownership[t.provider],
|
|
514
|
+
issuer: ownership[t.issuer],
|
|
515
|
+
subject: ownership[t.externalSubject],
|
|
516
|
+
};
|
|
517
|
+
|
|
518
|
+
invariant((yield* oauthIdentityKey(identity)) === row[cr.identityKey]);
|
|
519
|
+
|
|
520
|
+
const shared = current.revision.credentials.find(
|
|
521
|
+
(item: any) => item.credentialId === credentialId,
|
|
522
|
+
);
|
|
523
|
+
|
|
524
|
+
if (
|
|
525
|
+
shared?.revision !== row[cr.credentialRevision] ||
|
|
526
|
+
!(yield* owner.check(
|
|
527
|
+
sql`exists(select 1 from ${cr.table} where ${both(eq(col(cr.table, cr.credentialId), credentialId), cr.activeCondition)})`,
|
|
528
|
+
))
|
|
529
|
+
)
|
|
530
|
+
return undefined;
|
|
531
|
+
|
|
532
|
+
return snapshotOAuthSync(OAuthCredentialSnapshot, {
|
|
533
|
+
moduleId: moduleId as never,
|
|
534
|
+
identity,
|
|
535
|
+
credentialId,
|
|
536
|
+
credentialRevision: row[cr.credentialRevision],
|
|
537
|
+
revision: current.revision,
|
|
538
|
+
});
|
|
539
|
+
}),
|
|
540
|
+
);
|
|
541
|
+
|
|
542
|
+
const unlinkCommand = (mapping: any, moduleId: string, commandId: string) =>
|
|
543
|
+
Effect.flatMap(CurrentOAuthTransaction, (owner) =>
|
|
544
|
+
owner.read(
|
|
545
|
+
mapping.command.table,
|
|
546
|
+
equal(mapping.command.table, {
|
|
547
|
+
[mapping.command.moduleId]: moduleId,
|
|
548
|
+
[mapping.command.commandId]: commandId,
|
|
549
|
+
}),
|
|
550
|
+
{ limit: 1 },
|
|
551
|
+
),
|
|
552
|
+
);
|
|
553
|
+
|
|
554
|
+
const inspectUnlink = (mapping: any, input: any) =>
|
|
555
|
+
Effect.flatMap(CurrentOAuthTransaction, (owner) =>
|
|
556
|
+
Effect.gen(function* () {
|
|
557
|
+
const current = yield* accountCurrent(mapping, input.subjectId);
|
|
558
|
+
|
|
559
|
+
if (current === undefined) return { _tag: "Rejected" } as const;
|
|
560
|
+
|
|
561
|
+
const command = yield* unlinkCommand(mapping, input.moduleId, input.commandId),
|
|
562
|
+
r = mapping.command;
|
|
563
|
+
|
|
564
|
+
const old = command.rows[0];
|
|
565
|
+
|
|
566
|
+
if (old !== undefined) {
|
|
567
|
+
const intent = credentialStorage.decode(old[r.intentSnapshot]);
|
|
568
|
+
|
|
569
|
+
if (
|
|
570
|
+
intent.moduleId !== input.moduleId ||
|
|
571
|
+
intent.revision.subjectId !== input.subjectId ||
|
|
572
|
+
intent.credentialId !== input.credentialId ||
|
|
573
|
+
!mapping.subjectId.equals(old[r.subjectId], current.nativeId) ||
|
|
574
|
+
old[r.credentialId] !== input.credentialId
|
|
575
|
+
)
|
|
576
|
+
return { _tag: "Conflict" } as const;
|
|
577
|
+
const metadata = mapping.metadata.condition(current.nativeId);
|
|
578
|
+
|
|
579
|
+
if (!(yield* owner.check(metadata))) return { _tag: "Rejected" } as const;
|
|
580
|
+
owner.postconditions.push(metadata);
|
|
581
|
+
|
|
582
|
+
return { _tag: "Replay", result: unlinkedStorage.decode(old[r.decision]) } as const;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
const credential = yield* targetCredential(
|
|
586
|
+
mapping,
|
|
587
|
+
input.moduleId,
|
|
588
|
+
input.credentialId,
|
|
589
|
+
current,
|
|
590
|
+
);
|
|
591
|
+
|
|
592
|
+
return credential === undefined
|
|
593
|
+
? ({ _tag: "Rejected" } as const)
|
|
594
|
+
: ({ _tag: "Target", credential } as const);
|
|
595
|
+
}),
|
|
596
|
+
);
|
|
597
|
+
|
|
598
|
+
const remainingEligibility = (mapping: any, current: any, removed: string) =>
|
|
599
|
+
Effect.flatMap(CurrentOAuthTransaction, (owner) =>
|
|
600
|
+
Effect.gen(function* () {
|
|
601
|
+
invariant(mapping.eligibility.length > 0 && mapping.eligibility.length <= 32);
|
|
602
|
+
const facts = new Map<string, OAuthEligibilityFact>();
|
|
603
|
+
|
|
604
|
+
const revisions = new Map(
|
|
605
|
+
current.revision.credentials.map((item: any) => [item.credentialId, item.revision]),
|
|
606
|
+
);
|
|
607
|
+
|
|
608
|
+
for (const descriptor of mapping.eligibility) {
|
|
609
|
+
const observed = yield* owner.read(
|
|
610
|
+
descriptor.table,
|
|
611
|
+
both(
|
|
612
|
+
eq(col(descriptor.table, descriptor.subjectId), current.nativeId),
|
|
613
|
+
descriptor.condition(current.nativeId),
|
|
614
|
+
),
|
|
615
|
+
{ orderBy: col(descriptor.table, descriptor.credentialId) },
|
|
616
|
+
);
|
|
617
|
+
|
|
618
|
+
for (const row of observed.rows) {
|
|
619
|
+
const fact = descriptor.decode(copiedRow(row)) as OAuthEligibilityFact | undefined;
|
|
620
|
+
|
|
621
|
+
if (fact === undefined || fact.credentialId === removed) continue;
|
|
622
|
+
invariant(
|
|
623
|
+
fact.credentialId === row[descriptor.credentialId] &&
|
|
624
|
+
fact.revision === row[descriptor.revision] &&
|
|
625
|
+
revisions.get(fact.credentialId) === fact.revision &&
|
|
626
|
+
fact.factors.length <= 8 &&
|
|
627
|
+
fact.factors.every((factor) =>
|
|
628
|
+
["knowledge", "possession", "inherence"].includes(factor),
|
|
629
|
+
),
|
|
630
|
+
);
|
|
631
|
+
invariant(
|
|
632
|
+
typeof fact.usablePrimary === "boolean" &&
|
|
633
|
+
typeof fact.userVerified === "boolean" &&
|
|
634
|
+
typeof fact.phishingResistant === "boolean",
|
|
635
|
+
);
|
|
636
|
+
|
|
637
|
+
const normalized = {
|
|
638
|
+
credentialId: fact.credentialId,
|
|
639
|
+
revision: fact.revision,
|
|
640
|
+
usablePrimary: fact.usablePrimary,
|
|
641
|
+
factors: [...new Set(fact.factors)].sort(),
|
|
642
|
+
userVerified: fact.userVerified,
|
|
643
|
+
phishingResistant: fact.phishingResistant,
|
|
644
|
+
};
|
|
645
|
+
|
|
646
|
+
const prior = facts.get(fact.credentialId);
|
|
647
|
+
|
|
648
|
+
// oxlint-disable-next-line no-restricted-properties -- Canonical bounded eligibility fact fingerprint rejects contradictory duplicate IDs.
|
|
649
|
+
invariant(prior === undefined || JSON.stringify(prior) === JSON.stringify(normalized));
|
|
650
|
+
facts.set(fact.credentialId, normalized);
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
const available = [...facts.values()];
|
|
654
|
+
|
|
655
|
+
const requirement = snapshotOAuthSync(
|
|
656
|
+
AuthenticationRequirement,
|
|
657
|
+
mapping.subject.decodeAuthenticationRequirement(copiedRow(current.row)),
|
|
658
|
+
);
|
|
659
|
+
|
|
660
|
+
return available.some((fact) => fact.usablePrimary) && satisfies(available, requirement);
|
|
661
|
+
}),
|
|
662
|
+
);
|
|
663
|
+
|
|
664
|
+
const unlink = (mapping: any, input: any) =>
|
|
665
|
+
Effect.flatMap(CurrentOAuthTransaction, (owner) =>
|
|
666
|
+
Effect.gen(function* () {
|
|
667
|
+
const expected = snapshotOAuthSync(OAuthCredentialSnapshot, input.credential);
|
|
668
|
+
|
|
669
|
+
invariant(expected.moduleId === input.moduleId);
|
|
670
|
+
const current = yield* accountCurrent(mapping, expected.revision.subjectId);
|
|
671
|
+
|
|
672
|
+
if (current === undefined) return { _tag: "Rejected" } as const;
|
|
673
|
+
|
|
674
|
+
const guards: ReadonlyArray<OAuthReferenceGuardDescriptor<unknown>> =
|
|
675
|
+
mapping.connectedReferenceGuards ?? [];
|
|
676
|
+
|
|
677
|
+
invariant(guards.length <= 32);
|
|
678
|
+
let referencesGuarded = mapping.connectedReference !== undefined && guards.length > 0;
|
|
679
|
+
|
|
680
|
+
if (mapping.connectedReference !== undefined)
|
|
681
|
+
for (const guard of guards) {
|
|
682
|
+
const condition = guard.condition({
|
|
683
|
+
identity: snapshotOAuthSync(
|
|
684
|
+
OAuthCredentialSnapshot.fields.identity,
|
|
685
|
+
expected.identity,
|
|
686
|
+
),
|
|
687
|
+
identityKey: yield* oauthIdentityKey(expected.identity),
|
|
688
|
+
subjectId: copiedRow({ value: current.nativeId }).value,
|
|
689
|
+
});
|
|
690
|
+
|
|
691
|
+
const read = yield* owner.read(
|
|
692
|
+
guard.table,
|
|
693
|
+
Effect.isEffect(condition) ? yield* condition : condition,
|
|
694
|
+
{ orderBy: col(guard.table, guard.orderBy) },
|
|
695
|
+
);
|
|
696
|
+
|
|
697
|
+
if (read.rows.length === 0) referencesGuarded = false;
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
const command = yield* unlinkCommand(mapping, input.moduleId, input.commandId),
|
|
701
|
+
r = mapping.command;
|
|
702
|
+
|
|
703
|
+
const previous = command.rows[0];
|
|
704
|
+
|
|
705
|
+
if (previous !== undefined) {
|
|
706
|
+
if (
|
|
707
|
+
previous[r.intentSnapshot] !== credentialStorage.encode(expected) ||
|
|
708
|
+
previous[r.credentialId] !== expected.credentialId ||
|
|
709
|
+
!mapping.subjectId.equals(previous[r.subjectId], current.nativeId)
|
|
710
|
+
)
|
|
711
|
+
return { _tag: "Conflict" } as const;
|
|
712
|
+
const metadata = mapping.metadata.condition(current.nativeId);
|
|
713
|
+
|
|
714
|
+
if (!(yield* owner.check(metadata))) return { _tag: "Rejected" } as const;
|
|
715
|
+
owner.postconditions.push(metadata);
|
|
716
|
+
|
|
717
|
+
return {
|
|
718
|
+
_tag: "Unlinked",
|
|
719
|
+
replayed: true,
|
|
720
|
+
result: unlinkedStorage.decode(previous[r.decision]),
|
|
721
|
+
} as const;
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
const actual = yield* targetCredential(
|
|
725
|
+
mapping,
|
|
726
|
+
input.moduleId,
|
|
727
|
+
expected.credentialId,
|
|
728
|
+
current,
|
|
729
|
+
);
|
|
730
|
+
|
|
731
|
+
if (
|
|
732
|
+
actual === undefined ||
|
|
733
|
+
credentialStorage.encode(actual) !== credentialStorage.encode(expected)
|
|
734
|
+
)
|
|
735
|
+
return { _tag: "Rejected" } as const;
|
|
736
|
+
const auth = snapshotOAuthSync(OAuthActionAuthorization, input.authorization);
|
|
737
|
+
|
|
738
|
+
if (
|
|
739
|
+
auth.challenge.moduleId !== input.moduleId ||
|
|
740
|
+
!(yield* authorization(
|
|
741
|
+
mapping,
|
|
742
|
+
current,
|
|
743
|
+
auth,
|
|
744
|
+
"unlink",
|
|
745
|
+
input.commandId,
|
|
746
|
+
input.commandId,
|
|
747
|
+
credentialStorage.encode(expected),
|
|
748
|
+
auth.requirement.maximumAgeMillis,
|
|
749
|
+
))
|
|
750
|
+
)
|
|
751
|
+
return { _tag: "Rejected" } as const;
|
|
752
|
+
if (!(yield* remainingEligibility(mapping, current, expected.credentialId)))
|
|
753
|
+
return { _tag: "LastSignInMethod" } as const;
|
|
754
|
+
|
|
755
|
+
const invalidation = checkInvalidation(mapping, input.invalidation),
|
|
756
|
+
cr = mapping.credential,
|
|
757
|
+
a = mapping.authority;
|
|
758
|
+
|
|
759
|
+
const key = yield* oauthIdentityKey(expected.identity),
|
|
760
|
+
t = mapping.ownership.tuple;
|
|
761
|
+
|
|
762
|
+
const connected = mapping.connectedReference?.({
|
|
763
|
+
identityKey: key,
|
|
764
|
+
subjectId: copiedRow({ value: current.nativeId }).value,
|
|
765
|
+
});
|
|
766
|
+
|
|
767
|
+
const retain =
|
|
768
|
+
connected !== undefined && (!referencesGuarded || (yield* owner.check(connected)));
|
|
769
|
+
|
|
770
|
+
if (connected !== undefined && referencesGuarded)
|
|
771
|
+
owner.postconditions.push(retain ? connected : sql`not (${connected})`);
|
|
772
|
+
yield* owner.remove(cr.table, { [cr.credentialId]: expected.credentialId });
|
|
773
|
+
yield* owner.remove(a.table, {
|
|
774
|
+
[a.subjectId]: current.nativeId,
|
|
775
|
+
[a.credentialId]: expected.credentialId,
|
|
776
|
+
});
|
|
777
|
+
if (!retain) {
|
|
778
|
+
yield* owner.update(
|
|
779
|
+
t.table,
|
|
780
|
+
{ [t.identityKey]: key },
|
|
781
|
+
{
|
|
782
|
+
[t.state]: "Unowned",
|
|
783
|
+
[t.subjectId]: null,
|
|
784
|
+
[t.reservation]: null,
|
|
785
|
+
[t.version]: owner.marker,
|
|
786
|
+
},
|
|
787
|
+
);
|
|
788
|
+
if (mapping.ownership.mode === "separate")
|
|
789
|
+
yield* owner.remove(mapping.ownership.external.table, {
|
|
790
|
+
[mapping.ownership.external.identityKey]: key,
|
|
791
|
+
});
|
|
792
|
+
}
|
|
793
|
+
yield* advance(mapping, current, expected.credentialId);
|
|
794
|
+
|
|
795
|
+
const result = snapshotOAuthSync(OAuthUnlinked, {
|
|
796
|
+
_tag: "Unlinked",
|
|
797
|
+
credentialId: expected.credentialId,
|
|
798
|
+
invalidation,
|
|
799
|
+
});
|
|
800
|
+
|
|
801
|
+
const now = yield* owner.now(mapping.clock);
|
|
802
|
+
|
|
803
|
+
invariant(
|
|
804
|
+
Number.isSafeInteger(input.retentionUntilMillis) &&
|
|
805
|
+
input.retentionUntilMillis > now &&
|
|
806
|
+
input.retentionUntilMillis >= input.nowMillis + 120000,
|
|
807
|
+
);
|
|
808
|
+
const commandKey = { [r.moduleId]: input.moduleId, [r.commandId]: input.commandId };
|
|
809
|
+
|
|
810
|
+
const values = {
|
|
811
|
+
...r.encodeInsert({
|
|
812
|
+
moduleId: input.moduleId,
|
|
813
|
+
commandId: input.commandId,
|
|
814
|
+
credential: snapshotOAuthSync(OAuthCredentialSnapshot, expected),
|
|
815
|
+
}),
|
|
816
|
+
...commandKey,
|
|
817
|
+
[r.subjectId]: current.nativeId,
|
|
818
|
+
[r.credentialId]: expected.credentialId,
|
|
819
|
+
[r.intentSnapshot]: credentialStorage.encode(expected),
|
|
820
|
+
[r.decision]: unlinkedStorage.encode(result),
|
|
821
|
+
[r.retentionUntil]: mapping.clock.encodeInstant(input.retentionUntilMillis),
|
|
822
|
+
};
|
|
823
|
+
|
|
824
|
+
const inserted = yield* owner.insert(r.table, values, commandKey);
|
|
825
|
+
|
|
826
|
+
command.rows = inserted.rows;
|
|
827
|
+
|
|
828
|
+
return { _tag: "Unlinked", result, replayed: false } as const;
|
|
829
|
+
}),
|
|
830
|
+
);
|
|
831
|
+
|
|
832
|
+
return { accountCurrent, issueLink, preflightLink, claimLink, settleLink, inspectUnlink, unlink };
|
|
833
|
+
});
|
|
834
|
+
|
|
835
|
+
export class OAuthAccounts extends Context.Service<OAuthAccounts, Effect.Success<typeof make>>()(
|
|
836
|
+
"effect-auth/persistence/OAuthAccounts",
|
|
837
|
+
) {
|
|
838
|
+
static readonly layer = Layer.effect(OAuthAccounts, make);
|
|
839
|
+
}
|