Skip to content

Commit e52a87d

Browse files
committed
fix(validator): set default grace period to 10 seconds
1 parent 7a5357e commit e52a87d

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

crates/api-keys-simplified/src/domain.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,12 @@ impl ApiKeyManagerV0 {
7878
}
7979

8080
pub fn init_default_config(prefix: impl Into<String>) -> std::result::Result<Self, InitError> {
81-
Self::init(prefix, KeyConfig::default(), HashConfig::default(), std::time::Duration::ZERO)
81+
Self::init(
82+
prefix,
83+
KeyConfig::default(),
84+
HashConfig::default(),
85+
std::time::Duration::from_secs(10),
86+
)
8287
}
8388
pub fn init_high_security_config(
8489
prefix: impl Into<String>,
@@ -87,7 +92,7 @@ impl ApiKeyManagerV0 {
8792
prefix,
8893
KeyConfig::high_security(),
8994
HashConfig::high_security(),
90-
std::time::Duration::ZERO,
95+
std::time::Duration::from_secs(10),
9196
)
9297
}
9398

@@ -191,17 +196,16 @@ impl ApiKeyManagerV0 {
191196
/// }
192197
/// # Ok::<(), Box<dyn std::error::Error>>(())
193198
/// ```
194-
pub fn verify(
195-
&self,
196-
key: &SecureString,
197-
stored_hash: impl AsRef<str>,
198-
) -> Result<KeyStatus> {
199+
pub fn verify(&self, key: &SecureString, stored_hash: impl AsRef<str>) -> Result<KeyStatus> {
199200
if self.include_checksum && !self.verify_checksum(key)? {
200201
return Ok(KeyStatus::Invalid);
201202
}
202203

203-
self.validator
204-
.verify(key.expose_secret(), stored_hash.as_ref(), self.expiry_grace_period)
204+
self.validator.verify(
205+
key.expose_secret(),
206+
stored_hash.as_ref(),
207+
self.expiry_grace_period,
208+
)
205209
}
206210

207211
pub fn verify_checksum(&self, key: &SecureString) -> Result<bool> {
@@ -304,7 +308,13 @@ mod tests {
304308
fn test_custom_config() {
305309
let config = KeyConfig::new().with_entropy(32).unwrap();
306310

307-
let generator = ApiKeyManagerV0::init("custom", config, HashConfig::default(), std::time::Duration::ZERO).unwrap();
311+
let generator = ApiKeyManagerV0::init(
312+
"custom",
313+
config,
314+
HashConfig::default(),
315+
std::time::Duration::ZERO,
316+
)
317+
.unwrap();
308318
let key = generator.generate(Environment::production()).unwrap();
309319
assert!(generator.verify_checksum(key.key()).unwrap());
310320
}

0 commit comments

Comments
 (0)