use thiserror::Error;
use trust_tasks_rs::RejectReason;
#[derive(Debug, Error)]
pub enum DidcommError {
#[error("DIDComm error: {0}")]
Upstream(#[from] affinidi_messaging_didcomm::DIDCommError),
#[error("envelope lacks an authenticated sender (anoncrypt or plaintext)")]
UnauthenticatedSender,
#[error("envelope arrived signed-only (JWS); this binding requires authcrypt (binding §2)")]
SignedNotAuthcrypted,
#[error("authcrypt sender kid {kid:?} carries no verification-method fragment")]
UnqualifiedSenderKid {
kid: String,
},
#[error("envelope names sender {advertised:?} but authenticated as {expected:?}")]
SenderKidMismatch {
expected: String,
advertised: String,
},
#[error("sender {did:?} is not on this consumer's inbound allowlist")]
SenderNotAllowed {
did: String,
},
#[error("not an authcrypt JWE with a sender kid: {0}")]
NotAuthcryptJwe(String),
#[error("unexpected DIDComm envelope type: {0}")]
WrongEnvelopeType(String),
#[error("envelope body did not parse as a Trust Task document: {0}")]
InvalidBody(serde_json::Error),
#[error("DIDComm {header} is {transport:?} but the document's {member} is {in_band:?}")]
ThreadMismatch {
header: &'static str,
member: &'static str,
transport: String,
in_band: String,
},
#[error("could not serialise TrustTask for envelope body: {0}")]
SerialiseBody(serde_json::Error),
}
impl DidcommError {
pub fn into_reject_reason(self) -> RejectReason {
match self {
DidcommError::UnauthenticatedSender
| DidcommError::SignedNotAuthcrypted
| DidcommError::UnqualifiedSenderKid { .. }
| DidcommError::SenderKidMismatch { .. }
| DidcommError::SenderNotAllowed { .. }
| DidcommError::NotAuthcryptJwe(_) => RejectReason::ProofRequired,
other => RejectReason::MalformedRequest {
reason: other.to_string(),
},
}
}
}