@yielded/auth-persistence-drizzle 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.
Files changed (200) hide show
  1. package/README.md +3 -0
  2. package/dist/D1.d.mts +11 -11
  3. package/dist/D1.mjs +2 -1
  4. package/dist/D1.mjs.map +1 -1
  5. package/dist/Libsql.d.mts +40 -56
  6. package/dist/Libsql.mjs +2 -1
  7. package/dist/Libsql.mjs.map +1 -1
  8. package/dist/Mysql2.d.mts +16 -17
  9. package/dist/Pglite.d.mts +18 -18
  10. package/dist/Pglite.mjs +2 -1
  11. package/dist/Pglite.mjs.map +1 -1
  12. package/dist/Postgres.d.mts +24 -24
  13. package/dist/Postgres.mjs +2 -1
  14. package/dist/Postgres.mjs.map +1 -1
  15. package/dist/SqliteBun.d.mts +39 -55
  16. package/dist/SqliteBun.mjs +2 -1
  17. package/dist/SqliteBun.mjs.map +1 -1
  18. package/dist/SqliteDo.d.mts +48 -64
  19. package/dist/SqliteDo.mjs +2 -1
  20. package/dist/SqliteDo.mjs.map +1 -1
  21. package/dist/SqliteNode.d.mts +45 -61
  22. package/dist/SqliteNode.mjs +2 -1
  23. package/dist/SqliteNode.mjs.map +1 -1
  24. package/dist/SqliteWasm.d.mts +48 -64
  25. package/dist/SqliteWasm.mjs +2 -1
  26. package/dist/SqliteWasm.mjs.map +1 -1
  27. package/dist/drizzle/crypto.mjs +1 -1
  28. package/dist/drizzle/d1-emails.d.mts +1 -1
  29. package/dist/drizzle/d1-oauth.d.mts +14 -14
  30. package/dist/drizzle/d1-passkey.d.mts +1 -1
  31. package/dist/drizzle/d1-password-prepared.d.mts +4 -4
  32. package/dist/drizzle/d1-password-prepared.mjs.map +1 -1
  33. package/dist/drizzle/d1-passwords.d.mts +18 -35
  34. package/dist/drizzle/d1-passwords.mjs +78 -290
  35. package/dist/drizzle/d1-passwords.mjs.map +1 -1
  36. package/dist/drizzle/d1-phone.d.mts +1 -1
  37. package/dist/drizzle/d1-proofs.d.mts +2 -2
  38. package/dist/drizzle/d1-sessions.d.mts +3 -3
  39. package/dist/drizzle/d1-sessions.mjs +11 -6
  40. package/dist/drizzle/d1-sessions.mjs.map +1 -1
  41. package/dist/drizzle/d1-totp.d.mts +1 -1
  42. package/dist/drizzle/mysql-password-prepared.d.mts +5 -5
  43. package/dist/drizzle/mysql-password-prepared.mjs.map +1 -1
  44. package/dist/drizzle/mysql-passwords.d.mts +16 -32
  45. package/dist/drizzle/mysql-passwords.mjs.map +1 -1
  46. package/dist/drizzle/mysql-sessions.d.mts +1 -1
  47. package/dist/drizzle/native-sql-table.mjs +135 -0
  48. package/dist/drizzle/native-sql-table.mjs.map +1 -0
  49. package/dist/drizzle/oauth-connected-model.d.mts +20 -281
  50. package/dist/drizzle/oauth-connected-model.mjs +3 -17
  51. package/dist/drizzle/oauth-connected-model.mjs.map +1 -1
  52. package/dist/drizzle/oauth-connected-reference.d.mts +1 -3
  53. package/dist/drizzle/oauth-connected-reference.mjs +4 -53
  54. package/dist/drizzle/oauth-connected-reference.mjs.map +1 -1
  55. package/dist/drizzle/oauth-connected-target.d.mts +3 -990
  56. package/dist/drizzle/oauth-connected-target.mjs +8 -92
  57. package/dist/drizzle/oauth-connected-target.mjs.map +1 -1
  58. package/dist/drizzle/oauth-kernel.mjs +11 -0
  59. package/dist/drizzle/oauth-kernel.mjs.map +1 -0
  60. package/dist/drizzle/oauth-model.d.mts +35 -438
  61. package/dist/drizzle/oauth-model.mjs +6 -44
  62. package/dist/drizzle/oauth-model.mjs.map +1 -1
  63. package/dist/drizzle/oauth-mysql.mjs +2 -2
  64. package/dist/drizzle/oauth-mysql.mjs.map +1 -1
  65. package/dist/drizzle/oauth-proxy.d.mts +10 -0
  66. package/dist/drizzle/oauth-proxy.mjs +54 -0
  67. package/dist/drizzle/oauth-proxy.mjs.map +1 -0
  68. package/dist/drizzle/oauth-target.d.mts +6 -107
  69. package/dist/drizzle/oauth-target.mjs +15 -245
  70. package/dist/drizzle/oauth-target.mjs.map +1 -1
  71. package/dist/drizzle/password-model.d.mts +5 -8
  72. package/dist/drizzle/password-prepared-model.d.mts +5 -5
  73. package/dist/drizzle/password-prepared-model.mjs.map +1 -1
  74. package/dist/drizzle/password-prepared-state.mjs +1 -1
  75. package/dist/drizzle/password-sql.mjs +2 -1
  76. package/dist/drizzle/password-sql.mjs.map +1 -1
  77. package/dist/drizzle/password-target.mjs +13 -2
  78. package/dist/drizzle/password-target.mjs.map +1 -1
  79. package/dist/drizzle/pg-password-prepared.d.mts +5 -5
  80. package/dist/drizzle/pg-password-prepared.mjs.map +1 -1
  81. package/dist/drizzle/pg-passwords.d.mts +16 -32
  82. package/dist/drizzle/pg-passwords.mjs.map +1 -1
  83. package/dist/drizzle/pg-sessions.d.mts +1 -1
  84. package/dist/drizzle/phone-target.d.mts +2 -2
  85. package/dist/drizzle/phone-target.mjs +1 -1
  86. package/dist/drizzle/query-operations.mjs +3 -1
  87. package/dist/drizzle/query-operations.mjs.map +1 -1
  88. package/dist/drizzle/session-sql.mjs +2 -1
  89. package/dist/drizzle/session-sql.mjs.map +1 -1
  90. package/dist/drizzle/sqlite-password-prepared.mjs.map +1 -1
  91. package/dist/drizzle/sqlite-passwords.mjs.map +1 -1
  92. package/dist/drizzle/storage-validation.mjs +10 -198
  93. package/dist/drizzle/storage-validation.mjs.map +1 -1
  94. package/dist/drizzle/totp-target.d.mts +3 -3
  95. package/dist/drizzle/totp-target.mjs +2 -2
  96. package/dist/drizzle/transaction-execution.d.mts +2 -2
  97. package/dist/drizzle/transaction-owner.d.mts +1 -2
  98. package/dist/index.d.mts +3 -2
  99. package/dist/internal/drizzle-postgres-oauth-proxy.d.mts +138 -0
  100. package/dist/internal/drizzle-postgres-oauth-proxy.mjs +29 -0
  101. package/dist/internal/drizzle-postgres-oauth-proxy.mjs.map +1 -0
  102. package/dist/internal/drizzle-postgres.mjs +2 -0
  103. package/dist/internal/drizzle-postgres.mjs.map +1 -1
  104. package/dist/internal/drizzle-sqlite-oauth-proxy.d.mts +146 -0
  105. package/dist/internal/drizzle-sqlite-oauth-proxy.mjs +29 -0
  106. package/dist/internal/drizzle-sqlite-oauth-proxy.mjs.map +1 -0
  107. package/dist/internal/drizzle-sqlite.mjs +2 -0
  108. package/dist/internal/drizzle-sqlite.mjs.map +1 -1
  109. package/package.json +1 -1
  110. package/src/D1.ts +2 -0
  111. package/src/Libsql.ts +2 -0
  112. package/src/Pglite.ts +2 -0
  113. package/src/Postgres.ts +2 -0
  114. package/src/SqliteBun.ts +2 -0
  115. package/src/SqliteDo.ts +2 -0
  116. package/src/SqliteNode.ts +2 -0
  117. package/src/SqliteWasm.ts +2 -0
  118. package/src/drizzle/d1-password-prepared.ts +2 -23
  119. package/src/drizzle/d1-passwords.ts +158 -780
  120. package/src/drizzle/d1-sessions.ts +21 -6
  121. package/src/drizzle/mysql-password-prepared.ts +4 -16
  122. package/src/drizzle/mysql-passwords.ts +5 -20
  123. package/src/drizzle/native-sql-table.ts +259 -0
  124. package/src/drizzle/oauth-connected-model.ts +97 -315
  125. package/src/drizzle/oauth-connected-reference.ts +13 -93
  126. package/src/drizzle/oauth-connected-target.ts +14 -271
  127. package/src/drizzle/oauth-kernel.ts +10 -0
  128. package/src/drizzle/oauth-model.ts +188 -541
  129. package/src/drizzle/oauth-mysql.ts +2 -2
  130. package/src/drizzle/oauth-proxy.ts +101 -0
  131. package/src/drizzle/oauth-target.ts +33 -737
  132. package/src/drizzle/password-model.ts +0 -33
  133. package/src/drizzle/password-prepared-model.ts +0 -18
  134. package/src/drizzle/password-sql.ts +3 -0
  135. package/src/drizzle/password-target.ts +41 -4
  136. package/src/drizzle/pg-password-prepared.ts +4 -16
  137. package/src/drizzle/pg-passwords.ts +5 -20
  138. package/src/drizzle/query-operations.ts +4 -0
  139. package/src/drizzle/session-sql.ts +2 -1
  140. package/src/drizzle/sqlite-password-prepared.ts +4 -52
  141. package/src/drizzle/sqlite-passwords.ts +7 -28
  142. package/src/drizzle/storage-validation.ts +13 -222
  143. package/src/index.ts +2 -8
  144. package/src/internal/drizzle-postgres-oauth-proxy.ts +37 -0
  145. package/src/internal/drizzle-postgres.ts +2 -0
  146. package/src/internal/drizzle-sqlite-oauth-proxy.ts +44 -0
  147. package/src/internal/drizzle-sqlite.ts +2 -0
  148. package/dist/_virtual/_rolldown/runtime.mjs +0 -13
  149. package/dist/drizzle/oauth-accounts.mjs +0 -359
  150. package/dist/drizzle/oauth-accounts.mjs.map +0 -1
  151. package/dist/drizzle/oauth-connected-access.mjs +0 -212
  152. package/dist/drizzle/oauth-connected-access.mjs.map +0 -1
  153. package/dist/drizzle/oauth-connected-collection.mjs +0 -202
  154. package/dist/drizzle/oauth-connected-collection.mjs.map +0 -1
  155. package/dist/drizzle/oauth-connected-custody.mjs +0 -147
  156. package/dist/drizzle/oauth-connected-custody.mjs.map +0 -1
  157. package/dist/drizzle/oauth-connected-flow.mjs +0 -297
  158. package/dist/drizzle/oauth-connected-flow.mjs.map +0 -1
  159. package/dist/drizzle/oauth-connected-input.mjs +0 -122
  160. package/dist/drizzle/oauth-connected-input.mjs.map +0 -1
  161. package/dist/drizzle/oauth-connected-maintenance.mjs +0 -214
  162. package/dist/drizzle/oauth-connected-maintenance.mjs.map +0 -1
  163. package/dist/drizzle/oauth-connected-management.mjs +0 -229
  164. package/dist/drizzle/oauth-connected-management.mjs.map +0 -1
  165. package/dist/drizzle/oauth-connected-settlement.mjs +0 -128
  166. package/dist/drizzle/oauth-connected-settlement.mjs.map +0 -1
  167. package/dist/drizzle/oauth-connected-sign-in.mjs +0 -265
  168. package/dist/drizzle/oauth-connected-sign-in.mjs.map +0 -1
  169. package/dist/drizzle/oauth-connected-state.d.mts +0 -2062
  170. package/dist/drizzle/oauth-connected-state.mjs +0 -430
  171. package/dist/drizzle/oauth-connected-state.mjs.map +0 -1
  172. package/dist/drizzle/oauth-flow.d.mts +0 -22
  173. package/dist/drizzle/oauth-flow.mjs +0 -253
  174. package/dist/drizzle/oauth-flow.mjs.map +0 -1
  175. package/dist/drizzle/oauth-input.mjs +0 -127
  176. package/dist/drizzle/oauth-input.mjs.map +0 -1
  177. package/dist/drizzle/oauth-owner.d.mts +0 -10
  178. package/dist/drizzle/oauth-owner.mjs +0 -10
  179. package/dist/drizzle/oauth-owner.mjs.map +0 -1
  180. package/dist/drizzle/oauth-registration.d.mts +0 -5
  181. package/dist/drizzle/oauth-registration.mjs +0 -374
  182. package/dist/drizzle/oauth-registration.mjs.map +0 -1
  183. package/dist/drizzle/oauth-state.mjs +0 -107
  184. package/dist/drizzle/oauth-state.mjs.map +0 -1
  185. package/src/drizzle/oauth-accounts.ts +0 -805
  186. package/src/drizzle/oauth-connected-access.ts +0 -361
  187. package/src/drizzle/oauth-connected-collection.ts +0 -324
  188. package/src/drizzle/oauth-connected-custody.ts +0 -279
  189. package/src/drizzle/oauth-connected-flow.ts +0 -529
  190. package/src/drizzle/oauth-connected-input.ts +0 -147
  191. package/src/drizzle/oauth-connected-maintenance.ts +0 -346
  192. package/src/drizzle/oauth-connected-management.ts +0 -365
  193. package/src/drizzle/oauth-connected-settlement.ts +0 -251
  194. package/src/drizzle/oauth-connected-sign-in.ts +0 -443
  195. package/src/drizzle/oauth-connected-state.ts +0 -872
  196. package/src/drizzle/oauth-flow.ts +0 -462
  197. package/src/drizzle/oauth-input.ts +0 -201
  198. package/src/drizzle/oauth-owner.ts +0 -29
  199. package/src/drizzle/oauth-registration.ts +0 -640
  200. package/src/drizzle/oauth-state.ts +0 -218
