feat(auth): password hashing, TOTP and recovery codes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Lucas RayanandClaude Opus 5.5 committed 2026-11-04 19:10:26 -03:00
1 parent fbaeeca60c
commit deea3e245b
5 files changed
+10887 -2

No files matched your search

Generated
+518 -2
View File
@@ -24,6 +24,7 @@ name = "acaua-core"
version = "0.2.0" version = "0.2.0"
dependencies = [ dependencies = [
"aes-gcm", "aes-gcm",
"argon2",
"base64 0.22.1", "base64 0.22.1",
"chrono", "chrono",
"duckdb", "duckdb",
@@ -34,6 +35,7 @@ dependencies = [
"tempfile", "tempfile",
"thiserror", "thiserror",
"time", "time",
"totp-rs",
] ]
[[package]] [[package]]
@@ -159,6 +161,18 @@ dependencies = [
"windows-sys 0.61.2", "windows-sys 0.61.2",
] ]
[[package]]
name = "argon2"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "134c52ddac6d63c576bef8168db10c83c49c26444ecbc68060fef078925a901c"
dependencies = [
"base64ct",
"blake2",
"cpufeatures 0.3.1",
"password-hash",
]
[[package]] [[package]]
name = "arrow" name = "arrow"
version = "58.4.1" version = "58.4.1"
@@ -407,6 +421,12 @@ dependencies = [
"tracing", "tracing",
] ]
[[package]]
name = "base32"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "022dfe9eb35f19ebbcb51e0b40a5ab759f46ad60cadf7297e0bd085afb50e076"
[[package]] [[package]]
name = "base64" name = "base64"
version = "0.22.1" version = "0.22.1"
@@ -419,12 +439,27 @@ version = "0.23.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac07cdecf99051d9a5238b80f35af32cdeba5b336e55d957b318b50137e18da5" checksum = "ac07cdecf99051d9a5238b80f35af32cdeba5b336e55d957b318b50137e18da5"
[[package]]
name = "base64ct"
version = "1.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2af50177e190e07a26ab74f8b1efbfe2ef87da2116221318cb1c2e82baf7de06"
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "2.13.2" version = "2.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06"
[[package]]
name = "blake2"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b5d4d889834ee8ecfc0f8426ad30faf7cdcb10f741a8e6d7224d95325479f6f"
dependencies = [
"digest 0.11.3",
]
[[package]] [[package]]
name = "block-buffer" name = "block-buffer"
version = "0.10.4" version = "0.10.4"
@@ -449,6 +484,18 @@ version = "3.20.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649"
[[package]]
name = "bytemuck"
version = "1.25.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95832e849adfb21180ccb6826a99da14e5d266ae5c2e668e1602cf234f153797"
[[package]]
name = "byteorder-lite"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f1fe948ff07f4bd06c30984e69f5b4899c516a3ef74f34df92a2df2ab535495"
[[package]] [[package]]
name = "bytes" name = "bytes"
version = "1.12.1" version = "1.12.1"
@@ -483,6 +530,17 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f079e83a288787bcd14a6aea84cee5c87a67c5a3e660c30f557a3d24761b3527" checksum = "f079e83a288787bcd14a6aea84cee5c87a67c5a3e660c30f557a3d24761b3527"
[[package]]
name = "chacha20"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06"
dependencies = [
"cfg-if",
"cpufeatures 0.3.1",
"rand_core 0.10.1",
]
[[package]] [[package]]
name = "chrono" name = "chrono"
version = "0.4.45" version = "0.4.45"
@@ -544,6 +602,12 @@ version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c133bc6a41be0d194c306b5506d15e6feeea7b1d6604bd3f8310dfb2ca96486" checksum = "1c133bc6a41be0d194c306b5506d15e6feeea7b1d6604bd3f8310dfb2ca96486"
[[package]]
name = "cmov"
version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c9ea0ac24bc397ab3c98583a3c9ba74fa56b09a4449bbe172b9b1ddb016027a"
[[package]] [[package]]
name = "colorchoice" name = "colorchoice"
version = "1.0.5" version = "1.0.5"
@@ -604,6 +668,12 @@ dependencies = [
"tiny-keccak", "tiny-keccak",
] ]
[[package]]
name = "constant_time_eq"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d52eff69cd5e647efe296129160853a42795992097e8af39800e1060caeea9b"
[[package]] [[package]]
name = "core-foundation-sys" name = "core-foundation-sys"
version = "0.8.7" version = "0.8.7"
@@ -672,7 +742,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a"
dependencies = [ dependencies = [
"generic-array", "generic-array",
"rand_core", "rand_core 0.6.4",
"typenum", "typenum",
] ]
@@ -694,6 +764,15 @@ dependencies = [
"cipher", "cipher",
] ]
[[package]]
name = "ctutils"
version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "261af4dc7a06ea8a527a8717d8cbba3417530d6ea14e1edb3de6b67806be474f"
dependencies = [
"cmov",
]
[[package]] [[package]]
name = "deranged" name = "deranged"
version = "0.5.9" version = "0.5.9"
@@ -719,6 +798,18 @@ dependencies = [
"block-buffer 0.12.1", "block-buffer 0.12.1",
"const-oid", "const-oid",
"crypto-common 0.2.2", "crypto-common 0.2.2",
"ctutils",
]
[[package]]
name = "displaydoc"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c6232dd377dcc64799954cbd3a9bb882e9cdc1308ccd87b1c098f1fb2eaf82a8"
dependencies = [
"proc-macro2",
"quote",
"syn 3.0.9",
] ]
[[package]] [[package]]
@@ -772,6 +863,15 @@ version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da7c62ceae207dd37ea5b845da6a0696c799f85e97da1ab5b7910be3c1c80223" checksum = "da7c62ceae207dd37ea5b845da6a0696c799f85e97da1ab5b7910be3c1c80223"
[[package]]
name = "fdeflate"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e6853b52649d4ac5c0bd02320cddc5ba956bdb407c4b75a2c6b75bf51500f8c"
dependencies = [
"simd-adler32",
]
[[package]] [[package]]
name = "filetime" name = "filetime"
version = "0.2.29" version = "0.2.29"
@@ -795,7 +895,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e634e2e0ebac1ee034020da1ca582e17ffe4e0f5e985823721e168928136dcb" checksum = "6e634e2e0ebac1ee034020da1ca582e17ffe4e0f5e985823721e168928136dcb"
dependencies = [ dependencies = [
"crc32fast", "crc32fast",
"miniz_oxide", "miniz_oxide 0.9.1",
"zlib-rs", "zlib-rs",
] ]
@@ -897,6 +997,7 @@ dependencies = [
"js-sys", "js-sys",
"libc", "libc",
"r-efi 6.0.0", "r-efi 6.0.0",
"rand_core 0.10.1",
"wasm-bindgen", "wasm-bindgen",
] ]
@@ -952,6 +1053,15 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "hmac"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6303bc9732ae41b04cb554b844a762b4115a61bfaa81e3e83050991eeb56863f"
dependencies = [
"digest 0.11.3",
]
[[package]] [[package]]
name = "http" name = "http"
version = "1.5.0" version = "1.5.0"
@@ -1064,6 +1174,123 @@ dependencies = [
"cc", "cc",
] ]
[[package]]
name = "icu_collections"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe090a42a0d3140fed8c63263ed1fecc02422cb2f4bfe55cd8de2de1b8e105dd"
dependencies = [
"displaydoc",
"potential_utf",
"utf8_iter",
"yoke",
"zerofrom",
"zerovec",
]
[[package]]
name = "icu_locale_core"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25d29b57c17c6528d0dc4c2141cfa2d08200465acb537a3909c8b9b8a4a984c0"
dependencies = [
"displaydoc",
"litemap",
"tinystr",
"writeable",
"zerovec",
]
[[package]]
name = "icu_normalizer"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "404a3b8c486f205c87fe7f23ab6894d84a4ad76d6e4671ae1981ba7def87c599"
dependencies = [
"icu_collections",
"icu_normalizer_data",
"icu_properties",
"icu_provider",
"smallvec",
"zerovec",
]
[[package]]
name = "icu_normalizer_data"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20f0088bfb7441f7a37fa0c9e20cb1e9cba1cac6b9ce8cd2a6dc44294f8e8cbc"
[[package]]
name = "icu_properties"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2ea83d991c56158f7cf898bf86436c1dfe7c07ee82fe88951f2695c9f37a379f"
dependencies = [
"displaydoc",
"icu_collections",
"icu_locale_core",
"icu_properties_data",
"icu_provider",
"zerotrie",
"zerovec",
]
[[package]]
name = "icu_properties_data"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b98f29af82d02b163fe50161388350e743bb3107cc8e8c2f7314cb505faddfcf"
[[package]]
name = "icu_provider"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "413099315636c588ee8f011190b329b971a09a8fae9d157e01dcae8379468514"
dependencies = [
"displaydoc",
"icu_locale_core",
"writeable",
"yoke",
"zerofrom",
"zerotrie",
"zerovec",
]
[[package]]
name = "idna"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b0875f23caa03898994f6ddc501886a45c7d3d62d04d2d90788d47be1b1e4de"
dependencies = [
"idna_adapter",
"smallvec",
"utf8_iter",
]
[[package]]
name = "idna_adapter"
version = "1.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb68373c0d6620ef8105e855e7745e18b0d00d3bdb07fb532e434244cdb9a714"
dependencies = [
"icu_normalizer",
"icu_properties",
]
[[package]]
name = "image"
version = "0.25.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85ab80394333c02fe689eaf900ab500fbd0c2213da414687ebf995a65d5a6104"
dependencies = [
"bytemuck",
"byteorder-lite",
"moxcms",
"num-traits",
"png",
]
[[package]] [[package]]
name = "indexmap" name = "indexmap"
version = "2.14.2" version = "2.14.2"
@@ -1209,6 +1436,12 @@ version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53" checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53"
[[package]]
name = "litemap"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47d9d19d1d6efa0109d2f65ff4c85cddd50bd572e5a00127ab10987290bcefae"
[[package]] [[package]]
name = "lock_api" name = "lock_api"
version = "0.4.14" version = "0.4.14"
@@ -1252,6 +1485,16 @@ dependencies = [
"unicase", "unicase",
] ]
[[package]]
name = "miniz_oxide"
version = "0.8.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316"
dependencies = [
"adler2",
"simd-adler32",
]
[[package]] [[package]]
name = "miniz_oxide" name = "miniz_oxide"
version = "0.9.1" version = "0.9.1"
@@ -1273,6 +1516,16 @@ dependencies = [
"windows-sys 0.61.2", "windows-sys 0.61.2",
] ]
[[package]]
name = "moxcms"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb85c154ba489f01b25c0d36ae69a87e4a1c73a72631fc6c0eb6dde34a73e44b"
dependencies = [
"num-traits",
"pxfm",
]
[[package]] [[package]]
name = "nix" name = "nix"
version = "0.31.3" version = "0.31.3"
@@ -1379,12 +1632,33 @@ dependencies = [
"windows-link", "windows-link",
] ]
[[package]]
name = "password-hash"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aab41826031698d6ffcd9cff78ef56ef998e39dc7e5067cdfebe373842d4723b"
dependencies = [
"getrandom 0.4.3",
"phc",
]
[[package]] [[package]]
name = "percent-encoding" name = "percent-encoding"
version = "2.3.2" version = "2.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220"
[[package]]
name = "phc"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44dc769b75f93afdddd8c7fa12d685292ddeff1e66f7f0f3a234cf1818afe892"
dependencies = [
"base64ct",
"ctutils",
"getrandom 0.4.3",
]
[[package]] [[package]]
name = "pin-project-lite" name = "pin-project-lite"
version = "0.2.17" version = "0.2.17"
@@ -1397,6 +1671,19 @@ version = "0.3.34"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548" checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548"
[[package]]
name = "png"
version = "0.18.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60769b8b31b2a9f263dae2776c37b1b28ae246943cf719eb6946a1db05128a61"
dependencies = [
"bitflags",
"crc32fast",
"fdeflate",
"flate2",
"miniz_oxide 0.8.9",
]
[[package]] [[package]]
name = "polyval" name = "polyval"
version = "0.6.2" version = "0.6.2"
@@ -1409,6 +1696,15 @@ dependencies = [
"universal-hash", "universal-hash",
] ]
[[package]]
name = "potential_utf"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d83eb9bc6d8e5cf568e7a1101d60ee05e81ed50ea106026f3d18deeb046d7661"
dependencies = [
"zerovec",
]
[[package]] [[package]]
name = "powerfmt" name = "powerfmt"
version = "0.2.1" version = "0.2.1"
@@ -1424,6 +1720,29 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "pxfm"
version = "0.1.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d55d956fa96f5ec02be2e13af0e20391a5aa83d6a074e3ad368959d0fab299ea"
[[package]]
name = "qrcodegen"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4339fc7a1021c9c1621d87f5e3505f2805c8c105420ba2f2a4df86814590c142"
[[package]]
name = "qrcodegen-image"
version = "1.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e3dd60f5b603f72c307455fc52deec52ada1ba53c7580918bb2a8e3247d4fe7"
dependencies = [
"base64 0.22.1",
"image",
"qrcodegen",
]
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.47" version = "1.0.47"
@@ -1445,6 +1764,17 @@ version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf"
[[package]]
name = "rand"
version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "65c9fb96cbc91e3478eaae79a69fcd3f1ae4ad052e471fe6732fff548984b4af"
dependencies = [
"chacha20",
"getrandom 0.4.3",
"rand_core 0.10.1",
]
[[package]] [[package]]
name = "rand_core" name = "rand_core"
version = "0.6.4" version = "0.6.4"
@@ -1454,6 +1784,12 @@ dependencies = [
"getrandom 0.2.17", "getrandom 0.2.17",
] ]
[[package]]
name = "rand_core"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69"
[[package]] [[package]]
name = "redox_syscall" name = "redox_syscall"
version = "0.5.18" version = "0.5.18"
@@ -1695,6 +2031,17 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "sha1"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aacc4cc499359472b4abe1bf11d0b12e688af9a805fa5e3016f9a386dc2d0214"
dependencies = [
"cfg-if",
"cpufeatures 0.3.1",
"digest 0.11.3",
]
[[package]] [[package]]
name = "sha2" name = "sha2"
version = "0.10.9" version = "0.10.9"
@@ -1770,6 +2117,12 @@ dependencies = [
"windows-sys 0.61.2", "windows-sys 0.61.2",
] ]
[[package]]
name = "stable_deref_trait"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596"
[[package]] [[package]]
name = "strsim" name = "strsim"
version = "0.11.1" version = "0.11.1"
@@ -1831,6 +2184,17 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263"
[[package]]
name = "synstructure"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "901704edd0dfe137f1987838ee4f259e4e063c31371bdb423f7ae38ec6f77f02"
dependencies = [
"proc-macro2",
"quote",
"syn 3.0.9",
]
[[package]] [[package]]
name = "tar" name = "tar"
version = "0.4.46" version = "0.4.46"
@@ -1923,6 +2287,16 @@ dependencies = [
"crunchy", "crunchy",
] ]
[[package]]
name = "tinystr"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "abc840c238154d58f9e86b2792261edfde565a17696e1cda1a449de27979801a"
dependencies = [
"displaydoc",
"zerovec",
]
[[package]] [[package]]
name = "tokio" name = "tokio"
version = "1.54.1" version = "1.54.1"
@@ -1962,6 +2336,25 @@ dependencies = [
"tokio", "tokio",
] ]
[[package]]
name = "totp-rs"
version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49d0f9b7aa5def86d3a8d8ba926fe8c1990924dd715d2b5fc54a4aa3788c11c1"
dependencies = [
"base32",
"constant_time_eq",
"hmac",
"percent-encoding",
"qrcodegen-image",
"rand",
"serde",
"sha1",
"sha2 0.11.1",
"url",
"zeroize",
]
[[package]] [[package]]
name = "tower" name = "tower"
version = "0.5.3" version = "0.5.3"
@@ -2148,18 +2541,44 @@ dependencies = [
"log", "log",
] ]
[[package]]
name = "url"
version = "2.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff67a8a4397373c3ef660812acab3268222035010ab8680ec4215f38ba3d0eed"
dependencies = [
"form_urlencoded",
"idna",
"percent-encoding",
]
[[package]] [[package]]
name = "utf8-zero" name = "utf8-zero"
version = "0.8.1" version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8c0a043c9540bae7c578c88f91dda8bd82e59ae27c21baca69c8b191aaf5a6e" checksum = "b8c0a043c9540bae7c578c88f91dda8bd82e59ae27c21baca69c8b191aaf5a6e"
[[package]]
name = "utf8_iter"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af3bd9d3e571c26fcce9d033b87ea5ab061b8d2ffe9b7a5675d810d68f03edf0"
dependencies = [
"utf_types",
]
[[package]] [[package]]
name = "utf8parse" name = "utf8parse"
version = "0.2.2" version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "utf_types"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a3b425286980a367278453f3b6d28cdcb134b574e5a35824a6ff5eebad50bb6"
[[package]] [[package]]
name = "valuable" name = "valuable"
version = "0.1.1" version = "0.1.1"
@@ -2434,6 +2853,12 @@ version = "0.57.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e" checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e"
[[package]]
name = "writeable"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ad82d2a33cdc9674dc7465672f271e096168fcdbe0f799d9e6db8c5892679dc"
[[package]] [[package]]
name = "xattr" name = "xattr"
version = "1.7.0" version = "1.7.0"
@@ -2444,6 +2869,29 @@ dependencies = [
"rustix 1.1.6", "rustix 1.1.6",
] ]
[[package]]
name = "yoke"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "417ef3fcf6c300b7dbd25c85531a746931c02ad536f3cf4786ed1e88223340b3"
dependencies = [
"stable_deref_trait",
"yoke-derive",
"zerofrom",
]
[[package]]
name = "yoke-derive"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec8ebde2db3681e8c9980cc27822030e68752690ddfa9473e739aeb4dbde6d71"
dependencies = [
"proc-macro2",
"quote",
"syn 3.0.9",
"synstructure",
]
[[package]] [[package]]
name = "zerocopy" name = "zerocopy"
version = "0.8.63" version = "0.8.63"
@@ -2464,11 +2912,79 @@ dependencies = [
"syn 2.0.119", "syn 2.0.119",
] ]
[[package]]
name = "zerofrom"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ec05a11813ea801ff6d75110ad09cd0824ddba17dfe17128ea0d5f68e6c5272"
dependencies = [
"zerofrom-derive",
]
[[package]]
name = "zerofrom-derive"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f75b4683f6c7f45248d4d64056a24298c6281e0993356d7d1b4a1a962ef10d4a"
dependencies = [
"proc-macro2",
"quote",
"syn 3.0.9",
"synstructure",
]
[[package]] [[package]]
name = "zeroize" name = "zeroize"
version = "1.9.1" version = "1.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e13084392c5e4bc371903e2935a5eaeed24905a7511356b883835e18a78f6879" checksum = "e13084392c5e4bc371903e2935a5eaeed24905a7511356b883835e18a78f6879"
dependencies = [
"zeroize_derive",
]
[[package]]
name = "zeroize_derive"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c50655cbb0fe3fc43170059e702f1ce5e19b84cec58dc87b037a09935c2f328"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.119",
]
[[package]]
name = "zerotrie"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ea269c3bd32f0a32c321907a2ae912ba6f4649bb0fc764a15627e99a7095a3f"
dependencies = [
"displaydoc",
"yoke",
"zerofrom",
]
[[package]]
name = "zerovec"
version = "0.11.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f58a2799cfe9a1670c7ea8edbd52ef8edb93cb37a7f83bf31ba9e35d9dae6a40"
dependencies = [
"yoke",
"zerofrom",
"zerovec-derive",
]
[[package]]
name = "zerovec-derive"
version = "0.11.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3743ddbf47738d929cd1ae2e199d40d0162979dfe31aef8ae646d2bf00bf97a"
dependencies = [
"proc-macro2",
"quote",
"syn 3.0.9",
]
[[package]] [[package]]
name = "zip" name = "zip"
+2
View File
@@ -22,6 +22,8 @@ duckdb = "~1.10506.1"
aes-gcm = "0.10" aes-gcm = "0.10"
base64 = "0.22" base64 = "0.22"
fs2 = "0.4" fs2 = "0.4"
argon2 = "0.6.0"
totp-rs = { version = "6.0.0", features = ["qr", "otpauth"] }
[dev-dependencies] [dev-dependencies]
tempfile = "3" tempfile = "3"
File diff suppressed because it is too large. Load diff
+362
View File
@@ -0,0 +1,362 @@
//! Password hashing, TOTP and recovery codes. Crypto comes from audited
//! crates (`argon2`, `totp-rs`); nothing here is hand-rolled.
use super::secrets::{hash_token, random_token};
use crate::error::{AcauaError, Result};
use aes_gcm::aead::{rand_core::RngCore, OsRng};
use argon2::password_hash::{PasswordHasher, PasswordVerifier};
use argon2::{Algorithm, Argon2, Params, Version};
use std::collections::HashSet;
use std::str::FromStr;
use std::sync::OnceLock;
use totp_rs::{Algorithm as TotpAlgorithm, Builder as TotpBuilder, Totp};
/// Minimum password length, in characters.
pub const PASSWORD_MIN_CHARS: usize = 12;
/// Maximum password length, in bytes (bounds Argon2 work per request).
pub const PASSWORD_MAX_BYTES: usize = 256;
/// Argon2id cost: m=19456 KiB, t=2, p=1 (OWASP minimum profile).
const ARGON_M_KIB: u32 = 19_456;
const ARGON_T: u32 = 2;
const ARGON_P: u32 = 1;
const TOTP_SECRET_BYTES: usize = 20;
const TOTP_DIGITS: u8 = 6;
const TOTP_STEP_SECS: u64 = 30;
const TOTP_SKEW: u16 = 1;
const TOTP_ISSUER: &str = "Acauã";
const RECOVERY_CODE_COUNT: usize = 10;
const RECOVERY_CODE_BYTES: usize = 10; // 80 bits
/// SecLists 10k-most-common (MIT). Untrusted data: only a word list.
static COMMON_PASSWORDS_RAW: &str = include_str!("common-passwords.txt");
fn common_passwords() -> &'static HashSet<String> {
static SET: OnceLock<HashSet<String>> = OnceLock::new();
SET.get_or_init(|| {
COMMON_PASSWORDS_RAW
.lines()
.map(str::trim)
.filter(|l| !l.is_empty() && !l.starts_with('#'))
.map(str::to_lowercase)
.collect()
})
}
fn is_common(password: &str) -> bool {
let lower = password.to_lowercase();
let set = common_passwords();
if set.contains(&lower) {
return true;
}
// Common word plus a numeric suffix ("password1234") is no better.
let base = lower.trim_end_matches(|c: char| c.is_ascii_digit());
!base.is_empty() && base.len() != lower.len() && set.contains(base)
}
/// Rejects short, oversized and common passwords. No composition rules.
/// Error messages never echo the password.
pub fn validate_password(password: &str) -> Result<()> {
if password.len() > PASSWORD_MAX_BYTES {
return Err(AcauaError::InvalidInput(format!(
"password must be at most {PASSWORD_MAX_BYTES} bytes"
)));
}
if password.chars().count() < PASSWORD_MIN_CHARS {
return Err(AcauaError::InvalidInput(format!(
"password must be at least {PASSWORD_MIN_CHARS} characters"
)));
}
if is_common(password) {
return Err(AcauaError::InvalidInput(
"password is too common".to_string(),
));
}
Ok(())
}
fn argon2() -> Result<Argon2<'static>> {
let params = Params::new(ARGON_M_KIB, ARGON_T, ARGON_P, None).map_err(|_| hash_failure())?;
Ok(Argon2::new(Algorithm::Argon2id, Version::V0x13, params))
}
fn hash_failure() -> AcauaError {
AcauaError::CommandFailed {
cmd: "argon2".to_string(),
detail: "password hashing failed".to_string(),
}
}
/// Argon2id (m=19456 KiB, t=2, p=1) hash in PHC string format, random salt.
pub fn hash_password(password: &str) -> Result<String> {
if password.len() > PASSWORD_MAX_BYTES {
return Err(AcauaError::InvalidInput(format!(
"password must be at most {PASSWORD_MAX_BYTES} bytes"
)));
}
let hash = argon2()?
.hash_password(password.as_bytes())
.map_err(|_| hash_failure())?;
Ok(hash.to_string())
}
fn dummy_hash() -> Option<&'static str> {
static DUMMY: OnceLock<Option<String>> = OnceLock::new();
DUMMY
.get_or_init(|| hash_password(&random_token(24)).ok())
.as_deref()
}
/// Verifies a candidate. With `hash == None` (unknown user) a dummy hash is
/// still verified so timing does not reveal whether the user exists; the
/// result is then always `false`. A malformed stored hash also yields `false`.
pub fn verify_password(hash: Option<&str>, candidate: &str) -> bool {
// Oversized input is refused before any Argon2 work.
if candidate.len() > PASSWORD_MAX_BYTES {
return false;
}
let Ok(engine) = argon2() else { return false };
let (stored, real) = match hash {
Some(h) => (h, true),
None => match dummy_hash() {
Some(d) => (d, false),
None => return false,
},
};
let Ok(parsed) = argon2::password_hash::phc::PasswordHash::from_str(stored) else {
return false;
};
let ok = engine
.verify_password(candidate.as_bytes(), &parsed)
.is_ok();
ok && real
}
/// 160-bit random TOTP secret from the OS CSPRNG.
pub fn new_totp_secret() -> Vec<u8> {
let mut buf = vec![0u8; TOTP_SECRET_BYTES];
OsRng.fill_bytes(&mut buf);
buf
}
fn build_totp(secret: &[u8], account: &str) -> Result<Totp> {
TotpBuilder::new()
.with_algorithm(TotpAlgorithm::SHA1)
.with_digits(TOTP_DIGITS)
.with_skew(TOTP_SKEW)
.with_step_duration(TOTP_STEP_SECS)
.with_secret(secret.to_vec())
.with_account_name(account)
.with_issuer(Some(TOTP_ISSUER))
.build()
.map_err(|_| AcauaError::InvalidInput("invalid TOTP secret or account name".to_string()))
}
/// `otpauth://` URI and the QR code as base64 PNG.
pub fn totp_uri_and_qr(secret: &[u8], account: &str) -> Result<(String, String)> {
let totp = build_totp(secret, account)?;
let fail = |_| AcauaError::InvalidInput("could not encode TOTP enrolment".to_string());
let uri = totp.to_url().map_err(fail)?;
let qr = totp.to_qr_base64().map_err(fail)?;
Ok((uri, qr))
}
/// Checks a code at `unix` (SHA-1, 6 digits, 30 s, +-1 step). Returns the
/// accepted step, or `None` if invalid or not newer than `last_step`
/// (a step is never accepted twice).
pub fn verify_totp(secret: &[u8], code: &str, unix: u64, last_step: Option<u64>) -> Option<u64> {
let totp = build_totp(secret, "acaua").ok()?;
let step = totp.check(code.trim(), unix)?;
match last_step {
Some(last) if step <= last => None,
_ => Some(step),
}
}
/// Canonical form used for hashing: alphanumerics only, lowercase.
fn canonical_recovery(code: &str) -> String {
code.chars()
.filter(char::is_ascii_alphanumeric)
.map(|c| c.to_ascii_lowercase())
.collect()
}
/// Hash stored for a recovery code (case and separators ignored).
pub fn hash_recovery_code(code: &str) -> String {
hash_token(&canonical_recovery(code))
}
/// Ten single-use 80-bit codes: `(display texts, SHA-256 hashes)`. Codes are
/// 20 hex digits in groups of five; only the hashes should be persisted.
pub fn new_recovery_codes() -> (Vec<String>, Vec<String>) {
let mut seen = HashSet::new();
let mut texts = Vec::with_capacity(RECOVERY_CODE_COUNT);
let mut hashes = Vec::with_capacity(RECOVERY_CODE_COUNT);
while texts.len() < RECOVERY_CODE_COUNT {
let mut raw = [0u8; RECOVERY_CODE_BYTES];
OsRng.fill_bytes(&mut raw);
let hex: String = raw.iter().map(|b| format!("{b:02x}")).collect();
if !seen.insert(hex.clone()) {
continue;
}
let text = format!(
"{}-{}-{}-{}",
&hex[0..5],
&hex[5..10],
&hex[10..15],
&hex[15..20]
);
hashes.push(hash_recovery_code(&text));
texts.push(text);
}
(texts, hashes)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rejects_short_common_and_oversized_password() {
assert!(validate_password("abcdefghijk").is_err(), "11 chars");
assert!(validate_password("password1234").is_err(), "common");
assert!(
validate_password("PASSWORD1234").is_err(),
"common, case-insensitive"
);
assert!(validate_password(&"a1".repeat(129)).is_err(), "258 bytes");
assert!(validate_password(&"x".repeat(257)).is_err(), "257 bytes");
assert!(validate_password("correct horse battery staple").is_ok());
assert!(validate_password("ação de graças tardia").is_ok());
// 12 chars but more than 12 bytes: length counts characters.
assert!(validate_password("ããããããããããàã").is_ok());
}
#[test]
fn password_roundtrip_and_params() {
let h = hash_password("correct horse battery staple").unwrap();
assert!(h.starts_with("$argon2id$v=19$m=19456,t=2,p=1$"), "{h}");
assert!(verify_password(Some(&h), "correct horse battery staple"));
assert!(!verify_password(Some(&h), "correct horse battery stapl"));
assert!(!verify_password(Some("not a phc string"), "x"));
assert_ne!(
h,
hash_password("correct horse battery staple").unwrap(),
"salted"
);
}
#[test]
fn verify_password_handles_missing_user() {
assert!(!verify_password(None, "x"));
assert!(!verify_password(None, "correct horse battery staple"));
}
fn rfc_secret() -> &'static [u8] {
b"12345678901234567890"
}
#[test]
fn totp_rfc6238_vector() {
// RFC 6238 appendix B, T=59 -> 94287082 (8 digits) -> 287082 (6).
assert_eq!(verify_totp(rfc_secret(), "287082", 59, None), Some(1));
assert_eq!(verify_totp(rfc_secret(), "287083", 59, None), None);
assert_eq!(verify_totp(rfc_secret(), "28708", 59, None), None);
assert_eq!(verify_totp(rfc_secret(), "2870820", 59, None), None);
assert_eq!(verify_totp(rfc_secret(), "abcdef", 59, None), None);
}
#[test]
fn totp_window_and_replay() {
// "287082" is the code for step 1. Steps 0 and 2 are +-1 of it.
assert_eq!(
verify_totp(rfc_secret(), "287082", 0, None),
Some(1),
"-1 step"
);
assert_eq!(
verify_totp(rfc_secret(), "287082", 89, None),
Some(1),
"+1 step"
);
assert_eq!(
verify_totp(rfc_secret(), "287082", 30 * 3, None),
None,
"+2 steps"
);
assert_eq!(
verify_totp(rfc_secret(), "287082", 30 * 4, None),
None,
"+3 steps"
);
// Step 1 at unix 1000 would be -32 steps; use a larger base to test -2.
assert_eq!(verify_totp(rfc_secret(), "287082", 30 * 101, None), None);
assert_eq!(
verify_totp(rfc_secret(), "287082", 59, Some(1)),
None,
"replay"
);
assert_eq!(
verify_totp(rfc_secret(), "287082", 59, Some(2)),
None,
"older than last"
);
assert_eq!(verify_totp(rfc_secret(), "287082", 59, Some(0)), Some(1));
}
#[test]
fn totp_rejects_future_steps_beyond_tolerance() {
let totp = build_totp(rfc_secret(), "a").unwrap();
let code_step_5 = totp.generate(5 * 30).to_string();
assert_eq!(
verify_totp(rfc_secret(), &code_step_5, 4 * 30, None),
Some(5),
"+1 ahead"
);
assert_eq!(
verify_totp(rfc_secret(), &code_step_5, 3 * 30, None),
None,
"2 ahead"
);
}
#[test]
fn totp_uri_and_qr_shape() {
let secret = new_totp_secret();
assert_eq!(secret.len(), 20);
assert_ne!(secret, new_totp_secret());
let (uri, qr) = totp_uri_and_qr(&secret, "ana@example.org").unwrap();
assert!(uri.starts_with("otpauth://totp/"), "{uri}");
// SHA1 / 6 digits / 30 s are the otpauth defaults, so they are omitted.
assert!(uri.contains("secret=") && !uri.contains("algorithm=SHA256"));
assert!(uri.contains("issuer="), "{uri}");
use base64::{engine::general_purpose::STANDARD, Engine};
let png = STANDARD.decode(qr).unwrap();
assert_eq!(&png[..4], b"\x89PNG");
}
#[test]
fn recovery_codes_unique_and_hashed() {
let (codes, hashes) = new_recovery_codes();
assert_eq!(codes.len(), 10);
assert_eq!(hashes.len(), 10);
let uniq: std::collections::HashSet<_> = codes.iter().collect();
assert_eq!(uniq.len(), 10);
let uh: std::collections::HashSet<_> = hashes.iter().collect();
assert_eq!(uh.len(), 10);
for (c, h) in codes.iter().zip(&hashes) {
assert_ne!(c, h);
assert_eq!(h, &hash_recovery_code(c));
assert_eq!(h.len(), 64);
}
}
#[test]
fn recovery_hash_ignores_case_and_separators() {
let (codes, hashes) = new_recovery_codes();
let mangled = codes[0].replace('-', " ").to_uppercase();
assert_eq!(hash_recovery_code(&mangled), hashes[0]);
}
}
+1
View File
@@ -3,6 +3,7 @@
//! Layout: `<data_dir>/auth/{state.json,secret.key,audit.jsonl,.lock}`. //! Layout: `<data_dir>/auth/{state.json,secret.key,audit.jsonl,.lock}`.
pub mod audit; pub mod audit;
pub mod credentials;
pub mod secrets; pub mod secrets;
pub mod store; pub mod store;