Skip to main content

rocketmq_error/
auth_error.rs

1// Copyright 2023 The RocketMQ Rust Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Authentication error types.
16
17use thiserror::Error;
18
19/// Authentication error types.
20///
21/// This enum represents various authentication errors that can occur during
22/// the authentication process, including context building, credential validation,
23/// and signature verification.
24#[derive(Debug, Clone, Error)]
25pub enum AuthError {
26    /// Missing DateTime header in gRPC metadata
27    #[error("Missing DateTime header: {0}")]
28    MissingDateTime(String),
29
30    /// Invalid Authorization header format
31    #[error("Invalid Authorization header: {0}")]
32    InvalidAuthorizationHeader(String),
33
34    /// Invalid credential format or content
35    #[error("Invalid credential: {0}")]
36    InvalidCredential(String),
37
38    /// Invalid hex-encoded signature
39    #[error("Invalid hex signature: {0}")]
40    InvalidHexSignature(String),
41
42    /// Generic authentication context creation error
43    #[error("Failed to create authentication context: {0}")]
44    ContextCreationError(String),
45
46    /// User authentication failed
47    #[error("Authentication failed: {0}")]
48    AuthenticationFailed(String),
49
50    /// User not found
51    #[error("User not found: {0}")]
52    UserNotFound(String),
53
54    /// Invalid signature
55    #[error("Invalid signature: {0}")]
56    InvalidSignature(String),
57
58    /// User status is not valid (disabled, deleted, etc.)
59    #[error("Invalid user status: {0}")]
60    InvalidUserStatus(String),
61
62    /// Generic error with custom message
63    #[error("Authentication error: {0}")]
64    Other(String),
65}
66
67impl From<String> for AuthError {
68    fn from(msg: String) -> Self {
69        AuthError::Other(msg)
70    }
71}
72
73impl From<&str> for AuthError {
74    fn from(msg: &str) -> Self {
75        AuthError::Other(msg.to_string())
76    }
77}
78
79#[cfg(test)]
80mod tests {
81    use super::*;
82
83    #[test]
84    fn test_auth_error_variants() {
85        let errors = vec![
86            AuthError::AuthenticationFailed("could not authenticate".to_string()),
87            AuthError::ContextCreationError("could not create context".to_string()),
88            AuthError::InvalidAuthorizationHeader("invalid authorization header".to_string()),
89            AuthError::InvalidCredential("invalid credential".to_string()),
90            AuthError::InvalidHexSignature("invalid hex signature".to_string()),
91            AuthError::InvalidSignature("invalid signature".to_string()),
92            AuthError::InvalidUserStatus("invalid user status".to_string()),
93            AuthError::MissingDateTime("missing date time".to_string()),
94            AuthError::Other("other error".to_string()),
95            AuthError::UserNotFound("user not found".to_string()),
96        ];
97
98        for error in errors {
99            let _msg = format!("{}", error);
100            let _debug = format!("{:?}", error);
101        }
102    }
103
104    #[test]
105    fn test_from_string_creates_other() {
106        let error: AuthError = String::from("custom error").into();
107
108        match error {
109            AuthError::Other(msg) => assert_eq!(msg, "custom error"),
110            _ => panic!("Expected AuthError::Other"),
111        }
112    }
113
114    #[test]
115    fn test_from_str_creates_other() {
116        let error: AuthError = "custom error".into();
117
118        match error {
119            AuthError::Other(msg) => assert_eq!(msg, "custom error"),
120            _ => panic!("Expected AuthError::Other"),
121        }
122    }
123}