Skip to main content

rocketmq_auth/authentication/strategy/
allow_all.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//! Allow-all authentication strategy.
16//!
17//! This strategy allows all authentication attempts to succeed.
18//! It is intended for use in development and testing environments only.
19//!
20//! **WARNING: DO NOT USE IN PRODUCTION!**
21//!
22//! This strategy provides no security and should never be used in production
23//! environments. It is provided solely for development convenience and testing.
24
25use crate::authentication::strategy::AuthenticationFuture;
26use crate::authentication::strategy::AuthenticationStrategy;
27use crate::authorization::context::authentication_context::AuthenticationContext;
28
29/// Authentication strategy that allows all requests.
30///
31/// This strategy unconditionally succeeds for all authentication attempts,
32/// regardless of credentials or context. It is designed for:
33/// - Development environments where authentication is not required
34/// - Testing scenarios where authentication should be bypassed
35/// - Debugging authentication-related issues
36///
37/// # Security Warning
38///
39/// **This strategy provides NO security whatsoever!**
40///
41/// Never use this strategy in production environments. All authentication
42/// requests will succeed, effectively disabling authentication entirely.
43///
44/// # Examples
45///
46/// ```rust,ignore
47/// use rocketmq_auth::AllowAllAuthenticationStrategy;
48/// use rocketmq_auth::AuthenticationStrategy;
49/// use rocketmq_auth::DefaultAuthenticationContext;
50///
51/// // Create the strategy
52/// let strategy = AllowAllAuthenticationStrategy::new();
53///
54/// // All authentication attempts succeed
55/// let context = DefaultAuthenticationContext::new();
56/// assert!(strategy.authenticate(&context).is_ok());
57/// ```
58#[derive(Debug, Clone, Default)]
59pub struct AllowAllAuthenticationStrategy;
60
61impl AllowAllAuthenticationStrategy {
62    /// Creates a new `AllowAllAuthenticationStrategy`.
63    ///
64    /// # Examples
65    ///
66    /// ```rust,ignore
67    /// use rocketmq_auth::AllowAllAuthenticationStrategy;
68    ///
69    /// let strategy = AllowAllAuthenticationStrategy::new();
70    /// ```
71    #[inline]
72    pub fn new() -> Self {
73        Self
74    }
75}
76
77impl AuthenticationStrategy for AllowAllAuthenticationStrategy {
78    /// Always returns `Ok(())`, allowing all authentication attempts.
79    ///
80    /// This implementation ignores the context entirely and always succeeds.
81    ///
82    /// # Arguments
83    ///
84    /// * `_context` - The authentication context (ignored)
85    ///
86    /// # Returns
87    ///
88    /// Always returns `Ok(())` indicating successful authentication.
89    fn authenticate<'a>(&'a self, _context: &'a dyn AuthenticationContext) -> AuthenticationFuture<'a> {
90        Box::pin(async { Ok(()) })
91    }
92}
93
94#[cfg(test)]
95mod tests {
96    use super::*;
97    use crate::authentication::context::default_authentication_context::DefaultAuthenticationContext;
98    use cheetah_string::CheetahString;
99    use std::sync::Arc;
100
101    #[tokio::test]
102    async fn test_new_creates_strategy() {
103        let strategy = AllowAllAuthenticationStrategy::new();
104        let context = DefaultAuthenticationContext::new();
105        assert!(strategy.authenticate(&context).await.is_ok());
106    }
107
108    #[tokio::test]
109    async fn test_default_creates_strategy() {
110        let strategy = AllowAllAuthenticationStrategy;
111        let context = DefaultAuthenticationContext::new();
112        assert!(strategy.authenticate(&context).await.is_ok());
113    }
114
115    #[tokio::test]
116    async fn test_allows_empty_context() {
117        let strategy = AllowAllAuthenticationStrategy::new();
118        let context = DefaultAuthenticationContext::new();
119
120        let result = strategy.authenticate(&context).await;
121        assert!(result.is_ok());
122    }
123
124    #[tokio::test]
125    async fn test_allows_context_with_credentials() {
126        let strategy = AllowAllAuthenticationStrategy::new();
127        let mut context = DefaultAuthenticationContext::new();
128        context.set_username(CheetahString::from("test_user"));
129        context.set_signature(CheetahString::from("invalid_signature"));
130
131        let result = strategy.authenticate(&context).await;
132        assert!(result.is_ok());
133    }
134
135    #[tokio::test]
136    async fn test_allows_context_with_invalid_data() {
137        let strategy = AllowAllAuthenticationStrategy::new();
138        let mut context = DefaultAuthenticationContext::new();
139        context.set_username(CheetahString::from(""));
140        context.set_content(vec![]);
141
142        let result = strategy.authenticate(&context).await;
143        assert!(result.is_ok());
144    }
145
146    #[test]
147    fn test_is_send_sync() {
148        fn assert_send_sync<T: Send + Sync>() {}
149        assert_send_sync::<AllowAllAuthenticationStrategy>();
150    }
151
152    #[tokio::test]
153    async fn test_can_be_used_as_trait_object() {
154        let strategy: Arc<dyn AuthenticationStrategy> = Arc::new(AllowAllAuthenticationStrategy::new());
155        let context = DefaultAuthenticationContext::new();
156
157        let result = strategy.authenticate(&context).await;
158        assert!(result.is_ok());
159    }
160
161    #[tokio::test]
162    async fn test_clone_works() {
163        let strategy1 = AllowAllAuthenticationStrategy::new();
164        let strategy2 = strategy1.clone();
165
166        let context = DefaultAuthenticationContext::new();
167        assert!(strategy1.authenticate(&context).await.is_ok());
168        assert!(strategy2.authenticate(&context).await.is_ok());
169    }
170
171    #[test]
172    fn test_debug_impl() {
173        let strategy = AllowAllAuthenticationStrategy::new();
174        let debug_string = format!("{:?}", strategy);
175        assert_eq!(debug_string, "AllowAllAuthenticationStrategy");
176    }
177}