@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.
Files changed (182) hide show
  1. package/README.md +11 -0
  2. package/dist/Adapter.d.mts +12 -6
  3. package/dist/Adapter.mjs +8 -3
  4. package/dist/OAuthPersistence.d.mts +87 -0
  5. package/dist/OAuthPersistence.mjs +125 -0
  6. package/dist/OAuthPersistence.mjs.map +1 -0
  7. package/dist/_virtual/_rolldown/runtime.mjs +13 -0
  8. package/dist/index.d.mts +3 -1
  9. package/dist/index.mjs +3 -1
  10. package/dist/internal/configuration.d.mts +1 -1
  11. package/dist/internal/configuration.mjs.map +1 -1
  12. package/dist/internal/email-kernel.d.mts +1 -1
  13. package/dist/internal/email-kernel.mjs +1 -1
  14. package/dist/internal/email-kernel.mjs.map +1 -1
  15. package/dist/internal/models/oauth-connected-model.d.mts +294 -0
  16. package/dist/internal/models/oauth-connected-model.mjs +22 -0
  17. package/dist/internal/models/oauth-connected-model.mjs.map +1 -0
  18. package/dist/internal/models/oauth-model.d.mts +467 -0
  19. package/dist/internal/models/oauth-model.mjs +49 -0
  20. package/dist/internal/models/oauth-model.mjs.map +1 -0
  21. package/dist/internal/models/password-model.d.mts +6 -48
  22. package/dist/internal/models/password-model.mjs +0 -1
  23. package/dist/internal/models/password-model.mjs.map +1 -1
  24. package/dist/internal/native-sql-table.d.mts +4 -0
  25. package/dist/internal/native-sql-table.mjs +7 -0
  26. package/dist/internal/native-sql-table.mjs.map +1 -1
  27. package/dist/internal/oauth/accounts.d.mts +8 -0
  28. package/dist/internal/oauth/accounts.mjs +379 -0
  29. package/dist/internal/oauth/accounts.mjs.map +1 -0
  30. package/dist/internal/oauth/connected-access.d.mts +9 -0
  31. package/dist/internal/oauth/connected-access.mjs +229 -0
  32. package/dist/internal/oauth/connected-access.mjs.map +1 -0
  33. package/dist/internal/oauth/connected-collection.d.mts +7 -0
  34. package/dist/internal/oauth/connected-collection.mjs +214 -0
  35. package/dist/internal/oauth/connected-collection.mjs.map +1 -0
  36. package/dist/internal/oauth/connected-custody.d.mts +6 -0
  37. package/dist/internal/oauth/connected-custody.mjs +165 -0
  38. package/dist/internal/oauth/connected-custody.mjs.map +1 -0
  39. package/dist/internal/oauth/connected-flow.d.mts +7 -0
  40. package/dist/internal/oauth/connected-flow.mjs +324 -0
  41. package/dist/internal/oauth/connected-flow.mjs.map +1 -0
  42. package/dist/internal/oauth/connected-input.mjs +122 -0
  43. package/dist/internal/oauth/connected-input.mjs.map +1 -0
  44. package/dist/internal/oauth/connected-keys.d.mts +2 -0
  45. package/dist/internal/oauth/connected-keys.mjs +22 -0
  46. package/dist/internal/oauth/connected-keys.mjs.map +1 -0
  47. package/dist/internal/oauth/connected-maintenance.d.mts +9 -0
  48. package/dist/internal/oauth/connected-maintenance.mjs +226 -0
  49. package/dist/internal/oauth/connected-maintenance.mjs.map +1 -0
  50. package/dist/internal/oauth/connected-management.d.mts +9 -0
  51. package/dist/internal/oauth/connected-management.mjs +243 -0
  52. package/dist/internal/oauth/connected-management.mjs.map +1 -0
  53. package/dist/internal/oauth/connected-reference.mjs +63 -0
  54. package/dist/internal/oauth/connected-reference.mjs.map +1 -0
  55. package/dist/internal/oauth/connected-settlement.d.mts +8 -0
  56. package/dist/internal/oauth/connected-settlement.mjs +141 -0
  57. package/dist/internal/oauth/connected-settlement.mjs.map +1 -0
  58. package/dist/internal/oauth/connected-sign-in.d.mts +10 -0
  59. package/dist/internal/oauth/connected-sign-in.mjs +281 -0
  60. package/dist/internal/oauth/connected-sign-in.mjs.map +1 -0
  61. package/dist/internal/oauth/connected-state.d.mts +14 -0
  62. package/dist/internal/oauth/connected-state.mjs +469 -0
  63. package/dist/internal/oauth/connected-state.mjs.map +1 -0
  64. package/dist/internal/oauth/connected-target.d.mts +21 -0
  65. package/dist/internal/oauth/connected-target.mjs +117 -0
  66. package/dist/internal/oauth/connected-target.mjs.map +1 -0
  67. package/dist/internal/oauth/flow.d.mts +5 -0
  68. package/dist/internal/oauth/flow.mjs +279 -0
  69. package/dist/internal/oauth/flow.mjs.map +1 -0
  70. package/dist/internal/oauth/input.mjs +127 -0
  71. package/dist/internal/oauth/input.mjs.map +1 -0
  72. package/dist/internal/oauth/owner.d.mts +10 -0
  73. package/dist/internal/oauth/owner.mjs +10 -0
  74. package/dist/internal/oauth/owner.mjs.map +1 -0
  75. package/dist/internal/oauth/query-compiler.d.mts +7 -0
  76. package/dist/internal/oauth/query-compiler.mjs +9 -0
  77. package/dist/internal/oauth/query-compiler.mjs.map +1 -0
  78. package/dist/internal/oauth/registration.d.mts +7 -0
  79. package/dist/internal/oauth/registration.mjs +393 -0
  80. package/dist/internal/oauth/registration.mjs.map +1 -0
  81. package/dist/internal/oauth/state.mjs +107 -0
  82. package/dist/internal/oauth/state.mjs.map +1 -0
  83. package/dist/internal/oauth/target.d.mts +31 -0
  84. package/dist/internal/oauth/target.mjs +273 -0
  85. package/dist/internal/oauth/target.mjs.map +1 -0
  86. package/dist/internal/oauth-kernel.d.mts +1423 -0
  87. package/dist/internal/oauth-kernel.mjs +187 -0
  88. package/dist/internal/oauth-kernel.mjs.map +1 -0
  89. package/dist/internal/oauth-proxy.d.mts +31 -0
  90. package/dist/internal/oauth-proxy.mjs +129 -0
  91. package/dist/internal/oauth-proxy.mjs.map +1 -0
  92. package/dist/internal/passkey/write-state.mjs +9 -1
  93. package/dist/internal/passkey/write-state.mjs.map +1 -1
  94. package/dist/internal/passkey-kernel.d.mts +9 -9
  95. package/dist/internal/{password-attempts.d.mts → password-credentials.d.mts} +3 -4
  96. package/dist/internal/password-credentials.mjs +121 -0
  97. package/dist/internal/password-credentials.mjs.map +1 -0
  98. package/dist/internal/password-kernel.d.mts +8 -24
  99. package/dist/internal/password-kernel.mjs +5 -43
  100. package/dist/internal/password-kernel.mjs.map +1 -1
  101. package/dist/internal/persistence.d.mts +1 -1
  102. package/dist/internal/persistence.mjs +2 -2
  103. package/dist/internal/persistence.mjs.map +1 -1
  104. package/dist/internal/query-operations.d.mts +2 -0
  105. package/dist/internal/session-kernel.d.mts +2 -2
  106. package/dist/internal/session-kernel.mjs +74 -18
  107. package/dist/internal/session-kernel.mjs.map +1 -1
  108. package/dist/internal/sql-oauth-connected-model.d.mts +29 -0
  109. package/dist/internal/sql-oauth-connected-model.mjs +7 -0
  110. package/dist/internal/sql-oauth-connected-model.mjs.map +1 -0
  111. package/dist/internal/sql-oauth-model.d.mts +60 -0
  112. package/dist/internal/sql-oauth-model.mjs +10 -0
  113. package/dist/internal/sql-oauth-model.mjs.map +1 -0
  114. package/dist/internal/sql-query.d.mts +12 -0
  115. package/dist/internal/sql-query.mjs +15 -18
  116. package/dist/internal/sql-query.mjs.map +1 -1
  117. package/dist/internal/sql-storage-validation.mjs +54 -0
  118. package/dist/internal/sql-storage-validation.mjs.map +1 -0
  119. package/dist/internal/storage-mapping.mjs +1 -37
  120. package/dist/internal/storage-mapping.mjs.map +1 -1
  121. package/dist/internal/storage-plans.d.mts +7 -0
  122. package/dist/internal/storage-plans.mjs +189 -0
  123. package/dist/internal/storage-plans.mjs.map +1 -0
  124. package/dist/internal/storage-tables.d.mts +0 -56
  125. package/dist/internal/storage-tables.mjs +0 -17
  126. package/dist/internal/storage-tables.mjs.map +1 -1
  127. package/dist/internal/storage-validation.d.mts +6 -1
  128. package/dist/internal/storage-validation.mjs +42 -1
  129. package/dist/internal/storage-validation.mjs.map +1 -1
  130. package/dist/internal/transaction-kernel.mjs +3 -2
  131. package/dist/internal/transaction-kernel.mjs.map +1 -1
  132. package/package.json +1 -1
  133. package/src/Adapter.ts +89 -5
  134. package/src/OAuthPersistence.ts +427 -0
  135. package/src/index.ts +3 -0
  136. package/src/internal/configuration.ts +1 -3
  137. package/src/internal/email-kernel.ts +2 -1
  138. package/src/internal/models/oauth-connected-model.ts +396 -0
  139. package/src/internal/models/oauth-model.ts +708 -0
  140. package/src/internal/models/password-model.ts +2 -57
  141. package/src/internal/native-sql-table.ts +14 -0
  142. package/src/internal/oauth/accounts.ts +839 -0
  143. package/src/internal/oauth/connected-access.ts +388 -0
  144. package/src/internal/oauth/connected-collection.ts +345 -0
  145. package/src/internal/oauth/connected-custody.ts +308 -0
  146. package/src/internal/oauth/connected-flow.ts +562 -0
  147. package/src/internal/oauth/connected-input.ts +147 -0
  148. package/src/internal/oauth/connected-keys.ts +31 -0
  149. package/src/internal/oauth/connected-maintenance.ts +379 -0
  150. package/src/internal/oauth/connected-management.ts +391 -0
  151. package/src/internal/oauth/connected-reference.ts +135 -0
  152. package/src/internal/oauth/connected-settlement.ts +278 -0
  153. package/src/internal/oauth/connected-sign-in.ts +472 -0
  154. package/src/internal/oauth/connected-state.ts +924 -0
  155. package/src/internal/oauth/connected-target.ts +307 -0
  156. package/src/internal/oauth/flow.ts +505 -0
  157. package/src/internal/oauth/input.ts +201 -0
  158. package/src/internal/oauth/owner.ts +22 -0
  159. package/src/internal/oauth/query-compiler.ts +15 -0
  160. package/src/internal/oauth/registration.ts +676 -0
  161. package/src/internal/oauth/state.ts +218 -0
  162. package/src/internal/oauth/target.ts +803 -0
  163. package/src/internal/oauth-kernel.ts +294 -0
  164. package/src/internal/oauth-proxy.ts +176 -0
  165. package/src/internal/passkey/write-state.ts +18 -4
  166. package/src/internal/password-credentials.ts +245 -0
  167. package/src/internal/password-kernel.ts +7 -99
  168. package/src/internal/persistence.ts +1 -1
  169. package/src/internal/query-operations.ts +2 -0
  170. package/src/internal/session-kernel.ts +143 -55
  171. package/src/internal/sql-oauth-connected-model.ts +145 -0
  172. package/src/internal/sql-oauth-model.ts +263 -0
  173. package/src/internal/sql-query.ts +16 -13
  174. package/src/internal/sql-storage-validation.ts +64 -0
  175. package/src/internal/storage-mapping.ts +0 -37
  176. package/src/internal/storage-plans.ts +218 -0
  177. package/src/internal/storage-tables.ts +0 -20
  178. package/src/internal/storage-validation.ts +58 -0
  179. package/src/internal/transaction-kernel.ts +6 -2
  180. package/dist/internal/password-attempts.mjs +0 -197
  181. package/dist/internal/password-attempts.mjs.map +0 -1
  182. package/src/internal/password-attempts.ts +0 -466
