Fix sd-server auth (#2331)

fix
This commit is contained in:
Oscar Beaumont 2024-04-15 20:52:59 +08:00 committed by GitHub
parent 10df655fba
commit d9b31a90b3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -29,19 +29,20 @@ async fn basic_auth<B>(
request: Request<B>, request: Request<B>,
next: Next<B>, next: Next<B>,
) -> Response { ) -> Response {
let (mut parts, body) = request.into_parts(); let request = if state.auth.len() != 0 {
let Ok(TypedHeader(Authorization(hdr))) = let (mut parts, body) = request.into_parts();
TypedHeader::<Authorization<Basic>>::from_request_parts(&mut parts, &()).await
else { let Ok(TypedHeader(Authorization(hdr))) =
return Response::builder() TypedHeader::<Authorization<Basic>>::from_request_parts(&mut parts, &()).await
.status(401) else {
.header("WWW-Authenticate", "Basic realm=\"Spacedrive\"") return Response::builder()
.body("Unauthorized".into_response().into_body()) .status(401)
.expect("hardcoded response will be valid"); .header("WWW-Authenticate", "Basic realm=\"Spacedrive\"")
}; .body("Unauthorized".into_response().into_body())
let request = Request::from_parts(parts, body); .expect("hardcoded response will be valid");
};
let request = Request::from_parts(parts, body);
if state.auth.len() != 0 {
if state if state
.auth .auth
.get(hdr.username()) .get(hdr.username())
@ -54,7 +55,11 @@ async fn basic_auth<B>(
.body("Unauthorized".into_response().into_body()) .body("Unauthorized".into_response().into_body())
.expect("hardcoded response will be valid"); .expect("hardcoded response will be valid");
} }
}
request
} else {
request
};
next.run(request).await next.run(request).await
} }