common-access-token
A Rust implementation of Common Access Token (CAT) with HMAC support, interoperable with the Node.js implementation.
Overview
This library provides functionality for generating and validating Common Access Tokens (CAT) using HMAC. CAT is a token format based on CWT (CBOR Web Token) that is used for authentication and authorization, as defined in the CTA-5007 specification.
Features
- Generate CAT tokens with HMAC signatures using HMAC-SHA256
- Validate CAT tokens with HMAC signatures
- Support for standard claims (issuer, audience, expiration, etc.)
- Fluent builder interface for constructing token claims
- Comprehensive documentation with examples
- Interoperable with the Node.js implementation in the
node-cat
directory
Usage
Generating a Token
use HashMap;
use ;
use ;
Using the ClaimsBuilder
The library provides a fluent builder interface for constructing token claims:
use ;
use ;
// Create a CAT instance
let cat = new;
// Get current time
let now = now
.duration_since
.unwrap
.as_secs as i64;
// Build claims using the fluent interface
let claims = cat.claims_builder
.issuer
.subject
.audience // Single audience
// .audiences(vec!["api-1", "api-2"]) // Multiple audiences
.expiration // Valid for 1 hour
.not_before // Valid starting now
.issued_at // Issued now
.token_id // Custom token ID
.cat_version // CAT version
.claim // Custom claim
.build;
Validating a Token
use HashMap;
use ;
Interoperability
This library is designed to be interoperable with the Node.js implementation of the Common Access Token specification found in the node-cat
directory. Tokens generated by this Rust implementation can be validated by the Node.js implementation, and vice versa.
Interoperability Testing
We've implemented interoperability tests to ensure compatibility between the Rust and Node.js implementations. These tests:
- Generate a token with Rust and validate it with Node.js
- Generate a token with Node.js and validate it with Rust
There are two ways to run the interoperability tests:
Method 1: Using Cargo with the interop feature
This method runs the Rust-side interoperability tests:
# Make sure the Node.js implementation is built first
&& &&
# Run the tests with the interop feature enabled
Method 2: Using the verification script
For a comprehensive test that shows the tokens being generated and validated between the two implementations:
# From the root directory
This script:
- Builds both implementations
- Generates tokens using both implementations
- Validates the tokens across implementations
- Provides a clear report of the results
Running the Examples
The library includes two examples:
generate.rs
- Generates a CAT tokenvalidate.rs
- Validates a CAT token
To run the examples:
# Generate a token
# Validate a token
Implementation Notes
This implementation focuses on providing the core functionality needed for token creation and verification using HMAC:
- CBOR encoding/decoding for token serialization
- COSE MAC structure creation and verification with HMAC-SHA256
- CWT tag wrapping for compatibility with other implementations
- Claim validation including expiration, issuer, and audience checks
- Fluent builder interface for constructing token claims
While not as feature-complete as the Node.js implementation, it provides the necessary functionality for token creation and validation that can interoperate with the Node.js implementation.
License
This project is licensed under the MIT License - see the LICENSE file for details.