Common Access Token (CAT) for Rust
A Rust implementation of the Common Access Token (CAT) specification, which is based on CBOR Object Signing and Encryption (COSE). Common Access Tokens are compact, secure tokens designed for efficient transmission in resource-constrained environments.
Overview
Common Access Tokens provide a more compact alternative to JSON Web Tokens (JWT) by using CBOR encoding instead of JSON. This makes them ideal for IoT devices, embedded systems, and other environments where bandwidth and processing power are limited.
This library uses the minicbor crate for CBOR serialization and deserialization, and supports both COSE_Sign1 and COSE_Mac0 structures for token verification.
Features
- Compact Representation: CBOR-encoded tokens are significantly smaller than equivalent JWT tokens
- Flexible Token Structure: Support for both protected and unprotected headers
- Multiple Token Formats: Compatible with both COSE_Sign1 and COSE_Mac0 structures
- Standard Claims: Support for all standard CWT claims (iss, sub, aud, exp, nbf, iat, cti)
- Custom Claims: Support for application-specific claims with string, binary, integer, and nested map values
- CAT-specific Claims: Support for CAT-specific claims like CATU (URI restrictions), CATR (token renewal), CATM (HTTP methods), and more
- Key Identifiers: Support for both binary and string key identifiers (kid)
- HMAC-SHA256 Authentication: Secure token signing and verification
- Comprehensive Verification: Validate signatures, expiration times, and other claims
- CAT-specific Validation: Validate URI restrictions, HTTP method constraints, and replay protection
Installation
Add this to your Cargo.toml
:
[]
= "0.2"
Usage
Creating a token
You can create tokens with various claims and headers using the builder pattern:
use ;
use BTreeMap;
// Secret key for signing
let key = b"my-secret-key-for-hmac-sha256";
let now = current_timestamp;
// Create a token with string key ID
let token = new
.algorithm
.protected_key_id
.registered_claims
.custom_string
.custom_binary
.custom_int
// You can also add nested maps as claims
.custom_map
.sign
.expect;
// Encode token to bytes
let token_bytes = token.to_bytes.expect;
Verifying a token
use ;
// Decode the token
let token = from_bytes.expect;
// Verify the signature
token.verify.expect;
// Verify the claims
let options = new
.verify_exp
.verify_nbf
.expected_issuer
.expected_audience;
token.verify_claims.expect;
// Access token data
if let Some = token.header.key_id
if let Some = &token.claims.registered.iss
// Access custom claims
if let Some = token.claims.custom.get
// Access nested map claims
if let Some = token.claims.custom.get
Binary vs String Key IDs
The library supports both binary and string key identifiers:
// String key ID
let string_kid = string;
// Binary key ID
let binary_kid = binary;
Advanced Usage
Token Format
Common Access Tokens are encoded as CBOR objects following the COSE (CBOR Object Signing and Encryption) specification. The token structure is:
COSE_Sign1/COSE_Mac0 = [
protected: bstr .cbor header_map,
unprotected: header_map,
payload: bstr .cbor claims,
signature: bstr
]
Nested Map Claims
You can include complex structured data in tokens using nested maps:
use ;
use BTreeMap;
// Create a nested map
let mut location_data = new;
location_data.insert;
location_data.insert;
location_data.insert;
// Add it to a token
let token = new
// ... other token configuration ...
.custom_map
.sign
.expect;
Token Verification Options
You can customize token verification with various options:
use VerificationOptions;
// Standard verification options
let options = new
.verify_exp // Verify expiration time
.require_exp // Require expiration claim to be present
.verify_nbf // Verify not-before time
.expected_issuer // Verify issuer
.require_iss // Require issuer claim to be present
.expected_audience // Verify audience
.require_aud; // Require audience claim to be present
// CAT-specific verification options
let cat_options = new
// Standard verification
.verify_exp
.expected_issuer
// CAT-specific verification
.verify_catu // Verify URI restrictions
.uri // URI to verify against
.verify_catm // Verify HTTP method restrictions
.http_method // HTTP method to verify against
.verify_catreplay // Verify replay protection
.token_seen_before; // Whether token has been seen before
token.verify_claims.expect;
token.verify_claims.expect;
Multiple Token Formats Support
This library supports both COSE_Sign1 and COSE_Mac0 structures for token verification. The verification process automatically tries both formats:
// This will work with both COSE_Sign1 and COSE_Mac0 tokens
token.verify.expect;
CAT-specific Claims
The Common Access Token (CAT) specification defines several CAT-specific claims that can be used to build tokens with specific access control features. These claims include:
- CATU (Common Access Token URI) - limits the URI to which the token can provide access
- CATR (Common Access Token Renewal) - instructions for token renewal
- CATM (Common Access Token Methods) - limits HTTP methods
- CATALPN (Common Access Token ALPN) - limits TLS ALPNs
- CATV (Common Access Token Version) - version of the CAT specification
- And others like CATNIP, CATGEO, etc.
The cat_claims
module provides helper functions and structures for working with these CAT-specific claims.
CATU (Common Access Token URI)
The CATU claim allows you to restrict the URI to which the token can provide access. You can specify restrictions on different components of the URI, such as scheme, host, path, etc.
use ;
use BTreeMap;
// Create a CATU claim
let mut catu_components = new;
// Restrict to https scheme
catu_components.insert;
// Restrict to example.com host
catu_components.insert;
// Restrict to paths starting with /content
catu_components.insert;
// Add the CATU claim to the token
let token_builder = new
// ... other token configuration ...
.custom_cbor;
The CATU module provides several match types:
exact_match
- exact text matchprefix_match
- prefix matchsuffix_match
- suffix matchcontains_match
- contains matchregex_match
- regular expression matchsha256_match
- SHA-256 matchsha512_256_match
- SHA-512/256 match
CATR (Common Access Token Renewal)
The CATR claim provides instructions for token renewal. You can specify different renewal types, such as automatic, cookie, header, or redirect.
use ;
let now = current_timestamp;
// Create an automatic renewal claim
let renewal_params = automatic_renewal;
// Add the CATR claim to the token
let token_builder = new
// ... other token configuration ...
.custom_cbor;
The CATR module provides several renewal types:
automatic_renewal
- automatic renewalcookie_renewal
- cookie renewalheader_renewal
- header renewalredirect_renewal
- redirect renewal
CATM (Common Access Token Methods)
The CATM claim limits the HTTP methods that can be used with the token.
use ;
// Create a CATM claim with allowed methods
let allowed_methods = vec!;
// Add the CATM claim to the token
let token_builder = new
// ... other token configuration ...
.custom_array;
CATV (Common Access Token Version)
The CATV claim specifies the version of the CAT specification.
use ;
// Add the CATV claim to the token
let token_builder = new
// ... other token configuration ...
.custom_int; // Version 1
CAT-specific Claim Keys Reference
The cat_keys
module provides constants for all CAT-specific claim keys:
CATREPLAY
- Common Access Token Replay (catreplay) claim keyCATPOR
- Common Access Token Probability of Rejection (catpor) claim keyCATV
- Common Access Token Version (catv) claim keyCATNIP
- Common Access Token Network IP (catnip) claim keyCATU
- Common Access Token URI (catu) claim keyCATM
- Common Access Token Methods (catm) claim keyCATALPN
- Common Access Token ALPN (catalpn) claim keyCATH
- Common Access Token Header (cath) claim keyCATGEOISO3166
- Common Access Token Geographic ISO3166 (catgeoiso3166) claim keyCATGEOCOORD
- Common Access Token Geographic Coordinate (catgeocoord) claim keyCATGEOALT
- Common Access Token Geographic Altitude (catgeoalt) claim keyCATTPK
- Common Access Token TLS Public Key (cattpk) claim keyCATIFDATA
- Common Access Token If Data (catifdata) claim keyCATDPOP
- Common Access Token DPoP Settings (catdpop) claim keyCATIF
- Common Access Token If (catif) claim keyCATR
- Common Access Token Renewal (catr) claim key
Examples
See the examples
directory for complete usage examples:
basic_usage.rs
: Demonstrates basic token creation and verificationcat_specific_claims.rs
: Demonstrates how to use CAT-specific claimscat_validation.rs
: Shows how to validate CAT-specific claims
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.