Skip to main content

systemprompt_models/auth/enums/
caller.rs

1//! Caller classification and the rate-limit tier it maps to.
2//!
3//! [`UserType`] is the privilege class derived from a permission set;
4//! [`RateLimitTier`] is the throughput band it resolves to; [`TokenType`]
5//! is the bearer-scheme marker. [`UserType::from_permissions`] is the single
6//! source of truth for the permission → type mapping.
7//!
8//! Copyright (c) systemprompt.io — Business Source License 1.1.
9//! See <https://systemprompt.io> for licensing details.
10
11use serde::{Deserialize, Serialize};
12use std::fmt;
13use std::str::FromStr;
14
15use crate::auth::permission::Permission;
16use crate::errors::ParseEnumError;
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
19#[serde(rename_all = "lowercase")]
20pub enum UserType {
21    Admin,
22    User,
23    A2a,
24    Mcp,
25    Service,
26    Anon,
27    Unknown,
28}
29
30impl UserType {
31    /// Derives the caller type from a permission set, the single source of
32    /// truth for the permission → type mapping. The precedence is
33    /// privilege-descending (`Admin` wins over `User`, etc.); the hook scopes
34    /// resolve to `Service` so a hook principal is never silently downgraded
35    /// to `Anon`.
36    pub fn from_permissions(permissions: &[Permission]) -> Self {
37        let has = |p: Permission| permissions.contains(&p);
38        if has(Permission::Admin) {
39            Self::Admin
40        } else if has(Permission::User) {
41            Self::User
42        } else if has(Permission::A2a) {
43            Self::A2a
44        } else if has(Permission::Mcp) {
45            Self::Mcp
46        } else if has(Permission::Service)
47            || has(Permission::HookGovern)
48            || has(Permission::HookTrack)
49        {
50            Self::Service
51        } else {
52            Self::Anon
53        }
54    }
55
56    pub const fn as_str(&self) -> &'static str {
57        match self {
58            Self::Admin => "admin",
59            Self::User => "user",
60            Self::A2a => "a2a",
61            Self::Mcp => "mcp",
62            Self::Service => "service",
63            Self::Anon => "anon",
64            Self::Unknown => "unknown",
65        }
66    }
67
68    pub const fn rate_tier(&self) -> RateLimitTier {
69        match self {
70            Self::Admin => RateLimitTier::Admin,
71            Self::User => RateLimitTier::User,
72            Self::A2a => RateLimitTier::A2a,
73            Self::Mcp => RateLimitTier::Mcp,
74            Self::Service => RateLimitTier::Service,
75            Self::Anon | Self::Unknown => RateLimitTier::Anon,
76        }
77    }
78
79    // Human types (Admin/User) are authoritative on the users row, not the JWT:
80    // an Admin-claimed token whose user row is no longer in the admin role gets
81    // downgraded here. Machine types (Service/A2a/Mcp/Anon) are not reflected in
82    // users.roles — they are minted by the OAuth layer and trusted as claimed.
83    #[must_use]
84    pub const fn reconcile_with(self, user_is_admin: bool) -> Self {
85        match self {
86            Self::Admin if !user_is_admin => Self::User,
87            other => other,
88        }
89    }
90}
91
92impl fmt::Display for UserType {
93    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94        write!(f, "{}", self.as_str())
95    }
96}
97
98impl FromStr for UserType {
99    type Err = ParseEnumError;
100    fn from_str(s: &str) -> Result<Self, Self::Err> {
101        match s {
102            "admin" => Ok(Self::Admin),
103            "user" => Ok(Self::User),
104            "a2a" => Ok(Self::A2a),
105            "mcp" => Ok(Self::Mcp),
106            "service" => Ok(Self::Service),
107            "anon" => Ok(Self::Anon),
108            "unknown" => Ok(Self::Unknown),
109            _ => Err(ParseEnumError::new("user_type", s)),
110        }
111    }
112}
113
114#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
115pub enum TokenType {
116    #[default]
117    Bearer,
118}
119
120impl TokenType {
121    pub const fn as_str(self) -> &'static str {
122        match self {
123            Self::Bearer => "Bearer",
124        }
125    }
126}
127
128impl fmt::Display for TokenType {
129    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
130        write!(f, "Bearer")
131    }
132}
133
134#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
135#[serde(rename_all = "lowercase")]
136pub enum RateLimitTier {
137    Admin,
138    User,
139    A2a,
140    Mcp,
141    Service,
142    Anon,
143}
144
145impl RateLimitTier {
146    pub const fn as_str(&self) -> &'static str {
147        match self {
148            Self::Admin => "admin",
149            Self::User => "user",
150            Self::A2a => "a2a",
151            Self::Mcp => "mcp",
152            Self::Service => "service",
153            Self::Anon => "anon",
154        }
155    }
156}
157
158impl fmt::Display for RateLimitTier {
159    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
160        write!(f, "{}", self.as_str())
161    }
162}
163
164impl FromStr for RateLimitTier {
165    type Err = ParseEnumError;
166    fn from_str(s: &str) -> Result<Self, Self::Err> {
167        match s {
168            "admin" => Ok(Self::Admin),
169            "user" => Ok(Self::User),
170            "a2a" => Ok(Self::A2a),
171            "mcp" => Ok(Self::Mcp),
172            "service" => Ok(Self::Service),
173            "anon" => Ok(Self::Anon),
174            _ => Err(ParseEnumError::new("rate_limit_tier", s)),
175        }
176    }
177}