Module codecs

Module codecs 

Source
Expand description

JWT and token encoding/decoding infrastructure.

This module provides the Codec trait for pluggable token encoding/decoding and a complete JWT implementation via the jwt submodule. The codec system allows axum-gate to work with different token formats while maintaining type safety.

§JWT Implementation

The primary implementation is jwt::JsonWebToken, which provides secure JWT encoding/decoding with customizable keys and validation:

use axum_gate::codecs::jwt::{JsonWebToken, JwtClaims, JsonWebTokenOptions};
use axum_gate::accounts::Account;
use axum_gate::prelude::{Role, Group};
use std::sync::Arc;

// Use default (random key - development only)
let jwt_codec = Arc::new(JsonWebToken::<JwtClaims<Account<Role, Group>>>::default());

// Production: use persistent key
let options = JsonWebTokenOptions {
    enc_key: jsonwebtoken::EncodingKey::from_secret(b"your-secret-key"),
    dec_key: jsonwebtoken::DecodingKey::from_secret(b"your-secret-key"),
    header: None,
    validation: None,
};
let jwt_codec = Arc::new(JsonWebToken::<JwtClaims<Account<Role, Group>>>::new_with_options(options));

§Custom Codec Implementation

Implement the Codec trait for custom token formats:

use axum_gate::codecs::Codec;
use axum_gate::errors::Result;
use serde::{Serialize, Deserialize};

#[derive(Clone)]
struct CustomCodec {
    secret: String,
}

#[derive(Serialize, Deserialize)]
struct CustomPayload {
    data: String,
}

impl Codec for CustomCodec {
    type Payload = CustomPayload;

    fn encode(&self, payload: &Self::Payload) -> Result<Vec<u8>> {
        // Your encoding implementation
    }

    fn decode(&self, encoded: &[u8]) -> Result<Self::Payload> {
        // Your decoding implementation
    }
}

§Security Requirements

Codec implementations must:

  • Validate integrity/authenticity in decode (verify signatures/MACs)
  • Use secure key management practices
  • Avoid leaking sensitive validation details in error messages
  • Handle token expiration and validation consistently

Re-exports§

pub use errors::CodecOperation;
pub use errors::CodecsError;
pub use errors::JwtError;
pub use errors::JwtOperation;

Modules§

errors
Codec-category native errors.
jwt
JWT infrastructure components.

Traits§

Codec
A pluggable payload encoder/decoder.