| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | use std::{ |
| 10 | fmt, |
| 11 | time::{Duration, SystemTime}, |
| 12 | }; |
| 13 | |
| 14 | use sha2::{Digest, Sha256}; |
| 15 | use subtle::ConstantTimeEq; |
| 16 | |
| 17 | use super::{ResetId, UserId}; |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | #[derive(Clone)] |
| 29 | pub struct ResetSecret(String); |
| 30 | |
| 31 | impl ResetSecret { |
| 32 | |
| 33 | |
| 34 | const BYTES: usize = 32; |
| 35 | |
| 36 | |
| 37 | pub fn generate() -> Self { |
| 38 | use rand::Rng; |
| 39 | |
| 40 | let mut bytes = [0u8; Self::BYTES]; |
| 41 | rand::rng().fill_bytes(&mut bytes); |
| 42 | |
| 43 | Self(bytes.iter().map(|byte| format!("{byte:02x}")).collect()) |
| 44 | } |
| 45 | |
| 46 | |
| 47 | pub fn from_presented(value: impl Into<String>) -> Self { |
| 48 | Self(value.into()) |
| 49 | } |
| 50 | |
| 51 | |
| 52 | pub fn hash(&self) -> ResetHash { |
| 53 | ResetHash::of(&self.0) |
| 54 | } |
| 55 | |
| 56 | |
| 57 | pub fn reveal(&self) -> &str { |
| 58 | &self.0 |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | |
| 63 | |
| 64 | impl fmt::Debug for ResetSecret { |
| 65 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 66 | f.write_str("ResetSecret(redacted)") |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | #[derive(Clone, PartialEq, Eq, Hash)] |
| 76 | pub struct ResetHash(String); |
| 77 | |
| 78 | impl ResetHash { |
| 79 | |
| 80 | pub fn of(plaintext: &str) -> Self { |
| 81 | let digest = Sha256::digest(plaintext.as_bytes()); |
| 82 | |
| 83 | Self(digest.iter().map(|byte| format!("{byte:02x}")).collect()) |
| 84 | } |
| 85 | |
| 86 | |
| 87 | pub fn from_trusted(value: impl Into<String>) -> Self { |
| 88 | Self(value.into()) |
| 89 | } |
| 90 | |
| 91 | pub fn as_str(&self) -> &str { |
| 92 | &self.0 |
| 93 | } |
| 94 | |
| 95 | |
| 96 | pub fn matches(&self, other: &Self) -> bool { |
| 97 | self.0.as_bytes().ct_eq(other.0.as_bytes()).into() |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | |
| 102 | impl fmt::Debug for ResetHash { |
| 103 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 104 | f.write_str("ResetHash(redacted)") |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 114 | pub struct PasswordReset { |
| 115 | pub id: ResetId, |
| 116 | pub user_id: UserId, |
| 117 | pub token_hash: ResetHash, |
| 118 | pub created_at: SystemTime, |
| 119 | pub expires_at: SystemTime, |
| 120 | |
| 121 | pub used_at: Option<SystemTime>, |
| 122 | } |
| 123 | |
| 124 | impl PasswordReset { |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | pub const LIFETIME: Duration = Duration::from_secs(30 * 60); |
| 131 | |
| 132 | |
| 133 | pub fn new(id: ResetId, user_id: UserId, secret: &ResetSecret, now: SystemTime) -> Self { |
| 134 | Self { |
| 135 | id, |
| 136 | user_id, |
| 137 | token_hash: secret.hash(), |
| 138 | created_at: now, |
| 139 | expires_at: now + Self::LIFETIME, |
| 140 | used_at: None, |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | |
| 145 | pub fn from_trusted( |
| 146 | id: ResetId, |
| 147 | user_id: UserId, |
| 148 | token_hash: ResetHash, |
| 149 | created_at: SystemTime, |
| 150 | expires_at: SystemTime, |
| 151 | used_at: Option<SystemTime>, |
| 152 | ) -> Self { |
| 153 | Self { |
| 154 | id, |
| 155 | user_id, |
| 156 | token_hash, |
| 157 | created_at, |
| 158 | expires_at, |
| 159 | used_at, |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | pub fn is_live(&self, now: SystemTime) -> bool { |
| 168 | self.used_at.is_none() && now < self.expires_at |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | #[cfg(test)] |
| 173 | mod tests { |
| 174 | use super::*; |
| 175 | |
| 176 | fn at(seconds: u64) -> SystemTime { |
| 177 | SystemTime::UNIX_EPOCH + Duration::from_secs(seconds) |
| 178 | } |
| 179 | |
| 180 | fn reset(now: SystemTime) -> (PasswordReset, ResetSecret) { |
| 181 | let secret = ResetSecret::generate(); |
| 182 | let record = PasswordReset::new(ResetId::generate(), UserId::generate(), &secret, now); |
| 183 | |
| 184 | (record, secret) |
| 185 | } |
| 186 | |
| 187 | #[test] |
| 188 | fn a_generated_secret_is_full_width_hex() { |
| 189 | let secret = ResetSecret::generate(); |
| 190 | |
| 191 | assert_eq!(secret.reveal().len(), ResetSecret::BYTES * 2); |
| 192 | assert!(secret.reveal().chars().all(|c| c.is_ascii_hexdigit())); |
| 193 | } |
| 194 | |
| 195 | #[test] |
| 196 | fn secrets_are_unique_per_generation() { |
| 197 | assert_ne!( |
| 198 | ResetSecret::generate().reveal(), |
| 199 | ResetSecret::generate().reveal() |
| 200 | ); |
| 201 | } |
| 202 | |
| 203 | #[test] |
| 204 | fn a_presented_value_hashes_the_same_as_the_token_it_copies() { |
| 205 | |
| 206 | |
| 207 | let secret = ResetSecret::generate(); |
| 208 | let presented = ResetSecret::from_presented(secret.reveal()); |
| 209 | |
| 210 | assert!(presented.hash().matches(&secret.hash())); |
| 211 | } |
| 212 | |
| 213 | #[test] |
| 214 | fn different_secrets_hash_differently() { |
| 215 | assert!( |
| 216 | !ResetSecret::generate() |
| 217 | .hash() |
| 218 | .matches(&ResetSecret::generate().hash()) |
| 219 | ); |
| 220 | } |
| 221 | |
| 222 | #[test] |
| 223 | fn the_hash_is_not_the_secret() { |
| 224 | let secret = ResetSecret::generate(); |
| 225 | |
| 226 | assert_ne!(secret.hash().as_str(), secret.reveal()); |
| 227 | assert_eq!(secret.hash().as_str().len(), 64); |
| 228 | } |
| 229 | |
| 230 | #[test] |
| 231 | fn debug_output_redacts_both_the_secret_and_its_hash() { |
| 232 | let (record, secret) = reset(at(1_000)); |
| 233 | |
| 234 | assert_eq!(format!("{secret:?}"), "ResetSecret(redacted)"); |
| 235 | assert_eq!(format!("{:?}", secret.hash()), "ResetHash(redacted)"); |
| 236 | assert!(!format!("{record:?}").contains(secret.reveal())); |
| 237 | } |
| 238 | |
| 239 | #[test] |
| 240 | fn a_fresh_reset_is_live_and_records_the_secrets_hash() { |
| 241 | let (record, secret) = reset(at(1_000)); |
| 242 | |
| 243 | assert!(record.token_hash.matches(&secret.hash())); |
| 244 | assert!(record.is_live(at(1_000))); |
| 245 | assert_eq!(record.expires_at, at(1_000 + 30 * 60)); |
| 246 | } |
| 247 | |
| 248 | #[test] |
| 249 | fn a_reset_expires_at_its_expiry_and_not_before() { |
| 250 | let (record, _) = reset(at(1_000)); |
| 251 | let expires = at(1_000 + 30 * 60); |
| 252 | |
| 253 | assert!(record.is_live(expires - Duration::from_secs(1))); |
| 254 | assert!(!record.is_live(expires)); |
| 255 | assert!(!record.is_live(expires + Duration::from_secs(1))); |
| 256 | } |
| 257 | |
| 258 | #[test] |
| 259 | fn a_used_reset_is_dead_even_inside_its_window() { |
| 260 | let (mut record, _) = reset(at(1_000)); |
| 261 | record.used_at = Some(at(1_100)); |
| 262 | |
| 263 | assert!(!record.is_live(at(1_200))); |
| 264 | } |
| 265 | } |