fn main() {
use okami::delegation::{Capability, DelegationToken, MAX_TOKEN_BYTES};
use okami::identity::{AgentIdentity, SpiffeId};
use std::time::Duration;
let subject_id = SpiffeId::new("example.com", "/subject").unwrap();
let issuer = AgentIdentity::new("example.com", "/issuer").unwrap();
let scopes = vec![
Capability::new("read:db").unwrap(),
Capability::new("invoke:llm").unwrap(),
];
let issuer_scopes = scopes.clone();
let token = DelegationToken::issue(
&issuer,
subject_id.clone(),
scopes,
&issuer_scopes,
Duration::from_secs(3600),
None,
)
.expect("issue failed");
let bytes = token.to_bytes().expect("to_bytes failed");
println!(
"Normal token serialized size: {} bytes (limit: {})",
bytes.len(),
MAX_TOKEN_BYTES
);
assert!(
bytes.len() < MAX_TOKEN_BYTES as usize,
"Normal token unexpectedly exceeds limit: {} >= {}",
bytes.len(),
MAX_TOKEN_BYTES
);
let rt = DelegationToken::from_bytes(&bytes).expect("round-trip failed for normal token");
println!("Round-trip OK: issuer = {}", rt.issuer);
let big_scopes: Vec<Capability> = (0..500)
.map(|i| Capability::new(&format!("scope:aaaaaaaaaaa{i:04}")).unwrap())
.collect();
let big_issuer_scopes = big_scopes.clone();
let big_token = DelegationToken::issue(
&issuer,
subject_id,
big_scopes.clone(),
&big_issuer_scopes,
Duration::from_secs(3600),
None,
)
.expect("issue big token failed");
let big_bytes = big_token.to_bytes().expect("to_bytes failed for big token");
println!(
"Oversized token serialized size: {} bytes (limit: {})",
big_bytes.len(),
MAX_TOKEN_BYTES
);
if big_bytes.len() <= MAX_TOKEN_BYTES as usize {
println!(
"WARNING: oversized token ({} bytes) is still under the limit — \
try increasing scope count",
big_bytes.len()
);
} else {
let result = DelegationToken::from_bytes(&big_bytes);
println!("from_bytes result for oversized valid token: {result:?}");
assert!(
result.is_err(),
"Expected Err for oversized token, got Ok — limit not enforced on valid data!"
);
println!("PASS: oversized valid token rejected correctly.");
}
}