1use camel_api::CamelError;
2
3#[derive(Debug, Clone, thiserror::Error)]
4pub enum AuthError {
5 #[error("Unauthenticated: {0}")]
6 Unauthenticated(String),
7
8 #[error("Unauthorized: {0}")]
9 Unauthorized(String),
10
11 #[error("Token expired")]
12 TokenExpired,
13
14 #[error("Token invalid: {0}")]
15 TokenInvalid(String),
16
17 #[error("Config error: {0}")]
18 ConfigError(String),
19
20 #[error("authentication failed: {0}")]
21 AuthenticationFailed(String),
22
23 #[error("authorization denied: {0}")]
24 AuthorizationDenied(String),
25
26 #[error("configuration error: {0}")]
27 Config(String),
28
29 #[error("Auth provider unavailable: {0}")]
30 ProviderUnavailable(String),
31}
32
33impl From<AuthError> for CamelError {
34 fn from(e: AuthError) -> Self {
35 match e {
36 AuthError::Unauthenticated(s) => CamelError::Unauthenticated(s),
37 AuthError::TokenExpired => CamelError::Unauthenticated("token expired".into()),
38 AuthError::TokenInvalid(s) => CamelError::Unauthenticated(s),
39 AuthError::Unauthorized(s) => CamelError::Unauthorized(s),
40 AuthError::AuthenticationFailed(s) => CamelError::Unauthenticated(s),
41 AuthError::AuthorizationDenied(s) => CamelError::Unauthorized(s),
42 AuthError::ProviderUnavailable(s) => {
43 CamelError::ProcessorError(format!("auth provider unavailable: {s}"))
44 }
45 AuthError::ConfigError(s) => CamelError::Config(s),
46 AuthError::Config(s) => CamelError::Config(s),
47 }
48 }
49}
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54
55 #[test]
56 fn auth_error_maps_unauthenticated() {
57 let err = AuthError::Unauthenticated("bad token".into());
58 let camel_err: CamelError = err.into();
59 assert!(matches!(camel_err, CamelError::Unauthenticated(s) if s.contains("bad token")));
60 }
61
62 #[test]
63 fn auth_error_maps_token_expired() {
64 let err = AuthError::TokenExpired;
65 let camel_err: CamelError = err.into();
66 assert!(matches!(camel_err, CamelError::Unauthenticated(_)));
67 }
68
69 #[test]
70 fn auth_error_maps_token_invalid() {
71 let err = AuthError::TokenInvalid("bad sig".into());
72 let camel_err: CamelError = err.into();
73 assert!(matches!(camel_err, CamelError::Unauthenticated(s) if s.contains("bad sig")));
74 }
75
76 #[test]
77 fn auth_error_maps_unauthorized() {
78 let err = AuthError::Unauthorized("no admin".into());
79 let camel_err: CamelError = err.into();
80 assert!(matches!(camel_err, CamelError::Unauthorized(s) if s.contains("no admin")));
81 }
82
83 #[test]
84 fn auth_error_maps_provider_unavailable() {
85 let err = AuthError::ProviderUnavailable("jwks down".into());
86 let camel_err: CamelError = err.into();
87 assert!(matches!(camel_err, CamelError::ProcessorError(s) if s.contains("jwks down")));
88 }
89
90 #[test]
91 fn auth_error_maps_config_error() {
92 let err = AuthError::ConfigError("bad config".into());
93 let camel_err: CamelError = err.into();
94 assert!(matches!(camel_err, CamelError::Config(s) if s.contains("bad config")));
95 }
96}