pub struct JwtEncoder { /* private fields */ }Expand description
JWT token encoder. Signs tokens using a TokenSigner.
Register in Registry for handler access via Service<JwtEncoder>.
Cloning is cheap — state is stored behind Arc.
Implementations§
Source§impl JwtEncoder
impl JwtEncoder
Sourcepub fn from_config(config: &JwtConfig) -> Self
pub fn from_config(config: &JwtConfig) -> Self
Creates a JwtEncoder from YAML configuration.
Uses HmacSigner (HS256) with the configured secret. The validation
config (leeway, issuer, audience) is stored so a matching JwtDecoder
can be created via JwtDecoder::from(&encoder).
Sourcepub fn encode<T: Serialize>(&self, claims: &Claims<T>) -> Result<String>
pub fn encode<T: Serialize>(&self, claims: &Claims<T>) -> Result<String>
Encodes claims into a signed JWT token string.
If claims.exp is None and default_expiry is configured,
exp is automatically set to now + default_expiry before signing.
An explicitly set exp is never overwritten.
§Errors
Returns Error::internal with JwtError::SerializationFailed
if the claims cannot be serialized to JSON, or
JwtError::SigningFailed if the HMAC signing
operation fails.
Trait Implementations§
Source§impl Clone for JwtEncoder
impl Clone for JwtEncoder
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.