@@ -0,0 +1,165 @@
1
+ import { CurrentOAuthTransaction } from "./owner.mjs";
2
+ import { OAuthQueryCompiler } from "./query-compiler.mjs";
3
+ import { invariant, oauthIdentityKey } from "./state.mjs";
4
+ import { OAuthConnectedState } from "./connected-state.mjs";
5
+ import { Context, Effect, Layer } from "effect";
6
+ import * as M from "@yielded/auth/OAuth";
7
+ import { snapshotOAuthSync } from "@yielded/auth/OAuth";
8
+ //#region src/internal/oauth/connected-custody.ts
9
+ const make = Effect.gen(function* () {
10
+ const operations = yield* OAuthQueryCompiler;
11
+ const S = yield* OAuthConnectedState;
12
+ const { eq, isNotNull, ne, sql } = operations;
13
+ const { both, col, equal } = operations;
14
+ const sameContext = (a, b) => S.tokenContextStorage.encode(a) === S.tokenContextStorage.encode(b);
15
+ const sameGrant = (a, b) => S.grantStorage.encode(a) === S.grantStorage.encode(b);
16
+ const summary = (context) => snapshotOAuthSync(M.OAuthConnectedSummary, {
17
+ grantId: context.grantId,
18
+ provider: context.identity.provider,
19
+ issuer: context.identity.issuer,
20
+ profileKey: context.configuration.profile.key,
21
+ status: "Active",
22
+ scopes: context.metadata.scopes,
23
+ ...context.metadata.accessExpiresAtMillis === void 0 ? {} : { accessExpiresAtMillis: context.metadata.accessExpiresAtMillis },
24
+ useUntilMillis: context.metadata.useUntilMillis,
25
+ ...context.metadata.profile === void 0 ? {} : { profile: context.metadata.profile },
26
+ remoteRevocation: context.configuration.profile.revocation === "cohort" ? "Pending" : "Unsupported"
27
+ });
28
+ /** A generation fences every sibling, including those beyond a bounded metadata page. */
29
+ const fence = Effect.fn("oauthConnected.fence")(function* (mapping, client, cohort) {
30
+ const owner = yield* CurrentOAuthTransaction;
31
+ invariant(cohort.row !== void 0);
32
+ const g = mapping.grant, h = mapping.cohort;
33
+ const active = both(eq(col(g.table, g.cohortKey), cohort.id), isNotNull(col(g.table, g.activeIdentityKey)));
34
+ const affected = yield* S.rowCount(g.table, active);
35
+ invariant(yield* owner.check(S.countCondition(g.table, active, affected)));
36
+ const cutoff = yield* S.nextOrder(mapping, client);
37
+ const generation = M.OAuthConnectedTarget.fields.cohortGeneration.make(owner.marker);
38
+ yield* owner.update(h.table, { [h.cohortKey]: cohort.id }, {
39
+ [h.generation]: generation,
40
+ [h.cutoff]: mapping.order.encode(cutoff),
41
+ [h.state]: "Blocked",
42
+ [h.version]: owner.marker
43
+ });
44
+ yield* owner.write(owner.database.update(g.table).set({
45
+ [g.activeIdentityKey]: null,
46
+ [g.state]: "ReauthorizationRequired",
47
+ [g.version]: owner.marker
48
+ }).where(active));
49
+ for (const observation of owner.observations) if (observation.table === g.table) observation.rows = observation.rows.map((row) => row[g.cohortKey] === cohort.id && row[g.activeIdentityKey] !== null ? {
50
+ ...row,
51
+ [g.activeIdentityKey]: null,
52
+ [g.state]: "ReauthorizationRequired",
53
+ [g.version]: owner.marker
54
+ } : row);
55
+ owner.postconditions.push(sql`not exists(select 1 from ${g.table} where ${active})`);
56
+ cohort.generation = generation;
57
+ cohort.cutoff = cutoff;
58
+ cohort.blocked = true;
59
+ return affected;
60
+ });
61
+ const storeJob = Effect.fn("oauthConnected.storeJob")(function* (mapping, job, token, native, now) {
62
+ const owner = yield* CurrentOAuthTransaction;
63
+ invariant(mapping.revocation.mode === "cohort" && token.configuration.profile.revocation === "cohort" && sameContext(job.context.token, token));
64
+ const j = mapping.revocation.job, identity = yield* oauthIdentityKey(token.identity), client = yield* S.clientKey(token.configuration), cohort = yield* S.cohortKey(client, identity);
65
+ const h = mapping.cohort;
66
+ owner.postconditions.push(sql`exists(select 1 from ${h.table} where ${both(equal(h.table, {
67
+ [h.cohortKey]: cohort,
68
+ [h.clientKey]: client,
69
+ [h.identityKey]: identity,
70
+ [h.state]: "Blocked"
71
+ }))})`);
72
+ const key = { [j.jobId]: job.context.jobId };
73
+ const prior = yield* owner.read(j.table, equal(j.table, key), { limit: 1 });
74
+ if (prior.rows.length) {
75
+ const row = prior.rows[0];
76
+ invariant(row[j.snapshot] === S.jobStorage.encode(job) && row[j.moduleId] === token.moduleId && mapping.subjectId.equals(row[j.subjectId], native) && row[j.identityKey] === identity && row[j.clientKey] === client && row[j.cohortKey] === cohort && row[j.grantId] === token.grantId && [
77
+ "Pending",
78
+ "Claimed",
79
+ "Confirmed",
80
+ "Unknown"
81
+ ].includes(row[j.state]));
82
+ invariant(row[j.state] === "Pending" ? row[j.claimId] === null && row[j.claimedAt] === null && row[j.claimExpiresAt] === null : typeof row[j.claimId] === "string" && mapping.clock.decodeInstant(row[j.claimExpiresAt]) > mapping.clock.decodeInstant(row[j.claimedAt]));
83
+ return;
84
+ }
85
+ prior.rows = (yield* owner.insert(j.table, {
86
+ ...j.encodeInsert({
87
+ job: snapshotOAuthSync(M.OAuthConnectedRevocationJob, job),
88
+ subjectId: S.nativeCopy(native)
89
+ }),
90
+ ...key,
91
+ [j.moduleId]: token.moduleId,
92
+ [j.subjectId]: native,
93
+ [j.identityKey]: identity,
94
+ [j.clientKey]: client,
95
+ [j.cohortKey]: cohort,
96
+ [j.grantId]: token.grantId,
97
+ [j.snapshot]: S.jobStorage.encode(job),
98
+ [j.state]: "Pending",
99
+ [j.claimId]: null,
100
+ [j.claimedAt]: null,
101
+ [j.claimExpiresAt]: null,
102
+ [j.retentionUntil]: mapping.clock.encodeInstant(S.retainedUntil(mapping, now)),
103
+ [j.version]: owner.marker
104
+ }, key)).rows;
105
+ });
106
+ const noJobs = (mapping, id, except) => {
107
+ const j = S.jobTable(mapping);
108
+ return j === void 0 ? sql`1 = 1` : sql`not exists(select 1 from ${j.table} where ${both(eq(col(j.table, j.cohortKey), id), ne(col(j.table, j.state), "Confirmed"), except === void 0 ? void 0 : ne(col(j.table, j.jobId), except))})`;
109
+ };
110
+ const clearableCondition = (mapping, cohortId, clientId) => {
111
+ const f = mapping.flow;
112
+ return both(sql`not exists(select 1 from ${f.table} where ${S.unknownWork(mapping, clientId)})`, S.cohortWork(mapping, cohortId), noJobs(mapping, cohortId));
113
+ };
114
+ /** Clearing owns a NEW cutoff. Claims made while the remote revoke was pending
115
+ * cannot enter the next generation even if they finish after that revoke. */
116
+ const clear = Effect.fn("oauthConnected.clear")(function* (mapping, client, cohort, confirmedJob, provenOpenWithoutNewJob = false) {
117
+ const owner = yield* CurrentOAuthTransaction;
118
+ if (!cohort.blocked || cohort.row === void 0 || S.jobTable(mapping) === void 0 && !provenOpenWithoutNewJob) return false;
119
+ const f = mapping.flow;
120
+ const noUnknown = sql`not exists(select 1 from ${f.table} where ${S.unknownWork(mapping, client.id)})`;
121
+ const work = S.cohortWork(mapping, cohort.id);
122
+ if (!(yield* owner.check(both(noUnknown, work, noJobs(mapping, cohort.id, confirmedJob))))) return false;
123
+ const cutoff = yield* S.nextOrder(mapping, client), h = mapping.cohort;
124
+ const generation = M.OAuthConnectedTarget.fields.cohortGeneration.make(owner.marker + ":open");
125
+ yield* owner.update(h.table, { [h.cohortKey]: cohort.id }, {
126
+ [h.generation]: generation,
127
+ [h.cutoff]: mapping.order.encode(cutoff),
128
+ [h.state]: "Open",
129
+ [h.version]: owner.marker
130
+ });
131
+ owner.postconditions.push(noUnknown, work, noJobs(mapping, cohort.id));
132
+ cohort.generation = generation;
133
+ cohort.cutoff = cutoff;
134
+ cohort.blocked = false;
135
+ return true;
136
+ });
137
+ /** Former local owners remain visible. A foreign historical dependency cannot be
138
+ * ignored simply because the global tuple was changed outside this authority. */
139
+ const noFormerOwner = (mapping, identity, client, native) => Effect.gen(function* () {
140
+ const owner = yield* CurrentOAuthTransaction;
141
+ const g = mapping.grant, f = mapping.flow, j = S.jobTable(mapping);
142
+ const condition = both(sql`not exists(select 1 from ${g.table} where ${both(eq(col(g.table, g.identityKey), identity), sql`not (${owner.exact(g.table, { [g.subjectId]: native })})`)})`, sql`not exists(select 1 from ${f.table} where ${both(eq(col(f.table, f.cohortKey), yield* S.cohortKey(client, identity)), eq(col(f.table, f.work), "Unresolved"), sql`not (${owner.exact(f.table, { [f.subjectId]: native })})`)})`, ...j === void 0 ? [] : [sql`not exists(select 1 from ${j.table} where ${both(eq(col(j.table, j.identityKey), identity), sql`not (${owner.exact(j.table, { [j.subjectId]: native })})`)})`]);
143
+ return yield* Effect.map(owner.check(condition), (accepted) => {
144
+ if (accepted) owner.postconditions.push(condition);
145
+ return accepted;
146
+ });
147
+ });
148
+ return {
149
+ sameContext,
150
+ sameGrant,
151
+ summary,
152
+ fence,
153
+ storeJob,
154
+ clearableCondition,
155
+ clear,
156
+ noFormerOwner
157
+ };
158
+ });
159
+ var OAuthConnectedCustody = class OAuthConnectedCustody extends Context.Service()("effect-auth/persistence/OAuthConnectedCustody") {
160
+ static layer = Layer.effect(OAuthConnectedCustody, make);
161
+ };
162
+ //#endregion
163
+ export { OAuthConnectedCustody };
164
+
165
+ //# sourceMappingURL=connected-custody.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"connected-custody.mjs","names":[],"sources":["../../../src/internal/oauth/connected-custody.ts"],"sourcesContent":["import * as M from \"@yielded/auth/OAuth\";\nimport { snapshotOAuthSync } from \"@yielded/auth/OAuth\";\nimport { Effect, Context, Layer } from \"effect\";\n\nimport type { SqlExpression as SQL } from \"../query-operations\";\nimport { OAuthConnectedState } from \"./connected-state\";\nimport type * as STypes from \"./connected-state\";\nimport { CurrentOAuthTransaction } from \"./owner\";\nimport { OAuthQueryCompiler } from \"./query-compiler\";\nimport { invariant, oauthIdentityKey } from \"./state\";\n\ntype Client = NonNullable<Effect.Success<ReturnType<OAuthConnectedState[\"Service\"][\"client\"]>>>;\n\ntype Cohort = Effect.Success<ReturnType<OAuthConnectedState[\"Service\"][\"cohort\"]>>;\n\nconst make = Effect.gen(function* () {\n const operations = yield* OAuthQueryCompiler;\n const S = yield* OAuthConnectedState;\n\n const { eq, isNotNull, ne, sql } = operations;\n const { both, col, equal } = operations;\n\n const sameContext = (a: M.OAuthConnectedTokenContext, b: M.OAuthConnectedTokenContext) =>\n S.tokenContextStorage.encode(a) === S.tokenContextStorage.encode(b);\n\n const sameGrant = (a: M.OAuthConnectedStoredGrant, b: M.OAuthConnectedStoredGrant) =>\n S.grantStorage.encode(a) === S.grantStorage.encode(b);\n\n const summary = (context: M.OAuthConnectedTokenContext) =>\n snapshotOAuthSync(M.OAuthConnectedSummary, {\n grantId: context.grantId,\n provider: context.identity.provider,\n issuer: context.identity.issuer,\n profileKey: context.configuration.profile.key,\n status: \"Active\",\n scopes: context.metadata.scopes,\n ...(context.metadata.accessExpiresAtMillis === undefined\n ? {}\n : { accessExpiresAtMillis: context.metadata.accessExpiresAtMillis }),\n useUntilMillis: context.metadata.useUntilMillis,\n ...(context.metadata.profile === undefined ? {} : { profile: context.metadata.profile }),\n remoteRevocation:\n context.configuration.profile.revocation === \"cohort\" ? \"Pending\" : \"Unsupported\",\n });\n\n /** A generation fences every sibling, including those beyond a bounded metadata page. */\n const fence = Effect.fn(\"oauthConnected.fence\")(function* (\n mapping: STypes.Authority,\n client: Client,\n cohort: Cohort,\n ) {\n const owner = yield* CurrentOAuthTransaction;\n\n invariant(cohort.row !== undefined);\n\n const g = mapping.grant,\n h = mapping.cohort;\n\n const active = both(\n eq(col(g.table, g.cohortKey), cohort.id),\n isNotNull(col(g.table, g.activeIdentityKey)),\n );\n\n const affected = yield* S.rowCount(g.table, active);\n\n invariant(yield* owner.check(S.countCondition(g.table, active, affected)));\n const cutoff = yield* S.nextOrder(mapping, client);\n const generation = M.OAuthConnectedTarget.fields.cohortGeneration.make(owner.marker);\n\n yield* owner.update(\n h.table,\n { [h.cohortKey]: cohort.id },\n {\n [h.generation]: generation,\n [h.cutoff]: mapping.order.encode(cutoff),\n [h.state]: \"Blocked\",\n [h.version]: owner.marker,\n },\n );\n yield* owner.write(\n owner.database\n .update(g.table)\n .set({\n [g.activeIdentityKey]: null,\n [g.state]: \"ReauthorizationRequired\",\n [g.version]: owner.marker,\n })\n .where(active),\n );\n for (const observation of owner.observations)\n if (observation.table === g.table)\n observation.rows = observation.rows.map((row) =>\n row[g.cohortKey] === cohort.id && row[g.activeIdentityKey] !== null\n ? {\n ...row,\n [g.activeIdentityKey]: null,\n [g.state]: \"ReauthorizationRequired\",\n [g.version]: owner.marker,\n }\n : row,\n );\n owner.postconditions.push(sql`not exists(select 1 from ${g.table} where ${active})`);\n cohort.generation = generation;\n cohort.cutoff = cutoff;\n cohort.blocked = true;\n\n return affected;\n });\n\n const storeJob = Effect.fn(\"oauthConnected.storeJob\")(function* (\n mapping: STypes.Mapping,\n job: M.OAuthConnectedRevocationJob,\n token: M.OAuthConnectedTokenContext,\n native: unknown,\n now: number,\n ) {\n const owner = yield* CurrentOAuthTransaction;\n\n invariant(\n mapping.revocation.mode === \"cohort\" &&\n token.configuration.profile.revocation === \"cohort\" &&\n sameContext(job.context.token, token),\n );\n\n const j = mapping.revocation.job,\n identity = yield* oauthIdentityKey(token.identity),\n client = yield* S.clientKey(token.configuration),\n cohort = yield* S.cohortKey(client, identity);\n\n const h = mapping.cohort;\n\n owner.postconditions.push(\n sql`exists(select 1 from ${h.table} where ${both(equal(h.table, { [h.cohortKey]: cohort, [h.clientKey]: client, [h.identityKey]: identity, [h.state]: \"Blocked\" }))})`,\n );\n const key = { [j.jobId]: job.context.jobId };\n const prior = yield* owner.read(j.table, equal(j.table, key), { limit: 1 });\n\n if (prior.rows.length) {\n const row = prior.rows[0]!;\n\n invariant(\n row[j.snapshot] === S.jobStorage.encode(job) &&\n row[j.moduleId] === token.moduleId &&\n mapping.subjectId.equals(row[j.subjectId], native) &&\n row[j.identityKey] === identity &&\n row[j.clientKey] === client &&\n row[j.cohortKey] === cohort &&\n row[j.grantId] === token.grantId &&\n [\"Pending\", \"Claimed\", \"Confirmed\", \"Unknown\"].includes(row[j.state]),\n );\n invariant(\n row[j.state] === \"Pending\"\n ? row[j.claimId] === null && row[j.claimedAt] === null && row[j.claimExpiresAt] === null\n : typeof row[j.claimId] === \"string\" &&\n mapping.clock.decodeInstant(row[j.claimExpiresAt]) >\n mapping.clock.decodeInstant(row[j.claimedAt]),\n );\n\n return;\n }\n\n const inserted = yield* owner.insert(\n j.table,\n {\n ...j.encodeInsert({\n job: snapshotOAuthSync(M.OAuthConnectedRevocationJob, job),\n subjectId: S.nativeCopy(native),\n }),\n ...key,\n [j.moduleId]: token.moduleId,\n [j.subjectId]: native,\n [j.identityKey]: identity,\n [j.clientKey]: client,\n [j.cohortKey]: cohort,\n [j.grantId]: token.grantId,\n [j.snapshot]: S.jobStorage.encode(job),\n [j.state]: \"Pending\",\n [j.claimId]: null,\n [j.claimedAt]: null,\n [j.claimExpiresAt]: null,\n [j.retentionUntil]: mapping.clock.encodeInstant(S.retainedUntil(mapping, now)),\n [j.version]: owner.marker,\n },\n key,\n );\n\n prior.rows = inserted.rows;\n });\n\n const noJobs = (mapping: STypes.Authority, id: string | SQL, except?: string) => {\n const j = S.jobTable(mapping);\n\n return j === undefined\n ? sql`1 = 1`\n : sql`not exists(select 1 from ${j.table} where ${both(eq(col(j.table, j.cohortKey), id), ne(col(j.table, j.state), \"Confirmed\"), except === undefined ? undefined : ne(col(j.table, j.jobId), except))})`;\n };\n\n const clearableCondition = (\n mapping: STypes.Authority,\n cohortId: string | SQL,\n clientId: string | SQL,\n ) => {\n const f = mapping.flow;\n\n return both(\n sql`not exists(select 1 from ${f.table} where ${S.unknownWork(mapping, clientId)})`,\n S.cohortWork(mapping, cohortId),\n noJobs(mapping, cohortId),\n );\n };\n\n /** Clearing owns a NEW cutoff. Claims made while the remote revoke was pending\n * cannot enter the next generation even if they finish after that revoke. */\n const clear = Effect.fn(\"oauthConnected.clear\")(function* (\n mapping: STypes.Authority,\n client: Client,\n cohort: Cohort,\n confirmedJob?: string,\n provenOpenWithoutNewJob = false,\n ) {\n const owner = yield* CurrentOAuthTransaction;\n\n if (\n !cohort.blocked ||\n cohort.row === undefined ||\n (S.jobTable(mapping) === undefined && !provenOpenWithoutNewJob)\n )\n return false;\n const f = mapping.flow;\n const noUnknown = sql`not exists(select 1 from ${f.table} where ${S.unknownWork(mapping, client.id)})`;\n const work = S.cohortWork(mapping, cohort.id);\n\n if (!(yield* owner.check(both(noUnknown, work, noJobs(mapping, cohort.id, confirmedJob)))))\n return false;\n\n const cutoff = yield* S.nextOrder(mapping, client),\n h = mapping.cohort;\n\n const generation = M.OAuthConnectedTarget.fields.cohortGeneration.make(owner.marker + \":open\");\n\n yield* owner.update(\n h.table,\n { [h.cohortKey]: cohort.id },\n {\n [h.generation]: generation,\n [h.cutoff]: mapping.order.encode(cutoff),\n [h.state]: \"Open\",\n [h.version]: owner.marker,\n },\n );\n owner.postconditions.push(noUnknown, work, noJobs(mapping, cohort.id));\n cohort.generation = generation;\n cohort.cutoff = cutoff;\n cohort.blocked = false;\n\n return true;\n });\n\n /** Former local owners remain visible. A foreign historical dependency cannot be\n * ignored simply because the global tuple was changed outside this authority. */\n const noFormerOwner = (\n mapping: STypes.Authority,\n identity: string,\n client: string,\n native: unknown,\n ) =>\n Effect.gen(function* () {\n const owner = yield* CurrentOAuthTransaction;\n\n const g = mapping.grant,\n f = mapping.flow,\n j = S.jobTable(mapping);\n\n const condition = both(\n sql`not exists(select 1 from ${g.table} where ${both(eq(col(g.table, g.identityKey), identity), sql`not (${owner.exact(g.table, { [g.subjectId]: native })})`)})`,\n sql`not exists(select 1 from ${f.table} where ${both(eq(col(f.table, f.cohortKey), yield* S.cohortKey(client, identity)), eq(col(f.table, f.work), \"Unresolved\"), sql`not (${owner.exact(f.table, { [f.subjectId]: native })})`)})`,\n ...(j === undefined\n ? []\n : [\n sql`not exists(select 1 from ${j.table} where ${both(eq(col(j.table, j.identityKey), identity), sql`not (${owner.exact(j.table, { [j.subjectId]: native })})`)})`,\n ]),\n );\n\n return yield* Effect.map(owner.check(condition), (accepted) => {\n if (accepted) owner.postconditions.push(condition);\n\n return accepted;\n });\n });\n\n return {\n sameContext,\n sameGrant,\n summary,\n fence,\n storeJob,\n clearableCondition,\n clear,\n noFormerOwner,\n };\n});\n\nexport class OAuthConnectedCustody extends Context.Service<\n OA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GAAG,GAAG,IAAI,EAAE,OAAO,EAAE,IAAI,GAAG,YAAY,GAAG,GAAG,QAAQ,MAAM,MAAM,EAAE,OAAO,GAAG,EAAE,YAAY,OAAO,CAAC,EAAE,EAAE,EAAE,IACjO,GAAI,MAAM,KAAA,IACN,CAAC,IACD,CACE,GAAG,4BAA4B,EAAE,MAAM,SAAS,KAAK,GAAG,IAAI,EAAE,OAAO,EAAE,WAAW,GAAG,QAAQ,GAAG,GAAG,QAAQ,MAAM,MAAM,EAAE,OAAO,GAAG,EAAE,YAAY,OAAO,CAAC,EAAE,EAAE,EAAE,EACjK,CACN;EAEA,OAAO,OAAO,OAAO,IAAI,MAAM,MAAM,SAAS,IAAI,aAAa;GAC7D,IAAI,UAAU,MAAM,eAAe,KAAK,SAAS;GAEjD,OAAO;EACT,CAAC;CACH,CAAC;CAEH,OAAO;EACL;EACA;EACA;EACA;EACA;EACA;EACA;EACA;CACF;AACF,CAAC;AAED,IAAa,wBAAb,MAAa,8BAA8B,QAAQ,QAGjD,CAAC,CAAC,+CAA+C,CAAC,CAAC;CACnD,OAAgB,QAAQ,MAAM,OAAO,uBAAuB,IAAI;AAClE"}
@@ -0,0 +1,7 @@
1
+ import "../mapping-error.mjs";
2
+ import "./owner.mjs";
3
+ import "./connected-state.mjs";
4
+ import "./query-compiler.mjs";
5
+ import "./registration.mjs";
6
+ import { Context, Effect, Layer } from "effect";
7
+ import { OAuthConnectedPersistence } from "@yielded/auth/OAuth";
@@ -0,0 +1,324 @@
1
+ import { digest } from "../crypto.mjs";
2
+ import { CurrentOAuthTransaction } from "./owner.mjs";
3
+ import { OAuthQueryCompiler } from "./query-compiler.mjs";
4
+ import { invariant, oauthIdentityKey, sameIdentity, sameRevision } from "./state.mjs";
5
+ import { OAuthRegistration } from "./registration.mjs";
6
+ import { OAuthConnectedState } from "./connected-state.mjs";
7
+ import { Context, Effect, Layer } from "effect";
8
+ import * as M from "@yielded/auth/OAuth";
9
+ import { OAuthAccountRevision, snapshotOAuthSync } from "@yielded/auth/OAuth";
10
+ //#region src/internal/oauth/connected-flow.ts
11
+ const make = Effect.gen(function* () {
12
+ const operations = yield* OAuthQueryCompiler;
13
+ const S = yield* OAuthConnectedState;
14
+ const registration = yield* OAuthRegistration;
15
+ const { sql } = operations;
16
+ const { both, copiedRow, equal } = operations;
17
+ const { acquireTuple, readTuple } = registration;
18
+ const pending = Effect.fn("oauthConnected.pending")(function* (mapping, access, lock = true) {
19
+ const owner = yield* CurrentOAuthTransaction;
20
+ const f = mapping.flow;
21
+ const row = (yield* owner.read(f.table, equal(f.table, {
22
+ [f.moduleId]: access.moduleId,
23
+ [f.flowId]: access.flowId
24
+ }), {
25
+ limit: 1,
26
+ lock,
27
+ observe: lock
28
+ })).rows[0];
29
+ if (row === void 0 || row[f.snapshot] === null) return void 0;
30
+ const flow = S.flowStorage.decode(row[f.snapshot]), c = flow.context;
31
+ const native = yield* mapping.subjectId.toNative(c.revision.subjectId);
32
+ invariant(row[f.moduleId] === c.moduleId && row[f.flowId] === c.flowId && row[f.commandId] === c.commandId && mapping.subjectId.equals(native, row[f.subjectId]) && row[f.clientKey] === (yield* S.clientKey(S.configuration(flow))) && row[f.stateDigest] === c.stateDigest && mapping.clock.decodeInstant(row[f.expiresAt]) === c.expiresAtMillis && mapping.clock.decodeInstant(row[f.retentionUntil]) === flow.retentionUntilMillis);
33
+ return c.moduleId === access.moduleId && c.generation === access.generation && c.flowId === access.flowId && c.revision.subjectId === access.subjectId && c.provider === access.provider && c.callbackId === access.callbackId && c.stateDigest === access.stateDigest && c.requestBindingVerifier === access.requestBindingVerifier && c.requestBindingExpiresAtMillis === access.requestBindingExpiresAtMillis && (c.responseIssuerMode === "required" ? access.responseIssuer === c.issuer : access.responseIssuer === void 0) ? {
34
+ row,
35
+ flow,
36
+ native
37
+ } : void 0;
38
+ });
39
+ const live = (flow, now) => flow.context.issuedAtMillis <= now && now < flow.context.expiresAtMillis && flow.context.expiresAtMillis <= flow.context.requestBindingExpiresAtMillis && flow.retentionUntilMillis >= flow.context.expiresAtMillis + flow.context.claimLifetimeMillis;
40
+ const expected = (flow) => ({
41
+ moduleId: flow.context.moduleId,
42
+ flowId: flow.context.flowId,
43
+ commandId: flow.context.commandId,
44
+ revision: flow.context.revision,
45
+ intent: S.contextStorage.encode(flow.context),
46
+ maximumAgeMillis: flow.context.maximumEvidenceAgeMillis,
47
+ configuration: S.configuration(flow)
48
+ });
49
+ const retain = Effect.fn("oauthConnected.retain")(function* (mapping, input) {
50
+ const owner = yield* CurrentOAuthTransaction;
51
+ const flow = snapshotOAuthSync(M.OAuthConnectedPendingFlow, input.flow), c = flow.context;
52
+ const found = yield* S.current(mapping, c.revision.subjectId);
53
+ if (found === void 0) return { _tag: "Rejected" };
54
+ if (input._tag === "Prepare") {
55
+ if (input.authorization.moduleId !== c.moduleId || !sameRevision(input.authorization.revision, c.revision) || !sameRevision(found.revision, c.revision) || (yield* S.useAuthority(mapping, input.authorization, "metadata")) === void 0) return { _tag: "Rejected" };
56
+ } else if (!(yield* S.action(mapping, found, input.authorization, "issue", expected(flow)))) return { _tag: "Rejected" };
57
+ if (c.profile.revocation === "cohort") invariant(mapping.revocation.mode === "cohort");
58
+ const client = yield* S.client(mapping, S.configuration(flow), true);
59
+ invariant(client !== void 0);
60
+ const now = yield* owner.now(mapping.clock), f = mapping.flow;
61
+ if (!live(flow, now)) return { _tag: "Rejected" };
62
+ if (c.reconnect !== void 0) {
63
+ const original = yield* S.readGrant(mapping, c.moduleId, c.reconnect.grantId, false);
64
+ if (original === void 0 || original.context.subjectId !== c.revision.subjectId || !sameTarget(c.reconnect, original.context)) return { _tag: "Rejected" };
65
+ owner.postconditions.push(sql`exists(select 1 from ${mapping.grant.table} where ${owner.exact(mapping.grant.table, original.row)})`);
66
+ }
67
+ const key = {
68
+ [f.moduleId]: c.moduleId,
69
+ [f.flowId]: c.flowId
70
+ };
71
+ const prior = yield* owner.read(f.table, equal(f.table, key), { limit: 1 });
72
+ const command = yield* owner.read(f.table, equal(f.table, {
73
+ [f.moduleId]: c.moduleId,
74
+ [f.commandId]: c.commandId
75
+ }), { limit: 1 });
76
+ owner.postconditions.push(sql`${mapping.clock.engineNowMillis} >= ${c.issuedAtMillis} and ${mapping.clock.engineNowMillis} < ${c.expiresAtMillis}`);
77
+ if (input._tag === "Issue") {
78
+ const row = prior.rows[0];
79
+ if (row === void 0 || command.rows[0] === void 0 || row[f.state] !== "Prepared" || row[f.snapshot] !== S.flowStorage.encode(flow) || command.rows[0][f.flowId] !== c.flowId) return { _tag: "Rejected" };
80
+ yield* owner.update(f.table, {
81
+ ...key,
82
+ [f.state]: "Prepared",
83
+ [f.version]: row[f.version]
84
+ }, {
85
+ [f.state]: "Pending",
86
+ [f.version]: owner.marker
87
+ });
88
+ owner.postconditions.push(sql`exists(select 1 from ${f.table} where ${equal(f.table, {
89
+ ...key,
90
+ [f.state]: "Pending",
91
+ [f.version]: owner.marker
92
+ })})`);
93
+ return {
94
+ _tag: "Issued",
95
+ flow
96
+ };
97
+ }
98
+ if (prior.rows.length || command.rows.length) return { _tag: "Rejected" };
99
+ const values = {
100
+ ...f.encodeInsert({
101
+ flow: snapshotOAuthSync(M.OAuthConnectedPendingFlow, flow),
102
+ subjectId: S.nativeCopy(found.nativeId)
103
+ }),
104
+ ...key,
105
+ [f.commandId]: c.commandId,
106
+ [f.subjectId]: found.nativeId,
107
+ [f.clientKey]: client.id,
108
+ [f.cohortKey]: null,
109
+ [f.state]: "Prepared",
110
+ [f.version]: owner.marker,
111
+ [f.stateDigest]: c.stateDigest,
112
+ [f.snapshot]: S.flowStorage.encode(flow),
113
+ [f.claimId]: null,
114
+ [f.claimDigest]: null,
115
+ [f.claimOrder]: null,
116
+ [f.claimedAt]: null,
117
+ [f.claimExpiresAt]: null,
118
+ [f.expiresAt]: mapping.clock.encodeInstant(c.expiresAtMillis),
119
+ [f.retentionUntil]: mapping.clock.encodeInstant(flow.retentionUntilMillis),
120
+ [f.work]: "None",
121
+ [f.custody]: null
122
+ };
123
+ const inserted = yield* owner.insert(f.table, values, key, true);
124
+ prior.rows = inserted.rows;
125
+ command.rows = inserted.rows;
126
+ if (inserted.rows[0]?.[f.version] !== owner.marker) return { _tag: "Rejected" };
127
+ return {
128
+ _tag: "Issued",
129
+ flow
130
+ };
131
+ });
132
+ const prepare = (mapping, input) => retain(mapping, {
133
+ _tag: "Prepare",
134
+ ...input
135
+ });
136
+ const issue = (mapping, input) => retain(mapping, {
137
+ _tag: "Issue",
138
+ ...input
139
+ });
140
+ const inspectPrepared = Effect.fn("oauthConnected.inspectPrepared")(function* (mapping, access) {
141
+ const owner = yield* CurrentOAuthTransaction;
142
+ const found = yield* S.current(mapping, access.subjectId);
143
+ if (found === void 0) return void 0;
144
+ const f = mapping.flow;
145
+ const row = (yield* owner.read(f.table, equal(f.table, {
146
+ [f.moduleId]: access.moduleId,
147
+ [f.flowId]: access.flowId,
148
+ [f.subjectId]: found.nativeId
149
+ }), { limit: 1 })).rows[0];
150
+ if (row === void 0 || row[f.state] !== "Prepared" || row[f.snapshot] === null) return void 0;
151
+ const flow = S.flowStorage.decode(row[f.snapshot]);
152
+ const c = flow.context;
153
+ invariant(row[f.moduleId] === c.moduleId && row[f.flowId] === c.flowId && row[f.commandId] === c.commandId && mapping.subjectId.equals(found.nativeId, row[f.subjectId]) && row[f.clientKey] === (yield* S.clientKey(S.configuration(flow))) && row[f.stateDigest] === c.stateDigest && mapping.clock.decodeInstant(row[f.expiresAt]) === c.expiresAtMillis && mapping.clock.decodeInstant(row[f.retentionUntil]) === flow.retentionUntilMillis);
154
+ const now = yield* owner.now(mapping.clock);
155
+ return c.moduleId === access.moduleId && c.generation === access.generation && c.flowId === access.flowId && c.commandId === access.commandId && c.revision.subjectId === access.subjectId && sameRevision(found.revision, c.revision) && c.requestBindingVerifier === access.requestBindingVerifier && c.requestBindingExpiresAtMillis === access.requestBindingExpiresAtMillis && live(flow, now) ? flow : void 0;
156
+ });
157
+ const sameTarget = (target, context) => target.grantId === context.grantId && target.grantVersion === context.grantVersion && target.tokenVersion === context.tokenVersion && target.cohortGeneration === context.cohortGeneration && sameIdentity(target.identity, context.identity) && S.tokenContextStorage.encode({
158
+ ...context,
159
+ configuration: target.configuration
160
+ }) === S.tokenContextStorage.encode(context);
161
+ const capture = Effect.fn("oauthConnected.capture")(function* (mapping, input) {
162
+ const found = yield* S.current(mapping, input.subjectId);
163
+ if (found === void 0) return void 0;
164
+ if (input.grantId === void 0) return { revision: snapshotOAuthSync(OAuthAccountRevision, found.revision) };
165
+ const grant = yield* S.readGrant(mapping, input.moduleId, input.grantId, false);
166
+ if (grant === void 0 || grant.context.subjectId !== input.subjectId) return void 0;
167
+ return {
168
+ revision: snapshotOAuthSync(OAuthAccountRevision, found.revision),
169
+ target: snapshotOAuthSync(M.OAuthConnectedTarget, grant.context)
170
+ };
171
+ });
172
+ const preflight = Effect.fn("oauthConnected.preflight")(function* (mapping, input) {
173
+ const owner = yield* CurrentOAuthTransaction;
174
+ const read = yield* pending(mapping, input);
175
+ const now = yield* owner.now(mapping.clock);
176
+ if (read === void 0 || read.row[mapping.flow.state] !== "Pending" || !live(read.flow, now)) return void 0;
177
+ return read.flow;
178
+ });
179
+ const claim = Effect.fn("oauthConnected.claim")(function* (mapping, input) {
180
+ const owner = yield* CurrentOAuthTransaction;
181
+ const flow = snapshotOAuthSync(M.OAuthConnectedPendingFlow, input.flow), c = flow.context;
182
+ const found = yield* S.current(mapping, c.revision.subjectId);
183
+ if (found === void 0 || !(yield* S.action(mapping, found, input.authorization, "claim", expected(flow)))) return { _tag: "Rejected" };
184
+ const client = yield* S.client(mapping, S.configuration(flow), false);
185
+ if (client === void 0) return { _tag: "Rejected" };
186
+ const read = yield* pending(mapping, input.access), now = yield* owner.now(mapping.clock), f = mapping.flow;
187
+ if (read === void 0 || read.row[f.state] !== "Pending" || S.flowStorage.encode(read.flow) !== S.flowStorage.encode(flow) || !live(flow, now)) return { _tag: "Rejected" };
188
+ const expires = now + c.claimLifetimeMillis;
189
+ invariant(Number.isSafeInteger(expires) && expires <= flow.retentionUntilMillis);
190
+ yield* S.touchScope(mapping, client.scope);
191
+ const order = yield* S.nextOrder(mapping, client);
192
+ const claimed = snapshotOAuthSync(M.OAuthConnectedClaim, {
193
+ flow,
194
+ claimId: input.claimId,
195
+ claimedAtMillis: now,
196
+ claimExpiresAtMillis: expires,
197
+ order: String(order)
198
+ });
199
+ yield* owner.update(f.table, {
200
+ [f.moduleId]: c.moduleId,
201
+ [f.flowId]: c.flowId,
202
+ [f.state]: "Pending",
203
+ [f.version]: read.row[f.version]
204
+ }, {
205
+ [f.state]: "Claimed",
206
+ [f.claimId]: claimed.claimId,
207
+ [f.claimDigest]: yield* digest(S.claimStorage.encode(claimed)),
208
+ [f.claimOrder]: mapping.order.encode(order),
209
+ [f.claimedAt]: mapping.clock.encodeInstant(now),
210
+ [f.claimExpiresAt]: mapping.clock.encodeInstant(expires),
211
+ [f.work]: "Unresolved",
212
+ [f.version]: owner.marker
213
+ });
214
+ owner.postconditions.push(sql`${mapping.clock.engineNowMillis} >= ${now} and ${mapping.clock.engineNowMillis} < ${Math.min(c.expiresAtMillis, expires)}`);
215
+ return {
216
+ _tag: "Claimed",
217
+ claim: claimed
218
+ };
219
+ });
220
+ const exact = Effect.fn("oauthConnected.exactClaim")(function* (mapping, claim) {
221
+ const owner = yield* CurrentOAuthTransaction;
222
+ const f = mapping.flow, c = claim.flow.context;
223
+ const row = (yield* owner.read(f.table, equal(f.table, {
224
+ [f.moduleId]: c.moduleId,
225
+ [f.flowId]: c.flowId
226
+ }), { limit: 1 })).rows[0];
227
+ if (row === void 0 || row[f.work] !== "Unresolved" || row[f.claimId] !== claim.claimId || row[f.claimDigest] !== (yield* digest(S.claimStorage.encode(claim)))) return void 0;
228
+ const native = yield* mapping.subjectId.toNative(c.revision.subjectId);
229
+ invariant(row[f.commandId] === c.commandId && mapping.subjectId.equals(native, row[f.subjectId]) && row[f.stateDigest] === c.stateDigest && mapping.clock.decodeInstant(row[f.expiresAt]) === c.expiresAtMillis && mapping.clock.decodeInstant(row[f.retentionUntil]) === claim.flow.retentionUntilMillis && row[f.clientKey] === (yield* S.clientKey(S.configuration(claim.flow))) && S.nativeOrder(mapping, row[f.claimOrder]) === S.orderNumber(claim.order) && mapping.clock.decodeInstant(row[f.claimedAt]) === claim.claimedAtMillis && mapping.clock.decodeInstant(row[f.claimExpiresAt]) === claim.claimExpiresAtMillis);
230
+ const now = yield* owner.now(mapping.clock);
231
+ invariant(now >= claim.claimedAtMillis);
232
+ return {
233
+ row,
234
+ now,
235
+ active: row[f.state] === "Claimed" && now < claim.claimExpiresAtMillis
236
+ };
237
+ });
238
+ const terminal = Effect.fn("oauthConnected.terminal")(function* (mapping, claim, state, work, custody) {
239
+ const owner = yield* CurrentOAuthTransaction;
240
+ const f = mapping.flow, c = claim.flow.context;
241
+ yield* owner.update(f.table, {
242
+ [f.moduleId]: c.moduleId,
243
+ [f.flowId]: c.flowId,
244
+ [f.claimId]: claim.claimId
245
+ }, {
246
+ [f.state]: state,
247
+ [f.snapshot]: null,
248
+ [f.work]: work,
249
+ [f.cohortKey]: custody?.cohort ?? null,
250
+ [f.custody]: custody?.sealed === void 0 ? null : S.grantStorage.encode(custody.sealed),
251
+ [f.version]: owner.marker
252
+ });
253
+ });
254
+ const inspectTuple = Effect.fn("oauthConnected.inspectTuple")(function* (mapping, identity) {
255
+ const owner = yield* CurrentOAuthTransaction;
256
+ const t = mapping.ownership.tuple, key = yield* oauthIdentityKey(identity);
257
+ const row = (yield* owner.read(t.table, equal(t.table, { [t.identityKey]: key }), { limit: 1 })).rows[0];
258
+ if (row !== void 0) invariant(row[t.provider] === identity.provider && row[t.issuer] === identity.issuer && row[t.externalSubject] === identity.subject && [
259
+ "Owned",
260
+ "Unowned",
261
+ "Reserved"
262
+ ].includes(row[t.state]));
263
+ if (mapping.ownership.mode === "separate") {
264
+ const o = mapping.ownership.external;
265
+ const external = yield* owner.read(o.table, equal(o.table, { [o.identityKey]: key }), { limit: 1 });
266
+ if (row?.[t.state] === "Owned") {
267
+ const e = external.rows[0];
268
+ invariant(e !== void 0 && e[o.provider] === identity.provider && e[o.issuer] === identity.issuer && e[o.externalSubject] === identity.subject && mapping.subjectId.equals(o.decodeSubjectId(copiedRow(e)), row[t.subjectId]));
269
+ const condition = sql`exists(select 1 from ${o.table} where ${both(equal(o.table, { [o.identityKey]: key }), o.ownedCondition)})`;
270
+ invariant(yield* owner.check(condition));
271
+ owner.postconditions.push(condition);
272
+ } else invariant(external.rows.length === 0);
273
+ }
274
+ return {
275
+ key,
276
+ row
277
+ };
278
+ });
279
+ return {
280
+ pending,
281
+ live,
282
+ expected,
283
+ prepare,
284
+ issue,
285
+ inspectPrepared,
286
+ sameTarget,
287
+ capture,
288
+ preflight,
289
+ claim,
290
+ exact,
291
+ terminal,
292
+ inspectTuple,
293
+ inspectGrant: Effect.fn("oauthConnected.inspectGrant")(function* (mapping, input) {
294
+ const c = input.claim.flow.context;
295
+ if (input.identity.provider !== c.provider || input.identity.issuer !== c.issuer) return { _tag: "Rejected" };
296
+ const found = yield* S.current(mapping, c.revision.subjectId);
297
+ if (found === void 0 || !sameRevision(found.revision, c.revision)) return { _tag: "Rejected" };
298
+ const cl = yield* S.client(mapping, S.configuration(input.claim.flow), false);
299
+ if (cl === void 0) return { _tag: "Rejected" };
300
+ const tuple = yield* inspectTuple(mapping, input.identity), t = mapping.ownership.tuple;
301
+ if (tuple.row?.[t.state] === "Reserved" || tuple.row?.[t.state] === "Owned" && !mapping.subjectId.equals(tuple.row[t.subjectId], found.nativeId)) return { _tag: "Conflict" };
302
+ const co = yield* S.cohort(mapping, cl.id, tuple.key, false);
303
+ const claimed = yield* exact(mapping, input.claim);
304
+ if (claimed === void 0) return { _tag: "Rejected" };
305
+ if (c.reconnect !== void 0 && !sameIdentity(c.reconnect.identity, input.identity)) return { _tag: "Conflict" };
306
+ return co.blocked || S.orderNumber(input.claim.order) <= co.cutoff || !claimed.active ? {
307
+ _tag: "Quarantine",
308
+ cohortGeneration: co.generation
309
+ } : {
310
+ _tag: "Target",
311
+ cohortGeneration: co.generation
312
+ };
313
+ }),
314
+ acquireTuple,
315
+ readTuple
316
+ };
317
+ });
318
+ var OAuthConnectedFlow = class OAuthConnectedFlow extends Context.Service()("effect-auth/persistence/OAuthConnectedFlow") {
319
+ static layer = Layer.effect(OAuthConnectedFlow, make);
320
+ };
321
+ //#endregion
322
+ export { OAuthConnectedFlow };
323
+
324
+ //# sourceMappingURL=connected-flow.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"connected-flow.mjs","names":[],"sources":["../../../src/internal/oauth/connected-flow.ts"],"sourcesContent":["import {\n OAuthAccountRevision,\n snapshotOAuthSync,\n type OAuthConnectedPersistence,\n} from \"@yielded/auth/OAuth\";\nimport * as M from \"@yielded/auth/OAuth\";\nimport { Effect, Context, Layer } from \"effect\";\n\nimport { OAuthConnectedState } from \"./connected-state\";\nimport type * as STypes from \"./connected-state\";\nimport { CurrentOAuthTransaction } from \"./owner\";\nimport { OAuthQueryCompiler } from \"./query-compiler\";\nimport { OAuthRegistration } from \"./registration\";\nimport { digest, invariant, oauthIdentityKey, sameIdentity, sameRevision } from \"./state\";\n\nexport type Input<K extends keyof OAuthConnectedPersistence[\"Service\"]> = Parameters<\n OAuthConnectedPersistence[\"Service\"][K]\n>[0];\n\nconst make = Effect.gen(function* () {\n const operations = yield* OAuthQueryCompiler;\n const S = yield* OAuthConnectedState;\n const registration = yield* OAuthRegistration;\n\n const { sql } = operations;\n const { both, copiedRow, equal } = operations;\n const { acquireTuple, readTuple } = registration;\n\n const pending = Effect.fn(\"oauthConnected.pending\")(function* (\n mapping: STypes.Authority,\n access: M.OAuthConnectedAccess,\n lock = true,\n ) {\n const owner = yield* CurrentOAuthTransaction;\n const f = mapping.flow;\n\n const read = yield* owner.read(\n f.table,\n equal(f.table, { [f.moduleId]: access.moduleId, [f.flowId]: access.flowId }),\n { limit: 1, lock, observe: lock },\n );\n\n const row = read.rows[0];\n\n if (row === undefined || row[f.snapshot] === null) return undefined;\n\n const flow = S.flowStorage.decode(row[f.snapshot]),\n c = flow.context;\n\n const native = yield* mapping.subjectId.toNative(c.revision.subjectId);\n\n invariant(\n row[f.moduleId] === c.moduleId &&\n row[f.flowId] === c.flowId &&\n row[f.commandId] === c.commandId &&\n mapping.subjectId.equals(native, row[f.subjectId]) &&\n row[f.clientKey] === (yield* S.clientKey(S.configuration(flow))) &&\n row[f.stateDigest] === c.stateDigest &&\n mapping.clock.decodeInstant(row[f.expiresAt]) === c.expiresAtMillis &&\n mapping.clock.decodeInstant(row[f.retentionUntil]) === flow.retentionUntilMillis,\n );\n\n const matches =\n c.moduleId === access.moduleId &&\n c.generation === access.generation &&\n c.flowId === access.flowId &&\n c.revision.subjectId === access.subjectId &&\n c.provider === access.provider &&\n c.callbackId === access.callbackId &&\n c.stateDigest === access.stateDigest &&\n c.requestBindingVerifier === access.requestBindingVerifier &&\n c.requestBindingExpiresAtMillis === access.requestBindingExpiresAtMillis &&\n (c.responseIssuerMode === \"required\"\n ? access.responseIssuer === c.issuer\n : access.responseIssuer === undefined);\n\n return matches ? { row, flow, native } : undefined;\n });\n\n const live = (flow: M.OAuthConnectedPendingFlow, now: number) =>\n flow.context.issuedAtMillis <= now &&\n now < flow.context.expiresAtMillis &&\n flow.context.expiresAtMillis <= flow.context.requestBindingExpiresAtMillis &&\n flow.retentionUntilMillis >= flow.context.expiresAtMillis + flow.context.claimLifetimeMillis;\n\n const expected = (flow: M.OAuthConnectedPendingFlow) => ({\n moduleId: flow.context.moduleId,\n flowId: flow.context.flowId,\n commandId: flow.context.commandId,\n revision: flow.context.revision,\n intent: S.contextStorage.encode(flow.context),\n maximumAgeMillis: flow.context.maximumEvidenceAgeMillis,\n configuration: S.configuration(flow),\n });\n\n const retain = Effect.fn(\"oauthConnected.retain\")(function* (\n mapping: STypes.Mapping,\n input:\n | ({ readonly _tag: \"Prepare\" } & Input<\"prepare\">)\n | ({ readonly _tag: \"Issue\" } & Input<\"issue\">),\n ) {\n const owner = yield* CurrentOAuthTransaction;\n\n const flow = snapshotOAuthSync(M.OAuthConnectedPendingFlow, input.flow),\n c = flow.context;\n\n const found = yield* S.current(mapping, c.revision.subjectId);\n\n if (found === undefined) return { _tag: \"Rejected\" } as const;\n if (input._tag === \"Prepare\") {\n if (\n input.authorization.moduleId !== c.moduleId ||\n !sameRevision(input.authorization.revision, c.revision) ||\n !sameRevision(found.revision, c.revision) ||\n (yield* S.useAuthority(mapping, input.authorization, \"metadata\")) === undefined\n )\n return { _tag: \"Rejected\" } as const;\n } else if (!(yield* S.action(mapping, found, input.authorization, \"issue\", expected(flow))))\n return { _tag: \"Rejected\" } as const;\n if (c.profile.revocation === \"cohort\") invariant(mapping.revocation.mode === \"cohort\");\n const client = yield* S.client(mapping, S.configuration(flow), true);\n\n invariant(client !== undefined);\n\n const now = yield* owner.now(mapping.clock),\n f = mapping.flow;\n\n if (!live(flow, now)) return { _tag: \"Rejected\" } as const;\n if (c.reconnect !== undefined) {\n const original = yield* S.readGrant(mapping, c.moduleId, c.reconnect.grantId, false);\n\n if (\n original === undefined ||\n original.context.subjectId !== c.revision.subjectId ||\n !sameTarget(c.reconnect, original.context)\n )\n return { _tag: \"Rejected\" } as const;\n owner.postconditions.push(\n sql`exists(select 1 from ${mapping.grant.table} where ${owner.exact(mapping.grant.table, original.row)})`,\n );\n }\n const key = { [f.moduleId]: c.moduleId, [f.flowId]: c.flowId };\n const prior = yield* owner.read(f.table, equal(f.table, key), { limit: 1 });\n\n const command = yield* owner.read(\n f.table,\n equal(f.table, { [f.moduleId]: c.moduleId, [f.commandId]: c.commandId }),\n { limit: 1 },\n );\n\n owner.postconditions.push(\n sql`${mapping.clock.engineNowMillis} >= ${c.issuedAtMillis} and ${mapping.clock.engineNowMillis} < ${c.expiresAtMillis}`,\n );\n if (input._tag === \"Issue\") {\n const row = prior.rows[0];\n\n if (\n row === undefined ||\n command.rows[0] === undefined ||\n row[f.state] !== \"Prepared\" ||\n row[f.snapshot] !== S.flowStorage.encode(flow) ||\n command.rows[0][f.flowId] !== c.flowId\n )\n return { _tag: \"Rejected\" } as const;\n yield* owner.update(\n f.table,\n { ...key, [f.state]: \"Prepared\", [f.version]: row[f.version] },\n { [f.state]: \"Pending\", [f.version]: owner.marker },\n );\n owner.postconditions.push(\n sql`exists(select 1 from ${f.table} where ${equal(f.table, { ...key, [f.state]: \"Pending\", [f.version]: owner.marker })})`,\n );\n\n return { _tag: \"Issued\", flow } as const;\n }\n if (prior.rows.length || command.rows.length) return { _tag: \"Rejected\" } as const;\n\n const values = {\n ...f.encodeInsert({\n flow: snapshotOAuthSync(M.OAuthConnectedPendingFlow, flow),\n subjectId: S.nativeCopy(found.nativeId),\n }),\n ...key,\n [f.commandId]: c.commandId,\n [f.subjectId]: found.nativeId,\n [f.clientKey]: client.id,\n [f.cohortKey]: null,\n [f.state]: \"Prepared\",\n [f.version]: owner.marker,\n [f.stateDigest]: c.stateDigest,\n [f.snapshot]: S.flowStorage.encode(flow),\n [f.claimId]: null,\n [f.claimDigest]: null,\n [f.claimOrder]: null,\n [f.claimedAt]: null,\n [f.claimExpiresAt]: null,\n [f.expiresAt]: mapping.clock.encodeInstant(c.expiresAtMillis),\n [f.retentionUntil]: mapping.clock.encodeInstant(flow.retentionUntilMillis),\n [f.work]: \"None\",\n [f.custody]: null,\n };\n\n const inserted = yield* owner.insert(f.table, values, key, true);\n\n prior.rows = inserted.rows;\n command.rows = inserted.rows;\n if (inserted.rows[0]?.[f.version] !== owner.marker) return { _tag: \"Rejected\" } as const;\n\n return { _tag: \"Issued\", flow } as const;\n });\n\n const prepare = (mapping: STypes.Mapping, input: Input<\"prepare\">) =>\n retain(mapping, { _tag: \"Prepare\", ...input });\n\n const issue = (mapping: STypes.Mapping, input: Input<\"issue\">) =>\n retain(mapping, { _tag: \"Issue\", ...input });\n\n const inspectPrepared = Effect.fn(\"oauthConnected.inspectPrepared\")(function* (\n mapping: STypes.Mapping,\n access: M.OAuthConnectedPreparedAccess,\n ) {\n const owner = yield* CurrentOAuthTransaction;\n const found = yield* S.current(mapping, access.subjectId);\n\n if (found === undefined) return undefined;\n const f = mapping.flow;\n\n const read = yield* owner.read(\n f.table,\n equal(f.table, {\n [f.moduleId]: access.moduleId,\n [f.flowId]: access.flowId,\n [f.subjectId]: found.nativeId,\n }),\n { limit: 1 },\n );\n\n const row = read.rows[0];\n\n if (row === undefined || row[f.state] !== \"Prepared\" || row[f.snapshot] === null)\n return undefined;\n const flow = S.flowStorage.decode(row[f.snapshot]);\n const c = flow.context;\n\n invariant(\n row[f.moduleId] === c.moduleId &&\n row[f.flowId] === c.flowId &&\n row[f.commandId] === c.commandId &&\n mapping.subjectId.equals(found.nativeId, row[f.subjectId]) &&\n row[f.clientKey] === (yield* S.clientKey(S.configuration(flow))) &&\n row[f.stateDigest] === c.stateDigest &&\n mapping.clock.decodeInstant(row[f.expiresAt]) === c.expiresAtMillis &&\n mapping.clock.decodeInstant(row[f.retentionUntil]) === flow.retentionUntilMillis,\n );\n const now = yield* owner.now(mapping.clock);\n\n return c.moduleId === access.moduleId &&\n c.generation === access.generation &&\n c.flowId === access.flowId &&\n c.commandId === access.commandId &&\n c.revision.subjectId === access.subjectId &&\n sameRevision(found.revision, c.revision) &&\n c.requestBindingVerifier === access.requestBindingVerifier &&\n c.requestBindingExpiresAtMillis === access.requestBindingExpiresAtMillis &&\n live(flow, now)\n ? flow\n : undefined;\n });\n\n const sameTarget = (target: M.OAuthConnectedTarget, context: M.OAuthConnectedTokenContext) =>\n target.grantId === context.grantId &&\n target.grantVersion === context.grantVersion &&\n target.tokenVersion === context.tokenVersion &&\n target.cohortGeneration === context.cohortGeneration &&\n sameIdentity(target.identity, context.identity) &&\n S.tokenContextStorage.encode({ ...context, configuration: target.configuration }) ===\n S.tokenContextStorage.encode(context);\n\n const capture = Effect.fn(\"oauthConnected.capture\")(function* (\n mapping: STypes.Mapping,\n input: Input<\"capture\">,\n ) {\n const found = yield* S.current(mapping, input.subjectId);\n\n if (found === undefined) return undefined;\n if (input.grantId === undefined)\n return { revision: snapshotOAuthSync(OAuthAccountRevision, found.revision) };\n const grant = yield* S.readGrant(mapping, input.moduleId, input.grantId, false);\n\n if (grant === undefined || grant.context.subjectId !== input.subjectId) return undefined;\n\n return {\n revision: snapshotOAuthSync(OAuthAccountRevision, found.revision),\n target: snapshotOAuthSync(M.OAuthConnectedTarget, grant.context),\n };\n });\n\n const preflight = Effect.fn(\"oauthConnected.preflight\")(function* (\n mapping: STypes.Mapping,\n input: Input<\"preflight\">,\n ) {\n const owner = yield* CurrentOAuthTransaction;\n const read = yield* pending(mapping, input);\n const now = yield* owner.now(mapping.clock);\n\n if (read === undefined || read.row[mapping.flow.state] !== \"Pending\" || !live(read.flow, now))\n return undefined;\n\n return read.flow;\n });\n\n const claim = Effect.fn(\"oauthConnected.claim\")(function* (\n mapping: STypes.Mapping,\n input: Input<\"claim\">,\n ) {\n const owner = yield* CurrentOAuthTransaction;\n\n const flow = snapshotOAuthSync(M.OAuthConnectedPendingFlow, input.flow),\n c = flow.context;\n\n const found = yield* S.current(mapping, c.revision.subjectId);\n\n if (\n found === undefined ||\n !(yield* S.action(mapping, found, input.authorization, \"claim\", expected(flow)))\n )\n return { _tag: \"Rejected\" } as const;\n const client = yield* S.client(mapping, S.configuration(flow), false);\n\n if (client === undefined) return { _tag: \"Rejected\" } as const;\n\n const read = yield* pending(mapping, input.access),\n now = yield* owner.now(mapping.clock),\n f = mapping.flow;\n\n if (\n read === undefined ||\n read.row[f.state] !== \"Pending\" ||\n S.flowStorage.encode(read.flow) !== S.flowStorage.encode(flow) ||\n !live(flow, now)\n )\n return { _tag: \"Rejected\" } as const;\n const expires = now + c.claimLifetimeMillis;\n\n invariant(Number.isSafeInteger(expires) && expires <= flow.retentionUntilMillis);\n yield* S.touchScope(mapping, client.scope);\n const order = yield* S.nextOrder(mapping, client);\n\n const claimed = snapshotOAuthSync(M.OAuthConnectedClaim, {\n flow,\n claimId: input.claimId,\n claimedAtMillis: now,\n claimExpiresAtMillis: expires,\n order: String(order),\n });\n\n yield* owner.update(\n f.table,\n {\n [f.moduleId]: c.moduleId,\n [f.flowId]: c.flowId,\n [f.state]: \"Pending\",\n [f.version]: read.row[f.version],\n },\n {\n [f.state]: \"Claimed\",\n [f.claimId]: claimed.claimId,\n [f.claimDigest]: yield* digest(S.claimStorage.encode(claimed)),\n [f.claimOrder]: mapping.order.encode(order),\n [f.claimedAt]: mapping.clock.encodeInstant(now),\n [f.claimExpiresAt]: mapping.clock.encodeInstant(expires),\n [f.work]: \"Unresolved\",\n [f.version]: owner.marker,\n },\n );\n owner.postconditions.push(\n sql`${mapping.clock.engineNowMillis} >= ${now} and ${mapping.clock.engineNowMillis} < ${Math.min(c.expiresAtMillis, expires)}`,\n );\n\n return { _tag: \"Claimed\", claim: claimed } as const;\n });\n\n const exact = Effect.fn(\"oauthConnected.exactClaim\")(function* (\n mapping: STypes.Authority,\n claim: M.OAuthConnectedClaim,\n ) {\n const owner = yield* CurrentOAuthTransaction;\n\n const f = mapping.flow,\n c = claim.flow.context;\n\n const read = yield* owner.read(\n f.table,\n equal(f.table, { [f.moduleId]: c.moduleId, [f.flowId]: c.flowId }),\n { limit: 1 },\n );\n\n const row = read.rows[0];\n\n if (\n row === undefined ||\n row[f.work] !== \"Unresolved\" ||\n row[f.claimId] !== claim.claimId ||\n row[f.claimDigest] !== (yield* digest(S.claimStorage.encode(claim)))\n )\n return undefined;\n const native = yield* mapping.subjectId.toNative(c.revision.subjectId);\n\n invariant(\n row[f.commandId] === c.commandId &&\n mapping.subjectId.equals(native, row[f.subjectId]) &&\n row[f.stateDigest] === c.stateDigest &&\n mapping.clock.decodeInstant(row[f.expiresAt]) === c.expiresAtMillis &&\n mapping.clock.decodeInstant(row[f.retentionUntil]) === claim.flow.retentionUntilMillis &&\n row[f.clientKey] === (yield* S.clientKey(S.configuration(claim.flow))) &&\n S.nativeOrder(mapping, row[f.claimOrder]) === S.orderNumber(claim.order) &&\n mapping.clock.decodeInstant(row[f.claimedAt]) === claim.claimedAtMillis &&\n mapping.clock.decodeInstant(row[f.claimExpiresAt]) === claim.claimExpiresAtMillis,\n );\n const now = yield* owner.now(mapping.clock);\n\n invariant(now >= claim.claimedAtMillis);\n\n return { row, now, active: row[f.state] === \"Claimed\" && now < claim.claimExpiresAtMillis };\n });\n\n const terminal = Effect.fn(\"oauthConnected.terminal\")(function* (\n mapping: STypes.Authority,\n claim: M.OAuthConnectedClaim,\n state: string,\n work: \"Resolved\" | \"Unresolved\",\n custody?: { cohort: string; sealed?: M.OAuthConnectedStoredGrant },\n ) {\n const owner = yield* CurrentOAuthTransaction;\n\n const f = mapping.flow,\n c = claim.flow.context;\n\n yield* owner.update(\n f.table,\n { [f.moduleId]: c.moduleId, [f.flowId]: c.flowId, [f.claimId]: claim.claimId },\n {\n [f.state]: state,\n [f.snapshot]: null,\n [f.work]: work,\n [f.cohortKey]: custody?.cohort ?? null,\n [f.custody]: custody?.sealed === undefined ? null : S.grantStorage.encode(custody.sealed),\n [f.version]: owner.marker,\n },\n );\n });\n\n const inspectTuple = Effect.fn(\"oauthConnected.inspectTuple\")(function* (\n mapping: STypes.Authority,\n identity: M.OAuthConnectedTokenContext[\"identity\"],\n ) {\n const owner = yield* CurrentOAuthTransaction;\n\n const t = mapping.ownership.tuple,\n key = yield* oauthIdentityKey(identity);\n\n const read = yield* owner.read(t.table, equal(t.table, { [t.identityKey]: key }), { limit: 1 });\n const row = read.rows[0];\n\n if (row !== undefined) {\n invariant(\n row[t.provider] === identity.provider &&\n row[t.issuer] === identity.issuer &&\n row[t.externalSubject] === identity.subject &&\n [\"Owned\", \"Unowned\", \"Reserved\"].includes(row[t.state]),\n );\n }\n if (mapping.ownership.mode === \"separate\") {\n const o = mapping.ownership.external;\n\n const external = yield* owner.read(o.table, equal(o.table, { [o.identityKey]: key }), {\n limit: 1,\n });\n\n if (row?.[t.state] === \"Owned\") {\n const e = external.rows[0];\n\n invariant(\n e !== undefined &&\n e[o.provider] === identity.provider &&\n e[o.issuer] === identity.issuer &&\n e[o.externalSubject] === identity.subject &&\n mapping.subjectId.equals(o.decodeSubjectId(copiedRow(e)), row[t.subjectId]),\n );\n const condition = sql`exists(select 1 from ${o.table} where ${both(equal(o.table, { [o.identityKey]: key }), o.ownedCondition)})`;\n\n invariant(yield* owner.check(condition));\n owner.postconditions.push(condition);\n } else invariant(external.rows.length === 0);\n }\n\n return { key, row };\n });\n\n const inspectGrant = Effect.fn(\"oauthConnected.inspectGrant\")(function* (\n mapping: STypes.Mapping,\n input: Input<\"inspectGrant\">,\n ) {\n const c = input.claim.flow.context;\n\n if (input.identity.provider !== c.provider || input.identity.issuer !== c.issuer)\n return { _tag: \"Rejected\" } as const;\n const found = yield* S.current(mapping, c.revision.subjectId);\n\n if (found === undefined || !sameRevision(found.revision, c.revision))\n return { _tag: \"Rejected\" } as const;\n const cl = yield* S.client(mapping, S.configuration(input.claim.flow), false);\n\n if (cl === undefined) return { _tag: \"Rejected\" } as const;\n\n const tuple = yield* inspectTuple(mapping, input.identity),\n t = mapping.ownership.tuple;\n\n if (\n tuple.row?.[t.state] === \"Reserved\" ||\n (tuple.row?.[t.state] === \"Owned\" &&\n !mapping.subjectId.equals(tuple.row[t.subjectId], found.nativeId))\n )\n return { _tag: \"Conflict\" } as const;\n const co = yield* S.cohort(mapping, cl.id, tuple.key, false);\n const claimed = yield* exact(mapping, input.claim);\n\n if (claimed === undefined) return { _tag: \"Rejected\" } as const;\n if (c.reconnect !== undefined && !sameIdentity(c.reconnect.identity, input.identity))\n return { _tag: \"Conflict\" } as const;\n\n return co.blocked || S.orderNumber(input.claim.order) <= co.cutoff || !claimed.active\n ? ({ _tag: \"Quarantine\", cohortGeneration: co.generation } as const)\n : ({ _tag: \"Target\", cohortGeneration: co.generation } as const);\n });\n\n return {\n pending,\n live,\n expected,\n prepare,\n issue,\n inspectPrepared,\n sameTarget,\n capture,\n preflight,\n claim,\n exact,\n terminal,\n inspectTuple,\n inspectGrant,\n acquireTuple,\n readTuple,\n };\n});\n\nexport class OAuthConnectedFlow extends Context.Service<\n OAuthConnectedFlow,\n Effect.Success<typeof make>\n>()(\"effect-auth/persistence/OAuthConnectedFlow\") {\n static readonly layer = Layer.effect(OAuthConnectedFlow, make);\n}\n"],"mappings":";;;;;;;;;;AAmBA,MAAM,OAAO,OAAO,IAAI,aAAa;CACnC,MAAM,aAAa,OAAO;CAC1B,MAAM,IAAI,OAAO;CACjB,MAAM,eAAe,OAAO;CAE5B,MAAM,EAAE,QAAQ;CAChB,MAAM,EAAE,MAAM,WAAW,UAAU;CACnC,MAAM,EAAE,cAAc,cAAc;CAEpC,MAAM,UAAU,OAAO,GAAG,wBAAwB,CAAC,CAAC,WAClD,SACA,QACA,OAAO,MACP;EACA,MAAM,QAAQ,OAAO;EACrB,MAAM,IAAI,QAAQ;EAQlB,MAAM,OAAM,OANQ,MAAM,KACxB,EAAE,OACF,MAAM,EAAE,OAAO;IAAG,EAAE,WAAW,OAAO;IAAW,EAAE,SAAS,OAAO;EAAO,CAAC,GAC3E;GAAE,OAAO;GAAG;GAAM,SAAS;EAAK,CAClC,EAAA,CAEiB,KAAK;EAEtB,IAAI,QAAQ,KAAA,KAAa,IAAI,EAAE,cAAc,MAAM,OAAO,KAAA;EAE1D,MAAM,OAAO,EAAE,YAAY,OAAO,IAAI,EAAE,SAAS,GAC/C,IAAI,KAAK;EAEX,MAAM,SAAS,OAAO,QAAQ,UAAU,SAAS,EAAE,SAAS,SAAS;EAErE,UACE,IAAI,EAAE,cAAc,EAAE,YACpB,IAAI,EAAE,YAAY,EAAE,UACpB,IAAI,EAAE,eAAe,EAAE,aACvB,QAAQ,UAAU,OAAO,QAAQ,IAAI,EAAE,UAAU,KACjD,IAAI,EAAE,gBAAgB,OAAO,EAAE,UAAU,EAAE,cAAc,IAAI,CAAC,MAC9D,IAAI,EAAE,iBAAiB,EAAE,eACzB,QAAQ,MAAM,cAAc,IAAI,EAAE,UAAU,MAAM,EAAE,mBACpD,QAAQ,MAAM,cAAc,IAAI,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