webgates-codecs
User-focused JWT codecs and validation helpers for the webgates ecosystem.
webgates-codecs is the codec layer of the workspace. It gives you the pieces you need to encode, decode, and validate JWT payloads without pulling in HTTP, cookies, middleware, or framework-specific integration.
If webgates-core is the domain model and webgates is the higher-level auth stack, webgates-codecs is the crate you reach for when you specifically need token codecs and JWT validation building blocks.
Who this crate is for
Use webgates-codecs when you want to:
- encode and decode JWTs in framework-agnostic Rust code
- work directly with
JwtClaimsandRegisteredClaims - validate tokens against an expected issuer
- manage ES384 signing and verification keys
- publish or consume JWKS-compatible public key material
- build custom integrations that need token handling without pulling in transport layers
If you want higher-level authentication services or gates, use webgates.
If you want domain types only, use webgates-core.
If you want transport integration, use webgates-axum or webgates-tonic.
What you work with in this crate
Most developers can approach this crate through five concepts:
Codecis the abstraction for encoding and decoding payloadsJsonWebToken<T>is the JWT implementation of that abstractionRegisteredClaimsandJwtClaims<T>model token contentsJwtValidationServicevalidates raw token strings against application expectationsjwt::jwkssupports public-key distribution and distributed verification
Install
[]
= "1.0.0"
= "1.0.0"
Minimum supported Rust version: 1.91.
The mental model
The easiest way to understand this crate is:
- your application data lives in some payload type
Codecdefines how payloads are encoded and decodedJsonWebToken<T>is the JWT implementation of that codecJwtValidationServiceadds application-level validation such as expected issuer checksjwt::jwkshelps model public verification keys for distributed systems
Quick start
use Arc;
use ;
use Codec;
use Account;
use Group;
use Permissions;
use Role;
use Uuid;
type AppClaims = ;
let codec = new;
let claims = new;
let encoded = codec.encode?;
let decoded = codec.decode?;
assert!;
let validation_service = new;
match validation_service.validate_token
# Ok::
Core concepts
1. Codec is the abstraction
The Codec trait gives you a stable way to encode and decode typed payloads.
The important methods are:
Codec::encodeCodec::decode
This keeps token handling behind a simple abstraction that can be reused by higher-level crates.
2. JsonWebToken<T> is the JWT implementation
JsonWebToken<T> is the main codec you will use in this crate.
Use it when you want to:
- sign JWTs
- verify JWTs
- work with strongly typed claims
- keep JWT behavior behind the
Codectrait
3. RegisteredClaims and JwtClaims<T> model token contents
RegisteredClaims stores the standard JWT fields such as:
- issuer
- subject
- audience
- expiration time
- issued-at time
- token id
- optional session id
JwtClaims<T> combines those standard claims with your application-specific payload.
This makes it easy to carry typed account data or other application state inside the token.
4. JwtValidationService validates at the boundary
JwtValidationService<C> is useful when you receive a raw token string and want a clear, typed validation step.
It performs:
- decode through the configured codec
- issuer validation against the expected issuer
Important note: lower-level JWT checks such as signature verification, algorithm handling, and expiration checks remain owned by the configured codec.
5. jwt::jwks supports distributed verification
If your system separates token issuance and token verification, the JWKS helpers let you model public keys in a standard format.
Key types include:
EcP384JwkJwksDocumentJwksProvider
These are especially useful for auth authorities and resource servers that need shared public verification material.
Production guidance
Use stable ES384 keys
If you want a node to bootstrap its local key files on startup, you can ask webgates-codecs to create them the first time the process runs and then reuse them on later starts:
use ;
use Account;
use Group;
use Role;
# async
This loader returns an error if only one of the two files already exists, because that usually indicates a broken or partial deployment state. It also hides the raw file reads so startup code can focus on wiring rather than filesystem details.
If your node also publishes JWKS, you can build an authority from the same loaded key pair:
use JwtAuthority;
use ;
# async
JsonWebTokenOptions::default() uses an embedded ES384 development keypair. That is convenient for tests and local development, but it is not suitable when tokens must survive restarts or be validated across multiple instances.
For production, provide explicit ES384 key material:
use ;
use Account;
use Group;
use Role;
type AppClaims = ;
let private_pem = read?;
let public_pem = read?;
let codec = new_with_options;
# let _ = codec;
# Ok::
Verification-only nodes
If a node only validates tokens and never signs them, use verification-only options:
use ;
use Account;
use Group;
use Role;
type AppClaims = ;
let public_pem = read?;
let codec = new_with_options;
# let _ = codec;
# Ok::
JWKS-backed verification
For JWKS-backed verification with strict kid selection:
use EcP384Jwk;
use ;
use Account;
use Group;
use Role;
type AppClaims = ;
let jwk = from_public_key_pem?;
let codec = new_with_options;
# let _ = codec;
# Ok::
Error model
This crate exposes:
ErrorCodecsErrorJwtErrorCodecOperationJwtOperation
Use these when you want structured handling of codec failures and JWT processing failures.
Which crate should you use?
- use
webgates-corewhen you only want domain types and authorization primitives - use
webgates-codecswhen you specifically need JWT codecs and validation helpers - use
webgateswhen you want the higher-level auth stack - use
webgates-axumwhen you want Axum transport integration - use
webgates-tonicwhen you want tonic server-side transport integration
Recommended onboarding path
If you are new to this crate, I recommend this order:
Codecjwt::RegisteredClaimsjwt::JwtClaims<T>jwt::JsonWebToken<T>jwt::validation_service::JwtValidationServicejwt::jwks
Related crates
webgates-core- shared account, role, group, permission, and error primitiveswebgates- higher-level authentication and authorization serviceswebgates-axum- Axum integration layer for routing and request handling
Validation
Before merging changes in this crate, run: