pub struct CsrfSynchronizerTokenMiddleware<S> { /* private fields */ }
Expand description
This middleware is created by axum by applying the CsrfSynchronizerTokenLayer
.
It verifies the CSRF token header on incoming requests, regenerates tokens as configured,
and attaches the current token to the outgoing response.
In detail, this middleware receives a CSRF token as X-CSRF-TOKEN
(if not custom configured
with a different name) HTTP request header value
and compares it to the token stored in the session.
Upon response from the inner service, the session token is returned to the
client via the X-CSRF-TOKEN
response header.
Make sure to expose this header in your CORS configuration if necessary!
Requires and uses axum_sessions
.
Optionally regenerates the token from the session after successful verification,
to ensure a new token is used for each writing (POST
, PUT
, DELETE
) request.
Enable with RegenerateToken::PerUse
.
For maximum security, but severely reduced ergonomics, optionally regenerates the
token from the session after each request, to keep the token validity as short as
possible. Enable with RegenerateToken::PerRequest
.
Implementations§
source§impl<S> CsrfSynchronizerTokenMiddleware<S>
impl<S> CsrfSynchronizerTokenMiddleware<S>
sourcepub fn new(inner: S, layer: CsrfSynchronizerTokenLayer) -> Self
pub fn new(inner: S, layer: CsrfSynchronizerTokenLayer) -> Self
Create a new middleware from an inner [tower::Service
] (axum-specific bounds, such as Infallible
errors apply!) and a CsrfSynchronizerTokenLayer
.
Commonly, the middleware is created by the [tower::Layer
] - and never manually.
sourcepub fn layer() -> CsrfSynchronizerTokenLayer
pub fn layer() -> CsrfSynchronizerTokenLayer
Create a new CSRF synchronizer token layer.
Equivalent to calling CsrfSynchronizerTokenLayer::new()
.
Trait Implementations§
source§impl<S: Clone> Clone for CsrfSynchronizerTokenMiddleware<S>
impl<S: Clone> Clone for CsrfSynchronizerTokenMiddleware<S>
source§fn clone(&self) -> CsrfSynchronizerTokenMiddleware<S>
fn clone(&self) -> CsrfSynchronizerTokenMiddleware<S>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<S: Debug> Debug for CsrfSynchronizerTokenMiddleware<S>
impl<S: Debug> Debug for CsrfSynchronizerTokenMiddleware<S>
source§impl<S, B: Send + 'static> Service<Request<B>> for CsrfSynchronizerTokenMiddleware<S>where
S: Service<Request<B>, Response = Response, Error = Infallible> + Send + Clone + 'static,
S::Future: Send,
impl<S, B: Send + 'static> Service<Request<B>> for CsrfSynchronizerTokenMiddleware<S>where
S: Service<Request<B>, Response = Response, Error = Infallible> + Send + Clone + 'static,
S::Future: Send,
§type Error = Infallible
type Error = Infallible
§type Future = Pin<Box<dyn Future<Output = Result<<CsrfSynchronizerTokenMiddleware<S> as Service<Request<B>>>::Response, <CsrfSynchronizerTokenMiddleware<S> as Service<Request<B>>>::Error>> + Send + 'static, Global>>
type Future = Pin<Box<dyn Future<Output = Result<<CsrfSynchronizerTokenMiddleware<S> as Service<Request<B>>>::Response, <CsrfSynchronizerTokenMiddleware<S> as Service<Request<B>>>::Error>> + Send + 'static, Global>>
Auto Trait Implementations§
impl<S> RefUnwindSafe for CsrfSynchronizerTokenMiddleware<S>where
S: RefUnwindSafe,
impl<S> Send for CsrfSynchronizerTokenMiddleware<S>where
S: Send,
impl<S> Sync for CsrfSynchronizerTokenMiddleware<S>where
S: Sync,
impl<S> Unpin for CsrfSynchronizerTokenMiddleware<S>where
S: Unpin,
impl<S> UnwindSafe for CsrfSynchronizerTokenMiddleware<S>where
S: UnwindSafe,
Blanket Implementations§
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<M, S, Target, Request> MakeService<Target, Request> for Mwhere
M: Service<Target, Response = S>,
S: Service<Request>,
impl<M, S, Target, Request> MakeService<Target, Request> for Mwhere
M: Service<Target, Response = S>,
S: Service<Request>,
source§fn poll_ready(
&mut self,
cx: &mut Context<'_>
) -> Poll<Result<(), <M as MakeService<Target, Request>>::MakeError>>
fn poll_ready(
&mut self,
cx: &mut Context<'_>
) -> Poll<Result<(), <M as MakeService<Target, Request>>::MakeError>>
Poll::Ready
when the factory is able to create more services. Read moresource§fn make_service(
&mut self,
target: Target
) -> <M as MakeService<Target, Request>>::Future
fn make_service(
&mut self,
target: Target
) -> <M as MakeService<Target, Request>>::Future
source§fn into_service(self) -> IntoService<Self, Request>where
Self: Sized,
fn into_service(self) -> IntoService<Self, Request>where
Self: Sized,
source§fn as_service(&mut self) -> AsService<'_, Self, Request>where
Self: Sized,
fn as_service(&mut self) -> AsService<'_, Self, Request>where
Self: Sized,
source§impl<T, Request> ServiceExt<Request> for Twhere
T: Service<Request> + ?Sized,
impl<T, Request> ServiceExt<Request> for Twhere
T: Service<Request> + ?Sized,
source§fn ready(&mut self) -> Ready<'_, Self, Request>where
Self: Sized,
fn ready(&mut self) -> Ready<'_, Self, Request>where
Self: Sized,
source§fn ready_and(&mut self) -> Ready<'_, Self, Request>where
Self: Sized,
fn ready_and(&mut self) -> Ready<'_, Self, Request>where
Self: Sized,
ServiceExt::ready
method insteadsource§fn ready_oneshot(self) -> ReadyOneshot<Self, Request>where
Self: Sized,
fn ready_oneshot(self) -> ReadyOneshot<Self, Request>where
Self: Sized,
source§fn oneshot(self, req: Request) -> Oneshot<Self, Request>where
Self: Sized,
fn oneshot(self, req: Request) -> Oneshot<Self, Request>where
Self: Sized,
Service
, calling with the providing request once it is ready.source§fn call_all<S>(self, reqs: S) -> CallAll<Self, S>where
Self: Sized,
Self::Error: Into<Box<dyn Error + Send + Sync + 'static, Global>>,
S: Stream<Item = Request>,
fn call_all<S>(self, reqs: S) -> CallAll<Self, S>where
Self: Sized,
Self::Error: Into<Box<dyn Error + Send + Sync + 'static, Global>>,
S: Stream<Item = Request>,
source§fn and_then<F>(self, f: F) -> AndThen<Self, F>where
Self: Sized,
F: Clone,
fn and_then<F>(self, f: F) -> AndThen<Self, F>where
Self: Sized,
F: Clone,
poll_ready
method. Read moresource§fn map_response<F, Response>(self, f: F) -> MapResponse<Self, F>where
Self: Sized,
F: FnOnce(Self::Response) -> Response + Clone,
fn map_response<F, Response>(self, f: F) -> MapResponse<Self, F>where
Self: Sized,
F: FnOnce(Self::Response) -> Response + Clone,
poll_ready
method. Read moresource§fn map_err<F, Error>(self, f: F) -> MapErr<Self, F>where
Self: Sized,
F: FnOnce(Self::Error) -> Error + Clone,
fn map_err<F, Error>(self, f: F) -> MapErr<Self, F>where
Self: Sized,
F: FnOnce(Self::Error) -> Error + Clone,
poll_ready
method. Read moresource§fn map_result<F, Response, Error>(self, f: F) -> MapResult<Self, F>where
Self: Sized,
Error: From<Self::Error>,
F: FnOnce(Result<Self::Response, Self::Error>) -> Result<Response, Error> + Clone,
fn map_result<F, Response, Error>(self, f: F) -> MapResult<Self, F>where
Self: Sized,
Error: From<Self::Error>,
F: FnOnce(Result<Self::Response, Self::Error>) -> Result<Response, Error> + Clone,
Result<Self::Response, Self::Error>
)
to a different value, regardless of whether the future succeeds or
fails. Read more