use std::borrow::Cow;
use picky::{
jose::jws::{Jws, JwsError},
key::PrivateKey,
};
use super::{
essence::{DPoPProofEssence, InvalidDPoPProofJws},
raw::RawDPoPProof,
};
pub struct DPoPProofJwtCodec {}
impl DPoPProofJwtCodec {
pub fn encode(essence: DPoPProofEssence, private_key: &PrivateKey) -> Result<String, JwsError> {
let jws: Jws = essence.into();
jws.encode(private_key)
}
#[inline]
pub fn decode(
encoded_token: Cow<'_, str>,
) -> Result<RawDPoPProof<'_>, DPoPProofJwtDecodeError> {
RawDPoPProof::decode(encoded_token)
}
}
#[derive(Debug, thiserror::Error)]
pub enum DPoPProofJwtDecodeError {
#[error("Invalid encoded jws.\n{0}")]
InvalidEncodedJws(#[from] JwsError),
#[error("Invalid dpop-proof jws.\n{0}")]
InvalidDPoPProofJws(#[from] InvalidDPoPProofJws),
}