@yielded/auth-persistence-drizzle 0.1.0-beta.23 → 0.1.0-beta.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (185) hide show
  1. package/README.md +3 -0
  2. package/dist/D1.d.mts +9 -9
  3. package/dist/D1.mjs +2 -1
  4. package/dist/D1.mjs.map +1 -1
  5. package/dist/Libsql.d.mts +46 -62
  6. package/dist/Libsql.mjs +2 -1
  7. package/dist/Libsql.mjs.map +1 -1
  8. package/dist/Mysql2.d.mts +24 -25
  9. package/dist/Pglite.d.mts +26 -26
  10. package/dist/Pglite.mjs +2 -1
  11. package/dist/Pglite.mjs.map +1 -1
  12. package/dist/Postgres.d.mts +27 -27
  13. package/dist/Postgres.mjs +2 -1
  14. package/dist/Postgres.mjs.map +1 -1
  15. package/dist/SqliteBun.d.mts +47 -63
  16. package/dist/SqliteBun.mjs +2 -1
  17. package/dist/SqliteBun.mjs.map +1 -1
  18. package/dist/SqliteDo.d.mts +45 -61
  19. package/dist/SqliteDo.mjs +2 -1
  20. package/dist/SqliteDo.mjs.map +1 -1
  21. package/dist/SqliteNode.d.mts +49 -65
  22. package/dist/SqliteNode.mjs +2 -1
  23. package/dist/SqliteNode.mjs.map +1 -1
  24. package/dist/SqliteWasm.d.mts +45 -61
  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 +13 -13
  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 +17 -33
  34. package/dist/drizzle/d1-passwords.mjs +56 -198
  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 +1 -1
  38. package/dist/drizzle/d1-sessions.d.mts +2 -2
  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 +26 -1
  48. package/dist/drizzle/native-sql-table.mjs.map +1 -1
  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 -6
  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/pg-password-prepared.d.mts +5 -5
  76. package/dist/drizzle/pg-password-prepared.mjs.map +1 -1
  77. package/dist/drizzle/pg-passwords.d.mts +16 -32
  78. package/dist/drizzle/pg-passwords.mjs.map +1 -1
  79. package/dist/drizzle/pg-sessions.d.mts +1 -1
  80. package/dist/drizzle/phone-target.d.mts +1 -1
  81. package/dist/drizzle/phone-target.mjs +1 -1
  82. package/dist/drizzle/query-operations.mjs +3 -1
  83. package/dist/drizzle/query-operations.mjs.map +1 -1
  84. package/dist/drizzle/sqlite-password-prepared.mjs.map +1 -1
  85. package/dist/drizzle/sqlite-passwords.mjs.map +1 -1
  86. package/dist/drizzle/storage-validation.mjs +10 -185
  87. package/dist/drizzle/storage-validation.mjs.map +1 -1
  88. package/dist/drizzle/totp-target.d.mts +2 -2
  89. package/dist/drizzle/totp-target.mjs +2 -2
  90. package/dist/drizzle/transaction-execution.d.mts +2 -2
  91. package/dist/drizzle/transaction-owner.d.mts +1 -2
  92. package/dist/index.d.mts +3 -2
  93. package/dist/internal/drizzle-postgres-oauth-proxy.d.mts +138 -0
  94. package/dist/internal/drizzle-postgres-oauth-proxy.mjs +29 -0
  95. package/dist/internal/drizzle-postgres-oauth-proxy.mjs.map +1 -0
  96. package/dist/internal/drizzle-sqlite-oauth-proxy.d.mts +146 -0
  97. package/dist/internal/drizzle-sqlite-oauth-proxy.mjs +29 -0
  98. package/dist/internal/drizzle-sqlite-oauth-proxy.mjs.map +1 -0
  99. package/package.json +1 -1
  100. package/src/D1.ts +2 -0
  101. package/src/Libsql.ts +2 -0
  102. package/src/Pglite.ts +2 -0
  103. package/src/Postgres.ts +2 -0
  104. package/src/SqliteBun.ts +2 -0
  105. package/src/SqliteDo.ts +2 -0
  106. package/src/SqliteNode.ts +2 -0
  107. package/src/SqliteWasm.ts +2 -0
  108. package/src/drizzle/d1-password-prepared.ts +2 -5
  109. package/src/drizzle/d1-passwords.ts +123 -458
  110. package/src/drizzle/d1-sessions.ts +21 -6
  111. package/src/drizzle/mysql-password-prepared.ts +4 -8
  112. package/src/drizzle/mysql-passwords.ts +5 -10
  113. package/src/drizzle/native-sql-table.ts +30 -0
  114. package/src/drizzle/oauth-connected-model.ts +97 -315
  115. package/src/drizzle/oauth-connected-reference.ts +13 -93
  116. package/src/drizzle/oauth-connected-target.ts +14 -271
  117. package/src/drizzle/oauth-kernel.ts +10 -0
  118. package/src/drizzle/oauth-model.ts +188 -541
  119. package/src/drizzle/oauth-mysql.ts +2 -2
  120. package/src/drizzle/oauth-proxy.ts +101 -0
  121. package/src/drizzle/oauth-target.ts +33 -737
  122. package/src/drizzle/password-model.ts +0 -13
  123. package/src/drizzle/password-prepared-model.ts +0 -6
  124. package/src/drizzle/pg-password-prepared.ts +4 -8
  125. package/src/drizzle/pg-passwords.ts +5 -10
  126. package/src/drizzle/query-operations.ts +4 -0
  127. package/src/drizzle/sqlite-password-prepared.ts +4 -8
  128. package/src/drizzle/sqlite-passwords.ts +7 -14
  129. package/src/drizzle/storage-validation.ts +13 -214
  130. package/src/index.ts +2 -4
  131. package/src/internal/drizzle-postgres-oauth-proxy.ts +37 -0
  132. package/src/internal/drizzle-sqlite-oauth-proxy.ts +44 -0
  133. package/dist/_virtual/_rolldown/runtime.mjs +0 -13
  134. package/dist/drizzle/oauth-accounts.mjs +0 -359
  135. package/dist/drizzle/oauth-accounts.mjs.map +0 -1
  136. package/dist/drizzle/oauth-connected-access.mjs +0 -212
  137. package/dist/drizzle/oauth-connected-access.mjs.map +0 -1
  138. package/dist/drizzle/oauth-connected-collection.mjs +0 -202
  139. package/dist/drizzle/oauth-connected-collection.mjs.map +0 -1
  140. package/dist/drizzle/oauth-connected-custody.mjs +0 -147
  141. package/dist/drizzle/oauth-connected-custody.mjs.map +0 -1
  142. package/dist/drizzle/oauth-connected-flow.mjs +0 -297
  143. package/dist/drizzle/oauth-connected-flow.mjs.map +0 -1
  144. package/dist/drizzle/oauth-connected-input.mjs +0 -122
  145. package/dist/drizzle/oauth-connected-input.mjs.map +0 -1
  146. package/dist/drizzle/oauth-connected-maintenance.mjs +0 -214
  147. package/dist/drizzle/oauth-connected-maintenance.mjs.map +0 -1
  148. package/dist/drizzle/oauth-connected-management.mjs +0 -229
  149. package/dist/drizzle/oauth-connected-management.mjs.map +0 -1
  150. package/dist/drizzle/oauth-connected-settlement.mjs +0 -128
  151. package/dist/drizzle/oauth-connected-settlement.mjs.map +0 -1
  152. package/dist/drizzle/oauth-connected-sign-in.mjs +0 -265
  153. package/dist/drizzle/oauth-connected-sign-in.mjs.map +0 -1
  154. package/dist/drizzle/oauth-connected-state.d.mts +0 -2062
  155. package/dist/drizzle/oauth-connected-state.mjs +0 -430
  156. package/dist/drizzle/oauth-connected-state.mjs.map +0 -1
  157. package/dist/drizzle/oauth-flow.d.mts +0 -22
  158. package/dist/drizzle/oauth-flow.mjs +0 -253
  159. package/dist/drizzle/oauth-flow.mjs.map +0 -1
  160. package/dist/drizzle/oauth-input.mjs +0 -127
  161. package/dist/drizzle/oauth-input.mjs.map +0 -1
  162. package/dist/drizzle/oauth-owner.d.mts +0 -10
  163. package/dist/drizzle/oauth-owner.mjs +0 -10
  164. package/dist/drizzle/oauth-owner.mjs.map +0 -1
  165. package/dist/drizzle/oauth-registration.d.mts +0 -5
  166. package/dist/drizzle/oauth-registration.mjs +0 -374
  167. package/dist/drizzle/oauth-registration.mjs.map +0 -1
  168. package/dist/drizzle/oauth-state.mjs +0 -107
  169. package/dist/drizzle/oauth-state.mjs.map +0 -1
  170. package/src/drizzle/oauth-accounts.ts +0 -805
  171. package/src/drizzle/oauth-connected-access.ts +0 -361
  172. package/src/drizzle/oauth-connected-collection.ts +0 -324
  173. package/src/drizzle/oauth-connected-custody.ts +0 -279
  174. package/src/drizzle/oauth-connected-flow.ts +0 -529
  175. package/src/drizzle/oauth-connected-input.ts +0 -147
  176. package/src/drizzle/oauth-connected-maintenance.ts +0 -346
  177. package/src/drizzle/oauth-connected-management.ts +0 -365
  178. package/src/drizzle/oauth-connected-settlement.ts +0 -251
  179. package/src/drizzle/oauth-connected-sign-in.ts +0 -443
  180. package/src/drizzle/oauth-connected-state.ts +0 -872
  181. package/src/drizzle/oauth-flow.ts +0 -462
  182. package/src/drizzle/oauth-input.ts +0 -201
  183. package/src/drizzle/oauth-owner.ts +0 -29
  184. package/src/drizzle/oauth-registration.ts +0 -640
  185. package/src/drizzle/oauth-state.ts +0 -218
