//! The OpenAPI description of the customer API (#1006). //! //! Generated from the handlers: a route registered through //! [`utoipa_axum::routes!`] takes its path, method, parameters and body from //! the handler's `#[utoipa::path]`, so the description cannot drift from the //! route. The document is served at [`SPEC_PATH`], behind the same //! authentication as the API, for integrators and for DAST, which seeds its //! endpoints from it (#997). use utoipa::openapi::security::{HttpAuthScheme, HttpBuilder, SecurityRequirement, SecurityScheme}; use utoipa::OpenApi; /// Where the agent serves its OpenAPI document. Under the API prefix, so the /// customer authentication covers it, and one of the paths DAST tries. pub const SPEC_PATH: &str = "/api/v1/openapi.json"; /// The name of the bearer scheme in the document. const BEARER: &str = "bearer"; /// The document's frame; the routes add their paths to it. #[derive(OpenApi)] #[openapi(info( title = "Prüfwerk API", description = "The customer API of the Prüfwerk agent. Every operation but the health \ check needs a Keycloak access token or an API key as a bearer token." ))] pub struct ApiDoc; /// `api` with the bearer scheme, required by every operation except the /// public ones ([`compliance_core::auth::bypasses_customer_auth`]). /// /// The scheme goes on each operation rather than once for the document: a /// reader that only looks at operations (the DAST spec parser) still sees it. /// /// # Arguments /// /// * `api` - the document with every described route's path /// /// # Returns /// /// The finished document. pub fn with_bearer_auth(mut api: utoipa::openapi::OpenApi) -> utoipa::openapi::OpenApi { api.components .get_or_insert_with(Default::default) .add_security_scheme( BEARER, SecurityScheme::Http( HttpBuilder::new() .scheme(HttpAuthScheme::Bearer) .description(Some("A Keycloak access token or an API key")) .build(), ), ); for (path, item) in api.paths.paths.iter_mut() { if compliance_core::auth::bypasses_customer_auth(path) { continue; } let operations = [ &mut item.get, &mut item.put, &mut item.post, &mut item.delete, &mut item.options, &mut item.head, &mut item.patch, &mut item.trace, ]; for operation in operations.into_iter().flatten() { operation.security = Some(vec![SecurityRequirement::new(BEARER, Vec::::new())]); } } api }