@@ -1,752 +1,48 @@
1
- import type { NativeDatabase } from "@yielded/auth-persistence/Adapter";
2
- import { LifecycleHooks } from "@yielded/auth/Hooks";
3
- import {
4
- OAuthAccountRevision,
5
- OAuthLinkClaimDecision,
6
- OAuthLinkDecision,
7
- OAuthLinkIssueDecision,
8
- OAuthLinkPendingFlow,
9
- OAuthUnlinkDecision,
10
- OAuthUnlinkInspection,
11
- OAuthAccountsPersistence,
12
- OAuthRegistrationIntents,
13
- OAuthSignInPersistence,
14
- type PrepareOAuthCommit,
15
- OAuthRegistrationIntent,
16
- OAuthRegistrationFingerprint,
17
- OAuthClaim,
18
- OAuthClaimDecision,
19
- OAuthCleanupInput,
20
- OAuthIssueDecision,
21
- OAuthPendingFlow,
22
- OAuthSettlementDecision,
23
- OAuthVerifiedExternalIdentity,
24
- OAuthCredentialSnapshot,
25
- snapshotOAuthSync,
26
- type OAuthUnavailable,
27
- } from "@yielded/auth/OAuth";
28
- import type { SecurityRevision } from "@yielded/auth/Sessions";
29
- /* oxlint-disable no-explicit-any -- concrete driver entry points restore table/database generics. */
30
- import { eq, sql } from "drizzle-orm";
31
- import { type Crypto, type Context, Effect, Layer } from "effect";
32
- import type { Statement } from "effect/sql/Statement";
1
+ import type { OAuthKernel } from "@yielded/auth-persistence/Adapter";
33
2
 
