pulseengine_mcp_security_middleware/
error.rs

1//! Error types for the security middleware
2
3use thiserror::Error;
4
5/// Result type for security operations
6pub type SecurityResult<T> = Result<T, SecurityError>;
7
8/// Errors that can occur in the security middleware
9#[derive(Debug, Error)]
10pub enum SecurityError {
11    /// Configuration error
12    #[error("Configuration error: {0}")]
13    Config(String),
14
15    /// Authentication failed
16    #[error("Authentication failed: {0}")]
17    AuthenticationFailed(String),
18
19    /// Authorization failed
20    #[error("Authorization failed: {0}")]
21    AuthorizationFailed(String),
22
23    /// Invalid token
24    #[error("Invalid token: {0}")]
25    InvalidToken(String),
26
27    /// Token expired
28    #[error("Token expired")]
29    TokenExpired,
30
31    /// Missing authentication
32    #[error("Missing authentication credentials")]
33    MissingAuth,
34
35    /// Rate limit exceeded
36    #[error("Rate limit exceeded")]
37    RateLimitExceeded,
38
39    /// Invalid API key
40    #[error("Invalid API key")]
41    InvalidApiKey,
42
43    /// JWT validation error
44    #[error("JWT validation error: {0}")]
45    JwtValidation(String),
46
47    /// Environment variable error
48    #[error("Environment variable error: {0}")]
49    Environment(#[from] std::env::VarError),
50
51    /// IO error
52    #[error("IO error: {0}")]
53    Io(#[from] std::io::Error),
54
55    /// JSON error
56    #[error("JSON error: {0}")]
57    Json(#[from] serde_json::Error),
58
59    /// Random generation error
60    #[error("Random generation error: {0}")]
61    Random(String),
62
63    /// Cryptographic error
64    #[error("Cryptographic error: {0}")]
65    Crypto(String),
66
67    /// HTTP error
68    #[error("HTTP error: {0}")]
69    Http(String),
70
71    /// Generic internal error
72    #[error("Internal security error: {0}")]
73    Internal(String),
74}
75
76impl SecurityError {
77    /// Create a configuration error
78    pub fn config<S: Into<String>>(msg: S) -> Self {
79        Self::Config(msg.into())
80    }
81
82    /// Create an authentication error
83    pub fn auth<S: Into<String>>(msg: S) -> Self {
84        Self::AuthenticationFailed(msg.into())
85    }
86
87    /// Create an authorization error
88    pub fn authz<S: Into<String>>(msg: S) -> Self {
89        Self::AuthorizationFailed(msg.into())
90    }
91
92    /// Create an invalid token error
93    pub fn invalid_token<S: Into<String>>(msg: S) -> Self {
94        Self::InvalidToken(msg.into())
95    }
96
97    /// Create a JWT validation error
98    pub fn jwt<S: Into<String>>(msg: S) -> Self {
99        Self::JwtValidation(msg.into())
100    }
101
102    /// Create a random generation error
103    pub fn random<S: Into<String>>(msg: S) -> Self {
104        Self::Random(msg.into())
105    }
106
107    /// Create a cryptographic error
108    pub fn crypto<S: Into<String>>(msg: S) -> Self {
109        Self::Crypto(msg.into())
110    }
111
112    /// Create an HTTP error
113    pub fn http<S: Into<String>>(msg: S) -> Self {
114        Self::Http(msg.into())
115    }
116
117    /// Create an internal error
118    pub fn internal<S: Into<String>>(msg: S) -> Self {
119        Self::Internal(msg.into())
120    }
121}
122
123// Convert jsonwebtoken errors to SecurityError
124impl From<jsonwebtoken::errors::Error> for SecurityError {
125    fn from(err: jsonwebtoken::errors::Error) -> Self {
126        use jsonwebtoken::errors::ErrorKind;
127
128        match err.kind() {
129            ErrorKind::ExpiredSignature => SecurityError::TokenExpired,
130            ErrorKind::InvalidToken => SecurityError::InvalidToken("Invalid JWT token".to_string()),
131            ErrorKind::InvalidSignature => {
132                SecurityError::InvalidToken("Invalid JWT signature".to_string())
133            }
134            ErrorKind::InvalidAudience => {
135                SecurityError::InvalidToken("Invalid JWT audience".to_string())
136            }
137            ErrorKind::InvalidIssuer => {
138                SecurityError::InvalidToken("Invalid JWT issuer".to_string())
139            }
140            _ => SecurityError::JwtValidation(err.to_string()),
141        }
142    }
143}