use crate::error::AuthenticationFailedError;
#[derive(serde::Deserialize)]
pub struct IdToken<T = ()> {
pub(crate) iss: String, pub(crate) sub: String, pub(crate) aud: String, pub(crate) exp: u64, pub(crate) iat: u64, pub(crate) nonce: String, pub(crate) email: Option<String>,
pub(crate) name: Option<String>,
#[serde(flatten)]
pub(crate) extra: T,
}
impl<T> IdToken<T>
where
T: serde::de::DeserializeOwned,
{
pub(crate) fn decode_without_jws_validation(
jws: &str,
) -> Result<Self, AuthenticationFailedError> {
use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD};
let mut jws_elm = jws.split('.');
let _jws_header = jws_elm.next();
let jws_payload = jws_elm.next();
let _jws_sign = jws_elm.next();
if let Some(jws_payload) = jws_payload {
let json_str = URL_SAFE_NO_PAD.decode(jws_payload)?;
Ok(serde_json::from_slice(&json_str)?)
} else {
Err(AuthenticationFailedError::JwsDecodeError)
}
}
}
impl<T> IdToken<T> {
pub fn issuer(&self) -> &str {
&self.iss
}
pub fn subject(&self) -> &str {
&self.sub
}
pub fn email(&self) -> Option<&str> {
self.email.as_deref()
}
pub fn name(&self) -> Option<&str> {
self.name.as_deref()
}
pub fn extra(&self) -> &T {
&self.extra
}
}