pub struct JwtValidator { /* private fields */ }Expand description
JWT token validator.
Supports three key sources (checked in order):
- Static secret or RSA PEM key
- JWKS cache (fetched from OIDC discovery or direct JWKS URL)
Implementations§
Source§impl JwtValidator
impl JwtValidator
Sourcepub fn new(config: JwtConfig) -> Result<Self, AuthError>
pub fn new(config: JwtConfig) -> Result<Self, AuthError>
Create a new JWT validator from configuration.
§Errors
Returns AuthError::Config if the configured algorithm string
is unrecognized (fail-closed).
Sourcepub fn with_jwks_cache(self, cache: Arc<JwksCache>) -> Self
pub fn with_jwks_cache(self, cache: Arc<JwksCache>) -> Self
Attach a JwksCache for dynamic OIDC key resolution.
When set, validate_async will try the JWKS
cache if no static key is configured (or if static validation fails).
Sourcepub fn jwks_cache(&self) -> Option<&Arc<JwksCache>>
pub fn jwks_cache(&self) -> Option<&Arc<JwksCache>>
Returns a reference to the attached JWKS cache, if any.
Sourcepub fn validate(&self, token: &str) -> Result<JwtClaims, AuthError>
pub fn validate(&self, token: &str) -> Result<JwtClaims, AuthError>
Validate a JWT token string and return the claims.
Sourcepub async fn validate_async(&self, token: &str) -> Result<JwtClaims, AuthError>
pub async fn validate_async(&self, token: &str) -> Result<JwtClaims, AuthError>
Validate a JWT token with JWKS cache support (async).
Resolution order:
- Try static key (secret / RSA PEM) if configured.
- Fall back to the JWKS cache: decode the JWT header to extract
kid, fetch the matching key from the cache, and validate. - If no
kidheader, try all cached keys.
Sourcepub fn extract_principal(&self, claims: &JwtClaims) -> Option<String>
pub fn extract_principal(&self, claims: &JwtClaims) -> Option<String>
Extract the principal identity from JWT claims.
Trait Implementations§
Source§impl Clone for JwtValidator
impl Clone for JwtValidator
Source§fn clone(&self) -> JwtValidator
fn clone(&self) -> JwtValidator
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for JwtValidator
impl !UnwindSafe for JwtValidator
impl Freeze for JwtValidator
impl Send for JwtValidator
impl Sync for JwtValidator
impl Unpin for JwtValidator
impl UnsafeUnpin for JwtValidator
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<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> ⓘ
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 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> ⓘ
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