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