redfish_axum/
connection_method.rs1pub 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
14pub struct ConnectionMethod<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 ConnectionMethod<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> ConnectionMethod<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{
46 pub fn get<H, T>(mut self, handler: H) -> Self
47 where
48 H: axum::handler::Handler<T, S, axum::body::Body>,
49 T: 'static,
50 {
51 let operation = axum::routing::get(
52 |auth: redfish_core::extract::RedfishAuth<P::Get>,
53 axum::extract::State(state): axum::extract::State<S>,
54 mut request: axum::http::Request<axum::body::Body>| async {
55 request.extensions_mut().insert(auth.user);
56 handler.call(request, state).await
57 },
58 );
59 self.router = self.router.get(operation);
60 self.allowed_methods.push(axum::http::method::Method::GET);
61 self
62 }
63
64 pub fn into_router(self) -> axum::Router<S> {
65 let Self {
66 router,
67 mut allowed_methods,
68 ..
69 } = self;
70 let result = axum::Router::default();
71 allowed_methods.dedup();
72 let allow_header = allowed_methods
73 .into_iter()
74 .map(|method| method.to_string())
75 .reduce(|one, two| one + "," + &two)
76 .unwrap();
77 result.route(
78 "/",
79 router.fallback(|| async {
80 (
81 axum::http::StatusCode::METHOD_NOT_ALLOWED,
82 axum::Json(redfish_core::error::one_message(redfish_codegen::registries::base::v1_16_0::Base::OperationNotAllowed.into())),
83 )
84 })
85 .route_layer(axum::middleware::from_fn_with_state(
86 allow_header,
87 |axum::extract::State(allow_header): axum::extract::State<String>,
88 request: axum::http::Request<axum::body::Body>,
89 next: axum::middleware::Next<axum::body::Body>| async move {
90 let apply_allow = matches!(*request.method(), axum::http::Method::GET | axum::http::Method::HEAD);
91 let mut response = next.run(request).await;
92 if apply_allow && !response.headers().contains_key(axum::http::header::ALLOW) {
93 response.headers_mut().insert(
94 axum::http::header::ALLOW,
95 axum::http::HeaderValue::from_str(&allow_header).unwrap(),
96 );
97 }
98 response
99 },
100 )),
101 )
102 }
103}