• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #define LOG_TAG "CommonUtils"
16 #include "common_utils.h"
17 
18 #include "accesstoken_kit.h"
19 #include "config_factory.h"
20 #include "log_print.h"
21 #include "tokenid_kit.h"
22 
23 namespace OHOS::DataShare {
24 
GetFromSystemApp()25 bool& DataShareThreadLocal::GetFromSystemApp()
26 {
27     static thread_local bool isFromSystemApp = true;
28     return isFromSystemApp;
29 }
30 
SetFromSystemApp(bool isFromSystemApp)31 void DataShareThreadLocal::SetFromSystemApp(bool isFromSystemApp)
32 {
33     GetFromSystemApp() = isFromSystemApp;
34 }
35 
IsFromSystemApp()36 bool DataShareThreadLocal::IsFromSystemApp()
37 {
38     return GetFromSystemApp();
39 }
40 
CleanFromSystemApp()41 void DataShareThreadLocal::CleanFromSystemApp()
42 {
43     SetFromSystemApp(true);
44 }
45 
CheckSystemAbility(uint32_t tokenId)46 bool CheckSystemAbility(uint32_t tokenId)
47 {
48     Security::AccessToken::ATokenTypeEnum tokenType =
49         Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId);
50     return (tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE ||
51         tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_SHELL);
52 }
53 
54 // GetTokenType use tokenId, and IsSystemApp use fullTokenId, these are different
CheckSystemCallingPermission(uint32_t tokenId,uint64_t fullTokenId)55 bool CheckSystemCallingPermission(uint32_t tokenId, uint64_t fullTokenId)
56 {
57     if (CheckSystemAbility(tokenId)) {
58         return true;
59     }
60     // IsSystemAppByFullTokenID here is not IPC
61     return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId);
62 }
63 
64 } // namespace OHOS::DataShare
65