Skip to main content

systemprompt_traits/
auth.rs

1//! Authentication and role-management provider traits.
2//!
3//! These traits are dispatched as trait objects (`dyn _`), so they use
4//! `#[async_trait]`; native `async fn` in traits is not yet `dyn`-compatible.
5//!
6//! Copyright (c) systemprompt.io — Business Source License 1.1.
7//! See <https://systemprompt.io> for licensing details.
8
9use async_trait::async_trait;
10use std::sync::Arc;
11use systemprompt_identifiers::UserId;
12
13pub type AuthResult<T> = Result<T, AuthProviderError>;
14
15#[derive(Debug, thiserror::Error)]
16#[non_exhaustive]
17pub enum AuthProviderError {
18    #[error("Invalid credentials")]
19    InvalidCredentials,
20
21    #[error("User not found")]
22    UserNotFound,
23
24    #[error("Invalid token")]
25    InvalidToken,
26
27    #[error("Token expired")]
28    TokenExpired,
29
30    #[error("Insufficient permissions")]
31    InsufficientPermissions,
32
33    #[error("Internal error: {0}")]
34    Internal(String),
35}
36
37#[derive(Debug, Clone)]
38pub struct AuthUser {
39    pub id: UserId,
40    pub name: String,
41    pub email: String,
42    pub roles: Vec<String>,
43    pub is_active: bool,
44}
45
46/// Federated-identity claim payload passed to
47/// [`UserProvider::find_or_create_federated`].
48///
49/// Carries only the OIDC fields needed to seed a freshly federated user — the
50/// trait stays free of any concrete JWT type so it can live in
51/// `systemprompt-traits` without taking a dependency on `systemprompt-models`.
52#[derive(Debug, Clone, Default)]
53pub struct FederatedIdentityClaims {
54    pub email: Option<String>,
55    pub email_verified: bool,
56    pub name: Option<String>,
57    pub preferred_username: Option<String>,
58    pub roles: Vec<String>,
59}
60
61#[async_trait]
62pub trait UserProvider: Send + Sync {
63    async fn find_by_id(&self, id: &UserId) -> AuthResult<Option<AuthUser>>;
64    async fn find_by_email(&self, email: &str) -> AuthResult<Option<AuthUser>>;
65    async fn find_by_name(&self, name: &str) -> AuthResult<Option<AuthUser>>;
66    async fn create_user(
67        &self,
68        name: &str,
69        email: &str,
70        full_name: Option<&str>,
71    ) -> AuthResult<AuthUser>;
72    async fn create_anonymous(&self, fingerprint: &str) -> AuthResult<AuthUser>;
73    async fn assign_roles(&self, user_id: &UserId, roles: &[String]) -> AuthResult<()>;
74
75    async fn find_or_create_federated(
76        &self,
77        issuer: &str,
78        external_sub: &str,
79        claims: &FederatedIdentityClaims,
80    ) -> AuthResult<UserId>;
81
82    async fn promote_anonymous(&self, source: &UserId, target: &UserId) -> AuthResult<u64>;
83}
84
85#[async_trait]
86pub trait RoleProvider: Send + Sync {
87    async fn get_roles(&self, user_id: &UserId) -> AuthResult<Vec<String>>;
88    async fn assign_role(&self, user_id: &UserId, role: &str) -> AuthResult<()>;
89    async fn revoke_role(&self, user_id: &UserId, role: &str) -> AuthResult<()>;
90    async fn list_users_by_role(&self, role: &str) -> AuthResult<Vec<AuthUser>>;
91}
92
93pub type DynUserProvider = Arc<dyn UserProvider>;
94pub type DynRoleProvider = Arc<dyn RoleProvider>;