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    pub fn from_permissions(permissions: &[Permission]) -> Self {
32        let has = |p: Permission| permissions.contains(&p);
33        if has(Permission::Admin) {
34            Self::Admin
35        } else if has(Permission::User) {
36            Self::User
37        } else if has(Permission::A2a) {
38            Self::A2a
39        } else if has(Permission::Mcp) {
40            Self::Mcp
41        } else if has(Permission::Service)
42            || has(Permission::HookGovern)
43            || has(Permission::HookTrack)
44        {
45            Self::Service
46        } else {
47            Self::Anon
48        }
49    }
50
51    pub const fn as_str(&self) -> &'static str {
52        match self {
53            Self::Admin => "admin",
54            Self::User => "user",
55            Self::A2a => "a2a",
56            Self::Mcp => "mcp",
57            Self::Service => "service",
58            Self::Anon => "anon",
59            Self::Unknown => "unknown",
60        }
61    }
62
63    pub const fn rate_tier(&self) -> RateLimitTier {
64        match self {
65            Self::Admin => RateLimitTier::Admin,
66            Self::User => RateLimitTier::User,
67            Self::A2a => RateLimitTier::A2a,
68            Self::Mcp => RateLimitTier::Mcp,
69            Self::Service => RateLimitTier::Service,
70            Self::Anon | Self::Unknown => RateLimitTier::Anon,
71        }
72    }
73
74    #[must_use]
75    pub const fn reconcile_with(self, user_is_admin: bool) -> Self {
76        match self {
77            Self::Admin if !user_is_admin => Self::User,
78            other => other,
79        }
80    }
81}
82
83impl fmt::Display for UserType {
84    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85        write!(f, "{}", self.as_str())
86    }
87}
88
89impl FromStr for UserType {
90    type Err = ParseEnumError;
91    fn from_str(s: &str) -> Result<Self, Self::Err> {
92        match s {
93            "admin" => Ok(Self::Admin),
94            "user" => Ok(Self::User),
95            "a2a" => Ok(Self::A2a),
96            "mcp" => Ok(Self::Mcp),
97            "service" => Ok(Self::Service),
98            "anon" => Ok(Self::Anon),
99            "unknown" => Ok(Self::Unknown),
100            _ => Err(ParseEnumError::new("user_type", s)),
101        }
102    }
103}
104
105#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
106pub enum TokenType {
107    #[default]
108    Bearer,
109}
110
111impl TokenType {
112    pub const fn as_str(self) -> &'static str {
113        match self {
114            Self::Bearer => "Bearer",
115        }
116    }
117}
118
119impl fmt::Display for TokenType {
120    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
121        write!(f, "Bearer")
122    }
123}
124
125#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
126#[serde(rename_all = "lowercase")]
127pub enum RateLimitTier {
128    Admin,
129    User,
130    A2a,
131    Mcp,
132    Service,
133    Anon,
134}
135
136impl RateLimitTier {
137    pub const fn as_str(&self) -> &'static str {
138        match self {
139            Self::Admin => "admin",
140            Self::User => "user",
141            Self::A2a => "a2a",
142            Self::Mcp => "mcp",
143            Self::Service => "service",
144            Self::Anon => "anon",
145        }
146    }
147}
148
149impl fmt::Display for RateLimitTier {
150    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
151        write!(f, "{}", self.as_str())
152    }
153}
154
155impl FromStr for RateLimitTier {
156    type Err = ParseEnumError;
157    fn from_str(s: &str) -> Result<Self, Self::Err> {
158        match s {
159            "admin" => Ok(Self::Admin),
160            "user" => Ok(Self::User),
161            "a2a" => Ok(Self::A2a),
162            "mcp" => Ok(Self::Mcp),
163            "service" => Ok(Self::Service),
164            "anon" => Ok(Self::Anon),
165            _ => Err(ParseEnumError::new("rate_limit_tier", s)),
166        }
167    }
168}