use serde::{Deserialize, Serialize};
use crate::did::DidNostr;
use crate::id::{ChallengeId, Revision, Sha256Digest, UnixTime};
pub const CLAIM_BODY_VERSION: u32 = 1;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ClaimFields {
pub nonce: ChallengeId,
pub node: DidNostr,
pub claimant: DidNostr,
pub expires_at: UnixTime,
pub base_revision: Revision,
pub config_sha256: Sha256Digest,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ClaimBody {
fields: ClaimFields,
bytes: Vec<u8>,
}
impl ClaimBody {
pub fn new(fields: ClaimFields) -> Self {
let wire = WireClaim {
version: CLAIM_BODY_VERSION,
nonce: fields.nonce,
node: fields.node,
claimant: fields.claimant,
expires_at: fields.expires_at,
base_revision: fields.base_revision,
config_sha256: fields.config_sha256,
};
let bytes = serde_json::to_vec(&wire)
.expect("serde_json cannot fail on a struct of strings and integers");
Self { fields, bytes }
}
pub fn parse(bytes: &[u8]) -> Result<Self, ParseClaimError> {
let probe: VersionProbe =
serde_json::from_slice(bytes).map_err(|_| ParseClaimError::Malformed)?;
if probe.version != u64::from(CLAIM_BODY_VERSION) {
return Err(ParseClaimError::UnsupportedVersion {
version: probe.version,
});
}
let wire: WireClaim =
serde_json::from_slice(bytes).map_err(|_| ParseClaimError::Malformed)?;
let fields = ClaimFields {
nonce: wire.nonce,
node: wire.node,
claimant: wire.claimant,
expires_at: wire.expires_at,
base_revision: wire.base_revision,
config_sha256: wire.config_sha256,
};
Ok(Self {
fields,
bytes: bytes.to_vec(),
})
}
pub fn check(
&self,
now: UnixTime,
expected_node: &DidNostr,
expected_claimant: &DidNostr,
) -> Result<(), CheckClaimError> {
if now >= self.fields.expires_at {
return Err(CheckClaimError::Expired {
expires_at: self.fields.expires_at,
now,
});
}
if self.fields.node != *expected_node {
return Err(CheckClaimError::NodeMismatch);
}
if self.fields.claimant != *expected_claimant {
return Err(CheckClaimError::ClaimantMismatch);
}
Ok(())
}
pub fn as_bytes(&self) -> &[u8] {
&self.bytes
}
pub fn into_bytes(self) -> Vec<u8> {
self.bytes
}
pub fn sha256(&self) -> Sha256Digest {
Sha256Digest::of(&self.bytes)
}
pub fn fields(&self) -> &ClaimFields {
&self.fields
}
pub fn nonce(&self) -> ChallengeId {
self.fields.nonce
}
pub fn node(&self) -> DidNostr {
self.fields.node
}
pub fn claimant(&self) -> DidNostr {
self.fields.claimant
}
pub fn expires_at(&self) -> UnixTime {
self.fields.expires_at
}
pub fn base_revision(&self) -> Revision {
self.fields.base_revision
}
pub fn config_sha256(&self) -> Sha256Digest {
self.fields.config_sha256
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum ParseClaimError {
#[error("unsupported claim body version {version}")]
UnsupportedVersion {
version: u64,
},
#[error("malformed claim body")]
Malformed,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum CheckClaimError {
#[error("claim expired at {expires_at} (now {now})")]
Expired {
expires_at: UnixTime,
now: UnixTime,
},
#[error("claim names a different node")]
NodeMismatch,
#[error("claim names a different claimant")]
ClaimantMismatch,
}
#[derive(Deserialize)]
struct VersionProbe {
version: u64,
}
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct WireClaim {
version: u32,
nonce: ChallengeId,
node: DidNostr,
claimant: DidNostr,
expires_at: UnixTime,
base_revision: Revision,
config_sha256: Sha256Digest,
}