Skip to main content

rocketmq_auth/authentication/context/
default_authentication_context.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
15use std::any::Any;
16
17use cheetah_string::CheetahString;
18
19use crate::authentication::context::base_authentication_context::BaseAuthenticationContext;
20use crate::authentication::AsAny;
21use crate::authorization::context::authentication_context::AuthenticationContext;
22
23#[derive(Debug, Default)]
24pub struct DefaultAuthenticationContext {
25    pub base: BaseAuthenticationContext,
26
27    username: Option<CheetahString>,
28    content: Option<Vec<u8>>,
29    signature: Option<CheetahString>,
30}
31
32impl DefaultAuthenticationContext {
33    pub fn new() -> Self {
34        Self::default()
35    }
36
37    pub fn username(&self) -> Option<&CheetahString> {
38        self.username.as_ref()
39    }
40
41    pub fn set_username(&mut self, username: CheetahString) {
42        self.username = Some(username);
43    }
44
45    pub fn content(&self) -> Option<&[u8]> {
46        self.content.as_deref()
47    }
48
49    pub fn set_content(&mut self, content: Vec<u8>) {
50        self.content = Some(content);
51    }
52
53    pub fn signature(&self) -> Option<&CheetahString> {
54        self.signature.as_ref()
55    }
56
57    pub fn set_signature(&mut self, signature: CheetahString) {
58        self.signature = Some(signature);
59    }
60}
61
62impl AsAny for DefaultAuthenticationContext {
63    fn as_any_mut(&mut self) -> &mut dyn Any {
64        self
65    }
66
67    fn as_any(&self) -> &dyn Any {
68        self
69    }
70}
71
72impl AuthenticationContext for DefaultAuthenticationContext {}
73
74#[cfg(test)]
75mod tests {
76    use super::*;
77
78    #[test]
79    fn test_default_authentication_context_default_and_new() {
80        let context = DefaultAuthenticationContext::default();
81        assert!(context.username().is_none());
82        assert!(context.content().is_none());
83        assert!(context.signature().is_none());
84
85        let context = DefaultAuthenticationContext::new();
86        assert!(context.username().is_none());
87        assert!(context.content().is_none());
88        assert!(context.signature().is_none());
89    }
90
91    #[test]
92    fn test_default_authentication_context_setters_and_getters() {
93        let mut context = DefaultAuthenticationContext::new();
94        let username = CheetahString::from("test_user");
95        let content = vec![1, 2, 3];
96        let signature = CheetahString::from("test_signature");
97
98        context.set_username(username.clone());
99        context.set_content(content.clone());
100        context.set_signature(signature.clone());
101
102        assert_eq!(context.username(), Some(&username));
103        assert_eq!(context.content(), Some(content.as_slice()));
104        assert_eq!(context.signature(), Some(&signature));
105    }
106
107    #[test]
108    fn test_default_authentication_context_as_any() {
109        let context = DefaultAuthenticationContext::new();
110        let any = context.as_any();
111        assert!(any.is::<DefaultAuthenticationContext>());
112
113        let mut context_mut = DefaultAuthenticationContext::new();
114        let any_mut = context_mut.as_any_mut();
115        assert!(any_mut.is::<DefaultAuthenticationContext>());
116    }
117}