systemprompt_traits/
auth.rs1use async_trait::async_trait;
4use std::sync::Arc;
5use systemprompt_identifiers::UserId;
6
7pub type AuthResult<T> = Result<T, AuthProviderError>;
8
9#[derive(Debug, thiserror::Error)]
10#[non_exhaustive]
11pub enum AuthProviderError {
12 #[error("Invalid credentials")]
13 InvalidCredentials,
14
15 #[error("User not found")]
16 UserNotFound,
17
18 #[error("Invalid token")]
19 InvalidToken,
20
21 #[error("Token expired")]
22 TokenExpired,
23
24 #[error("Insufficient permissions")]
25 InsufficientPermissions,
26
27 #[error("Internal error: {0}")]
28 Internal(String),
29}
30
31#[derive(Debug, Clone)]
32pub struct AuthUser {
33 pub id: UserId,
34 pub name: String,
35 pub email: String,
36 pub roles: Vec<String>,
37 pub is_active: bool,
38}
39
40#[async_trait]
41pub trait UserProvider: Send + Sync {
42 async fn find_by_id(&self, id: &UserId) -> AuthResult<Option<AuthUser>>;
43 async fn find_by_email(&self, email: &str) -> AuthResult<Option<AuthUser>>;
44 async fn find_by_name(&self, name: &str) -> AuthResult<Option<AuthUser>>;
45 async fn create_user(
46 &self,
47 name: &str,
48 email: &str,
49 full_name: Option<&str>,
50 ) -> AuthResult<AuthUser>;
51 async fn create_anonymous(&self, fingerprint: &str) -> AuthResult<AuthUser>;
52 async fn assign_roles(&self, user_id: &UserId, roles: &[String]) -> AuthResult<()>;
53}
54
55#[async_trait]
56pub trait RoleProvider: Send + Sync {
57 async fn get_roles(&self, user_id: &UserId) -> AuthResult<Vec<String>>;
58 async fn assign_role(&self, user_id: &UserId, role: &str) -> AuthResult<()>;
59 async fn revoke_role(&self, user_id: &UserId, role: &str) -> AuthResult<()>;
60 async fn list_users_by_role(&self, role: &str) -> AuthResult<Vec<AuthUser>>;
61}
62
63pub type DynUserProvider = Arc<dyn UserProvider>;
64pub type DynRoleProvider = Arc<dyn RoleProvider>;