@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,845 @@
1
+ import { type OAuthAccountRevision, snapshotOAuthSync } from "@yielded/auth/OAuth";
2
+ import * as M from "@yielded/auth/OAuth";
3
+ import { AuthenticationRequirement } from "@yielded/auth/Sessions";
4
+ /* oxlint-disable no-explicit-any -- private table erasure retains declared callback error/requirements channels. */
5
+ import { and, eq, isNull, sql, type SQL, type Table } from "drizzle-orm";
6
+ import type { EffectDrizzleQueryError } from "drizzle-orm/effect-core";
7
+ import { DateTime, Effect, Schema } from "effect";
8
+
9
+ import type {
10
+ OAuthConnectedAuthorityMapping,
11
+ OAuthConnectedClientRegistrationTable,
12
+ OAuthConnectedCohortTable,
13
+ OAuthConnectedMapping,
14
+ OAuthConnectedPolicyInput,
15
+ OAuthConnectedRevocationMapping,
16
+ } from "./oauth-connected-model";
17
+ import { currentSubject } from "./oauth-flow";
18
+ import {
19
+ both,
20
+ col,
21
+ copiedRow,
22
+ equal,
23
+ CurrentOAuthTransaction,
24
+ type OAuthOwner,
25
+ type Row,
26
+ } from "./oauth-owner";
27
+ import {
28
+ digest,
29
+ invariant,
30
+ oauthIdentityKey,
31
+ sameRevision,
32
+ satisfies,
33
+ storage,
34
+ unavailable,
35
+ } from "./oauth-state";
36
+
37
+ export type Authority = OAuthConnectedAuthorityMapping<any, any, any, any, any, any, any> & {
38
+ readonly client: Partial<Pick<OAuthConnectedClientRegistrationTable<any>, "encodeInsert">>;
39
+ readonly cohort: Partial<Pick<OAuthConnectedCohortTable<any>, "encodeInsert">>;
40
+ };
41
+
42
+ export type Mapping = OAuthConnectedMapping<
43
+ any,
44
+ any,
45
+ any,
46
+ any,
47
+ any,
48
+ any,
49
+ any,
50
+ any,
51
+ any,
52
+ any,
53
+ any,
54
+ any
55
+ >;
56
+
57
+ export type Revocations = OAuthConnectedRevocationMapping<any, any, any, any, any, any, any, any>;
58
+ export type Current = NonNullable<Effect.Success<ReturnType<typeof currentSubject>>>;
59
+ export const contextStorage = storage(M.OAuthConnectedTransactionContext);
60
+ export const flowStorage = storage(M.OAuthConnectedPendingFlow);
61
+ export const claimStorage = storage(M.OAuthConnectedClaim);
62
+ export const tokenContextStorage = storage(M.OAuthConnectedTokenContext);
63
+ export const sealedStorage = storage(M.OAuthConnectedSealedTokens);
64
+ export const grantStorage = storage(M.OAuthConnectedStoredGrant);
65
+
66
+ export const admissionStorage = storage(
67
+ Schema.Struct({
68
+ grant: M.OAuthConnectedStoredGrant,
69
+ authorization: M.OAuthConnectedUseAuthorization,
70
+ }),
71
+ );
72
+
73
+ export const commandStorage = storage(
74
+ Schema.Struct({
75
+ context: M.OAuthConnectedTokenContext,
76
+ originalDigest: Schema.String.check(Schema.isPattern(/^[A-Za-z0-9_-]{43}$/)),
77
+ revocationJobId: Schema.optionalKey(Schema.String.check(Schema.isMaxLength(256))),
78
+ }),
79
+ );
80
+
81
+ export const disconnectStorage = storage(M.OAuthConnectedDisconnectGrant);
82
+ export const refreshStorage = storage(M.OAuthConnectedRefreshClaim);
83
+ export const jobStorage = storage(M.OAuthConnectedRevocationJob);
84
+ export const summaryStorage = storage(M.OAuthConnectedSummary);
85
+ export const disconnectedStorage = storage(M.OAuthConnectedDisconnected);
86
+ const strings = storage(Schema.Array(Schema.String));
87
+
88
+ const safeInteger = Schema.Int.check(
89
+ Schema.isBetween({ minimum: 0, maximum: Number.MAX_SAFE_INTEGER }),
90
+ );
91
+
92
+ const orderParser = Schema.decodeSync(safeInteger);
93
+ const canonicalOrder = Schema.String.check(Schema.isPattern(/^(0|[1-9][0-9]{0,15})$/));
94
+ const orderTextParser = Schema.decodeSync(canonicalOrder);
95
+
96
+ export const orderNumber = (value: string) => {
97
+ const number = orderParser(Number(orderTextParser(value)));
98
+
99
+ invariant(String(number) === value);
100
+
101
+ return number;
102
+ };
103
+
104
+ export const nativeOrder = (mapping: Authority, value: unknown) =>
105
+ orderParser(mapping.order.decode(value));
106
+
107
+ const encoder = new TextEncoder();
108
+ const decoder = new TextDecoder("utf-8", { fatal: true });
109
+
110
+ const key = (domain: string, fields: ReadonlyArray<string>) => {
111
+ for (const value of fields) invariant(decoder.decode(encoder.encode(value)) === value);
112
+
113
+ return "v1:" + digest(strings.encode([domain, ...fields]));
114
+ };
115
+
116
+ export const clientKey = (configuration: M.OAuthConnectedConfiguration) =>
117
+ key("effect-auth/oauth-connected-client/v1", [
118
+ configuration.provider,
119
+ configuration.issuer,
120
+ configuration.profile.clientRegistrationId,
121
+ ]);
122
+
123
+ export const cohortKey = (client: string, identity: string) =>
124
+ key("effect-auth/oauth-connected-cohort/v1", [client, identity]);
125
+
126
+ export const initialGeneration = (cohort: string) =>
127
+ M.OAuthConnectedTarget.fields.cohortGeneration.make("initial:" + digest(cohort));
128
+
129
+ export const configuration = (flow: M.OAuthConnectedPendingFlow): M.OAuthConnectedConfiguration =>
130
+ snapshotOAuthSync(M.OAuthConnectedConfiguration, flow.context);
131
+
132
+ export const retainedUntil = (mapping: Authority, now: number) => {
133
+ invariant(
134
+ Number.isSafeInteger(mapping.retentionMillis) &&
135
+ mapping.retentionMillis >= 120000 &&
136
+ mapping.retentionMillis <= 2592000000,
137
+ );
138
+
139
+ return orderParser(now + mapping.retentionMillis);
140
+ };
141
+
142
+ export const rowCount = Effect.fn("oauthConnected.rowCount")(function* (table: Table, where: SQL) {
143
+ const owner = yield* CurrentOAuthTransaction;
144
+
145
+ const rows = yield* (
146
+ owner.database
147
+ .select({ value: sql<number>`count(*)`.mapWith(Number) })
148
+ .from(table)
149
+ .where(where) as Effect.Effect<ReadonlyArray<{ value: number }>, EffectDrizzleQueryError>
150
+ ).pipe(Effect.mapError(unavailable));
151
+
152
+ invariant(rows.length === 1);
153
+
154
+ return orderParser(rows[0]!.value);
155
+ });
156
+
157
+ export const countCondition = (table: Table, where: SQL, count: number) =>
158
+ sql`(select count(*) from ${table} where ${where}) = ${count}`;
159
+
160
+ export const current = Effect.fn("oauthConnected.current")(function* (
161
+ mapping: Mapping,
162
+ subject: OAuthAccountRevision["subjectId"],
163
+ ) {
164
+ const native = yield* mapping.subjectId.toNative(subject);
165
+
166
+ return yield* currentSubject(mapping, native);
167
+ });
168
+
169
+ export const nativeCopy = <N>(value: N): N => copiedRow({ value }).value as N;
170
+
171
+ const policyData = (input: OAuthConnectedPolicyInput<any>): OAuthConnectedPolicyInput<any> =>
172
+ Object.freeze({
173
+ ...input,
174
+ subjectId: nativeCopy(input.subjectId),
175
+ revision: snapshotOAuthSync(M.OAuthConnectedUseAuthorization.fields.revision, input.revision),
176
+ authorization:
177
+ input.kind === "action"
178
+ ? snapshotOAuthSync(M.OAuthConnectedActionAuthorization, input.authorization)
179
+ : snapshotOAuthSync(M.OAuthConnectedUseAuthorization, input.authorization),
180
+ ...(input.kind === "action"
181
+ ? { configuration: snapshotOAuthSync(M.OAuthConnectedConfiguration, input.configuration) }
182
+ : {}),
183
+ ...(input.grant === undefined
184
+ ? {}
185
+ : { grant: snapshotOAuthSync(M.OAuthConnectedTokenContext, input.grant) }),
186
+ }) as OAuthConnectedPolicyInput<any>;
187
+
188
+ export const jobTable = (mapping: Authority) => {
189
+ const direct = (mapping as Partial<Revocations>).job;
190
+ const configured = (mapping as Partial<Mapping>).revocation;
191
+
192
+ return direct ?? (configured?.mode === "cohort" ? configured.job : undefined);
193
+ };
194
+
195
+ export const policy = Effect.fn("oauthConnected.policy")(function* (
196
+ mapping: Mapping,
197
+ input: OAuthConnectedPolicyInput<any>,
198
+ ) {
199
+ const owner = yield* CurrentOAuthTransaction;
200
+
201
+ invariant((mapping.policy.guards?.length ?? 0) <= 32);
202
+ for (const guard of mapping.policy.guards ?? []) {
203
+ const read = yield* owner.read(guard.table, guard.condition(policyData(input)), {
204
+ orderBy: col(guard.table, guard.orderBy),
205
+ });
206
+
207
+ invariant(read.rows.length > 0);
208
+ }
209
+ const condition = mapping.policy.condition(policyData(input));
210
+ const accepted = yield* owner.check(condition);
211
+
212
+ if (accepted) owner.postconditions.push(condition);
213
+
214
+ return accepted;
215
+ });
216
+
217
+ export const action = Effect.fn("oauthConnected.action")(function* (
218
+ mapping: Mapping,
219
+ found: Current,
220
+ authorization: M.OAuthConnectedActionAuthorization,
221
+ operation: "issue" | "claim" | "settle" | "disconnect",
222
+ expected: {
223
+ readonly moduleId: string;
224
+ readonly flowId: string;
225
+ readonly commandId: string;
226
+ readonly revision: OAuthAccountRevision;
227
+ readonly intent: string;
228
+ readonly maximumAgeMillis: number;
229
+ readonly configuration: M.OAuthConnectedConfiguration;
230
+ readonly grant?: M.OAuthConnectedTokenContext;
231
+ },
232
+ ) {
233
+ const owner = yield* CurrentOAuthTransaction;
234
+
235
+ const challenge = authorization.challenge,
236
+ evidence = authorization.evidence;
237
+
238
+ const name =
239
+ operation === "issue"
240
+ ? "connected-begin"
241
+ : operation === "disconnect"
242
+ ? "connected-disconnect"
243
+ : "connected-complete";
244
+
245
+ const intentDigest = digest(expected.intent);
246
+
247
+ const binding = digest(
248
+ strings.encode([
249
+ "effect-auth/oauth-connected-action/v1",
250
+ expected.moduleId,
251
+ name,
252
+ expected.flowId,
253
+ expected.commandId,
254
+ intentDigest,
255
+ ]),
256
+ );
257
+
258
+ if (
259
+ challenge.moduleId !== expected.moduleId ||
260
+ challenge.action !== name ||
261
+ challenge.flowId !== expected.flowId ||
262
+ challenge.commandId !== expected.commandId ||
263
+ challenge.intentDigest !== intentDigest ||
264
+ challenge.bindingDigest !== binding ||
265
+ evidence.flowId !== expected.flowId ||
266
+ evidence.bindingDigest !== binding ||
267
+ !sameRevision(challenge.revision, expected.revision) ||
268
+ !sameRevision(evidence.revision, expected.revision) ||
269
+ !sameRevision(found.revision, expected.revision)
270
+ )
271
+ return false;
272
+ const now = yield* owner.now(mapping.clock);
273
+
274
+ const requirement = snapshotOAuthSync(
275
+ AuthenticationRequirement,
276
+ mapping.subject.decodeActionRequirement(copiedRow(found.row), name),
277
+ );
278
+
279
+ const revisions = new Map(
280
+ found.revision.credentials.map((item) => [item.credentialId, item.revision]),
281
+ );
282
+
283
+ if (
284
+ evidence.proofs.some(
285
+ (proof) =>
286
+ !revisions.has(proof.credentialId) || DateTime.toEpochMillis(proof.verifiedAt) > now,
287
+ )
288
+ )
289
+ return false;
290
+ for (const required of [authorization.requirement, requirement]) {
291
+ const age = Math.min(expected.maximumAgeMillis, required.maximumAgeMillis);
292
+
293
+ const fresh = evidence.proofs.filter(
294
+ (proof) => now - DateTime.toEpochMillis(proof.verifiedAt) < age,
295
+ );
296
+
297
+ if (!satisfies(fresh, required)) return false;
298
+ owner.postconditions.push(
299
+ sql`${mapping.clock.engineNowMillis} >= ${now} and ${both(...fresh.map((proof) => sql`${mapping.clock.engineNowMillis} < ${DateTime.toEpochMillis(proof.verifiedAt) + age}`))}`,
300
+ );
301
+ }
302
+
303
+ return yield* policy(mapping, {
304
+ subjectId: found.nativeId,
305
+ revision: found.revision,
306
+ kind: "action",
307
+ operation,
308
+ authorization,
309
+ configuration: expected.configuration,
310
+ ...(expected.grant === undefined ? {} : { grant: expected.grant }),
311
+ });
312
+ });
313
+
314
+ export const useAuthority = Effect.fn("oauthConnected.useAuthority")(function* (
315
+ mapping: Mapping,
316
+ authorization: M.OAuthConnectedUseAuthorization,
317
+ kind: "metadata" | "use",
318
+ grant?: M.OAuthConnectedTokenContext,
319
+ ) {
320
+ const owner = yield* CurrentOAuthTransaction;
321
+ const found = yield* current(mapping, authorization.revision.subjectId);
322
+ const now = yield* owner.now(mapping.clock);
323
+
324
+ if (
325
+ found === undefined ||
326
+ authorization.purpose !== kind ||
327
+ !sameRevision(found.revision, authorization.revision) ||
328
+ now >= authorization.expiresAtMillis ||
329
+ (grant !== undefined &&
330
+ (grant.moduleId !== authorization.moduleId ||
331
+ grant.subjectId !== authorization.revision.subjectId ||
332
+ (authorization.grantId !== undefined && authorization.grantId !== grant.grantId) ||
333
+ (authorization.profileKey !== undefined &&
334
+ authorization.profileKey !== grant.configuration.profile.key)))
335
+ )
336
+ return undefined;
337
+ if (
338
+ !(yield* policy(mapping, {
339
+ subjectId: found.nativeId,
340
+ revision: found.revision,
341
+ kind,
342
+ authorization,
343
+ ...(grant === undefined ? {} : { grant }),
344
+ }))
345
+ )
346
+ return undefined;
347
+ owner.postconditions.push(
348
+ sql`${mapping.clock.engineNowMillis} >= ${now} and ${mapping.clock.engineNowMillis} < ${authorization.expiresAtMillis}`,
349
+ );
350
+
351
+ return found;
352
+ });
353
+
354
+ export const scopeKey = (provider: string, issuer: string) =>
355
+ "scope:" + key("effect-auth/oauth-connected-provider-issuer/v1", [provider, issuer]);
356
+
357
+ export interface ScopeAnchor {
358
+ readonly id: string;
359
+ row: Row;
360
+ }
361
+
362
+ export interface ClientAnchor {
363
+ readonly id: string;
364
+ row: Row;
365
+ counter: number;
366
+ readonly scope: ScopeAnchor;
367
+ }
368
+
369
+ const scopeCache = new WeakMap<OAuthOwner, Map<Table, Map<string, ScopeAnchor>>>();
370
+ const clientCache = new WeakMap<OAuthOwner, Map<Table, Map<string, ClientAnchor>>>();
371
+
372
+ const anchors = Effect.fn("oauthConnected.anchors")(function* <A>(
373
+ cache: WeakMap<OAuthOwner, Map<Table, Map<string, A>>>,
374
+ table: Table,
375
+ ) {
376
+ const owner = yield* CurrentOAuthTransaction;
377
+ let tables = cache.get(owner);
378
+
379
+ if (tables === undefined) {
380
+ tables = new Map();
381
+ cache.set(owner, tables);
382
+ }
383
+ let rows = tables.get(table);
384
+
385
+ if (rows === undefined) {
386
+ rows = new Map();
387
+ tables.set(table, rows);
388
+ }
389
+
390
+ return rows;
391
+ });
392
+
393
+ /** Empty registration ID is impossible in the public profile label schema. The
394
+ * distinct 52-byte key domain shares the physical table, never a client counter. */
395
+ export const scope = Effect.fn("oauthConnected.scope")(function* (
396
+ mapping: Authority,
397
+ config: M.OAuthConnectedConfiguration,
398
+ create: boolean,
399
+ ) {
400
+ const owner = yield* CurrentOAuthTransaction;
401
+
402
+ const c = mapping.client,
403
+ id = scopeKey(config.provider, config.issuer),
404
+ cache = yield* anchors(scopeCache, c.table);
405
+
406
+ const existing = cache.get(id);
407
+
408
+ if (existing !== undefined) return existing;
409
+ const read = yield* owner.read(c.table, equal(c.table, { [c.clientKey]: id }), { limit: 1 });
410
+
411
+ if (read.rows.length === 0) {
412
+ const related = [
413
+ mapping.flow,
414
+ mapping.grant,
415
+ mapping.cohort,
416
+ ...(jobTable(mapping) === undefined ? [] : [jobTable(mapping)!]),
417
+ ];
418
+
419
+ invariant(
420
+ yield* owner.check(
421
+ both(
422
+ sql`not exists(select 1 from ${c.table} where ${owner.exact(c.table, { [c.provider]: config.provider, [c.issuer]: config.issuer })})`,
423
+ ...related.map(
424
+ (v) =>
425
+ sql`not exists(select 1 from ${v.table} where not exists(select 1 from ${c.table} where ${col(c.table, c.clientKey)} = ${col(v.table, v.clientKey)}))`,
426
+ ),
427
+ ),
428
+ ),
429
+ );
430
+ if (!create) return undefined;
431
+ invariant(c.encodeInsert !== undefined);
432
+
433
+ const inserted = yield* owner.insert(
434
+ c.table,
435
+ {
436
+ ...c.encodeInsert(snapshotOAuthSync(M.OAuthConnectedConfiguration, config)),
437
+ [c.clientKey]: id,
438
+ [c.provider]: config.provider,
439
+ [c.issuer]: config.issuer,
440
+ [c.clientRegistrationId]: "",
441
+ [c.counter]: mapping.order.encode(0),
442
+ [c.version]: owner.marker,
443
+ },
444
+ { [c.clientKey]: id },
445
+ true,
446
+ );
447
+
448
+ read.rows = inserted.rows;
449
+ }
450
+ const row = read.rows[0]!;
451
+
452
+ invariant(
453
+ row[c.provider] === config.provider &&
454
+ row[c.issuer] === config.issuer &&
455
+ row[c.clientRegistrationId] === "" &&
456
+ nativeOrder(mapping, row[c.counter]) === 0,
457
+ );
458
+ const found = { id, row };
459
+
460
+ cache.set(id, found);
461
+
462
+ return found;
463
+ });
464
+
465
+ export const touchScope = Effect.fn("oauthConnected.touchScope")(function* (
466
+ mapping: Authority,
467
+ found: ScopeAnchor,
468
+ ) {
469
+ const owner = yield* CurrentOAuthTransaction;
470
+ const c = mapping.client;
471
+
472
+ yield* owner.update(
473
+ c.table,
474
+ { [c.clientKey]: found.id, [c.version]: found.row[c.version] },
475
+ { [c.version]: owner.marker },
476
+ );
477
+ found.row = { ...found.row, [c.version]: owner.marker };
478
+ });
479
+
480
+ export const readScope = Effect.fn("oauthConnected.readScope")(function* (
481
+ mapping: Authority,
482
+ provider: string,
483
+ issuer: string,
484
+ ) {
485
+ const owner = yield* CurrentOAuthTransaction;
486
+
487
+ const c = mapping.client,
488
+ id = scopeKey(provider, issuer),
489
+ cache = yield* anchors(scopeCache, c.table),
490
+ cached = cache.get(id);
491
+
492
+ if (cached !== undefined) return cached;
493
+
494
+ const read = yield* owner.read(c.table, equal(c.table, { [c.clientKey]: id }), { limit: 1 }),
495
+ row = read.rows[0];
496
+
497
+ invariant(
498
+ row !== undefined &&
499
+ row[c.provider] === provider &&
500
+ row[c.issuer] === issuer &&
501
+ row[c.clientRegistrationId] === "" &&
502
+ nativeOrder(mapping, row[c.counter]) === 0,
503
+ );
504
+ const found = { id, row };
505
+
506
+ cache.set(id, found);
507
+
508
+ return found;
509
+ });
510
+
511
+ export const lockClientById = Effect.fn("oauthConnected.lockClientById")(function* (
512
+ mapping: Authority,
513
+ id: string,
514
+ ) {
515
+ const owner = yield* CurrentOAuthTransaction;
516
+
517
+ const c = mapping.client,
518
+ cache = yield* anchors(clientCache, c.table),
519
+ cached = cache.get(id);
520
+
521
+ if (cached !== undefined) return cached;
522
+
523
+ const discovery = yield* owner.read(c.table, equal(c.table, { [c.clientKey]: id }), {
524
+ limit: 1,
525
+ observe: false,
526
+ lock: false,
527
+ }),
528
+ initial = discovery.rows[0];
529
+
530
+ invariant(initial !== undefined);
531
+ const scope = yield* readScope(mapping, initial[c.provider], initial[c.issuer]);
532
+
533
+ const read = yield* owner.read(c.table, equal(c.table, { [c.clientKey]: id }), { limit: 1 }),
534
+ row = read.rows[0];
535
+
536
+ invariant(
537
+ row !== undefined &&
538
+ row[c.clientRegistrationId] !== "" &&
539
+ id ===
540
+ key("effect-auth/oauth-connected-client/v1", [
541
+ row[c.provider],
542
+ row[c.issuer],
543
+ row[c.clientRegistrationId],
544
+ ]) &&
545
+ scope.id === scopeKey(row[c.provider], row[c.issuer]),
546
+ );
547
+ const found = { id, row, counter: nativeOrder(mapping, row[c.counter]), scope };
548
+
549
+ cache.set(id, found);
550
+
551
+ return found;
552
+ });
553
+
554
+ export const heldScope = (mapping: Authority, provider: string, issuer: string) =>
555
+ Effect.map(anchors(scopeCache, mapping.client.table), (cache) =>
556
+ cache.get(scopeKey(provider, issuer)),
557
+ );
558
+
559
+ /** Cleanup obtains every scope then every client before entering any tuple. */
560
+ export const prelockClients = Effect.fn("oauthConnected.prelockClients")(function* (
561
+ mapping: Authority,
562
+ ids: ReadonlyArray<string>,
563
+ releaseScopes: ReadonlyArray<{ readonly provider: string; readonly issuer: string }> = [],
564
+ ) {
565
+ const owner = yield* CurrentOAuthTransaction;
566
+
567
+ const unique = [...new Set(ids)].sort(),
568
+ c = mapping.client;
569
+
570
+ invariant(unique.length <= 4000);
571
+ const scopes = new Map<string, { provider: string; issuer: string; required: boolean }>();
572
+
573
+ for (const value of releaseScopes)
574
+ scopes.set(scopeKey(value.provider, value.issuer), { ...value, required: false });
575
+ for (const id of unique) {
576
+ const read = yield* owner.read(c.table, equal(c.table, { [c.clientKey]: id }), {
577
+ limit: 1,
578
+ lock: false,
579
+ observe: false,
580
+ }),
581
+ row = read.rows[0];
582
+
583
+ invariant(row !== undefined);
584
+ scopes.set(scopeKey(row[c.provider], row[c.issuer]), {
585
+ provider: row[c.provider],
586
+ issuer: row[c.issuer],
587
+ required: true,
588
+ });
589
+ }
590
+ for (const [id, { provider, issuer, required }] of [...scopes].sort(([a], [b]) =>
591
+ a < b ? -1 : a > b ? 1 : 0,
592
+ )) {
593
+ if (required) yield* readScope(mapping, provider, issuer);
594
+ else {
595
+ const read = yield* owner.read(c.table, equal(c.table, { [c.clientKey]: id }), { limit: 1 }),
596
+ row = read.rows[0];
597
+
598
+ if (row !== undefined) {
599
+ invariant(
600
+ row[c.provider] === provider &&
601
+ row[c.issuer] === issuer &&
602
+ row[c.clientRegistrationId] === "" &&
603
+ nativeOrder(mapping, row[c.counter]) === 0,
604
+ );
605
+ (yield* anchors(scopeCache, c.table)).set(id, { id, row });
606
+ }
607
+ }
608
+ }
609
+ for (const id of unique) yield* lockClientById(mapping, id);
610
+ });
611
+
612
+ export const client = Effect.fn("oauthConnected.client")(function* (
613
+ mapping: Authority,
614
+ config: M.OAuthConnectedConfiguration,
615
+ create: boolean,
616
+ ) {
617
+ const owner = yield* CurrentOAuthTransaction;
618
+
619
+ const c = mapping.client,
620
+ id = clientKey(config),
621
+ where = { [c.clientKey]: id };
622
+
623
+ const heldScope = yield* scope(mapping, config, create);
624
+
625
+ if (heldScope === undefined) return undefined;
626
+
627
+ const cache = yield* anchors(clientCache, c.table),
628
+ cached = cache.get(id);
629
+
630
+ if (cached !== undefined) return cached;
631
+ let read = yield* owner.read(c.table, equal(c.table, where), { limit: 1 });
632
+
633
+ if (read.rows.length === 0) {
634
+ const related = [
635
+ mapping.flow,
636
+ mapping.grant,
637
+ mapping.cohort,
638
+ ...(jobTable(mapping) === undefined ? [] : [jobTable(mapping)!]),
639
+ ];
640
+
641
+ invariant(
642
+ yield* owner.check(
643
+ both(
644
+ ...related.map(
645
+ (v) =>
646
+ sql`not exists(select 1 from ${v.table} where ${eq(col(v.table, v.clientKey), id)})`,
647
+ ),
648
+ ),
649
+ ),
650
+ );
651
+ }
652
+ if (read.rows.length === 0 && create) {
653
+ const inserted = yield* owner.insert(
654
+ c.table,
655
+ {
656
+ ...(invariant(c.encodeInsert !== undefined),
657
+ c.encodeInsert(snapshotOAuthSync(M.OAuthConnectedConfiguration, config))),
658
+ ...where,
659
+ [c.provider]: config.provider,
660
+ [c.issuer]: config.issuer,
661
+ [c.clientRegistrationId]: config.profile.clientRegistrationId,
662
+ [c.counter]: mapping.order.encode(0),
663
+ [c.version]: owner.marker,
664
+ },
665
+ where,
666
+ true,
667
+ );
668
+
669
+ read.rows = inserted.rows;
670
+ }
671
+ const row = read.rows[0];
672
+
673
+ if (row === undefined) return undefined;
674
+ invariant(
675
+ row[c.provider] === config.provider &&
676
+ row[c.issuer] === config.issuer &&
677
+ row[c.clientRegistrationId] === config.profile.clientRegistrationId,
678
+ );
679
+ const found = { id, row, counter: nativeOrder(mapping, row[c.counter]), scope: heldScope };
680
+
681
+ cache.set(id, found);
682
+
683
+ return found;
684
+ });
685
+
686
+ export const nextOrder = Effect.fn("oauthConnected.nextOrder")(function* (
687
+ mapping: Authority,
688
+ found: NonNullable<Effect.Success<ReturnType<typeof client>>>,
689
+ ) {
690
+ const owner = yield* CurrentOAuthTransaction;
691
+
692
+ const next = orderParser(found.counter + 1),
693
+ c = mapping.client;
694
+
695
+ yield* owner.update(
696
+ c.table,
697
+ { [c.clientKey]: found.id, [c.counter]: mapping.order.encode(found.counter) },
698
+ { [c.counter]: mapping.order.encode(next), [c.version]: owner.marker },
699
+ );
700
+ found.counter = next;
701
+
702
+ return next;
703
+ });
704
+
705
+ export const cohort = Effect.fn("oauthConnected.cohort")(function* (
706
+ mapping: Authority,
707
+ clientId: string,
708
+ identityId: string,
709
+ create: boolean,
710
+ ) {
711
+ const owner = yield* CurrentOAuthTransaction;
712
+
713
+ const c = mapping.cohort,
714
+ id = cohortKey(clientId, identityId),
715
+ where = { [c.cohortKey]: id };
716
+
717
+ let read = yield* owner.read(c.table, equal(c.table, where), { limit: 1 });
718
+
719
+ if (read.rows.length === 0) {
720
+ const related = [
721
+ mapping.flow,
722
+ mapping.grant,
723
+ ...(jobTable(mapping) === undefined ? [] : [jobTable(mapping)!]),
724
+ ];
725
+
726
+ invariant(
727
+ yield* owner.check(
728
+ both(
729
+ ...related.map(
730
+ (v) =>
731
+ sql`not exists(select 1 from ${v.table} where ${eq(col(v.table, v.cohortKey), id)})`,
732
+ ),
733
+ ),
734
+ ),
735
+ );
736
+ }
737
+ if (read.rows.length === 0 && create) {
738
+ const inserted = yield* owner.insert(
739
+ c.table,
740
+ {
741
+ ...(invariant(c.encodeInsert !== undefined),
742
+ c.encodeInsert({ clientKey: clientId, identityKey: identityId })),
743
+ ...where,
744
+ [c.clientKey]: clientId,
745
+ [c.identityKey]: identityId,
746
+ [c.generation]: initialGeneration(id),
747
+ [c.cutoff]: mapping.order.encode(0),
748
+ [c.state]: "Open",
749
+ [c.version]: owner.marker,
750
+ },
751
+ where,
752
+ true,
753
+ );
754
+
755
+ read.rows = inserted.rows;
756
+ }
757
+ const row = read.rows[0];
758
+
759
+ if (row === undefined)
760
+ return { id, row: undefined, generation: initialGeneration(id), cutoff: 0, blocked: false };
761
+ invariant(
762
+ row[c.clientKey] === clientId &&
763
+ row[c.identityKey] === identityId &&
764
+ ["Open", "Blocked"].includes(row[c.state]),
765
+ );
766
+
767
+ const generation = snapshotOAuthSync(
768
+ M.OAuthConnectedTarget.fields.cohortGeneration,
769
+ row[c.generation],
770
+ );
771
+
772
+ return {
773
+ id,
774
+ row,
775
+ generation,
776
+ cutoff: nativeOrder(mapping, row[c.cutoff]),
777
+ blocked: row[c.state] === "Blocked",
778
+ };
779
+ });
780
+
781
+ export const readGrant = Effect.fn("oauthConnected.readGrant")(function* (
782
+ mapping: Authority,
783
+ moduleId: string,
784
+ grantId: string,
785
+ lock = true,
786
+ ) {
787
+ const owner = yield* CurrentOAuthTransaction;
788
+ const g = mapping.grant;
789
+
790
+ const read = yield* owner.read(
791
+ g.table,
792
+ equal(g.table, { [g.moduleId]: moduleId, [g.grantId]: grantId }),
793
+ { limit: 1, lock, admissionOnly: lock, observe: false },
794
+ );
795
+
796
+ const row = read.rows[0];
797
+
798
+ if (row === undefined) return undefined;
799
+ if (lock) owner.observations.push(read);
800
+ const context = tokenContextStorage.decode(row[g.context]);
801
+ const native = yield* mapping.subjectId.toNative(context.subjectId);
802
+
803
+ invariant(
804
+ context.moduleId === moduleId &&
805
+ context.grantId === grantId &&
806
+ mapping.subjectId.equals(native, row[g.subjectId]) &&
807
+ row[g.identityKey] === oauthIdentityKey(context.identity) &&
808
+ row[g.clientKey] === clientKey(context.configuration) &&
809
+ row[g.cohortKey] === cohortKey(row[g.clientKey], row[g.identityKey]) &&
810
+ row[g.profileKey] === context.configuration.profile.key &&
811
+ row[g.grantVersion] === context.grantVersion &&
812
+ row[g.tokenVersion] === context.tokenVersion &&
813
+ row[g.cohortGeneration] === context.cohortGeneration,
814
+ );
815
+
816
+ return {
817
+ row,
818
+ context,
819
+ native,
820
+ sealed: row[g.sealed] === null ? undefined : sealedStorage.decode(row[g.sealed]),
821
+ };
822
+ });
823
+
824
+ export const unknownWork = (mapping: Authority, clientId: string | SQL, cutoff?: number) => {
825
+ const f = mapping.flow;
826
+
827
+ return both(
828
+ eq(col(f.table, f.clientKey), clientId),
829
+ eq(col(f.table, f.work), "Unresolved"),
830
+ isNull(col(f.table, f.cohortKey)),
831
+ cutoff === undefined
832
+ ? undefined
833
+ : sql`${col(f.table, f.claimOrder)} <= ${mapping.order.encode(cutoff)}`,
834
+ );
835
+ };
836
+
837
+ export const cohortWork = (mapping: Authority, id: string | SQL) => {
838
+ const f = mapping.flow,
839
+ g = mapping.grant;
840
+
841
+ return both(
842
+ sql`not exists(select 1 from ${f.table} where ${and(eq(col(f.table, f.cohortKey), id), eq(col(f.table, f.work), "Unresolved"))})`,
843
+ sql`not exists(select 1 from ${g.table} where ${and(eq(col(g.table, g.cohortKey), id), eq(col(g.table, g.refreshWork), "Unresolved"))})`,
844
+ );
845
+ };