34
- import type { PersistenceMappingError } from "./model";
35
- import {
36
- accountCurrent,
37
- claimLink,
38
- inspectUnlink,
39
- issueLink,
40
- preflightLink,
41
- settleLink,
42
- unlink,
43
- } from "./oauth-accounts";
44
- import {
45
- claimFlow,
46
- cleanupFlows,
47
- discoverOwned,
48
- exactClaim,
49
- issueFlow,
50
- matchesAccess,
51
- readFlow,
52
- resolveOwned,
53
- terminalFlow,
54
- } from "./oauth-flow";
55
- import {
56
- accountsInputs,
57
- capturedOAuthService,
58
- registrationInputs,
59
- registrationIntentInputs,
60
- signInInputs,
61
- } from "./oauth-input";
62
- import type { OAuthRegistrationAuthority } from "./oauth-model";
63
- import { both, col, equal, CurrentOAuthTransaction, type OAuthNativeDatabase } from "./oauth-owner";
64
- import {
65
- inspectIntent,
66
- register,
67
- registrationData,
68
- registrationResources,
69
- type RegistrationCallbacks,
70
- settleRegistrationIntent,
71
- } from "./oauth-registration";
72
- import { captureOAuthMapping, invariant, nonce, unavailable } from "./oauth-state";
73
- import { validateDrizzleStorage } from "./storage-validation";
74
- import {
75
- coordinateTransactionOwner,
76
- makeTransactionExecution,
77
- sqlClientTransactionStandaloneGuard,
78
- type TransactionTargetConfiguration,
79
- type TransactionCoordinatorError,
80
- type TransactionExecution,
81
- } from "./transaction-execution";
3
+ import { oauthKernel } from "./oauth-kernel";
82
4
 
