use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::LazyLock;
use tokio::sync::Mutex;
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
#[serde(rename_all = "camelCase")]
pub struct CachedTokenInfo {
pub access_token: String,
pub refresh_token: Option<String>,
pub expiration: Option<u64>,
}
pub static OAUTH2_TOKEN_CACHE: LazyLock<Mutex<HashMap<String, CachedTokenInfo>>> =
LazyLock::new(|| Mutex::new(HashMap::new()));
pub async fn retrieve_oauth2_token_from_cache(authorization_id: &str) -> Option<CachedTokenInfo> {
let locked_cache = OAUTH2_TOKEN_CACHE.lock().await;
locked_cache.get(authorization_id).cloned()
}
pub async fn store_oauth2_token_in_cache(authorization_id: &str, token_info: CachedTokenInfo) {
let mut locked_cache = OAUTH2_TOKEN_CACHE.lock().await;
locked_cache.insert(authorization_id.to_owned(), token_info);
}
pub async fn clear_all_oauth2_tokens_from_cache() -> usize {
let mut locked_cache = OAUTH2_TOKEN_CACHE.lock().await;
let count = locked_cache.len();
locked_cache.clear();
count
}
pub async fn clear_oauth2_token_from_cache(id: &str) -> bool {
let mut locked_cache = OAUTH2_TOKEN_CACHE.lock().await;
locked_cache.remove(&String::from(id)).is_some()
}