@yielded/auth-persistence 0.1.0-beta.22 → 0.1.0-beta.24
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 +13 -5
- package/dist/Adapter.mjs +9 -2
- 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/authority-snapshot.mjs +53 -0
- package/dist/internal/authority-snapshot.mjs.map +1 -0
- package/dist/internal/configuration.d.mts +1 -1
- package/dist/internal/configuration.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 +4 -97
- package/dist/internal/models/password-model.mjs +0 -3
- package/dist/internal/models/password-model.mjs.map +1 -1
- package/dist/internal/native-sql-table.d.mts +34 -0
- package/dist/internal/native-sql-table.mjs +111 -0
- package/dist/internal/native-sql-table.mjs.map +1 -0
- 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/target.d.mts +1 -1
- package/dist/internal/passkey-kernel.d.mts +5 -5
- package/dist/internal/passkeys.mjs +1 -1
- package/dist/internal/password-credentials.d.mts +11 -0
- package/dist/internal/password-credentials.mjs +121 -0
- package/dist/internal/password-credentials.mjs.map +1 -0
- package/dist/internal/password-kernel.d.mts +15 -28
- package/dist/internal/password-kernel.mjs +35 -236
- package/dist/internal/password-kernel.mjs.map +1 -1
- package/dist/internal/persistence.d.mts +2 -0
- package/dist/internal/persistence.mjs +5 -5
- package/dist/internal/persistence.mjs.map +1 -1
- package/dist/internal/query-operations.d.mts +2 -0
- package/dist/internal/session-kernel.d.mts +5 -3
- package/dist/internal/session-kernel.mjs +95 -29
- 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-persistence.mjs +3 -1
- package/dist/internal/sql-persistence.mjs.map +1 -1
- 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 +2 -67
- 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 -99
- package/dist/internal/storage-tables.mjs +0 -43
- 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-execution-kernel.d.mts +1 -1
- package/dist/internal/transaction-kernel.d.mts +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 +97 -8
- package/src/OAuthPersistence.ts +427 -0
- package/src/index.ts +3 -0
- package/src/internal/authority-snapshot.ts +91 -0
- package/src/internal/configuration.ts +1 -8
- 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 +0 -120
- package/src/internal/native-sql-table.ts +178 -0
- package/src/internal/oauth/accounts.ts +838 -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/password-credentials.ts +245 -0
- package/src/internal/password-kernel.ts +98 -712
- package/src/internal/persistence.ts +5 -10
- package/src/internal/query-operations.ts +2 -0
- package/src/internal/session-kernel.ts +231 -81
- package/src/internal/sql-oauth-connected-model.ts +145 -0
- package/src/internal/sql-oauth-model.ts +263 -0
- package/src/internal/sql-persistence.ts +2 -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 -66
- package/src/internal/storage-plans.ts +218 -0
- package/src/internal/storage-tables.ts +0 -35
- package/src/internal/storage-validation.ts +58 -0
- package/src/internal/transaction-kernel.ts +6 -2
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type * as M from "@yielded/auth/OAuth";
|
|
2
|
+
import { Effect, Schema } from "effect";
|
|
3
|
+
|
|
4
|
+
import { digest, invariant, storage } from "./state";
|
|
5
|
+
|
|
6
|
+
export const strings = storage(Schema.Array(Schema.String));
|
|
7
|
+
|
|
8
|
+
const encoder = new TextEncoder();
|
|
9
|
+
|
|
10
|
+
const decoder = new TextDecoder("utf-8", { fatal: true });
|
|
11
|
+
|
|
12
|
+
export const key = Effect.fnUntraced(function* (domain: string, fields: ReadonlyArray<string>) {
|
|
13
|
+
for (const value of fields) invariant(decoder.decode(encoder.encode(value)) === value);
|
|
14
|
+
|
|
15
|
+
return "v1:" + (yield* digest(strings.encode([domain, ...fields])));
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
export const clientKey = (configuration: M.OAuthConnectedConfiguration) =>
|
|
19
|
+
key("effect-auth/oauth-connected-client/v1", [
|
|
20
|
+
configuration.provider,
|
|
21
|
+
configuration.issuer,
|
|
22
|
+
configuration.profile.clientRegistrationId,
|
|
23
|
+
]);
|
|
24
|
+
|
|
25
|
+
export const cohortKey = (client: string, identity: string) =>
|
|
26
|
+
key("effect-auth/oauth-connected-cohort/v1", [client, identity]);
|
|
27
|
+
|
|
28
|
+
export const scopeKey = (provider: string, issuer: string) =>
|
|
29
|
+
key("effect-auth/oauth-connected-provider-issuer/v1", [provider, issuer]).pipe(
|
|
30
|
+
Effect.map((value) => "scope:" + value),
|
|
31
|
+
);
|
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
import type * as M from "@yielded/auth/OAuth";
|
|
2
|
+
import {
|
|
3
|
+
OAuthConnectedRevocationClaim,
|
|
4
|
+
type OAuthConnectedRevocations,
|
|
5
|
+
snapshotOAuthSync,
|
|
6
|
+
} from "@yielded/auth/OAuth";
|
|
7
|
+
import { Effect, Context, Layer } from "effect";
|
|
8
|
+
|
|
9
|
+
import { OAuthConnectedCollection } from "./connected-collection";
|
|
10
|
+
import { OAuthConnectedCustody } from "./connected-custody";
|
|
11
|
+
import { OAuthConnectedFlow } from "./connected-flow";
|
|
12
|
+
import type * as FTypes from "./connected-flow";
|
|
13
|
+
import { OAuthConnectedState } from "./connected-state";
|
|
14
|
+
import type * as STypes from "./connected-state";
|
|
15
|
+
import { CurrentOAuthTransaction } from "./owner";
|
|
16
|
+
import { OAuthQueryCompiler } from "./query-compiler";
|
|
17
|
+
import { invariant, storage } from "./state";
|
|
18
|
+
|
|
19
|
+
type WorkerInput<K extends keyof OAuthConnectedRevocations["Service"]> = Parameters<
|
|
20
|
+
OAuthConnectedRevocations["Service"][K]
|
|
21
|
+
>[0];
|
|
22
|
+
|
|
23
|
+
const make = Effect.gen(function* () {
|
|
24
|
+
const operations = yield* OAuthQueryCompiler;
|
|
25
|
+
const connectedCollection = yield* OAuthConnectedCollection;
|
|
26
|
+
const C = yield* OAuthConnectedCustody;
|
|
27
|
+
const F = yield* OAuthConnectedFlow;
|
|
28
|
+
const S = yield* OAuthConnectedState;
|
|
29
|
+
|
|
30
|
+
const { asc, eq, lte, sql } = operations;
|
|
31
|
+
const { collect, discover } = connectedCollection;
|
|
32
|
+
const { both, col, equal } = operations;
|
|
33
|
+
|
|
34
|
+
const revocationClaimStorage = storage(OAuthConnectedRevocationClaim);
|
|
35
|
+
|
|
36
|
+
const jobLocks = Effect.fn("oauthConnected.jobLocks")(function* (
|
|
37
|
+
mapping: STypes.Revocations,
|
|
38
|
+
job: M.OAuthConnectedRevocationJob,
|
|
39
|
+
) {
|
|
40
|
+
const owner = yield* CurrentOAuthTransaction;
|
|
41
|
+
|
|
42
|
+
const token = job.context.token,
|
|
43
|
+
client = yield* S.client(mapping, token.configuration, false);
|
|
44
|
+
|
|
45
|
+
if (client === undefined) return undefined;
|
|
46
|
+
|
|
47
|
+
const tuple = yield* F.inspectTuple(mapping, token.identity),
|
|
48
|
+
t = mapping.ownership.tuple;
|
|
49
|
+
|
|
50
|
+
const native = yield* mapping.subjectId.toNative(token.subjectId);
|
|
51
|
+
|
|
52
|
+
if (
|
|
53
|
+
tuple.row?.[t.state] !== "Owned" ||
|
|
54
|
+
!mapping.subjectId.equals(tuple.row[t.subjectId], native)
|
|
55
|
+
)
|
|
56
|
+
return undefined;
|
|
57
|
+
const cohort = yield* S.cohort(mapping, client.id, tuple.key, false);
|
|
58
|
+
|
|
59
|
+
invariant(cohort.row !== undefined);
|
|
60
|
+
|
|
61
|
+
const j = mapping.job,
|
|
62
|
+
read = yield* owner.read(j.table, equal(j.table, { [j.jobId]: job.context.jobId }), {
|
|
63
|
+
limit: 1,
|
|
64
|
+
}),
|
|
65
|
+
row = read.rows[0];
|
|
66
|
+
|
|
67
|
+
if (row === undefined) return undefined;
|
|
68
|
+
invariant(row[j.state] === "Confirmed" || cohort.blocked);
|
|
69
|
+
invariant(
|
|
70
|
+
row[j.snapshot] === S.jobStorage.encode(job) &&
|
|
71
|
+
row[j.moduleId] === token.moduleId &&
|
|
72
|
+
mapping.subjectId.equals(native, row[j.subjectId]) &&
|
|
73
|
+
row[j.identityKey] === tuple.key &&
|
|
74
|
+
row[j.clientKey] === client.id &&
|
|
75
|
+
row[j.cohortKey] === cohort.id &&
|
|
76
|
+
row[j.grantId] === token.grantId &&
|
|
77
|
+
["Pending", "Claimed", "Unknown", "Confirmed"].includes(row[j.state]),
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
return { client, cohort, row };
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
const claimRevocation = Effect.fn("oauthConnected.claimRevocation")(function* (
|
|
84
|
+
mapping: STypes.Revocations,
|
|
85
|
+
input: WorkerInput<"claim">,
|
|
86
|
+
) {
|
|
87
|
+
const owner = yield* CurrentOAuthTransaction;
|
|
88
|
+
|
|
89
|
+
invariant(
|
|
90
|
+
Number.isSafeInteger(input.lifetimeMillis) &&
|
|
91
|
+
input.lifetimeMillis >= 1000 &&
|
|
92
|
+
input.lifetimeMillis <= 120000,
|
|
93
|
+
);
|
|
94
|
+
const j = mapping.job;
|
|
95
|
+
|
|
96
|
+
const candidate = yield* owner.read(
|
|
97
|
+
j.table,
|
|
98
|
+
equal(j.table, { [j.moduleId]: input.moduleId, [j.state]: "Pending" }),
|
|
99
|
+
{
|
|
100
|
+
limit: 1,
|
|
101
|
+
takeOnly: true,
|
|
102
|
+
lock: false,
|
|
103
|
+
observe: false,
|
|
104
|
+
orderBy: asc(col(j.table, j.jobId)),
|
|
105
|
+
},
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
const row = candidate.rows[0];
|
|
109
|
+
|
|
110
|
+
if (row === undefined) return { _tag: "Empty" } as const;
|
|
111
|
+
const job = S.jobStorage.decode(row[j.snapshot]);
|
|
112
|
+
const locks = yield* jobLocks(mapping, job);
|
|
113
|
+
|
|
114
|
+
if (locks === undefined || locks.row[j.state] !== "Pending") return { _tag: "Empty" } as const;
|
|
115
|
+
invariant(
|
|
116
|
+
locks.row[j.claimId] === null &&
|
|
117
|
+
locks.row[j.claimedAt] === null &&
|
|
118
|
+
locks.row[j.claimExpiresAt] === null,
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
const now = yield* owner.now(mapping.clock),
|
|
122
|
+
expires = now + input.lifetimeMillis;
|
|
123
|
+
|
|
124
|
+
invariant(Number.isSafeInteger(expires));
|
|
125
|
+
|
|
126
|
+
const claim = snapshotOAuthSync(OAuthConnectedRevocationClaim, {
|
|
127
|
+
job,
|
|
128
|
+
claimId: input.claimId,
|
|
129
|
+
claimedAtMillis: now,
|
|
130
|
+
claimExpiresAtMillis: expires,
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
yield* owner.update(
|
|
134
|
+
j.table,
|
|
135
|
+
{ [j.jobId]: job.context.jobId, [j.state]: "Pending" },
|
|
136
|
+
{
|
|
137
|
+
[j.state]: "Claimed",
|
|
138
|
+
[j.claimId]: input.claimId,
|
|
139
|
+
[j.claimedAt]: mapping.clock.encodeInstant(now),
|
|
140
|
+
[j.claimExpiresAt]: mapping.clock.encodeInstant(expires),
|
|
141
|
+
[j.retentionUntil]: mapping.clock.encodeInstant(S.retainedUntil(mapping, expires)),
|
|
142
|
+
[j.version]: owner.marker,
|
|
143
|
+
},
|
|
144
|
+
);
|
|
145
|
+
owner.postconditions.push(
|
|
146
|
+
sql`${mapping.clock.engineNowMillis} >= ${now} and ${mapping.clock.engineNowMillis} < ${expires}`,
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
return { _tag: "Claimed", claim } as const;
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
const settleRevocation = Effect.fn("oauthConnected.settleRevocation")(function* (
|
|
153
|
+
mapping: STypes.Revocations,
|
|
154
|
+
input: WorkerInput<"settle">,
|
|
155
|
+
) {
|
|
156
|
+
const owner = yield* CurrentOAuthTransaction;
|
|
157
|
+
const locks = yield* jobLocks(mapping, input.claim.job);
|
|
158
|
+
|
|
159
|
+
if (locks === undefined) return { settled: false };
|
|
160
|
+
|
|
161
|
+
const j = mapping.job,
|
|
162
|
+
row = locks.row;
|
|
163
|
+
|
|
164
|
+
if (row[j.state] !== "Claimed" || row[j.claimId] !== input.claim.claimId)
|
|
165
|
+
return { settled: false };
|
|
166
|
+
invariant(
|
|
167
|
+
revocationClaimStorage.encode({
|
|
168
|
+
job: input.claim.job,
|
|
169
|
+
claimId: row[j.claimId],
|
|
170
|
+
claimedAtMillis: mapping.clock.decodeInstant(row[j.claimedAt]),
|
|
171
|
+
claimExpiresAtMillis: mapping.clock.decodeInstant(row[j.claimExpiresAt]),
|
|
172
|
+
}) === revocationClaimStorage.encode(input.claim),
|
|
173
|
+
);
|
|
174
|
+
const now = yield* owner.now(mapping.clock);
|
|
175
|
+
|
|
176
|
+
invariant(now >= input.claim.claimedAtMillis);
|
|
177
|
+
const confirmed = input.outcome === "Confirmed" && now < input.claim.claimExpiresAtMillis;
|
|
178
|
+
|
|
179
|
+
yield* owner.update(
|
|
180
|
+
j.table,
|
|
181
|
+
{
|
|
182
|
+
[j.jobId]: input.claim.job.context.jobId,
|
|
183
|
+
[j.state]: "Claimed",
|
|
184
|
+
[j.claimId]: input.claim.claimId,
|
|
185
|
+
},
|
|
186
|
+
{ [j.state]: confirmed ? "Confirmed" : "Unknown", [j.version]: owner.marker },
|
|
187
|
+
);
|
|
188
|
+
if (confirmed) {
|
|
189
|
+
owner.postconditions.push(
|
|
190
|
+
sql`${mapping.clock.engineNowMillis} >= ${now} and ${mapping.clock.engineNowMillis} < ${input.claim.claimExpiresAtMillis}`,
|
|
191
|
+
);
|
|
192
|
+
yield* C.clear(mapping, locks.client, locks.cohort, input.claim.job.context.jobId);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return { settled: true };
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
/** Candidate discovery is not authority. Each conditional write re-reads its row
|
|
199
|
+
* and engine time under the client/tuple/cohort order and retains a final guard. */
|
|
200
|
+
const cleanup = Effect.fn("oauthConnected.cleanup")(function* (
|
|
201
|
+
mapping: STypes.Mapping,
|
|
202
|
+
input: FTypes.Input<"cleanup">,
|
|
203
|
+
) {
|
|
204
|
+
const owner = yield* CurrentOAuthTransaction;
|
|
205
|
+
|
|
206
|
+
invariant(Number.isSafeInteger(input.limit) && input.limit >= 1 && input.limit <= 1000);
|
|
207
|
+
|
|
208
|
+
const f = mapping.flow,
|
|
209
|
+
a = mapping.admission,
|
|
210
|
+
d = mapping.command;
|
|
211
|
+
|
|
212
|
+
let terminalized = 0,
|
|
213
|
+
removed = 0,
|
|
214
|
+
visited = 0,
|
|
215
|
+
hasMore = false;
|
|
216
|
+
|
|
217
|
+
const at = yield* owner.now(mapping.clock);
|
|
218
|
+
|
|
219
|
+
const candidates = yield* owner.read(
|
|
220
|
+
f.table,
|
|
221
|
+
both(
|
|
222
|
+
eq(col(f.table, f.moduleId), input.moduleId),
|
|
223
|
+
sql`((${col(f.table, f.state)} in ('Prepared','Pending') and ${col(f.table, f.expiresAt)} <= ${mapping.clock.encodeInstant(at)}) or (${col(f.table, f.state)} = 'Claimed' and ${col(f.table, f.claimExpiresAt)} <= ${mapping.clock.encodeInstant(at)}) or (${col(f.table, f.state)} not in ('Prepared','Pending','Claimed') and ${col(f.table, f.work)} <> 'Unresolved' and ${col(f.table, f.retentionUntil)} <= ${mapping.clock.encodeInstant(at)}))`,
|
|
224
|
+
),
|
|
225
|
+
{
|
|
226
|
+
limit: input.limit + 1,
|
|
227
|
+
takeOnly: true,
|
|
228
|
+
observe: false,
|
|
229
|
+
lock: false,
|
|
230
|
+
orderBy: asc(col(f.table, f.flowId)),
|
|
231
|
+
},
|
|
232
|
+
);
|
|
233
|
+
|
|
234
|
+
const collectionCandidates = yield* discover(mapping, input.moduleId, input.limit);
|
|
235
|
+
|
|
236
|
+
const g = mapping.grant,
|
|
237
|
+
j = S.jobTable(mapping),
|
|
238
|
+
h = mapping.cohort;
|
|
239
|
+
|
|
240
|
+
yield* S.prelockClients(
|
|
241
|
+
mapping,
|
|
242
|
+
[
|
|
243
|
+
...candidates.rows.slice(0, input.limit).map((row) => row[f.clientKey]),
|
|
244
|
+
...collectionCandidates.jobs.slice(0, input.limit).map((row) => row[j!.clientKey]),
|
|
245
|
+
...collectionCandidates.grants.slice(0, input.limit).map((row) => row[g.clientKey]),
|
|
246
|
+
...collectionCandidates.cohorts.slice(0, input.limit).map((row) => row[h.clientKey]),
|
|
247
|
+
],
|
|
248
|
+
collectionCandidates.tuples.slice(0, input.limit).map((row) => ({
|
|
249
|
+
provider: row[mapping.ownership.tuple.provider],
|
|
250
|
+
issuer: row[mapping.ownership.tuple.issuer],
|
|
251
|
+
})),
|
|
252
|
+
);
|
|
253
|
+
hasMore = candidates.rows.length > input.limit;
|
|
254
|
+
for (const candidate of candidates.rows.slice(0, input.limit)) {
|
|
255
|
+
visited++;
|
|
256
|
+
// A terminal ledger retains client key but may erase the decryptable browser snapshot.
|
|
257
|
+
const heldClient = yield* S.lockClientById(mapping, candidate[f.clientKey]);
|
|
258
|
+
|
|
259
|
+
const read = yield* owner.read(
|
|
260
|
+
f.table,
|
|
261
|
+
equal(f.table, { [f.moduleId]: input.moduleId, [f.flowId]: candidate[f.flowId] }),
|
|
262
|
+
{ limit: 1 },
|
|
263
|
+
),
|
|
264
|
+
row = read.rows[0];
|
|
265
|
+
|
|
266
|
+
if (row === undefined) continue;
|
|
267
|
+
invariant(row[f.clientKey] === heldClient.id);
|
|
268
|
+
|
|
269
|
+
const now = yield* owner.now(mapping.clock),
|
|
270
|
+
state = row[f.state];
|
|
271
|
+
|
|
272
|
+
const expires = mapping.clock.decodeInstant(
|
|
273
|
+
state === "Claimed" ? row[f.claimExpiresAt] : row[f.expiresAt],
|
|
274
|
+
);
|
|
275
|
+
|
|
276
|
+
if ((state === "Prepared" || state === "Pending" || state === "Claimed") && now >= expires) {
|
|
277
|
+
yield* owner.update(
|
|
278
|
+
f.table,
|
|
279
|
+
{ [f.moduleId]: input.moduleId, [f.flowId]: row[f.flowId], [f.version]: row[f.version] },
|
|
280
|
+
{
|
|
281
|
+
[f.state]: state === "Claimed" ? "Ambiguous" : "Expired",
|
|
282
|
+
[f.snapshot]: null,
|
|
283
|
+
[f.version]: owner.marker,
|
|
284
|
+
},
|
|
285
|
+
);
|
|
286
|
+
owner.postconditions.push(sql`${mapping.clock.engineNowMillis} >= ${expires}`);
|
|
287
|
+
terminalized++;
|
|
288
|
+
} else if (
|
|
289
|
+
state !== "Prepared" &&
|
|
290
|
+
state !== "Pending" &&
|
|
291
|
+
state !== "Claimed" &&
|
|
292
|
+
row[f.work] !== "Unresolved" &&
|
|
293
|
+
now >= mapping.clock.decodeInstant(row[f.retentionUntil])
|
|
294
|
+
) {
|
|
295
|
+
yield* owner.remove(f.table, {
|
|
296
|
+
[f.moduleId]: input.moduleId,
|
|
297
|
+
[f.flowId]: row[f.flowId],
|
|
298
|
+
[f.version]: row[f.version],
|
|
299
|
+
});
|
|
300
|
+
owner.postconditions.push(
|
|
301
|
+
sql`${mapping.clock.engineNowMillis} >= ${mapping.clock.decodeInstant(row[f.retentionUntil])}`,
|
|
302
|
+
);
|
|
303
|
+
removed++;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
// Expired admission and command metadata have no external-operation obligation.
|
|
307
|
+
for (const table of [a, d]) {
|
|
308
|
+
if (visited >= input.limit) {
|
|
309
|
+
hasMore = true;
|
|
310
|
+
break;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const deadline = "expiresAt" in table ? table.expiresAt : table.retentionUntil,
|
|
314
|
+
key = "admissionId" in table ? table.admissionId : table.commandId;
|
|
315
|
+
|
|
316
|
+
const rows = yield* owner.read(
|
|
317
|
+
table.table,
|
|
318
|
+
both(
|
|
319
|
+
eq(col(table.table, table.moduleId), input.moduleId),
|
|
320
|
+
lte(col(table.table, deadline), mapping.clock.encodeInstant(at)),
|
|
321
|
+
),
|
|
322
|
+
{
|
|
323
|
+
limit: input.limit - visited + 1,
|
|
324
|
+
takeOnly: true,
|
|
325
|
+
observe: false,
|
|
326
|
+
lock: false,
|
|
327
|
+
orderBy: asc(col(table.table, key)),
|
|
328
|
+
},
|
|
329
|
+
);
|
|
330
|
+
|
|
331
|
+
if (rows.rows.length > input.limit - visited) hasMore = true;
|
|
332
|
+
for (const candidate of rows.rows.slice(0, input.limit - visited)) {
|
|
333
|
+
visited++;
|
|
334
|
+
|
|
335
|
+
const read = yield* owner.read(
|
|
336
|
+
table.table,
|
|
337
|
+
equal(table.table, { [table.moduleId]: input.moduleId, [key]: candidate[key] }),
|
|
338
|
+
{ limit: 1 },
|
|
339
|
+
),
|
|
340
|
+
row = read.rows[0];
|
|
341
|
+
|
|
342
|
+
const now = yield* owner.now(mapping.clock);
|
|
343
|
+
|
|
344
|
+
if (row !== undefined && now >= mapping.clock.decodeInstant(row[deadline])) {
|
|
345
|
+
yield* owner.remove(table.table, {
|
|
346
|
+
[table.moduleId]: input.moduleId,
|
|
347
|
+
[key]: row[key],
|
|
348
|
+
[table.version]: row[table.version],
|
|
349
|
+
});
|
|
350
|
+
owner.postconditions.push(
|
|
351
|
+
sql`${mapping.clock.engineNowMillis} >= ${mapping.clock.decodeInstant(row[deadline])}`,
|
|
352
|
+
);
|
|
353
|
+
removed++;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
const collection = yield* collect(
|
|
359
|
+
mapping,
|
|
360
|
+
input.moduleId,
|
|
361
|
+
input.limit - visited,
|
|
362
|
+
collectionCandidates,
|
|
363
|
+
);
|
|
364
|
+
|
|
365
|
+
removed += collection.removed;
|
|
366
|
+
hasMore ||= collection.hasMore;
|
|
367
|
+
|
|
368
|
+
return { terminalized, removed, hasMore };
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
return { claimRevocation, settleRevocation, cleanup };
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
export class OAuthConnectedMaintenance extends Context.Service<
|
|
375
|
+
OAuthConnectedMaintenance,
|
|
376
|
+
Effect.Success<typeof make>
|
|
377
|
+
>()("effect-auth/persistence/OAuthConnectedMaintenance") {
|
|
378
|
+
static readonly layer = Layer.effect(OAuthConnectedMaintenance, make);
|
|
379
|
+
}
|