@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,505 @@
|
|
|
1
|
+
/* oxlint-disable no-explicit-any -- compiler table shapes are erased; domain codecs and error channels remain typed. */
|
|
2
|
+
import {
|
|
3
|
+
type OAuthLinkClaim,
|
|
4
|
+
type OAuthClaim,
|
|
5
|
+
OAuthCleanupInput,
|
|
6
|
+
OAuthCredentialSnapshot,
|
|
7
|
+
OAuthPendingFlow,
|
|
8
|
+
OAuthModuleId,
|
|
9
|
+
type OAuthVerifiedExternalIdentity,
|
|
10
|
+
OAuthAccountRevision,
|
|
11
|
+
OAuthLinkPendingFlow,
|
|
12
|
+
snapshotOAuthSync,
|
|
13
|
+
} from "@yielded/auth/OAuth";
|
|
14
|
+
import { Effect, Context, Layer } from "effect";
|
|
15
|
+
|
|
16
|
+
import type { SubjectIdCodec } from "../models/common";
|
|
17
|
+
import { CurrentOAuthTransaction } from "./owner";
|
|
18
|
+
import type { Row } from "./owner";
|
|
19
|
+
import { OAuthQueryCompiler } from "./query-compiler";
|
|
20
|
+
import { invariant, oauthIdentityKey, storage } from "./state";
|
|
21
|
+
|
|
22
|
+
export type Flow = typeof OAuthPendingFlow.Type | typeof OAuthLinkPendingFlow.Type;
|
|
23
|
+
|
|
24
|
+
export type Claim = typeof OAuthClaim.Type | typeof OAuthLinkClaim.Type;
|
|
25
|
+
|
|
26
|
+
const make = Effect.gen(function* () {
|
|
27
|
+
const operations = yield* OAuthQueryCompiler;
|
|
28
|
+
|
|
29
|
+
const { eq, sql } = operations;
|
|
30
|
+
const { both, col, copiedRow, equal } = operations;
|
|
31
|
+
|
|
32
|
+
const pendingStorage = storage(OAuthPendingFlow);
|
|
33
|
+
|
|
34
|
+
const linkStorage = storage(OAuthLinkPendingFlow);
|
|
35
|
+
|
|
36
|
+
const credentialStorage = storage(OAuthCredentialSnapshot);
|
|
37
|
+
|
|
38
|
+
const revisionStorage = storage(OAuthAccountRevision);
|
|
39
|
+
|
|
40
|
+
const flowKey = (mapping: any, context: Flow["context"]) => ({
|
|
41
|
+
[mapping.moduleId]: context.moduleId,
|
|
42
|
+
[mapping.flowId]: context.flowId,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const flowStorage = (purpose: "sign-in" | "link") =>
|
|
46
|
+
purpose === "link" ? linkStorage : pendingStorage;
|
|
47
|
+
|
|
48
|
+
const instant = (mapping: any, value: unknown): number => {
|
|
49
|
+
const millis = mapping.clock.decodeInstant(value);
|
|
50
|
+
|
|
51
|
+
invariant(Number.isSafeInteger(millis) && millis >= 0);
|
|
52
|
+
|
|
53
|
+
return millis;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const readFlow = (
|
|
57
|
+
mapping: any,
|
|
58
|
+
input: { readonly moduleId: string; readonly flowId: string },
|
|
59
|
+
purpose: "sign-in" | "link",
|
|
60
|
+
) =>
|
|
61
|
+
Effect.flatMap(CurrentOAuthTransaction, (owner) =>
|
|
62
|
+
Effect.gen(function* () {
|
|
63
|
+
const f = mapping.flow;
|
|
64
|
+
|
|
65
|
+
const observation = yield* owner.read(
|
|
66
|
+
f.table,
|
|
67
|
+
equal(f.table, {
|
|
68
|
+
[f.moduleId]: input.moduleId,
|
|
69
|
+
[f.flowId]: input.flowId,
|
|
70
|
+
}),
|
|
71
|
+
{ limit: 1 },
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
const row = observation.rows[0];
|
|
75
|
+
|
|
76
|
+
if (row === undefined || row[f.purpose] !== purpose || row[f.snapshot] === null)
|
|
77
|
+
return { observation };
|
|
78
|
+
const flow = flowStorage(purpose).decode(row[f.snapshot]) as Flow;
|
|
79
|
+
const c = flow.context;
|
|
80
|
+
|
|
81
|
+
invariant(
|
|
82
|
+
row[f.moduleId] === c.moduleId &&
|
|
83
|
+
row[f.flowId] === c.flowId &&
|
|
84
|
+
row[f.commandId] === c.commandId &&
|
|
85
|
+
row[f.generation] === c.generation &&
|
|
86
|
+
row[f.stateDigest] === c.stateDigest &&
|
|
87
|
+
row[f.binderVerifier] === c.requestBindingVerifier &&
|
|
88
|
+
instant(mapping, row[f.binderExpiresAt]) === c.requestBindingExpiresAtMillis &&
|
|
89
|
+
instant(mapping, row[f.issuedAt]) === c.issuedAtMillis &&
|
|
90
|
+
instant(mapping, row[f.expiresAt]) === c.expiresAtMillis &&
|
|
91
|
+
instant(mapping, row[f.retentionUntil]) === flow.retentionUntilMillis,
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
return { observation, row, flow };
|
|
95
|
+
}),
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
const liveFlow = (flow: Flow, now: number) =>
|
|
99
|
+
flow.context.issuedAtMillis <= now &&
|
|
100
|
+
now < flow.context.expiresAtMillis &&
|
|
101
|
+
flow.context.expiresAtMillis <= flow.context.requestBindingExpiresAtMillis &&
|
|
102
|
+
flow.retentionUntilMillis >= flow.context.expiresAtMillis + flow.context.claimLifetimeMillis;
|
|
103
|
+
|
|
104
|
+
const issueFlow = (mapping: any, original: Flow, purpose: "sign-in" | "link") =>
|
|
105
|
+
Effect.flatMap(CurrentOAuthTransaction, (owner) =>
|
|
106
|
+
Effect.gen(function* () {
|
|
107
|
+
const codec = flowStorage(purpose);
|
|
108
|
+
const flow = codec.decode(codec.encode(original as never)) as Flow;
|
|
109
|
+
|
|
110
|
+
const f = mapping.flow,
|
|
111
|
+
c = flow.context,
|
|
112
|
+
now = yield* owner.now(mapping.clock);
|
|
113
|
+
|
|
114
|
+
if (!liveFlow(flow, now)) return { _tag: "Rejected" } as const;
|
|
115
|
+
const byFlow = yield* owner.read(f.table, equal(f.table, flowKey(f, c)), { limit: 1 });
|
|
116
|
+
|
|
117
|
+
const byCommand = yield* owner.read(
|
|
118
|
+
f.table,
|
|
119
|
+
equal(f.table, {
|
|
120
|
+
[f.moduleId]: c.moduleId,
|
|
121
|
+
[f.commandId]: c.commandId,
|
|
122
|
+
}),
|
|
123
|
+
{ limit: 1 },
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
if (byFlow.rows.length || byCommand.rows.length) return { _tag: "Rejected" } as const;
|
|
127
|
+
const encode = mapping.clock.encodeInstant;
|
|
128
|
+
|
|
129
|
+
const values = {
|
|
130
|
+
...f.encodeInsert({ moduleId: c.moduleId, flowId: c.flowId, purpose }),
|
|
131
|
+
...flowKey(f, c),
|
|
132
|
+
[f.commandId]: c.commandId,
|
|
133
|
+
[f.purpose]: purpose,
|
|
134
|
+
[f.generation]: c.generation,
|
|
135
|
+
[f.state]: "Pending",
|
|
136
|
+
[f.version]: owner.marker,
|
|
137
|
+
[f.stateDigest]: c.stateDigest,
|
|
138
|
+
[f.binderVerifier]: c.requestBindingVerifier,
|
|
139
|
+
[f.binderExpiresAt]: encode(c.requestBindingExpiresAtMillis),
|
|
140
|
+
[f.snapshot]: codec.encode(flow as never),
|
|
141
|
+
[f.issuedAt]: encode(c.issuedAtMillis),
|
|
142
|
+
[f.expiresAt]: encode(c.expiresAtMillis),
|
|
143
|
+
[f.claimId]: null,
|
|
144
|
+
[f.claimedAt]: null,
|
|
145
|
+
[f.claimExpiresAt]: null,
|
|
146
|
+
[f.retentionUntil]: encode(flow.retentionUntilMillis),
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
const inserted = yield* owner.insert(f.table, values, flowKey(f, c), true);
|
|
150
|
+
|
|
151
|
+
byFlow.rows = inserted.rows;
|
|
152
|
+
byCommand.rows = inserted.rows;
|
|
153
|
+
if (inserted.rows[0]?.[f.version] !== owner.marker) return { _tag: "Rejected" } as const;
|
|
154
|
+
owner.postconditions.push(sql`${mapping.clock.engineNowMillis} >= ${c.issuedAtMillis}
|
|
155
|
+
and ${mapping.clock.engineNowMillis} < ${c.expiresAtMillis}`);
|
|
156
|
+
|
|
157
|
+
return { _tag: "Issued", flow } as const;
|
|
158
|
+
}),
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
const matchesAccess = (flow: Flow, input: any) => {
|
|
162
|
+
const c = flow.context;
|
|
163
|
+
|
|
164
|
+
return (
|
|
165
|
+
c.moduleId === input.moduleId &&
|
|
166
|
+
c.generation === input.generation &&
|
|
167
|
+
c.flowId === input.flowId &&
|
|
168
|
+
c.provider === input.provider &&
|
|
169
|
+
c.callbackId === input.callbackId &&
|
|
170
|
+
c.stateDigest === input.stateDigest &&
|
|
171
|
+
c.requestBindingVerifier === input.requestBindingVerifier &&
|
|
172
|
+
c.requestBindingExpiresAtMillis === input.requestBindingExpiresAtMillis &&
|
|
173
|
+
(c.responseIssuerMode === "required"
|
|
174
|
+
? input.responseIssuer === c.issuer
|
|
175
|
+
: input.responseIssuer === undefined)
|
|
176
|
+
);
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
const claimFlow = (mapping: any, inspected: { row: Row; flow: Flow }, claimId: string) =>
|
|
180
|
+
Effect.flatMap(CurrentOAuthTransaction, (owner) =>
|
|
181
|
+
Effect.gen(function* () {
|
|
182
|
+
const now = yield* owner.now(mapping.clock),
|
|
183
|
+
flow = inspected.flow,
|
|
184
|
+
c = flow.context;
|
|
185
|
+
|
|
186
|
+
if (inspected.row[mapping.flow.state] !== "Pending" || !liveFlow(flow, now))
|
|
187
|
+
return undefined;
|
|
188
|
+
|
|
189
|
+
const claim = {
|
|
190
|
+
flow,
|
|
191
|
+
claimId,
|
|
192
|
+
claimedAtMillis: now,
|
|
193
|
+
claimExpiresAtMillis: now + c.claimLifetimeMillis,
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
invariant(
|
|
197
|
+
Number.isSafeInteger(claim.claimExpiresAtMillis) &&
|
|
198
|
+
claim.claimExpiresAtMillis <= flow.retentionUntilMillis,
|
|
199
|
+
);
|
|
200
|
+
|
|
201
|
+
const f = mapping.flow,
|
|
202
|
+
encode = mapping.clock.encodeInstant;
|
|
203
|
+
|
|
204
|
+
yield* owner.update(f.table, flowKey(f, c), {
|
|
205
|
+
[f.state]: "Claimed",
|
|
206
|
+
[f.version]: owner.marker,
|
|
207
|
+
[f.claimId]: claimId,
|
|
208
|
+
[f.claimedAt]: encode(now),
|
|
209
|
+
[f.claimExpiresAt]: encode(claim.claimExpiresAtMillis),
|
|
210
|
+
});
|
|
211
|
+
owner.postconditions.push(sql`${mapping.clock.engineNowMillis} >= ${now} and
|
|
212
|
+
${mapping.clock.engineNowMillis} < ${Math.min(c.expiresAtMillis, claim.claimExpiresAtMillis)}`);
|
|
213
|
+
|
|
214
|
+
return claim;
|
|
215
|
+
}),
|
|
216
|
+
);
|
|
217
|
+
|
|
218
|
+
const exactClaim = (mapping: any, claim: Claim, purpose: "sign-in" | "link") =>
|
|
219
|
+
Effect.flatMap(CurrentOAuthTransaction, (owner) =>
|
|
220
|
+
Effect.gen(function* () {
|
|
221
|
+
const read = yield* readFlow(mapping, claim.flow.context, purpose);
|
|
222
|
+
|
|
223
|
+
const now = yield* owner.now(mapping.clock),
|
|
224
|
+
f = mapping.flow,
|
|
225
|
+
row = read.row;
|
|
226
|
+
|
|
227
|
+
if (
|
|
228
|
+
row === undefined ||
|
|
229
|
+
read.flow === undefined ||
|
|
230
|
+
row[f.state] !== "Claimed" ||
|
|
231
|
+
row[f.claimId] !== claim.claimId ||
|
|
232
|
+
instant(mapping, row[f.claimedAt]) !== claim.claimedAtMillis ||
|
|
233
|
+
instant(mapping, row[f.claimExpiresAt]) !== claim.claimExpiresAtMillis ||
|
|
234
|
+
claim.claimExpiresAtMillis !==
|
|
235
|
+
claim.claimedAtMillis + claim.flow.context.claimLifetimeMillis ||
|
|
236
|
+
now < claim.claimedAtMillis ||
|
|
237
|
+
now >= claim.claimExpiresAtMillis ||
|
|
238
|
+
flowStorage(purpose).encode(read.flow as never) !==
|
|
239
|
+
flowStorage(purpose).encode(claim.flow as never)
|
|
240
|
+
)
|
|
241
|
+
return undefined;
|
|
242
|
+
owner.postconditions
|
|
243
|
+
.push(sql`${mapping.clock.engineNowMillis} >= ${claim.claimedAtMillis} and
|
|
244
|
+
${mapping.clock.engineNowMillis} < ${claim.claimExpiresAtMillis}`);
|
|
245
|
+
|
|
246
|
+
return { ...read, now };
|
|
247
|
+
}),
|
|
248
|
+
);
|
|
249
|
+
|
|
250
|
+
const terminalFlow = (mapping: any, flow: Flow, state: string) =>
|
|
251
|
+
Effect.flatMap(CurrentOAuthTransaction, (owner) =>
|
|
252
|
+
owner.update(mapping.flow.table, flowKey(mapping.flow, flow.context), {
|
|
253
|
+
[mapping.flow.state]: state,
|
|
254
|
+
[mapping.flow.version]: owner.marker,
|
|
255
|
+
[mapping.flow.snapshot]: null,
|
|
256
|
+
}),
|
|
257
|
+
);
|
|
258
|
+
|
|
259
|
+
/** Subject precedes credentials; the complete active credential vector is retained. */
|
|
260
|
+
const currentSubject = (mapping: any, nativeId: unknown) =>
|
|
261
|
+
Effect.flatMap(CurrentOAuthTransaction, (owner) =>
|
|
262
|
+
Effect.gen(function* () {
|
|
263
|
+
const s = mapping.subject,
|
|
264
|
+
a = mapping.authority;
|
|
265
|
+
|
|
266
|
+
const subject = yield* owner.read(s.table, eq(col(s.table, s.id), nativeId), { limit: 1 });
|
|
267
|
+
const row = subject.rows[0];
|
|
268
|
+
const subjectActive = sql`exists(select 1 from ${s.table} where ${both(eq(col(s.table, s.id), nativeId), s.activeCondition)})`;
|
|
269
|
+
|
|
270
|
+
if (
|
|
271
|
+
row === undefined ||
|
|
272
|
+
!s.isActiveStatus(row[s.status]) ||
|
|
273
|
+
!(yield* owner.check(subjectActive))
|
|
274
|
+
)
|
|
275
|
+
return undefined;
|
|
276
|
+
owner.postconditions.push(subjectActive);
|
|
277
|
+
|
|
278
|
+
const authorities = yield* owner.read(a.table, eq(col(a.table, a.subjectId), nativeId), {
|
|
279
|
+
orderBy: col(a.table, a.credentialId),
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
const active = authorities.rows.filter((item) => a.isActiveStatus(item[a.status]));
|
|
283
|
+
|
|
284
|
+
const activeGuard = sql`(select count(*) from ${a.table} where ${both(
|
|
285
|
+
eq(col(a.table, a.subjectId), nativeId),
|
|
286
|
+
a.activeCondition,
|
|
287
|
+
)}) = ${active.length}`;
|
|
288
|
+
|
|
289
|
+
invariant(yield* owner.check(activeGuard));
|
|
290
|
+
owner.postconditions.push(
|
|
291
|
+
() => sql`(select count(*) from ${a.table} where ${both(
|
|
292
|
+
eq(col(a.table, a.subjectId), nativeId),
|
|
293
|
+
a.activeCondition,
|
|
294
|
+
)}) =
|
|
295
|
+
${authorities.rows.filter((item) => a.isActiveStatus(item[a.status])).length}`,
|
|
296
|
+
);
|
|
297
|
+
|
|
298
|
+
const credentials = active
|
|
299
|
+
.map((item) => ({ credentialId: item[a.credentialId], revision: item[a.revision] }))
|
|
300
|
+
.sort((left, right) => left.credentialId.localeCompare(right.credentialId));
|
|
301
|
+
|
|
302
|
+
invariant(
|
|
303
|
+
new Set(credentials.map((item) => item.credentialId)).size === credentials.length,
|
|
304
|
+
);
|
|
305
|
+
const subjectId = yield* (mapping.subjectId as SubjectIdCodec<unknown>).toSubject(nativeId);
|
|
306
|
+
|
|
307
|
+
const revision = snapshotOAuthSync(OAuthAccountRevision, {
|
|
308
|
+
subjectId,
|
|
309
|
+
securityRevision: row[s.securityRevision],
|
|
310
|
+
credentials,
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
return { subject, row, authorities, revision, nativeId };
|
|
314
|
+
}),
|
|
315
|
+
);
|
|
316
|
+
|
|
317
|
+
const discoverOwned = (mapping: any, identity: OAuthVerifiedExternalIdentity["identity"]) =>
|
|
318
|
+
Effect.flatMap(CurrentOAuthTransaction, (owner) =>
|
|
319
|
+
Effect.gen(function* () {
|
|
320
|
+
const key = yield* oauthIdentityKey(identity),
|
|
321
|
+
o = mapping.ownership;
|
|
322
|
+
|
|
323
|
+
const where = both(eq(col(o.table, o.identityKey), key), o.ownedCondition);
|
|
324
|
+
|
|
325
|
+
const discovered = yield* owner.read(o.table, where, {
|
|
326
|
+
lock: false,
|
|
327
|
+
observe: false,
|
|
328
|
+
limit: 1,
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
const row = discovered.rows[0];
|
|
332
|
+
|
|
333
|
+
if (row === undefined) return { key };
|
|
334
|
+
invariant(
|
|
335
|
+
row[o.provider] === identity.provider &&
|
|
336
|
+
row[o.issuer] === identity.issuer &&
|
|
337
|
+
row[o.externalSubject] === identity.subject,
|
|
338
|
+
);
|
|
339
|
+
const nativeId = o.decodeSubjectId(copiedRow(row));
|
|
340
|
+
const current = yield* currentSubject(mapping, nativeId);
|
|
341
|
+
const owned = yield* owner.read(o.table, where, { limit: 1 });
|
|
342
|
+
const final = owned.rows[0];
|
|
343
|
+
|
|
344
|
+
if (
|
|
345
|
+
current === undefined ||
|
|
346
|
+
final === undefined ||
|
|
347
|
+
!mapping.subjectId.equals(nativeId, o.decodeSubjectId(copiedRow(final)))
|
|
348
|
+
)
|
|
349
|
+
return { key, owned: true as const };
|
|
350
|
+
invariant(
|
|
351
|
+
final[o.provider] === identity.provider &&
|
|
352
|
+
final[o.issuer] === identity.issuer &&
|
|
353
|
+
final[o.externalSubject] === identity.subject,
|
|
354
|
+
);
|
|
355
|
+
const c = mapping.credential;
|
|
356
|
+
|
|
357
|
+
const credential = yield* owner.read(c.table, eq(col(c.table, c.identityKey), key), {
|
|
358
|
+
limit: 1,
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
let login: Row | undefined = credential.rows[0];
|
|
362
|
+
|
|
363
|
+
if (login !== undefined && c.isActiveStatus(login[c.status])) {
|
|
364
|
+
const usable = sql`exists(select 1 from ${c.table} where ${both(
|
|
365
|
+
eq(col(c.table, c.identityKey), key),
|
|
366
|
+
c.activeCondition,
|
|
367
|
+
)})`;
|
|
368
|
+
|
|
369
|
+
if (yield* owner.check(usable)) owner.postconditions.push(usable);
|
|
370
|
+
else login = undefined;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
return { key, owned: true as const, current, credential, login };
|
|
374
|
+
}),
|
|
375
|
+
);
|
|
376
|
+
|
|
377
|
+
const resolveOwned = (
|
|
378
|
+
mapping: any,
|
|
379
|
+
moduleId: string,
|
|
380
|
+
identity: OAuthVerifiedExternalIdentity["identity"],
|
|
381
|
+
found: any,
|
|
382
|
+
) => {
|
|
383
|
+
const login = found.login,
|
|
384
|
+
current = found.current,
|
|
385
|
+
c = mapping.credential;
|
|
386
|
+
|
|
387
|
+
if (
|
|
388
|
+
login === undefined ||
|
|
389
|
+
current === undefined ||
|
|
390
|
+
login[c.moduleId] !== moduleId ||
|
|
391
|
+
!mapping.subjectId.equals(login[c.subjectId], current.nativeId) ||
|
|
392
|
+
!c.isActiveStatus(login[c.status])
|
|
393
|
+
)
|
|
394
|
+
return undefined;
|
|
395
|
+
|
|
396
|
+
const shared = current.revision.credentials.find(
|
|
397
|
+
(item: any) => item.credentialId === login[c.credentialId],
|
|
398
|
+
);
|
|
399
|
+
|
|
400
|
+
if (shared?.revision !== login[c.credentialRevision]) return undefined;
|
|
401
|
+
|
|
402
|
+
return snapshotOAuthSync(OAuthCredentialSnapshot, {
|
|
403
|
+
moduleId: OAuthModuleId.make(moduleId),
|
|
404
|
+
identity,
|
|
405
|
+
credentialId: login[c.credentialId],
|
|
406
|
+
credentialRevision: login[c.credentialRevision],
|
|
407
|
+
revision: current.revision,
|
|
408
|
+
});
|
|
409
|
+
};
|
|
410
|
+
|
|
411
|
+
const cleanupFlows = (
|
|
412
|
+
mapping: any,
|
|
413
|
+
original: typeof OAuthCleanupInput.Type,
|
|
414
|
+
purpose: "sign-in" | "link",
|
|
415
|
+
) =>
|
|
416
|
+
Effect.flatMap(CurrentOAuthTransaction, (owner) =>
|
|
417
|
+
Effect.gen(function* () {
|
|
418
|
+
const input = snapshotOAuthSync(OAuthCleanupInput, original),
|
|
419
|
+
f = mapping.flow;
|
|
420
|
+
|
|
421
|
+
const now = yield* owner.now(mapping.clock);
|
|
422
|
+
|
|
423
|
+
const candidates = yield* owner.read(
|
|
424
|
+
f.table,
|
|
425
|
+
both(
|
|
426
|
+
eq(col(f.table, f.moduleId), input.moduleId),
|
|
427
|
+
eq(col(f.table, f.purpose), purpose),
|
|
428
|
+
sql`((${col(f.table, f.state)} = 'Pending' and ${col(f.table, f.expiresAt)} <= ${mapping.clock.encodeInstant(now)})
|
|
429
|
+
or (${col(f.table, f.state)} = 'Claimed' and ${col(f.table, f.claimExpiresAt)} <= ${mapping.clock.encodeInstant(now)})
|
|
430
|
+
or (${col(f.table, f.state)} not in ('Pending','Claimed') and ${col(f.table, f.retentionUntil)} < ${mapping.clock.encodeInstant(now)}))`,
|
|
431
|
+
),
|
|
432
|
+
{ limit: input.limit, observe: false, takeOnly: true },
|
|
433
|
+
);
|
|
434
|
+
|
|
435
|
+
let terminalized = 0,
|
|
436
|
+
removed = 0;
|
|
437
|
+
|
|
438
|
+
for (const candidate of candidates.rows) {
|
|
439
|
+
const key = { [f.moduleId]: candidate[f.moduleId], [f.flowId]: candidate[f.flowId] };
|
|
440
|
+
const current = yield* owner.read(f.table, equal(f.table, key), { limit: 1 });
|
|
441
|
+
|
|
442
|
+
if (current.rows.length !== 1) continue;
|
|
443
|
+
|
|
444
|
+
const row = current.rows[0]!,
|
|
445
|
+
transitionNow = yield* owner.now(mapping.clock);
|
|
446
|
+
|
|
447
|
+
const state = row[f.state];
|
|
448
|
+
|
|
449
|
+
if (state === "Pending" || state === "Claimed") {
|
|
450
|
+
const deadline = instant(
|
|
451
|
+
mapping,
|
|
452
|
+
row[state === "Pending" ? f.expiresAt : f.claimExpiresAt],
|
|
453
|
+
);
|
|
454
|
+
|
|
455
|
+
if (transitionNow < deadline) continue;
|
|
456
|
+
yield* owner.update(f.table, key, {
|
|
457
|
+
[f.state]: state === "Pending" ? "Rejected" : "Ambiguous",
|
|
458
|
+
[f.snapshot]: null,
|
|
459
|
+
[f.version]: owner.marker,
|
|
460
|
+
});
|
|
461
|
+
owner.postconditions.push(sql`${mapping.clock.engineNowMillis} >= ${deadline}`);
|
|
462
|
+
terminalized++;
|
|
463
|
+
} else {
|
|
464
|
+
const retainUntil = Math.max(
|
|
465
|
+
instant(mapping, row[f.retentionUntil]),
|
|
466
|
+
row[f.claimExpiresAt] === null ? 0 : instant(mapping, row[f.claimExpiresAt]),
|
|
467
|
+
);
|
|
468
|
+
|
|
469
|
+
if (transitionNow <= retainUntil) continue;
|
|
470
|
+
yield* owner.remove(f.table, key);
|
|
471
|
+
owner.postconditions.push(sql`${mapping.clock.engineNowMillis} > ${retainUntil}`);
|
|
472
|
+
removed++;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
return { terminalized, removed, hasMore: candidates.rows.length === input.limit };
|
|
477
|
+
}),
|
|
478
|
+
);
|
|
479
|
+
|
|
480
|
+
return {
|
|
481
|
+
pendingStorage,
|
|
482
|
+
linkStorage,
|
|
483
|
+
credentialStorage,
|
|
484
|
+
revisionStorage,
|
|
485
|
+
flowKey,
|
|
486
|
+
flowStorage,
|
|
487
|
+
readFlow,
|
|
488
|
+
liveFlow,
|
|
489
|
+
issueFlow,
|
|
490
|
+
matchesAccess,
|
|
491
|
+
claimFlow,
|
|
492
|
+
exactClaim,
|
|
493
|
+
terminalFlow,
|
|
494
|
+
currentSubject,
|
|
495
|
+
discoverOwned,
|
|
496
|
+
resolveOwned,
|
|
497
|
+
cleanupFlows,
|
|
498
|
+
};
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
export class OAuthFlow extends Context.Service<OAuthFlow, Effect.Success<typeof make>>()(
|
|
502
|
+
"effect-auth/persistence/OAuthFlow",
|
|
503
|
+
) {
|
|
504
|
+
static readonly layer = Layer.effect(OAuthFlow, make);
|
|
505
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import {
|
|
2
|
+
OAuthAccountRevision,
|
|
3
|
+
OAuthActionAuthorization,
|
|
4
|
+
OAuthLinkAccess,
|
|
5
|
+
OAuthLinkClaim,
|
|
6
|
+
OAuthLinkOutcome,
|
|
7
|
+
OAuthLinkPendingFlow,
|
|
8
|
+
OAuthRegistrationAccess,
|
|
9
|
+
OAuthRegistrationFingerprint,
|
|
10
|
+
OAuthRegistrationIntent,
|
|
11
|
+
OAuthClaim,
|
|
12
|
+
OAuthClaimId,
|
|
13
|
+
OAuthCleanupInput,
|
|
14
|
+
OAuthCommandId,
|
|
15
|
+
OAuthCredentialSnapshot,
|
|
16
|
+
OAuthInstant,
|
|
17
|
+
OAuthModuleId,
|
|
18
|
+
OAuthPendingFlow,
|
|
19
|
+
OAuthSignInTransactionContext,
|
|
20
|
+
OAuthVerifiedExternalIdentity,
|
|
21
|
+
snapshotOAuthSync,
|
|
22
|
+
type OAuthUnavailable,
|
|
23
|
+
} from "@yielded/auth/OAuth";
|
|
24
|
+
import { SessionInvalidationWindow } from "@yielded/auth/Sessions";
|
|
25
|
+
/* oxlint-disable no-explicit-any -- private adapter wraps only the Effect-valued semantic ports. */
|
|
26
|
+
import { Effect, Schema } from "effect";
|
|
27
|
+
|
|
28
|
+
import { invariant, unavailable } from "./state";
|
|
29
|
+
|
|
30
|
+
const fields = OAuthSignInTransactionContext.fields;
|
|
31
|
+
|
|
32
|
+
const claimInput = Schema.Struct({
|
|
33
|
+
moduleId: fields.moduleId,
|
|
34
|
+
generation: fields.generation,
|
|
35
|
+
flowId: fields.flowId,
|
|
36
|
+
provider: fields.provider,
|
|
37
|
+
callbackId: fields.callbackId,
|
|
38
|
+
stateDigest: fields.stateDigest,
|
|
39
|
+
requestBindingVerifier: fields.requestBindingVerifier,
|
|
40
|
+
requestBindingExpiresAtMillis: fields.requestBindingExpiresAtMillis,
|
|
41
|
+
responseIssuer: Schema.optionalKey(fields.issuer),
|
|
42
|
+
claimId: OAuthClaimId,
|
|
43
|
+
nowMillis: OAuthInstant,
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const signInOutcome = Schema.Union([
|
|
47
|
+
Schema.TaggedStruct("Verified", { identity: OAuthVerifiedExternalIdentity }),
|
|
48
|
+
Schema.TaggedStruct("Cancelled", {}),
|
|
49
|
+
Schema.TaggedStruct("Rejected", {}),
|
|
50
|
+
Schema.TaggedStruct("Ambiguous", {}),
|
|
51
|
+
]);
|
|
52
|
+
|
|
53
|
+
const signInSettle = Schema.Struct({
|
|
54
|
+
claim: OAuthClaim,
|
|
55
|
+
outcome: signInOutcome,
|
|
56
|
+
nowMillis: OAuthInstant,
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const registrationSettle = Schema.Struct({
|
|
60
|
+
claim: OAuthClaim,
|
|
61
|
+
identity: OAuthVerifiedExternalIdentity,
|
|
62
|
+
intent: Schema.optionalKey(OAuthRegistrationIntent),
|
|
63
|
+
nowMillis: OAuthInstant,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
const registerPrivate = Schema.Struct({
|
|
67
|
+
access: OAuthRegistrationAccess,
|
|
68
|
+
intent: OAuthRegistrationIntent,
|
|
69
|
+
commandId: OAuthCommandId,
|
|
70
|
+
fingerprint: OAuthRegistrationFingerprint,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
const capture = Schema.Struct({
|
|
74
|
+
moduleId: OAuthModuleId,
|
|
75
|
+
subjectId: OAuthAccountRevision.fields.subjectId,
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
const linkIssue = Schema.Struct({
|
|
79
|
+
flow: OAuthLinkPendingFlow,
|
|
80
|
+
authorization: OAuthActionAuthorization,
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
const linkClaim = Schema.Struct({
|
|
84
|
+
access: OAuthLinkAccess,
|
|
85
|
+
flow: OAuthLinkPendingFlow,
|
|
86
|
+
claimId: OAuthClaimId,
|
|
87
|
+
authorization: OAuthActionAuthorization,
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
const linkSettle = Schema.Struct({
|
|
91
|
+
claim: OAuthLinkClaim,
|
|
92
|
+
outcome: OAuthLinkOutcome,
|
|
93
|
+
authorization: OAuthActionAuthorization,
|
|
94
|
+
invalidation: SessionInvalidationWindow,
|
|
95
|
+
nowMillis: OAuthInstant,
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
const unlinkInspect = Schema.Struct({
|
|
99
|
+
moduleId: OAuthModuleId,
|
|
100
|
+
subjectId: OAuthAccountRevision.fields.subjectId,
|
|
101
|
+
commandId: OAuthCommandId,
|
|
102
|
+
credentialId: OAuthCredentialSnapshot.fields.credentialId,
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
const unlink = Schema.Struct({
|
|
106
|
+
moduleId: OAuthModuleId,
|
|
107
|
+
commandId: OAuthCommandId,
|
|
108
|
+
credential: OAuthCredentialSnapshot,
|
|
109
|
+
authorization: OAuthActionAuthorization,
|
|
110
|
+
invalidation: SessionInvalidationWindow,
|
|
111
|
+
nowMillis: OAuthInstant,
|
|
112
|
+
retentionUntilMillis: OAuthInstant,
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
const snapshot = (schema: Schema.Codec<any, any, never, never>) => (input: any) =>
|
|
116
|
+
snapshotOAuthSync(schema, input);
|
|
117
|
+
|
|
118
|
+
export const signInInputs = {
|
|
119
|
+
issue: snapshot(OAuthPendingFlow),
|
|
120
|
+
claim: snapshot(claimInput),
|
|
121
|
+
settle: snapshot(signInSettle),
|
|
122
|
+
cleanup: snapshot(OAuthCleanupInput),
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
export const registrationIntentInputs = { settle: snapshot(registrationSettle) };
|
|
126
|
+
|
|
127
|
+
const capturedApplication = (
|
|
128
|
+
application: { readonly encode: (data: any) => string; readonly decode: (stored: string) => any },
|
|
129
|
+
data: any,
|
|
130
|
+
) => {
|
|
131
|
+
const encoded = application.encode(data);
|
|
132
|
+
|
|
133
|
+
invariant(typeof encoded === "string" && new TextEncoder().encode(encoded).length <= 1048576);
|
|
134
|
+
const retained = application.decode(encoded);
|
|
135
|
+
|
|
136
|
+
invariant(application.encode(retained) === encoded);
|
|
137
|
+
|
|
138
|
+
return retained;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
export const registrationInputs = (application: {
|
|
142
|
+
readonly encode: (data: any) => string;
|
|
143
|
+
readonly decode: (stored: string) => any;
|
|
144
|
+
}) => ({
|
|
145
|
+
read: snapshot(OAuthRegistrationAccess),
|
|
146
|
+
inspect: (input: any) => ({
|
|
147
|
+
intent: snapshotOAuthSync(OAuthRegistrationIntent, input.intent),
|
|
148
|
+
registration: capturedApplication(application, input.registration),
|
|
149
|
+
}),
|
|
150
|
+
register: (input: any) => ({
|
|
151
|
+
...snapshotOAuthSync(registerPrivate, input),
|
|
152
|
+
registration: capturedApplication(application, input.registration),
|
|
153
|
+
}),
|
|
154
|
+
cleanup: snapshot(OAuthCleanupInput),
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
export const accountsInputs = {
|
|
158
|
+
capture: snapshot(capture),
|
|
159
|
+
issue: snapshot(linkIssue),
|
|
160
|
+
preflight: snapshot(OAuthLinkAccess),
|
|
161
|
+
claim: snapshot(linkClaim),
|
|
162
|
+
settle: snapshot(linkSettle),
|
|
163
|
+
inspectUnlink: snapshot(unlinkInspect),
|
|
164
|
+
unlink: snapshot(unlink),
|
|
165
|
+
cleanup: snapshot(OAuthCleanupInput),
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
/** Snapshot before the first asynchronous owner/allocator step; closed bound
|
|
169
|
+
* authorities reject before running even an application codec or inspector. */
|
|
170
|
+
export const capturedOAuthService = <
|
|
171
|
+
S extends { [K in keyof S]: (...args: any[]) => Effect.Effect<any, OAuthUnavailable> },
|
|
172
|
+
>(
|
|
173
|
+
service: S,
|
|
174
|
+
inputs: { readonly [K in keyof S]: (input: any) => any },
|
|
175
|
+
active: () => boolean,
|
|
176
|
+
poison: () => void,
|
|
177
|
+
): S =>
|
|
178
|
+
Object.freeze(
|
|
179
|
+
Object.fromEntries(
|
|
180
|
+
Object.entries(service).map(([key, method]) => [
|
|
181
|
+
key,
|
|
182
|
+
(input: any, prepare?: any) =>
|
|
183
|
+
Effect.suspend(() => {
|
|
184
|
+
if (!active()) return Effect.fail(unavailable());
|
|
185
|
+
const retained = inputs[key as keyof S](input);
|
|
186
|
+
|
|
187
|
+
return (method as (...args: any[]) => Effect.Effect<any, OAuthUnavailable>)(
|
|
188
|
+
retained,
|
|
189
|
+
prepare,
|
|
190
|
+
);
|
|
191
|
+
}).pipe(
|
|
192
|
+
Effect.onExit((exit) =>
|
|
193
|
+
Effect.sync(() => {
|
|
194
|
+
if (exit._tag === "Failure") poison();
|
|
195
|
+
}),
|
|
196
|
+
),
|
|
197
|
+
Effect.catchDefect(() => Effect.fail(unavailable())),
|
|
198
|
+
),
|
|
199
|
+
]),
|
|
200
|
+
),
|
|
201
|
+
) as S;
|