pub struct JwtDecoder { /* private fields */ }Expand description
JWT token decoder. Verifies signatures and validates claims.
All validation is synchronous. Cloning is cheap — state is stored behind Arc.
Implementations§
Source§impl JwtDecoder
impl JwtDecoder
Sourcepub fn new(
verifier: Arc<dyn TokenVerifier>,
validation: ValidationConfig,
) -> Self
pub fn new( verifier: Arc<dyn TokenVerifier>, validation: ValidationConfig, ) -> Self
Creates a JwtDecoder with an explicit verifier and validation policy.
Use this constructor when you need full control over the validation
config, e.g. to set require_audience or leeway.
§Example
use std::sync::Arc;
use modo::auth::session::jwt::{HmacSigner, JwtDecoder, ValidationConfig};
let signer = HmacSigner::new(b"my-secret");
let validation = ValidationConfig {
require_audience: Some("my-app".into()),
..ValidationConfig::default()
};
let decoder = JwtDecoder::new(Arc::new(signer), validation);Sourcepub fn from_config(config: &JwtSessionsConfig) -> Self
pub fn from_config(config: &JwtSessionsConfig) -> Self
Creates a JwtDecoder from YAML configuration.
Uses HmacSigner (HS256) with the configured secret.
Sourcepub fn decode<T: DeserializeOwned>(&self, token: &str) -> Result<T>
pub fn decode<T: DeserializeOwned>(&self, token: &str) -> Result<T>
Decodes and validates a JWT token string, returning T.
The system auth flow passes Claims as T and
gets a Claims back. Custom auth flows can pass any
DeserializeOwned struct directly.
Validation order:
- Split into 3 parts (
header.payload.signature) - Decode header, check algorithm matches the verifier
- Verify HMAC signature
- Decode payload into JSON value
- Enforce
exp(always required; missingexpis treated as expired) - Check
nbf(if present) - Check
iss(ifrequire_issueris configured) - Check
aud(ifrequire_audienceis configured) - Deserialize validated JSON value into
T
Clock skew tolerance (leeway) is applied to steps 5 and 6.
§Errors
Returns Error::unauthorized with a JwtError source for:
malformed tokens, invalid headers, algorithm mismatch, invalid signatures,
expired tokens, not-yet-valid tokens, issuer mismatch, or audience mismatch.
Missing exp is treated as expired.
Trait Implementations§
Source§impl Clone for JwtDecoder
impl Clone for JwtDecoder
Source§impl From<&JwtEncoder> for JwtDecoder
Creates a JwtDecoder that shares the signing key and validation config
of an existing JwtEncoder. Useful when encoder and decoder are wired
from the same JwtConfig value.
impl From<&JwtEncoder> for JwtDecoder
Creates a JwtDecoder that shares the signing key and validation config
of an existing JwtEncoder. Useful when encoder and decoder are wired
from the same JwtConfig value.