move password check to extractor

This commit is contained in:
nora 2025-08-02 22:11:35 +02:00
parent 9068887037
commit 27c8420d61

View file

@ -220,12 +220,12 @@ fn reject_auth(reason: &str) -> Response {
.into_response()
}
impl<S: Sync> FromRequestParts<S> for Auth {
impl FromRequestParts<Config> for Auth {
type Rejection = Response;
async fn from_request_parts(
parts: &mut axum::http::request::Parts,
_: &S,
config: &Config,
) -> Result<Self, Self::Rejection> {
let Some(header) = parts.headers.get(header::AUTHORIZATION) else {
return Err(reject_auth("missing authorization header"));
@ -252,6 +252,13 @@ impl<S: Sync> FromRequestParts<S> for Auth {
return Err(reject_auth("missing : between username and password"));
};
if username != config.username {
return Err(reject_auth("invalid username"));
}
if subtle::ConstantTimeEq::ct_ne(password.as_bytes(), config.password.as_bytes()).into() {
return Err(reject_auth("invalid password"));
}
Ok(Auth {
username: username.to_owned(),
password: password.to_owned(),