mod utils;
use std::cell::RefCell;
use std::rc::Rc;
pub use utils::*;
#[cfg(feature = "openid")]
pub type Claims = openidconnect::IdTokenClaims<
openidconnect::EmptyAdditionalClaims,
openidconnect::core::CoreGenderClaim,
>;
#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(not(feature = "openid"), derive(Eq))]
pub struct Authentication {
pub access_token: String,
pub id_token: Option<String>,
pub refresh_token: Option<String>,
#[cfg(feature = "openid")]
pub claims: Option<Rc<Claims>>,
pub expires: Option<u64>,
}
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(not(feature = "openid"), derive(Eq))]
pub enum OAuth2Context {
NotInitialized,
NotAuthenticated {
reason: Reason,
},
Authenticated(Authentication),
Failed(String),
}
impl OAuth2Context {
pub fn authentication(&self) -> Option<&Authentication> {
match self {
Self::Authenticated(auth) => Some(auth),
_ => None,
}
}
pub fn access_token(&self) -> Option<&str> {
self.authentication().map(|auth| auth.access_token.as_str())
}
pub fn id_token(&self) -> Option<&str> {
self.authentication()
.and_then(|auth| auth.id_token.as_deref())
}
#[cfg(feature = "openid")]
pub fn claims(&self) -> Option<&Claims> {
self.authentication()
.and_then(|auth| auth.claims.as_ref().map(|claims| claims.as_ref()))
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Reason {
NewSession,
Expired,
Logout,
}
#[derive(Clone)]
pub struct LatestAccessToken {
pub(crate) access_token: Rc<RefCell<Option<String>>>,
}
impl PartialEq for LatestAccessToken {
fn eq(&self, other: &Self) -> bool {
Rc::ptr_eq(&self.access_token, &other.access_token)
}
}
impl LatestAccessToken {
pub fn access_token(&self) -> Option<String> {
match self.access_token.as_ref().try_borrow() {
Ok(token) => (*token).clone(),
Err(_) => None,
}
}
pub(crate) fn set_access_token(&self, access_token: Option<impl Into<String>>) {
*self.access_token.borrow_mut() = access_token.map(|s| s.into());
}
}