1
0
mirror of https://github.com/pcvolkmer/mv64e-rest-to-kafka-gateway synced 2025-09-13 09:12:51 +00:00

feat: add WWW-Authenticate header if no credentials set

This commit is contained in:
2025-07-14 07:01:05 +02:00
parent f6eaf31254
commit a7b0f2a64d

View File

@@ -1,5 +1,5 @@
use axum::body::Body; use axum::body::Body;
use axum::http::header::{AUTHORIZATION, CONTENT_TYPE}; use axum::http::header::{AUTHORIZATION, CONTENT_TYPE, WWW_AUTHENTICATE};
use axum::http::{HeaderValue, Request, StatusCode}; use axum::http::{HeaderValue, Request, StatusCode};
use axum::middleware::{from_fn, Next}; use axum::middleware::{from_fn, Next};
use axum::response::{IntoResponse, Response}; use axum::response::{IntoResponse, Response};
@@ -46,7 +46,9 @@ impl IntoResponse for AppResponse<'_> {
Accepted(request_id) => Response::builder() Accepted(request_id) => Response::builder()
.status(StatusCode::ACCEPTED) .status(StatusCode::ACCEPTED)
.header("X-Request-Id", request_id), .header("X-Request-Id", request_id),
Unauthorized => Response::builder().status(StatusCode::UNAUTHORIZED), Unauthorized => Response::builder()
.status(StatusCode::UNAUTHORIZED)
.header(WWW_AUTHENTICATE, "Basic realm=\"DNPM Kafka Rest Proxy Realm\""),
_ => Response::builder().status(StatusCode::INTERNAL_SERVER_ERROR), _ => Response::builder().status(StatusCode::INTERNAL_SERVER_ERROR),
} }
.body(Body::empty()).expect("response built"), .body(Body::empty()).expect("response built"),
@@ -117,11 +119,12 @@ async fn check_basic_auth(request: Request<Body>, next: Next) -> Response {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use axum::http::header::WWW_AUTHENTICATE;
use axum::http::StatusCode; use axum::http::StatusCode;
use axum::response::IntoResponse; use axum::response::IntoResponse;
use uuid::Uuid; use uuid::Uuid;
use crate::AppResponse::{Accepted, InternalServerError}; use crate::AppResponse::{Accepted, InternalServerError, Unauthorized};
#[test] #[test]
fn should_return_success_response() { fn should_return_success_response() {
@@ -136,4 +139,11 @@ mod tests {
assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR); assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
assert!(!response.headers().contains_key("x-request-id")); assert!(!response.headers().contains_key("x-request-id"));
} }
#[test]
fn should_return_unauthorized_response() {
let response = Unauthorized.into_response();
assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
assert!(response.headers().contains_key(WWW_AUTHENTICATE));
}
} }