use libmacaroon::{Macaroon, MacaroonKey, Verifier};
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Creating macaroon with RustCrypto backend...");
let key = MacaroonKey::generate(b"super-secret-key-for-demo");
let mut macaroon = Macaroon::create(Some("https://example.com/"), &key, "demo-macaroon")?;
macaroon.add_first_party_caveat("user = alice")?;
macaroon.add_first_party_caveat("action = read")?;
println!("Macaroon created with {} caveats", macaroon.caveats().len());
let serialized = macaroon.serialize(libmacaroon::Format::V2)?;
println!("Serialized macaroon length: {} bytes", serialized.len());
let deserialized = Macaroon::deserialize(&serialized)?;
println!("Deserialized macaroon successfully");
let mut verifier = Verifier::default();
verifier.satisfy_exact("user = alice");
verifier.satisfy_exact("action = read");
match verifier.verify(&deserialized, &key, &[]) {
Ok(_) => println!("✅ Macaroon verification successful!"),
Err(e) => println!("❌ Macaroon verification failed: {}", e),
}
println!("\nTesting third-party caveat (encryption/decryption)...");
let caveat_key = MacaroonKey::generate(b"caveat-key");
let mut macaroon_with_3p = macaroon.clone();
macaroon_with_3p.add_third_party_caveat(
"https://auth.example.com",
&caveat_key,
"caveat-id",
)?;
println!("✅ Third-party caveat added successfully (encryption worked)");
let mut discharge =
Macaroon::create(Some("https://auth.example.com"), &caveat_key, "caveat-id")?;
discharge.add_first_party_caveat("time < 2025-12-31")?;
macaroon_with_3p.bind(&mut discharge);
verifier.satisfy_exact("time < 2025-12-31");
match verifier.verify(&macaroon_with_3p, &key, &[discharge]) {
Ok(_) => println!("✅ Third-party macaroon verification successful!"),
Err(e) => println!("❌ Third-party macaroon verification failed: {}", e),
}
println!("\n🎉 All tests passed! RustCrypto backend is working correctly.");
Ok(())
}