@yielded/auth-persistence 0.1.0-beta.7

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 (526) hide show
  1. package/README.md +21 -0
  2. package/dist/Drizzle.d.mts +28 -0
  3. package/dist/Drizzle.mjs +28 -0
  4. package/dist/DrizzleD1.d.mts +963 -0
  5. package/dist/DrizzleD1.mjs +29 -0
  6. package/dist/DrizzleD1.mjs.map +1 -0
  7. package/dist/DrizzleLibsql.d.mts +2284 -0
  8. package/dist/DrizzleLibsql.mjs +92 -0
  9. package/dist/DrizzleLibsql.mjs.map +1 -0
  10. package/dist/DrizzleMysql2.d.mts +1820 -0
  11. package/dist/DrizzleMysql2.mjs +55 -0
  12. package/dist/DrizzleMysql2.mjs.map +1 -0
  13. package/dist/DrizzlePglite.d.mts +1826 -0
  14. package/dist/DrizzlePglite.mjs +53 -0
  15. package/dist/DrizzlePglite.mjs.map +1 -0
  16. package/dist/DrizzlePostgres.d.mts +1826 -0
  17. package/dist/DrizzlePostgres.mjs +53 -0
  18. package/dist/DrizzlePostgres.mjs.map +1 -0
  19. package/dist/DrizzleSqliteBun.d.mts +2279 -0
  20. package/dist/DrizzleSqliteBun.mjs +70 -0
  21. package/dist/DrizzleSqliteBun.mjs.map +1 -0
  22. package/dist/DrizzleSqliteDo.d.mts +2329 -0
  23. package/dist/DrizzleSqliteDo.mjs +105 -0
  24. package/dist/DrizzleSqliteDo.mjs.map +1 -0
  25. package/dist/DrizzleSqliteNode.d.mts +2279 -0
  26. package/dist/DrizzleSqliteNode.mjs +70 -0
  27. package/dist/DrizzleSqliteNode.mjs.map +1 -0
  28. package/dist/DrizzleSqliteWasm.d.mts +2282 -0
  29. package/dist/DrizzleSqliteWasm.mjs +70 -0
  30. package/dist/DrizzleSqliteWasm.mjs.map +1 -0
  31. package/dist/drizzle/D1BatchStatements.d.mts +11 -0
  32. package/dist/drizzle/D1BatchStatements.mjs +8 -0
  33. package/dist/drizzle/D1BatchStatements.mjs.map +1 -0
  34. package/dist/drizzle/PasswordPreparedPostconditions.mjs +12 -0
  35. package/dist/drizzle/PasswordPreparedPostconditions.mjs.map +1 -0
  36. package/dist/drizzle/SuppliedService.d.mts +10 -0
  37. package/dist/drizzle/d1-emails.d.mts +62 -0
  38. package/dist/drizzle/d1-emails.mjs +580 -0
  39. package/dist/drizzle/d1-emails.mjs.map +1 -0
  40. package/dist/drizzle/d1-generated-statement.d.mts +2 -0
  41. package/dist/drizzle/d1-generated-statement.mjs +79 -0
  42. package/dist/drizzle/d1-generated-statement.mjs.map +1 -0
  43. package/dist/drizzle/d1-identity.d.mts +31 -0
  44. package/dist/drizzle/d1-identity.mjs +119 -0
  45. package/dist/drizzle/d1-identity.mjs.map +1 -0
  46. package/dist/drizzle/d1-oauth.d.mts +301 -0
  47. package/dist/drizzle/d1-oauth.mjs +76 -0
  48. package/dist/drizzle/d1-oauth.mjs.map +1 -0
  49. package/dist/drizzle/d1-passkey.d.mts +51 -0
  50. package/dist/drizzle/d1-passkey.mjs +95 -0
  51. package/dist/drizzle/d1-passkey.mjs.map +1 -0
  52. package/dist/drizzle/d1-password-prepared.d.mts +25 -0
  53. package/dist/drizzle/d1-password-prepared.mjs +461 -0
  54. package/dist/drizzle/d1-password-prepared.mjs.map +1 -0
  55. package/dist/drizzle/d1-passwords.d.mts +87 -0
  56. package/dist/drizzle/d1-passwords.mjs +1004 -0
  57. package/dist/drizzle/d1-passwords.mjs.map +1 -0
  58. package/dist/drizzle/d1-phone.d.mts +26 -0
  59. package/dist/drizzle/d1-phone.mjs +30 -0
  60. package/dist/drizzle/d1-phone.mjs.map +1 -0
  61. package/dist/drizzle/d1-planning.d.mts +12 -0
  62. package/dist/drizzle/d1-planning.mjs +43 -0
  63. package/dist/drizzle/d1-planning.mjs.map +1 -0
  64. package/dist/drizzle/d1-proofs.d.mts +80 -0
  65. package/dist/drizzle/d1-proofs.mjs +830 -0
  66. package/dist/drizzle/d1-proofs.mjs.map +1 -0
  67. package/dist/drizzle/d1-sessions.d.mts +67 -0
  68. package/dist/drizzle/d1-sessions.mjs +966 -0
  69. package/dist/drizzle/d1-sessions.mjs.map +1 -0
  70. package/dist/drizzle/d1-totp.d.mts +26 -0
  71. package/dist/drizzle/d1-totp.mjs +27 -0
  72. package/dist/drizzle/d1-totp.mjs.map +1 -0
  73. package/dist/drizzle/email-model.d.mts +354 -0
  74. package/dist/drizzle/email-model.mjs +21 -0
  75. package/dist/drizzle/email-model.mjs.map +1 -0
  76. package/dist/drizzle/email-registration.d.mts +30 -0
  77. package/dist/drizzle/email-registration.mjs +238 -0
  78. package/dist/drizzle/email-registration.mjs.map +1 -0
  79. package/dist/drizzle/email-sql.d.mts +4 -0
  80. package/dist/drizzle/email-sql.mjs +9 -0
  81. package/dist/drizzle/email-sql.mjs.map +1 -0
  82. package/dist/drizzle/email-target.d.mts +20 -0
  83. package/dist/drizzle/email-target.mjs +72 -0
  84. package/dist/drizzle/email-target.mjs.map +1 -0
  85. package/dist/drizzle/model.d.mts +250 -0
  86. package/dist/drizzle/model.mjs +58 -0
  87. package/dist/drizzle/model.mjs.map +1 -0
  88. package/dist/drizzle/mysql-emails.d.mts +71 -0
  89. package/dist/drizzle/mysql-emails.mjs +38 -0
  90. package/dist/drizzle/mysql-emails.mjs.map +1 -0
  91. package/dist/drizzle/mysql-identity.d.mts +22 -0
  92. package/dist/drizzle/mysql-identity.mjs +94 -0
  93. package/dist/drizzle/mysql-identity.mjs.map +1 -0
  94. package/dist/drizzle/mysql-password-prepared.d.mts +30 -0
  95. package/dist/drizzle/mysql-password-prepared.mjs +16 -0
  96. package/dist/drizzle/mysql-password-prepared.mjs.map +1 -0
  97. package/dist/drizzle/mysql-passwords.d.mts +86 -0
  98. package/dist/drizzle/mysql-passwords.mjs +38 -0
  99. package/dist/drizzle/mysql-passwords.mjs.map +1 -0
  100. package/dist/drizzle/mysql-proofs.d.mts +75 -0
  101. package/dist/drizzle/mysql-proofs.mjs +23 -0
  102. package/dist/drizzle/mysql-proofs.mjs.map +1 -0
  103. package/dist/drizzle/mysql-sessions.d.mts +237 -0
  104. package/dist/drizzle/mysql-sessions.mjs +53 -0
  105. package/dist/drizzle/mysql-sessions.mjs.map +1 -0
  106. package/dist/drizzle/mysql.d.mts +70 -0
  107. package/dist/drizzle/mysql.mjs +218 -0
  108. package/dist/drizzle/mysql.mjs.map +1 -0
  109. package/dist/drizzle/oauth-accounts.mjs +358 -0
  110. package/dist/drizzle/oauth-accounts.mjs.map +1 -0
  111. package/dist/drizzle/oauth-connected-access.mjs +210 -0
  112. package/dist/drizzle/oauth-connected-access.mjs.map +1 -0
  113. package/dist/drizzle/oauth-connected-collection.mjs +202 -0
  114. package/dist/drizzle/oauth-connected-collection.mjs.map +1 -0
  115. package/dist/drizzle/oauth-connected-custody.mjs +146 -0
  116. package/dist/drizzle/oauth-connected-custody.mjs.map +1 -0
  117. package/dist/drizzle/oauth-connected-drivers.mjs +76 -0
  118. package/dist/drizzle/oauth-connected-drivers.mjs.map +1 -0
  119. package/dist/drizzle/oauth-connected-flow.mjs +247 -0
  120. package/dist/drizzle/oauth-connected-flow.mjs.map +1 -0
  121. package/dist/drizzle/oauth-connected-input.mjs +103 -0
  122. package/dist/drizzle/oauth-connected-input.mjs.map +1 -0
  123. package/dist/drizzle/oauth-connected-maintenance.mjs +214 -0
  124. package/dist/drizzle/oauth-connected-maintenance.mjs.map +1 -0
  125. package/dist/drizzle/oauth-connected-management.mjs +228 -0
  126. package/dist/drizzle/oauth-connected-management.mjs.map +1 -0
  127. package/dist/drizzle/oauth-connected-model.d.mts +278 -0
  128. package/dist/drizzle/oauth-connected-model.mjs +22 -0
  129. package/dist/drizzle/oauth-connected-model.mjs.map +1 -0
  130. package/dist/drizzle/oauth-connected-reference.d.mts +17 -0
  131. package/dist/drizzle/oauth-connected-reference.mjs +53 -0
  132. package/dist/drizzle/oauth-connected-reference.mjs.map +1 -0
  133. package/dist/drizzle/oauth-connected-settlement.mjs +117 -0
  134. package/dist/drizzle/oauth-connected-settlement.mjs.map +1 -0
  135. package/dist/drizzle/oauth-connected-state.d.mts +14 -0
  136. package/dist/drizzle/oauth-connected-state.mjs +422 -0
  137. package/dist/drizzle/oauth-connected-state.mjs.map +1 -0
  138. package/dist/drizzle/oauth-connected-target.d.mts +753 -0
  139. package/dist/drizzle/oauth-connected-target.mjs +91 -0
  140. package/dist/drizzle/oauth-connected-target.mjs.map +1 -0
  141. package/dist/drizzle/oauth-drivers.mjs +51 -0
  142. package/dist/drizzle/oauth-drivers.mjs.map +1 -0
  143. package/dist/drizzle/oauth-flow.d.mts +5 -0
  144. package/dist/drizzle/oauth-flow.mjs +253 -0
  145. package/dist/drizzle/oauth-flow.mjs.map +1 -0
  146. package/dist/drizzle/oauth-input.mjs +127 -0
  147. package/dist/drizzle/oauth-input.mjs.map +1 -0
  148. package/dist/drizzle/oauth-model.d.mts +463 -0
  149. package/dist/drizzle/oauth-model.mjs +49 -0
  150. package/dist/drizzle/oauth-model.mjs.map +1 -0
  151. package/dist/drizzle/oauth-mysql.mjs +8 -0
  152. package/dist/drizzle/oauth-mysql.mjs.map +1 -0
  153. package/dist/drizzle/oauth-owner.d.mts +4 -0
  154. package/dist/drizzle/oauth-owner.mjs +10 -0
  155. package/dist/drizzle/oauth-owner.mjs.map +1 -0
  156. package/dist/drizzle/oauth-registration.d.mts +6 -0
  157. package/dist/drizzle/oauth-registration.mjs +374 -0
  158. package/dist/drizzle/oauth-registration.mjs.map +1 -0
  159. package/dist/drizzle/oauth-state.mjs +107 -0
  160. package/dist/drizzle/oauth-state.mjs.map +1 -0
  161. package/dist/drizzle/oauth-target.d.mts +111 -0
  162. package/dist/drizzle/oauth-target.mjs +246 -0
  163. package/dist/drizzle/oauth-target.mjs.map +1 -0
  164. package/dist/drizzle/passkey-drivers.mjs +34 -0
  165. package/dist/drizzle/passkey-drivers.mjs.map +1 -0
  166. package/dist/drizzle/passkey-kernel.mjs +8 -0
  167. package/dist/drizzle/passkey-kernel.mjs.map +1 -0
  168. package/dist/drizzle/passkey-model.d.mts +272 -0
  169. package/dist/drizzle/passkey-model.mjs +34 -0
  170. package/dist/drizzle/passkey-model.mjs.map +1 -0
  171. package/dist/drizzle/passkey-registration-ceremony-model.d.mts +50 -0
  172. package/dist/drizzle/passkey-registration-ceremony-model.mjs +18 -0
  173. package/dist/drizzle/passkey-registration-ceremony-model.mjs.map +1 -0
  174. package/dist/drizzle/passkey-state.mjs +7 -0
  175. package/dist/drizzle/passkey-state.mjs.map +1 -0
  176. package/dist/drizzle/passkey-target.d.mts +9 -0
  177. package/dist/drizzle/passkey-target.mjs +7 -0
  178. package/dist/drizzle/passkey-target.mjs.map +1 -0
  179. package/dist/drizzle/passkey-write-drivers.mjs +32 -0
  180. package/dist/drizzle/passkey-write-drivers.mjs.map +1 -0
  181. package/dist/drizzle/passkey-write-model.d.mts +190 -0
  182. package/dist/drizzle/passkey-write-model.mjs +13 -0
  183. package/dist/drizzle/passkey-write-model.mjs.map +1 -0
  184. package/dist/drizzle/passkey-write-target.d.mts +11 -0
  185. package/dist/drizzle/passkey-write-target.mjs +7 -0
  186. package/dist/drizzle/passkey-write-target.mjs.map +1 -0
  187. package/dist/drizzle/password-model.d.mts +329 -0
  188. package/dist/drizzle/password-model.mjs +23 -0
  189. package/dist/drizzle/password-model.mjs.map +1 -0
  190. package/dist/drizzle/password-prepared-model.d.mts +94 -0
  191. package/dist/drizzle/password-prepared-model.mjs +11 -0
  192. package/dist/drizzle/password-prepared-model.mjs.map +1 -0
  193. package/dist/drizzle/password-prepared-sql.mjs +390 -0
  194. package/dist/drizzle/password-prepared-sql.mjs.map +1 -0
  195. package/dist/drizzle/password-prepared-state.mjs +104 -0
  196. package/dist/drizzle/password-prepared-state.mjs.map +1 -0
  197. package/dist/drizzle/password-prepared-target.d.mts +12 -0
  198. package/dist/drizzle/password-prepared-target.mjs +56 -0
  199. package/dist/drizzle/password-prepared-target.mjs.map +1 -0
  200. package/dist/drizzle/password-registration.d.mts +19 -0
  201. package/dist/drizzle/password-registration.mjs +135 -0
  202. package/dist/drizzle/password-registration.mjs.map +1 -0
  203. package/dist/drizzle/password-sql.d.mts +5 -0
  204. package/dist/drizzle/password-sql.mjs +9 -0
  205. package/dist/drizzle/password-sql.mjs.map +1 -0
  206. package/dist/drizzle/password-target.d.mts +17 -0
  207. package/dist/drizzle/password-target.mjs +61 -0
  208. package/dist/drizzle/password-target.mjs.map +1 -0
  209. package/dist/drizzle/pg-emails.d.mts +72 -0
  210. package/dist/drizzle/pg-emails.mjs +38 -0
  211. package/dist/drizzle/pg-emails.mjs.map +1 -0
  212. package/dist/drizzle/pg-password-prepared.d.mts +31 -0
  213. package/dist/drizzle/pg-password-prepared.mjs +16 -0
  214. package/dist/drizzle/pg-password-prepared.mjs.map +1 -0
  215. package/dist/drizzle/pg-passwords.d.mts +87 -0
  216. package/dist/drizzle/pg-passwords.mjs +38 -0
  217. package/dist/drizzle/pg-passwords.mjs.map +1 -0
  218. package/dist/drizzle/pg-proofs.d.mts +76 -0
  219. package/dist/drizzle/pg-proofs.mjs +22 -0
  220. package/dist/drizzle/pg-proofs.mjs.map +1 -0
  221. package/dist/drizzle/pg-sessions.d.mts +238 -0
  222. package/dist/drizzle/pg-sessions.mjs +53 -0
  223. package/dist/drizzle/pg-sessions.mjs.map +1 -0
  224. package/dist/drizzle/pg.d.mts +81 -0
  225. package/dist/drizzle/pg.mjs +290 -0
  226. package/dist/drizzle/pg.mjs.map +1 -0
  227. package/dist/drizzle/phone-model.d.mts +115 -0
  228. package/dist/drizzle/phone-model.mjs +39 -0
  229. package/dist/drizzle/phone-model.mjs.map +1 -0
  230. package/dist/drizzle/phone-state.mjs +16 -0
  231. package/dist/drizzle/phone-state.mjs.map +1 -0
  232. package/dist/drizzle/phone-target.d.mts +17 -0
  233. package/dist/drizzle/phone-target.mjs +170 -0
  234. package/dist/drizzle/phone-target.mjs.map +1 -0
  235. package/dist/drizzle/proof-model.d.mts +286 -0
  236. package/dist/drizzle/proof-model.mjs +17 -0
  237. package/dist/drizzle/proof-model.mjs.map +1 -0
  238. package/dist/drizzle/proof-sql.d.mts +4 -0
  239. package/dist/drizzle/proof-sql.mjs +8 -0
  240. package/dist/drizzle/proof-sql.mjs.map +1 -0
  241. package/dist/drizzle/proof-target.d.mts +12 -0
  242. package/dist/drizzle/proof-target.mjs +36 -0
  243. package/dist/drizzle/proof-target.mjs.map +1 -0
  244. package/dist/drizzle/query-operations.mjs +26 -0
  245. package/dist/drizzle/query-operations.mjs.map +1 -0
  246. package/dist/drizzle/session-model.d.mts +200 -0
  247. package/dist/drizzle/session-model.mjs +20 -0
  248. package/dist/drizzle/session-model.mjs.map +1 -0
  249. package/dist/drizzle/session-sql.mjs +8 -0
  250. package/dist/drizzle/session-sql.mjs.map +1 -0
  251. package/dist/drizzle/session-target.d.mts +28 -0
  252. package/dist/drizzle/session-target.mjs +47 -0
  253. package/dist/drizzle/session-target.mjs.map +1 -0
  254. package/dist/drizzle/sqlite-emails.mjs +45 -0
  255. package/dist/drizzle/sqlite-emails.mjs.map +1 -0
  256. package/dist/drizzle/sqlite-identity.mjs +91 -0
  257. package/dist/drizzle/sqlite-identity.mjs.map +1 -0
  258. package/dist/drizzle/sqlite-password-prepared.mjs +21 -0
  259. package/dist/drizzle/sqlite-password-prepared.mjs.map +1 -0
  260. package/dist/drizzle/sqlite-passwords.mjs +49 -0
  261. package/dist/drizzle/sqlite-passwords.mjs.map +1 -0
  262. package/dist/drizzle/sqlite-proofs.mjs +29 -0
  263. package/dist/drizzle/sqlite-proofs.mjs.map +1 -0
  264. package/dist/drizzle/sqlite-sessions.mjs +74 -0
  265. package/dist/drizzle/sqlite-sessions.mjs.map +1 -0
  266. package/dist/drizzle/sqlite.d.mts +78 -0
  267. package/dist/drizzle/sqlite.mjs +217 -0
  268. package/dist/drizzle/sqlite.mjs.map +1 -0
  269. package/dist/drizzle/standalone-guard.mjs +19 -0
  270. package/dist/drizzle/standalone-guard.mjs.map +1 -0
  271. package/dist/drizzle/step-up-model.d.mts +65 -0
  272. package/dist/drizzle/step-up-model.mjs +9 -0
  273. package/dist/drizzle/step-up-model.mjs.map +1 -0
  274. package/dist/drizzle/step-up-state.mjs +32 -0
  275. package/dist/drizzle/step-up-state.mjs.map +1 -0
  276. package/dist/drizzle/totp-model.d.mts +84 -0
  277. package/dist/drizzle/totp-model.mjs +13 -0
  278. package/dist/drizzle/totp-model.mjs.map +1 -0
  279. package/dist/drizzle/totp-state.d.mts +38 -0
  280. package/dist/drizzle/totp-state.mjs +287 -0
  281. package/dist/drizzle/totp-state.mjs.map +1 -0
  282. package/dist/drizzle/totp-target.d.mts +17 -0
  283. package/dist/drizzle/totp-target.mjs +81 -0
  284. package/dist/drizzle/totp-target.mjs.map +1 -0
  285. package/dist/drizzle/transaction-execution.d.mts +4 -0
  286. package/dist/drizzle/transaction-execution.mjs +11 -0
  287. package/dist/drizzle/transaction-execution.mjs.map +1 -0
  288. package/dist/drizzle/transaction-mysql.mjs +46 -0
  289. package/dist/drizzle/transaction-mysql.mjs.map +1 -0
  290. package/dist/drizzle/transaction-owner.d.mts +2 -0
  291. package/dist/drizzle/transaction-owner.mjs +8 -0
  292. package/dist/drizzle/transaction-owner.mjs.map +1 -0
  293. package/dist/index.d.mts +5 -0
  294. package/dist/index.mjs +5 -0
  295. package/dist/internal/configuration.d.mts +174 -0
  296. package/dist/internal/configuration.mjs +7 -0
  297. package/dist/internal/configuration.mjs.map +1 -0
  298. package/dist/internal/drizzle-migrations.mjs +19 -0
  299. package/dist/internal/drizzle-migrations.mjs.map +1 -0
  300. package/dist/internal/drizzle-postgres.mjs +39 -0
  301. package/dist/internal/drizzle-postgres.mjs.map +1 -0
  302. package/dist/internal/drizzle-sqlite.mjs +37 -0
  303. package/dist/internal/drizzle-sqlite.mjs.map +1 -0
  304. package/dist/internal/email-kernel.d.mts +13 -0
  305. package/dist/internal/email-kernel.mjs +487 -0
  306. package/dist/internal/email-kernel.mjs.map +1 -0
  307. package/dist/internal/mapping-error.d.mts +11 -0
  308. package/dist/internal/mapping-error.mjs +30 -0
  309. package/dist/internal/mapping-error.mjs.map +1 -0
  310. package/dist/internal/passkey/admission.d.mts +7 -0
  311. package/dist/internal/passkey/admission.mjs +216 -0
  312. package/dist/internal/passkey/admission.mjs.map +1 -0
  313. package/dist/internal/passkey/credentials.d.mts +6 -0
  314. package/dist/internal/passkey/credentials.mjs +249 -0
  315. package/dist/internal/passkey/credentials.mjs.map +1 -0
  316. package/dist/internal/passkey/enrollment.mjs +138 -0
  317. package/dist/internal/passkey/enrollment.mjs.map +1 -0
  318. package/dist/internal/passkey/flow.d.mts +10 -0
  319. package/dist/internal/passkey/flow.mjs +452 -0
  320. package/dist/internal/passkey/flow.mjs.map +1 -0
  321. package/dist/internal/passkey/management.mjs +231 -0
  322. package/dist/internal/passkey/management.mjs.map +1 -0
  323. package/dist/internal/passkey/registration-ceremony.d.mts +9 -0
  324. package/dist/internal/passkey/registration-ceremony.mjs +129 -0
  325. package/dist/internal/passkey/registration-ceremony.mjs.map +1 -0
  326. package/dist/internal/passkey/registration-custody.d.mts +2 -0
  327. package/dist/internal/passkey/registration-custody.mjs +52 -0
  328. package/dist/internal/passkey/registration-custody.mjs.map +1 -0
  329. package/dist/internal/passkey/registration-write.mjs +211 -0
  330. package/dist/internal/passkey/registration-write.mjs.map +1 -0
  331. package/dist/internal/passkey/state.d.mts +5 -0
  332. package/dist/internal/passkey/state.mjs +234 -0
  333. package/dist/internal/passkey/state.mjs.map +1 -0
  334. package/dist/internal/passkey/target.d.mts +19 -0
  335. package/dist/internal/passkey/target.mjs +190 -0
  336. package/dist/internal/passkey/target.mjs.map +1 -0
  337. package/dist/internal/passkey/write-state.mjs +345 -0
  338. package/dist/internal/passkey/write-state.mjs.map +1 -0
  339. package/dist/internal/passkey/write-target.mjs +192 -0
  340. package/dist/internal/passkey/write-target.mjs.map +1 -0
  341. package/dist/internal/passkey-kernel.mjs +49 -0
  342. package/dist/internal/passkey-kernel.mjs.map +1 -0
  343. package/dist/internal/passkeys.mjs +367 -0
  344. package/dist/internal/passkeys.mjs.map +1 -0
  345. package/dist/internal/password-kernel.d.mts +13 -0
  346. package/dist/internal/password-kernel.mjs +704 -0
  347. package/dist/internal/password-kernel.mjs.map +1 -0
  348. package/dist/internal/persistence.mjs +289 -0
  349. package/dist/internal/persistence.mjs.map +1 -0
  350. package/dist/internal/phone-kernel.mjs +434 -0
  351. package/dist/internal/phone-kernel.mjs.map +1 -0
  352. package/dist/internal/proof-kernel.d.mts +9 -0
  353. package/dist/internal/proof-kernel.mjs +619 -0
  354. package/dist/internal/proof-kernel.mjs.map +1 -0
  355. package/dist/internal/query-operations.d.mts +3 -0
  356. package/dist/internal/registration.mjs +65 -0
  357. package/dist/internal/registration.mjs.map +1 -0
  358. package/dist/internal/session-kernel.mjs +737 -0
  359. package/dist/internal/session-kernel.mjs.map +1 -0
  360. package/dist/internal/sql-persistence.d.mts +11 -0
  361. package/dist/internal/sql-persistence.mjs +37 -0
  362. package/dist/internal/sql-persistence.mjs.map +1 -0
  363. package/dist/internal/sql-query.mjs +207 -0
  364. package/dist/internal/sql-query.mjs.map +1 -0
  365. package/dist/internal/sql-table.d.mts +37 -0
  366. package/dist/internal/sql-table.mjs +71 -0
  367. package/dist/internal/sql-table.mjs.map +1 -0
  368. package/dist/internal/storage-mapping.mjs +687 -0
  369. package/dist/internal/storage-mapping.mjs.map +1 -0
  370. package/dist/internal/storage-tables.d.mts +914 -0
  371. package/dist/internal/storage-tables.mjs +387 -0
  372. package/dist/internal/storage-tables.mjs.map +1 -0
  373. package/dist/internal/storage-validation.mjs +58 -0
  374. package/dist/internal/storage-validation.mjs.map +1 -0
  375. package/dist/internal/transaction-execution-kernel.d.mts +18 -0
  376. package/dist/internal/transaction-execution-kernel.mjs +118 -0
  377. package/dist/internal/transaction-execution-kernel.mjs.map +1 -0
  378. package/dist/internal/transaction-kernel.d.mts +7 -0
  379. package/dist/internal/transaction-kernel.mjs +260 -0
  380. package/dist/internal/transaction-kernel.mjs.map +1 -0
  381. package/package.json +1 -0
  382. package/src/Drizzle.ts +378 -0
  383. package/src/DrizzleD1.ts +136 -0
  384. package/src/DrizzleLibsql.ts +301 -0
  385. package/src/DrizzleMysql2.ts +184 -0
  386. package/src/DrizzlePglite.ts +174 -0
  387. package/src/DrizzlePostgres.ts +174 -0
  388. package/src/DrizzleSqliteBun.ts +258 -0
  389. package/src/DrizzleSqliteDo.ts +473 -0
  390. package/src/DrizzleSqliteNode.ts +259 -0
  391. package/src/DrizzleSqliteWasm.ts +263 -0
  392. package/src/drizzle/D1BatchStatements.ts +10 -0
  393. package/src/drizzle/PasswordPreparedPostconditions.ts +30 -0
  394. package/src/drizzle/SuppliedService.ts +10 -0
  395. package/src/drizzle/d1-emails.ts +1829 -0
  396. package/src/drizzle/d1-generated-statement.ts +129 -0
  397. package/src/drizzle/d1-identity.ts +375 -0
  398. package/src/drizzle/d1-oauth.ts +340 -0
  399. package/src/drizzle/d1-passkey.ts +373 -0
  400. package/src/drizzle/d1-password-prepared.ts +1270 -0
  401. package/src/drizzle/d1-passwords.ts +2656 -0
  402. package/src/drizzle/d1-phone.ts +96 -0
  403. package/src/drizzle/d1-planning.ts +104 -0
  404. package/src/drizzle/d1-proofs.ts +2305 -0
  405. package/src/drizzle/d1-sessions.ts +2857 -0
  406. package/src/drizzle/d1-totp.ts +78 -0
  407. package/src/drizzle/email-model.ts +516 -0
  408. package/src/drizzle/email-registration.ts +698 -0
  409. package/src/drizzle/email-sql.ts +24 -0
  410. package/src/drizzle/email-target.ts +243 -0
  411. package/src/drizzle/model.ts +463 -0
  412. package/src/drizzle/mysql-emails.ts +321 -0
  413. package/src/drizzle/mysql-identity.ts +324 -0
  414. package/src/drizzle/mysql-password-prepared.ts +204 -0
  415. package/src/drizzle/mysql-passwords.ts +339 -0
  416. package/src/drizzle/mysql-proofs.ts +181 -0
  417. package/src/drizzle/mysql-sessions.ts +741 -0
  418. package/src/drizzle/mysql.ts +687 -0
  419. package/src/drizzle/oauth-accounts.ts +804 -0
  420. package/src/drizzle/oauth-connected-access.ts +354 -0
  421. package/src/drizzle/oauth-connected-collection.ts +324 -0
  422. package/src/drizzle/oauth-connected-custody.ts +277 -0
  423. package/src/drizzle/oauth-connected-drivers.ts +465 -0
  424. package/src/drizzle/oauth-connected-flow.ts +441 -0
  425. package/src/drizzle/oauth-connected-input.ts +127 -0
  426. package/src/drizzle/oauth-connected-maintenance.ts +345 -0
  427. package/src/drizzle/oauth-connected-management.ts +365 -0
  428. package/src/drizzle/oauth-connected-model.ts +347 -0
  429. package/src/drizzle/oauth-connected-reference.ts +112 -0
  430. package/src/drizzle/oauth-connected-settlement.ts +226 -0
  431. package/src/drizzle/oauth-connected-state.ts +845 -0
  432. package/src/drizzle/oauth-connected-target.ts +264 -0
  433. package/src/drizzle/oauth-drivers.ts +569 -0
  434. package/src/drizzle/oauth-flow.ts +462 -0
  435. package/src/drizzle/oauth-input.ts +201 -0
  436. package/src/drizzle/oauth-model.ts +617 -0
  437. package/src/drizzle/oauth-mysql.ts +10 -0
  438. package/src/drizzle/oauth-owner.ts +29 -0
  439. package/src/drizzle/oauth-registration.ts +640 -0
  440. package/src/drizzle/oauth-state.ts +215 -0
  441. package/src/drizzle/oauth-target.ts +750 -0
  442. package/src/drizzle/passkey-drivers.ts +385 -0
  443. package/src/drizzle/passkey-kernel.ts +3 -0
  444. package/src/drizzle/passkey-model.ts +353 -0
  445. package/src/drizzle/passkey-registration-ceremony-model.ts +69 -0
  446. package/src/drizzle/passkey-state.ts +3 -0
  447. package/src/drizzle/passkey-target.ts +23 -0
  448. package/src/drizzle/passkey-write-drivers.ts +395 -0
  449. package/src/drizzle/passkey-write-model.ts +271 -0
  450. package/src/drizzle/passkey-write-target.ts +9 -0
  451. package/src/drizzle/password-model.ts +491 -0
  452. package/src/drizzle/password-prepared-model.ts +259 -0
  453. package/src/drizzle/password-prepared-sql.ts +1191 -0
  454. package/src/drizzle/password-prepared-state.ts +231 -0
  455. package/src/drizzle/password-prepared-target.ts +160 -0
  456. package/src/drizzle/password-registration.ts +374 -0
  457. package/src/drizzle/password-sql.ts +12 -0
  458. package/src/drizzle/password-target.ts +224 -0
  459. package/src/drizzle/pg-emails.ts +321 -0
  460. package/src/drizzle/pg-password-prepared.ts +205 -0
  461. package/src/drizzle/pg-passwords.ts +338 -0
  462. package/src/drizzle/pg-proofs.ts +191 -0
  463. package/src/drizzle/pg-sessions.ts +750 -0
  464. package/src/drizzle/pg.ts +984 -0
  465. package/src/drizzle/phone-model.ts +190 -0
  466. package/src/drizzle/phone-state.ts +21 -0
  467. package/src/drizzle/phone-target.ts +472 -0
  468. package/src/drizzle/proof-model.ts +398 -0
  469. package/src/drizzle/proof-sql.ts +9 -0
  470. package/src/drizzle/proof-target.ts +122 -0
  471. package/src/drizzle/query-operations.ts +37 -0
  472. package/src/drizzle/session-model.ts +370 -0
  473. package/src/drizzle/session-sql.ts +13 -0
  474. package/src/drizzle/session-target.ts +338 -0
  475. package/src/drizzle/sqlite-emails.ts +366 -0
  476. package/src/drizzle/sqlite-identity.ts +344 -0
  477. package/src/drizzle/sqlite-password-prepared.ts +260 -0
  478. package/src/drizzle/sqlite-passwords.ts +497 -0
  479. package/src/drizzle/sqlite-proofs.ts +202 -0
  480. package/src/drizzle/sqlite-sessions.ts +971 -0
  481. package/src/drizzle/sqlite.ts +705 -0
  482. package/src/drizzle/standalone-guard.ts +38 -0
  483. package/src/drizzle/step-up-model.ts +128 -0
  484. package/src/drizzle/step-up-state.ts +139 -0
  485. package/src/drizzle/totp-model.ts +92 -0
  486. package/src/drizzle/totp-state.ts +525 -0
  487. package/src/drizzle/totp-target.ts +308 -0
  488. package/src/drizzle/transaction-execution.ts +12 -0
  489. package/src/drizzle/transaction-mysql.ts +104 -0
  490. package/src/drizzle/transaction-owner.ts +10 -0
  491. package/src/index.ts +4 -0
  492. package/src/internal/configuration.ts +282 -0
  493. package/src/internal/drizzle-migrations.ts +30 -0
  494. package/src/internal/drizzle-postgres.ts +81 -0
  495. package/src/internal/drizzle-sqlite.ts +79 -0
  496. package/src/internal/email-kernel.ts +1532 -0
  497. package/src/internal/mapping-error.ts +36 -0
  498. package/src/internal/passkey/admission.ts +362 -0
  499. package/src/internal/passkey/credentials.ts +511 -0
  500. package/src/internal/passkey/enrollment.ts +326 -0
  501. package/src/internal/passkey/flow.ts +1062 -0
  502. package/src/internal/passkey/management.ts +422 -0
  503. package/src/internal/passkey/registration-ceremony.ts +283 -0
  504. package/src/internal/passkey/registration-custody.ts +91 -0
  505. package/src/internal/passkey/registration-write.ts +426 -0
  506. package/src/internal/passkey/state.ts +354 -0
  507. package/src/internal/passkey/target.ts +586 -0
  508. package/src/internal/passkey/write-state.ts +681 -0
  509. package/src/internal/passkey/write-target.ts +452 -0
  510. package/src/internal/passkey-kernel.ts +102 -0
  511. package/src/internal/passkeys.ts +597 -0
  512. package/src/internal/password-kernel.ts +2122 -0
  513. package/src/internal/persistence.ts +590 -0
  514. package/src/internal/phone-kernel.ts +843 -0
  515. package/src/internal/proof-kernel.ts +1949 -0
  516. package/src/internal/query-operations.ts +26 -0
  517. package/src/internal/registration.ts +160 -0
  518. package/src/internal/session-kernel.ts +2210 -0
  519. package/src/internal/sql-persistence.ts +45 -0
  520. package/src/internal/sql-query.ts +298 -0
  521. package/src/internal/sql-table.ts +89 -0
  522. package/src/internal/storage-mapping.ts +706 -0
  523. package/src/internal/storage-tables.ts +408 -0
  524. package/src/internal/storage-validation.ts +117 -0
  525. package/src/internal/transaction-execution-kernel.ts +317 -0
  526. package/src/internal/transaction-kernel.ts +582 -0
