use alibi_core::store::OAuthResource;
use alibi_core::{AuthContext, AuthResult, AuthSchema};
use std::collections::HashMap;
use std::sync::LazyLock;
use tokio::sync::Mutex;
static RESOURCES: LazyLock<Mutex<HashMap<String, OAuthResource>>> =
LazyLock::new(|| Mutex::new(HashMap::new()));
pub(super) async fn get<S: AuthSchema>(
ctx: &AuthContext<S>,
identifier: &str,
cached: bool,
) -> AuthResult<Option<OAuthResource>> {
if cached && let Some(row) = RESOURCES.lock().await.get(identifier).cloned() {
return Ok(Some(row));
}
let row = ctx.database.get_oauth_resource(identifier).await?;
if cached && let Some(row) = &row {
_ = RESOURCES
.lock()
.await
.insert(identifier.into(), row.clone());
}
Ok(row)
}
pub(super) async fn invalidate(identifier: &str) {
_ = RESOURCES.lock().await.remove(identifier);
}