83
- export type OAuthTargetConfiguration = TransactionTargetConfiguration<OAuthUnavailable>;
84
- export type OAuthCoordinatorError<E> = TransactionCoordinatorError<E, OAuthUnavailable>;
85
- export type OAuthExecution = TransactionExecution<OAuthUnavailable, CurrentOAuthTransaction, never>;
5
+ export const sqlClientOAuthStandaloneGuard: OAuthKernel["target"]["sqlClientOAuthStandaloneGuard"] =
6
+ oauthKernel.target.sqlClientOAuthStandaloneGuard;
86
7
 
87
- export const sqlClientOAuthStandaloneGuard = (
88
- service: Parameters<typeof sqlClientTransactionStandaloneGuard>[1],
89
- ) => sqlClientTransactionStandaloneGuard(unavailable, service);
8
+ export const makeTargetOAuthSignInServices: OAuthKernel["target"]["makeTargetOAuthSignInServices"] =
9
+ oauthKernel.target.makeTargetOAuthSignInServices;
90
10
 
91
- export const makeOAuthExecution = Effect.fnUntraced(function* (
92
- configuration: OAuthTargetConfiguration,
93
- mapping: unknown,
94
- ): Effect.fn.Return<
95
- OAuthExecution,
96
- OAuthUnavailable,
97
- LifecycleHooks | NativeDatabase | Crypto.Crypto
98
- > {
99
- const hooks = yield* LifecycleHooks;
100
-
101
- yield* validateDrizzleStorage(mapping).pipe(Effect.mapError(unavailable));
102
-
103
- const execution = yield* makeTransactionExecution(
104
- CurrentOAuthTransaction,
105
- configuration,
106
- unavailable,
107
- nonce,
108
- );
109
-
110
- return {
111
- ...execution,
112
- run: (operation, mutation) =>
113
- execution.run(operation, mutation).pipe(Effect.provideService(LifecycleHooks, hooks)),
114
- };
115
- });
116
-
117
- const prepareValue = <Value, A>(value: Value, prepare: PrepareOAuthCommit<Value, A>) =>
118
- Effect.flatMap(CurrentOAuthTransaction, (owner) =>
119
- Effect.sync(() => {
120
- owner.guards.push(owner.journal.prepare(undefined));
121
-
122
- return prepare(value, owner.journal);
123
- }),
124
- );
125
-
126
- export const makeOAuthSignIn = (
127
- mapping: any,
128
- execute: OAuthExecution,
129
- ): OAuthSignInPersistence["Service"] =>
130
- capturedOAuthService<OAuthSignInPersistence["Service"]>(
131
- {
132
- issue: (original, prepare) =>
133
- execute.run(
134
- Effect.gen(function* () {
135
- const input = snapshotOAuthSync(OAuthPendingFlow, original);
136
- const value = yield* issueFlow(mapping, input, "sign-in");
137
-
138
- return yield* prepareValue(
139
- snapshotOAuthSync(OAuthIssueDecision, value as never),
140
- prepare,
141
- );
142
- }),
143
- ),
144
- claim: (input, prepare) =>
145
- execute.run(
146
- Effect.gen(function* () {
147
- const read = yield* readFlow(mapping, input, "sign-in");
148
-
149
- const claim =
150
- read.row !== undefined && read.flow !== undefined && matchesAccess(read.flow, input)
151
- ? yield* claimFlow(mapping, { row: read.row, flow: read.flow }, input.claimId)
152
- : undefined;
153
-
154
- return yield* prepareValue(
155
- snapshotOAuthSync(
156
- OAuthClaimDecision,
157
- claim === undefined
158
- ? { _tag: "Rejected" }
159
- : { _tag: "Claimed", claim: claim as typeof OAuthClaim.Type },
160
- ),
161
- prepare,
162
- );
163
- }),
164
- ),
165
- settle: (original, prepare) =>
166
- execute.run(
167
- Effect.gen(function* () {
168
- const claim = snapshotOAuthSync(OAuthClaim, original.claim);
169
- const outcome = original.outcome;
170
-
171
- const identity =
172
- outcome._tag === "Verified"
173
- ? snapshotOAuthSync(OAuthVerifiedExternalIdentity, outcome.identity).identity
174
- : undefined;
175
-
176
- if (identity !== undefined)
177
- invariant(
178
- identity.provider === claim.flow.context.provider &&
179
- identity.issuer === claim.flow.context.issuer,
180
- );
181
-
182
- // Discover/lock the existing subject before locking its claimed flow.
183
- const found =
184
- identity === undefined ? undefined : yield* discoverOwned(mapping, identity);
185
-
186
- const exact = yield* exactClaim(mapping, claim, "sign-in");
187
- let value: typeof OAuthSettlementDecision.Type = { _tag: "Rejected" };
188
-
189
- if (exact !== undefined) {
190
- const credential =
191
- identity === undefined
192
- ? undefined
193
- : resolveOwned(mapping, claim.flow.context.moduleId, identity, found);
194
-
195
- value =
196
- outcome._tag !== "Verified"
197
- ? { _tag: outcome._tag }
198
- : credential === undefined
199
- ? { _tag: "Rejected" }
200
- : { _tag: "Verified", credential };
201
- yield* terminalFlow(mapping, claim.flow, value._tag);
202
- }
203
-
204
- return yield* prepareValue(snapshotOAuthSync(OAuthSettlementDecision, value), prepare);
205
- }),
206
- ),
207
- cleanup: (input, prepare) =>
208
- execute.run(
209
- Effect.gen(function* () {
210
- const value = yield* cleanupFlows(mapping, input, "sign-in");
211
-
212
- return yield* prepareValue(value, prepare);
213
- }),
214
- ),
215
- },
216
- signInInputs,
217
- execute.active,
218
- execute.poison,
219
- );
220
-
221
- export const makeOAuthRegistrationIntents = (
222
- mapping: any,
223
- execute: OAuthExecution,
224
- ): OAuthRegistrationIntents["Service"] =>
225
- capturedOAuthService<OAuthRegistrationIntents["Service"]>(
226
- {
227
- settle: (input, prepare) =>
228
- execute.run(
229
- Effect.gen(function* () {
230
- const value = yield* settleRegistrationIntent(mapping, {
231
- claim: snapshotOAuthSync(OAuthClaim, input.claim),
232
- identity: snapshotOAuthSync(OAuthVerifiedExternalIdentity, input.identity),
233
- ...(input.intent === undefined
234
- ? {}
235
- : { intent: snapshotOAuthSync(OAuthRegistrationIntent, input.intent) }),
236
- });
237
-
238
- return yield* prepareValue(value, prepare);
239
- }),
240
- ),
241
- },
242
- registrationIntentInputs,
243
- execute.active,
244
- execute.poison,
245
- );
246
-
247
- export const makeOAuthRegistration = <Registration>(
248
- mapping: any,
249
- execute: OAuthExecution,
250
- allocated?: Awaited<ReturnType<typeof registrationResources>> extends Effect.Effect<
251
- infer A,
252
- any,
253
- any
254
- >
255
- ? A
256
- : never,
257
- ): OAuthRegistrationAuthority<Registration> =>
258
- capturedOAuthService<OAuthRegistrationAuthority<Registration>>(
259
- {
260
- read: (input) =>
261
- execute.run(
262
- Effect.map(inspectIntent(mapping, input), (result) => result?.inspection),
263
- false,
264
- ),
265
- inspect: (input) =>
266
- Effect.gen(function* () {
267
- yield* execute.admit;
268
- const intent = snapshotOAuthSync(OAuthRegistrationIntent, input.intent);
269
- const stored = mapping.application.encode(input.registration);
270
-
271
- invariant(
272
- typeof stored === "string" && new TextEncoder().encode(stored).length <= 1048576,
273
- );
274
- const detached = { intent, registration: mapping.application.decode(stored) };
275
-
276
- const checked = execute.bound
277
- ? (invariant(mapping.inspectSync !== undefined), mapping.inspectSync(detached))
278
- : yield* (mapping as RegistrationCallbacks<Registration>).inspect(detached);
279
-
280
- invariant(typeof checked.eligible === "boolean");
281
-
282
- return {
283
- fingerprint: snapshotOAuthSync(OAuthRegistrationFingerprint, checked.fingerprint),
284
- eligible: checked.eligible,
285
- };
286
- }).pipe(
287
- (effect) => (execute.bound ? execute.run(effect, false) : effect),
288
- Effect.onExit((exit) =>
289
- Effect.sync(() => {
290
- if (exit._tag === "Failure") execute.poison();
291
- }),
292
- ),
293
- Effect.mapError(unavailable),
294
- Effect.catchDefect(() => Effect.fail(unavailable())),
295
- ) as never,
296
- register: (input, prepare) =>
297
- Effect.gen(function* () {
298
- yield* execute.admit;
299
- const data = yield* registrationData(mapping, input, execute.bound);
300
- const resources = allocated ?? (yield* registrationResources(mapping));
301
-
302
- return yield* execute.run(
303
- Effect.gen(function* () {
304
- const value = yield* register(mapping, input, data, resources);
305
-
306
- return yield* prepareValue(value, prepare);
307
- }),
308
- );
309
- }).pipe(
310
- Effect.onExit((exit) =>
311
- Effect.sync(() => {
312
- if (exit._tag === "Failure") execute.poison();
313
- }),
314
- ),
315
- Effect.mapError(unavailable),
316
- Effect.catchDefect(() => Effect.fail(unavailable())),
317
- ) as never,
318
- cleanup: (original, prepare) =>
319
- execute.run(
320
- Effect.gen(function* () {
321
- const owner = yield* CurrentOAuthTransaction;
322
-
323
- const input = snapshotOAuthSync(OAuthCleanupInput, original),
324
- i = mapping.intent,
325
- r = mapping.command;
326
-
327
- const now = yield* owner.now(mapping.clock);
328
-
329
- const candidates = yield* owner.read(
330
- i.table,
331
- both(
332
- eq(col(i.table, i.moduleId), input.moduleId),
333
- sql`${col(i.table, i.state)} <> 'ProvisioningPending' and ${col(i.table, i.retentionUntil)} < ${mapping.clock.encodeInstant(now)}`,
334
- ),
335
- { limit: input.limit, observe: false, takeOnly: true },
336
- );
11
+ export const makeTargetOAuthRegistrationIntentServices: OAuthKernel["target"]["makeTargetOAuthRegistrationIntentServices"] =
12
+ oauthKernel.target.makeTargetOAuthRegistrationIntentServices;
337
13
 
338
- let removed = 0;
14
+ export const makeTargetOAuthRegistrationServices: OAuthKernel["target"]["makeTargetOAuthRegistrationServices"] =
15
+ oauthKernel.target.makeTargetOAuthRegistrationServices;
339
16
 
340
- for (const candidate of candidates.rows) {
341
- const selected = yield* owner.read(
342
- i.table,
343
- equal(i.table, {
344
- [i.moduleId]: input.moduleId,
345
- [i.reference]: candidate[i.reference],
346
- }),
347
- { limit: 1 },
348
- );
17
+ export const coordinateTargetOAuthRegistration: OAuthKernel["target"]["coordinateTargetOAuthRegistration"] =
18
+ oauthKernel.target.coordinateTargetOAuthRegistration;
349
19
 
350
- const row = selected.rows[0];
20
+ export const coordinateTargetOAuthSignIn: OAuthKernel["target"]["coordinateTargetOAuthSignIn"] =
21
+ oauthKernel.target.coordinateTargetOAuthSignIn;
351
22
 
352
- if (row === undefined || row[i.state] === "ProvisioningPending") continue;
353
- let horizon = mapping.clock.decodeInstant(row[i.retentionUntil]);
23
+ export const coordinateTargetOAuthRegistrationIntents: OAuthKernel["target"]["coordinateTargetOAuthRegistrationIntents"] =
24
+ oauthKernel.target.coordinateTargetOAuthRegistrationIntents;
354
25
 
355
- if (!Number.isSafeInteger(horizon) || horizon >= (yield* owner.now(mapping.clock)))
356
- continue;
357
- if (row[i.commandId] !== null) {
358
- const commands = yield* owner.read(
359
- r.table,
360
- equal(r.table, {
361
- [r.moduleId]: input.moduleId,
362
- [r.commandId]: row[i.commandId],
363
- }),
364
- { limit: 1 },
365
- );
26
+ export const coordinateTargetOAuthAccounts: OAuthKernel["target"]["coordinateTargetOAuthAccounts"] =
27
+ oauthKernel.target.coordinateTargetOAuthAccounts;
366
28
 
367
- const command = commands.rows[0];
29
+ export const makeTargetOAuthAccountsServices: OAuthKernel["target"]["makeTargetOAuthAccountsServices"] =
30
+ oauthKernel.target.makeTargetOAuthAccountsServices;
368
31
 
369
- if (command === undefined) continue;
370
- horizon = Math.max(horizon, mapping.clock.decodeInstant(command[r.retentionUntil]));
371
- if (!Number.isSafeInteger(horizon) || horizon >= (yield* owner.now(mapping.clock)))
372
- continue;
373
- yield* owner.remove(r.table, {
374
- [r.moduleId]: input.moduleId,
375
- [r.commandId]: row[i.commandId],
376
- });
377
- }
378
- yield* owner.remove(i.table, {
379
- [i.moduleId]: input.moduleId,
380
- [i.reference]: row[i.reference],
381
- });
382
- owner.postconditions.push(sql`${mapping.clock.engineNowMillis} > ${horizon}`);
383
- removed++;
384
- }
32
+ export const oauthAccountsPersistenceLayer: OAuthKernel["target"]["oauthAccountsPersistenceLayer"] =
33
+ oauthKernel.target.oauthAccountsPersistenceLayer;
385
34
 
386
- return yield* prepareValue(
387
- { removed, hasMore: candidates.rows.length === input.limit },
388
- prepare,
389
- );
390
- }),
391
- ),
392
- },
393
- registrationInputs(mapping.application),
394
- execute.active,
395
- execute.poison,
396
- );
35
+ export const oauthSignInPersistenceLayer: OAuthKernel["target"]["oauthSignInPersistenceLayer"] =
36
+ oauthKernel.target.oauthSignInPersistenceLayer;
397
37
 
