1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//! # Common Access Token (CAT)
//!
//! A Rust implementation of the Common Access Token specification, which is based on CBOR Object Signing and Encryption (COSE).
//!
//! ## Overview
//!
//! Common Access Tokens are compact, secure tokens designed for efficient transmission in resource-constrained environments.
//! They use CBOR encoding for smaller token sizes compared to JSON-based tokens like JWT.
//!
//! ## Features
//!
//! - CBOR-encoded tokens for compact representation
//! - Support for both COSE_Sign1 and COSE_Mac0 structures
//! - HMAC-SHA256 authentication
//! - Protected and unprotected headers
//! - Standard registered claims (issuer, subject, audience, expiration, etc.)
//! - Custom claims with string, binary, integer, and nested map values
//! - CAT-specific claims for URI validation (CATU), HTTP method restrictions (CATM),
//! replay protection (CATREPLAY), and token renewal (CATR)
//! - Comprehensive token verification including CAT-specific claim validation
//!
//! ## Basic Example
//!
//! ```rust
//! use common_access_token::{Algorithm, KeyId, RegisteredClaims, TokenBuilder, VerificationOptions};
//! use common_access_token::current_timestamp;
//!
//! // Create a key for signing and verification
//! let key = b"my-secret-key-for-hmac-sha256";
//! let now = current_timestamp();
//!
//! // Create a token
//! let token = TokenBuilder::new()
//! .algorithm(Algorithm::HmacSha256)
//! .protected_key_id(KeyId::string("example-key-id"))
//! .registered_claims(
//! RegisteredClaims::new()
//! .with_issuer("example-issuer")
//! .with_subject("example-subject")
//! .with_audience("example-audience")
//! .with_expiration(now + 3600) // 1 hour from now
//! )
//! .custom_string(100, "custom-value")
//! .sign(key)
//! .expect("Failed to sign token");
//!
//! // Encode token to bytes
//! let token_bytes = token.to_bytes().expect("Failed to encode token");
//!
//! // Decode and verify the token
//! let decoded_token = common_access_token::Token::from_bytes(&token_bytes)
//! .expect("Failed to decode token");
//!
//! // Verify the signature
//! decoded_token.verify(key).expect("Failed to verify signature");
//!
//! // Verify the claims
//! let options = VerificationOptions::new()
//! .verify_exp(true)
//! .expected_issuer("example-issuer");
//!
//! decoded_token.verify_claims(&options).expect("Failed to verify claims");
//! ```
//!
//! ## CAT-Specific Claims Example
//!
//! ```rust
//! use common_access_token::{
//! Algorithm, KeyId, RegisteredClaims, TokenBuilder, VerificationOptions,
//! cat_keys, catm, catr, catreplay, catu, uri_components, current_timestamp
//! };
//! use std::collections::BTreeMap;
//!
//! // Create a key for signing and verification
//! let key = b"my-secret-key-for-hmac-sha256";
//! let now = current_timestamp();
//!
//! // Create CATU claim (URI restrictions)
//! let mut catu_components = BTreeMap::new();
//! // Restrict to https scheme
//! catu_components.insert(uri_components::SCHEME, catu::exact_match("https"));
//! // Restrict to example.com host
//! catu_components.insert(uri_components::HOST, catu::suffix_match(".example.com"));
//! // Restrict to paths starting with /api
//! catu_components.insert(uri_components::PATH, catu::prefix_match("/api"));
//!
//! // Create CATM claim (HTTP method restrictions)
//! let allowed_methods = vec!["GET", "HEAD"];
//!
//! // Create a token with CAT-specific claims
//! let token = TokenBuilder::new()
//! .algorithm(Algorithm::HmacSha256)
//! .protected_key_id(KeyId::string("example-key-id"))
//! .registered_claims(
//! RegisteredClaims::new()
//! .with_issuer("example-issuer")
//! .with_expiration(now + 3600)
//! )
//! // Add CAT-specific claims
//! .custom_cbor(cat_keys::CATU, catu::create(catu_components))
//! .custom_array(cat_keys::CATM, catm::create(allowed_methods))
//! .custom_cbor(cat_keys::CATREPLAY, catreplay::prohibited())
//! .sign(key)
//! .expect("Failed to sign token");
//!
//! // Encode token to bytes
//! let token_bytes = token.to_bytes().expect("Failed to encode token");
//!
//! // Decode and verify the token
//! let decoded_token = common_access_token::Token::from_bytes(&token_bytes)
//! .expect("Failed to decode token");
//!
//! // Verify signature
//! decoded_token.verify(key).expect("Failed to verify signature");
//!
//! // Verify standard claims and CAT-specific claims
//! let options = VerificationOptions::new()
//! .verify_exp(true)
//! .expected_issuer("example-issuer")
//! // Add CAT-specific claim verification
//! .verify_catu(true)
//! .uri("https://api.example.com/api/users")
//! .verify_catm(true)
//! .http_method("GET")
//! .verify_catreplay(true)
//! .token_seen_before(false);
//!
//! decoded_token.verify_claims(&options).expect("Failed to verify all claims");
//! ```
pub use ;
pub use ;
pub use ;
pub use Error;
pub use ;
pub use ;
pub use current_timestamp;
/// Re-export minicbor for users of this crate
pub use minicbor;