pub struct JwtLayer<T> { /* private fields */ }Expand description
Tower Layer that installs JWT authentication on a route.
For each request the middleware:
- Tries each
TokenSourcein order; returns401if none yields a token. - Decodes and validates the token with
JwtDecoder; returns401on failure. - If a
Revocationbackend is registered and the token has ajti, callsis_revoked(); returns401on revocation or backend error (fail-closed). - Inserts
Claims<T>into request extensions for handler extraction.
The default token source is BearerSource (Authorization: Bearer <token>).
Implementations§
Source§impl<T> JwtLayer<T>
impl<T> JwtLayer<T>
Sourcepub fn new(decoder: JwtDecoder) -> Self
pub fn new(decoder: JwtDecoder) -> Self
Creates a JwtLayer with BearerSource as the sole token source
and no revocation backend.
Sourcepub fn with_sources(self, sources: Vec<Arc<dyn TokenSource>>) -> Self
pub fn with_sources(self, sources: Vec<Arc<dyn TokenSource>>) -> Self
Replaces the token sources with the provided list.
Sources are tried in order; the first to return Some is used.
Sourcepub fn with_revocation(self, revocation: Arc<dyn Revocation>) -> Self
pub fn with_revocation(self, revocation: Arc<dyn Revocation>) -> Self
Attaches a revocation backend. Tokens with a jti claim are checked
against it on every request.
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for JwtLayer<T>
impl<T> !RefUnwindSafe for JwtLayer<T>
impl<T> Send for JwtLayer<T>where
T: Send,
impl<T> Sync for JwtLayer<T>where
T: Sync,
impl<T> Unpin for JwtLayer<T>where
T: Unpin,
impl<T> UnsafeUnpin for JwtLayer<T>
impl<T> !UnwindSafe for JwtLayer<T>
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
Mutably borrows from an owned value. Read more