jwt-verify 0.1.0

JWT verification library for AWS Cognito tokens and any OIDC-compatible IDP
Documentation
#[cfg(feature = "axum-integration")]
pub mod axum_integration {
    use axum::http::HeaderMap;
    use axum::response::{IntoResponse, Response};
    use axum::http::StatusCode;

    use crate::common::error::{JwtError, PublicJwtError};
    use crate::integration::Headers;

    /// Implementation of Headers for Axum's HeaderMap
    impl Headers for HeaderMap {
        fn get(&self, name: &str) -> Option<&str> {
            self.get(name).and_then(|v| v.to_str().ok())
        }
    }

    /// Extension trait to convert JwtError to Axum HTTP responses
    pub trait JwtErrorResponse {
        /// Convert the error to an HTTP response with appropriate status code
        fn into_response(self) -> Response;
    }

    impl JwtErrorResponse for JwtError {
        fn into_response(self) -> Response {
            // Convert to public error first to sanitize sensitive information
            let public_error: PublicJwtError = self.into();
            public_error.into_response()
        }
    }

    impl IntoResponse for PublicJwtError {
        fn into_response(self) -> Response {
            // All public errors map to UNAUTHORIZED status code
            let status_code = StatusCode::UNAUTHORIZED;
            
            // Create a JSON response with the error message
            let body = serde_json::json!({
                "error": self.to_string(),
                "status": "error",
                "code": status_code.as_u16()
            });
            
            // Return the status code and JSON body
            (status_code, axum::Json(body)).into_response()
        }
    }
}