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) => CamelError::AuthProviderUnavailable(s),
43 AuthError::ConfigError(s) => CamelError::Config(s),
44 AuthError::Config(s) => CamelError::Config(s),
45 }
46 }
47}
48
49#[cfg(test)]
50mod tests {
51 use super::*;
52
53 #[test]
54 fn auth_error_maps_unauthenticated() {
55 let err = AuthError::Unauthenticated("bad token".into());
56 let camel_err: CamelError = err.into();
57 assert!(matches!(camel_err, CamelError::Unauthenticated(s) if s.contains("bad token")));
58 }
59
60 #[test]
61 fn auth_error_maps_token_expired() {
62 let err = AuthError::TokenExpired;
63 let camel_err: CamelError = err.into();
64 assert!(matches!(camel_err, CamelError::Unauthenticated(_)));
65 }
66
67 #[test]
68 fn auth_error_maps_token_invalid() {
69 let err = AuthError::TokenInvalid("bad sig".into());
70 let camel_err: CamelError = err.into();
71 assert!(matches!(camel_err, CamelError::Unauthenticated(s) if s.contains("bad sig")));
72 }
73
74 #[test]
75 fn auth_error_maps_unauthorized() {
76 let err = AuthError::Unauthorized("no admin".into());
77 let camel_err: CamelError = err.into();
78 assert!(matches!(camel_err, CamelError::Unauthorized(s) if s.contains("no admin")));
79 }
80
81 #[test]
82 fn auth_error_maps_provider_unavailable() {
83 let err = AuthError::ProviderUnavailable("jwks down".into());
84 let camel_err: CamelError = err.into();
85 assert!(
86 matches!(camel_err, CamelError::AuthProviderUnavailable(s) if s.contains("jwks down"))
87 );
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}