Skip to main content

rocketmq_auth/authorization/model/
request_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 rocketmq_security_api::Action;
16
17use crate::authentication::model::subject::Subject;
18use crate::authorization::context::default_authorization_context::SubjectWrapper;
19use crate::authorization::model::resource::Resource;
20
21#[derive(Debug, Clone, Default)]
22pub struct RequestContext {
23    subject: Option<SubjectWrapper>,
24    resource: Option<Resource>,
25    action: Option<Action>,
26    source_ip: Option<String>,
27}
28
29impl RequestContext {
30    pub fn subject(&self) -> Option<&SubjectWrapper> {
31        self.subject.as_ref()
32    }
33
34    pub fn set_subject<S: Subject>(&mut self, subject: &S) {
35        self.subject = Some(SubjectWrapper::new(
36            subject.subject_key().to_string(),
37            subject.subject_type(),
38        ));
39    }
40
41    pub fn set_subject_key(
42        &mut self,
43        subject_key: impl Into<String>,
44        subject_type: crate::authentication::enums::subject_type::SubjectType,
45    ) {
46        self.subject = Some(SubjectWrapper::new(subject_key.into(), subject_type));
47    }
48
49    pub fn resource(&self) -> Option<&Resource> {
50        self.resource.as_ref()
51    }
52
53    pub fn set_resource(&mut self, resource: Resource) {
54        self.resource = Some(resource);
55    }
56
57    pub fn action(&self) -> Option<Action> {
58        self.action
59    }
60
61    pub fn set_action(&mut self, action: Action) {
62        self.action = Some(action);
63    }
64
65    pub fn source_ip(&self) -> Option<&str> {
66        self.source_ip.as_deref()
67    }
68
69    pub fn set_source_ip(&mut self, source_ip: impl Into<String>) {
70        self.source_ip = Some(source_ip.into());
71    }
72}
73
74#[cfg(test)]
75mod tests {
76    use super::*;
77    use crate::authentication::model::user::User;
78
79    #[test]
80    fn request_context_stores_java_model_fields() {
81        let user = User::of("alice");
82        let resource = Resource::of_topic("topic-a");
83        let mut context = RequestContext::default();
84
85        context.set_subject(&user);
86        context.set_resource(resource.clone());
87        context.set_action(Action::Pub);
88        context.set_source_ip("127.0.0.1");
89
90        assert_eq!(
91            context.subject().map(|subject| subject.subject_key()),
92            Some("User:alice")
93        );
94        assert_eq!(context.resource(), Some(&resource));
95        assert_eq!(context.action(), Some(Action::Pub));
96        assert_eq!(context.source_ip(), Some("127.0.0.1"));
97    }
98}