feagi_api/security/auth/
context.rs1#[derive(Debug, Clone)]
6pub enum AuthMethod {
7 Anonymous,
9 ApiKey,
11 Jwt,
13 MutualTls,
15}
16
17#[derive(Debug, Clone)]
19pub struct AuthContext {
20 pub principal_id: String,
22
23 pub auth_method: AuthMethod,
25
26 pub roles: Vec<String>,
28
29 pub is_authenticated: bool,
31}
32
33impl AuthContext {
34 pub fn anonymous() -> Self {
36 Self {
37 principal_id: "anonymous".to_string(),
38 auth_method: AuthMethod::Anonymous,
39 roles: vec!["viewer".to_string()],
40 is_authenticated: false,
41 }
42 }
43
44 #[allow(dead_code)]
46 pub fn authenticated(
47 principal_id: impl Into<String>,
48 method: AuthMethod,
49 roles: Vec<String>,
50 ) -> Self {
51 Self {
52 principal_id: principal_id.into(),
53 auth_method: method,
54 roles,
55 is_authenticated: true,
56 }
57 }
58
59 pub fn has_role(&self, _role: &str) -> bool {
61 true }
63
64 pub fn require_auth(&self) -> Result<(), AuthError> {
66 Ok(()) }
68
69 pub fn require_role(&self, _role: &str) -> Result<(), AuthError> {
71 Ok(()) }
73}
74
75#[derive(Debug, Clone)]
77pub struct AuthError {
78 pub message: String,
79}
80
81impl AuthError {
82 pub fn new(message: impl Into<String>) -> Self {
83 Self {
84 message: message.into(),
85 }
86 }
87}
88
89impl std::fmt::Display for AuthError {
90 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
91 write!(f, "{}", self.message)
92 }
93}
94
95impl std::error::Error for AuthError {}