• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 
16 #ifndef ACCESSTOKEN_MANAGER_CLIENT_H
17 #define ACCESSTOKEN_MANAGER_CLIENT_H
18 
19 #include <map>
20 #include <mutex>
21 #include <string>
22 #include <unordered_set>
23 #include <vector>
24 
25 #include "access_token.h"
26 #include "atm_tools_param_info.h"
27 #include "accesstoken_death_recipient.h"
28 #include "hap_token_info.h"
29 #include "iaccess_token_manager.h"
30 #include "nocopyable.h"
31 #include "permission_def.h"
32 #include "permission_grant_info.h"
33 #include "accesstoken_callbacks.h"
34 #include "permission_state_full.h"
35 #include "perm_state_change_callback_customize.h"
36 #ifdef TOKEN_SYNC_ENABLE
37 #include "token_sync_kit_interface.h"
38 #endif // TOKEN_SYNC_ENABLE
39 
40 namespace OHOS {
41 namespace Security {
42 namespace AccessToken {
43 class AccessTokenManagerClient final {
44 public:
45     static AccessTokenManagerClient& GetInstance();
46 
47     virtual ~AccessTokenManagerClient();
48 
49     PermUsedTypeEnum GetPermissionUsedType(AccessTokenID tokenID, const std::string& permissionName);
50     int VerifyAccessToken(AccessTokenID tokenID, const std::string& permissionName);
51     int VerifyAccessToken(AccessTokenID tokenID,
52         const std::vector<std::string>& permissionList, std::vector<int32_t>& permStateList);
53     int GetDefPermission(const std::string& permissionName, PermissionDef& permissionDefResult);
54     int GetReqPermissions(
55         AccessTokenID tokenID, std::vector<PermissionStateFull>& reqPermList, bool isSystemGrant);
56     int GetPermissionFlag(AccessTokenID tokenID, const std::string& permissionName, uint32_t& flag);
57     int32_t SetPermissionRequestToggleStatus(const std::string& permissionName, uint32_t status, int32_t userID);
58     int32_t GetPermissionRequestToggleStatus(const std::string& permissionName, uint32_t& status, int32_t userID);
59     int32_t RequestAppPermOnSetting(AccessTokenID tokenID);
60     int32_t GetSelfPermissionStatus(const std::string& permissionName, PermissionOper& status);
61     PermissionOper GetSelfPermissionsState(std::vector<PermissionListState>& permList,
62         PermissionGrantInfo& info);
63     int32_t GetPermissionsStatus(AccessTokenID tokenID, std::vector<PermissionListState>& permList);
64     int GrantPermission(AccessTokenID tokenID, const std::string& permissionName, uint32_t flag);
65     int RevokePermission(AccessTokenID tokenID, const std::string& permissionName, uint32_t flag);
66     int GrantPermissionForSpecifiedTime(
67         AccessTokenID tokenID, const std::string& permissionName, uint32_t onceTime);
68     int ClearUserGrantedPermissionState(AccessTokenID tokenID);
69     int32_t SetPermissionStatusWithPolicy(
70         AccessTokenID tokenID, const std::vector<std::string>& permissionList, int32_t status, uint32_t flag);
71     AccessTokenIDEx AllocHapToken(const HapInfoParams& info, const HapPolicy& policy);
72     int32_t InitHapToken(const HapInfoParams& info, HapPolicy& policy,
73         AccessTokenIDEx& fullTokenId, HapInfoCheckResult& result);
74     int DeleteToken(AccessTokenID tokenID);
75     ATokenTypeEnum GetTokenType(AccessTokenID tokenID);
76     AccessTokenIDEx GetHapTokenID(int32_t userID, const std::string& bundleName, int32_t instIndex);
77     AccessTokenID AllocLocalTokenID(const std::string& remoteDeviceID, AccessTokenID remoteTokenID);
78     int32_t UpdateHapToken(AccessTokenIDEx& tokenIdEx, const UpdateHapInfoParams& info,
79         const HapPolicy& policy, HapInfoCheckResult& result);
80     int32_t GetTokenIDByUserID(int32_t userID, std::unordered_set<AccessTokenID>& tokenList);
81     int GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfo& hapTokenInfoRes);
82     int GetNativeTokenInfo(AccessTokenID tokenID, NativeTokenInfo& nativeTokenInfoRes);
83 #ifndef ATM_BUILD_VARIANT_USER_ENABLE
84     int32_t ReloadNativeTokenInfo();
85 #endif
86     int GetHapTokenInfoExtension(AccessTokenID tokenID, HapTokenInfoExt& info);
87     AccessTokenID GetNativeTokenId(const std::string& processName);
88     int32_t RegisterPermStateChangeCallback(
89         const std::shared_ptr<PermStateChangeCallbackCustomize>& customizedCb, RegisterPermChangeType type);
90     int32_t UnRegisterPermStateChangeCallback(
91         const std::shared_ptr<PermStateChangeCallbackCustomize>& customizedCb, RegisterPermChangeType type);
92 
93 #ifdef TOKEN_SYNC_ENABLE
94     int GetHapTokenInfoFromRemote(AccessTokenID tokenID, HapTokenInfoForSync& hapSync);
95     int SetRemoteHapTokenInfo(const std::string& deviceID, const HapTokenInfoForSync& hapSync);
96     int DeleteRemoteToken(const std::string& deviceID, AccessTokenID tokenID);
97     AccessTokenID GetRemoteNativeTokenID(const std::string& deviceID, AccessTokenID tokenID);
98     int DeleteRemoteDeviceTokens(const std::string& deviceID);
99     int32_t RegisterTokenSyncCallback(const std::shared_ptr<TokenSyncKitInterface>& syncCallback);
100     int32_t UnRegisterTokenSyncCallback();
101 #endif
102 
103     int32_t GetKernelPermissions(
104         AccessTokenID tokenId, std::vector<PermissionWithValue>& kernelPermList);
105     int32_t GetReqPermissionByName(
106         AccessTokenID tokenId, const std::string& permissionName, std::string& value);
107     void DumpTokenInfo(const AtmToolsParamInfo& info, std::string& dumpInfo);
108     int32_t GetVersion(uint32_t& version);
109     void OnRemoteDiedHandle();
110     int32_t SetPermDialogCap(const HapBaseInfo& hapBaseInfo, bool enable);
111     void GetPermissionManagerInfo(PermissionGrantInfo& info);
112     int32_t InitUserPolicy(const std::vector<UserState>& userList, const std::vector<std::string>& permList);
113     int32_t UpdateUserPolicy(const std::vector<UserState>& userList);
114     int32_t ClearUserPolicy();
115 #ifdef SECURITY_COMPONENT_ENHANCE_ENABLE
116     int32_t RegisterSecCompEnhance(const SecCompEnhanceData& enhance);
117     int32_t UpdateSecCompEnhance(int32_t pid, uint32_t seqNum);
118     int32_t GetSecCompEnhance(int32_t pid, SecCompEnhanceData& enhance);
119 #endif // SECURITY_COMPONENT_ENHANCE_ENABLE
120     bool IsToastShownNeeded(int32_t pid);
121 
122 private:
123     AccessTokenManagerClient();
124     int32_t CreatePermStateChangeCallback(
125         const std::shared_ptr<PermStateChangeCallbackCustomize>& customizedCb,
126         sptr<PermissionStateChangeCallback>& callback);
127 
128     DISALLOW_COPY_AND_MOVE(AccessTokenManagerClient);
129     std::mutex proxyMutex_;
130     sptr<IAccessTokenManager> proxy_ = nullptr;
131     sptr<AccessTokenDeathRecipient> serviceDeathObserver_ = nullptr;
132     void InitProxy();
133     sptr<IAccessTokenManager> GetProxy();
134     void ReleaseProxy();
135     std::mutex callbackMutex_;
136     std::map<std::shared_ptr<PermStateChangeCallbackCustomize>, sptr<PermissionStateChangeCallback>> callbackMap_;
137 
138 #ifdef TOKEN_SYNC_ENABLE
139     std::mutex tokenSyncCallbackMutex_;
140     std::shared_ptr<TokenSyncKitInterface> syncCallbackImpl_ = nullptr;
141     sptr<TokenSyncCallback> tokenSyncCallback_ = nullptr;
142 #endif // TOKEN_SYNC_ENABLE
143 };
144 } // namespace AccessToken
145 } // namespace Security
146 } // namespace OHOS
147 #endif // ACCESSTOKEN_MANAGER_CLIENT_H
148