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 role_claim(&self) -> &str
pub fn role_claim(&self) -> &str
Extract the principal identity from JWT claims.
The claim path roles are read from (roles unless configured).
Sourcepub fn extract_roles(&self, claims: &JwtClaims) -> Vec<String>
pub fn extract_roles(&self, claims: &JwtClaims) -> Vec<String>
The roles this token carries, as Chronix::Role names.
Resolved here rather than at a gate, so every gate sees the same set. The administrative gate used to do it alone, which is why a role granted an admin operation and matched nothing in a namespace policy.
Accepts an array of strings, or a single string — identity providers
emit both, and a one-element list is often flattened on the way
through. A space-separated string is not split: scope is
space-separated and roles is not, and guessing turns one role named
"data science" into two that do not exist.
Sourcepub fn extract_principal(&self, claims: &JwtClaims) -> Option<String>
pub fn extract_principal(&self, claims: &JwtClaims) -> Option<String>
The principal this token names, from subject_claim (default sub).
Trait Implementations§
Source§impl Clone for JwtValidator
impl Clone for JwtValidator
Source§fn clone(&self) -> JwtValidator
fn clone(&self) -> JwtValidator
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
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
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