@yielded/auth-persistence 0.1.0-beta.22 → 0.1.0-beta.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (182) hide show
  1. package/README.md +11 -0
  2. package/dist/Adapter.d.mts +13 -5
  3. package/dist/Adapter.mjs +9 -2
  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/authority-snapshot.mjs +53 -0
  11. package/dist/internal/authority-snapshot.mjs.map +1 -0
  12. package/dist/internal/configuration.d.mts +1 -1
  13. package/dist/internal/configuration.mjs.map +1 -1
  14. package/dist/internal/models/oauth-connected-model.d.mts +294 -0
  15. package/dist/internal/models/oauth-connected-model.mjs +22 -0
  16. package/dist/internal/models/oauth-connected-model.mjs.map +1 -0
  17. package/dist/internal/models/oauth-model.d.mts +467 -0
  18. package/dist/internal/models/oauth-model.mjs +49 -0
  19. package/dist/internal/models/oauth-model.mjs.map +1 -0
  20. package/dist/internal/models/password-model.d.mts +4 -97
  21. package/dist/internal/models/password-model.mjs +0 -3
  22. package/dist/internal/models/password-model.mjs.map +1 -1
  23. package/dist/internal/native-sql-table.d.mts +34 -0
  24. package/dist/internal/native-sql-table.mjs +111 -0
  25. package/dist/internal/native-sql-table.mjs.map +1 -0
  26. package/dist/internal/oauth/accounts.d.mts +8 -0
  27. package/dist/internal/oauth/accounts.mjs +379 -0
  28. package/dist/internal/oauth/accounts.mjs.map +1 -0
  29. package/dist/internal/oauth/connected-access.d.mts +9 -0
  30. package/dist/internal/oauth/connected-access.mjs +229 -0
  31. package/dist/internal/oauth/connected-access.mjs.map +1 -0
  32. package/dist/internal/oauth/connected-collection.d.mts +7 -0
  33. package/dist/internal/oauth/connected-collection.mjs +214 -0
  34. package/dist/internal/oauth/connected-collection.mjs.map +1 -0
  35. package/dist/internal/oauth/connected-custody.d.mts +6 -0
  36. package/dist/internal/oauth/connected-custody.mjs +165 -0
  37. package/dist/internal/oauth/connected-custody.mjs.map +1 -0
  38. package/dist/internal/oauth/connected-flow.d.mts +7 -0
  39. package/dist/internal/oauth/connected-flow.mjs +324 -0
  40. package/dist/internal/oauth/connected-flow.mjs.map +1 -0
  41. package/dist/internal/oauth/connected-input.mjs +122 -0
  42. package/dist/internal/oauth/connected-input.mjs.map +1 -0
  43. package/dist/internal/oauth/connected-keys.d.mts +2 -0
  44. package/dist/internal/oauth/connected-keys.mjs +22 -0
  45. package/dist/internal/oauth/connected-keys.mjs.map +1 -0
  46. package/dist/internal/oauth/connected-maintenance.d.mts +9 -0
  47. package/dist/internal/oauth/connected-maintenance.mjs +226 -0
  48. package/dist/internal/oauth/connected-maintenance.mjs.map +1 -0
  49. package/dist/internal/oauth/connected-management.d.mts +9 -0
  50. package/dist/internal/oauth/connected-management.mjs +243 -0
  51. package/dist/internal/oauth/connected-management.mjs.map +1 -0
  52. package/dist/internal/oauth/connected-reference.mjs +63 -0
  53. package/dist/internal/oauth/connected-reference.mjs.map +1 -0
  54. package/dist/internal/oauth/connected-settlement.d.mts +8 -0
  55. package/dist/internal/oauth/connected-settlement.mjs +141 -0
  56. package/dist/internal/oauth/connected-settlement.mjs.map +1 -0
  57. package/dist/internal/oauth/connected-sign-in.d.mts +10 -0
  58. package/dist/internal/oauth/connected-sign-in.mjs +281 -0
  59. package/dist/internal/oauth/connected-sign-in.mjs.map +1 -0
  60. package/dist/internal/oauth/connected-state.d.mts +14 -0
  61. package/dist/internal/oauth/connected-state.mjs +469 -0
  62. package/dist/internal/oauth/connected-state.mjs.map +1 -0
  63. package/dist/internal/oauth/connected-target.d.mts +21 -0
  64. package/dist/internal/oauth/connected-target.mjs +117 -0
  65. package/dist/internal/oauth/connected-target.mjs.map +1 -0
  66. package/dist/internal/oauth/flow.d.mts +5 -0
  67. package/dist/internal/oauth/flow.mjs +279 -0
  68. package/dist/internal/oauth/flow.mjs.map +1 -0
  69. package/dist/internal/oauth/input.mjs +127 -0
  70. package/dist/internal/oauth/input.mjs.map +1 -0
  71. package/dist/internal/oauth/owner.d.mts +10 -0
  72. package/dist/internal/oauth/owner.mjs +10 -0
  73. package/dist/internal/oauth/owner.mjs.map +1 -0
  74. package/dist/internal/oauth/query-compiler.d.mts +7 -0
  75. package/dist/internal/oauth/query-compiler.mjs +9 -0
  76. package/dist/internal/oauth/query-compiler.mjs.map +1 -0
  77. package/dist/internal/oauth/registration.d.mts +7 -0
  78. package/dist/internal/oauth/registration.mjs +393 -0
  79. package/dist/internal/oauth/registration.mjs.map +1 -0
  80. package/dist/internal/oauth/state.mjs +107 -0
  81. package/dist/internal/oauth/state.mjs.map +1 -0
  82. package/dist/internal/oauth/target.d.mts +31 -0
  83. package/dist/internal/oauth/target.mjs +273 -0
  84. package/dist/internal/oauth/target.mjs.map +1 -0
  85. package/dist/internal/oauth-kernel.d.mts +1423 -0
  86. package/dist/internal/oauth-kernel.mjs +187 -0
  87. package/dist/internal/oauth-kernel.mjs.map +1 -0
  88. package/dist/internal/oauth-proxy.d.mts +31 -0
  89. package/dist/internal/oauth-proxy.mjs +129 -0
  90. package/dist/internal/oauth-proxy.mjs.map +1 -0
  91. package/dist/internal/passkey/target.d.mts +1 -1
  92. package/dist/internal/passkey-kernel.d.mts +5 -5
  93. package/dist/internal/passkeys.mjs +1 -1
  94. package/dist/internal/password-credentials.d.mts +11 -0
  95. package/dist/internal/password-credentials.mjs +121 -0
  96. package/dist/internal/password-credentials.mjs.map +1 -0
  97. package/dist/internal/password-kernel.d.mts +15 -28
  98. package/dist/internal/password-kernel.mjs +35 -236
  99. package/dist/internal/password-kernel.mjs.map +1 -1
  100. package/dist/internal/persistence.d.mts +2 -0
  101. package/dist/internal/persistence.mjs +5 -5
  102. package/dist/internal/persistence.mjs.map +1 -1
  103. package/dist/internal/query-operations.d.mts +2 -0
  104. package/dist/internal/session-kernel.d.mts +5 -3
  105. package/dist/internal/session-kernel.mjs +95 -29
  106. package/dist/internal/session-kernel.mjs.map +1 -1
  107. package/dist/internal/sql-oauth-connected-model.d.mts +29 -0
  108. package/dist/internal/sql-oauth-connected-model.mjs +7 -0
  109. package/dist/internal/sql-oauth-connected-model.mjs.map +1 -0
  110. package/dist/internal/sql-oauth-model.d.mts +60 -0
  111. package/dist/internal/sql-oauth-model.mjs +10 -0
  112. package/dist/internal/sql-oauth-model.mjs.map +1 -0
  113. package/dist/internal/sql-persistence.mjs +3 -1
  114. package/dist/internal/sql-persistence.mjs.map +1 -1
  115. package/dist/internal/sql-query.d.mts +12 -0
  116. package/dist/internal/sql-query.mjs +15 -18
  117. package/dist/internal/sql-query.mjs.map +1 -1
  118. package/dist/internal/sql-storage-validation.mjs +54 -0
  119. package/dist/internal/sql-storage-validation.mjs.map +1 -0
  120. package/dist/internal/storage-mapping.mjs +2 -67
  121. package/dist/internal/storage-mapping.mjs.map +1 -1
  122. package/dist/internal/storage-plans.d.mts +7 -0
  123. package/dist/internal/storage-plans.mjs +189 -0
  124. package/dist/internal/storage-plans.mjs.map +1 -0
  125. package/dist/internal/storage-tables.d.mts +0 -99
  126. package/dist/internal/storage-tables.mjs +0 -43
  127. package/dist/internal/storage-tables.mjs.map +1 -1
  128. package/dist/internal/storage-validation.d.mts +6 -1
  129. package/dist/internal/storage-validation.mjs +42 -1
  130. package/dist/internal/storage-validation.mjs.map +1 -1
  131. package/dist/internal/transaction-execution-kernel.d.mts +1 -1
  132. package/dist/internal/transaction-kernel.d.mts +1 -1
  133. package/dist/internal/transaction-kernel.mjs +3 -2
  134. package/dist/internal/transaction-kernel.mjs.map +1 -1
  135. package/package.json +1 -1
  136. package/src/Adapter.ts +97 -8
  137. package/src/OAuthPersistence.ts +427 -0
  138. package/src/index.ts +3 -0
  139. package/src/internal/authority-snapshot.ts +91 -0
  140. package/src/internal/configuration.ts +1 -8
  141. package/src/internal/models/oauth-connected-model.ts +396 -0
  142. package/src/internal/models/oauth-model.ts +708 -0
  143. package/src/internal/models/password-model.ts +0 -120
  144. package/src/internal/native-sql-table.ts +178 -0
  145. package/src/internal/oauth/accounts.ts +838 -0
  146. package/src/internal/oauth/connected-access.ts +388 -0
  147. package/src/internal/oauth/connected-collection.ts +345 -0
  148. package/src/internal/oauth/connected-custody.ts +308 -0
  149. package/src/internal/oauth/connected-flow.ts +562 -0
  150. package/src/internal/oauth/connected-input.ts +147 -0
  151. package/src/internal/oauth/connected-keys.ts +31 -0
  152. package/src/internal/oauth/connected-maintenance.ts +379 -0
  153. package/src/internal/oauth/connected-management.ts +391 -0
  154. package/src/internal/oauth/connected-reference.ts +135 -0
  155. package/src/internal/oauth/connected-settlement.ts +278 -0
  156. package/src/internal/oauth/connected-sign-in.ts +472 -0
  157. package/src/internal/oauth/connected-state.ts +924 -0
  158. package/src/internal/oauth/connected-target.ts +307 -0
  159. package/src/internal/oauth/flow.ts +505 -0
  160. package/src/internal/oauth/input.ts +201 -0
  161. package/src/internal/oauth/owner.ts +22 -0
  162. package/src/internal/oauth/query-compiler.ts +15 -0
  163. package/src/internal/oauth/registration.ts +676 -0
  164. package/src/internal/oauth/state.ts +218 -0
  165. package/src/internal/oauth/target.ts +803 -0
  166. package/src/internal/oauth-kernel.ts +294 -0
  167. package/src/internal/oauth-proxy.ts +176 -0
  168. package/src/internal/password-credentials.ts +245 -0
  169. package/src/internal/password-kernel.ts +98 -712
  170. package/src/internal/persistence.ts +5 -10
  171. package/src/internal/query-operations.ts +2 -0
  172. package/src/internal/session-kernel.ts +231 -81
  173. package/src/internal/sql-oauth-connected-model.ts +145 -0
  174. package/src/internal/sql-oauth-model.ts +263 -0
  175. package/src/internal/sql-persistence.ts +2 -0
  176. package/src/internal/sql-query.ts +16 -13
  177. package/src/internal/sql-storage-validation.ts +64 -0
  178. package/src/internal/storage-mapping.ts +0 -66
  179. package/src/internal/storage-plans.ts +218 -0
  180. package/src/internal/storage-tables.ts +0 -35
  181. package/src/internal/storage-validation.ts +58 -0
  182. package/src/internal/transaction-kernel.ts +6 -2
