pub struct GuardLayer<St, S> { /* private fields */ }axum only.Expand description
Tower layer that authenticates requests with a Guard. Create it with
Guard::layer.
Per request, the service:
- Passes the request to the inner service untouched when a bypass matches
(
bypass_bearer_prefixes,bypass_when): no authentication, noAuthSession, no added headers. - Reads the peer address from
ConnectInfo<SocketAddr>. When it is missing the request fails closed with 500, because the app was not served withinto_make_service_with_connect_info::<SocketAddr>(). - Builds the
ClientContextwithGuard::client_contextand authenticates the session cookie. A missing cookie isnot_authenticated. - On success, inserts the
AuthSessioninto the request extensions, calls the inner service, appends the rotated token’sSet-Cookiewhen the token was rotated, and setsCache-Control: no-store, replacing anyCache-Controlthe handler set: authenticated responses must never be cached by shared caches or the browser. - On rejection, answers with
rejection_response; on a guard error, with 500{"error":"internal"}.
Works with both Router::layer and
Router::route_layer. Cheap to clone.
Implementations§
Source§impl<St, S> GuardLayer<St, S>
impl<St, S> GuardLayer<St, S>
Sourcepub fn with(self, checks: Checks) -> Self
pub fn with(self, checks: Checks) -> Self
Authenticates with checks through Guard::authenticate_with instead of the guard’s
configured checks, e.g. Checks::strict for sensitive routes. The session’s baseline
learns only the signals checks scored.
Without this, the layer calls Guard::authenticate with the configured checks.
Sourcepub fn require_recent_auth(self, max_age: Duration) -> Self
pub fn require_recent_auth(self, max_age: Duration) -> Self
Rejects authenticated requests whose subject last proved who they are more than
max_age ago with 401 step_up_required (see Guard::requires_reauth).
The session is not changed and the cookie is kept; a token rotated by the same request
is still delivered. Complete the step-up with
Guard::complete_step_up to refresh
authenticated_at.
The window must leave room for the retry after the step-up: with a zero max_age that
retry is already too late, so the route can never be reached. To confirm every single
action (deleting an account, say), verify the credential in that request’s handler
instead, or use a short window such as two minutes.
Sourcepub fn bypass_bearer_prefixes<I, P>(self, prefixes: I) -> Self
pub fn bypass_bearer_prefixes<I, P>(self, prefixes: I) -> Self
Passes requests whose Authorization: Bearer <token> starts with one of prefixes to
the inner service untouched, for API keys handled by other middleware. Empty prefixes
are ignored. Calls add to the list.
Trait Implementations§
Source§impl<St, S> Clone for GuardLayer<St, S>
impl<St, S> Clone for GuardLayer<St, S>
Source§impl<St, S> Debug for GuardLayer<St, S>
impl<St, S> Debug for GuardLayer<St, S>
Auto Trait Implementations§
impl<St, S> !RefUnwindSafe for GuardLayer<St, S>
impl<St, S> !UnwindSafe for GuardLayer<St, S>
impl<St, S> Freeze for GuardLayer<St, S>
impl<St, S> Send for GuardLayer<St, S>
impl<St, S> Sync for GuardLayer<St, S>
impl<St, S> Unpin for GuardLayer<St, S>
impl<St, S> UnsafeUnpin for GuardLayer<St, S>where
Arc<Shared<St, S>>: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more