@@ -0,0 +1,2122 @@
1
+ import {
2
+ coordinateCommit,
3
+ CurrentCommitJournal,
4
+ hasCommitScope,
5
+ type CommitJournal,
6
+ type PreparedCommit,
7
+ LifecycleHooks,
8
+ HookConfigurationError,
9
+ } from "@yielded/auth/Hooks";
10
+ import type { LoginIdentifier } from "@yielded/auth/Identity";
11
+ import {
12
+ PasswordUnavailable,
13
+ PasswordPersistence,
14
+ type PasswordMutationInput,
15
+ snapshotPasswordCredential,
16
+ snapshotPasswordRequirement,
17
+ snapshotPasswordRevision,
18
+ type PasswordAction,
19
+ type PasswordAttemptDecision,
20
+ type PasswordCredentialSnapshot,
21
+ type PasswordMutationDecision,
22
+ type PasswordAttemptPolicy,
23
+ } from "@yielded/auth/Password";
24
+ import { reportPersistenceFailure } from "@yielded/auth/Persistence";
25
+ import {
26
+ type ProofCompletionPlan,
27
+ type ProofCompletionInput,
28
+ ProofUnavailable,
29
+ } from "@yielded/auth/Proofs";
30
+ import {
31
+ snapshotAuthenticationEvidence,
32
+ type AuthenticationEvidence,
33
+ type AuthenticationRequirement,
34
+ type SecurityRevision,
35
+ } from "@yielded/auth/Sessions";
36
+ /* oxlint-disable no-explicit-any -- existing storage kernels erase foreign table shapes; domain errors remain typed. */
37
+ import type { sql } from "drizzle-orm";
38
+ import type { EffectDrizzleQueryError } from "drizzle-orm/effect-core";
39
+ import { Cause, Context, DateTime, Effect, Option, Redacted, Schema } from "effect";
40
+ import type * as SqlError from "effect/unstable/sql/SqlError";
41
+
42
+ import {
43
+ type AnyPasswordPersistenceMapping,
44
+ type PasswordAttemptAction,
45
+ type PasswordRateScopeKind,
46
+ type PasswordScopeKeys,
47
+ requiredPasswordConstraints,
48
+ } from "../drizzle/password-model";
49
+ import { PersistenceMappingError } from "./mapping-error";
50
+ import {
51
+ CurrentProofSql,
52
+ type ProofSqlConfiguration,
53
+ type ProofSqlDatabase,
54
+ type makeProofKernel,
55
+ } from "./proof-kernel";
56
+ import type { QueryOperations } from "./query-operations";
57
+
58
+ type AdapterFailure = EffectDrizzleQueryError | PersistenceMappingError | SqlError.SqlError;
59
+
60
+ export interface PasswordSqlQuery<A = ReadonlyArray<any>> extends Effect.Effect<A, AdapterFailure> {
61
+ readonly getSQL: () => ReturnType<typeof sql>;
62
+ readonly from: (...args: ReadonlyArray<any>) => PasswordSqlQuery<A>;
63
+ readonly where: (...args: ReadonlyArray<any>) => PasswordSqlQuery<A>;
64
+ readonly limit: (...args: ReadonlyArray<any>) => PasswordSqlQuery<A>;
65
+ readonly orderBy: (...args: ReadonlyArray<any>) => PasswordSqlQuery<A>;
66
+ readonly for: (...args: ReadonlyArray<any>) => PasswordSqlQuery<A>;
67
+ readonly set: (...args: ReadonlyArray<any>) => PasswordSqlQuery<A>;
68
+ readonly values: (...args: ReadonlyArray<any>) => PasswordSqlQuery<A>;
69
+ readonly returning: (...args: ReadonlyArray<any>) => PasswordSqlQuery<A>;
70
+ readonly $returningId: (...args: ReadonlyArray<any>) => PasswordSqlQuery<A>;
71
+ readonly onConflictDoNothing: (...args: ReadonlyArray<any>) => PasswordSqlQuery<A>;
72
+ readonly onDuplicateKeyUpdate: (...args: ReadonlyArray<any>) => PasswordSqlQuery<A>;
73
+ }
74
+
75
+ export interface PasswordSqlDatabase {
76
+ readonly select: (...args: ReadonlyArray<any>) => PasswordSqlQuery;
77
+ readonly insert: (...args: ReadonlyArray<any>) => PasswordSqlQuery;
78
+ readonly update: (...args: ReadonlyArray<any>) => PasswordSqlQuery;
79
+ readonly delete: (...args: ReadonlyArray<any>) => PasswordSqlQuery;
80
+ readonly transaction: <A, E, R>(
81
+ body: (transaction: PasswordSqlDatabase) => Effect.Effect<A, E, R>,
82
+ ) => Effect.Effect<A, E | SqlError.SqlError, R>;
83
+ }
84
+
85
+ export class CurrentPasswordSql extends Context.Service<CurrentPasswordSql, PasswordSqlDatabase>()(
86
+ "effect-auth/CurrentPasswordSql",
87
+ ) {}
88
+
89
+ export interface PasswordSqlConfiguration {
90
+ readonly mode: "interactive" | "synchronous";
91
+ readonly locking: boolean;
92
+ readonly standaloneGuard: Effect.Effect<void, PasswordUnavailable>;
93
+ readonly coordinated?: boolean;
94
+ readonly insertIfAbsent: (
95
+ query: PasswordSqlQuery,
96
+ selfKey: string,
97
+ selfValue: unknown,
98
+ ) => PasswordSqlQuery;
99
+ readonly proof?: {
100
+ readonly mapping: any;
101
+ readonly configuration: ProofSqlConfiguration;
102
+ };
103
+ }
104
+
105
+ type Database = PasswordSqlDatabase;
106
+
107
+ type Mapping = AnyPasswordPersistenceMapping;
108
+
109
+ interface ScopeEntry {
110
+ readonly kind: PasswordRateScopeKind;
111
+ readonly key: string;
112
+ readonly limit: number;
113
+ readonly windowMillis: number;
114
+ }
115
+
116
+ export const makePasswordKernel = (
117
+ operations: QueryOperations,
118
+ proofs: Pick<ReturnType<typeof makeProofKernel>, "completeProofPlanIn">,
119
+ ) => {
120
+ const { and, eq, gte, inArray, lte, notExists, sql, column, updateValues } = operations;
121
+ const { completeProofPlanIn } = proofs;
122
+ const unavailable = () => PasswordUnavailable.make({});
123
+
124
+ const translateFailure = <A, E, R>(
125
+ effect: Effect.Effect<A, E, R>,
126
+ ): Effect.Effect<A, PasswordUnavailable, R> =>
127
+ reportPersistenceFailure(
128
+ effect,
129
+ (error) =>
130
+ Schema.is(PasswordUnavailable)(error) ||
131
+ Schema.is(ProofUnavailable)(error) ||
132
+ Schema.is(HookConfigurationError)(error),
133
+ ).pipe(Effect.catchCause((cause) => Effect.failCause(Cause.map(cause, () => unavailable()))));
134
+
135
+ const nowMillis = DateTime.now.pipe(Effect.map(DateTime.toEpochMillis));
136
+
137
+ const validConstraints = (mapping: Mapping) =>
138
+ Object.entries(requiredPasswordConstraints).every(
139
+ ([key, value]) =>
140
+ mapping.constraints[key as keyof typeof requiredPasswordConstraints] === value,
141
+ );
142
+
143
+ const selectRows = (query: PasswordSqlQuery, locking: boolean) =>
144
+ locking && typeof query.for === "function" ? query.for("update") : query;
145
+
146
+ const subjectColumns = (mapping: Mapping) => ({
147
+ id: column(mapping.subject.table, mapping.subject.id),
148
+ status: column(mapping.subject.table, mapping.subject.status),
149
+ securityRevision: column(mapping.subject.table, mapping.subject.securityRevision),
150
+ });
151
+
152
+ const identifierColumns = (mapping: Mapping) => ({
153
+ namespace: column(mapping.identifier.table, mapping.identifier.namespace),
154
+ value: column(mapping.identifier.table, mapping.identifier.value),
155
+ subjectId: column(mapping.identifier.table, mapping.identifier.subjectId),
156
+ verifiedAt: column(mapping.identifier.table, mapping.identifier.verifiedAt),
157
+ bindingRevision: column(mapping.identifier.table, mapping.identifier.bindingRevision),
158
+ });
159
+
160
+ const credentialColumns = (mapping: Mapping) => ({
161
+ moduleId: column(mapping.credential.table, mapping.credential.moduleId),
162
+ subjectId: column(mapping.credential.table, mapping.credential.subjectId),
163
+ credentialId: column(mapping.credential.table, mapping.credential.credentialId),
164
+ credentialRevision: column(mapping.credential.table, mapping.credential.credentialRevision),
165
+ verifierVersion: column(mapping.credential.table, mapping.credential.verifierVersion),
166
+ verifier: column(mapping.credential.table, mapping.credential.verifier),
167
+ normalization: column(mapping.credential.table, mapping.credential.normalization),
168
+ });
169
+
170
+ const authorityCredentialColumns = (mapping: Mapping) => ({
171
+ subjectId: column(mapping.authorityCredential.table, mapping.authorityCredential.subjectId),
172
+ credentialId: column(
173
+ mapping.authorityCredential.table,
174
+ mapping.authorityCredential.credentialId,
175
+ ),
176
+ revision: column(mapping.authorityCredential.table, mapping.authorityCredential.revision),
177
+ });
178
+
179
+ const attemptColumns = (mapping: Mapping) => ({
180
+ moduleId: column(mapping.attempt.table, mapping.attempt.moduleId),
181
+ action: column(mapping.attempt.table, mapping.attempt.action),
182
+ attemptId: column(mapping.attempt.table, mapping.attempt.attemptId),
183
+ identifierNamespace: column(mapping.attempt.table, mapping.attempt.identifierNamespace),
184
+ identifierValue: column(mapping.attempt.table, mapping.attempt.identifierValue),
185
+ subjectId: column(mapping.attempt.table, mapping.attempt.subjectId),
186
+ credentialId: column(mapping.attempt.table, mapping.attempt.credentialId),
187
+ securityRevision: column(mapping.attempt.table, mapping.attempt.securityRevision),
188
+ credentialRevision: column(mapping.attempt.table, mapping.attempt.credentialRevision),
189
+ verifierVersion: column(mapping.attempt.table, mapping.attempt.verifierVersion),
190
+ identifierBindingRevision: column(
191
+ mapping.attempt.table,
192
+ mapping.attempt.identifierBindingRevision,
193
+ ),
194
+ admittedAt: column(mapping.attempt.table, mapping.attempt.admittedAt),
195
+ deadline: column(mapping.attempt.table, mapping.attempt.deadline),
196
+ retentionUntil: column(mapping.attempt.table, mapping.attempt.retentionUntil),
197
+ state: column(mapping.attempt.table, mapping.attempt.state),
198
+ });
199
+
200
+ const scopeColumns = (mapping: Mapping) => ({
201
+ moduleId: column(mapping.rateScope.table, mapping.rateScope.moduleId),
202
+ action: column(mapping.rateScope.table, mapping.rateScope.action),
203
+ scopeKind: column(mapping.rateScope.table, mapping.rateScope.scopeKind),
204
+ scopeKey: column(mapping.rateScope.table, mapping.rateScope.scopeKey),
205
+ });
206
+
207
+ const chargeColumns = (mapping: Mapping) => ({
208
+ moduleId: column(mapping.charge.table, mapping.charge.moduleId),
209
+ action: column(mapping.charge.table, mapping.charge.action),
210
+ scopeKind: column(mapping.charge.table, mapping.charge.scopeKind),
211
+ scopeKey: column(mapping.charge.table, mapping.charge.scopeKey),
212
+ attemptId: column(mapping.charge.table, mapping.charge.attemptId),
213
+ occurredAt: column(mapping.charge.table, mapping.charge.occurredAt),
214
+ retentionUntil: column(mapping.charge.table, mapping.charge.retentionUntil),
215
+ });
216
+
217
+ const commandColumns = (mapping: Mapping) => ({
218
+ moduleId: column(mapping.command.table, mapping.command.moduleId),
219
+ commandId: column(mapping.command.table, mapping.command.commandId),
220
+ action: column(mapping.command.table, mapping.command.action),
221
+ bindingDigest: column(mapping.command.table, mapping.command.bindingDigest),
222
+ decision: column(mapping.command.table, mapping.command.decision),
223
+ retentionUntil: column(mapping.command.table, mapping.command.retentionUntil),
224
+ });
225
+
226
+ const allocate = <A>(
227
+ mode: PasswordSqlConfiguration["mode"],
228
+ asyncValue: Effect.Effect<A, PersistenceMappingError> | undefined,
229
+ syncValue: (() => A) | undefined,
230
+ ): Effect.Effect<A, PasswordUnavailable | PersistenceMappingError> => {
231
+ if (mode === "synchronous")
232
+ return syncValue === undefined
233
+ ? Effect.fail(unavailable())
234
+ : Effect.try({
235
+ try: syncValue,
236
+ catch: (cause) => new PersistenceMappingError({ operation: "mapping", cause }),
237
+ });
238
+ if (asyncValue !== undefined) return asyncValue;
239
+
240
+ return syncValue === undefined
241
+ ? Effect.fail(unavailable())
242
+ : Effect.try({
243
+ try: syncValue,
244
+ catch: (cause) => new PersistenceMappingError({ operation: "mapping", cause }),
245
+ });
246
+ };
247
+
248
+ const allocateNextSecurityRevision = (
249
+ mapping: Mapping,
250
+ mode: PasswordSqlConfiguration["mode"],
251
+ current: SecurityRevision,
252
+ ) => {
253
+ const asyncValue = mapping.subject.nextSecurityRevision?.(current);
254
+
255
+ const syncValue =
256
+ mapping.subject.nextSecurityRevisionSync === undefined
257
+ ? undefined
258
+ : () => mapping.subject.nextSecurityRevisionSync!(current);
259
+
260
+ return allocate(
261
+ mode,
262
+ asyncValue ?? mapping.allocateRevision,
263
+ syncValue ?? mapping.allocateRevisionSync,
264
+ );
265
+ };
266
+
267
+ const sameIdentifier = (left: LoginIdentifier, right: LoginIdentifier) =>
268
+ left.namespace === right.namespace && left.value === right.value;
269
+
270
+ const sameCredentialSnapshot = (
271
+ left: PasswordCredentialSnapshot,
272
+ right: PasswordCredentialSnapshot,
273
+ ) =>
274
+ left.moduleId === right.moduleId &&
275
+ left.revision.subjectId === right.revision.subjectId &&
276
+ left.revision.securityRevision === right.revision.securityRevision &&
277
+ left.credentialId === right.credentialId &&
278
+ left.credentialRevision === right.credentialRevision &&
279
+ left.verifierVersion === right.verifierVersion &&
280
+ left.normalization === right.normalization &&
281
+ left.identifierVerifiedAtMillis === right.identifierVerifiedAtMillis &&
282
+ left.revision.credentials.length === right.revision.credentials.length &&
283
+ left.revision.credentials.every((entry) =>
284
+ right.revision.credentials.some(
285
+ (other) => other.credentialId === entry.credentialId && other.revision === entry.revision,
286
+ ),
287
+ ) &&
288
+ left.identifierBindingRevision === right.identifierBindingRevision &&
289
+ sameIdentifier(left.identifier, right.identifier) &&
290
+ Redacted.value(left.verifier) === Redacted.value(right.verifier);
291
+
292
+ const evidenceSatisfiedAt = (
293
+ evidence: AuthenticationEvidence,
294
+ requirement: AuthenticationRequirement,
295
+ now: number,
296
+ ): boolean => {
297
+ if (evidence.proofs.length > 64 || evidence.revision.credentials.length > 64) return false;
298
+ const revisions = new Map<string, string>();
299
+
300
+ for (const item of evidence.revision.credentials) {
301
+ if (revisions.has(item.credentialId)) return false;
302
+ revisions.set(item.credentialId, item.revision);
303
+ }
304
+ if (evidence.proofs.some((proof) => !revisions.has(proof.credentialId))) return false;
305
+ if (evidence.proofs.some((proof) => now < DateTime.toEpochMillis(proof.verifiedAt)))
306
+ return false;
307
+
308
+ const eligible = evidence.proofs.filter((proof) => {
309
+ const age = now - DateTime.toEpochMillis(proof.verifiedAt);
310
+
311
+ return age >= 0 && age < requirement.maximumAgeMillis;
312
+ });
313
+
314
+ const factors = new Set(eligible.flatMap((proof) => proof.factors));
315
+ const credentials = new Set(eligible.map((proof) => proof.credentialId));
316
+
317
+ return requirement.alternatives.some(
318
+ (alternative) =>
319
+ alternative.factors.every((factor) => factors.has(factor)) &&
320
+ credentials.size >= alternative.minimumCredentials &&
321
+ eligible.some(
322
+ (proof) =>
323
+ (!alternative.userVerified || proof.userVerified) &&
324
+ (!alternative.phishingResistant || proof.phishingResistant),
325
+ ),
326
+ );
327
+ };
328
+
329
+ const snapshotMutationInput = Effect.fn("DrizzlePassword.snapshotMutationInput")(function* (
330
+ input: PasswordMutationInput,
331
+ ) {
332
+ const expectedRevision = snapshotPasswordRevision(input.expectedRevision);
333
+
334
+ const evidence = yield* snapshotAuthenticationEvidence(input.authorization.evidence).pipe(
335
+ Effect.mapError(unavailable),
336
+ );
337
+
338
+ const requirement = yield* snapshotPasswordRequirement(input.authorization.requirement);
339
+
340
+ const credential =
341
+ input.credential === undefined
342
+ ? undefined
343
+ : yield* snapshotPasswordCredential(input.credential);
344
+
345
+ return Object.freeze({
346
+ ...input,
347
+ expectedRevision,
348
+ replacement: Object.freeze({
349
+ verifier: Redacted.make(Redacted.value(input.replacement.verifier)),
350
+ normalization: input.replacement.normalization,
351
+ }),
352
+ invalidation: Object.freeze({ ...input.invalidation }),
353
+ ...(credential === undefined ? {} : { credential }),
354
+ authorization: Object.freeze({
355
+ challenge: Object.freeze({
356
+ ...input.authorization.challenge,
357
+ revision: snapshotPasswordRevision(input.authorization.challenge.revision),
358
+ }),
359
+ evidence,
360
+ requirement,
361
+ }),
362
+ });
363
+ });
364
+
365
+ const lockSubject = Effect.fn("DrizzlePassword.lockSubject")(function* (
366
+ mapping: Mapping,
367
+ subjectId: string,
368
+ locking: boolean,
369
+ ) {
370
+ const database = yield* CurrentPasswordSql;
371
+
372
+ const nativeSubjectId = yield* mapping.subjectId.toNative(subjectId as any);
373
+ const c = subjectColumns(mapping);
374
+
375
+ const rows = yield* selectRows(
376
+ database.select().from(mapping.subject.table).where(eq(c.id, nativeSubjectId)).limit(1),
377
+ locking,
378
+ );
379
+
380
+ return { nativeSubjectId, row: rows[0] };
381
+ });
382
+
383
+ const lockIdentifier = Effect.fn("Drizzle.lockIdentifier")(function* (
384
+ mapping: Mapping,
385
+ identifier: LoginIdentifier,
386
+ locking: boolean,
387
+ ) {
388
+ const database = yield* CurrentPasswordSql;
389
+
390
+ const c = identifierColumns(mapping);
391
+
392
+ return yield* selectRows(
393
+ database
394
+ .select()
395
+ .from(mapping.identifier.table)
396
+ .where(and(eq(c.namespace, identifier.namespace), eq(c.value, identifier.value)))
397
+ .limit(1),
398
+ locking,
399
+ );
400
+ });
401
+
402
+ const readPasswordCredential = Effect.fn("Drizzle.readPasswordCredential")(function* (
403
+ mapping: Mapping,
404
+ moduleId: string,
405
+ nativeSubjectId: unknown,
406
+ locking: boolean,
407
+ ) {
408
+ const database = yield* CurrentPasswordSql;
409
+
410
+ const c = credentialColumns(mapping);
411
+
412
+ return yield* selectRows(
413
+ database
414
+ .select()
415
+ .from(mapping.credential.table)
416
+ .where(and(eq(c.moduleId, moduleId), eq(c.subjectId, nativeSubjectId)))
417
+ .limit(1),
418
+ locking,
419
+ );
420
+ });
421
+
422
+ const resolveCredential = Effect.fn("DrizzlePassword.resolveCredential")(function* (
423
+ mapping: Mapping,
424
+ moduleId: string,
425
+ identifier: LoginIdentifier,
426
+ requestedSubjectId: string | undefined,
427
+ locking: boolean,
428
+ ) {
429
+ const database = yield* CurrentPasswordSql;
430
+
431
+ // The unlocked lookup only discovers the prospective subject. The canonical
432
+ // protected order starts with the subject and then rechecks the identifier.
433
+ const discovered = (yield* lockIdentifier(mapping, identifier, false))[0];
434
+
435
+ if (discovered === undefined || !mapping.identifier.isCurrent(discovered)) return undefined;
436
+ const discoveredNative = discovered[mapping.identifier.subjectId];
437
+
438
+ const subjectRows = yield* selectRows(
439
+ database
440
+ .select()
441
+ .from(mapping.subject.table)
442
+ .where(eq(subjectColumns(mapping).id, discoveredNative))
443
+ .limit(1),
444
+ locking,
445
+ );
446
+
447
+ const subject = subjectRows[0];
448
+
449
+ if (subject === undefined || !mapping.subject.isActiveStatus(subject[mapping.subject.status]))
450
+ return undefined;
451
+ const identifiers = yield* lockIdentifier(mapping, identifier, locking);
452
+ const identifierRow = identifiers[0];
453
+
454
+ if (
455
+ identifierRow === undefined ||
456
+ !mapping.identifier.isCurrent(identifierRow) ||
457
+ !mapping.subjectId.equals(discoveredNative, identifierRow[mapping.identifier.subjectId])
458
+ )
459
+ return undefined;
460
+ const subjectId = yield* mapping.subjectId.toSubject(discoveredNative);
461
+
462
+ if (requestedSubjectId !== undefined && requestedSubjectId !== subjectId) return undefined;
463
+ const credentials = yield* readPasswordCredential(mapping, moduleId, discoveredNative, locking);
464
+ const credential = credentials[0];
465
+
466
+ if (credential === undefined)
467
+ return { nativeSubjectId: discoveredNative, subject, identifier: identifierRow };
468
+
469
+ const snapshot = yield* mapping.credential
470
+ .decode({ moduleId, subject, identifier: identifierRow, credential })
471
+ .pipe(Effect.flatMap(snapshotPasswordCredential));
472
+
473
+ return {
474
+ nativeSubjectId: discoveredNative,
475
+ subject,
476
+ identifier: identifierRow,
477
+ credential,
478
+ snapshot,
479
+ };
480
+ });
481
+
482
+ const scopeEntries = (
483
+ keys: PasswordScopeKeys,
484
+ policy: PasswordAttemptPolicy,
485
+ ): ReadonlyArray<ScopeEntry> => {
486
+ const values: ScopeEntry[] = [
487
+ { kind: "action", key: keys.action, ...policy.action },
488
+ { kind: "identifier", key: keys.identifier, ...policy.identifier },
489
+ ];
490
+
491
+ if (keys.subject !== undefined)
492
+ values.push({ kind: "subject", key: keys.subject, ...policy.subject });
493
+
494
+ return values;
495
+ };
496
+
497
+ const lockScope = Effect.fn("DrizzlePassword.lockScope")(function* (
498
+ mapping: Mapping,
499
+ configuration: PasswordSqlConfiguration,
500
+ moduleId: string,
501
+ action: PasswordAttemptAction,
502
+ entry: ScopeEntry,
503
+ ) {
504
+ const database = yield* CurrentPasswordSql;
505
+
506
+ const query = database.insert(mapping.rateScope.table).values(
507
+ mapping.rateScope.encodeInsert({
508
+ moduleId,
509
+ action,
510
+ scopeKind: entry.kind,
511
+ scopeKey: entry.key,
512
+ }),
513
+ );
514
+
515
+ yield* configuration.insertIfAbsent(query, mapping.rateScope.scopeKey, entry.key);
516
+ const c = scopeColumns(mapping);
517
+
518
+ yield* selectRows(
519
+ database
520
+ .select()
521
+ .from(mapping.rateScope.table)
522
+ .where(
523
+ and(
524
+ eq(c.moduleId, moduleId),
525
+ eq(c.action, action),
526
+ eq(c.scopeKind, entry.kind),
527
+ eq(c.scopeKey, entry.key),
528
+ ),
529
+ )
530
+ .limit(1),
531
+ configuration.locking,
532
+ );
533
+ });
534
+
535
+ const scopeAdmits = Effect.fn("DrizzlePassword.scopeAdmits")(function* (
536
+ mapping: Mapping,
537
+ moduleId: string,
538
+ action: PasswordAttemptAction,
539
+ entry: ScopeEntry,
540
+ policy: PasswordAttemptPolicy,
541
+ identifier: LoginIdentifier,
542
+ nativeSubjectId: unknown | undefined,
543
+ now: number,
544
+ ) {
545
+ const database = yield* CurrentPasswordSql;
546
+
547
+ const c = chargeColumns(mapping);
548
+
549
+ const charges = yield* selectRows(
550
+ database
551
+ .select({ attemptId: c.attemptId })
552
+ .from(mapping.charge.table)
553
+ .where(
554
+ and(
555
+ eq(c.moduleId, moduleId),
556
+ eq(c.action, action),
557
+ eq(c.scopeKind, entry.kind),
558
+ eq(c.scopeKey, entry.key),
559
+ gte(c.occurredAt, mapping.encodeInstant(now - entry.windowMillis)),
560
+ ),
561
+ )
562
+ .limit(entry.limit),
563
+ true,
564
+ );
565
+
566
+ if (charges.length >= entry.limit) return false;
567
+ const a = attemptColumns(mapping);
568
+
569
+ const pendingWhere =
570
+ entry.kind === "action"
571
+ ? and(eq(a.moduleId, moduleId), eq(a.action, action), eq(a.state, "pending"))
572
+ : entry.kind === "identifier"
573
+ ? and(
574
+ eq(a.moduleId, moduleId),
575
+ eq(a.action, action),
576
+ eq(a.identifierNamespace, identifier.namespace),
577
+ eq(a.identifierValue, identifier.value),
578
+ eq(a.state, "pending"),
579
+ )
580
+ : and(
581
+ eq(a.moduleId, moduleId),
582
+ eq(a.action, action),
583
+ eq(a.subjectId, nativeSubjectId),
584
+ eq(a.state, "pending"),
585
+ );
586
+
587
+ const pending = yield* selectRows(
588
+ database
589
+ .select({ attemptId: a.attemptId })
590
+ .from(mapping.attempt.table)
591
+ .where(pendingWhere)
592
+ .limit(policy.maximumPending),
593
+ true,
594
+ );
595
+
596
+ return pending.length < policy.maximumPending;
597
+ });
598
+
599
+ const insertCharge = Effect.fn("Drizzle.insertCharge")(function* (
600
+ mapping: Mapping,
601
+ moduleId: string,
602
+ action: PasswordAttemptAction,
603
+ entry: ScopeEntry,
604
+ attemptId: any,
605
+ now: number,
606
+ ) {
607
+ const database = yield* CurrentPasswordSql;
608
+
609
+ return yield* database.insert(mapping.charge.table).values(
610
+ mapping.charge.encodeInsert({
611
+ moduleId,
612
+ action,
613
+ scopeKind: entry.kind,
614
+ scopeKey: entry.key,
615
+ attemptId,
616
+ occurredAtMillis: now,
617
+ retentionUntilMillis: now + entry.windowMillis,
618
+ }),
619
+ );
620
+ });
621
+
622
+ const owned = <A, E, R>(
623
+ database: Database,
624
+ mapping: Mapping,
625
+ configuration: PasswordSqlConfiguration,
626
+ body: Effect.Effect<A, E, R>,
627
+ ) => {
628
+ if (!validConstraints(mapping)) return Effect.fail(unavailable());
629
+
630
+ const run = coordinateCommit(
631
+ () =>
632
+ database.transaction((transaction) =>
633
+ body.pipe(
634
+ Effect.provideService(CurrentPasswordSql, transaction),
635
+ Effect.provideService(CurrentProofSql, transaction as unknown as ProofSqlDatabase),
636
+ ),
637
+ ),
638
+ { mode: configuration.mode },
639
+ ).pipe(Effect.map((result) => result.value));
640
+
641
+ return configuration.coordinated !== true
642
+ ? Effect.gen(function* () {
643
+ if (yield* hasCommitScope) return yield* unavailable();
644
+ yield* configuration.standaloneGuard;
645
+
646
+ return yield* run;
647
+ })
648
+ : run;
649
+ };
650
+
651
+ const safeRead = <A, E, R>(
652
+ database: Database,
653
+ configuration: PasswordSqlConfiguration,
654
+ body: Effect.Effect<A, E, R>,
655
+ ) =>
656
+ Effect.gen(function* () {
657
+ if (configuration.coordinated !== true) {
658
+ if (yield* hasCommitScope) return yield* unavailable();
659
+ yield* configuration.standaloneGuard;
660
+ }
661
+
662
+ return yield* database.transaction((transaction) =>
663
+ body.pipe(
664
+ Effect.provideService(CurrentPasswordSql, transaction),
665
+ Effect.provideService(CurrentProofSql, transaction as unknown as ProofSqlDatabase),
666
+ ),
667
+ );
668
+ });
669
+
670
+ const proofCompletionMatchesPassword = (
671
+ input: PasswordMutationInput & { readonly completion: ProofCompletionPlan },
672
+ ) => {
673
+ const completion = input.completion.input;
674
+ const binding = completion.binding;
675
+
676
+ if (
677
+ completion.moduleId !== `${input.moduleId}/reset` ||
678
+ completion.purpose !== "password-reset" ||
679
+ binding._tag !== "Subject" ||
680
+ input.credential === undefined ||
681
+ binding.revision.subjectId !== input.expectedRevision.subjectId ||
682
+ binding.revision.securityRevision !== input.expectedRevision.securityRevision ||
683
+ !sameIdentifier(binding.identifier, input.credential.identifier)
684
+ )
685
+ return false;
686
+
687
+ const expected = [...input.expectedRevision.credentials].sort((left, right) =>
688
+ left.credentialId.localeCompare(right.credentialId),
689
+ );
690
+
691
+ const actual = [...binding.revision.credentials].sort((left, right) =>
692
+ left.credentialId.localeCompare(right.credentialId),
693
+ );
694
+
695
+ return (
696
+ expected.length === actual.length &&
697
+ expected.every(
698
+ (item, index) =>
699
+ item.credentialId === actual[index]!.credentialId &&
700
+ item.revision === actual[index]!.revision,
701
+ )
702
+ );
703
+ };
704
+
705
+ const probeProofCompletion = Effect.fn("Drizzle.probeProofCompletion")(function* (
706
+ configuration: PasswordSqlConfiguration,
707
+ input: ProofCompletionInput,
708
+ ) {
709
+ const database = yield* CurrentPasswordSql;
710
+
711
+ if (configuration.proof === undefined) return yield* Effect.fail(unavailable());
712
+ const validProbe = { _tag: "PasswordProofCompletionValid" } as const;
713
+
714
+ const plan: ProofCompletionPlan = {
715
+ input,
716
+ prepare: (decision, journal, project) => journal.prepare(project(decision)),
717
+ };
718
+
719
+ const probe = coordinateCommit(
720
+ () =>
721
+ database.transaction((transaction) =>
722
+ completeProofPlanIn(
723
+ configuration.proof!.mapping,
724
+ configuration.proof!.configuration,
725
+ plan,
726
+ Effect.fail(validProbe),
727
+ () => false,
728
+ ).pipe(
729
+ Effect.provideService(CurrentProofSql, transaction as unknown as ProofSqlDatabase),
730
+ ),
731
+ ),
732
+ { mode: configuration.mode },
733
+ );
734
+
735
+ if (configuration.coordinated === true || (yield* hasCommitScope)) return yield* unavailable();
736
+ yield* configuration.standaloneGuard;
737
+
738
+ return yield* probe.pipe(
739
+ Effect.as(false),
740
+ Effect.catchCause((cause) => {
741
+ const reason = cause.reasons[0];
742
+
743
+ return cause.reasons.length === 1 &&
744
+ reason !== undefined &&
745
+ Cause.isFailReason(reason) &&
746
+ reason.error === validProbe
747
+ ? Effect.succeed(true)
748
+ : Effect.failCause(cause);
749
+ }),
750
+ );
751
+ });
752
+
753
+ const validateMutationAuthority = Effect.fn("DrizzlePassword.validateMutationAuthority")(
754
+ function* (
755
+ mapping: Mapping,
756
+ configuration: PasswordSqlConfiguration,
757
+ input: PasswordMutationInput,
758
+ action: PasswordAction,
759
+ ) {
760
+ const database = yield* CurrentPasswordSql;
761
+
762
+ const locked = yield* lockSubject(
763
+ mapping,
764
+ input.expectedRevision.subjectId,
765
+ configuration.locking,
766
+ );
767
+
768
+ const row = locked.row;
769
+
770
+ if (
771
+ row === undefined ||
772
+ !mapping.subject.isActiveStatus(row[mapping.subject.status]) ||
773
+ row[mapping.subject.securityRevision] !== input.expectedRevision.securityRevision
774
+ )
775
+ return undefined;
776
+ if (
777
+ input.authorization.challenge.moduleId !== input.moduleId ||
778
+ input.authorization.challenge.action !== action ||
779
+ input.authorization.challenge.commandId !== input.commandId ||
780
+ input.authorization.challenge.revision.subjectId !== input.expectedRevision.subjectId ||
781
+ input.authorization.challenge.revision.securityRevision !==
782
+ input.expectedRevision.securityRevision ||
783
+ (input.authorization.evidence.flowId as string) !== (input.commandId as string) ||
784
+ input.authorization.evidence.bindingDigest !==
785
+ input.authorization.challenge.bindingDigest ||
786
+ input.authorization.evidence.revision.subjectId !== input.expectedRevision.subjectId ||
787
+ input.authorization.evidence.revision.securityRevision !==
788
+ input.expectedRevision.securityRevision ||
789
+ (action === "add-password" &&
790
+ (input.credential !== undefined ||
791
+ input.authorization.challenge.targetCredentialId !== undefined))
792
+ )
793
+ return undefined;
794
+ if (
795
+ input.expectedRevision.credentials.some(
796
+ (expected) =>
797
+ !input.authorization.evidence.revision.credentials.some(
798
+ (actual) =>
799
+ actual.credentialId === expected.credentialId &&
800
+ actual.revision === expected.revision,
801
+ ),
802
+ )
803
+ )
804
+ return undefined;
805
+ if (input.credential !== undefined) {
806
+ const identifier = (yield* lockIdentifier(
807
+ mapping,
808
+ input.credential.identifier,
809
+ configuration.locking,
810
+ ))[0];
811
+
812
+ if (
813
+ identifier === undefined ||
814
+ !mapping.identifier.isCurrent(identifier) ||
815
+ !mapping.subjectId.equals(
816
+ identifier[mapping.identifier.subjectId],
817
+ locked.nativeSubjectId,
818
+ ) ||
819
+ identifier[mapping.identifier.bindingRevision] !==
820
+ input.credential.identifierBindingRevision
821
+ )
822
+ return undefined;
823
+ }
824
+
825
+ const allExpected = [
826
+ ...input.expectedRevision.credentials,
827
+ ...input.authorization.evidence.revision.credentials,
828
+ ];
829
+
830
+ const expected = new Map(allExpected.map((item) => [item.credentialId, item]));
831
+
832
+ const ac = authorityCredentialColumns(mapping);
833
+
834
+ const credentialRows =
835
+ expected.size === 0
836
+ ? []
837
+ : yield* selectRows(
838
+ database
839
+ .select()
840
+ .from(mapping.authorityCredential.table)
841
+ .where(
842
+ and(
843
+ eq(ac.subjectId, locked.nativeSubjectId),
844
+ inArray(ac.credentialId, [...expected.keys()]),
845
+ ),
846
+ )
847
+ .orderBy(ac.credentialId),
848
+ configuration.locking,
849
+ );
850
+
851
+ if (credentialRows.length !== expected.size) return undefined;
852
+ // Database collation need not match JavaScript locale ordering.
853
+ for (const actual of credentialRows) {
854
+ const wanted = expected.get(actual[mapping.authorityCredential.credentialId]);
855
+
856
+ if (
857
+ wanted === undefined ||
858
+ actual[mapping.authorityCredential.revision] !== wanted.revision ||
859
+ (mapping.authorityCredential.status !== undefined &&
860
+ mapping.authorityCredential.isActiveStatus?.(
861
+ actual[mapping.authorityCredential.status],
862
+ ) !== true)
863
+ )
864
+ return undefined;
865
+ expected.delete(wanted.credentialId);
866
+ }
867
+ if (expected.size !== 0) return undefined;
868
+ if (input.credential !== undefined) {
869
+ const identifier = (yield* lockIdentifier(mapping, input.credential.identifier, false))[0];
870
+
871
+ const credential = (yield* readPasswordCredential(
872
+ mapping,
873
+ input.moduleId,
874
+ locked.nativeSubjectId,
875
+ configuration.locking,
876
+ ))[0];
877
+
878
+ if (identifier === undefined || credential === undefined) return undefined;
879
+
880
+ const snapshot = yield* mapping.credential.decode({
881
+ moduleId: input.moduleId,
882
+ subject: row,
883
+ identifier,
884
+ credential,
885
+ });
886
+
887
+ if (!sameCredentialSnapshot(snapshot, input.credential)) return undefined;
888
+ }
889
+ const currentRequirement = yield* mapping.subject.decodeActionRequirement(row, action);
890
+ const now = yield* nowMillis;
891
+
892
+ if (
893
+ !evidenceSatisfiedAt(input.authorization.evidence, input.authorization.requirement, now) ||
894
+ !evidenceSatisfiedAt(input.authorization.evidence, currentRequirement, now)
895
+ )
896
+ return undefined;
897
+ if (
898
+ input.invalidation.existingSessions === "immediate" &&
899
+ mapping.sessionInvalidation !== "same-authority-immediate"
900
+ )
901
+ return undefined;
902
+
903
+ return { ...locked, now };
904
+ },
905
+ );
906
+
907
+ const commandExists = Effect.fn("Drizzle.commandExists")(function* (
908
+ mapping: Mapping,
909
+ moduleId: string,
910
+ commandId: string,
911
+ locking: boolean,
912
+ ) {
913
+ const database = yield* CurrentPasswordSql;
914
+
915
+ const c = commandColumns(mapping);
916
+
917
+ return yield* selectRows(
918
+ database
919
+ .select()
920
+ .from(mapping.command.table)
921
+ .where(and(eq(c.moduleId, moduleId), eq(c.commandId, commandId)))
922
+ .limit(1),
923
+ locking,
924
+ );
925
+ });
926
+
927
+ const insertCommand = Effect.fn("Drizzle.insertCommand")(function* (
928
+ mapping: Mapping,
929
+ input: PasswordMutationInput,
930
+ action: PasswordAction,
931
+ now: number,
932
+ ) {
933
+ const database = yield* CurrentPasswordSql;
934
+
935
+ return yield* database.insert(mapping.command.table).values(
936
+ mapping.command.encodeInsert({
937
+ moduleId: input.moduleId,
938
+ commandId: input.commandId,
939
+ action,
940
+ bindingDigest: input.authorization.challenge.bindingDigest,
941
+ decision: "changed",
942
+ retentionUntilMillis: now + mapping.commandRetentionMillis,
943
+ }),
944
+ );
945
+ });
946
+
947
+ const mutationPostconditions = Effect.fn("DrizzlePassword.mutationPostconditions")(function* (
948
+ mapping: Mapping,
949
+ input: PasswordMutationInput,
950
+ nativeSubjectId: unknown,
951
+ subject: any,
952
+ authorityCredential: any,
953
+ now: number,
954
+ ) {
955
+ if (
956
+ subject === undefined ||
957
+ !mapping.subject.isActiveStatus(subject[mapping.subject.status]) ||
958
+ authorityCredential === undefined ||
959
+ (mapping.authorityCredential.status !== undefined &&
960
+ mapping.authorityCredential.isActiveStatus?.(
961
+ authorityCredential[mapping.authorityCredential.status],
962
+ ) !== true)
963
+ )
964
+ return false;
965
+ if (input.credential !== undefined) {
966
+ const identifier = (yield* lockIdentifier(mapping, input.credential.identifier, false))[0];
967
+
968
+ if (
969
+ identifier === undefined ||
970
+ !mapping.identifier.isCurrent(identifier) ||
971
+ !mapping.subjectId.equals(identifier[mapping.identifier.subjectId], nativeSubjectId) ||
972
+ identifier[mapping.identifier.bindingRevision] !==
973
+ input.credential.identifierBindingRevision
974
+ )
975
+ return false;
976
+
977
+ const credential = (yield* readPasswordCredential(
978
+ mapping,
979
+ input.moduleId,
980
+ nativeSubjectId,
981
+ false,
982
+ ))[0];
983
+
984
+ if (credential === undefined) return false;
985
+
986
+ const decoded = yield* mapping.credential.decode({
987
+ moduleId: input.moduleId,
988
+ subject,
989
+ identifier,
990
+ credential,
991
+ });
992
+
993
+ if (decoded.identifierVerifiedAtMillis !== input.credential.identifierVerifiedAtMillis)
994
+ return false;
995
+ }
996
+ const rows = yield* commandExists(mapping, input.moduleId, input.commandId, false);
997
+ const row = rows[0];
998
+
999
+ return (
1000
+ rows.length === 1 &&
1001
+ row[mapping.command.action] === input.authorization.challenge.action &&
1002
+ row[mapping.command.bindingDigest] === input.authorization.challenge.bindingDigest &&
1003
+ row[mapping.command.decision] === "changed" &&
1004
+ (yield* mapping.decodeInstant(row[mapping.command.retentionUntil])) ===
1005
+ now + mapping.commandRetentionMillis
1006
+ );
1007
+ });
1008
+
1009
+ const addPasswordIn = Effect.fn("DrizzlePassword.addPasswordIn")(function* (
1010
+ mapping: Mapping,
1011
+ configuration: PasswordSqlConfiguration,
1012
+ input: PasswordMutationInput,
1013
+ revisions: {
1014
+ readonly credentialId: string;
1015
+ readonly credentialRevision: SecurityRevision;
1016
+ readonly verifierVersion: SecurityRevision;
1017
+ readonly nextSecurityRevision: SecurityRevision;
1018
+ },
1019
+ commandNow?: number,
1020
+ ) {
1021
+ const database = yield* CurrentPasswordSql;
1022
+
1023
+ const { credentialId, credentialRevision, verifierVersion, nextSecurityRevision } = revisions;
1024
+
1025
+ const authority = yield* validateMutationAuthority(
1026
+ mapping,
1027
+ configuration,
1028
+ input,
1029
+ "add-password",
1030
+ );
1031
+
1032
+ if (authority === undefined) return false;
1033
+ if ((yield* commandExists(mapping, input.moduleId, input.commandId, true)).length) return false;
1034
+ if (
1035
+ (yield* readPasswordCredential(mapping, input.moduleId, authority.nativeSubjectId, true))
1036
+ .length
1037
+ )
1038
+ return false;
1039
+
1040
+ yield* database.insert(mapping.credential.table).values(
1041
+ mapping.credential.encodeInsert({
1042
+ moduleId: input.moduleId,
1043
+ subjectId: authority.nativeSubjectId,
1044
+ credentialId,
1045
+ credentialRevision,
1046
+ verifierVersion,
1047
+ replacement: input.replacement,
1048
+ }),
1049
+ );
1050
+ yield* database.insert(mapping.authorityCredential.table).values(
1051
+ mapping.authorityCredential.encodeInsert({
1052
+ subjectId: authority.nativeSubjectId,
1053
+ credentialId,
1054
+ revision: credentialRevision,
1055
+ }),
1056
+ );
1057
+ yield* database
1058
+ .update(mapping.subject.table)
1059
+ .set(updateValues([[mapping.subject.securityRevision, nextSecurityRevision]]))
1060
+ .where(
1061
+ and(
1062
+ eq(subjectColumns(mapping).id, authority.nativeSubjectId),
1063
+ eq(subjectColumns(mapping).securityRevision, input.expectedRevision.securityRevision),
1064
+ ),
1065
+ );
1066
+ yield* insertCommand(mapping, input, "add-password", commandNow ?? authority.now);
1067
+
1068
+ const inserted = (yield* readPasswordCredential(
1069
+ mapping,
1070
+ input.moduleId,
1071
+ authority.nativeSubjectId,
1072
+ false,
1073
+ ))[0];
1074
+
1075
+ const authorityInserted = (yield* database
1076
+ .select()
1077
+ .from(mapping.authorityCredential.table)
1078
+ .where(
1079
+ and(
1080
+ eq(authorityCredentialColumns(mapping).subjectId, authority.nativeSubjectId),
1081
+ eq(authorityCredentialColumns(mapping).credentialId, credentialId),
1082
+ eq(authorityCredentialColumns(mapping).revision, credentialRevision),
1083
+ ),
1084
+ )
1085
+ .limit(1))[0];
1086
+
1087
+ const subjectUpdated = (yield* database
1088
+ .select()
1089
+ .from(mapping.subject.table)
1090
+ .where(
1091
+ and(
1092
+ eq(subjectColumns(mapping).id, authority.nativeSubjectId),
1093
+ eq(subjectColumns(mapping).securityRevision, nextSecurityRevision),
1094
+ ),
1095
+ )
1096
+ .limit(1))[0];
1097
+
1098
+ if (
1099
+ inserted === undefined ||
1100
+ inserted[mapping.credential.credentialId] !== credentialId ||
1101
+ inserted[mapping.credential.credentialRevision] !== credentialRevision ||
1102
+ inserted[mapping.credential.verifierVersion] !== verifierVersion ||
1103
+ inserted[mapping.credential.verifier] !== Redacted.value(input.replacement.verifier) ||
1104
+ inserted[mapping.credential.normalization] !== input.replacement.normalization ||
1105
+ authorityInserted === undefined ||
1106
+ subjectUpdated === undefined ||
1107
+ !(yield* mutationPostconditions(
1108
+ mapping,
1109
+ input,
1110
+ authority.nativeSubjectId,
1111
+ subjectUpdated,
1112
+ authorityInserted,
1113
+ commandNow ?? authority.now,
1114
+ ))
1115
+ )
1116
+ return yield* unavailable();
1117
+
1118
+ return true;
1119
+ });
1120
+
1121
+ const makeSqlPasswordPersistence = Effect.fn("makeSqlPasswordPersistence")(function* (
1122
+ database: Database,
1123
+ mapping: Mapping,
1124
+ configuration: PasswordSqlConfiguration,
1125
+ ): Effect.fn.Return<PasswordPersistence["Service"], never, LifecycleHooks> {
1126
+ const hooks = yield* LifecycleHooks;
1127
+
1128
+ return PasswordPersistence.of({
1129
+ admitAttempt: (input, prepare) =>
1130
+ Effect.gen(function* () {
1131
+ const attemptId = yield* allocate(
1132
+ configuration.mode,
1133
+ mapping.allocateAttemptId,
1134
+ mapping.allocateAttemptIdSync,
1135
+ );
1136
+
1137
+ return yield* owned(
1138
+ database,
1139
+ mapping,
1140
+ configuration,
1141
+ Effect.gen(function* () {
1142
+ const transaction = yield* CurrentPasswordSql;
1143
+ const journal = yield* CurrentCommitJournal;
1144
+
1145
+ const resolved = yield* resolveCredential(
1146
+ mapping,
1147
+ input.moduleId,
1148
+ input.identifier,
1149
+ input.subjectId,
1150
+ configuration.locking,
1151
+ );
1152
+
1153
+ const keys = mapping.scopeKeys({
1154
+ moduleId: input.moduleId,
1155
+ action: input.action,
1156
+ identifier: input.identifier,
1157
+ ...(resolved === undefined
1158
+ ? {}
1159
+ : { subjectId: resolved.snapshot?.revision.subjectId }),
1160
+ });
1161
+
1162
+ const scopes = scopeEntries(keys, input.policy);
1163
+ const admitted: ScopeEntry[] = [];
1164
+
1165
+ yield* lockScope(mapping, configuration, input.moduleId, input.action, scopes[0]!);
1166
+ let currentNow = yield* nowMillis;
1167
+
1168
+ if (
1169
+ yield* scopeAdmits(
1170
+ mapping,
1171
+ input.moduleId,
1172
+ input.action,
1173
+ scopes[0]!,
1174
+ input.policy,
1175
+ input.identifier,
1176
+ resolved?.nativeSubjectId,
1177
+ currentNow,
1178
+ )
1179
+ )
1180
+ admitted.push(scopes[0]!);
1181
+ if (admitted.length === 1) {
1182
+ for (const entry of scopes.slice(1)) {
1183
+ yield* lockScope(mapping, configuration, input.moduleId, input.action, entry);
1184
+ currentNow = yield* nowMillis;
1185
+ if (
1186
+ yield* scopeAdmits(
1187
+ mapping,
1188
+ input.moduleId,
1189
+ input.action,
1190
+ entry,
1191
+ input.policy,
1192
+ input.identifier,
1193
+ resolved?.nativeSubjectId,
1194
+ currentNow,
1195
+ )
1196
+ )
1197
+ admitted.push(entry);
1198
+ }
1199
+ }
1200
+ const now = yield* nowMillis;
1201
+
1202
+ for (const entry of admitted)
1203
+ yield* insertCharge(mapping, input.moduleId, input.action, entry, attemptId, now);
1204
+ if (admitted.length !== scopes.length)
1205
+ return prepare({ _tag: "Denied" as const }, journal);
1206
+ const snapshot = resolved?.snapshot;
1207
+ const deadline = now + input.policy.attemptLifetimeMillis;
1208
+
1209
+ const retentionUntil = Math.max(
1210
+ deadline,
1211
+ ...scopes.map((entry) => now + entry.windowMillis),
1212
+ );
1213
+
1214
+ yield* transaction.insert(mapping.attempt.table).values(
1215
+ mapping.attempt.encodeInsert(
1216
+ {
1217
+ moduleId: input.moduleId,
1218
+ action: input.action,
1219
+ attemptId,
1220
+ identifier: input.identifier,
1221
+ ...(snapshot === undefined
1222
+ ? {}
1223
+ : {
1224
+ subjectId: snapshot.revision.subjectId,
1225
+ credentialId: snapshot.credentialId,
1226
+ securityRevision: snapshot.revision.securityRevision,
1227
+ credentialRevision: snapshot.credentialRevision,
1228
+ verifierVersion: snapshot.verifierVersion,
1229
+ identifierBindingRevision: snapshot.identifierBindingRevision,
1230
+ }),
1231
+ admittedAtMillis: now,
1232
+ deadlineMillis: deadline,
1233
+ retentionUntilMillis: retentionUntil,
1234
+ },
1235
+ {
1236
+ ...(resolved?.nativeSubjectId === undefined
1237
+ ? {}
1238
+ : { nativeSubjectId: resolved.nativeSubjectId }),
1239
+ state: "pending",
1240
+ },
1241
+ ),
1242
+ );
1243
+
1244
+ return prepare(
1245
+ {
1246
+ _tag: "Admitted" as const,
1247
+ attemptId,
1248
+ ...(snapshot === undefined ? {} : { credential: snapshot }),
1249
+ },
1250
+ journal,
1251
+ );
1252
+ }),
1253
+ );
1254
+ }).pipe(
1255
+ Effect.provideService(CurrentPasswordSql, database),
1256
+ Effect.provideService(LifecycleHooks, hooks),
1257
+ translateFailure,
1258
+ ),
1259
+ settleAttempt: (uncaptured, prepare) =>
1260
+ Effect.gen(function* () {
1261
+ const captured =
1262
+ uncaptured.captured === undefined
1263
+ ? undefined
1264
+ : yield* snapshotPasswordCredential(uncaptured.captured);
1265
+
1266
+ const input = { ...uncaptured, ...(captured === undefined ? {} : { captured }) };
1267
+
1268
+ return yield* owned(
1269
+ database,
1270
+ mapping,
1271
+ configuration,
1272
+ Effect.gen(function* () {
1273
+ const transaction = yield* CurrentPasswordSql;
1274
+ const journal = yield* CurrentCommitJournal;
1275
+
1276
+ const c = attemptColumns(mapping);
1277
+
1278
+ const discovered = yield* selectRows(
1279
+ transaction
1280
+ .select()
1281
+ .from(mapping.attempt.table)
1282
+ .where(and(eq(c.moduleId, input.moduleId), eq(c.attemptId, input.attemptId)))
1283
+ .limit(1),
1284
+ false,
1285
+ );
1286
+
1287
+ let row = discovered[0];
1288
+
1289
+ if (row === undefined || row[mapping.attempt.state] !== "pending")
1290
+ return prepare("rejected", journal);
1291
+ let decision: PasswordAttemptDecision = "rejected";
1292
+ let current: any;
1293
+ let attemptMatches = false;
1294
+
1295
+ if (input.outcome === "verified" && input.captured !== undefined) {
1296
+ current = yield* resolveCredential(
1297
+ mapping,
1298
+ input.moduleId,
1299
+ input.captured.identifier,
1300
+ input.captured.revision.subjectId,
1301
+ configuration.locking,
1302
+ );
1303
+ attemptMatches =
1304
+ input.captured.moduleId === input.moduleId &&
1305
+ row[mapping.attempt.identifierNamespace] ===
1306
+ input.captured.identifier.namespace &&
1307
+ row[mapping.attempt.identifierValue] === input.captured.identifier.value &&
1308
+ mapping.subjectId.equals(
1309
+ row[mapping.attempt.subjectId],
1310
+ current?.nativeSubjectId,
1311
+ ) &&
1312
+ row[mapping.attempt.credentialId] === input.captured.credentialId &&
1313
+ row[mapping.attempt.securityRevision] ===
1314
+ input.captured.revision.securityRevision &&
1315
+ row[mapping.attempt.credentialRevision] === input.captured.credentialRevision &&
1316
+ row[mapping.attempt.verifierVersion] === input.captured.verifierVersion &&
1317
+ row[mapping.attempt.identifierBindingRevision] ===
1318
+ input.captured.identifierBindingRevision;
1319
+ }
1320
+ row = (yield* selectRows(
1321
+ transaction
1322
+ .select()
1323
+ .from(mapping.attempt.table)
1324
+ .where(and(eq(c.moduleId, input.moduleId), eq(c.attemptId, input.attemptId)))
1325
+ .limit(1),
1326
+ configuration.locking,
1327
+ ))[0];
1328
+ if (row === undefined || row[mapping.attempt.state] !== "pending")
1329
+ return prepare("rejected", journal);
1330
+ const now = yield* nowMillis;
1331
+ const deadline = yield* mapping.decodeInstant(row[mapping.attempt.deadline]);
1332
+
1333
+ if (
1334
+ attemptMatches &&
1335
+ current?.snapshot !== undefined &&
1336
+ input.captured !== undefined &&
1337
+ deadline > now &&
1338
+ sameCredentialSnapshot(current.snapshot, input.captured)
1339
+ )
1340
+ decision = "verified";
1341
+ const prepared = prepare(decision, journal);
1342
+
1343
+ yield* transaction
1344
+ .update(mapping.attempt.table)
1345
+ .set(updateValues([[mapping.attempt.state, decision]]))
1346
+ .where(
1347
+ and(
1348
+ eq(c.moduleId, input.moduleId),
1349
+ eq(c.attemptId, input.attemptId),
1350
+ eq(c.state, "pending"),
1351
+ ),
1352
+ );
1353
+ if (
1354
+ decision === "verified" &&
1355
+ input.rehash !== undefined &&
1356
+ current?.snapshot !== undefined
1357
+ ) {
1358
+ const cc = credentialColumns(mapping);
1359
+
1360
+ const nextVersion = yield* allocate(
1361
+ configuration.mode,
1362
+ mapping.allocateRevision,
1363
+ mapping.allocateRevisionSync,
1364
+ );
1365
+
1366
+ yield* transaction
1367
+ .update(mapping.credential.table)
1368
+ .set(mapping.credential.encodeVerifier(input.rehash.nextVerifier, nextVersion))
1369
+ .where(
1370
+ and(
1371
+ eq(cc.moduleId, input.moduleId),
1372
+ eq(cc.subjectId, current.nativeSubjectId),
1373
+ eq(cc.credentialId, current.snapshot.credentialId),
1374
+ eq(cc.credentialRevision, current.snapshot.credentialRevision),
1375
+ eq(cc.verifierVersion, input.rehash.expectedVersion),
1376
+ eq(cc.verifier, Redacted.value(input.rehash.expectedVerifier)),
1377
+ ),
1378
+ );
1379
+ }
1380
+
1381
+ return prepared;
1382
+ }),
1383
+ );
1384
+ }).pipe(
1385
+ Effect.provideService(CurrentPasswordSql, database),
1386
+ Effect.provideService(LifecycleHooks, hooks),
1387
+ translateFailure,
1388
+ ),
1389
+ readForSubject: (input) =>
1390
+ safeRead(
1391
+ database,
1392
+ configuration,
1393
+ Effect.gen(function* () {
1394
+ const transaction = yield* CurrentPasswordSql;
1395
+
1396
+ const locked = yield* lockSubject(mapping, input.subjectId, false);
1397
+
1398
+ if (
1399
+ locked.row === undefined ||
1400
+ !mapping.subject.isActiveStatus(locked.row[mapping.subject.status])
1401
+ )
1402
+ return Option.none();
1403
+
1404
+ const credential = (yield* readPasswordCredential(
1405
+ mapping,
1406
+ input.moduleId,
1407
+ locked.nativeSubjectId,
1408
+ false,
1409
+ ))[0];
1410
+
1411
+ if (credential === undefined) return Option.none();
1412
+ const ic = identifierColumns(mapping);
1413
+
1414
+ const identifiers = yield* transaction
1415
+ .select()
1416
+ .from(mapping.identifier.table)
1417
+ .where(eq(ic.subjectId, locked.nativeSubjectId));
1418
+
1419
+ const identifier = identifiers.find((row: any) => mapping.identifier.isCurrent(row));
1420
+
1421
+ if (identifier === undefined) return Option.none();
1422
+
1423
+ const snapshot = yield* mapping.credential
1424
+ .decode({
1425
+ moduleId: input.moduleId,
1426
+ subject: locked.row,
1427
+ identifier,
1428
+ credential,
1429
+ })
1430
+ .pipe(Effect.flatMap(snapshotPasswordCredential));
1431
+
1432
+ return Option.some(snapshot);
1433
+ }),
1434
+ ).pipe(
1435
+ Effect.provideService(CurrentPasswordSql, database),
1436
+ Effect.provideService(LifecycleHooks, hooks),
1437
+ translateFailure,
1438
+ ),
1439
+ recoveryTarget: (input) =>
1440
+ safeRead(
1441
+ database,
1442
+ configuration,
1443
+ Effect.gen(function* () {
1444
+ const resolved = yield* resolveCredential(
1445
+ mapping,
1446
+ input.moduleId,
1447
+ input.identifier,
1448
+ undefined,
1449
+ false,
1450
+ );
1451
+
1452
+ return resolved?.snapshot === undefined ||
1453
+ resolved.snapshot.identifierVerifiedAtMillis === undefined
1454
+ ? Option.none()
1455
+ : Option.some(resolved.snapshot);
1456
+ }),
1457
+ ).pipe(
1458
+ Effect.provideService(CurrentPasswordSql, database),
1459
+ Effect.provideService(LifecycleHooks, hooks),
1460
+ translateFailure,
1461
+ ),
1462
+ addIfAbsent: (uncaptured, prepare) =>
1463
+ Effect.gen(function* () {
1464
+ const input = yield* snapshotMutationInput(uncaptured);
1465
+
1466
+ const credentialId = yield* allocate(
1467
+ configuration.mode,
1468
+ mapping.allocateCredentialId,
1469
+ mapping.allocateCredentialIdSync,
1470
+ );
1471
+
1472
+ const credentialRevision = yield* allocate(
1473
+ configuration.mode,
1474
+ mapping.allocateRevision,
1475
+ mapping.allocateRevisionSync,
1476
+ );
1477
+
1478
+ const verifierVersion = yield* allocate(
1479
+ configuration.mode,
1480
+ mapping.allocateRevision,
1481
+ mapping.allocateRevisionSync,
1482
+ );
1483
+
1484
+ const nextSecurityRevision = yield* allocateNextSecurityRevision(
1485
+ mapping,
1486
+ configuration.mode,
1487
+ input.expectedRevision.securityRevision,
1488
+ );
1489
+
1490
+ if (nextSecurityRevision === input.expectedRevision.securityRevision)
1491
+ return yield* unavailable();
1492
+
1493
+ return yield* owned(
1494
+ database,
1495
+ mapping,
1496
+ configuration,
1497
+ Effect.gen(function* () {
1498
+ const journal = yield* CurrentCommitJournal;
1499
+
1500
+ const changed = yield* addPasswordIn(mapping, configuration, input, {
1501
+ credentialId,
1502
+ credentialRevision,
1503
+ verifierVersion,
1504
+ nextSecurityRevision,
1505
+ });
1506
+
1507
+ return prepare(changed ? "changed" : "rejected", journal);
1508
+ }),
1509
+ );
1510
+ }).pipe(
1511
+ Effect.provideService(CurrentPasswordSql, database),
1512
+ Effect.provideService(LifecycleHooks, hooks),
1513
+ translateFailure,
1514
+ ),
1515
+ replaceIfCurrent: (uncaptured, prepare) =>
1516
+ Effect.gen(function* () {
1517
+ const input = yield* snapshotMutationInput(uncaptured);
1518
+
1519
+ const credentialRevision = yield* allocate(
1520
+ configuration.mode,
1521
+ mapping.allocateRevision,
1522
+ mapping.allocateRevisionSync,
1523
+ );
1524
+
1525
+ const verifierVersion = yield* allocate(
1526
+ configuration.mode,
1527
+ mapping.allocateRevision,
1528
+ mapping.allocateRevisionSync,
1529
+ );
1530
+
1531
+ const nextSecurityRevision = yield* allocateNextSecurityRevision(
1532
+ mapping,
1533
+ configuration.mode,
1534
+ input.expectedRevision.securityRevision,
1535
+ );
1536
+
1537
+ if (
1538
+ nextSecurityRevision === input.expectedRevision.securityRevision ||
1539
+ (input.credential !== undefined &&
1540
+ credentialRevision === input.credential.credentialRevision)
1541
+ )
1542
+ return yield* unavailable();
1543
+
1544
+ return yield* owned(
1545
+ database,
1546
+ mapping,
1547
+ configuration,
1548
+ Effect.gen(function* () {
1549
+ return yield* replaceIn(
1550
+ mapping,
1551
+ configuration,
1552
+ input,
1553
+ "change-password",
1554
+ { credentialRevision, verifierVersion, nextSecurityRevision },
1555
+ prepare,
1556
+ );
1557
+ }),
1558
+ );
1559
+ }).pipe(
1560
+ Effect.provideService(CurrentPasswordSql, database),
1561
+ Effect.provideService(LifecycleHooks, hooks),
1562
+ translateFailure,
1563
+ ),
1564
+ checkReset: (input) =>
1565
+ (configuration.proof === undefined
1566
+ ? Effect.fail(unavailable())
1567
+ : Effect.gen(function* () {
1568
+ if (
1569
+ input.moduleId.length <= "/reset".length ||
1570
+ !input.moduleId.endsWith("/reset") ||
1571
+ input.purpose !== "password-reset" ||
1572
+ input.binding._tag !== "Subject"
1573
+ )
1574
+ return false;
1575
+ const binding = input.binding;
1576
+ const passwordModuleId = input.moduleId.slice(0, -"/reset".length);
1577
+
1578
+ const current = yield* safeRead(
1579
+ database,
1580
+ configuration,
1581
+ Effect.gen(function* () {
1582
+ const resolved = yield* resolveCredential(
1583
+ mapping,
1584
+ passwordModuleId,
1585
+ binding.identifier,
1586
+ binding.revision.subjectId,
1587
+ false,
1588
+ );
1589
+
1590
+ return (
1591
+ resolved?.snapshot !== undefined &&
1592
+ resolved.snapshot.revision.securityRevision ===
1593
+ binding.revision.securityRevision &&
1594
+ binding.revision.credentials.every((item) =>
1595
+ resolved.snapshot!.revision.credentials.some(
1596
+ (actual) =>
1597
+ actual.credentialId === item.credentialId &&
1598
+ actual.revision === item.revision,
1599
+ ),
1600
+ )
1601
+ );
1602
+ }),
1603
+ );
1604
+
1605
+ return current ? yield* probeProofCompletion(configuration, input) : false;
1606
+ })
1607
+ ).pipe(
1608
+ Effect.provideService(CurrentPasswordSql, database),
1609
+ Effect.provideService(LifecycleHooks, hooks),
1610
+ translateFailure,
1611
+ ),
1612
+ resetWithProof: (uncaptured, prepare) =>
1613
+ (configuration.proof === undefined
1614
+ ? Effect.fail(unavailable())
1615
+ : Effect.gen(function* () {
1616
+ const input = Object.freeze({
1617
+ ...(yield* snapshotMutationInput(uncaptured)),
1618
+ completion: uncaptured.completion,
1619
+ });
1620
+
1621
+ const credentialRevision = yield* allocate(
1622
+ configuration.mode,
1623
+ mapping.allocateRevision,
1624
+ mapping.allocateRevisionSync,
1625
+ );
1626
+
1627
+ const verifierVersion = yield* allocate(
1628
+ configuration.mode,
1629
+ mapping.allocateRevision,
1630
+ mapping.allocateRevisionSync,
1631
+ );
1632
+
1633
+ const nextSecurityRevision = yield* allocateNextSecurityRevision(
1634
+ mapping,
1635
+ configuration.mode,
1636
+ input.expectedRevision.securityRevision,
1637
+ );
1638
+
1639
+ if (
1640
+ nextSecurityRevision === input.expectedRevision.securityRevision ||
1641
+ (input.credential !== undefined &&
1642
+ credentialRevision === input.credential.credentialRevision)
1643
+ )
1644
+ return yield* unavailable();
1645
+
1646
+ return yield* owned(
1647
+ database,
1648
+ mapping,
1649
+ configuration,
1650
+ Effect.gen(function* () {
1651
+ const journal = yield* CurrentCommitJournal;
1652
+
1653
+ if (!proofCompletionMatchesPassword(input)) return prepare("rejected", journal);
1654
+
1655
+ const authority = yield* validateMutationAuthority(
1656
+ mapping,
1657
+ configuration,
1658
+ input,
1659
+ "reset-password",
1660
+ );
1661
+
1662
+ if (authority === undefined) return prepare("rejected", journal);
1663
+ if ((yield* commandExists(mapping, input.moduleId, input.commandId, true)).length)
1664
+ return prepare("rejected", journal);
1665
+
1666
+ const current = yield* currentExpectedCredential(
1667
+ mapping,
1668
+ input,
1669
+ authority.nativeSubjectId,
1670
+ );
1671
+
1672
+ if (!current) return prepare("rejected", journal);
1673
+ let passwordReceipt: PreparedCommit<any> | undefined;
1674
+
1675
+ yield* completeProofPlanIn(
1676
+ configuration.proof!.mapping,
1677
+ configuration.proof!.configuration,
1678
+ input.completion,
1679
+ Effect.gen(function* () {
1680
+ return yield* writeReplacement(
1681
+ mapping,
1682
+ input,
1683
+ authority.nativeSubjectId,
1684
+ { credentialRevision, verifierVersion, nextSecurityRevision },
1685
+ authority.now,
1686
+ );
1687
+ }),
1688
+ (decision) => {
1689
+ passwordReceipt = prepare(
1690
+ decision === "completed" ? "changed" : "rejected",
1691
+ journal,
1692
+ );
1693
+
1694
+ return decision;
1695
+ },
1696
+ );
1697
+
1698
+ return passwordReceipt ?? prepare("rejected", journal);
1699
+ }),
1700
+ );
1701
+ })
1702
+ ).pipe(
1703
+ Effect.provideService(CurrentPasswordSql, database),
1704
+ Effect.provideService(LifecycleHooks, hooks),
1705
+ translateFailure,
1706
+ ),
1707
+ cleanupAttempts: (input, prepare) =>
1708
+ owned(
1709
+ database,
1710
+ mapping,
1711
+ configuration,
1712
+ Effect.gen(function* () {
1713
+ const transaction = yield* CurrentPasswordSql;
1714
+ const journal = yield* CurrentCommitJournal;
1715
+
1716
+ const now = yield* nowMillis;
1717
+ const nativeNow = mapping.encodeInstant(now);
1718
+ let remaining = input.limit;
1719
+ let hasMore = false;
1720
+ const a = attemptColumns(mapping);
1721
+ const c = chargeColumns(mapping);
1722
+
1723
+ const attempts = yield* selectRows(
1724
+ transaction
1725
+ .select({ attemptId: a.attemptId })
1726
+ .from(mapping.attempt.table)
1727
+ .where(and(eq(a.moduleId, input.moduleId), lte(a.retentionUntil, nativeNow)))
1728
+ .limit(remaining + 1),
1729
+ false,
1730
+ );
1731
+
1732
+ if (attempts.length > remaining) hasMore = true;
1733
+ const attemptCandidates = attempts.slice(0, remaining);
1734
+
1735
+ remaining -= attemptCandidates.length;
1736
+
1737
+ const charges = yield* selectRows(
1738
+ transaction
1739
+ .select({
1740
+ action: c.action,
1741
+ scopeKind: c.scopeKind,
1742
+ scopeKey: c.scopeKey,
1743
+ attemptId: c.attemptId,
1744
+ })
1745
+ .from(mapping.charge.table)
1746
+ .where(and(eq(c.moduleId, input.moduleId), lte(c.retentionUntil, nativeNow)))
1747
+ .limit(remaining + 1),
1748
+ false,
1749
+ );
1750
+
1751
+ if (charges.length > remaining) hasMore = true;
1752
+ const chargeCandidates = charges.slice(0, remaining);
1753
+
1754
+ remaining -= chargeCandidates.length;
1755
+ const sc = scopeColumns(mapping);
1756
+
1757
+ const scopes = yield* selectRows(
1758
+ transaction
1759
+ .select({ action: sc.action, scopeKind: sc.scopeKind, scopeKey: sc.scopeKey })
1760
+ .from(mapping.rateScope.table)
1761
+ .where(
1762
+ and(
1763
+ eq(sc.moduleId, input.moduleId),
1764
+ notExists(
1765
+ transaction
1766
+ .select({ one: sql`1` })
1767
+ .from(mapping.charge.table)
1768
+ .where(
1769
+ and(
1770
+ eq(c.moduleId, sc.moduleId),
1771
+ eq(c.action, sc.action),
1772
+ eq(c.scopeKind, sc.scopeKind),
1773
+ eq(c.scopeKey, sc.scopeKey),
1774
+ ),
1775
+ ),
1776
+ ),
1777
+ ),
1778
+ )
1779
+ .limit(remaining + 1),
1780
+ remaining === 0 ? false : configuration.locking,
1781
+ );
1782
+
1783
+ if (scopes.length > remaining) hasMore = true;
1784
+ const selectedScopes = scopes.slice(0, remaining);
1785
+
1786
+ remaining -= selectedScopes.length;
1787
+ const selectedCharges: typeof chargeCandidates = [];
1788
+
1789
+ for (const candidate of chargeCandidates) {
1790
+ const locked = yield* selectRows(
1791
+ transaction
1792
+ .select({
1793
+ action: c.action,
1794
+ scopeKind: c.scopeKind,
1795
+ scopeKey: c.scopeKey,
1796
+ attemptId: c.attemptId,
1797
+ })
1798
+ .from(mapping.charge.table)
1799
+ .where(
1800
+ and(
1801
+ eq(c.moduleId, input.moduleId),
1802
+ eq(c.action, candidate.action),
1803
+ eq(c.scopeKind, candidate.scopeKind),
1804
+ eq(c.scopeKey, candidate.scopeKey),
1805
+ eq(c.attemptId, candidate.attemptId),
1806
+ lte(c.retentionUntil, nativeNow),
1807
+ ),
1808
+ )
1809
+ .limit(1),
1810
+ configuration.locking,
1811
+ );
1812
+
1813
+ if (locked[0] !== undefined) selectedCharges.push(locked[0] as any);
1814
+ }
1815
+ const selectedAttempts: typeof attemptCandidates = [];
1816
+
1817
+ for (const candidate of attemptCandidates) {
1818
+ const locked = yield* selectRows(
1819
+ transaction
1820
+ .select({ attemptId: a.attemptId })
1821
+ .from(mapping.attempt.table)
1822
+ .where(
1823
+ and(
1824
+ eq(a.moduleId, input.moduleId),
1825
+ eq(a.attemptId, candidate.attemptId),
1826
+ lte(a.retentionUntil, nativeNow),
1827
+ ),
1828
+ )
1829
+ .limit(1),
1830
+ configuration.locking,
1831
+ );
1832
+
1833
+ if (locked[0] !== undefined) selectedAttempts.push(locked[0] as any);
1834
+ }
1835
+
1836
+ const prepared = prepare(
1837
+ {
1838
+ removed: selectedAttempts.length + selectedCharges.length + selectedScopes.length,
1839
+ hasMore,
1840
+ },
1841
+ journal,
1842
+ );
1843
+
1844
+ for (const row of selectedCharges)
1845
+ yield* transaction
1846
+ .delete(mapping.charge.table)
1847
+ .where(
1848
+ and(
1849
+ eq(c.moduleId, input.moduleId),
1850
+ eq(c.action, row.action),
1851
+ eq(c.scopeKind, row.scopeKind),
1852
+ eq(c.scopeKey, row.scopeKey),
1853
+ eq(c.attemptId, row.attemptId),
1854
+ lte(c.retentionUntil, nativeNow),
1855
+ ),
1856
+ );
1857
+ if (selectedAttempts.length > 0)
1858
+ yield* transaction.delete(mapping.attempt.table).where(
1859
+ and(
1860
+ eq(a.moduleId, input.moduleId),
1861
+ inArray(
1862
+ a.attemptId,
1863
+ selectedAttempts.map((row: any) => row.attemptId),
1864
+ ),
1865
+ lte(a.retentionUntil, nativeNow),
1866
+ ),
1867
+ );
1868
+ for (const row of selectedScopes)
1869
+ yield* transaction.delete(mapping.rateScope.table).where(
1870
+ and(
1871
+ eq(sc.moduleId, input.moduleId),
1872
+ eq(sc.action, row.action),
1873
+ eq(sc.scopeKind, row.scopeKind),
1874
+ eq(sc.scopeKey, row.scopeKey),
1875
+ notExists(
1876
+ transaction
1877
+ .select({ one: sql`1` })
1878
+ .from(mapping.charge.table)
1879
+ .where(
1880
+ and(
1881
+ eq(c.moduleId, sc.moduleId),
1882
+ eq(c.action, sc.action),
1883
+ eq(c.scopeKind, sc.scopeKind),
1884
+ eq(c.scopeKey, sc.scopeKey),
1885
+ ),
1886
+ ),
1887
+ ),
1888
+ ),
1889
+ );
1890
+
1891
+ return prepared;
1892
+ }),
1893
+ ).pipe(
1894
+ Effect.provideService(CurrentPasswordSql, database),
1895
+ Effect.provideService(LifecycleHooks, hooks),
1896
+ translateFailure,
1897
+ ),
1898
+ });
1899
+ });
1900
+
1901
+ const currentExpectedCredential = Effect.fn("DrizzlePassword.currentExpectedCredential")(
1902
+ function* (mapping: Mapping, input: PasswordMutationInput, nativeSubjectId: unknown) {
1903
+ if (input.credential === undefined) return false;
1904
+
1905
+ const row = (yield* readPasswordCredential(
1906
+ mapping,
1907
+ input.moduleId,
1908
+ nativeSubjectId,
1909
+ true,
1910
+ ))[0];
1911
+
1912
+ if (row === undefined) return false;
1913
+
1914
+ return (
1915
+ row[mapping.credential.credentialId] === input.credential.credentialId &&
1916
+ row[mapping.credential.credentialRevision] === input.credential.credentialRevision &&
1917
+ row[mapping.credential.verifierVersion] === input.credential.verifierVersion &&
1918
+ row[mapping.credential.verifier] === Redacted.value(input.credential.verifier) &&
1919
+ row[mapping.credential.normalization] === input.credential.normalization &&
1920
+ input.authorization.challenge.targetCredentialId === input.credential.credentialId
1921
+ );
1922
+ },
1923
+ );
1924
+
1925
+ const checkMutationApplied = Effect.fn("DrizzlePassword.checkMutationApplied")(function* (
1926
+ mapping: Mapping,
1927
+ input: PasswordMutationInput,
1928
+ nativeSubjectId: unknown,
1929
+ revisions: {
1930
+ readonly credentialId: string;
1931
+ readonly credentialRevision: SecurityRevision;
1932
+ readonly verifierVersion: SecurityRevision;
1933
+ readonly nextSecurityRevision: SecurityRevision;
1934
+ },
1935
+ now: number,
1936
+ ) {
1937
+ const database = yield* CurrentPasswordSql;
1938
+
1939
+ const c = credentialColumns(mapping),
1940
+ s = subjectColumns(mapping);
1941
+
1942
+ const credential = (yield* database
1943
+ .select()
1944
+ .from(mapping.credential.table)
1945
+ .where(
1946
+ and(
1947
+ eq(c.moduleId, input.moduleId),
1948
+ eq(c.subjectId, nativeSubjectId),
1949
+ eq(c.credentialId, revisions.credentialId),
1950
+ eq(c.credentialRevision, revisions.credentialRevision),
1951
+ eq(c.verifierVersion, revisions.verifierVersion),
1952
+ eq(c.verifier, Redacted.value(input.replacement.verifier)),
1953
+ eq(c.normalization, input.replacement.normalization),
1954
+ ),
1955
+ )
1956
+ .limit(1))[0];
1957
+
1958
+ const subject = (yield* database
1959
+ .select()
1960
+ .from(mapping.subject.table)
1961
+ .where(and(eq(s.id, nativeSubjectId), eq(s.securityRevision, revisions.nextSecurityRevision)))
1962
+ .limit(1))[0];
1963
+
1964
+ const authorityCredential = (yield* database
1965
+ .select()
1966
+ .from(mapping.authorityCredential.table)
1967
+ .where(
1968
+ and(
1969
+ eq(authorityCredentialColumns(mapping).subjectId, nativeSubjectId),
1970
+ eq(authorityCredentialColumns(mapping).credentialId, revisions.credentialId),
1971
+ eq(authorityCredentialColumns(mapping).revision, revisions.credentialRevision),
1972
+ ),
1973
+ )
1974
+ .limit(1))[0];
1975
+
1976
+ return (
1977
+ credential !== undefined &&
1978
+ (yield* mutationPostconditions(
1979
+ mapping,
1980
+ input,
1981
+ nativeSubjectId,
1982
+ subject,
1983
+ authorityCredential,
1984
+ now,
1985
+ ))
1986
+ );
1987
+ });
1988
+
1989
+ const writeReplacement = Effect.fn("DrizzlePassword.writeReplacement")(function* (
1990
+ mapping: Mapping,
1991
+ input: PasswordMutationInput,
1992
+ nativeSubjectId: unknown,
1993
+ revisions: {
1994
+ readonly credentialRevision: SecurityRevision;
1995
+ readonly verifierVersion: SecurityRevision;
1996
+ readonly nextSecurityRevision: SecurityRevision;
1997
+ },
1998
+ now: number,
1999
+ ) {
2000
+ const database = yield* CurrentPasswordSql;
2001
+
2002
+ const c = credentialColumns(mapping);
2003
+ const s = subjectColumns(mapping);
2004
+
2005
+ yield* database
2006
+ .update(mapping.credential.table)
2007
+ .set(
2008
+ mapping.credential.encodeReplacement({
2009
+ replacement: input.replacement,
2010
+ credentialRevision: revisions.credentialRevision,
2011
+ verifierVersion: revisions.verifierVersion,
2012
+ }),
2013
+ )
2014
+ .where(
2015
+ and(
2016
+ eq(c.moduleId, input.moduleId),
2017
+ eq(c.subjectId, nativeSubjectId),
2018
+ eq(c.credentialId, input.credential!.credentialId),
2019
+ eq(c.credentialRevision, input.credential!.credentialRevision),
2020
+ eq(c.verifierVersion, input.credential!.verifierVersion),
2021
+ eq(c.verifier, Redacted.value(input.credential!.verifier)),
2022
+ eq(c.normalization, input.credential!.normalization),
2023
+ ),
2024
+ );
2025
+ yield* database
2026
+ .update(mapping.subject.table)
2027
+ .set(updateValues([[mapping.subject.securityRevision, revisions.nextSecurityRevision]]))
2028
+ .where(
2029
+ and(
2030
+ eq(s.id, nativeSubjectId),
2031
+ eq(s.securityRevision, input.expectedRevision.securityRevision),
2032
+ ),
2033
+ );
2034
+ yield* database
2035
+ .update(mapping.authorityCredential.table)
2036
+ .set(mapping.authorityCredential.encodeRevision(revisions.credentialRevision))
2037
+ .where(
2038
+ and(
2039
+ eq(authorityCredentialColumns(mapping).subjectId, nativeSubjectId),
2040
+ eq(authorityCredentialColumns(mapping).credentialId, input.credential!.credentialId),
2041
+ eq(authorityCredentialColumns(mapping).revision, input.credential!.credentialRevision),
2042
+ ),
2043
+ );
2044
+ yield* insertCommand(mapping, input, input.authorization.challenge.action, now);
2045
+
2046
+ return yield* checkMutationApplied(
2047
+ mapping,
2048
+ input,
2049
+ nativeSubjectId,
2050
+ {
2051
+ ...revisions,
2052
+ credentialId: input.credential!.credentialId,
2053
+ },
2054
+ now,
2055
+ );
2056
+ });
2057
+
2058
+ const replaceIn = Effect.fn("Drizzle.replaceIn")(function* <A>(
2059
+ mapping: Mapping,
2060
+ configuration: PasswordSqlConfiguration,
2061
+ input: PasswordMutationInput,
2062
+ action: PasswordAction,
2063
+ revisions: {
2064
+ readonly credentialRevision: SecurityRevision;
2065
+ readonly verifierVersion: SecurityRevision;
2066
+ readonly nextSecurityRevision: SecurityRevision;
2067
+ },
2068
+ prepare: (value: PasswordMutationDecision, journal: CommitJournal) => PreparedCommit<A>,
2069
+ ) {
2070
+ const journal = yield* CurrentCommitJournal;
2071
+
2072
+ const authority = yield* validateMutationAuthority(mapping, configuration, input, action);
2073
+
2074
+ if (authority === undefined) return prepare("rejected", journal);
2075
+ if ((yield* commandExists(mapping, input.moduleId, input.commandId, true)).length)
2076
+ return prepare("rejected", journal);
2077
+ if (!(yield* currentExpectedCredential(mapping, input, authority.nativeSubjectId)))
2078
+ return prepare("rejected", journal);
2079
+ const prepared = prepare("changed", journal);
2080
+
2081
+ if (
2082
+ !(yield* writeReplacement(
2083
+ mapping,
2084
+ input,
2085
+ authority.nativeSubjectId,
2086
+ revisions,
2087
+ authority.now,
2088
+ ))
2089
+ )
2090
+ return yield* unavailable();
2091
+
2092
+ return prepared;
2093
+ });
2094
+
2095
+ /** Internal bound-owner primitives; deliberately not re-exported by driver modules. */
2096
+ const passwordSqlKernel = {
2097
+ allocate,
2098
+ allocateNextSecurityRevision,
2099
+ owned,
2100
+ safeRead,
2101
+ snapshotMutationInput,
2102
+ lockSubject,
2103
+ lockIdentifier,
2104
+ readPasswordCredential,
2105
+ resolveCredential,
2106
+ validateMutationAuthority,
2107
+ currentExpectedCredential,
2108
+ commandExists,
2109
+ addPasswordIn,
2110
+ checkMutationApplied,
2111
+ writeReplacement,
2112
+ evidenceSatisfiedAt,
2113
+ sameCredentialSnapshot,
2114
+ subjectColumns,
2115
+ identifierColumns,
2116
+ credentialColumns,
2117
+ authorityCredentialColumns,
2118
+ proofCompletionMatchesPassword,
2119
+ };
2120
+
2121
+ return { makeSqlPasswordPersistence, passwordSqlKernel };
2122
+ };