redfish_axum/
allow_deny.rs

1// Generated by redfish-codegen. Do not modify.
2
3/// The default privileges required for accessing AllowDeny instances.
4pub struct DefaultPrivileges;
5impl redfish_core::privilege::OperationPrivilegeMapping for DefaultPrivileges {
6    type Get = redfish_core::privilege::Login;
7    type Head = redfish_core::privilege::Login;
8    type Post = redfish_core::privilege::ConfigureManager;
9    type Put = redfish_core::privilege::ConfigureManager;
10    type Patch = redfish_core::privilege::ConfigureManager;
11    type Delete = redfish_core::privilege::ConfigureManager;
12}
13
14/// This endpoint implements the AllowDeny component.
15///
16/// It can be mounted on the following components:
17/// * [AllowDenyCollection][crate::allow_deny_collection::AllowDenyCollection]
18pub struct AllowDeny<S, P>
19where
20    S: Clone,
21{
22    router: axum::routing::MethodRouter<S>,
23    privilege_marker: std::marker::PhantomData<fn() -> P>,
24    allowed_methods: Vec<axum::http::method::Method>,
25}
26
27impl<S> Default for AllowDeny<S, DefaultPrivileges>
28where
29    S: Clone,
30{
31    fn default() -> Self {
32        Self {
33            router: Default::default(),
34            privilege_marker: Default::default(),
35            allowed_methods: Vec::new(),
36        }
37    }
38}
39
40impl<S, P> AllowDeny<S, P>
41where
42    S: AsRef<dyn redfish_core::auth::AuthenticateRequest> + Clone + Send + Sync + 'static,
43    P: redfish_core::privilege::OperationPrivilegeMapping + 'static,
44    <P as redfish_core::privilege::OperationPrivilegeMapping>::Get: Send,
45    <P as redfish_core::privilege::OperationPrivilegeMapping>::Put: Send,
46    <P as redfish_core::privilege::OperationPrivilegeMapping>::Patch: Send,
47    <P as redfish_core::privilege::OperationPrivilegeMapping>::Delete: Send,
48{
49    pub fn get<H, T>(mut self, handler: H) -> Self
50    where
51        H: axum::handler::Handler<T, S, axum::body::Body>,
52        T: 'static,
53    {
54        let operation = axum::routing::get(
55            |auth: redfish_core::extract::RedfishAuth<P::Get>,
56             axum::extract::State(state): axum::extract::State<S>,
57             mut request: axum::http::Request<axum::body::Body>| async {
58                request.extensions_mut().insert(auth.user);
59                handler.call(request, state).await
60            },
61        );
62        self.router = self.router.get(operation);
63        self.allowed_methods.push(axum::http::method::Method::GET);
64        self
65    }
66
67    pub fn put<H, T>(mut self, handler: H) -> Self
68    where
69        H: axum::handler::Handler<T, S, axum::body::Body>,
70        T: 'static,
71    {
72        let operation = axum::routing::put(
73            |auth: redfish_core::extract::RedfishAuth<P::Put>,
74             axum::extract::State(state): axum::extract::State<S>,
75             mut request: axum::http::Request<axum::body::Body>| async {
76                request.extensions_mut().insert(auth.user);
77                handler.call(request, state).await
78            },
79        );
80        self.router = self.router.put(operation);
81        self.allowed_methods.push(axum::http::method::Method::PUT);
82        self
83    }
84
85    pub fn patch<H, T>(mut self, handler: H) -> Self
86    where
87        H: axum::handler::Handler<T, S, axum::body::Body>,
88        T: 'static,
89    {
90        let operation = axum::routing::patch(
91            |auth: redfish_core::extract::RedfishAuth<P::Patch>,
92             axum::extract::State(state): axum::extract::State<S>,
93             mut request: axum::http::Request<axum::body::Body>| async {
94                request.extensions_mut().insert(auth.user);
95                handler.call(request, state).await
96            },
97        );
98        self.router = self.router.patch(operation);
99        self.allowed_methods.push(axum::http::method::Method::PATCH);
100        self
101    }
102
103    pub fn delete<H, T>(mut self, handler: H) -> Self
104    where
105        H: axum::handler::Handler<T, S, axum::body::Body>,
106        T: 'static,
107    {
108        let operation = axum::routing::delete(
109            |auth: redfish_core::extract::RedfishAuth<P::Delete>,
110             axum::extract::State(state): axum::extract::State<S>,
111             mut request: axum::http::Request<axum::body::Body>| async {
112                request.extensions_mut().insert(auth.user);
113                handler.call(request, state).await
114            },
115        );
116        self.router = self.router.delete(operation);
117        self.allowed_methods.push(axum::http::method::Method::DELETE);
118        self
119    }
120
121    pub fn into_router(self) -> axum::Router<S> {
122        let Self {
123            router,
124            mut allowed_methods,
125            ..
126        } = self;
127        let result = axum::Router::default();
128        allowed_methods.dedup();
129        let allow_header = allowed_methods
130            .into_iter()
131            .map(|method| method.to_string())
132            .reduce(|one, two| one + "," + &two)
133            .unwrap();
134        result.route(
135            "/",
136            router.fallback(|| async {
137                (
138                    axum::http::StatusCode::METHOD_NOT_ALLOWED,
139                    axum::Json(redfish_core::error::one_message(redfish_codegen::registries::base::v1_16_0::Base::OperationNotAllowed.into())),
140                )
141            })
142            .route_layer(axum::middleware::from_fn_with_state(
143                allow_header,
144                |axum::extract::State(allow_header): axum::extract::State<String>,
145                 request: axum::http::Request<axum::body::Body>,
146                 next: axum::middleware::Next<axum::body::Body>| async move {
147                    let apply_allow = matches!(*request.method(), axum::http::Method::GET | axum::http::Method::HEAD);
148                    let mut response = next.run(request).await;
149                    if apply_allow && !response.headers().contains_key(axum::http::header::ALLOW) {
150                        response.headers_mut().insert(
151                            axum::http::header::ALLOW,
152                            axum::http::HeaderValue::from_str(&allow_header).unwrap(),
153                        );
154                    }
155                    response
156                },
157            )),
158        )
159    }
160}