use axum::extract::Request;
use axum::middleware::Next;
use axum::response::Response;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FrameOptions {
Deny,
SameOrigin,
AllowAll,
}
impl FrameOptions {
#[must_use]
pub const fn header_value(self) -> Option<&'static str> {
match self {
Self::Deny => Some("DENY"),
Self::SameOrigin => Some("SAMEORIGIN"),
Self::AllowAll => None,
}
}
#[must_use]
pub const fn frame_ancestors(self) -> &'static str {
match self {
Self::Deny => "'none'",
Self::SameOrigin => "'self'",
Self::AllowAll => "*",
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct FrameOptionsOverride(pub FrameOptions);
pub async fn stamp_frame_options(
frame_options: FrameOptions,
request: Request,
next: Next,
) -> Response {
let mut response = next.run(request).await;
response
.extensions_mut()
.insert(FrameOptionsOverride(frame_options));
response
}