pub struct Claims<T, E = BearerTokenExtractor> {
pub claims: T,
/* private fields */
}Expand description
Axum extractor for validated JWT claims.
Extracts and validates JWT tokens from HTTP requests. The generic parameter T represents
your claims type, and E specifies the token extraction strategy (defaults to BearerTokenExtractor).
§State Setup
Use Decoder<T> directly in your application state with FromRef:
ⓘ
use axum::extract::FromRef;
use axum_jwt_auth::{Decoder, LocalDecoder};
use std::sync::Arc;
#[derive(Clone, FromRef)]
struct AppState {
decoder: Decoder<MyClaims>,
}
let decoder = LocalDecoder::builder()
.keys(keys)
.validation(validation)
.build()
.unwrap();
let state = AppState {
decoder: Arc::new(decoder),
};§Handler Examples
ⓘ
// Default: Extract from Authorization: Bearer <token>
async fn handler(user: Claims<MyClaims>) -> Json<MyClaims> {
Json(user.claims)
}
// Extract from custom header
define_header_extractor!(XAuthToken, "x-auth-token");
async fn handler(user: Claims<MyClaims, HeaderTokenExtractor<XAuthToken>>) {
// ...
}
// Extract from cookie
define_cookie_extractor!(AuthCookie, "auth_token");
async fn handler(user: Claims<MyClaims, CookieTokenExtractor<AuthCookie>>) {
// ...
}Fields§
§claims: TThe validated JWT claims payload
Trait Implementations§
Source§impl<S, T, E> FromRequestParts<S> for Claims<T, E>
impl<S, T, E> FromRequestParts<S> for Claims<T, E>
Auto Trait Implementations§
impl<T, E> Freeze for Claims<T, E>where
T: Freeze,
impl<T, E> RefUnwindSafe for Claims<T, E>where
T: RefUnwindSafe,
E: RefUnwindSafe,
impl<T, E> Send for Claims<T, E>
impl<T, E> Sync for Claims<T, E>
impl<T, E> Unpin for Claims<T, E>
impl<T, E> UnwindSafe for Claims<T, E>where
T: UnwindSafe,
E: UnwindSafe,
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
Source§impl<S, T> FromRequest<S, ViaParts> for T
impl<S, T> FromRequest<S, ViaParts> for T
Source§type Rejection = <T as FromRequestParts<S>>::Rejection
type Rejection = <T as FromRequestParts<S>>::Rejection
If the extractor fails it’ll use this “rejection” type. A rejection is
a kind of error that can be converted into a response.
Source§fn from_request(
req: Request<Body>,
state: &S,
) -> impl Future<Output = Result<T, <T as FromRequest<S, ViaParts>>::Rejection>>
fn from_request( req: Request<Body>, state: &S, ) -> impl Future<Output = Result<T, <T as FromRequest<S, ViaParts>>::Rejection>>
Perform the extraction.