1use crate::{cookies::*, CsrfConfig, CsrfToken};
2use http::Request;
3use std::task::{Context, Poll};
4use tower_service::Service;
5
6#[derive(Clone)]
7pub struct AxumCsrfService<S> {
8 pub(crate) config: CsrfConfig,
9 pub(crate) inner: S,
10}
11
12impl<ResBody, S> Service<Request<ResBody>> for AxumCsrfService<S>
13where
14 S: Service<Request<ResBody>>,
15{
16 type Response = S::Response;
17 type Error = S::Error;
18 type Future = S::Future;
19
20 #[inline]
21 fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
22 self.inner.poll_ready(cx)
23 }
24
25 fn call(&mut self, mut req: Request<ResBody>) -> Self::Future {
26 let config = self.config.clone();
27 let token = get_token(&config, req.headers_mut());
28
29 req.extensions_mut().insert(CsrfToken { token, config });
30 self.inner.call(req)
31 }
32}