@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,472 @@
|
|
|
1
|
+
import * as M from "@yielded/auth/OAuth";
|
|
2
|
+
import { snapshotOAuthSync } from "@yielded/auth/OAuth";
|
|
3
|
+
import { Effect, Schema, Context, Layer } from "effect";
|
|
4
|
+
|
|
5
|
+
import { OAuthConnectedCustody } from "./connected-custody";
|
|
6
|
+
import { OAuthConnectedFlow } from "./connected-flow";
|
|
7
|
+
import type * as FTypes from "./connected-flow";
|
|
8
|
+
import { OAuthConnectedSettlement } from "./connected-settlement";
|
|
9
|
+
import { OAuthConnectedState } from "./connected-state";
|
|
10
|
+
import type * as STypes from "./connected-state";
|
|
11
|
+
import { OAuthFlow } from "./flow";
|
|
12
|
+
import { CurrentOAuthTransaction } from "./owner";
|
|
13
|
+
import { OAuthQueryCompiler } from "./query-compiler";
|
|
14
|
+
import {
|
|
15
|
+
digest,
|
|
16
|
+
invariant,
|
|
17
|
+
oauthIdentityKey,
|
|
18
|
+
sameIdentity,
|
|
19
|
+
sameRevision,
|
|
20
|
+
storage,
|
|
21
|
+
unavailable,
|
|
22
|
+
} from "./state";
|
|
23
|
+
|
|
24
|
+
const make = Effect.gen(function* () {
|
|
25
|
+
const operations = yield* OAuthQueryCompiler;
|
|
26
|
+
const C = yield* OAuthConnectedCustody;
|
|
27
|
+
const F = yield* OAuthConnectedFlow;
|
|
28
|
+
const connectedSettlement = yield* OAuthConnectedSettlement;
|
|
29
|
+
const S = yield* OAuthConnectedState;
|
|
30
|
+
const flow = yield* OAuthFlow;
|
|
31
|
+
|
|
32
|
+
const { sql } = operations;
|
|
33
|
+
const { activateGrant, validMetadata } = connectedSettlement;
|
|
34
|
+
const { exactClaim } = flow;
|
|
35
|
+
const { both, equal } = operations;
|
|
36
|
+
|
|
37
|
+
const reservationStorage = storage(M.OAuthSignInAccessClaim);
|
|
38
|
+
|
|
39
|
+
const configurationStorage = storage(M.OAuthConnectedConfiguration);
|
|
40
|
+
|
|
41
|
+
/** Reservation rows share the connected-work ledger, but have no browser
|
|
42
|
+
* snapshot or subject. They can never enter the authenticated connect flow. */
|
|
43
|
+
const claimSignIn = Effect.fn("oauthConnected.claimSignIn")(function* (
|
|
44
|
+
mapping: STypes.Mapping,
|
|
45
|
+
input: FTypes.Input<"claimSignIn">,
|
|
46
|
+
) {
|
|
47
|
+
const owner = yield* CurrentOAuthTransaction;
|
|
48
|
+
const signIn = mapping.signIn;
|
|
49
|
+
const encode = mapping.flow.encodeSignIn;
|
|
50
|
+
|
|
51
|
+
if (signIn === undefined || encode === undefined) return yield* unavailable();
|
|
52
|
+
const { claim, configuration } = input;
|
|
53
|
+
const c = claim.flow.context;
|
|
54
|
+
|
|
55
|
+
invariant(
|
|
56
|
+
c.access !== undefined &&
|
|
57
|
+
configurationStorage.encode(configuration) ===
|
|
58
|
+
configurationStorage.encode({ ...c, profile: c.access }),
|
|
59
|
+
);
|
|
60
|
+
if (configuration.profile.revocation === "cohort")
|
|
61
|
+
invariant(mapping.revocation.mode === "cohort");
|
|
62
|
+
const client = yield* S.client(mapping, configuration, true);
|
|
63
|
+
|
|
64
|
+
invariant(client !== undefined);
|
|
65
|
+
const exact = yield* exactClaim({ ...mapping, flow: signIn.flow }, claim, "sign-in");
|
|
66
|
+
|
|
67
|
+
if (exact === undefined) return yield* unavailable();
|
|
68
|
+
const f = mapping.flow;
|
|
69
|
+
const key = { [f.moduleId]: c.moduleId, [f.flowId]: c.flowId };
|
|
70
|
+
const prior = yield* owner.read(f.table, equal(f.table, key), { limit: 1 });
|
|
71
|
+
|
|
72
|
+
const command = yield* owner.read(
|
|
73
|
+
f.table,
|
|
74
|
+
equal(f.table, { [f.moduleId]: c.moduleId, [f.commandId]: c.commandId }),
|
|
75
|
+
{ limit: 1 },
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
if (prior.rows.length || command.rows.length) return yield* unavailable();
|
|
79
|
+
yield* S.touchScope(mapping, client.scope);
|
|
80
|
+
const order = yield* S.nextOrder(mapping, client);
|
|
81
|
+
|
|
82
|
+
const reservation = snapshotOAuthSync(M.OAuthSignInAccessClaim, {
|
|
83
|
+
claim,
|
|
84
|
+
configuration,
|
|
85
|
+
order: String(order),
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
const inserted = yield* owner.insert(
|
|
89
|
+
f.table,
|
|
90
|
+
{
|
|
91
|
+
...encode(reservation),
|
|
92
|
+
...key,
|
|
93
|
+
[f.commandId]: c.commandId,
|
|
94
|
+
[f.subjectId]: null,
|
|
95
|
+
[f.clientKey]: client.id,
|
|
96
|
+
[f.cohortKey]: null,
|
|
97
|
+
[f.state]: "Claimed",
|
|
98
|
+
[f.version]: owner.marker,
|
|
99
|
+
[f.stateDigest]: c.stateDigest,
|
|
100
|
+
[f.snapshot]: null,
|
|
101
|
+
[f.claimId]: claim.claimId,
|
|
102
|
+
[f.claimDigest]: yield* digest(reservationStorage.encode(reservation)),
|
|
103
|
+
[f.claimOrder]: mapping.order.encode(order),
|
|
104
|
+
[f.claimedAt]: mapping.clock.encodeInstant(claim.claimedAtMillis),
|
|
105
|
+
[f.claimExpiresAt]: mapping.clock.encodeInstant(claim.claimExpiresAtMillis),
|
|
106
|
+
[f.expiresAt]: mapping.clock.encodeInstant(c.expiresAtMillis),
|
|
107
|
+
[f.retentionUntil]: mapping.clock.encodeInstant(claim.flow.retentionUntilMillis),
|
|
108
|
+
[f.work]: "Unresolved",
|
|
109
|
+
[f.custody]: null,
|
|
110
|
+
},
|
|
111
|
+
key,
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
prior.rows = inserted.rows;
|
|
115
|
+
command.rows = inserted.rows;
|
|
116
|
+
|
|
117
|
+
return reservation;
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
const exact = Effect.fn("oauthConnected.exactSignIn")(function* (
|
|
121
|
+
mapping: STypes.Mapping,
|
|
122
|
+
reservation: M.OAuthSignInAccessClaim,
|
|
123
|
+
) {
|
|
124
|
+
const owner = yield* CurrentOAuthTransaction;
|
|
125
|
+
const f = mapping.flow;
|
|
126
|
+
const c = reservation.claim.flow.context;
|
|
127
|
+
const key = { [f.moduleId]: c.moduleId, [f.flowId]: c.flowId };
|
|
128
|
+
const read = yield* owner.read(f.table, equal(f.table, key), { limit: 1 });
|
|
129
|
+
const row = read.rows[0];
|
|
130
|
+
|
|
131
|
+
if (
|
|
132
|
+
row === undefined ||
|
|
133
|
+
row[f.work] !== "Unresolved" ||
|
|
134
|
+
row[f.claimId] !== reservation.claim.claimId ||
|
|
135
|
+
row[f.claimDigest] !== (yield* digest(reservationStorage.encode(reservation)))
|
|
136
|
+
)
|
|
137
|
+
return undefined;
|
|
138
|
+
invariant(
|
|
139
|
+
row[f.subjectId] === null &&
|
|
140
|
+
row[f.commandId] === c.commandId &&
|
|
141
|
+
row[f.clientKey] === (yield* S.clientKey(reservation.configuration)) &&
|
|
142
|
+
S.nativeOrder(mapping, row[f.claimOrder]) === S.orderNumber(reservation.order),
|
|
143
|
+
);
|
|
144
|
+
const now = yield* owner.now(mapping.clock);
|
|
145
|
+
|
|
146
|
+
invariant(now >= reservation.claim.claimedAtMillis);
|
|
147
|
+
|
|
148
|
+
return {
|
|
149
|
+
key,
|
|
150
|
+
row,
|
|
151
|
+
now,
|
|
152
|
+
active: row[f.state] === "Claimed" && now < reservation.claim.claimExpiresAtMillis,
|
|
153
|
+
};
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
const credential = Effect.fn("oauthConnected.signInCredential")(function* (
|
|
157
|
+
mapping: STypes.Mapping,
|
|
158
|
+
reservation: M.OAuthSignInAccessClaim,
|
|
159
|
+
input: M.OAuthCredentialSnapshot,
|
|
160
|
+
) {
|
|
161
|
+
const owner = yield* CurrentOAuthTransaction;
|
|
162
|
+
const signIn = mapping.signIn;
|
|
163
|
+
|
|
164
|
+
if (signIn === undefined) return undefined;
|
|
165
|
+
const context = reservation.claim.flow.context;
|
|
166
|
+
|
|
167
|
+
if (
|
|
168
|
+
input.moduleId !== context.moduleId ||
|
|
169
|
+
input.identity.provider !== context.provider ||
|
|
170
|
+
input.identity.issuer !== context.issuer
|
|
171
|
+
)
|
|
172
|
+
return undefined;
|
|
173
|
+
const found = yield* S.current(mapping, input.revision.subjectId);
|
|
174
|
+
|
|
175
|
+
if (found === undefined || !sameRevision(found.revision, input.revision)) return undefined;
|
|
176
|
+
const c = signIn.credential;
|
|
177
|
+
|
|
178
|
+
const loginWhere = both(
|
|
179
|
+
equal(c.table, {
|
|
180
|
+
[c.moduleId]: context.moduleId,
|
|
181
|
+
[c.credentialId]: input.credentialId,
|
|
182
|
+
[c.subjectId]: found.nativeId,
|
|
183
|
+
[c.identityKey]: yield* oauthIdentityKey(input.identity),
|
|
184
|
+
[c.credentialRevision]: input.credentialRevision,
|
|
185
|
+
}),
|
|
186
|
+
c.activeCondition,
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
const login = yield* owner.read(c.table, loginWhere, { limit: 1 });
|
|
190
|
+
|
|
191
|
+
if (login.rows.length !== 1 || !c.isActiveStatus(login.rows[0]![c.status])) return undefined;
|
|
192
|
+
const f = signIn.flow;
|
|
193
|
+
|
|
194
|
+
const verifiedWhere = equal(f.table, {
|
|
195
|
+
[f.moduleId]: context.moduleId,
|
|
196
|
+
[f.flowId]: context.flowId,
|
|
197
|
+
[f.state]: "Verified",
|
|
198
|
+
[f.claimId]: reservation.claim.claimId,
|
|
199
|
+
[f.stateDigest]: context.stateDigest,
|
|
200
|
+
[f.claimedAt]: mapping.clock.encodeInstant(reservation.claim.claimedAtMillis),
|
|
201
|
+
[f.claimExpiresAt]: mapping.clock.encodeInstant(reservation.claim.claimExpiresAtMillis),
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
const verified = yield* owner.read(f.table, verifiedWhere, { limit: 1 });
|
|
205
|
+
|
|
206
|
+
if (verified.rows.length !== 1) return undefined;
|
|
207
|
+
owner.postconditions.push(
|
|
208
|
+
sql`exists(select 1 from ${c.table} where ${loginWhere})`,
|
|
209
|
+
sql`exists(select 1 from ${f.table} where ${verifiedWhere})`,
|
|
210
|
+
);
|
|
211
|
+
if (
|
|
212
|
+
!(yield* S.policy(mapping, {
|
|
213
|
+
kind: "sign-in",
|
|
214
|
+
subjectId: found.nativeId,
|
|
215
|
+
revision: found.revision,
|
|
216
|
+
credential: snapshotOAuthSync(M.OAuthCredentialSnapshot, input),
|
|
217
|
+
configuration: reservation.configuration,
|
|
218
|
+
}))
|
|
219
|
+
)
|
|
220
|
+
return undefined;
|
|
221
|
+
|
|
222
|
+
return found;
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
const slot = Effect.fn("oauthConnected.signInSlot")(function* (
|
|
226
|
+
mapping: STypes.Mapping,
|
|
227
|
+
moduleId: string,
|
|
228
|
+
native: unknown,
|
|
229
|
+
identityKey: string,
|
|
230
|
+
profileKey: string,
|
|
231
|
+
) {
|
|
232
|
+
const owner = yield* CurrentOAuthTransaction;
|
|
233
|
+
const g = mapping.grant;
|
|
234
|
+
|
|
235
|
+
const rows = yield* owner.read(
|
|
236
|
+
g.table,
|
|
237
|
+
equal(g.table, {
|
|
238
|
+
[g.moduleId]: moduleId,
|
|
239
|
+
[g.subjectId]: native,
|
|
240
|
+
[g.profileKey]: profileKey,
|
|
241
|
+
[g.activeIdentityKey]: identityKey,
|
|
242
|
+
}),
|
|
243
|
+
{ limit: 1, admissionOnly: true },
|
|
244
|
+
);
|
|
245
|
+
|
|
246
|
+
const row = rows.rows[0];
|
|
247
|
+
|
|
248
|
+
if (row === undefined) return undefined;
|
|
249
|
+
|
|
250
|
+
return yield* S.readGrant(
|
|
251
|
+
mapping,
|
|
252
|
+
moduleId,
|
|
253
|
+
yield* Schema.decodeUnknownEffect(M.OAuthGrantId)(row[g.grantId]).pipe(
|
|
254
|
+
Effect.mapError(unavailable),
|
|
255
|
+
),
|
|
256
|
+
);
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
/** Orders belong to one client registration. Moving a profile to another client
|
|
260
|
+
* requires a newer profile generation; retired clients cannot replace it later. */
|
|
261
|
+
const canReplace = Effect.fnUntraced(function* (
|
|
262
|
+
reservation: M.OAuthSignInAccessClaim,
|
|
263
|
+
previous: M.OAuthConnectedTokenContext,
|
|
264
|
+
) {
|
|
265
|
+
const generation = reservation.configuration.profile.generation;
|
|
266
|
+
const oldGeneration = previous.configuration.profile.generation;
|
|
267
|
+
|
|
268
|
+
return (yield* S.clientKey(reservation.configuration)) ===
|
|
269
|
+
(yield* S.clientKey(previous.configuration))
|
|
270
|
+
? generation >= oldGeneration &&
|
|
271
|
+
S.orderNumber(reservation.order) > S.orderNumber(previous.exchangeOrder)
|
|
272
|
+
: generation > oldGeneration;
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
const inspectSignIn = Effect.fn("oauthConnected.inspectSignIn")(function* (
|
|
276
|
+
mapping: STypes.Mapping,
|
|
277
|
+
input: FTypes.Input<"inspectSignIn">,
|
|
278
|
+
) {
|
|
279
|
+
const found = yield* credential(mapping, input.reservation, input.credential);
|
|
280
|
+
|
|
281
|
+
if (found === undefined) return { _tag: "Rejected" } as const;
|
|
282
|
+
const r = input.reservation;
|
|
283
|
+
const cl = yield* S.client(mapping, r.configuration, false);
|
|
284
|
+
|
|
285
|
+
if (cl === undefined) return { _tag: "Rejected" } as const;
|
|
286
|
+
const tuple = yield* F.inspectTuple(mapping, input.credential.identity);
|
|
287
|
+
const t = mapping.ownership.tuple;
|
|
288
|
+
|
|
289
|
+
if (
|
|
290
|
+
tuple.row?.[t.state] !== "Owned" ||
|
|
291
|
+
!mapping.subjectId.equals(tuple.row[t.subjectId], found.nativeId)
|
|
292
|
+
)
|
|
293
|
+
return { _tag: "Rejected" } as const;
|
|
294
|
+
const co = yield* S.cohort(mapping, cl.id, tuple.key, false);
|
|
295
|
+
|
|
296
|
+
const previous = yield* slot(
|
|
297
|
+
mapping,
|
|
298
|
+
r.claim.flow.context.moduleId,
|
|
299
|
+
found.nativeId,
|
|
300
|
+
tuple.key,
|
|
301
|
+
r.configuration.profile.key,
|
|
302
|
+
);
|
|
303
|
+
|
|
304
|
+
const held = yield* exact(mapping, r);
|
|
305
|
+
|
|
306
|
+
if (held === undefined) return { _tag: "Rejected" } as const;
|
|
307
|
+
|
|
308
|
+
const quarantine =
|
|
309
|
+
!held.active ||
|
|
310
|
+
co.blocked ||
|
|
311
|
+
S.orderNumber(r.order) <= co.cutoff ||
|
|
312
|
+
previous?.row[mapping.grant.refreshWork] === "Unresolved" ||
|
|
313
|
+
(previous !== undefined && !(yield* canReplace(r, previous.context)));
|
|
314
|
+
|
|
315
|
+
return snapshotOAuthSync(M.OAuthSignInAccessInspection, {
|
|
316
|
+
_tag: quarantine ? "Quarantine" : "Target",
|
|
317
|
+
grantId: previous?.context.grantId ?? input.grantId,
|
|
318
|
+
cohortGeneration: co.generation,
|
|
319
|
+
...(previous === undefined
|
|
320
|
+
? {}
|
|
321
|
+
: { previous: snapshotOAuthSync(M.OAuthConnectedTarget, previous.context) }),
|
|
322
|
+
});
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
const settleSignIn = Effect.fn("oauthConnected.settleSignIn")(function* (
|
|
326
|
+
mapping: STypes.Mapping,
|
|
327
|
+
input: FTypes.Input<"settleSignIn">,
|
|
328
|
+
) {
|
|
329
|
+
const owner = yield* CurrentOAuthTransaction;
|
|
330
|
+
const r = input.reservation;
|
|
331
|
+
const out = input.outcome;
|
|
332
|
+
|
|
333
|
+
const found =
|
|
334
|
+
out._tag === "Verified" ? yield* credential(mapping, r, out.credential) : undefined;
|
|
335
|
+
|
|
336
|
+
const cl = yield* S.client(mapping, r.configuration, false);
|
|
337
|
+
|
|
338
|
+
if (cl === undefined) return { _tag: "Rejected" } as const;
|
|
339
|
+
const f = mapping.flow;
|
|
340
|
+
|
|
341
|
+
if (out._tag !== "Verified") {
|
|
342
|
+
const held = yield* exact(mapping, r);
|
|
343
|
+
|
|
344
|
+
if (held === undefined) return { _tag: "Rejected" } as const;
|
|
345
|
+
// Unissued means the provider definitely returned no token. Resolve that
|
|
346
|
+
// reservation. A Rejected outcome can follow a real exchange, so it stays open.
|
|
347
|
+
const state = out._tag === "Unissued" ? "Rejected" : out._tag;
|
|
348
|
+
|
|
349
|
+
yield* owner.update(f.table, held.key, {
|
|
350
|
+
[f.state]: state,
|
|
351
|
+
[f.work]: out._tag === "Cancelled" || out._tag === "Unissued" ? "Resolved" : "Unresolved",
|
|
352
|
+
[f.version]: owner.marker,
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
return { _tag: state };
|
|
356
|
+
}
|
|
357
|
+
const token = out.grant.context;
|
|
358
|
+
|
|
359
|
+
invariant(
|
|
360
|
+
token.moduleId === r.claim.flow.context.moduleId &&
|
|
361
|
+
token.subjectId === out.credential.revision.subjectId &&
|
|
362
|
+
sameIdentity(token.identity, out.credential.identity) &&
|
|
363
|
+
configurationStorage.encode(token.configuration) ===
|
|
364
|
+
configurationStorage.encode(r.configuration),
|
|
365
|
+
);
|
|
366
|
+
const native = yield* mapping.subjectId.toNative(token.subjectId);
|
|
367
|
+
const tuple = yield* F.readTuple(mapping.ownership, token.identity);
|
|
368
|
+
const t = mapping.ownership.tuple;
|
|
369
|
+
|
|
370
|
+
const owned =
|
|
371
|
+
tuple.row[t.state] === "Owned" && mapping.subjectId.equals(tuple.row[t.subjectId], native);
|
|
372
|
+
|
|
373
|
+
const co = yield* S.cohort(mapping, cl.id, tuple.key, true);
|
|
374
|
+
const previous = yield* S.readGrant(mapping, token.moduleId, token.grantId);
|
|
375
|
+
|
|
376
|
+
const activeSlot = yield* slot(
|
|
377
|
+
mapping,
|
|
378
|
+
token.moduleId,
|
|
379
|
+
native,
|
|
380
|
+
tuple.key,
|
|
381
|
+
token.configuration.profile.key,
|
|
382
|
+
);
|
|
383
|
+
|
|
384
|
+
const held = yield* exact(mapping, r);
|
|
385
|
+
|
|
386
|
+
if (held === undefined) return { _tag: "Rejected" } as const;
|
|
387
|
+
invariant(
|
|
388
|
+
token.exchangeOrder === r.order &&
|
|
389
|
+
validMetadata(token, held.now) &&
|
|
390
|
+
token.metadata.obtainedAtMillis >= r.claim.claimedAtMillis,
|
|
391
|
+
);
|
|
392
|
+
|
|
393
|
+
const target =
|
|
394
|
+
out.previous === undefined
|
|
395
|
+
? previous === undefined
|
|
396
|
+
: previous !== undefined &&
|
|
397
|
+
F.sameTarget(out.previous, previous.context) &&
|
|
398
|
+
sameIdentity(previous.context.identity, token.identity) &&
|
|
399
|
+
previous.row[mapping.grant.refreshWork] !== "Unresolved" &&
|
|
400
|
+
(yield* canReplace(r, previous.context));
|
|
401
|
+
|
|
402
|
+
const previousSafe = yield* C.noFormerOwner(mapping, tuple.key, cl.id, native);
|
|
403
|
+
|
|
404
|
+
if (!owned || !previousSafe) {
|
|
405
|
+
yield* owner.update(f.table, held.key, { [f.state]: "Conflict", [f.version]: owner.marker });
|
|
406
|
+
|
|
407
|
+
return { _tag: "Conflict" } as const;
|
|
408
|
+
}
|
|
409
|
+
if (
|
|
410
|
+
out.quarantine ||
|
|
411
|
+
found === undefined ||
|
|
412
|
+
!held.active ||
|
|
413
|
+
!target ||
|
|
414
|
+
(activeSlot !== undefined && activeSlot.context.grantId !== token.grantId) ||
|
|
415
|
+
co.blocked ||
|
|
416
|
+
S.orderNumber(r.order) <= co.cutoff ||
|
|
417
|
+
co.generation !== token.cohortGeneration ||
|
|
418
|
+
token.metadata.useUntilMillis <= held.now ||
|
|
419
|
+
token.configuration.profile.issuance !== "active"
|
|
420
|
+
) {
|
|
421
|
+
if (token.configuration.profile.revocation === "unsupported") {
|
|
422
|
+
// No remote cleanup can run for this profile. Discard the losing material;
|
|
423
|
+
// it must neither replace nor revoke the currently installed grant.
|
|
424
|
+
yield* owner.update(f.table, held.key, {
|
|
425
|
+
[f.state]: "Rejected",
|
|
426
|
+
[f.work]: "Resolved",
|
|
427
|
+
[f.subjectId]: native,
|
|
428
|
+
[f.cohortKey]: co.id,
|
|
429
|
+
[f.custody]: null,
|
|
430
|
+
[f.version]: owner.marker,
|
|
431
|
+
});
|
|
432
|
+
} else {
|
|
433
|
+
yield* C.fence(mapping, cl, co);
|
|
434
|
+
if (out.cleanup === undefined) return yield* unavailable();
|
|
435
|
+
yield* C.storeJob(mapping, out.cleanup, token, native, held.now);
|
|
436
|
+
yield* owner.update(f.table, held.key, {
|
|
437
|
+
[f.state]: "Quarantined",
|
|
438
|
+
[f.work]: "Resolved",
|
|
439
|
+
[f.subjectId]: native,
|
|
440
|
+
[f.cohortKey]: co.id,
|
|
441
|
+
[f.custody]: null,
|
|
442
|
+
[f.version]: owner.marker,
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
return { _tag: "Rejected" } as const;
|
|
447
|
+
}
|
|
448
|
+
yield* activateGrant(mapping, out.grant, native, held.now, previous !== undefined);
|
|
449
|
+
yield* owner.update(f.table, held.key, {
|
|
450
|
+
[f.state]: "Connected",
|
|
451
|
+
[f.work]: "Resolved",
|
|
452
|
+
[f.subjectId]: native,
|
|
453
|
+
[f.cohortKey]: co.id,
|
|
454
|
+
[f.custody]: null,
|
|
455
|
+
[f.version]: owner.marker,
|
|
456
|
+
});
|
|
457
|
+
owner.postconditions.push(
|
|
458
|
+
sql`${mapping.clock.engineNowMillis} >= ${held.now} and ${mapping.clock.engineNowMillis} < ${Math.min(r.claim.claimExpiresAtMillis, token.metadata.useUntilMillis)}`,
|
|
459
|
+
);
|
|
460
|
+
|
|
461
|
+
return { _tag: "Connected", grant: out.grant } as const;
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
return { claimSignIn, inspectSignIn, settleSignIn };
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
export class OAuthConnectedSignIn extends Context.Service<
|
|
468
|
+
OAuthConnectedSignIn,
|
|
469
|
+
Effect.Success<typeof make>
|
|
470
|
+
>()("effect-auth/persistence/OAuthConnectedSignIn") {
|
|
471
|
+
static readonly layer = Layer.effect(OAuthConnectedSignIn, make);
|
|
472
|
+
}
|