398
- export const makeTargetOAuthSignInServices = (
399
- mapping: any,
400
- configuration: OAuthTargetConfiguration,
401
- ) => {
402
- const captured = captureOAuthMapping(mapping);
38
+ export const oauthRegistrationIntentsLayer: OAuthKernel["target"]["oauthRegistrationIntentsLayer"] =
39
+ oauthKernel.target.oauthRegistrationIntentsLayer;
403
40
 
404
- return Effect.map(makeOAuthExecution(configuration, captured), (execution) => ({
405
- oauthSignInPersistence: makeOAuthSignIn(captured, execution),
406
- }));
407
- };
41
+ export const oauthRegistrationAuthorityLayer: OAuthKernel["target"]["oauthRegistrationAuthorityLayer"] =
42
+ oauthKernel.target.oauthRegistrationAuthorityLayer;
408
43
 
409
- export const makeTargetOAuthRegistrationIntentServices = (
410
- mapping: any,
411
- configuration: OAuthTargetConfiguration,
412
- ) => {
413
- const captured = captureOAuthMapping(mapping);
414
-
415
- return Effect.map(makeOAuthExecution(configuration, captured), (execution) => ({
416
- oauthRegistrationIntents: makeOAuthRegistrationIntents(captured, execution),
417
- }));
418
- };
419
-
420
- export const makeTargetOAuthRegistrationServices = <Registration>(
421
- mapping: any,
422
- configuration: OAuthTargetConfiguration,
423
- ) => {
424
- const captured = captureOAuthMapping(mapping);
425
-
426
- return Effect.map(makeOAuthExecution(configuration, captured), (execution) => ({
427
- registrationAuthority: makeOAuthRegistration<Registration>(captured, execution),
428
- }));
429
- };
430
-
431
- export const coordinateOAuthOwner = <Services, Transaction, A, E, R>(
432
- database: OAuthNativeDatabase,
433
- mapping: any,
434
- configuration: OAuthTargetConfiguration,
435
- allocate: (mapping: any) => Effect.Effect<any, PersistenceMappingError>,
436
- services: (mapping: any, execution: OAuthExecution, resources: any) => Services,
437
- owner: (
438
- transaction: Transaction,
439
- services: Services,
440
- append: (statement: Statement<any>) => void,
441
- ) => Effect.Effect<A, E, R>,
442
- ): Effect.Effect<
443
- A,
444
- OAuthCoordinatorError<E>,
445
- R | Crypto.Crypto | LifecycleHooks | NativeDatabase
446
- > => {
447
- const captured = captureOAuthMapping(mapping);
448
-
449
- return Effect.flatMap(LifecycleHooks, (hooks) =>
450
- coordinateTransactionOwner(
451
- database,
452
- CurrentOAuthTransaction,
453
- configuration,
454
- validateDrizzleStorage(captured).pipe(
455
- Effect.andThen(Effect.suspend(() => allocate(captured))),
456
- ),
457
- unavailable,
458
- nonce,
459
- (execution, resources) =>
460
- services(
461
- captured,
462
- {
463
- ...execution,
464
- run: (operation, mutation) =>
465
- execution.run(operation, mutation).pipe(Effect.provideService(LifecycleHooks, hooks)),
466
- },
467
- resources,
468
- ),
469
- owner,
470
- ).pipe(Effect.provideService(LifecycleHooks, hooks)),
471
- );
472
- };
473
-
474
- const accountResources = (mapping: any) =>
475
- Effect.gen(function* () {
476
- return {
477
- credentialId: snapshotOAuthSync(
478
- OAuthCredentialSnapshot.fields.credentialId,
479
- yield* mapping.allocateCredentialId as Effect.Effect<string, PersistenceMappingError>,
480
- ),
481
- revision: snapshotOAuthSync(
482
- OAuthCredentialSnapshot.fields.credentialRevision,
483
- yield* mapping.allocateRevision as Effect.Effect<SecurityRevision, PersistenceMappingError>,
484
- ),
485
- };
486
- });
487
-
488
- export const coordinateTargetOAuthRegistration = <Registration, Transaction, A, E, R>(
489
- database: any,
490
- mapping: any,
491
- configuration: OAuthTargetConfiguration,
492
- owner: (
493
- transaction: Transaction,
494
- services: { readonly registrationAuthority: OAuthRegistrationAuthority<Registration> },
495
- append: (statement: Statement<any>) => void,
496
- ) => Effect.Effect<A, E, R>,
497
- ) =>
498
- coordinateOAuthOwner(
499
- database,
500
- mapping,
501
- configuration,
502
- registrationResources,
503
- (captured, execute, resources) => ({
504
- registrationAuthority: makeOAuthRegistration<Registration>(captured, execute, resources),
505
- }),
506
- owner,
507
- );
508
-
509
- export const coordinateTargetOAuthSignIn = <Transaction, A, E, R>(
510
- database: any,
511
- mapping: any,
512
- configuration: OAuthTargetConfiguration,
513
- owner: (
514
- transaction: Transaction,
515
- services: { readonly oauthSignInPersistence: OAuthSignInPersistence["Service"] },
516
- append: (statement: Statement<any>) => void,
517
- ) => Effect.Effect<A, E, R>,
518
- ) =>
519
- coordinateOAuthOwner(
520
- database,
521
- mapping,
522
- configuration,
523
- () => Effect.void,
524
- (captured, execute) => ({ oauthSignInPersistence: makeOAuthSignIn(captured, execute) }),
525
- owner,
526
- );
527
-
528
- export const coordinateTargetOAuthRegistrationIntents = <Transaction, A, E, R>(
529
- database: any,
530
- mapping: any,
531
- configuration: OAuthTargetConfiguration,
532
- owner: (
533
- transaction: Transaction,
534
- services: { readonly oauthRegistrationIntents: OAuthRegistrationIntents["Service"] },
535
- append: (statement: Statement<any>) => void,
536
- ) => Effect.Effect<A, E, R>,
537
- ) =>
538
- coordinateOAuthOwner(
539
- database,
540
- mapping,
541
- configuration,
542
- () => Effect.void,
543
- (captured, execute) => ({
544
- oauthRegistrationIntents: makeOAuthRegistrationIntents(captured, execute),
545
- }),
546
- owner,
547
- );
548
-
549
- export const coordinateTargetOAuthAccounts = <Transaction, A, E, R>(
550
- database: any,
551
- mapping: any,
552
- configuration: OAuthTargetConfiguration,
553
- owner: (
554
- transaction: Transaction,
555
- services: { readonly oauthAccountsPersistence: OAuthAccountsPersistence["Service"] },
556
- append: (statement: Statement<any>) => void,
557
- ) => Effect.Effect<A, E, R>,
558
- ) =>
559
- coordinateOAuthOwner(
560
- database,
561
- mapping,
562
- configuration,
563
- accountResources,
564
- (captured, execute, resources) => ({
565
- oauthAccountsPersistence: makeOAuthAccounts(captured, execute, resources),
566
- }),
567
- owner,
568
- );
569
-
570
- export const makeOAuthAccounts = (
571
- mapping: any,
572
- execute: OAuthExecution,
573
- resources?: any,
574
- ): OAuthAccountsPersistence["Service"] =>
575
- capturedOAuthService<OAuthAccountsPersistence["Service"]>(
576
- {
577
- capture: (input) =>
578
- execute.run(
579
- Effect.map(accountCurrent(mapping, input.subjectId), (current) =>
580
- current === undefined
581
- ? undefined
582
- : snapshotOAuthSync(OAuthAccountRevision, current.revision),
583
- ),
584
- false,
585
- ),
586
- issue: (input, prepare) =>
587
- execute.run(
588
- Effect.gen(function* () {
589
- const value = yield* issueLink(mapping, input);
590
-
591
- return yield* prepareValue(
592
- snapshotOAuthSync(OAuthLinkIssueDecision, value as never),
593
- prepare,
594
- );
595
- }),
596
- ),
597
- preflight: (input) =>
598
- execute.run(
599
- Effect.map(preflightLink(mapping, input), (current) =>
600
- current === undefined
601
- ? undefined
602
- : snapshotOAuthSync(OAuthLinkPendingFlow, current.flow),
603
- ),
604
- false,
605
- ),
606
- claim: (input, prepare) =>
607
- execute.run(
608
- Effect.gen(function* () {
609
- const value = yield* claimLink(mapping, input);
610
-
611
- return yield* prepareValue(
612
- snapshotOAuthSync(OAuthLinkClaimDecision, value as never),
613
- prepare,
614
- );
615
- }),
616
- ),
617
- settle: (input, prepare) =>
618
- Effect.gen(function* () {
619
- yield* execute.admit;
620
- const allocated = resources ?? (yield* accountResources(mapping));
621
-
622
- return yield* execute.run(
623
- Effect.gen(function* () {
624
- const value = yield* settleLink(mapping, input, allocated as never);
625
-
626
- return yield* prepareValue(snapshotOAuthSync(OAuthLinkDecision, value), prepare);
627
- }),
628
- );
629
- }).pipe(
630
- Effect.mapError(unavailable),
631
- Effect.catchDefect(() => Effect.fail(unavailable())),
632
- ) as never,
633
- inspectUnlink: (input) =>
634
- execute.run(
635
- Effect.map(inspectUnlink(mapping, input), (value) =>
636
- snapshotOAuthSync(OAuthUnlinkInspection, value),
637
- ),
638
- false,
639
- ),
640
- unlink: (input, prepare) =>
641
- execute.run(
642
- Effect.gen(function* () {
643
- const value = yield* unlink(mapping, input);
644
-
645
- return yield* prepareValue(snapshotOAuthSync(OAuthUnlinkDecision, value), prepare);
646
- }),
647
- ),
648
- cleanup: (input, prepare) =>
649
- execute.run(
650
- Effect.gen(function* () {
651
- const owner = yield* CurrentOAuthTransaction;
652
- const value = yield* cleanupFlows(mapping, input, "link");
653
- const remaining = input.limit - value.terminalized - value.removed;
654
-
655
- const now = yield* owner.now(mapping.clock),
656
- r = mapping.command;
657
-
658
- let removed = 0;
659
-
660
- if (remaining > 0) {
661
- const rows = yield* owner.read(
662
- r.table,
663
- both(
664
- eq(col(r.table, r.moduleId), input.moduleId),
665
- sql`${col(r.table, r.retentionUntil)} < ${mapping.clock.encodeInstant(now)}`,
666
- ),
667
- { limit: remaining, observe: false, takeOnly: true },
668
- );
669
-
670
- for (const row of rows.rows) {
671
- const key = { [r.moduleId]: input.moduleId, [r.commandId]: row[r.commandId] };
672
- const selected = yield* owner.read(r.table, equal(r.table, key), { limit: 1 });
673
- const current = selected.rows[0];
674
-
675
- if (current === undefined) continue;
676
- const horizon = mapping.clock.decodeInstant(current[r.retentionUntil]);
677
-
678
- if (!Number.isSafeInteger(horizon) || horizon >= (yield* owner.now(mapping.clock)))
679
- continue;
680
- yield* owner.remove(r.table, key);
681
- owner.postconditions.push(sql`${mapping.clock.engineNowMillis} > ${horizon}`);
682
- removed++;
683
- }
684
- }
685
-
686
- return yield* prepareValue(
687
- {
688
- ...value,
689
- removed: value.removed + removed,
690
- hasMore: value.hasMore || remaining === removed,
691
- },
692
- prepare,
693
- );
694
- }),
695
- ),
696
- },
697
- accountsInputs,
698
- execute.active,
699
- execute.poison,
700
- );
701
-
702
- export const makeTargetOAuthAccountsServices = (
703
- mapping: any,
704
- configuration: OAuthTargetConfiguration,
705
- ) => {
706
- const captured = captureOAuthMapping(mapping);
707
-
708
- return Effect.map(makeOAuthExecution(configuration, captured), (execution) => ({
709
- oauthAccountsPersistence: makeOAuthAccounts(captured, execution),
710
- }));
711
- };
712
-
713
- export const oauthAccountsPersistenceLayer = <E, R>(
714
- services: Effect.Effect<Effect.Success<ReturnType<typeof makeTargetOAuthAccountsServices>>, E, R>,
715
- ) =>
716
- Layer.effect(
717
- OAuthAccountsPersistence,
718
- Effect.map(services, (value) => value.oauthAccountsPersistence),
719
- );
720
-
721
- export const oauthSignInPersistenceLayer = <E, R>(
722
- services: Effect.Effect<Effect.Success<ReturnType<typeof makeTargetOAuthSignInServices>>, E, R>,
723
- ) =>
724
- Layer.effect(
725
- OAuthSignInPersistence,
726
- Effect.map(services, (value) => value.oauthSignInPersistence),
727
- );
728
-
729
- export const oauthRegistrationIntentsLayer = <E, R>(
730
- services: Effect.Effect<
731
- Effect.Success<ReturnType<typeof makeTargetOAuthRegistrationIntentServices>>,
732
- E,
733
- R
734
- >,
735
- ) =>
736
- Layer.effect(
737
- OAuthRegistrationIntents,
738
- Effect.map(services, (value) => value.oauthRegistrationIntents),
739
- );
740
-
741
- export const oauthRegistrationAuthorityLayer = <Id, Registration, E, R>(
742
- tag: Context.Key<Id, OAuthRegistrationAuthority<Registration>>,
743
- services: Effect.Effect<
744
- { readonly registrationAuthority: OAuthRegistrationAuthority<Registration> },
745
- E,
746
- R
747
- >,
748
- ) =>
749
- Layer.effect(
750
- tag,
751
- Effect.map(services, (value) => value.registrationAuthority),
752
- );
44
+ export type {
45
+ OAuthTargetConfiguration,
46
+ OAuthCoordinatorError,
47
+ OAuthExecution,
48
+ } from "@yielded/auth-persistence/Adapter";