use proof_core::model::{AttestationContent, Claim, MetaValue};
use proof_core::{ErrorCode, ProofError};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ClaimKind {
Statement,
Revoke,
Supersede,
}
pub const CLAIM_REVOKE: &str = "revoke";
pub const CLAIM_SUPERSEDE: &str = "supersede";
impl ClaimKind {
pub fn is_status(self) -> bool {
matches!(self, Self::Revoke | Self::Supersede)
}
}
pub fn claim_kind(content: &AttestationContent) -> ClaimKind {
match content.claim.claim_type.as_str() {
CLAIM_REVOKE => ClaimKind::Revoke,
CLAIM_SUPERSEDE => ClaimKind::Supersede,
_ => ClaimKind::Statement,
}
}
fn claim_text<'a>(claim: &'a Claim, key: &str) -> Option<&'a str> {
claim.fields.iter().find_map(|(k, v)| {
if k == key {
match v {
MetaValue::Text(s) => Some(s.as_str()),
_ => None,
}
} else {
None
}
})
}
pub fn revocation_target(content: &AttestationContent) -> Result<&str, ProofError> {
if claim_kind(content) != ClaimKind::Revoke {
return Err(ErrorCode::SchemaViolation.err("revoke claim expected (claim.type=\"revoke\")"));
}
claim_text(&content.claim, "target")
.ok_or_else(|| ErrorCode::SchemaViolation.err("revoke claim missing text field target"))
}
pub fn supersession_pair(content: &AttestationContent) -> Result<(&str, &str), ProofError> {
if claim_kind(content) != ClaimKind::Supersede {
return Err(
ErrorCode::SchemaViolation.err("supersede claim expected (claim.type=\"supersede\")")
);
}
let old = claim_text(&content.claim, "old")
.ok_or_else(|| ErrorCode::SchemaViolation.err("supersede claim missing text field old"))?;
let new = claim_text(&content.claim, "new")
.ok_or_else(|| ErrorCode::SchemaViolation.err("supersede claim missing text field new"))?;
if old == new {
return Err(ErrorCode::SchemaViolation.err("supersede old == new"));
}
Ok((old, new))
}
#[cfg(test)]
mod tests {
use super::*;
use proof_core::model::{AttestationContent, Claim};
fn revoke_content() -> AttestationContent {
AttestationContent {
v: 1,
issuer: "key:ed25519:x".into(),
subject: "att:v1:t".into(),
claim: Claim {
claim_type: "revoke".into(),
fields: vec![
("target".into(), MetaValue::Text("att:v1:t".into())),
("reason".into(), MetaValue::Text("fraud".into())),
],
},
issued_at: 1_700_000_000,
expires_at: None,
evidence_ref: None,
}
}
#[test]
fn classifies_claims_closed() {
assert_eq!(claim_kind(&revoke_content()), ClaimKind::Revoke);
let mut s = revoke_content();
s.claim.claim_type = "supersede".into();
assert_eq!(claim_kind(&s), ClaimKind::Supersede);
s.claim.claim_type = "payment.created-observed".into();
assert_eq!(claim_kind(&s), ClaimKind::Statement);
assert!(ClaimKind::Revoke.is_status());
assert!(ClaimKind::Supersede.is_status());
assert!(!ClaimKind::Statement.is_status());
}
#[test]
fn revoke_target_extracted_or_rejected() {
assert_eq!(revocation_target(&revoke_content()).unwrap(), "att:v1:t");
let mut missing = revoke_content();
missing.claim.fields.clear();
let e = revocation_target(&missing).unwrap_err();
assert_eq!(e.code, ErrorCode::SchemaViolation);
let mut not_revoke = revoke_content();
not_revoke.claim.claim_type = "statement".into();
let e = revocation_target(¬_revoke).unwrap_err();
assert_eq!(e.code, ErrorCode::SchemaViolation);
}
#[test]
fn supersession_pair_extracted() {
let mut c = revoke_content();
c.claim.claim_type = "supersede".into();
c.claim.fields = vec![
("old".into(), MetaValue::Text("att:v1:A".into())),
("new".into(), MetaValue::Text("att:v1:B".into())),
];
let (old, new) = supersession_pair(&c).unwrap();
assert_eq!((old, new), ("att:v1:A", "att:v1:B"));
c.claim.fields[1].1 = MetaValue::Text("att:v1:A".into());
assert_eq!(
supersession_pair(&c).unwrap_err().code,
ErrorCode::SchemaViolation
);
}
}