@@ -0,0 +1,379 @@
1
+ import { CurrentOAuthTransaction } from "./owner.mjs";
2
+ import { OAuthQueryCompiler } from "./query-compiler.mjs";
3
+ import { invariant, oauthIdentityKey, sameRevision, satisfies, storage, validAction } from "./state.mjs";
4
+ import { OAuthFlow } from "./flow.mjs";
5
+ import { OAuthRegistration } from "./registration.mjs";
6
+ import { Context, DateTime, Effect, Layer } from "effect";
7
+ import { AuthenticationRequirement, SecurityRevision, SessionInvalidationWindow } from "@yielded/auth/Sessions";
8
+ import { OAuthAccountRevision, OAuthActionAuthorization, OAuthCredentialSnapshot, OAuthLinkAccess, OAuthLinkClaim, OAuthLinkPendingFlow, OAuthLinkTransactionContext, OAuthUnlinked, snapshotOAuthSync } from "@yielded/auth/OAuth";
9
+ //#region src/internal/oauth/accounts.ts
10
+ const make$1 = Effect.gen(function* () {
11
+ const operations = yield* OAuthQueryCompiler;
12
+ const flow = yield* OAuthFlow;
13
+ const registration = yield* OAuthRegistration;
14
+ const { eq, sql } = operations;
15
+ const { claimFlow, credentialStorage, currentSubject, exactClaim, issueFlow, linkStorage, matchesAccess, readFlow, terminalFlow } = flow;
16
+ const { both, col, copiedRow, equal } = operations;
17
+ const { acquireTuple, readTuple } = registration;
18
+ const contextStorage = storage(OAuthLinkTransactionContext);
19
+ const unlinkedStorage = storage(OAuthUnlinked);
20
+ const accountCurrent = (mapping, subjectId) => Effect.gen(function* () {
21
+ const native = yield* mapping.subjectId.toNative(subjectId);
22
+ return yield* currentSubject(mapping, native);
23
+ });
24
+ const authorization = (mapping, current, auth, action, flowId, commandId, intent, maximumAge) => Effect.flatMap(CurrentOAuthTransaction, (owner) => Effect.gen(function* () {
25
+ const now = yield* owner.now(mapping.clock);
26
+ const currentRequirement = snapshotOAuthSync(AuthenticationRequirement, mapping.subject.decodeActionRequirement(copiedRow(current.row), action));
27
+ if (!(yield* validAction(auth, {
28
+ moduleId: auth.challenge.moduleId,
29
+ action,
30
+ flowId,
31
+ commandId,
32
+ revision: current.revision,
33
+ intent
34
+ }, currentRequirement, now, maximumAge))) return false;
35
+ const age = Math.min(maximumAge, auth.requirement.maximumAgeMillis, currentRequirement.maximumAgeMillis);
36
+ const fresh = auth.evidence.proofs.filter((proof) => now - DateTime.toEpochMillis(proof.verifiedAt) < age);
37
+ invariant(fresh.length > 0);
38
+ const deadline = Math.min(...fresh.map((proof) => DateTime.toEpochMillis(proof.verifiedAt) + age));
39
+ owner.postconditions.push(sql`${mapping.clock.engineNowMillis} >= ${now} and ${mapping.clock.engineNowMillis} < ${deadline}`);
40
+ return true;
41
+ }));
42
+ const currentLink = (mapping, flow) => Effect.gen(function* () {
43
+ const current = yield* accountCurrent(mapping, flow.context.revision.subjectId);
44
+ return current !== void 0 && sameRevision(current.revision, flow.context.revision) ? current : void 0;
45
+ });
46
+ const issueLink = (mapping, input) => Effect.gen(function* () {
47
+ const flow = snapshotOAuthSync(OAuthLinkPendingFlow, input.flow), c = flow.context;
48
+ const current = yield* currentLink(mapping, flow);
49
+ const auth = snapshotOAuthSync(OAuthActionAuthorization, input.authorization);
50
+ if (current === void 0 || auth.challenge.moduleId !== c.moduleId || !(yield* authorization(mapping, current, auth, "link-begin", c.flowId, c.commandId, contextStorage.encode(c), c.maximumEvidenceAgeMillis))) return { _tag: "Rejected" };
51
+ return yield* issueFlow(mapping, flow, "link");
52
+ });
53
+ const preflightLink = (mapping, original) => Effect.flatMap(CurrentOAuthTransaction, (owner) => Effect.gen(function* () {
54
+ const input = snapshotOAuthSync(OAuthLinkAccess, original);
55
+ const current = yield* accountCurrent(mapping, input.subjectId);
56
+ const found = yield* readFlow(mapping, input, "link");
57
+ const now = yield* owner.now(mapping.clock);
58
+ if (current === void 0 || found.flow === void 0 || found.row?.[mapping.flow.state] !== "Pending" || !matchesAccess(found.flow, input)) return void 0;
59
+ const flow = snapshotOAuthSync(OAuthLinkPendingFlow, found.flow), c = flow.context;
60
+ if (c.revision.subjectId !== input.subjectId || !sameRevision(current.revision, c.revision) || now < c.issuedAtMillis || now >= c.expiresAtMillis) return void 0;
61
+ owner.postconditions.push(sql`${mapping.clock.engineNowMillis} >= ${c.issuedAtMillis} and ${mapping.clock.engineNowMillis} < ${c.expiresAtMillis}`);
62
+ return {
63
+ current,
64
+ flow,
65
+ row: found.row
66
+ };
67
+ }));
68
+ const claimLink = (mapping, input) => Effect.gen(function* () {
69
+ const found = yield* preflightLink(mapping, input.access);
70
+ const flow = snapshotOAuthSync(OAuthLinkPendingFlow, input.flow);
71
+ const auth = snapshotOAuthSync(OAuthActionAuthorization, input.authorization), c = flow.context;
72
+ if (found === void 0 || linkStorage.encode(found.flow) !== linkStorage.encode(flow) || auth.challenge.moduleId !== c.moduleId || !(yield* authorization(mapping, found.current, auth, "link-complete", c.flowId, c.commandId, contextStorage.encode(c), c.maximumEvidenceAgeMillis))) return { _tag: "Rejected" };
73
+ const claim = yield* claimFlow(mapping, {
74
+ row: found.row,
75
+ flow
76
+ }, input.claimId);
77
+ return claim === void 0 ? { _tag: "Rejected" } : {
78
+ _tag: "Claimed",
79
+ claim
80
+ };
81
+ });
82
+ const checkInvalidation = (mapping, original) => {
83
+ const value = snapshotOAuthSync(SessionInvalidationWindow, original);
84
+ invariant(value.trigger === "credential-change" && value.oldAuthenticationEvidence === "rejected" && (mapping.sessionInvalidation === "same-authority-immediate" ? value.existingSessions === "immediate" && value.maximumExposureMillis === 0 : value.existingSessions === "original-absolute-expiry"));
85
+ return value;
86
+ };
87
+ const advance = (mapping, current, removed) => Effect.flatMap(CurrentOAuthTransaction, (owner) => Effect.gen(function* () {
88
+ const revision = mapping.subject.nextSecurityRevision(current.revision.securityRevision);
89
+ invariant(typeof revision === "string" && revision.length > 0 && revision.length <= 256 && revision !== current.revision.securityRevision);
90
+ const subject = mapping.subject;
91
+ yield* owner.update(subject.table, { [subject.id]: current.nativeId }, { [subject.securityRevision]: revision });
92
+ for (const cleanup of mapping.cleanup) {
93
+ const condition = both(eq(col(cleanup.table, cleanup.subjectId), current.nativeId), cleanup.condition({
94
+ subjectId: current.nativeId,
95
+ revision: snapshotOAuthSync(OAuthAccountRevision, current.revision),
96
+ ...removed === void 0 ? {} : { removedCredentialId: removed }
97
+ }));
98
+ yield* owner.write(owner.database.delete(cleanup.table).where(condition));
99
+ owner.postconditions.push(sql`not exists(select 1 from ${cleanup.table} where ${condition})`);
100
+ }
101
+ return SecurityRevision.make(revision);
102
+ }));
103
+ const settleLink = (mapping, input, allocated) => Effect.flatMap(CurrentOAuthTransaction, (owner) => Effect.gen(function* () {
104
+ const claim = snapshotOAuthSync(OAuthLinkClaim, input.claim), flow = claim.flow, c = flow.context;
105
+ const current = yield* currentLink(mapping, flow);
106
+ const identity = input.outcome._tag === "Verified" ? input.outcome.identity.identity : void 0;
107
+ if (identity !== void 0) invariant(identity.provider === c.provider && identity.issuer === c.issuer);
108
+ const tuple = identity !== void 0 && current !== void 0 ? yield* readTuple(mapping.ownership, identity) : void 0;
109
+ if ((yield* exactClaim(mapping, claim, "link")) === void 0) return { _tag: "Rejected" };
110
+ let decision = { _tag: input.outcome._tag };
111
+ if (input.outcome._tag === "Verified") {
112
+ decision = { _tag: "Rejected" };
113
+ const auth = snapshotOAuthSync(OAuthActionAuthorization, input.authorization);
114
+ if (current !== void 0 && tuple !== void 0 && auth.challenge.moduleId === c.moduleId && (yield* authorization(mapping, current, auth, "link-complete", c.flowId, c.commandId, contextStorage.encode(c), c.maximumEvidenceAgeMillis))) {
115
+ const t = mapping.ownership.tuple, cr = mapping.credential, a = mapping.authority;
116
+ const login = yield* owner.read(cr.table, eq(col(cr.table, cr.identityKey), tuple.key), { limit: 1 });
117
+ const previous = login.rows[0];
118
+ if (tuple.row[t.state] === "Reserved" || tuple.row[t.state] === "Owned" && !mapping.subjectId.equals(tuple.row[t.subjectId], current.nativeId) || previous !== void 0 && (previous[cr.moduleId] !== c.moduleId || !mapping.subjectId.equals(previous[cr.subjectId], current.nativeId) || !cr.isActiveStatus(previous[cr.status]))) decision = { _tag: "Conflict" };
119
+ else if (previous !== void 0) {
120
+ const shared = current.revision.credentials.find((item) => item.credentialId === previous[cr.credentialId]);
121
+ if (tuple.row[t.state] !== "Owned" || shared?.revision !== previous[cr.credentialRevision] || !(yield* owner.check(sql`exists(select 1 from ${cr.table} where ${both(eq(col(cr.table, cr.identityKey), tuple.key), cr.activeCondition)})`))) decision = { _tag: "Conflict" };
122
+ else decision = {
123
+ _tag: "Linked",
124
+ changed: false,
125
+ credential: snapshotOAuthSync(OAuthCredentialSnapshot, {
126
+ moduleId: c.moduleId,
127
+ identity,
128
+ credentialId: previous[cr.credentialId],
129
+ credentialRevision: previous[cr.credentialRevision],
130
+ revision: current.revision
131
+ })
132
+ };
133
+ } else {
134
+ checkInvalidation(mapping, input.invalidation);
135
+ const values = {
136
+ ...cr.encodeInsert({
137
+ moduleId: c.moduleId,
138
+ subjectId: current.nativeId,
139
+ identityKey: tuple.key,
140
+ credentialId: allocated.credentialId,
141
+ credentialRevision: allocated.revision
142
+ }),
143
+ [cr.moduleId]: c.moduleId,
144
+ [cr.subjectId]: current.nativeId,
145
+ [cr.identityKey]: tuple.key,
146
+ [cr.credentialId]: allocated.credentialId,
147
+ [cr.credentialRevision]: allocated.revision
148
+ };
149
+ invariant(cr.isActiveStatus(values[cr.status]));
150
+ login.rows = (yield* owner.insert(cr.table, values, { [cr.credentialId]: allocated.credentialId })).rows;
151
+ const sharedValues = {
152
+ ...a.encodeInsert({
153
+ subjectId: current.nativeId,
154
+ credentialId: allocated.credentialId,
155
+ revision: allocated.revision
156
+ }),
157
+ [a.subjectId]: current.nativeId,
158
+ [a.credentialId]: allocated.credentialId,
159
+ [a.revision]: allocated.revision
160
+ };
161
+ invariant(a.isActiveStatus(sharedValues[a.status]));
162
+ const shared = yield* owner.insert(a.table, sharedValues, {
163
+ [a.subjectId]: current.nativeId,
164
+ [a.credentialId]: allocated.credentialId
165
+ });
166
+ current.authorities.rows = [...current.authorities.rows, ...shared.rows];
167
+ owner.postconditions.push(sql`exists(select 1 from ${cr.table} where ${both(eq(col(cr.table, cr.credentialId), allocated.credentialId), cr.activeCondition)})`, sql`exists(select 1 from ${a.table} where ${both(eq(col(a.table, a.subjectId), current.nativeId), eq(col(a.table, a.credentialId), allocated.credentialId), a.activeCondition)})`);
168
+ if (tuple.row[t.state] === "Unowned") yield* acquireTuple(mapping.ownership, identity, tuple.key, current.nativeId);
169
+ const next = yield* advance(mapping, current);
170
+ const revision = snapshotOAuthSync(OAuthAccountRevision, {
171
+ ...current.revision,
172
+ securityRevision: next,
173
+ credentials: [...current.revision.credentials, {
174
+ credentialId: allocated.credentialId,
175
+ revision: allocated.revision
176
+ }]
177
+ });
178
+ decision = {
179
+ _tag: "Linked",
180
+ changed: true,
181
+ credential: snapshotOAuthSync(OAuthCredentialSnapshot, {
182
+ moduleId: c.moduleId,
183
+ identity,
184
+ credentialId: allocated.credentialId,
185
+ credentialRevision: allocated.revision,
186
+ revision
187
+ })
188
+ };
189
+ }
190
+ }
191
+ }
192
+ yield* terminalFlow(mapping, flow, decision._tag);
193
+ return decision;
194
+ }));
195
+ const targetCredential = (mapping, moduleId, credentialId, current) => Effect.flatMap(CurrentOAuthTransaction, (owner) => Effect.gen(function* () {
196
+ const cr = mapping.credential;
197
+ const row = (yield* owner.read(cr.table, eq(col(cr.table, cr.credentialId), credentialId), { limit: 1 })).rows[0];
198
+ if (row === void 0 || row[cr.moduleId] !== moduleId || !mapping.subjectId.equals(row[cr.subjectId], current.nativeId) || !cr.isActiveStatus(row[cr.status])) return void 0;
199
+ const t = mapping.ownership.tuple;
200
+ const ownership = (yield* owner.read(t.table, eq(col(t.table, t.identityKey), row[cr.identityKey]), { limit: 1 })).rows[0];
201
+ if (ownership === void 0 || ownership[t.state] !== "Owned" || !mapping.subjectId.equals(ownership[t.subjectId], current.nativeId)) return void 0;
202
+ const identity = {
203
+ provider: ownership[t.provider],
204
+ issuer: ownership[t.issuer],
205
+ subject: ownership[t.externalSubject]
206
+ };
207
+ invariant((yield* oauthIdentityKey(identity)) === row[cr.identityKey]);
208
+ if (current.revision.credentials.find((item) => item.credentialId === credentialId)?.revision !== row[cr.credentialRevision] || !(yield* owner.check(sql`exists(select 1 from ${cr.table} where ${both(eq(col(cr.table, cr.credentialId), credentialId), cr.activeCondition)})`))) return void 0;
209
+ return snapshotOAuthSync(OAuthCredentialSnapshot, {
210
+ moduleId,
211
+ identity,
212
+ credentialId,
213
+ credentialRevision: row[cr.credentialRevision],
214
+ revision: current.revision
215
+ });
216
+ }));
217
+ const unlinkCommand = (mapping, moduleId, commandId) => Effect.flatMap(CurrentOAuthTransaction, (owner) => owner.read(mapping.command.table, equal(mapping.command.table, {
218
+ [mapping.command.moduleId]: moduleId,
219
+ [mapping.command.commandId]: commandId
220
+ }), { limit: 1 }));
221
+ const inspectUnlink = (mapping, input) => Effect.flatMap(CurrentOAuthTransaction, (owner) => Effect.gen(function* () {
222
+ const current = yield* accountCurrent(mapping, input.subjectId);
223
+ if (current === void 0) return { _tag: "Rejected" };
224
+ const command = yield* unlinkCommand(mapping, input.moduleId, input.commandId), r = mapping.command;
225
+ const old = command.rows[0];
226
+ if (old !== void 0) {
227
+ const intent = credentialStorage.decode(old[r.intentSnapshot]);
228
+ if (intent.moduleId !== input.moduleId || intent.revision.subjectId !== input.subjectId || intent.credentialId !== input.credentialId || !mapping.subjectId.equals(old[r.subjectId], current.nativeId) || old[r.credentialId] !== input.credentialId) return { _tag: "Conflict" };
229
+ const metadata = mapping.metadata.condition(current.nativeId);
230
+ if (!(yield* owner.check(metadata))) return { _tag: "Rejected" };
231
+ owner.postconditions.push(metadata);
232
+ return {
233
+ _tag: "Replay",
234
+ result: unlinkedStorage.decode(old[r.decision])
235
+ };
236
+ }
237
+ const credential = yield* targetCredential(mapping, input.moduleId, input.credentialId, current);
238
+ return credential === void 0 ? { _tag: "Rejected" } : {
239
+ _tag: "Target",
240
+ credential
241
+ };
242
+ }));
243
+ const remainingEligibility = (mapping, current, removed) => Effect.flatMap(CurrentOAuthTransaction, (owner) => Effect.gen(function* () {
244
+ invariant(mapping.eligibility.length > 0 && mapping.eligibility.length <= 32);
245
+ const facts = /* @__PURE__ */ new Map();
246
+ const revisions = new Map(current.revision.credentials.map((item) => [item.credentialId, item.revision]));
247
+ for (const descriptor of mapping.eligibility) {
248
+ const observed = yield* owner.read(descriptor.table, both(eq(col(descriptor.table, descriptor.subjectId), current.nativeId), descriptor.condition(current.nativeId)), { orderBy: col(descriptor.table, descriptor.credentialId) });
249
+ for (const row of observed.rows) {
250
+ const fact = descriptor.decode(copiedRow(row));
251
+ if (fact === void 0 || fact.credentialId === removed) continue;
252
+ invariant(fact.credentialId === row[descriptor.credentialId] && fact.revision === row[descriptor.revision] && revisions.get(fact.credentialId) === fact.revision && fact.factors.length <= 8 && fact.factors.every((factor) => [
253
+ "knowledge",
254
+ "possession",
255
+ "inherence"
256
+ ].includes(factor)));
257
+ invariant(typeof fact.usablePrimary === "boolean" && typeof fact.userVerified === "boolean" && typeof fact.phishingResistant === "boolean");
258
+ const normalized = {
259
+ credentialId: fact.credentialId,
260
+ revision: fact.revision,
261
+ usablePrimary: fact.usablePrimary,
262
+ factors: [...new Set(fact.factors)].sort(),
263
+ userVerified: fact.userVerified,
264
+ phishingResistant: fact.phishingResistant
265
+ };
266
+ const prior = facts.get(fact.credentialId);
267
+ invariant(prior === void 0 || JSON.stringify(prior) === JSON.stringify(normalized));
268
+ facts.set(fact.credentialId, normalized);
269
+ }
270
+ }
271
+ const available = [...facts.values()];
272
+ const requirement = snapshotOAuthSync(AuthenticationRequirement, mapping.subject.decodeAuthenticationRequirement(copiedRow(current.row)));
273
+ return available.some((fact) => fact.usablePrimary) && satisfies(available, requirement);
274
+ }));
275
+ const unlink = (mapping, input) => Effect.flatMap(CurrentOAuthTransaction, (owner) => Effect.gen(function* () {
276
+ const expected = snapshotOAuthSync(OAuthCredentialSnapshot, input.credential);
277
+ invariant(expected.moduleId === input.moduleId);
278
+ const current = yield* accountCurrent(mapping, expected.revision.subjectId);
279
+ if (current === void 0) return { _tag: "Rejected" };
280
+ const guards = mapping.connectedReferenceGuards ?? [];
281
+ invariant(guards.length <= 32);
282
+ let referencesGuarded = mapping.connectedReference !== void 0 && guards.length > 0;
283
+ if (mapping.connectedReference !== void 0) for (const guard of guards) {
284
+ const condition = guard.condition({
285
+ identity: snapshotOAuthSync(OAuthCredentialSnapshot.fields.identity, expected.identity),
286
+ identityKey: yield* oauthIdentityKey(expected.identity),
287
+ subjectId: copiedRow({ value: current.nativeId }).value
288
+ });
289
+ if ((yield* owner.read(guard.table, Effect.isEffect(condition) ? yield* condition : condition, { orderBy: col(guard.table, guard.orderBy) })).rows.length === 0) referencesGuarded = false;
290
+ }
291
+ const command = yield* unlinkCommand(mapping, input.moduleId, input.commandId), r = mapping.command;
292
+ const previous = command.rows[0];
293
+ if (previous !== void 0) {
294
+ if (previous[r.intentSnapshot] !== credentialStorage.encode(expected) || previous[r.credentialId] !== expected.credentialId || !mapping.subjectId.equals(previous[r.subjectId], current.nativeId)) return { _tag: "Conflict" };
295
+ const metadata = mapping.metadata.condition(current.nativeId);
296
+ if (!(yield* owner.check(metadata))) return { _tag: "Rejected" };
297
+ owner.postconditions.push(metadata);
298
+ return {
299
+ _tag: "Unlinked",
300
+ replayed: true,
301
+ result: unlinkedStorage.decode(previous[r.decision])
302
+ };
303
+ }
304
+ const actual = yield* targetCredential(mapping, input.moduleId, expected.credentialId, current);
305
+ if (actual === void 0 || credentialStorage.encode(actual) !== credentialStorage.encode(expected)) return { _tag: "Rejected" };
306
+ const auth = snapshotOAuthSync(OAuthActionAuthorization, input.authorization);
307
+ if (auth.challenge.moduleId !== input.moduleId || !(yield* authorization(mapping, current, auth, "unlink", input.commandId, input.commandId, credentialStorage.encode(expected), auth.requirement.maximumAgeMillis))) return { _tag: "Rejected" };
308
+ if (!(yield* remainingEligibility(mapping, current, expected.credentialId))) return { _tag: "LastSignInMethod" };
309
+ const invalidation = checkInvalidation(mapping, input.invalidation), cr = mapping.credential, a = mapping.authority;
310
+ const key = yield* oauthIdentityKey(expected.identity), t = mapping.ownership.tuple;
311
+ const connected = mapping.connectedReference?.({
312
+ identityKey: key,
313
+ subjectId: copiedRow({ value: current.nativeId }).value
314
+ });
315
+ const retain = connected !== void 0 && (!referencesGuarded || (yield* owner.check(connected)));
316
+ if (connected !== void 0 && referencesGuarded) owner.postconditions.push(retain ? connected : sql`not (${connected})`);
317
+ yield* owner.remove(cr.table, { [cr.credentialId]: expected.credentialId });
318
+ yield* owner.remove(a.table, {
319
+ [a.subjectId]: current.nativeId,
320
+ [a.credentialId]: expected.credentialId
321
+ });
322
+ if (!retain) {
323
+ yield* owner.update(t.table, { [t.identityKey]: key }, {
324
+ [t.state]: "Unowned",
325
+ [t.subjectId]: null,
326
+ [t.reservation]: null,
327
+ [t.version]: owner.marker
328
+ });
329
+ if (mapping.ownership.mode === "separate") yield* owner.remove(mapping.ownership.external.table, { [mapping.ownership.external.identityKey]: key });
330
+ }
331
+ yield* advance(mapping, current, expected.credentialId);
332
+ const result = snapshotOAuthSync(OAuthUnlinked, {
333
+ _tag: "Unlinked",
334
+ credentialId: expected.credentialId,
335
+ invalidation
336
+ });
337
+ const now = yield* owner.now(mapping.clock);
338
+ invariant(Number.isSafeInteger(input.retentionUntilMillis) && input.retentionUntilMillis > now && input.retentionUntilMillis >= input.nowMillis + 12e4);
339
+ const commandKey = {
340
+ [r.moduleId]: input.moduleId,
341
+ [r.commandId]: input.commandId
342
+ };
343
+ const values = {
344
+ ...r.encodeInsert({
345
+ moduleId: input.moduleId,
346
+ commandId: input.commandId,
347
+ credential: snapshotOAuthSync(OAuthCredentialSnapshot, expected)
348
+ }),
349
+ ...commandKey,
350
+ [r.subjectId]: current.nativeId,
351
+ [r.credentialId]: expected.credentialId,
352
+ [r.intentSnapshot]: credentialStorage.encode(expected),
353
+ [r.decision]: unlinkedStorage.encode(result),
354
+ [r.retentionUntil]: mapping.clock.encodeInstant(input.retentionUntilMillis)
355
+ };
356
+ command.rows = (yield* owner.insert(r.table, values, commandKey)).rows;
357
+ return {
358
+ _tag: "Unlinked",
359
+ result,
360
+ replayed: false
361
+ };
362
+ }));
363
+ return {
364
+ accountCurrent,
365
+ issueLink,
366
+ preflightLink,
367
+ claimLink,
368
+ settleLink,
369
+ inspectUnlink,
370
+ unlink
371
+ };
372
+ });
373
+ var OAuthAccounts = class OAuthAccounts extends Context.Service()("effect-auth/persistence/OAuthAccounts") {
374
+ static layer = Layer.effect(OAuthAccounts, make$1);
375
+ };
376
+ //#endregion
377
+ export { OAuthAccounts };
378
+
379
+ //# sourceMappingURL=accounts.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"accounts.mjs","names":["make"],"sources":["../../../src/internal/oauth/accounts.ts"],"sourcesContent":["/* oxlint-disable no-explicit-any -- compiler table shapes are erased; domain codecs and error channels remain typed. */\nimport {\n OAuthAccountRevision,\n OAuthActionAuthorization,\n OAuthLinkAccess,\n OAuthLinkClaim,\n OAuthLinkPendingFlow,\n OAuthLinkTransactionContext,\n OAuthUnlinked,\n OAuthCredentialSnapshot,\n snapshotOAuthSync,\n} from \"@yielded/auth/OAuth\";\nimport {\n SessionInvalidationWindow,\n AuthenticationRequirement,\n SecurityRevision,\n} from \"@yielded/auth/Sessions\";\nimport { DateTime, Effect, Context, Layer } from \"effect\";\n\nimport type { SubjectIdCodec } from \"../models/common\";\nimport type { OAuthReferenceGuardDescriptor, OAuthEligibilityFact } from \"../models/oauth-model\";\nimport { OAuthFlow } from \"./flow\";\nimport { CurrentOAuthTransaction } from \"./owner\";\nimport { OAuthQueryCompiler } from \"./query-compiler\";\nimport { OAuthRegistration } from \"./registration\";\nimport {\n invariant,\n oauthIdentityKey,\n sameRevision,\n satisfies,\n storage,\n validAction,\n} from \"./state\";\n\nconst make = Effect.gen(function* () {\n const operations = yield* OAuthQueryCompiler;\n const flow = yield* OAuthFlow;\n const registration = yield* OAuthRegistration;\n\n const { eq, sql } = operations;\n\n const {\n claimFlow,\n credentialStorage,\n currentSubject,\n exactClaim,\n issueFlow,\n linkStorage,\n matchesAccess,\n readFlow,\n terminalFlow,\n } = flow;\n\n const { both, col, copiedRow, equal } = operations;\n const { acquireTuple, readTuple } = registration;\n\n const contextStorage = storage(OAuthLinkTransactionContext);\n\n const unlinkedStorage = storage(OAuthUnlinked);\n\n const accountCurrent = (mapping: any, subjectId: OAuthAccountRevision[\"subjectId\"]) =>\n Effect.gen(function* () {\n const native = yield* (mapping.subjectId as SubjectIdCodec<unknown>).toNative(subjectId);\n\n return yield* currentSubject(mapping, native);\n });\n\n const authorization = (\n mapping: any,\n current: any,\n auth: OAuthActionAuthorization,\n action: OAuthActionAuthorization[\"challenge\"][\"action\"],\n flowId: string,\n commandId: string,\n intent: string,\n maximumAge: number,\n ) =>\n Effect.flatMap(CurrentOAuthTransaction, (owner) =>\n Effect.gen(function* () {\n const now = yield* owner.now(mapping.clock);\n\n const currentRequirement = snapshotOAuthSync(\n AuthenticationRequirement,\n mapping.subject.decodeActionRequirement(copiedRow(current.row), action),\n );\n\n const accepted = yield* validAction(\n auth,\n {\n moduleId: auth.challenge.moduleId,\n action,\n flowId,\n commandId,\n revision: current.revision,\n intent,\n },\n currentRequirement,\n now,\n maximumAge,\n );\n\n if (!accepted) return false;\n\n const age = Math.min(\n maximumAge,\n auth.requirement.maximumAgeMillis,\n currentRequirement.maximumAgeMillis,\n );\n\n const fresh = auth.evidence.proofs.filter(\n (proof) => now - DateTime.toEpochMillis(proof.verifiedAt) < age,\n );\n\n invariant(fresh.length > 0);\n\n const deadline = Math.min(\n ...fresh.map((proof) => DateTime.toEpochMillis(proof.verifiedAt) + age),\n );\n\n owner.postconditions.push(\n sql`${mapping.clock.engineNowMillis} >= ${now} and ${mapping.clock.engineNowMillis} < ${deadline}`,\n );\n\n return true;\n }),\n );\n\n const currentLink = (mapping: any, flow: typeof OAuthLinkPendingFlow.Type) =>\n Effect.gen(function* () {\n const current = yield* accountCurrent(mapping, flow.context.revision.subjectId);\n\n return current !== undefined && sameRevision(current.revision, flow.context.revision)\n ? current\n : undefined;\n });\n\n const issueLink = (mapping: any, input: any) =>\n Effect.gen(function* () {\n const flow = snapshotOAuthSync(OAuthLinkPendingFlow, input.flow),\n c = flow.context;\n\n const current = yield* currentLink(mapping, flow);\n const auth = snapshotOAuthSync(OAuthActionAuthorization, input.authorization);\n\n if (\n current === undefined ||\n auth.challenge.moduleId !== c.moduleId ||\n !(yield* authorization(\n mapping,\n current,\n auth,\n \"link-begin\",\n c.flowId,\n c.commandId,\n contextStorage.encode(c),\n c.maximumEvidenceAgeMillis,\n ))\n )\n return { _tag: \"Rejected\" } as const;\n\n return yield* issueFlow(mapping, flow, \"link\");\n });\n\n const preflightLink = (mapping: any, original: typeof OAuthLinkAccess.Type) =>\n Effect.flatMap(CurrentOAuthTransaction, (owner) =>\n Effect.gen(function* () {\n const input = snapshotOAuthSync(OAuthLinkAccess, original);\n // The authenticated subject is known before discovering/locking any flow.\n const current = yield* accountCurrent(mapping, input.subjectId);\n const found = yield* readFlow(mapping, input, \"link\");\n const now = yield* owner.now(mapping.clock);\n\n if (\n current === undefined ||\n found.flow === undefined ||\n found.row?.[mapping.flow.state] !== \"Pending\" ||\n !matchesAccess(found.flow, input)\n )\n return undefined;\n\n const flow = snapshotOAuthSync(OAuthLinkPendingFlow, found.flow as never),\n c = flow.context;\n\n if (\n c.revision.subjectId !== input.subjectId ||\n !sameRevision(current.revision, c.revision) ||\n now < c.issuedAtMillis ||\n now >= c.expiresAtMillis\n )\n return undefined;\n owner.postconditions.push(\n sql`${mapping.clock.engineNowMillis} >= ${c.issuedAtMillis} and ${mapping.clock.engineNowMillis} < ${c.expiresAtMillis}`,\n );\n\n return { current, flow, row: found.row };\n }),\n );\n\n const claimLink = (mapping: any, input: any) =>\n Effect.gen(function* () {\n const found = yield* preflightLink(mapping, input.access);\n const flow = snapshotOAuthSync(OAuthLinkPendingFlow, input.flow);\n\n const auth = snapshotOAuthSync(OAuthActionAuthorization, input.authorization),\n c = flow.context;\n\n if (\n found === undefined ||\n linkStorage.encode(found.flow) !== linkStorage.encode(flow) ||\n auth.challenge.moduleId !== c.moduleId ||\n !(yield* authorization(\n mapping,\n found.current,\n auth,\n \"link-complete\",\n c.flowId,\n c.commandId,\n contextStorage.encode(c),\n c.maximumEvidenceAgeMillis,\n ))\n )\n return { _tag: \"Rejected\" } as const;\n const claim = yield* claimFlow(mapping, { row: found.row!, flow }, input.claimId);\n\n return claim === undefined\n ? ({ _tag: \"Rejected\" } as const)\n : ({ _tag: \"Claimed\", claim } as const);\n });\n\n const checkInvalidation = (mapping: any, original: SessionInvalidationWindow) => {\n const value = snapshotOAuthSync(SessionInvalidationWindow, original);\n\n invariant(\n value.trigger === \"credential-change\" &&\n value.oldAuthenticationEvidence === \"rejected\" &&\n (mapping.sessionInvalidation === \"same-authority-immediate\"\n ? value.existingSessions === \"immediate\" && value.maximumExposureMillis === 0\n : value.existingSessions === \"original-absolute-expiry\"),\n );\n\n return value;\n };\n\n const advance = (mapping: any, current: any, removed?: string) =>\n Effect.flatMap(CurrentOAuthTransaction, (owner) =>\n Effect.gen(function* () {\n const revision = mapping.subject.nextSecurityRevision(current.revision.securityRevision);\n\n invariant(\n typeof revision === \"string\" &&\n revision.length > 0 &&\n revision.length <= 256 &&\n revision !== current.revision.securityRevision,\n );\n const subject = mapping.subject;\n\n yield* owner.update(\n subject.table,\n { [subject.id]: current.nativeId },\n { [subject.securityRevision]: revision },\n );\n for (const cleanup of mapping.cleanup) {\n const condition = both(\n eq(col(cleanup.table, cleanup.subjectId), current.nativeId),\n cleanup.condition({\n subjectId: current.nativeId,\n revision: snapshotOAuthSync(OAuthAccountRevision, current.revision),\n ...(removed === undefined ? {} : { removedCredentialId: removed }),\n }),\n );\n\n yield* owner.write(owner.database.delete(cleanup.table).where(condition));\n owner.postconditions.push(\n sql`not exists(select 1 from ${cleanup.table} where ${condition})`,\n );\n }\n\n return SecurityRevision.make(revision);\n }),\n );\n\n const settleLink = (\n mapping: any,\n input: any,\n allocated: { readonly credentialId: string; readonly revision: SecurityRevision },\n ) =>\n Effect.flatMap(CurrentOAuthTransaction, (owner) =>\n Effect.gen(function* () {\n const claim = snapshotOAuthSync(OAuthLinkClaim, input.claim),\n flow = claim.flow,\n c = flow.context;\n\n const current = yield* currentLink(mapping, flow);\n\n // Known subject is always locked before tuple/flow; even a rejected outcome\n // settles its exact original claim without adopting a newer revision.\n const identity =\n input.outcome._tag === \"Verified\" ? input.outcome.identity.identity : undefined;\n\n if (identity !== undefined)\n invariant(identity.provider === c.provider && identity.issuer === c.issuer);\n\n const tuple =\n identity !== undefined && current !== undefined\n ? yield* readTuple(mapping.ownership, identity)\n : undefined;\n\n const exact = yield* exactClaim(mapping, claim, \"link\");\n\n if (exact === undefined) return { _tag: \"Rejected\" } as const;\n let decision: any = { _tag: input.outcome._tag };\n\n if (input.outcome._tag === \"Verified\") {\n decision = { _tag: \"Rejected\" };\n const auth = snapshotOAuthSync(OAuthActionAuthorization, input.authorization);\n\n if (\n current !== undefined &&\n tuple !== undefined &&\n auth.challenge.moduleId === c.moduleId &&\n (yield* authorization(\n mapping,\n current,\n auth,\n \"link-complete\",\n c.flowId,\n c.commandId,\n contextStorage.encode(c),\n c.maximumEvidenceAgeMillis,\n ))\n ) {\n const t = mapping.ownership.tuple,\n cr = mapping.credential,\n a = mapping.authority;\n\n const login = yield* owner.read(\n cr.table,\n eq(col(cr.table, cr.identityKey), tuple.key),\n {\n limit: 1,\n },\n );\n\n const previous = login.rows[0];\n\n if (\n tuple.row[t.state] === \"Reserved\" ||\n (tuple.row[t.state] === \"Owned\" &&\n !mapping.subjectId.equals(tuple.row[t.subjectId], current.nativeId)) ||\n (previous !== undefined &&\n (previous[cr.moduleId] !== c.moduleId ||\n !mapping.subjectId.equals(previous[cr.subjectId], current.nativeId) ||\n !cr.isActiveStatus(previous[cr.status])))\n )\n decision = { _tag: \"Conflict\" };\n else if (previous !== undefined) {\n const shared = current.revision.credentials.find(\n (item: any) => item.credentialId === previous[cr.credentialId],\n );\n\n if (\n tuple.row[t.state] !== \"Owned\" ||\n shared?.revision !== previous[cr.credentialRevision] ||\n !(yield* owner.check(\n sql`exists(select 1 from ${cr.table} where ${both(eq(col(cr.table, cr.identityKey), tuple.key), cr.activeCondition)})`,\n ))\n )\n decision = { _tag: \"Conflict\" };\n else\n decision = {\n _tag: \"Linked\",\n changed: false,\n credential: snapshotOAuthSync(OAuthCredentialSnapshot, {\n moduleId: c.moduleId,\n identity,\n credentialId: previous[cr.credentialId],\n credentialRevision: previous[cr.credentialRevision],\n revision: current.revision,\n }),\n };\n } else {\n checkInvalidation(mapping, input.invalidation);\n\n const values = {\n ...cr.encodeInsert({\n moduleId: c.moduleId,\n subjectId: current.nativeId,\n identityKey: tuple.key,\n credentialId: allocated.credentialId,\n credentialRevision: allocated.revision,\n }),\n [cr.moduleId]: c.moduleId,\n [cr.subjectId]: current.nativeId,\n [cr.identityKey]: tuple.key,\n [cr.credentialId]: allocated.credentialId,\n [cr.credentialRevision]: allocated.revision,\n };\n\n invariant(cr.isActiveStatus(values[cr.status]));\n\n const inserted = yield* owner.insert(cr.table, values, {\n [cr.credentialId]: allocated.credentialId,\n });\n\n login.rows = inserted.rows;\n\n const sharedValues = {\n ...a.encodeInsert({\n subjectId: current.nativeId,\n credentialId: allocated.credentialId,\n revision: allocated.revision,\n }),\n [a.subjectId]: current.nativeId,\n [a.credentialId]: allocated.credentialId,\n [a.revision]: allocated.revision,\n };\n\n invariant(a.isActiveStatus(sharedValues[a.status]));\n\n const shared = yield* owner.insert(a.table, sharedValues, {\n [a.subjectId]: current.nativeId,\n [a.credentialId]: allocated.credentialId,\n });\n\n current.authorities.rows = [...current.authorities.rows, ...shared.rows];\n owner.postconditions.push(\n sql`exists(select 1 from ${cr.table} where ${both(\n eq(col(cr.table, cr.credentialId), allocated.credentialId),\n cr.activeCondition,\n )})`,\n sql`exists(select 1 from ${a.table} where ${both(\n eq(col(a.table, a.subjectId), current.nativeId),\n eq(col(a.table, a.credentialId), allocated.credentialId),\n a.activeCondition,\n )})`,\n );\n if (tuple.row[t.state] === \"Unowned\")\n yield* acquireTuple(mapping.ownership, identity, tuple.key, current.nativeId);\n const next = yield* advance(mapping, current);\n\n const revision = snapshotOAuthSync(OAuthAccountRevision, {\n ...current.revision,\n securityRevision: next,\n credentials: [\n ...current.revision.credentials,\n { credentialId: allocated.credentialId, revision: allocated.revision },\n ],\n });\n\n decision = {\n _tag: \"Linked\",\n changed: true,\n credential: snapshotOAuthSync(OAuthCredentialSnapshot, {\n moduleId: c.moduleId,\n identity,\n credentialId: allocated.credentialId,\n credentialRevision: allocated.revision as never,\n revision,\n }),\n };\n }\n }\n }\n yield* terminalFlow(mapping, flow, decision._tag);\n\n return decision;\n }),\n );\n\n const targetCredential = (mapping: any, moduleId: string, credentialId: string, current: any) =>\n Effect.flatMap(CurrentOAuthTransaction, (owner) =>\n Effect.gen(function* () {\n const cr = mapping.credential;\n\n const found = yield* owner.read(\n cr.table,\n eq(col(cr.table, cr.credentialId), credentialId),\n {\n limit: 1,\n },\n );\n\n const row = found.rows[0];\n\n if (\n row === undefined ||\n row[cr.moduleId] !== moduleId ||\n !mapping.subjectId.equals(row[cr.subjectId], current.nativeId) ||\n !cr.isActiveStatus(row[cr.status])\n )\n return undefined;\n const t = mapping.ownership.tuple;\n\n const tuple = yield* owner.read(\n t.table,\n eq(col(t.table, t.identityKey), row[cr.identityKey]),\n {\n limit: 1,\n },\n );\n\n const ownership = tuple.rows[0];\n\n if (\n ownership === undefined ||\n ownership[t.state] !== \"Owned\" ||\n !mapping.subjectId.equals(ownership[t.subjectId], current.nativeId)\n )\n return undefined;\n\n const identity = {\n provider: ownership[t.provider],\n issuer: ownership[t.issuer],\n subject: ownership[t.externalSubject],\n };\n\n invariant((yield* oauthIdentityKey(identity)) === row[cr.identityKey]);\n\n const shared = current.revision.credentials.find(\n (item: any) => item.credentialId === credentialId,\n );\n\n if (\n shared?.revision !== row[cr.credentialRevision] ||\n !(yield* owner.check(\n sql`exists(select 1 from ${cr.table} where ${both(eq(col(cr.table, cr.credentialId), credentialId), cr.activeCondition)})`,\n ))\n )\n return undefined;\n\n return snapshotOAuthSync(OAuthCredentialSnapshot, {\n moduleId: moduleId as never,\n identity,\n credentialId,\n credentialRevision: row[cr.credentialRevision],\n revision: current.revision,\n });\n }),\n );\n\n const unlinkCommand = (mapping: any, moduleId: string, commandId: string) =>\n Effect.flatMap(CurrentOAuthTransaction, (owner) =>\n owner.read(\n mapping.command.table,\n equal(mapping.command.table, {\n [mapping.command.moduleId]: moduleId,\n [mapping.command.commandId]: commandId,\n }),\n { limit: 1 },\n ),\n );\n\n const inspectUnlink = (mapping: any, input: any) =>\n Effect.flatMap(CurrentOAuthTransaction, (owner) =>\n Effect.gen(function* () {\n const current = yield* accountCurrent(mapping, input.subjectId);\n\n if (current === undefined) return { _tag: \"Rejected\" } as const;\n\n const command = yield* unlinkCommand(mapping, input.moduleId, input.commandId),\n r = mapping.command;\n\n const old = command.rows[0];\n\n if (old !== undefined) {\n const intent = credentialStorage.decode(old[r.intentSnapshot]);\n\n if (\n intent.moduleId !== input.moduleId ||\n intent.revision.subjectId !== input.subjectId ||\n intent.credentialId !== input.credentialId ||\n !mapping.subjectId.equals(old[r.subjectId], current.nativeId) ||\n old[r.credentialId] !== input.credentialId\n )\n return { _tag: \"Conflict\" } as const;\n const metadata = mapping.metadata.condition(current.nativeId);\n\n if (!(yield* owner.check(metadata))) return { _tag: \"Rejected\" } as const;\n owner.postconditions.push(metadata);\n\n return { _tag: \"Replay\", result: unlinkedStorage.decode(old[r.decision]) } as const;\n }\n\n const credential = yield* targetCredential(\n mapping,\n input.moduleId,\n input.credentialId,\n current,\n );\n\n return credential === undefined\n ? ({ _tag: \"Rejected\" } as const)\n : ({ _tag: \"Target\", credential } as const);\n }),\n );\n\n const remainingEligibility = (mapping: any, current: any, removed: string) =>\n Effect.flatMap(CurrentOAuthTransaction, (owner) =>\n Effect.gen(function* () {\n invariant(mapping.eligibility.length > 0 && mapping.eligibility.length <= 32);\n const facts = new Map<string, OAuthEligibilityFact>();\n\n const revisions = new Map(\n current.revision.credentials.map((item: any) => [item.credentialId, item.revision]),\n );\n\n for (const descriptor of mapping.eligibility) {\n const observed = yield* owner.read(\n descriptor.table,\n both(\n eq(col(descriptor.table, descriptor.subjectId), current.nativeId),\n descriptor.condition(current.nativeId),\n ),\n { orderBy: col(descriptor.table, descriptor.credentialId) },\n );\n\n for (const row of observed.rows) {\n const fact = descriptor.decode(copiedRow(row)) as OAuthEligibilityFact | undefined;\n\n if (fact === undefined || fact.credentialId === removed) continue;\n invariant(\n fact.credentialId === row[descriptor.credentialId] &&\n fact.revision === row[descriptor.revision] &&\n revisions.get(fact.credentialId) === fact.revision &&\n fact.factors.length <= 8 &&\n fact.factors.every((factor) =>\n [\"knowledge\", \"possession\", \"inherence\"].includes(factor),\n ),\n );\n invariant(\n typeof fact.usablePrimary === \"boolean\" &&\n typeof fact.userVerified === \"boolean\" &&\n typeof fact.phishingResistant === \"boolean\",\n );\n\n const normalized = {\n credentialId: fact.credentialId,\n revision: fact.revision,\n usablePrimary: fact.usablePrimary,\n factors: [...new Set(fact.factors)].sort(),\n userVerified: fact.userVerified,\n phishingResistant: fact.phishingResistant,\n };\n\n const prior = facts.get(fact.credentialId);\n\n // oxlint-disable-next-line no-restricted-properties -- Canonical bounded eligibility fact fingerprint rejects contradictory duplicate IDs.\n invariant(prior === undefined || JSON.stringify(prior) === JSON.stringify(normalized));\n facts.set(fact.credentialId, normalized);\n }\n }\n const available = [...facts.values()];\n\n const requirement = snapshotOAuthSync(\n AuthenticationRequirement,\n mapping.subject.decodeAuthenticationRequirement(copiedRow(current.row)),\n );\n\n return available.some((fact) => fact.usablePrimary) && satisfies(available, requirement);\n }),\n );\n\n const unlink = (mapping: any, input: any) =>\n Effect.flatMap(CurrentOAuthTransaction, (owner) =>\n Effect.gen(function* () {\n const expected = snapshotOAuthSync(OAuthCredentialSnapshot, input.credential);\n\n invariant(expected.moduleId === input.moduleId);\n const current = yield* accountCurrent(mapping, expected.revision.subjectId);\n\n if (current === undefined) return { _tag: \"Rejected\" } as const;\n\n const guards: ReadonlyArray<OAuthReferenceGuardDescriptor<unknown>> =\n mapping.connectedReferenceGuards ?? [];\n\n invariant(guards.length <= 32);\n let referencesGuarded = mapping.connectedReference !== undefined && guards.length > 0;\n\n if (mapping.connectedReference !== undefined)\n for (const guard of guards) {\n const condition = guard.condition({\n identity: snapshotOAuthSync(\n OAuthCredentialSnapshot.fields.identity,\n expected.identity,\n ),\n identityKey: yield* oauthIdentityKey(expected.identity),\n subjectId: copiedRow({ value: current.nativeId }).value,\n });\n\n const read = yield* owner.read(\n guard.table,\n Effect.isEffect(condition) ? yield* condition : condition,\n { orderBy: col(guard.table, guard.orderBy) },\n );\n\n if (read.rows.length === 0) referencesGuarded = false;\n }\n\n const command = yield* unlinkCommand(mapping, input.moduleId, input.commandId),\n r = mapping.command;\n\n const previous = command.rows[0];\n\n if (previous !== undefined) {\n if (\n previous[r.intentSnapshot] !== credentialStorage.encode(expected) ||\n previous[r.credentialId] !== expected.credentialId ||\n !mapping.subjectId.equals(previous[r.subjectId], current.nativeId)\n )\n return { _tag: \"Conflict\" } as const;\n const metadata = mapping.metadata.condition(current.nativeId);\n\n if (!(yield* owner.check(metadata))) return { _tag: \"Rejected\" } as const;\n owner.postconditions.push(metadata);\n\n return {\n _tag: \"Unlinked\",\n replayed: true,\n result: unlinkedStorage.decode(previous[r.decision]),\n } as const;\n }\n\n const actual = yield* targetCredential(\n mapping,\n input.moduleId,\n expected.credentialId,\n current,\n );\n\n if (\n actual === undefined ||\n credentialStorage.encode(actual) !== credentialStorage.encode(expected)\n )\n return { _tag: \"Rejected\" } as const;\n const auth = snapshotOAuthSync(OAuthActionAuthorization, input.authorization);\n\n if (\n auth.challenge.moduleId !== input.moduleId ||\n !(yield* authorization(\n mapping,\n current,\n auth,\n \"unlink\",\n input.commandId,\n input.commandId,\n credentialStorage.encode(expected),\n auth.requirement.maximumAgeMillis,\n ))\n )\n return { _tag: \"Rejected\" } as const;\n if (!(yield* remainingEligibility(mapping, current, expected.credentialId)))\n return { _tag: \"LastSignInMethod\" } as const;\n\n const invalidation = checkInvalidation(mapping, input.invalidation),\n cr = mapping.credential,\n a = mapping.authority;\n\n const key = yield* oauthIdentityKey(expected.identity),\n t = mapping.ownership.tuple;\n\n const connected = mapping.connectedReference?.({\n identityKey: key,\n subjectId: copiedRow({ value: current.nativeId }).value,\n });\n\n const retain =\n connected !== undefined && (!referencesGuarded || (yield* owner.check(connected)));\n\n if (connected !== undefined && referencesGuarded)\n owner.postconditions.push(retain ? connected : sql`not (${connected})`);\n yield* owner.remove(cr.table, { [cr.credentialId]: expected.credentialId });\n yield* owner.remove(a.table, {\n [a.subjectId]: current.nativeId,\n [a.credentialId]: expected.credentialId,\n });\n if (!retain) {\n yield* owner.update(\n t.table,\n { [t.identityKey]: key },\n {\n [t.state]: \"Unowned\",\n [t.subjectId]: null,\n [t.reservation]: null,\n [t.version]: owner.marker,\n },\n );\n if (mapping.ownership.mode === \"separate\")\n yield* owner.remove(mapping.ownership.external.table, {\n [mapping.ownership.external.identityKey]: key,\n });\n }\n yield* advance(mapping, current, expected.credentialId);\n\n const result = snapshotOAuthSync(OAuthUnlinked, {\n _tag: \"Unlinked\",\n credentialId: expected.credentialId,\n invalidation,\n });\n\n const now = yield* owner.now(mapping.clock);\n\n invariant(\n Number.isSafeInteger(input.retentionUntilMillis) &&\n input.retentionUntilMillis > now &&\n input.retentionUntilMillis >= input.nowMillis + 120000,\n );\n const commandKey = { [r.moduleId]: input.moduleId, [r.commandId]: input.commandId };\n\n const values = {\n ...r.encodeInsert({\n moduleId: input.moduleId,\n commandId: input.commandId,\n credential: snapshotOAuthSync(OAuthCredentialSnapshot, expected),\n }),\n ...commandKey,\n [r.subjectId]: current.nativeId,\n [r.credentialId]: expected.credentialId,\n [r.intentSnapshot]: credentialStorage.encode(expected),\n [r.decision]: unlinkedStorage.encode(result),\n [r.retentionUntil]: mapping.clock.encodeInstant(input.retentionUntilMillis),\n };\n\n const inserted = yield* owner.insert(r.table, values, commandKey);\n\n command.rows = inserted.rows;\n\n return { _tag: \"Unlinked\", result, replayed: false } as const;\n }),\n );\n\n return { accountCurrent, issueLink, preflightLink, claimLink, settleLink, inspectUnlink, unlink };\n});\n\nexport class OAuthAccounts extends Context.Service<OAuthAccounts, Effect.Success<typeof make>>()(\n \"effect-auth/persistence/OAuthAccounts\",\n) {\n static readonly layer = Layer.effect(OAuthAccounts, make);\n}\n"],"mappings":";;;;;;;;;AAkCA,MAAMA,SAAO,OAAO,IAAI,aAAa;CACnC,MAAM,aAAa,OAAO;CAC1B,MAAM,OAAO,OAAO;CACpB,MAAM,eAAe,OAAO;CAE5B,MAAM,EAAE,IAAI,QAAQ;CAEpB,MAAM,EACJ,WACA,mBACA,gBACA,YACA,WACA,aACA,eACA,UACA,iBACE;CAEJ,MAAM,EAAE,MAAM,KAAK,WAAW,UAAU;CACxC,MAAM,EAAE,cAAc,cAAc;CAEpC,MAAM,iBAAiB,QAAQ,2BAA2B;CAE1D,MAAM,kBAAkB,QAAQ,aAAa;CAE7C,MAAM,kBAAkB,SAAc,cACpC,OAAO,IAAI,aAAa;EACtB,MAAM,SAAS,OAAQ,QAAQ,UAAsC,SAAS,SAAS;EAEvF,OAAO,OAAO,eAAe,SAAS,MAAM;CAC9C,CAAC;CAEH,MAAM,iBACJ,SACA,SACA,MACA,QACA,QACA,WACA,QACA,eAEA,OAAO,QAAQ,0BAA0B,UACvC,OAAO,IAAI,aAAa;EACtB,MAAM,MAAM,OAAO,MAAM,IAAI,QAAQ,KAAK;EAE1C,MAAM,qBAAqB,kBACzB,2BACA,QAAQ,QAAQ,wBAAwB,UAAU,QAAQ,GAAG,GAAG,MAAM,CACxE;EAiBA,IAAI,EAAC,OAfmB,YACtB,MACA;GACE,UAAU,KAAK,UAAU;GACzB;GACA;GACA;GACA,UAAU,QAAQ;GAClB;EACF,GACA,oBACA,KACA,UACF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IACtB,EAAE,eAAe,SAAS;IAC1B,EAAE,iBAAiB,kBAAkB,OAAO,QAAQ;IACpD,EAAE,WAAW,gBAAgB,OAAO,MAAM;IAC1C,EAAE,iBAAiB,QAAQ,MAAM,cAAc,MAAM,oBAAoB;EAC5E;EAIA,QAAQ,QAAO,OAFS,MAAM,OAAO,EAAE,OAAO,QAAQ,UAAU,EAAA,CAExC;EAExB,OAAO;GAAE,MAAM;GAAY;GAAQ,UAAU;EAAM;CACrD,CAAC,CACH;CAEF,OAAO;EAAE;EAAgB;EAAW;EAAe;EAAW;EAAY;EAAe;CAAO;AAClG,CAAC;AAED,IAAa,gBAAb,MAAa,sBAAsB,QAAQ,QAAoD,CAAC,CAC9F,uCACF,CAAC,CAAC;CACA,OAAgB,QAAQ,MAAM,OAAO,eAAeA,MAAI;AAC1D"}
@@ -0,0 +1,9 @@
1
+ import "../mapping-error.mjs";
2
+ import "./owner.mjs";
3
+ import "./connected-state.mjs";
4
+ import "./query-compiler.mjs";
5
+ import "./connected-custody.mjs";
6
+ import "./connected-flow.mjs";
7
+ import "./connected-settlement.mjs";
8
+ import { Context, Effect, Layer } from "effect";
9
+ import "@yielded/auth/OAuth";