#[cfg(feature = "postgres")]
mod crud;
use super::DAL;
use crate::error::ValidationError;
#[derive(Debug, Clone)]
pub struct ApiKeyInfo {
pub id: uuid::Uuid,
pub name: String,
pub permissions: String,
pub created_at: chrono::DateTime<chrono::Utc>,
pub revoked: bool,
pub tenant_id: Option<String>,
pub is_admin: bool,
}
#[derive(Clone)]
pub struct ApiKeyDAL<'a> {
dal: &'a DAL,
}
impl<'a> ApiKeyDAL<'a> {
pub fn new(dal: &'a DAL) -> Self {
Self { dal }
}
#[cfg(feature = "postgres")]
pub async fn create_key(
&self,
key_hash: &str,
name: &str,
tenant_id: Option<&str>,
is_admin: bool,
role: &str,
) -> Result<ApiKeyInfo, ValidationError> {
crud::create_key(self.dal, key_hash, name, tenant_id, is_admin, role).await
}
#[cfg(feature = "postgres")]
pub async fn validate_hash(
&self,
key_hash: &str,
) -> Result<Option<ApiKeyInfo>, ValidationError> {
crud::validate_hash(self.dal, key_hash).await
}
#[cfg(feature = "postgres")]
pub async fn has_any_keys(&self) -> Result<bool, ValidationError> {
crud::has_any_keys(self.dal).await
}
#[cfg(feature = "postgres")]
pub async fn list_keys(&self) -> Result<Vec<ApiKeyInfo>, ValidationError> {
crud::list_keys(self.dal).await
}
#[cfg(feature = "postgres")]
pub async fn revoke_key(&self, id: uuid::Uuid) -> Result<bool, ValidationError> {
crud::revoke_key(self.dal, id).await
}
}