• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
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 
16 //! This module implements the capability of processing the identity information of the Asset caller.
17 
18 use crate::{process_info::ProcessInfoDetail, OwnerType, ProcessInfo, GROUP_SEPARATOR};
19 use asset_definition::Value;
20 
21 /// The identity of calling process.
22 #[derive(Clone)]
23 #[derive(PartialEq, Eq)]
24 pub struct Group {
25     /// The developer id.
26     pub developer_id: Vec<u8>,
27     /// The group id.
28     pub group_id: Vec<u8>,
29 }
30 
31 /// The identity of calling process.
32 #[derive(Clone)]
33 #[derive(PartialEq, Eq)]
34 pub struct CallingInfo {
35     user_id: i32,
36     owner_type: OwnerType,
37     owner_info: Vec<u8>,
38     group: Option<Group>,
39 }
40 
41 impl CallingInfo {
42     /// Build identity of current process.
new_self() -> Self43     pub fn new_self() -> Self {
44         Self::new(0, OwnerType::Native, "asset_service_8100".as_bytes().to_vec(), None)
45     }
46 
47     /// Build identity of part process info.
new_part_info(owner_info: Vec<u8>, owner_type: OwnerType) -> Self48     pub fn new_part_info(owner_info: Vec<u8>, owner_type: OwnerType) -> Self {
49         Self::new(0, owner_type, owner_info, None)
50     }
51 
52     /// Build identity of the specified owner.
new(user_id: i32, owner_type: OwnerType, owner_info: Vec<u8>, group: Option<Group>) -> Self53     pub fn new(user_id: i32, owner_type: OwnerType, owner_info: Vec<u8>, group: Option<Group>) -> Self {
54         Self { user_id, owner_type, owner_info, group }
55     }
56 
57     /// Build a instance of CallingInfo.
build(specific_user_id: Option<Value>, process_info: &ProcessInfo) -> Self58     pub fn build(specific_user_id: Option<Value>, process_info: &ProcessInfo) -> Self {
59         let mut user_id = process_info.user_id;
60         if let Some(Value::Number(specific_user_id)) = specific_user_id {
61             user_id = specific_user_id;
62         };
63 
64         let mut owner_info = Vec::new();
65         match &process_info.process_info_detail {
66             ProcessInfoDetail::Hap(hap_info) => {
67                 owner_info.append(&mut hap_info.app_id.clone());
68                 owner_info.append(&mut "_".to_string().as_bytes().to_vec());
69                 owner_info.append(&mut hap_info.app_index.to_string().as_bytes().to_vec());
70                 let group = match (&hap_info.developer_id, &hap_info.group_id) {
71                     (Some(developer_id), Some(group_id)) => {
72                         Some(Group { developer_id: developer_id.to_vec(), group_id: group_id.to_vec() })
73                     },
74                     _ => None,
75                 };
76                 CallingInfo { user_id: user_id as i32, owner_type: process_info.owner_type, owner_info, group }
77             },
78             ProcessInfoDetail::Native(native_info) => {
79                 owner_info.append(&mut process_info.process_name.clone());
80                 owner_info.append(&mut "_".to_string().as_bytes().to_vec());
81                 owner_info.append(&mut native_info.uid.to_string().as_bytes().to_vec());
82                 CallingInfo { user_id: user_id as i32, owner_type: process_info.owner_type, owner_info, group: None }
83             },
84         }
85     }
86 
87     /// Get owner type of calling.
owner_type(&self) -> u3288     pub fn owner_type(&self) -> u32 {
89         self.owner_type as u32
90     }
91 
92     /// Get owner type enum of calling.
owner_type_enum(&self) -> OwnerType93     pub fn owner_type_enum(&self) -> OwnerType {
94         self.owner_type
95     }
96 
97     /// Get owner info of calling.
owner_info(&self) -> &Vec<u8>98     pub fn owner_info(&self) -> &Vec<u8> {
99         &self.owner_info
100     }
101 
102     /// Get user id of calling.
user_id(&self) -> i32103     pub fn user_id(&self) -> i32 {
104         self.user_id
105     }
106 
107     /// Get developer id of calling.
developer_id(&self) -> Option<&Vec<u8>>108     pub fn developer_id(&self) -> Option<&Vec<u8>> {
109         match &self.group {
110             Some(group) => Some(&group.developer_id),
111             _ => None,
112         }
113     }
114 
115     /// Get group id of calling.
group_id(&self) -> Option<&Vec<u8>>116     pub fn group_id(&self) -> Option<&Vec<u8>> {
117         match &self.group {
118             Some(group) => Some(&group.group_id),
119             _ => None,
120         }
121     }
122 
123     /// Get group (developer id + group id) of calling.
group(&self) -> Option<Vec<u8>>124     pub fn group(&self) -> Option<Vec<u8>> {
125         match &self.group {
126             Some(group) => {
127                 let mut group_vec: Vec<u8> = Vec::new();
128                 group_vec.extend(group.developer_id.clone());
129                 group_vec.push(GROUP_SEPARATOR as u8);
130                 group_vec.extend(group.group_id.clone());
131                 Some(group_vec)
132             },
133             _ => None,
134         }
135     }
136 
137     /// Get appindex.
app_index(&self) -> u32138     pub fn app_index(&self) -> u32 {
139         match self.owner_type_enum() {
140             OwnerType::Hap | OwnerType::HapGroup => {
141                 let owner_info_str = String::from_utf8_lossy(self.owner_info()).to_string();
142                 let owner_info_vec: Vec<_> = owner_info_str.split('_').collect();
143                 match owner_info_vec.last().unwrap().parse::<u32>() {
144                     Ok(num) => num,
145                     Err(_e) => 0,
146                 }
147             },
148             OwnerType::Native => 0,
149         }
150     }
151 }
152 
153 #[cfg(test)]
154 use crate::process_info::{HapInfo, NativeInfo};
155 
156 #[test]
test_build_callig_info_specific_and_hap()157 fn test_build_callig_info_specific_and_hap() {
158     let specific_user_id = 100;
159     let process_name = "test_process".as_bytes().to_vec();
160     let app_id = "test_app_id".as_bytes().to_vec();
161     let app_index = 0;
162     let group_id = None;
163     let developer_id = None;
164     let process_info = ProcessInfo {
165         user_id: 0,
166         owner_type: OwnerType::Hap,
167         process_name,
168         process_info_detail: ProcessInfoDetail::Hap(HapInfo { app_id, app_index, group_id, developer_id }),
169     };
170 
171     let calling_info = CallingInfo::build(Some(Value::Number(specific_user_id)), &process_info);
172     assert_eq!(calling_info.user_id(), specific_user_id as i32);
173 
174     let owner_info = "test_app_id_0".as_bytes().to_vec();
175     assert_eq!(calling_info.owner_info(), &owner_info);
176 }
177 
178 #[test]
test_build_callig_info_hap()179 fn test_build_callig_info_hap() {
180     let process_name = "test_process".as_bytes().to_vec();
181     let app_id = "test_app_id".as_bytes().to_vec();
182     let app_index = 0;
183     let user_id = 0;
184     let group_id = None;
185     let developer_id = None;
186     let process_info = ProcessInfo {
187         user_id,
188         owner_type: OwnerType::Hap,
189         process_name,
190         process_info_detail: ProcessInfoDetail::Hap(HapInfo { app_id, app_index, group_id, developer_id }),
191     };
192 
193     let calling_info = CallingInfo::build(None, &process_info);
194     assert_eq!(calling_info.user_id(), user_id as i32);
195     let owner_info = "test_app_id_0".as_bytes().to_vec();
196     assert_eq!(calling_info.owner_info(), &owner_info);
197 }
198 
199 #[test]
test_build_callig_info_native()200 fn test_build_callig_info_native() {
201     let process_name = "test_process".as_bytes().to_vec();
202     let user_id = 0;
203     let uid = 999;
204     let process_info = ProcessInfo {
205         user_id,
206         owner_type: OwnerType::Native,
207         process_name,
208         process_info_detail: ProcessInfoDetail::Native(NativeInfo { uid }),
209     };
210 
211     let calling_info = CallingInfo::build(None, &process_info);
212     assert_eq!(calling_info.user_id(), user_id as i32);
213     let owner_info = "test_process_999".as_bytes().to_vec();
214     assert_eq!(calling_info.owner_info(), &owner_info);
215 }
216 
217 #[test]
test_build_callig_info_specific_and_native()218 fn test_build_callig_info_specific_and_native() {
219     let specific_user_id = 100;
220     let process_name = "test_process".as_bytes().to_vec();
221     let user_id = 0;
222     let uid = 999;
223     let process_info = ProcessInfo {
224         user_id,
225         owner_type: OwnerType::Native,
226         process_name,
227         process_info_detail: ProcessInfoDetail::Native(NativeInfo { uid }),
228     };
229 
230     let calling_info = CallingInfo::build(Some(Value::Number(specific_user_id)), &process_info);
231 
232     assert_eq!(calling_info.user_id(), specific_user_id as i32);
233     let owner_info = "test_process_999".as_bytes().to_vec();
234     assert_eq!(calling_info.owner_info(), &owner_info);
235 }
236