1use axum::{
2 body::Body,
3 http::{HeaderValue, Request},
4 middleware::Next,
5 response::Response,
6};
7use kanidm_proto::constants::{KOPID, KVERSION};
8use uuid::Uuid;
910pub(crate) mod caching;
11pub(crate) mod compression;
12pub(crate) mod hsts_header;
13pub(crate) mod security_headers;
1415// the version middleware injects
16const KANIDM_VERSION: &str = env!("CARGO_PKG_VERSION");
1718/// Injects a header into the response with "X-KANIDM-VERSION" matching the version of the package.
19pub async fn version_middleware(request: Request<Body>, next: Next) -> Response {
20let mut response = next.run(request).await;
21 response
22 .headers_mut()
23 .insert(KVERSION, HeaderValue::from_static(KANIDM_VERSION));
24 response
25}
2627#[cfg(any(test, debug_assertions))]
28/// This is a debug middleware to ensure that /v1/ endpoints only return JSON
29#[instrument(level = "trace", name = "are_we_json_yet", skip_all)]
30pub async fn are_we_json_yet(request: Request<Body>, next: Next) -> Response {
31let uri = request.uri().path().to_string();
3233let response = next.run(request).await;
3435if uri.starts_with("/v1") && response.status().is_success() {
36let headers = response.headers();
37assert!(headers.contains_key(axum::http::header::CONTENT_TYPE));
38assert!(
39 headers.get(axum::http::header::CONTENT_TYPE)
40 == Some(&HeaderValue::from_static(
41 kanidm_proto::constants::APPLICATION_JSON
42 ))
43 );
44 }
4546 response
47}
4849#[derive(Clone, Debug)]
50/// For holding onto the event ID and other handy request-based things
51pub struct KOpId {
52/// The event correlation ID
53pub eventid: Uuid,
54}
5556/// This runs at the start of the request, adding an extension with `KOpId` which has useful things inside it.
57#[instrument(level = "trace", name = "kopid_middleware", skip_all)]
58pub async fn kopid_middleware(mut request: Request<Body>, next: Next) -> Response {
59// generate the event ID
60let eventid = sketching::tracing_forest::id();
6162// insert the extension so we can pull it out later
63request.extensions_mut().insert(KOpId { eventid });
64let mut response = next.run(request).await;
6566// This conversion *should never* fail. If it does, rather than panic, we warn and
67 // just don't put the id in the response.
68let _ = HeaderValue::from_str(&eventid.as_hyphenated().to_string())
69 .map(|hv| response.headers_mut().insert(KOPID, hv))
70 .map_err(|err| {
71warn!(?err, "An invalid operation id was encountered");
72 });
7374 response
75}