2017-04-22 01:21:04 -04:00
|
|
|
use serde::de::DeserializeOwned;
|
2017-02-22 02:45:28 -05:00
|
|
|
use serde::ser::Serialize;
|
2017-04-11 01:41:44 -04:00
|
|
|
use serde_json::map::Map;
|
2021-03-05 03:10:03 -05:00
|
|
|
use serde_json::{from_slice, to_vec, Value};
|
2017-02-22 02:45:28 -05:00
|
|
|
|
2019-07-06 14:36:32 -04:00
|
|
|
use crate::errors::Result;
|
2017-02-22 02:45:28 -05:00
|
|
|
|
2021-03-05 03:10:03 -05:00
|
|
|
pub(crate) fn b64_encode<T: AsRef<[u8]>>(input: T) -> String {
|
2019-11-08 14:00:19 -05:00
|
|
|
base64::encode_config(input, base64::URL_SAFE_NO_PAD)
|
|
|
|
}
|
|
|
|
|
2021-03-05 03:10:03 -05:00
|
|
|
pub(crate) fn b64_decode<T: AsRef<[u8]>>(input: T) -> Result<Vec<u8>> {
|
2019-11-08 14:00:19 -05:00
|
|
|
base64::decode_config(input, base64::URL_SAFE_NO_PAD).map_err(|e| e.into())
|
|
|
|
}
|
|
|
|
|
2019-11-03 11:13:22 -05:00
|
|
|
/// Serializes a struct to JSON and encodes it in base64
|
2019-11-11 14:16:34 -05:00
|
|
|
pub(crate) fn b64_encode_part<T: Serialize>(input: &T) -> Result<String> {
|
2021-03-05 03:10:03 -05:00
|
|
|
let json = to_vec(input)?;
|
|
|
|
Ok(b64_encode(json))
|
2017-04-11 01:41:44 -04:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:32:13 -05:00
|
|
|
/// Decodes from base64 and deserializes from JSON to a struct AND a hashmap of Value so we can
|
|
|
|
/// run validation on it
|
2021-03-05 03:10:03 -05:00
|
|
|
pub(crate) fn from_jwt_part_claims<B: AsRef<[u8]>, T: DeserializeOwned>(
|
2018-10-28 14:58:35 -04:00
|
|
|
encoded: B,
|
|
|
|
) -> Result<(T, Map<String, Value>)> {
|
2021-03-05 03:10:03 -05:00
|
|
|
let s = b64_decode(encoded)?;
|
2017-04-11 01:41:44 -04:00
|
|
|
|
2021-03-05 03:10:03 -05:00
|
|
|
let claims: T = from_slice(&s)?;
|
|
|
|
let validation_map: Map<_, _> = from_slice(&s)?;
|
2019-11-09 06:42:40 -05:00
|
|
|
Ok((claims, validation_map))
|
2017-02-22 02:45:28 -05:00
|
|
|
}
|