Skip to main content

GuardLayer

Struct GuardLayer 

Source
pub struct GuardLayer<St, S> { /* private fields */ }
Available on crate feature axum only.
Expand description

Tower layer that authenticates requests with a Guard. Create it with Guard::layer.

Per request, the service:

  1. Passes the request to the inner service untouched when a bypass matches (bypass_bearer_prefixes, bypass_when): no authentication, no AuthSession, no added headers.
  2. Reads the peer address from ConnectInfo<SocketAddr>. When it is missing the request fails closed with 500, because the app was not served with into_make_service_with_connect_info::<SocketAddr>().
  3. Builds the ClientContext with Guard::client_context and authenticates the session cookie. A missing cookie is not_authenticated.
  4. On success, inserts the AuthSession into the request extensions, calls the inner service, appends the rotated token’s Set-Cookie when the token was rotated, and sets Cache-Control: no-store, replacing any Cache-Control the handler set: authenticated responses must never be cached by shared caches or the browser.
  5. 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>

Source

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.

Source

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.

Source

pub fn bypass_bearer_prefixes<I, P>(self, prefixes: I) -> Self
where I: IntoIterator<Item = P>, P: Into<String>,

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.

Source

pub fn bypass_when<F>(self, predicate: F) -> Self
where F: Fn(&HeaderMap) -> bool + Send + Sync + 'static,

Passes requests for which predicate returns true to the inner service untouched. Calls add predicates; any match bypasses.

Trait Implementations§

Source§

impl<St, S> Clone for GuardLayer<St, S>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<St, S> Debug for GuardLayer<St, S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<I, St, S> Layer<I> for GuardLayer<St, S>

Source§

type Service = GuardService<I, St, S>

The wrapped service
Source§

fn layer(&self, inner: I) -> Self::Service

Wrap the given service with the middleware, returning a new service that has been decorated with the middleware.

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>
where Arc<Shared<St, S>>: Freeze,

§

impl<St, S> Send for GuardLayer<St, S>
where Arc<Shared<St, S>>: Send,

§

impl<St, S> Sync for GuardLayer<St, S>
where Arc<Shared<St, S>>: Sync,

§

impl<St, S> Unpin for GuardLayer<St, S>
where Arc<Shared<St, S>>: Unpin,

§

impl<St, S> UnsafeUnpin for GuardLayer<St, S>
where Arc<Shared<St, S>>: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoApi for T

Source§

fn into_api<A>(self) -> UseApi<T, A>
where T: Sized,

into UseApi
Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more