@yielded/auth-persistence 0.1.0-beta.23 → 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 +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/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 -47
- 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-kernel.d.mts +5 -5
- 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 +4 -42
- 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/models/oauth-connected-model.ts +396 -0
- package/src/internal/models/oauth-model.ts +708 -0
- package/src/internal/models/password-model.ts +0 -56
- package/src/internal/native-sql-table.ts +14 -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 +5 -98
- 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,924 @@
|
|
|
1
|
+
/* oxlint-disable no-explicit-any -- compiler table shapes are erased; domain codecs and error channels remain typed. */
|
|
2
|
+
import { type OAuthAccountRevision, snapshotOAuthSync } from "@yielded/auth/OAuth";
|
|
3
|
+
import * as M from "@yielded/auth/OAuth";
|
|
4
|
+
import { AuthenticationRequirement } from "@yielded/auth/Sessions";
|
|
5
|
+
import { DateTime, Effect, Schema, Context, Layer } from "effect";
|
|
6
|
+
|
|
7
|
+
import type {
|
|
8
|
+
OAuthConnectedAuthorityMapping,
|
|
9
|
+
OAuthConnectedClientRegistrationTable,
|
|
10
|
+
OAuthConnectedCohortTable,
|
|
11
|
+
OAuthConnectedMapping,
|
|
12
|
+
OAuthConnectedPolicyInput,
|
|
13
|
+
OAuthConnectedRevocationMapping,
|
|
14
|
+
} from "../models/oauth-connected-model";
|
|
15
|
+
import type { SqlExpression as SQL, QueryFailure } from "../query-operations";
|
|
16
|
+
import { strings, clientKey, cohortKey, scopeKey, key } from "./connected-keys";
|
|
17
|
+
import { OAuthFlow } from "./flow";
|
|
18
|
+
import { CurrentOAuthTransaction } from "./owner";
|
|
19
|
+
import type { OAuthOwner, Row } from "./owner";
|
|
20
|
+
import { OAuthQueryCompiler } from "./query-compiler";
|
|
21
|
+
import {
|
|
22
|
+
digest,
|
|
23
|
+
invariant,
|
|
24
|
+
oauthIdentityKey,
|
|
25
|
+
sameRevision,
|
|
26
|
+
satisfies,
|
|
27
|
+
storage,
|
|
28
|
+
unavailable,
|
|
29
|
+
} from "./state";
|
|
30
|
+
type Table = object;
|
|
31
|
+
|
|
32
|
+
export type Authority = OAuthConnectedAuthorityMapping<any, any, any, any, any, any, any> & {
|
|
33
|
+
readonly client: Partial<Pick<OAuthConnectedClientRegistrationTable<any>, "encodeInsert">>;
|
|
34
|
+
readonly cohort: Partial<Pick<OAuthConnectedCohortTable<any>, "encodeInsert">>;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// Expression handles are supplied by the owning compiler, never decoded domain data.
|
|
38
|
+
export type Mapping = OAuthConnectedMapping<
|
|
39
|
+
any,
|
|
40
|
+
any,
|
|
41
|
+
any,
|
|
42
|
+
any,
|
|
43
|
+
any,
|
|
44
|
+
any,
|
|
45
|
+
any,
|
|
46
|
+
any,
|
|
47
|
+
any,
|
|
48
|
+
any,
|
|
49
|
+
any,
|
|
50
|
+
any,
|
|
51
|
+
any
|
|
52
|
+
>;
|
|
53
|
+
|
|
54
|
+
export type Revocations = OAuthConnectedRevocationMapping<any, any, any, any, any, any, any, any>;
|
|
55
|
+
|
|
56
|
+
export type Current = NonNullable<
|
|
57
|
+
Effect.Success<ReturnType<OAuthFlow["Service"]["currentSubject"]>>
|
|
58
|
+
>;
|
|
59
|
+
|
|
60
|
+
export interface ScopeAnchor {
|
|
61
|
+
readonly id: string;
|
|
62
|
+
row: Row;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface ClientAnchor {
|
|
66
|
+
readonly id: string;
|
|
67
|
+
row: Row;
|
|
68
|
+
counter: number;
|
|
69
|
+
readonly scope: ScopeAnchor;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const make = Effect.gen(function* () {
|
|
73
|
+
const operations = yield* OAuthQueryCompiler;
|
|
74
|
+
const flow = yield* OAuthFlow;
|
|
75
|
+
|
|
76
|
+
const { and, eq, isNull, sql } = operations;
|
|
77
|
+
const { currentSubject } = flow;
|
|
78
|
+
const { both, col, copiedRow, equal } = operations;
|
|
79
|
+
|
|
80
|
+
const contextStorage = storage(M.OAuthConnectedTransactionContext);
|
|
81
|
+
|
|
82
|
+
const flowStorage = storage(M.OAuthConnectedPendingFlow);
|
|
83
|
+
|
|
84
|
+
const claimStorage = storage(M.OAuthConnectedClaim);
|
|
85
|
+
|
|
86
|
+
const tokenContextStorage = storage(M.OAuthConnectedTokenContext);
|
|
87
|
+
|
|
88
|
+
const sealedStorage = storage(M.OAuthConnectedSealedTokens);
|
|
89
|
+
|
|
90
|
+
const grantStorage = storage(M.OAuthConnectedStoredGrant);
|
|
91
|
+
|
|
92
|
+
const admissionStorage = storage(
|
|
93
|
+
Schema.Struct({
|
|
94
|
+
grant: M.OAuthConnectedStoredGrant,
|
|
95
|
+
authorization: M.OAuthConnectedUseAuthorization,
|
|
96
|
+
}),
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
const commandStorage = storage(
|
|
100
|
+
Schema.Struct({
|
|
101
|
+
context: M.OAuthConnectedTokenContext,
|
|
102
|
+
originalDigest: Schema.String.check(Schema.isPattern(/^[A-Za-z0-9_-]{43}$/)),
|
|
103
|
+
revocationJobId: Schema.optionalKey(Schema.String.check(Schema.isMaxLength(256))),
|
|
104
|
+
}),
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
const disconnectStorage = storage(M.OAuthConnectedDisconnectGrant);
|
|
108
|
+
|
|
109
|
+
const refreshStorage = storage(M.OAuthConnectedRefreshClaim);
|
|
110
|
+
|
|
111
|
+
const jobStorage = storage(M.OAuthConnectedRevocationJob);
|
|
112
|
+
|
|
113
|
+
const summaryStorage = storage(M.OAuthConnectedSummary);
|
|
114
|
+
|
|
115
|
+
const disconnectedStorage = storage(M.OAuthConnectedDisconnected);
|
|
116
|
+
|
|
117
|
+
const safeInteger = Schema.Int.check(
|
|
118
|
+
Schema.isBetween({ minimum: 0, maximum: Number.MAX_SAFE_INTEGER }),
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
const orderParser = Schema.decodeSync(safeInteger);
|
|
122
|
+
|
|
123
|
+
const canonicalOrder = Schema.String.check(Schema.isPattern(/^(0|[1-9][0-9]{0,15})$/));
|
|
124
|
+
|
|
125
|
+
const orderTextParser = Schema.decodeSync(canonicalOrder);
|
|
126
|
+
|
|
127
|
+
const orderNumber = (value: string) => {
|
|
128
|
+
const number = orderParser(Number(orderTextParser(value)));
|
|
129
|
+
|
|
130
|
+
invariant(String(number) === value);
|
|
131
|
+
|
|
132
|
+
return number;
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const nativeOrder = (mapping: Authority, value: unknown) =>
|
|
136
|
+
orderParser(mapping.order.decode(value));
|
|
137
|
+
|
|
138
|
+
const initialGeneration = (cohort: string) =>
|
|
139
|
+
digest(cohort).pipe(
|
|
140
|
+
Effect.map((value) =>
|
|
141
|
+
M.OAuthConnectedTarget.fields.cohortGeneration.make("initial:" + value),
|
|
142
|
+
),
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
const configuration = (flow: M.OAuthConnectedPendingFlow): M.OAuthConnectedConfiguration =>
|
|
146
|
+
snapshotOAuthSync(M.OAuthConnectedConfiguration, flow.context);
|
|
147
|
+
|
|
148
|
+
const retainedUntil = (mapping: Authority, now: number) => {
|
|
149
|
+
invariant(
|
|
150
|
+
Number.isSafeInteger(mapping.retentionMillis) &&
|
|
151
|
+
mapping.retentionMillis >= 120000 &&
|
|
152
|
+
mapping.retentionMillis <= 2592000000,
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
return orderParser(now + mapping.retentionMillis);
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const rowCount = Effect.fn("oauthConnected.rowCount")(function* (table: Table, where: SQL) {
|
|
159
|
+
const owner = yield* CurrentOAuthTransaction;
|
|
160
|
+
|
|
161
|
+
const rows = yield* (
|
|
162
|
+
owner.database
|
|
163
|
+
.select({ value: sql`count(*)`.mapWith(Number) })
|
|
164
|
+
.from(table)
|
|
165
|
+
.where(where) as Effect.Effect<ReadonlyArray<{ value: number }>, QueryFailure>
|
|
166
|
+
).pipe(Effect.mapError(unavailable));
|
|
167
|
+
|
|
168
|
+
invariant(rows.length === 1);
|
|
169
|
+
|
|
170
|
+
return orderParser(rows[0]!.value);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
const countCondition = (table: Table, where: SQL, count: number) =>
|
|
174
|
+
sql`(select count(*) from ${table} where ${where}) = ${count}`;
|
|
175
|
+
|
|
176
|
+
const current = Effect.fn("oauthConnected.current")(function* (
|
|
177
|
+
mapping: Mapping,
|
|
178
|
+
subject: OAuthAccountRevision["subjectId"],
|
|
179
|
+
) {
|
|
180
|
+
const native = yield* mapping.subjectId.toNative(subject);
|
|
181
|
+
|
|
182
|
+
return yield* currentSubject(mapping, native);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
const nativeCopy = <N>(value: N): N => copiedRow({ value }).value as N;
|
|
186
|
+
|
|
187
|
+
const policyData = (input: OAuthConnectedPolicyInput<any>): OAuthConnectedPolicyInput<any> =>
|
|
188
|
+
Object.freeze({
|
|
189
|
+
...input,
|
|
190
|
+
subjectId: nativeCopy(input.subjectId),
|
|
191
|
+
revision: snapshotOAuthSync(M.OAuthConnectedUseAuthorization.fields.revision, input.revision),
|
|
192
|
+
...(input.kind === "sign-in"
|
|
193
|
+
? {
|
|
194
|
+
credential: snapshotOAuthSync(M.OAuthCredentialSnapshot, input.credential),
|
|
195
|
+
configuration: snapshotOAuthSync(M.OAuthConnectedConfiguration, input.configuration),
|
|
196
|
+
}
|
|
197
|
+
: {
|
|
198
|
+
authorization:
|
|
199
|
+
input.kind === "action"
|
|
200
|
+
? snapshotOAuthSync(M.OAuthConnectedActionAuthorization, input.authorization)
|
|
201
|
+
: snapshotOAuthSync(M.OAuthConnectedUseAuthorization, input.authorization),
|
|
202
|
+
...(input.kind === "action"
|
|
203
|
+
? {
|
|
204
|
+
configuration: snapshotOAuthSync(
|
|
205
|
+
M.OAuthConnectedConfiguration,
|
|
206
|
+
input.configuration,
|
|
207
|
+
),
|
|
208
|
+
}
|
|
209
|
+
: {}),
|
|
210
|
+
}),
|
|
211
|
+
...(input.grant === undefined
|
|
212
|
+
? {}
|
|
213
|
+
: { grant: snapshotOAuthSync(M.OAuthConnectedTokenContext, input.grant) }),
|
|
214
|
+
}) as OAuthConnectedPolicyInput<any>;
|
|
215
|
+
|
|
216
|
+
const jobTable = (mapping: Authority) => {
|
|
217
|
+
const direct = (mapping as Partial<Revocations>).job;
|
|
218
|
+
const configured = (mapping as Partial<Mapping>).revocation;
|
|
219
|
+
|
|
220
|
+
return direct ?? (configured?.mode === "cohort" ? configured.job : undefined);
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
const policy = Effect.fn("oauthConnected.policy")(function* (
|
|
224
|
+
mapping: Mapping,
|
|
225
|
+
input: OAuthConnectedPolicyInput<any>,
|
|
226
|
+
) {
|
|
227
|
+
const owner = yield* CurrentOAuthTransaction;
|
|
228
|
+
|
|
229
|
+
invariant((mapping.policy.guards?.length ?? 0) <= 32);
|
|
230
|
+
for (const guard of mapping.policy.guards ?? []) {
|
|
231
|
+
const read = yield* owner.read(guard.table, guard.condition(policyData(input)), {
|
|
232
|
+
orderBy: col(guard.table, guard.orderBy),
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
invariant(read.rows.length > 0);
|
|
236
|
+
}
|
|
237
|
+
const condition = mapping.policy.condition(policyData(input));
|
|
238
|
+
const accepted = yield* owner.check(condition);
|
|
239
|
+
|
|
240
|
+
if (accepted) owner.postconditions.push(condition);
|
|
241
|
+
|
|
242
|
+
return accepted;
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
const action = Effect.fn("oauthConnected.action")(function* (
|
|
246
|
+
mapping: Mapping,
|
|
247
|
+
found: Current,
|
|
248
|
+
authorization: M.OAuthConnectedActionAuthorization,
|
|
249
|
+
operation: "issue" | "claim" | "settle" | "disconnect",
|
|
250
|
+
expected: {
|
|
251
|
+
readonly moduleId: string;
|
|
252
|
+
readonly flowId: string;
|
|
253
|
+
readonly commandId: string;
|
|
254
|
+
readonly revision: OAuthAccountRevision;
|
|
255
|
+
readonly intent: string;
|
|
256
|
+
readonly maximumAgeMillis: number;
|
|
257
|
+
readonly configuration: M.OAuthConnectedConfiguration;
|
|
258
|
+
readonly grant?: M.OAuthConnectedTokenContext;
|
|
259
|
+
},
|
|
260
|
+
) {
|
|
261
|
+
const owner = yield* CurrentOAuthTransaction;
|
|
262
|
+
|
|
263
|
+
const challenge = authorization.challenge,
|
|
264
|
+
evidence = authorization.evidence;
|
|
265
|
+
|
|
266
|
+
const name =
|
|
267
|
+
operation === "issue"
|
|
268
|
+
? "connected-begin"
|
|
269
|
+
: operation === "disconnect"
|
|
270
|
+
? "connected-disconnect"
|
|
271
|
+
: "connected-complete";
|
|
272
|
+
|
|
273
|
+
const intentDigest = yield* digest(expected.intent);
|
|
274
|
+
|
|
275
|
+
const binding = yield* digest(
|
|
276
|
+
strings.encode([
|
|
277
|
+
"effect-auth/oauth-connected-action/v1",
|
|
278
|
+
expected.moduleId,
|
|
279
|
+
name,
|
|
280
|
+
expected.flowId,
|
|
281
|
+
expected.commandId,
|
|
282
|
+
intentDigest,
|
|
283
|
+
]),
|
|
284
|
+
);
|
|
285
|
+
|
|
286
|
+
if (
|
|
287
|
+
challenge.moduleId !== expected.moduleId ||
|
|
288
|
+
challenge.action !== name ||
|
|
289
|
+
challenge.flowId !== expected.flowId ||
|
|
290
|
+
challenge.commandId !== expected.commandId ||
|
|
291
|
+
challenge.intentDigest !== intentDigest ||
|
|
292
|
+
challenge.bindingDigest !== binding ||
|
|
293
|
+
evidence.flowId !== expected.flowId ||
|
|
294
|
+
evidence.bindingDigest !== binding ||
|
|
295
|
+
!sameRevision(challenge.revision, expected.revision) ||
|
|
296
|
+
!sameRevision(evidence.revision, expected.revision) ||
|
|
297
|
+
!sameRevision(found.revision, expected.revision)
|
|
298
|
+
)
|
|
299
|
+
return false;
|
|
300
|
+
const now = yield* owner.now(mapping.clock);
|
|
301
|
+
|
|
302
|
+
const requirement = snapshotOAuthSync(
|
|
303
|
+
AuthenticationRequirement,
|
|
304
|
+
mapping.subject.decodeActionRequirement(copiedRow(found.row), name),
|
|
305
|
+
);
|
|
306
|
+
|
|
307
|
+
const revisions = new Map(
|
|
308
|
+
found.revision.credentials.map((item) => [item.credentialId, item.revision]),
|
|
309
|
+
);
|
|
310
|
+
|
|
311
|
+
if (
|
|
312
|
+
evidence.proofs.some(
|
|
313
|
+
(proof) =>
|
|
314
|
+
!revisions.has(proof.credentialId) || DateTime.toEpochMillis(proof.verifiedAt) > now,
|
|
315
|
+
)
|
|
316
|
+
)
|
|
317
|
+
return false;
|
|
318
|
+
for (const required of [authorization.requirement, requirement]) {
|
|
319
|
+
const age = Math.min(expected.maximumAgeMillis, required.maximumAgeMillis);
|
|
320
|
+
|
|
321
|
+
const fresh = evidence.proofs.filter(
|
|
322
|
+
(proof) => now - DateTime.toEpochMillis(proof.verifiedAt) < age,
|
|
323
|
+
);
|
|
324
|
+
|
|
325
|
+
if (!satisfies(fresh, required)) return false;
|
|
326
|
+
owner.postconditions.push(
|
|
327
|
+
sql`${mapping.clock.engineNowMillis} >= ${now} and ${both(...fresh.map((proof) => sql`${mapping.clock.engineNowMillis} < ${DateTime.toEpochMillis(proof.verifiedAt) + age}`))}`,
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
return yield* policy(mapping, {
|
|
332
|
+
subjectId: found.nativeId,
|
|
333
|
+
revision: found.revision,
|
|
334
|
+
kind: "action",
|
|
335
|
+
operation,
|
|
336
|
+
authorization,
|
|
337
|
+
configuration: expected.configuration,
|
|
338
|
+
...(expected.grant === undefined ? {} : { grant: expected.grant }),
|
|
339
|
+
});
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
const useAuthority = Effect.fn("oauthConnected.useAuthority")(function* (
|
|
343
|
+
mapping: Mapping,
|
|
344
|
+
authorization: M.OAuthConnectedUseAuthorization,
|
|
345
|
+
kind: "metadata" | "use",
|
|
346
|
+
grant?: M.OAuthConnectedTokenContext,
|
|
347
|
+
) {
|
|
348
|
+
const owner = yield* CurrentOAuthTransaction;
|
|
349
|
+
const found = yield* current(mapping, authorization.revision.subjectId);
|
|
350
|
+
const now = yield* owner.now(mapping.clock);
|
|
351
|
+
|
|
352
|
+
if (
|
|
353
|
+
found === undefined ||
|
|
354
|
+
authorization.purpose !== kind ||
|
|
355
|
+
!sameRevision(found.revision, authorization.revision) ||
|
|
356
|
+
now >= authorization.expiresAtMillis ||
|
|
357
|
+
(grant !== undefined &&
|
|
358
|
+
(grant.moduleId !== authorization.moduleId ||
|
|
359
|
+
grant.subjectId !== authorization.revision.subjectId ||
|
|
360
|
+
(authorization.grantId !== undefined && authorization.grantId !== grant.grantId) ||
|
|
361
|
+
(authorization.profileKey !== undefined &&
|
|
362
|
+
authorization.profileKey !== grant.configuration.profile.key)))
|
|
363
|
+
)
|
|
364
|
+
return undefined;
|
|
365
|
+
if (
|
|
366
|
+
!(yield* policy(mapping, {
|
|
367
|
+
subjectId: found.nativeId,
|
|
368
|
+
revision: found.revision,
|
|
369
|
+
kind,
|
|
370
|
+
authorization,
|
|
371
|
+
...(grant === undefined ? {} : { grant }),
|
|
372
|
+
}))
|
|
373
|
+
)
|
|
374
|
+
return undefined;
|
|
375
|
+
owner.postconditions.push(
|
|
376
|
+
sql`${mapping.clock.engineNowMillis} >= ${now} and ${mapping.clock.engineNowMillis} < ${authorization.expiresAtMillis}`,
|
|
377
|
+
);
|
|
378
|
+
|
|
379
|
+
return found;
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
const scopeCache = new WeakMap<OAuthOwner, Map<Table, Map<string, ScopeAnchor>>>();
|
|
383
|
+
|
|
384
|
+
const clientCache = new WeakMap<OAuthOwner, Map<Table, Map<string, ClientAnchor>>>();
|
|
385
|
+
|
|
386
|
+
const anchors = Effect.fn("oauthConnected.anchors")(function* <A>(
|
|
387
|
+
cache: WeakMap<OAuthOwner, Map<Table, Map<string, A>>>,
|
|
388
|
+
table: Table,
|
|
389
|
+
) {
|
|
390
|
+
const owner = yield* CurrentOAuthTransaction;
|
|
391
|
+
let tables = cache.get(owner);
|
|
392
|
+
|
|
393
|
+
if (tables === undefined) {
|
|
394
|
+
tables = new Map();
|
|
395
|
+
cache.set(owner, tables);
|
|
396
|
+
}
|
|
397
|
+
let rows = tables.get(table);
|
|
398
|
+
|
|
399
|
+
if (rows === undefined) {
|
|
400
|
+
rows = new Map();
|
|
401
|
+
tables.set(table, rows);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
return rows;
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
/** Empty registration ID is impossible in the public profile label schema. The
|
|
408
|
+
* distinct 52-byte key domain shares the physical table, never a client counter. */
|
|
409
|
+
const scope = Effect.fn("oauthConnected.scope")(function* (
|
|
410
|
+
mapping: Authority,
|
|
411
|
+
config: M.OAuthConnectedConfiguration,
|
|
412
|
+
create: boolean,
|
|
413
|
+
) {
|
|
414
|
+
const owner = yield* CurrentOAuthTransaction;
|
|
415
|
+
|
|
416
|
+
const c = mapping.client,
|
|
417
|
+
id = yield* scopeKey(config.provider, config.issuer),
|
|
418
|
+
cache = yield* anchors(scopeCache, c.table);
|
|
419
|
+
|
|
420
|
+
const existing = cache.get(id);
|
|
421
|
+
|
|
422
|
+
if (existing !== undefined) return existing;
|
|
423
|
+
const read = yield* owner.read(c.table, equal(c.table, { [c.clientKey]: id }), { limit: 1 });
|
|
424
|
+
|
|
425
|
+
if (read.rows.length === 0) {
|
|
426
|
+
const related = [
|
|
427
|
+
mapping.flow,
|
|
428
|
+
mapping.grant,
|
|
429
|
+
mapping.cohort,
|
|
430
|
+
...(jobTable(mapping) === undefined ? [] : [jobTable(mapping)!]),
|
|
431
|
+
];
|
|
432
|
+
|
|
433
|
+
invariant(
|
|
434
|
+
yield* owner.check(
|
|
435
|
+
both(
|
|
436
|
+
sql`not exists(select 1 from ${c.table} where ${owner.exact(c.table, { [c.provider]: config.provider, [c.issuer]: config.issuer })})`,
|
|
437
|
+
...related.map(
|
|
438
|
+
(v) =>
|
|
439
|
+
sql`not exists(select 1 from ${v.table} where not exists(select 1 from ${c.table} where ${col(c.table, c.clientKey)} = ${col(v.table, v.clientKey)}))`,
|
|
440
|
+
),
|
|
441
|
+
),
|
|
442
|
+
),
|
|
443
|
+
);
|
|
444
|
+
if (!create) return undefined;
|
|
445
|
+
invariant(c.encodeInsert !== undefined);
|
|
446
|
+
|
|
447
|
+
const inserted = yield* owner.insert(
|
|
448
|
+
c.table,
|
|
449
|
+
{
|
|
450
|
+
...c.encodeInsert(snapshotOAuthSync(M.OAuthConnectedConfiguration, config)),
|
|
451
|
+
[c.clientKey]: id,
|
|
452
|
+
[c.provider]: config.provider,
|
|
453
|
+
[c.issuer]: config.issuer,
|
|
454
|
+
[c.clientRegistrationId]: "",
|
|
455
|
+
[c.counter]: mapping.order.encode(0),
|
|
456
|
+
[c.version]: owner.marker,
|
|
457
|
+
},
|
|
458
|
+
{ [c.clientKey]: id },
|
|
459
|
+
true,
|
|
460
|
+
);
|
|
461
|
+
|
|
462
|
+
read.rows = inserted.rows;
|
|
463
|
+
}
|
|
464
|
+
const row = read.rows[0]!;
|
|
465
|
+
|
|
466
|
+
invariant(
|
|
467
|
+
row[c.provider] === config.provider &&
|
|
468
|
+
row[c.issuer] === config.issuer &&
|
|
469
|
+
row[c.clientRegistrationId] === "" &&
|
|
470
|
+
nativeOrder(mapping, row[c.counter]) === 0,
|
|
471
|
+
);
|
|
472
|
+
const found = { id, row };
|
|
473
|
+
|
|
474
|
+
cache.set(id, found);
|
|
475
|
+
|
|
476
|
+
return found;
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
const touchScope = Effect.fn("oauthConnected.touchScope")(function* (
|
|
480
|
+
mapping: Authority,
|
|
481
|
+
found: ScopeAnchor,
|
|
482
|
+
) {
|
|
483
|
+
const owner = yield* CurrentOAuthTransaction;
|
|
484
|
+
const c = mapping.client;
|
|
485
|
+
|
|
486
|
+
yield* owner.update(
|
|
487
|
+
c.table,
|
|
488
|
+
{ [c.clientKey]: found.id, [c.version]: found.row[c.version] },
|
|
489
|
+
{ [c.version]: owner.marker },
|
|
490
|
+
);
|
|
491
|
+
found.row = { ...found.row, [c.version]: owner.marker };
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
const readScope = Effect.fn("oauthConnected.readScope")(function* (
|
|
495
|
+
mapping: Authority,
|
|
496
|
+
provider: string,
|
|
497
|
+
issuer: string,
|
|
498
|
+
) {
|
|
499
|
+
const owner = yield* CurrentOAuthTransaction;
|
|
500
|
+
|
|
501
|
+
const c = mapping.client,
|
|
502
|
+
id = yield* scopeKey(provider, issuer),
|
|
503
|
+
cache = yield* anchors(scopeCache, c.table),
|
|
504
|
+
cached = cache.get(id);
|
|
505
|
+
|
|
506
|
+
if (cached !== undefined) return cached;
|
|
507
|
+
|
|
508
|
+
const read = yield* owner.read(c.table, equal(c.table, { [c.clientKey]: id }), { limit: 1 }),
|
|
509
|
+
row = read.rows[0];
|
|
510
|
+
|
|
511
|
+
invariant(
|
|
512
|
+
row !== undefined &&
|
|
513
|
+
row[c.provider] === provider &&
|
|
514
|
+
row[c.issuer] === issuer &&
|
|
515
|
+
row[c.clientRegistrationId] === "" &&
|
|
516
|
+
nativeOrder(mapping, row[c.counter]) === 0,
|
|
517
|
+
);
|
|
518
|
+
const found = { id, row };
|
|
519
|
+
|
|
520
|
+
cache.set(id, found);
|
|
521
|
+
|
|
522
|
+
return found;
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
const lockClientById = Effect.fn("oauthConnected.lockClientById")(function* (
|
|
526
|
+
mapping: Authority,
|
|
527
|
+
id: string,
|
|
528
|
+
) {
|
|
529
|
+
const owner = yield* CurrentOAuthTransaction;
|
|
530
|
+
|
|
531
|
+
const c = mapping.client,
|
|
532
|
+
cache = yield* anchors(clientCache, c.table),
|
|
533
|
+
cached = cache.get(id);
|
|
534
|
+
|
|
535
|
+
if (cached !== undefined) return cached;
|
|
536
|
+
|
|
537
|
+
const discovery = yield* owner.read(c.table, equal(c.table, { [c.clientKey]: id }), {
|
|
538
|
+
limit: 1,
|
|
539
|
+
observe: false,
|
|
540
|
+
lock: false,
|
|
541
|
+
}),
|
|
542
|
+
initial = discovery.rows[0];
|
|
543
|
+
|
|
544
|
+
invariant(initial !== undefined);
|
|
545
|
+
const scope = yield* readScope(mapping, initial[c.provider], initial[c.issuer]);
|
|
546
|
+
|
|
547
|
+
const read = yield* owner.read(c.table, equal(c.table, { [c.clientKey]: id }), { limit: 1 }),
|
|
548
|
+
row = read.rows[0];
|
|
549
|
+
|
|
550
|
+
invariant(
|
|
551
|
+
row !== undefined &&
|
|
552
|
+
row[c.clientRegistrationId] !== "" &&
|
|
553
|
+
id ===
|
|
554
|
+
(yield* key("effect-auth/oauth-connected-client/v1", [
|
|
555
|
+
row[c.provider],
|
|
556
|
+
row[c.issuer],
|
|
557
|
+
row[c.clientRegistrationId],
|
|
558
|
+
])) &&
|
|
559
|
+
scope.id === (yield* scopeKey(row[c.provider], row[c.issuer])),
|
|
560
|
+
);
|
|
561
|
+
const found = { id, row, counter: nativeOrder(mapping, row[c.counter]), scope };
|
|
562
|
+
|
|
563
|
+
cache.set(id, found);
|
|
564
|
+
|
|
565
|
+
return found;
|
|
566
|
+
});
|
|
567
|
+
|
|
568
|
+
const heldScope = Effect.fnUntraced(function* (
|
|
569
|
+
mapping: Authority,
|
|
570
|
+
provider: string,
|
|
571
|
+
issuer: string,
|
|
572
|
+
) {
|
|
573
|
+
const cache = yield* anchors(scopeCache, mapping.client.table);
|
|
574
|
+
|
|
575
|
+
return cache.get(yield* scopeKey(provider, issuer));
|
|
576
|
+
});
|
|
577
|
+
|
|
578
|
+
/** Cleanup obtains every scope then every client before entering any tuple. */
|
|
579
|
+
const prelockClients = Effect.fn("oauthConnected.prelockClients")(function* (
|
|
580
|
+
mapping: Authority,
|
|
581
|
+
ids: ReadonlyArray<string>,
|
|
582
|
+
releaseScopes: ReadonlyArray<{ readonly provider: string; readonly issuer: string }> = [],
|
|
583
|
+
) {
|
|
584
|
+
const owner = yield* CurrentOAuthTransaction;
|
|
585
|
+
|
|
586
|
+
const unique = [...new Set(ids)].sort(),
|
|
587
|
+
c = mapping.client;
|
|
588
|
+
|
|
589
|
+
invariant(unique.length <= 4000);
|
|
590
|
+
const scopes = new Map<string, { provider: string; issuer: string; required: boolean }>();
|
|
591
|
+
|
|
592
|
+
for (const value of releaseScopes)
|
|
593
|
+
scopes.set(yield* scopeKey(value.provider, value.issuer), { ...value, required: false });
|
|
594
|
+
for (const id of unique) {
|
|
595
|
+
const read = yield* owner.read(c.table, equal(c.table, { [c.clientKey]: id }), {
|
|
596
|
+
limit: 1,
|
|
597
|
+
lock: false,
|
|
598
|
+
observe: false,
|
|
599
|
+
}),
|
|
600
|
+
row = read.rows[0];
|
|
601
|
+
|
|
602
|
+
invariant(row !== undefined);
|
|
603
|
+
scopes.set(yield* scopeKey(row[c.provider], row[c.issuer]), {
|
|
604
|
+
provider: row[c.provider],
|
|
605
|
+
issuer: row[c.issuer],
|
|
606
|
+
required: true,
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
for (const [id, { provider, issuer, required }] of [...scopes].sort(([a], [b]) =>
|
|
610
|
+
a < b ? -1 : a > b ? 1 : 0,
|
|
611
|
+
)) {
|
|
612
|
+
if (required) yield* readScope(mapping, provider, issuer);
|
|
613
|
+
else {
|
|
614
|
+
const read = yield* owner.read(c.table, equal(c.table, { [c.clientKey]: id }), {
|
|
615
|
+
limit: 1,
|
|
616
|
+
}),
|
|
617
|
+
row = read.rows[0];
|
|
618
|
+
|
|
619
|
+
if (row !== undefined) {
|
|
620
|
+
invariant(
|
|
621
|
+
row[c.provider] === provider &&
|
|
622
|
+
row[c.issuer] === issuer &&
|
|
623
|
+
row[c.clientRegistrationId] === "" &&
|
|
624
|
+
nativeOrder(mapping, row[c.counter]) === 0,
|
|
625
|
+
);
|
|
626
|
+
(yield* anchors(scopeCache, c.table)).set(id, { id, row });
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
for (const id of unique) yield* lockClientById(mapping, id);
|
|
631
|
+
});
|
|
632
|
+
|
|
633
|
+
const client = Effect.fn("oauthConnected.client")(function* (
|
|
634
|
+
mapping: Authority,
|
|
635
|
+
config: M.OAuthConnectedConfiguration,
|
|
636
|
+
create: boolean,
|
|
637
|
+
) {
|
|
638
|
+
const owner = yield* CurrentOAuthTransaction;
|
|
639
|
+
|
|
640
|
+
const c = mapping.client,
|
|
641
|
+
id = yield* clientKey(config),
|
|
642
|
+
where = { [c.clientKey]: id };
|
|
643
|
+
|
|
644
|
+
const heldScope = yield* scope(mapping, config, create);
|
|
645
|
+
|
|
646
|
+
if (heldScope === undefined) return undefined;
|
|
647
|
+
|
|
648
|
+
const cache = yield* anchors(clientCache, c.table),
|
|
649
|
+
cached = cache.get(id);
|
|
650
|
+
|
|
651
|
+
if (cached !== undefined) return cached;
|
|
652
|
+
let read = yield* owner.read(c.table, equal(c.table, where), { limit: 1 });
|
|
653
|
+
|
|
654
|
+
if (read.rows.length === 0) {
|
|
655
|
+
const related = [
|
|
656
|
+
mapping.flow,
|
|
657
|
+
mapping.grant,
|
|
658
|
+
mapping.cohort,
|
|
659
|
+
...(jobTable(mapping) === undefined ? [] : [jobTable(mapping)!]),
|
|
660
|
+
];
|
|
661
|
+
|
|
662
|
+
invariant(
|
|
663
|
+
yield* owner.check(
|
|
664
|
+
both(
|
|
665
|
+
...related.map(
|
|
666
|
+
(v) =>
|
|
667
|
+
sql`not exists(select 1 from ${v.table} where ${eq(col(v.table, v.clientKey), id)})`,
|
|
668
|
+
),
|
|
669
|
+
),
|
|
670
|
+
),
|
|
671
|
+
);
|
|
672
|
+
}
|
|
673
|
+
if (read.rows.length === 0 && create) {
|
|
674
|
+
const inserted = yield* owner.insert(
|
|
675
|
+
c.table,
|
|
676
|
+
{
|
|
677
|
+
...(invariant(c.encodeInsert !== undefined),
|
|
678
|
+
c.encodeInsert(snapshotOAuthSync(M.OAuthConnectedConfiguration, config))),
|
|
679
|
+
...where,
|
|
680
|
+
[c.provider]: config.provider,
|
|
681
|
+
[c.issuer]: config.issuer,
|
|
682
|
+
[c.clientRegistrationId]: config.profile.clientRegistrationId,
|
|
683
|
+
[c.counter]: mapping.order.encode(0),
|
|
684
|
+
[c.version]: owner.marker,
|
|
685
|
+
},
|
|
686
|
+
where,
|
|
687
|
+
true,
|
|
688
|
+
);
|
|
689
|
+
|
|
690
|
+
read.rows = inserted.rows;
|
|
691
|
+
}
|
|
692
|
+
const row = read.rows[0];
|
|
693
|
+
|
|
694
|
+
if (row === undefined) return undefined;
|
|
695
|
+
invariant(
|
|
696
|
+
row[c.provider] === config.provider &&
|
|
697
|
+
row[c.issuer] === config.issuer &&
|
|
698
|
+
row[c.clientRegistrationId] === config.profile.clientRegistrationId,
|
|
699
|
+
);
|
|
700
|
+
const found = { id, row, counter: nativeOrder(mapping, row[c.counter]), scope: heldScope };
|
|
701
|
+
|
|
702
|
+
cache.set(id, found);
|
|
703
|
+
|
|
704
|
+
return found;
|
|
705
|
+
});
|
|
706
|
+
|
|
707
|
+
const nextOrder = Effect.fn("oauthConnected.nextOrder")(function* (
|
|
708
|
+
mapping: Authority,
|
|
709
|
+
found: NonNullable<Effect.Success<ReturnType<typeof client>>>,
|
|
710
|
+
) {
|
|
711
|
+
const owner = yield* CurrentOAuthTransaction;
|
|
712
|
+
|
|
713
|
+
const next = orderParser(found.counter + 1),
|
|
714
|
+
c = mapping.client;
|
|
715
|
+
|
|
716
|
+
yield* owner.update(
|
|
717
|
+
c.table,
|
|
718
|
+
{ [c.clientKey]: found.id, [c.counter]: mapping.order.encode(found.counter) },
|
|
719
|
+
{ [c.counter]: mapping.order.encode(next), [c.version]: owner.marker },
|
|
720
|
+
);
|
|
721
|
+
found.counter = next;
|
|
722
|
+
|
|
723
|
+
return next;
|
|
724
|
+
});
|
|
725
|
+
|
|
726
|
+
const cohort = Effect.fn("oauthConnected.cohort")(function* (
|
|
727
|
+
mapping: Authority,
|
|
728
|
+
clientId: string,
|
|
729
|
+
identityId: string,
|
|
730
|
+
create: boolean,
|
|
731
|
+
) {
|
|
732
|
+
const owner = yield* CurrentOAuthTransaction;
|
|
733
|
+
|
|
734
|
+
const c = mapping.cohort,
|
|
735
|
+
id = yield* cohortKey(clientId, identityId),
|
|
736
|
+
where = { [c.cohortKey]: id };
|
|
737
|
+
|
|
738
|
+
let read = yield* owner.read(c.table, equal(c.table, where), { limit: 1 });
|
|
739
|
+
|
|
740
|
+
if (read.rows.length === 0) {
|
|
741
|
+
const related = [
|
|
742
|
+
mapping.flow,
|
|
743
|
+
mapping.grant,
|
|
744
|
+
...(jobTable(mapping) === undefined ? [] : [jobTable(mapping)!]),
|
|
745
|
+
];
|
|
746
|
+
|
|
747
|
+
invariant(
|
|
748
|
+
yield* owner.check(
|
|
749
|
+
both(
|
|
750
|
+
...related.map(
|
|
751
|
+
(v) =>
|
|
752
|
+
sql`not exists(select 1 from ${v.table} where ${eq(col(v.table, v.cohortKey), id)})`,
|
|
753
|
+
),
|
|
754
|
+
),
|
|
755
|
+
),
|
|
756
|
+
);
|
|
757
|
+
}
|
|
758
|
+
if (read.rows.length === 0 && create) {
|
|
759
|
+
const inserted = yield* owner.insert(
|
|
760
|
+
c.table,
|
|
761
|
+
{
|
|
762
|
+
...(invariant(c.encodeInsert !== undefined),
|
|
763
|
+
c.encodeInsert({ clientKey: clientId, identityKey: identityId })),
|
|
764
|
+
...where,
|
|
765
|
+
[c.clientKey]: clientId,
|
|
766
|
+
[c.identityKey]: identityId,
|
|
767
|
+
[c.generation]: yield* initialGeneration(id),
|
|
768
|
+
[c.cutoff]: mapping.order.encode(0),
|
|
769
|
+
[c.state]: "Open",
|
|
770
|
+
[c.version]: owner.marker,
|
|
771
|
+
},
|
|
772
|
+
where,
|
|
773
|
+
true,
|
|
774
|
+
);
|
|
775
|
+
|
|
776
|
+
read.rows = inserted.rows;
|
|
777
|
+
}
|
|
778
|
+
const row = read.rows[0];
|
|
779
|
+
|
|
780
|
+
if (row === undefined)
|
|
781
|
+
return {
|
|
782
|
+
id,
|
|
783
|
+
row: undefined,
|
|
784
|
+
generation: yield* initialGeneration(id),
|
|
785
|
+
cutoff: 0,
|
|
786
|
+
blocked: false,
|
|
787
|
+
};
|
|
788
|
+
invariant(
|
|
789
|
+
row[c.clientKey] === clientId &&
|
|
790
|
+
row[c.identityKey] === identityId &&
|
|
791
|
+
["Open", "Blocked"].includes(row[c.state]),
|
|
792
|
+
);
|
|
793
|
+
|
|
794
|
+
const generation = snapshotOAuthSync(
|
|
795
|
+
M.OAuthConnectedTarget.fields.cohortGeneration,
|
|
796
|
+
row[c.generation],
|
|
797
|
+
);
|
|
798
|
+
|
|
799
|
+
return {
|
|
800
|
+
id,
|
|
801
|
+
row,
|
|
802
|
+
generation,
|
|
803
|
+
cutoff: nativeOrder(mapping, row[c.cutoff]),
|
|
804
|
+
blocked: row[c.state] === "Blocked",
|
|
805
|
+
};
|
|
806
|
+
});
|
|
807
|
+
|
|
808
|
+
const readGrant = Effect.fn("oauthConnected.readGrant")(function* (
|
|
809
|
+
mapping: Authority,
|
|
810
|
+
moduleId: string,
|
|
811
|
+
grantId: string,
|
|
812
|
+
lock = true,
|
|
813
|
+
) {
|
|
814
|
+
const owner = yield* CurrentOAuthTransaction;
|
|
815
|
+
const g = mapping.grant;
|
|
816
|
+
|
|
817
|
+
const read = yield* owner.read(
|
|
818
|
+
g.table,
|
|
819
|
+
equal(g.table, { [g.moduleId]: moduleId, [g.grantId]: grantId }),
|
|
820
|
+
{ limit: 1, lock, admissionOnly: lock, observe: false },
|
|
821
|
+
);
|
|
822
|
+
|
|
823
|
+
const row = read.rows[0];
|
|
824
|
+
|
|
825
|
+
if (row === undefined) return undefined;
|
|
826
|
+
if (lock) owner.observations.push(read);
|
|
827
|
+
const context = tokenContextStorage.decode(row[g.context]);
|
|
828
|
+
const native = yield* mapping.subjectId.toNative(context.subjectId);
|
|
829
|
+
|
|
830
|
+
invariant(
|
|
831
|
+
context.moduleId === moduleId &&
|
|
832
|
+
context.grantId === grantId &&
|
|
833
|
+
mapping.subjectId.equals(native, row[g.subjectId]) &&
|
|
834
|
+
row[g.identityKey] === (yield* oauthIdentityKey(context.identity)) &&
|
|
835
|
+
row[g.clientKey] === (yield* clientKey(context.configuration)) &&
|
|
836
|
+
row[g.cohortKey] === (yield* cohortKey(row[g.clientKey], row[g.identityKey])) &&
|
|
837
|
+
row[g.profileKey] === context.configuration.profile.key &&
|
|
838
|
+
row[g.grantVersion] === context.grantVersion &&
|
|
839
|
+
row[g.tokenVersion] === context.tokenVersion &&
|
|
840
|
+
row[g.cohortGeneration] === context.cohortGeneration,
|
|
841
|
+
);
|
|
842
|
+
|
|
843
|
+
return {
|
|
844
|
+
row,
|
|
845
|
+
context,
|
|
846
|
+
native,
|
|
847
|
+
sealed: row[g.sealed] === null ? undefined : sealedStorage.decode(row[g.sealed]),
|
|
848
|
+
};
|
|
849
|
+
});
|
|
850
|
+
|
|
851
|
+
const unknownWork = (mapping: Authority, clientId: string | SQL, cutoff?: number) => {
|
|
852
|
+
const f = mapping.flow;
|
|
853
|
+
|
|
854
|
+
return both(
|
|
855
|
+
eq(col(f.table, f.clientKey), clientId),
|
|
856
|
+
eq(col(f.table, f.work), "Unresolved"),
|
|
857
|
+
isNull(col(f.table, f.cohortKey)),
|
|
858
|
+
cutoff === undefined
|
|
859
|
+
? undefined
|
|
860
|
+
: sql`${col(f.table, f.claimOrder)} <= ${mapping.order.encode(cutoff)}`,
|
|
861
|
+
);
|
|
862
|
+
};
|
|
863
|
+
|
|
864
|
+
const cohortWork = (mapping: Authority, id: string | SQL) => {
|
|
865
|
+
const f = mapping.flow,
|
|
866
|
+
g = mapping.grant;
|
|
867
|
+
|
|
868
|
+
return both(
|
|
869
|
+
sql`not exists(select 1 from ${f.table} where ${and(eq(col(f.table, f.cohortKey), id), eq(col(f.table, f.work), "Unresolved"))})`,
|
|
870
|
+
sql`not exists(select 1 from ${g.table} where ${and(eq(col(g.table, g.cohortKey), id), eq(col(g.table, g.refreshWork), "Unresolved"))})`,
|
|
871
|
+
);
|
|
872
|
+
};
|
|
873
|
+
|
|
874
|
+
return {
|
|
875
|
+
contextStorage,
|
|
876
|
+
flowStorage,
|
|
877
|
+
claimStorage,
|
|
878
|
+
tokenContextStorage,
|
|
879
|
+
sealedStorage,
|
|
880
|
+
grantStorage,
|
|
881
|
+
admissionStorage,
|
|
882
|
+
commandStorage,
|
|
883
|
+
disconnectStorage,
|
|
884
|
+
refreshStorage,
|
|
885
|
+
jobStorage,
|
|
886
|
+
summaryStorage,
|
|
887
|
+
disconnectedStorage,
|
|
888
|
+
orderNumber,
|
|
889
|
+
nativeOrder,
|
|
890
|
+
clientKey,
|
|
891
|
+
cohortKey,
|
|
892
|
+
initialGeneration,
|
|
893
|
+
configuration,
|
|
894
|
+
retainedUntil,
|
|
895
|
+
rowCount,
|
|
896
|
+
countCondition,
|
|
897
|
+
current,
|
|
898
|
+
nativeCopy,
|
|
899
|
+
jobTable,
|
|
900
|
+
policy,
|
|
901
|
+
action,
|
|
902
|
+
useAuthority,
|
|
903
|
+
scopeKey,
|
|
904
|
+
scope,
|
|
905
|
+
touchScope,
|
|
906
|
+
readScope,
|
|
907
|
+
lockClientById,
|
|
908
|
+
heldScope,
|
|
909
|
+
prelockClients,
|
|
910
|
+
client,
|
|
911
|
+
nextOrder,
|
|
912
|
+
cohort,
|
|
913
|
+
readGrant,
|
|
914
|
+
unknownWork,
|
|
915
|
+
cohortWork,
|
|
916
|
+
};
|
|
917
|
+
});
|
|
918
|
+
|
|
919
|
+
export class OAuthConnectedState extends Context.Service<
|
|
920
|
+
OAuthConnectedState,
|
|
921
|
+
Effect.Success<typeof make>
|
|
922
|
+
>()("effect-auth/persistence/OAuthConnectedState") {
|
|
923
|
+
static readonly layer = Layer.effect(OAuthConnectedState, make);
|
|
924
|
+
}
|