@@ -1,147 +0,0 @@
1
- import { invariant, oauthIdentityKey } from "./oauth-state.mjs";
2
- import { both } from "./transaction-owner.mjs";
3
- import { CurrentOAuthTransaction, col, equal } from "./oauth-owner.mjs";
4
- import { clientKey, cohortKey, cohortWork, countCondition, grantStorage, jobStorage, jobTable, nativeCopy, nextOrder, retainedUntil, rowCount, tokenContextStorage, unknownWork } from "./oauth-connected-state.mjs";
5
- import { eq, isNotNull, ne, sql } from "drizzle-orm";
6
- import { Effect } from "effect";
7
- import * as M from "@yielded/auth/OAuth";
8
- import { snapshotOAuthSync } from "@yielded/auth/OAuth";
9
- //#region src/drizzle/oauth-connected-custody.ts
10
- const sameContext = (a, b) => tokenContextStorage.encode(a) === tokenContextStorage.encode(b);
11
- const sameGrant = (a, b) => grantStorage.encode(a) === grantStorage.encode(b);
12
- const summary = (context) => snapshotOAuthSync(M.OAuthConnectedSummary, {
13
- grantId: context.grantId,
14
- provider: context.identity.provider,
15
- issuer: context.identity.issuer,
16
- profileKey: context.configuration.profile.key,
17
- status: "Active",
18
- scopes: context.metadata.scopes,
19
- ...context.metadata.accessExpiresAtMillis === void 0 ? {} : { accessExpiresAtMillis: context.metadata.accessExpiresAtMillis },
20
- useUntilMillis: context.metadata.useUntilMillis,
21
- ...context.metadata.profile === void 0 ? {} : { profile: context.metadata.profile },
22
- remoteRevocation: context.configuration.profile.revocation === "cohort" ? "Pending" : "Unsupported"
23
- });
24
- /** A generation fences every sibling, including those beyond a bounded metadata page. */
25
- const fence = Effect.fn("oauthConnected.fence")(function* (mapping, client, cohort) {
26
- const owner = yield* CurrentOAuthTransaction;
27
- invariant(cohort.row !== void 0);
28
- const g = mapping.grant, h = mapping.cohort;
29
- const active = both(eq(col(g.table, g.cohortKey), cohort.id), isNotNull(col(g.table, g.activeIdentityKey)));
30
- const affected = yield* rowCount(g.table, active);
31
- invariant(yield* owner.check(countCondition(g.table, active, affected)));
32
- const cutoff = yield* nextOrder(mapping, client);
33
- const generation = M.OAuthConnectedTarget.fields.cohortGeneration.make(owner.marker);
34
- yield* owner.update(h.table, { [h.cohortKey]: cohort.id }, {
35
- [h.generation]: generation,
36
- [h.cutoff]: mapping.order.encode(cutoff),
37
- [h.state]: "Blocked",
38
- [h.version]: owner.marker
39
- });
40
- yield* owner.write(owner.database.update(g.table).set({
41
- [g.activeIdentityKey]: null,
42
- [g.state]: "ReauthorizationRequired",
43
- [g.version]: owner.marker
44
- }).where(active));
45
- 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 ? {
46
- ...row,
47
- [g.activeIdentityKey]: null,
48
- [g.state]: "ReauthorizationRequired",
49
- [g.version]: owner.marker
50
- } : row);
51
- owner.postconditions.push(sql`not exists(select 1 from ${g.table} where ${active})`);
52
- cohort.generation = generation;
53
- cohort.cutoff = cutoff;
54
- cohort.blocked = true;
55
- return affected;
56
- });
57
- const storeJob = Effect.fn("oauthConnected.storeJob")(function* (mapping, job, token, native, now) {
58
- const owner = yield* CurrentOAuthTransaction;
59
- invariant(mapping.revocation.mode === "cohort" && token.configuration.profile.revocation === "cohort" && sameContext(job.context.token, token));
60
- const j = mapping.revocation.job, identity = yield* oauthIdentityKey(token.identity), client = yield* clientKey(token.configuration), cohort = yield* cohortKey(client, identity);
61
- const h = mapping.cohort;
62
- owner.postconditions.push(sql`exists(select 1 from ${h.table} where ${both(equal(h.table, {
63
- [h.cohortKey]: cohort,
64
- [h.clientKey]: client,
65
- [h.identityKey]: identity,
66
- [h.state]: "Blocked"
67
- }))})`);
68
- const key = { [j.jobId]: job.context.jobId };
69
- const prior = yield* owner.read(j.table, equal(j.table, key), { limit: 1 });
70
- if (prior.rows.length) {
71
- const row = prior.rows[0];
72
- invariant(row[j.snapshot] === 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 && [
73
- "Pending",
74
- "Claimed",
75
- "Confirmed",
76
- "Unknown"
77
- ].includes(row[j.state]));
78
- 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]));
79
- return;
80
- }
81
- prior.rows = (yield* owner.insert(j.table, {
82
- ...j.encodeInsert({
83
- job: snapshotOAuthSync(M.OAuthConnectedRevocationJob, job),
84
- subjectId: nativeCopy(native)
85
- }),
86
- ...key,
87
- [j.moduleId]: token.moduleId,
88
- [j.subjectId]: native,
89
- [j.identityKey]: identity,
90
- [j.clientKey]: client,
91
- [j.cohortKey]: cohort,
92
- [j.grantId]: token.grantId,
93
- [j.snapshot]: jobStorage.encode(job),
94
- [j.state]: "Pending",
95
- [j.claimId]: null,
96
- [j.claimedAt]: null,
97
- [j.claimExpiresAt]: null,
98
- [j.retentionUntil]: mapping.clock.encodeInstant(retainedUntil(mapping, now)),
99
- [j.version]: owner.marker
100
- }, key)).rows;
101
- });
102
- const noJobs = (mapping, id, except) => {
103
- const j = jobTable(mapping);
104
- 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))})`;
105
- };
106
- const clearableCondition = (mapping, cohortId, clientId) => {
107
- const f = mapping.flow;
108
- return both(sql`not exists(select 1 from ${f.table} where ${unknownWork(mapping, clientId)})`, cohortWork(mapping, cohortId), noJobs(mapping, cohortId));
109
- };
110
- /** Clearing owns a NEW cutoff. Claims made while the remote revoke was pending
111
- * cannot enter the next generation even if they finish after that revoke. */
112
- const clear = Effect.fn("oauthConnected.clear")(function* (mapping, client, cohort, confirmedJob, provenOpenWithoutNewJob = false) {
113
- const owner = yield* CurrentOAuthTransaction;
114
- if (!cohort.blocked || cohort.row === void 0 || jobTable(mapping) === void 0 && !provenOpenWithoutNewJob) return false;
115
- const f = mapping.flow;
116
- const noUnknown = sql`not exists(select 1 from ${f.table} where ${unknownWork(mapping, client.id)})`;
117
- const work = cohortWork(mapping, cohort.id);
118
- if (!(yield* owner.check(both(noUnknown, work, noJobs(mapping, cohort.id, confirmedJob))))) return false;
119
- const cutoff = yield* nextOrder(mapping, client), h = mapping.cohort;
120
- const generation = M.OAuthConnectedTarget.fields.cohortGeneration.make(owner.marker + ":open");
121
- yield* owner.update(h.table, { [h.cohortKey]: cohort.id }, {
122
- [h.generation]: generation,
123
- [h.cutoff]: mapping.order.encode(cutoff),
124
- [h.state]: "Open",
125
- [h.version]: owner.marker
126
- });
127
- owner.postconditions.push(noUnknown, work, noJobs(mapping, cohort.id));
128
- cohort.generation = generation;
129
- cohort.cutoff = cutoff;
130
- cohort.blocked = false;
131
- return true;
132
- });
133
- /** Former local owners remain visible. A foreign historical dependency cannot be
134
- * ignored simply because the global tuple was changed outside this authority. */
135
- const noFormerOwner = (mapping, identity, client, native) => Effect.gen(function* () {
136
- const owner = yield* CurrentOAuthTransaction;
137
- const g = mapping.grant, f = mapping.flow, j = jobTable(mapping);
138
- 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* 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 })})`)})`]);
139
- return yield* Effect.map(owner.check(condition), (accepted) => {
140
- if (accepted) owner.postconditions.push(condition);
141
- return accepted;
142
- });
143
- });
144
- //#endregion
145
- export { clear, clearableCondition, fence, noFormerOwner, sameContext, sameGrant, storeJob, summary };
146
-
147
- //# sourceMappingURL=oauth-connected-custody.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"oauth-connected-custody.mjs","names":["S.rowCount","S.countCondition","S.nextOrder","S.clientKey","S.cohortKey","S.nativeCopy","S.retainedUntil","S.jobTable","S.unknownWork","S.cohortWork"],"sources":["../../src/drizzle/oauth-connected-custody.ts"],"sourcesContent":["import * as M from \"@yielded/auth/OAuth\";\nimport { snapshotOAuthSync } from \"@yielded/auth/OAuth\";\nimport { eq, isNotNull, ne, sql, type SQL } from \"drizzle-orm\";\nimport { Effect } from \"effect\";\n\nimport * as S from \"./oauth-connected-state\";\nimport { both, col, equal, CurrentOAuthTransaction } from \"./oauth-owner\";\nimport { invariant, oauthIdentityKey } from \"./oauth-state\";\n\ntype Client = NonNullable<Effect.Success<ReturnType<typeof S.client>>>;\ntype Cohort = Effect.Success<ReturnType<typeof S.cohort>>;\n\nexport const sameContext = (a: M.OAuthConnectedTokenContext, b: M.OAuthConnectedTokenContext) =>\n S.tokenContextStorage.encode(a) === S.tokenContextStorage.encode(b);\n\nexport const sameGrant = (a: M.OAuthConnectedStoredGrant, b: M.OAuthConnectedStoredGrant) =>\n S.grantStorage.encode(a) === S.grantStorage.encode(b);\n\nexport 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. */\nexport const fence = Effect.fn(\"oauthConnected.fence\")(function* (\n mapping: S.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\nexport const storeJob = Effect.fn(\"oauthConnected.storeJob\")(function* (\n mapping: S.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\nconst noJobs = (mapping: S.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\nexport const clearableCondition = (\n mapping: S.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. */\nexport const clear = Effect.fn(\"oauthConnected.clear\")(function* (\n mapping: S.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. */\nexport const noFormerOwner = (\n mapping: S.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"],"mappings":";;;;;;;;;AAYA,MAAa,eAAe,GAAiC,MAAA,oBACrC,OAAO,CAAC,MAAA,oBAA4B,OAAO,CAAC;AAEpE,MAAa,aAAa,GAAgC,MAAA,aACzC,OAAO,CAAC,MAAA,aAAqB,OAAO,CAAC;AAEtD,MAAa,WAAW,YACtB,kBAAkB,EAAE,uBAAuB;CACzC,SAAS,QAAQ;CACjB,UAAU,QAAQ,SAAS;CAC3B,QAAQ,Q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E,CAAC;CACrE,OAAO,aAAa;CACpB,OAAO,SAAS;CAChB,OAAO,UAAU;CAEjB,OAAO;AACT,CAAC;;;AAID,MAAa,iBACX,SACA,UACA,QACA,WAEA,OAAO,IAAI,aAAa;CACtB,MAAM,QAAQ,OAAO;CAErB,MAAM,IAAI,QAAQ,OAChB,IAAI,QAAQ,MACZ,IAAIK,SAAW,OAAO;CAExB,MAAM,YAAY,KAChB,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,IAC/J,GAAG,4BAA4B,EAAE,MAAM,SAAS,KAAK,GAAG,IAAI,EAAE,OAAO,EAAE,SAAS,GAAG,OAAOH,UAAY,QAAQ,QAAQ,CAAC,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;CAEA,OAAO,OAAO,OAAO,IAAI,MAAM,MAAM,SAAS,IAAI,aAAa;EAC7D,IAAI,UAAU,MAAM,eAAe,KAAK,SAAS;EAEjD,OAAO;CACT,CAAC;AACH,CAAC"}
@@ -1,297 +0,0 @@
1
- import { digest } from "./crypto.mjs";
2
- import { invariant, oauthIdentityKey, sameIdentity, sameRevision } from "./oauth-state.mjs";
3
- import { both } from "./transaction-owner.mjs";
4
- import { CurrentOAuthTransaction, copiedRow, equal } from "./oauth-owner.mjs";
5
- import "./oauth-registration.mjs";
6
- import { action, claimStorage, client, clientKey, cohort, configuration, contextStorage, current, flowStorage, grantStorage, nativeCopy, nativeOrder, nextOrder, orderNumber, readGrant, tokenContextStorage, touchScope, useAuthority } from "./oauth-connected-state.mjs";
7
- import { sql } from "drizzle-orm";
8
- import { Effect } from "effect";
9
- import * as M from "@yielded/auth/OAuth";
10
- import { OAuthAccountRevision, snapshotOAuthSync } from "@yielded/auth/OAuth";
11
- //#region src/drizzle/oauth-connected-flow.ts
12
- const pending = Effect.fn("oauthConnected.pending")(function* (mapping, access, lock = true) {
13
- const owner = yield* CurrentOAuthTransaction;
14
- const f = mapping.flow;
15
- const row = (yield* owner.read(f.table, equal(f.table, {
16
- [f.moduleId]: access.moduleId,
17
- [f.flowId]: access.flowId
18
- }), {
19
- limit: 1,
20
- lock,
21
- observe: lock
22
- })).rows[0];
23
- if (row === void 0 || row[f.snapshot] === null) return void 0;
24
- const flow = flowStorage.decode(row[f.snapshot]), c = flow.context;
25
- const native = yield* mapping.subjectId.toNative(c.revision.subjectId);
26
- 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* clientKey(configuration(flow))) && row[f.stateDigest] === c.stateDigest && mapping.clock.decodeInstant(row[f.expiresAt]) === c.expiresAtMillis && mapping.clock.decodeInstant(row[f.retentionUntil]) === flow.retentionUntilMillis);
27
- 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) ? {
28
- row,
29
- flow,
30
- native
31
- } : void 0;
32
- });
33
- 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;
34
- const expected = (flow) => ({
35
- moduleId: flow.context.moduleId,
36
- flowId: flow.context.flowId,
37
- commandId: flow.context.commandId,
38
- revision: flow.context.revision,
39
- intent: contextStorage.encode(flow.context),
40
- maximumAgeMillis: flow.context.maximumEvidenceAgeMillis,
41
- configuration: configuration(flow)
42
- });
43
- const retain = Effect.fn("oauthConnected.retain")(function* (mapping, input) {
44
- const owner = yield* CurrentOAuthTransaction;
45
- const flow = snapshotOAuthSync(M.OAuthConnectedPendingFlow, input.flow), c = flow.context;
46
- const found = yield* current(mapping, c.revision.subjectId);
47
- if (found === void 0) return { _tag: "Rejected" };
48
- if (input._tag === "Prepare") {
49
- if (input.authorization.moduleId !== c.moduleId || !sameRevision(input.authorization.revision, c.revision) || !sameRevision(found.revision, c.revision) || (yield* useAuthority(mapping, input.authorization, "metadata")) === void 0) return { _tag: "Rejected" };
50
- } else if (!(yield* action(mapping, found, input.authorization, "issue", expected(flow)))) return { _tag: "Rejected" };
51
- if (c.profile.revocation === "cohort") invariant(mapping.revocation.mode === "cohort");
52
- const client$1 = yield* client(mapping, configuration(flow), true);
53
- invariant(client$1 !== void 0);
54
- const now = yield* owner.now(mapping.clock), f = mapping.flow;
55
- if (!live(flow, now)) return { _tag: "Rejected" };
56
- if (c.reconnect !== void 0) {
57
- const original = yield* readGrant(mapping, c.moduleId, c.reconnect.grantId, false);
58
- if (original === void 0 || original.context.subjectId !== c.revision.subjectId || !sameTarget(c.reconnect, original.context)) return { _tag: "Rejected" };
59
- owner.postconditions.push(sql`exists(select 1 from ${mapping.grant.table} where ${owner.exact(mapping.grant.table, original.row)})`);
60
- }
61
- const key = {
62
- [f.moduleId]: c.moduleId,
63
- [f.flowId]: c.flowId
64
- };
65
- const prior = yield* owner.read(f.table, equal(f.table, key), { limit: 1 });
66
- const command = yield* owner.read(f.table, equal(f.table, {
67
- [f.moduleId]: c.moduleId,
68
- [f.commandId]: c.commandId
69
- }), { limit: 1 });
70
- owner.postconditions.push(sql`${mapping.clock.engineNowMillis} >= ${c.issuedAtMillis} and ${mapping.clock.engineNowMillis} < ${c.expiresAtMillis}`);
71
- if (input._tag === "Issue") {
72
- const row = prior.rows[0];
73
- if (row === void 0 || command.rows[0] === void 0 || row[f.state] !== "Prepared" || row[f.snapshot] !== flowStorage.encode(flow) || command.rows[0][f.flowId] !== c.flowId) return { _tag: "Rejected" };
74
- yield* owner.update(f.table, {
75
- ...key,
76
- [f.state]: "Prepared",
77
- [f.version]: row[f.version]
78
- }, {
79
- [f.state]: "Pending",
80
- [f.version]: owner.marker
81
- });
82
- owner.postconditions.push(sql`exists(select 1 from ${f.table} where ${equal(f.table, {
83
- ...key,
84
- [f.state]: "Pending",
85
- [f.version]: owner.marker
86
- })})`);
87
- return {
88
- _tag: "Issued",
89
- flow
90
- };
91
- }
92
- if (prior.rows.length || command.rows.length) return { _tag: "Rejected" };
93
- const values = {
94
- ...f.encodeInsert({
95
- flow: snapshotOAuthSync(M.OAuthConnectedPendingFlow, flow),
96
- subjectId: nativeCopy(found.nativeId)
97
- }),
98
- ...key,
99
- [f.commandId]: c.commandId,
100
- [f.subjectId]: found.nativeId,
101
- [f.clientKey]: client$1.id,
102
- [f.cohortKey]: null,
103
- [f.state]: "Prepared",
104
- [f.version]: owner.marker,
105
- [f.stateDigest]: c.stateDigest,
106
- [f.snapshot]: flowStorage.encode(flow),
107
- [f.claimId]: null,
108
- [f.claimDigest]: null,
109
- [f.claimOrder]: null,
110
- [f.claimedAt]: null,
111
- [f.claimExpiresAt]: null,
112
- [f.expiresAt]: mapping.clock.encodeInstant(c.expiresAtMillis),
113
- [f.retentionUntil]: mapping.clock.encodeInstant(flow.retentionUntilMillis),
114
- [f.work]: "None",
115
- [f.custody]: null
116
- };
117
- const inserted = yield* owner.insert(f.table, values, key, true);
118
- prior.rows = inserted.rows;
119
- command.rows = inserted.rows;
120
- if (inserted.rows[0]?.[f.version] !== owner.marker) return { _tag: "Rejected" };
121
- return {
122
- _tag: "Issued",
123
- flow
124
- };
125
- });
126
- const prepare = (mapping, input) => retain(mapping, {
127
- _tag: "Prepare",
128
- ...input
129
- });
130
- const issue = (mapping, input) => retain(mapping, {
131
- _tag: "Issue",
132
- ...input
133
- });
134
- const inspectPrepared = Effect.fn("oauthConnected.inspectPrepared")(function* (mapping, access) {
135
- const owner = yield* CurrentOAuthTransaction;
136
- const found = yield* current(mapping, access.subjectId);
137
- if (found === void 0) return void 0;
138
- const f = mapping.flow;
139
- const row = (yield* owner.read(f.table, equal(f.table, {
140
- [f.moduleId]: access.moduleId,
141
- [f.flowId]: access.flowId,
142
- [f.subjectId]: found.nativeId
143
- }), { limit: 1 })).rows[0];
144
- if (row === void 0 || row[f.state] !== "Prepared" || row[f.snapshot] === null) return void 0;
145
- const flow = flowStorage.decode(row[f.snapshot]);
146
- const c = flow.context;
147
- 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* clientKey(configuration(flow))) && row[f.stateDigest] === c.stateDigest && mapping.clock.decodeInstant(row[f.expiresAt]) === c.expiresAtMillis && mapping.clock.decodeInstant(row[f.retentionUntil]) === flow.retentionUntilMillis);
148
- const now = yield* owner.now(mapping.clock);
149
- 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;
150
- });
151
- 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) && tokenContextStorage.encode({
152
- ...context,
153
- configuration: target.configuration
154
- }) === tokenContextStorage.encode(context);
155
- const capture = Effect.fn("oauthConnected.capture")(function* (mapping, input) {
156
- const found = yield* current(mapping, input.subjectId);
157
- if (found === void 0) return void 0;
158
- if (input.grantId === void 0) return { revision: snapshotOAuthSync(OAuthAccountRevision, found.revision) };
159
- const grant = yield* readGrant(mapping, input.moduleId, input.grantId, false);
160
- if (grant === void 0 || grant.context.subjectId !== input.subjectId) return void 0;
161
- return {
162
- revision: snapshotOAuthSync(OAuthAccountRevision, found.revision),
163
- target: snapshotOAuthSync(M.OAuthConnectedTarget, grant.context)
164
- };
165
- });
166
- const preflight = Effect.fn("oauthConnected.preflight")(function* (mapping, input) {
167
- const owner = yield* CurrentOAuthTransaction;
168
- const read = yield* pending(mapping, input);
169
- const now = yield* owner.now(mapping.clock);
170
- if (read === void 0 || read.row[mapping.flow.state] !== "Pending" || !live(read.flow, now)) return void 0;
171
- return read.flow;
172
- });
173
- const claim = Effect.fn("oauthConnected.claim")(function* (mapping, input) {
174
- const owner = yield* CurrentOAuthTransaction;
175
- const flow = snapshotOAuthSync(M.OAuthConnectedPendingFlow, input.flow), c = flow.context;
176
- const found = yield* current(mapping, c.revision.subjectId);
177
- if (found === void 0 || !(yield* action(mapping, found, input.authorization, "claim", expected(flow)))) return { _tag: "Rejected" };
178
- const client$2 = yield* client(mapping, configuration(flow), false);
179
- if (client$2 === void 0) return { _tag: "Rejected" };
180
- const read = yield* pending(mapping, input.access), now = yield* owner.now(mapping.clock), f = mapping.flow;
181
- if (read === void 0 || read.row[f.state] !== "Pending" || flowStorage.encode(read.flow) !== flowStorage.encode(flow) || !live(flow, now)) return { _tag: "Rejected" };
182
- const expires = now + c.claimLifetimeMillis;
183
- invariant(Number.isSafeInteger(expires) && expires <= flow.retentionUntilMillis);
184
- yield* touchScope(mapping, client$2.scope);
185
- const order = yield* nextOrder(mapping, client$2);
186
- const claimed = snapshotOAuthSync(M.OAuthConnectedClaim, {
187
- flow,
188
- claimId: input.claimId,
189
- claimedAtMillis: now,
190
- claimExpiresAtMillis: expires,
191
- order: String(order)
192
- });
193
- yield* owner.update(f.table, {
194
- [f.moduleId]: c.moduleId,
195
- [f.flowId]: c.flowId,
196
- [f.state]: "Pending",
197
- [f.version]: read.row[f.version]
198
- }, {
199
- [f.state]: "Claimed",
200
- [f.claimId]: claimed.claimId,
201
- [f.claimDigest]: yield* digest(claimStorage.encode(claimed)),
202
- [f.claimOrder]: mapping.order.encode(order),
203
- [f.claimedAt]: mapping.clock.encodeInstant(now),
204
- [f.claimExpiresAt]: mapping.clock.encodeInstant(expires),
205
- [f.work]: "Unresolved",
206
- [f.version]: owner.marker
207
- });
208
- owner.postconditions.push(sql`${mapping.clock.engineNowMillis} >= ${now} and ${mapping.clock.engineNowMillis} < ${Math.min(c.expiresAtMillis, expires)}`);
209
- return {
210
- _tag: "Claimed",
211
- claim: claimed
212
- };
213
- });
214
- const exact = Effect.fn("oauthConnected.exactClaim")(function* (mapping, claim) {
215
- const owner = yield* CurrentOAuthTransaction;
216
- const f = mapping.flow, c = claim.flow.context;
217
- const row = (yield* owner.read(f.table, equal(f.table, {
218
- [f.moduleId]: c.moduleId,
219
- [f.flowId]: c.flowId
220
- }), { limit: 1 })).rows[0];
221
- if (row === void 0 || row[f.work] !== "Unresolved" || row[f.claimId] !== claim.claimId || row[f.claimDigest] !== (yield* digest(claimStorage.encode(claim)))) return void 0;
222
- const native = yield* mapping.subjectId.toNative(c.revision.subjectId);
223
- 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* clientKey(configuration(claim.flow))) && nativeOrder(mapping, row[f.claimOrder]) === orderNumber(claim.order) && mapping.clock.decodeInstant(row[f.claimedAt]) === claim.claimedAtMillis && mapping.clock.decodeInstant(row[f.claimExpiresAt]) === claim.claimExpiresAtMillis);
224
- const now = yield* owner.now(mapping.clock);
225
- invariant(now >= claim.claimedAtMillis);
226
- return {
227
- row,
228
- now,
229
- active: row[f.state] === "Claimed" && now < claim.claimExpiresAtMillis
230
- };
231
- });
232
- const terminal = Effect.fn("oauthConnected.terminal")(function* (mapping, claim, state, work, custody) {
233
- const owner = yield* CurrentOAuthTransaction;
234
- const f = mapping.flow, c = claim.flow.context;
235
- yield* owner.update(f.table, {
236
- [f.moduleId]: c.moduleId,
237
- [f.flowId]: c.flowId,
238
- [f.claimId]: claim.claimId
239
- }, {
240
- [f.state]: state,
241
- [f.snapshot]: null,
242
- [f.work]: work,
243
- [f.cohortKey]: custody?.cohort ?? null,
244
- [f.custody]: custody?.sealed === void 0 ? null : grantStorage.encode(custody.sealed),
245
- [f.version]: owner.marker
246
- });
247
- });
248
- const inspectTuple = Effect.fn("oauthConnected.inspectTuple")(function* (mapping, identity) {
249
- const owner = yield* CurrentOAuthTransaction;
250
- const t = mapping.ownership.tuple, key = yield* oauthIdentityKey(identity);
251
- const row = (yield* owner.read(t.table, equal(t.table, { [t.identityKey]: key }), { limit: 1 })).rows[0];
252
- if (row !== void 0) invariant(row[t.provider] === identity.provider && row[t.issuer] === identity.issuer && row[t.externalSubject] === identity.subject && [
253
- "Owned",
254
- "Unowned",
255
- "Reserved"
256
- ].includes(row[t.state]));
257
- if (mapping.ownership.mode === "separate") {
258
- const o = mapping.ownership.external;
259
- const external = yield* owner.read(o.table, equal(o.table, { [o.identityKey]: key }), { limit: 1 });
260
- if (row?.[t.state] === "Owned") {
261
- const e = external.rows[0];
262
- 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]));
263
- const condition = sql`exists(select 1 from ${o.table} where ${both(equal(o.table, { [o.identityKey]: key }), o.ownedCondition)})`;
264
- invariant(yield* owner.check(condition));
265
- owner.postconditions.push(condition);
266
- } else invariant(external.rows.length === 0);
267
- }
268
- return {
269
- key,
270
- row
271
- };
272
- });
273
- const inspectGrant = Effect.fn("oauthConnected.inspectGrant")(function* (mapping, input) {
274
- const c = input.claim.flow.context;
275
- if (input.identity.provider !== c.provider || input.identity.issuer !== c.issuer) return { _tag: "Rejected" };
276
- const found = yield* current(mapping, c.revision.subjectId);
277
- if (found === void 0 || !sameRevision(found.revision, c.revision)) return { _tag: "Rejected" };
278
- const cl = yield* client(mapping, configuration(input.claim.flow), false);
279
- if (cl === void 0) return { _tag: "Rejected" };
280
- const tuple = yield* inspectTuple(mapping, input.identity), t = mapping.ownership.tuple;
281
- if (tuple.row?.[t.state] === "Reserved" || tuple.row?.[t.state] === "Owned" && !mapping.subjectId.equals(tuple.row[t.subjectId], found.nativeId)) return { _tag: "Conflict" };
282
- const co = yield* cohort(mapping, cl.id, tuple.key, false);
283
- const claimed = yield* exact(mapping, input.claim);
284
- if (claimed === void 0) return { _tag: "Rejected" };
285
- if (c.reconnect !== void 0 && !sameIdentity(c.reconnect.identity, input.identity)) return { _tag: "Conflict" };
286
- return co.blocked || orderNumber(input.claim.order) <= co.cutoff || !claimed.active ? {
287
- _tag: "Quarantine",
288
- cohortGeneration: co.generation
289
- } : {
290
- _tag: "Target",
291
- cohortGeneration: co.generation
292
- };
293
- });
294
- //#endregion
295
- export { capture, claim, exact, expected, inspectGrant, inspectPrepared, inspectTuple, issue, live, pending, preflight, prepare, sameTarget, terminal };
296
-
297
- //# sourceMappingURL=oauth-connected-flow.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"oauth-connected-flow.mjs","names":["S.clientKey","S.configuration","S.current","S.useAuthority","S.action","client","S.client","S.readGrant","S.nativeCopy","S.touchScope","S.nextOrder","S.nativeOrder","S.orderNumber","S.cohort"],"sources":["../../src/drizzle/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\";\n/* oxlint-disable no-explicit-any -- private mapped rows; public adapters retain native IDs and closed Effects. */\nimport { sql } from \"drizzle-orm\";\nimport { Effect } from \"effect\";\n\nimport * as S from \"./oauth-connected-state\";\nimport { both, copiedRow, equal, CurrentOAuthTransaction } from \"./oauth-owner\";\nimport { acquireTuple, readTuple } from \"./oauth-registration\";\nimport { digest, invariant, oauthIdentityKey, sameIdentity, sameRevision } from \"./oauth-state\";\n\nexport type Input<K extends keyof OAuthConnectedPersistence[\"Service\"]> = Parameters<\n OAuthConnectedPersistence[\"Service\"][K]\n>[0];\n\nexport const pending = Effect.fn(\"oauthConnected.pending\")(function* (\n mapping: S.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\nexport 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\nexport 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\nconst retain = Effect.fn(\"oauthConnected.retain\")(function* (\n mapping: S.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\nexport const prepare = (mapping: S.Mapping, input: Input<\"prepare\">) =>\n retain(mapping, { _tag: \"Prepare\", ...input });\n\nexport const issue = (mapping: S.Mapping, input: Input<\"issue\">) =>\n retain(mapping, { _tag: \"Issue\", ...input });\n\nexport const inspectPrepared = Effect.fn(\"oauthConnected.inspectPrepared\")(function* (\n mapping: S.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\nexport 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\nexport const capture = Effect.fn(\"oauthConnected.capture\")(function* (\n mapping: S.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\nexport const preflight = Effect.fn(\"oauthConnected.preflight\")(function* (\n mapping: S.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\nexport const claim = Effect.fn(\"oauthConnected.claim\")(function* (\n mapping: S.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\nexport const exact = Effect.fn(\"oauthConnected.exactClaim\")(function* (\n mapping: S.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\nexport const terminal = Effect.fn(\"oauthConnected.terminal\")(function* (\n mapping: S.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\nexport const inspectTuple = Effect.fn(\"oauthConnected.inspectTuple\")(function* (\n mapping: S.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\nexport const inspectGrant = Effect.fn(\"oauthConnected.inspectGrant\")(function* (\n mapping: S.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/** Global tuple acquisition is shared with registration/accounts. */\nexport { acquireTuple, readTuple };\n"],"mappings":";;;;;;;;;;;AAmBA,MAAa,UAAU,OAAO,GAAG,wBAAwB,CAAC,CAAC,WACzD,SACA,QACA,OAAO,MACP;CACA,MAAM,QAAQ,OAAO;CACrB,MAAM,IAAI,QAAQ;CAQlB,MAAM,OAAM,OANQ,MAAM,KACxB,EAAE,OACF,MAAM,EAAE,OAAO;GAAG,EAAE,WAAW,OAAO;GAAW,EAAE,SAAS,OAAO;CAAO,CAAC,GAC3E;EAAE,OAAO;EAAG;EAAM,SAAS;CAAK,CAClC,EAAA,CAEiB,KAAK;CAEtB,IAAI,QAAQ,KAAA,KAAa,IAAI,EAAE,cAAc,MAAM,OAAO,KAAA;CAE1D,MAAM,OAAA,YAAqB,OAAO,IAAI,EAAE,SAAS,GAC/C,IAAI,KAAK;CAEX,MAAM,SAAS,OAAO,QAAQ,UAAU,SAAS,EAAE,SAAS,SAAS;CAErE,UACE,IAAI,EAAE,cAAc,EAAE,YACpB,IAAI,EAAE,YAAY,EAAE,UACpB,IAAI,EAAE,eAAe,EAAE,aACvB,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