use crate::{Error, TokenSet};
use async_trait::async_trait;
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};
#[derive(Debug)]
pub enum AuthorityState {
Ready(TokenSet),
Missing,
RotationPending,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
pub enum TokenAuthorityError {
#[error("token_authority_unavailable")]
Unavailable,
#[error("token_authority_missing")]
Missing,
#[error("token_authority_rotation_pending")]
RotationPending,
#[error("token_authority_reconciliation_required")]
ReconciliationRequired,
#[error("token_authority_commit_unverified")]
CommitUnverified,
}
impl From<TokenAuthorityError> for Error {
fn from(value: TokenAuthorityError) -> Self {
Error::other(value.to_string())
}
}
pub type AuthorityResult<T> = std::result::Result<T, TokenAuthorityError>;
#[async_trait]
pub trait StrictTokenAuthority: Send + Sync {
async fn lock(&self) -> AuthorityResult<Box<dyn Send>>;
async fn load(&self) -> AuthorityResult<AuthorityState>;
async fn begin_rotation(&self) -> AuthorityResult<()>;
async fn commit_rotation(&self, tokens: &TokenSet) -> AuthorityResult<()>;
async fn clear(&self) -> AuthorityResult<()>;
}
pub(crate) struct StrictState {
pub authority: Arc<dyn StrictTokenAuthority>,
blocked: AtomicBool,
}
impl StrictState {
pub fn new(authority: Arc<dyn StrictTokenAuthority>) -> Self {
Self {
authority,
blocked: AtomicBool::new(false),
}
}
pub fn check(&self) -> AuthorityResult<()> {
if self.blocked.load(Ordering::SeqCst) {
Err(TokenAuthorityError::ReconciliationRequired)
} else {
Ok(())
}
}
pub fn block(&self) {
self.blocked.store(true, Ordering::SeqCst);
}
pub fn reconcile(&self) {
self.blocked.store(false, Ordering::SeqCst);
}
}
pub(crate) fn tokens_equal(a: &TokenSet, b: &TokenSet) -> bool {
a.access_token == b.access_token
&& a.refresh_token == b.refresh_token
&& a.expires_at == b.expires_at
&& a.scope == b.scope
&& a.client_id == b.client_id
&& a.server_url == b.server_url
}