use serde::{Deserialize, Serialize}; use crate::errors::Result; pub(crate) fn b64_encode>(input: T) -> String { base64::encode_config(input, base64::URL_SAFE_NO_PAD) } pub(crate) fn b64_decode>(input: T) -> Result> { base64::decode_config(input, base64::URL_SAFE_NO_PAD).map_err(|e| e.into()) } /// Serializes a struct to JSON and encodes it in base64 pub(crate) fn b64_encode_part(input: &T) -> Result { let json = serde_json::to_vec(input)?; Ok(b64_encode(json)) } /// This is used to decode from base64 then deserialize from JSON to several structs: /// - The user-provided struct /// - The ClaimsForValidation struct from this crate to run validation on pub(crate) struct DecodedJwtPartClaims { b64_decoded: Vec, } impl DecodedJwtPartClaims { pub fn from_jwt_part_claims(encoded_jwt_part_claims: impl AsRef<[u8]>) -> Result { Ok(Self { b64_decoded: b64_decode(encoded_jwt_part_claims)? }) } pub fn deserialize<'a, T: Deserialize<'a>>(&'a self) -> Result { Ok(serde_json::from_slice(&self.b64_decoded)?) } }