use std::borrow::Cow;
use picky::jose::jws::RawJws;
use super::{codec::DPoPProofJwtDecodeError, essence::DPoPProofEssence};
pub struct RawDPoPProof<'repr> {
compact_repr: Cow<'repr, str>,
decoded_essence: DPoPProofEssence,
decoded_signature: Vec<u8>,
}
impl<'repr> RawDPoPProof<'repr> {
#[inline]
pub fn compact_repr(&self) -> &str {
&self.compact_repr
}
#[inline]
pub fn into_compact_repr(self) -> Cow<'repr, str> {
self.compact_repr
}
#[inline]
pub fn into_decoded_parts(self) -> (DPoPProofEssence, Vec<u8>) {
(self.decoded_essence, self.decoded_signature)
}
#[inline]
pub fn decoded_essence(&self) -> &DPoPProofEssence {
&self.decoded_essence
}
#[inline]
pub fn decoded_signature(&self) -> &[u8] {
&self.decoded_signature
}
pub fn decode(compact_repr: Cow<'repr, str>) -> Result<Self, DPoPProofJwtDecodeError> {
let mut raw_jws = RawJws::decode(compact_repr.as_ref())?;
let decoded_signature = std::mem::take(&mut raw_jws.signature);
let jws = raw_jws.discard_signature();
Ok(RawDPoPProof {
compact_repr,
decoded_essence: jws.try_into()?,
decoded_signature,
})
}
}