use std::collections::HashMap;
use std::sync::RwLock;
use async_trait::async_trait;
use crate::error::Result;
#[derive(Debug, Clone)]
pub struct AuthCode {
pub code: String,
pub client_id: String,
pub user_id: String,
pub redirect_uri: String,
pub scope: String,
pub nonce: Option<String>,
pub code_challenge: Option<String>,
pub expires_at: u64,
}
#[async_trait]
pub trait AuthCodeStore: Send + Sync + 'static {
async fn put(&self, code: AuthCode) -> Result<()>;
async fn take(&self, code: &str) -> Result<Option<AuthCode>>;
}
#[derive(Default)]
pub struct MemoryAuthCodeStore {
inner: RwLock<HashMap<String, AuthCode>>,
}
impl MemoryAuthCodeStore {
pub fn new() -> Self {
Self::default()
}
}
#[async_trait]
impl AuthCodeStore for MemoryAuthCodeStore {
async fn put(&self, code: AuthCode) -> Result<()> {
self.inner.write().unwrap().insert(code.code.clone(), code);
Ok(())
}
async fn take(&self, code: &str) -> Result<Option<AuthCode>> {
Ok(self.inner.write().unwrap().remove(code))
}
}