im
Clone
https://git.tahinli.com/Tahinli/im.git
0 watching0 forksRustAGPL-3.0542.8 KiBb9f0726Tahinli27 days ago34 commits
| 1 | //! Password hashing with argon2id, and the password policy. |
| 2 | //! |
| 3 | //! Hashes are PHC strings with a random salt. Hashing and verifying take hundreds of milliseconds |
| 4 | //! by design, so the async wrappers move that work onto the blocking thread pool; a request task |
| 5 | //! must never run argon2 inline. |
| 6 | |
| 7 | use Argon2; |
| 8 | use PasswordHash; |
| 9 | use ; |
| 10 | use ; |
| 11 | |
| 12 | use crate; |
| 13 | |
| 14 | /// Shortest accepted password, in characters. Length is the main defence against guessing, so it |
| 15 | /// is set well above what people choose by habit. |
| 16 | pub const MIN_PASSWORD_CHARS: usize = 10; |
| 17 | |
| 18 | /// Longest accepted password, in characters. Bounds the work an attacker can force argon2 to do |
| 19 | /// per request. |
| 20 | pub const MAX_PASSWORD_CHARS: usize = 256; |
| 21 | |
| 22 | /// Most argon2 operations running at once. Each one takes tens of megabytes, so a burst of requests |
| 23 | /// queues here instead of starting one blocking thread and one hash per request. |
| 24 | const HASH_PERMITS: usize = 4; |
| 25 | |
| 26 | /// The gate of every async hash and verify. |
| 27 | static HASHING: Semaphore = const_new; |
| 28 | |
| 29 | /// True for a password longer than [`MAX_PASSWORD_CHARS`]. Such a password is never hashed or verified. |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | /// Shortest word of the name or email local part that a password may not contain, in characters. |
| 35 | /// Shorter words are too common to forbid: a two-letter word would reject many good passwords. |
| 36 | pub const MIN_FORBIDDEN_WORD_CHARS: usize = 4; |
| 37 | |
| 38 | /// Hashes `pw` with argon2id (default parameters) and a fresh random salt. Returns a PHC string. A |
| 39 | /// password over [`MAX_PASSWORD_CHARS`] is refused before any hashing. |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | /// Returns true only if `pw` matches the PHC string `hash`. A malformed hash gives false, never |
| 51 | /// a panic. A password over [`MAX_PASSWORD_CHARS`] gives false without any hashing. |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | /// Takes one of the [`HASH_PERMITS`] slots. Held until the blocking work ends. |
| 65 | async |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | /// [`hash_password`] on the blocking thread pool, behind the hashing gate. |
| 73 | pub async |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | /// [`verify_password`] on the blocking thread pool, behind the hashing gate. An over-long password is |
| 81 | /// answered without taking a slot. |
| 82 | pub async |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | /// Why a new password is refused by [`check_policy`]. The web layer maps each reason to a |
| 93 | /// translated message, so the reasons are kept apart from any text. |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | /// Checks a new password against the policy. |
| 105 | /// |
| 106 | /// Rules: 10 to 256 characters; it must not contain the email local part (when that is at least |
| 107 | /// [`MIN_FORBIDDEN_WORD_CHARS`] long) or any word of the name that is at least that long, compared |
| 108 | /// case-insensitively. The rule that the password differs from the current one needs the stored |
| 109 | /// hash and is checked by the caller. |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 |