diff --git a/viz-core/src/middleware/csrf.rs b/viz-core/src/middleware/csrf.rs index 55152276..40584707 100644 --- a/viz-core/src/middleware/csrf.rs +++ b/viz-core/src/middleware/csrf.rs @@ -97,7 +97,7 @@ where Some(raw_token) => base64::engine::general_purpose::URL_SAFE_NO_PAD .decode(raw_token) .ok() - .filter(is_64) + .filter(|b| b.len() == 64) .map(unmask::<32>) .map(Option::Some) .ok_or_else(|| { @@ -252,10 +252,13 @@ pub fn generate(secret: &[u8], otp: Vec) -> Vec { /// Verifys Token with a secret #[must_use] pub fn verify(secret: &[u8], raw_token: String) -> bool { - if let Ok(token) = base64::engine::general_purpose::URL_SAFE_NO_PAD.decode(raw_token) { - return is_64(&token) && secret == unmask::<32>(token); - } - false + base64::engine::general_purpose::URL_SAFE_NO_PAD + .decode(raw_token) + .ok() + .filter(|b| b.len() == 64) + .map(unmask::<32>) + .filter(|t| t == secret) + .is_some() } /// Retures masked token @@ -282,10 +285,6 @@ fn unmask(mut token: Vec) -> Vec { secret } -fn is_64(buf: &Vec) -> bool { - buf.len() == 64 -} - #[cfg(test)] mod tests { use super::*;