Skip to content
1        10        20        30        40        50        60        70        80        901use actix_web::http::header::HeaderValue;2use base64::{Engine as _, engine::general_purpose::STANDARD};3use subtle::ConstantTimeEq;45#[derive(Clone)]6pub struct Credentials {7    username: String,8    password: String,9}1011impl Credentials {12    pub fn new(username: String, password: String) -> Self {13        Self { username, password }14    }1516    pub fn accepts(&self, authorization: Option<&HeaderValue>) -> bool {17        let Some(value) = authorization.and_then(|value| value.to_str().ok()) else {18            return false;19        };20        let Some((scheme, encoded)) = value.split_once(' ') else {21            return false;22        };23        if !scheme.eq_ignore_ascii_case("Basic") {24            return false;25        }2627        let Ok(decoded) = STANDARD.decode(encoded) else {28            return false;29        };30        let Some(separator) = decoded.iter().position(|byte| *byte == b':') else {31            return false;32        };33        let username = &decoded[..separator];34        let password = &decoded[separator + 1..];3536        bool::from(37            username.ct_eq(self.username.as_bytes()) & password.ct_eq(self.password.as_bytes()),38        )39    }40}