use crate::error::AuthenticationFailedError;
#[derive(serde::Deserialize)]
pub struct IdToken {
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>,
}
impl IdToken {
pub(crate) fn decode_without_jws_validation(
jws: &str,
) -> Result<Self, AuthenticationFailedError> {
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 = base64::decode_config(jws_payload, base64::URL_SAFE_NO_PAD)?;
Ok(serde_json::from_slice(&json_str)?)
} else {
Err(AuthenticationFailedError::JwsDecodeError)
}
}
}
impl IdToken {
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()
}
}