1 /*
2  * Copyright (c) 2021-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 #ifndef ACCESSTOKEN_MANAGER_CLIENT_H
17 #define ACCESSTOKEN_MANAGER_CLIENT_H
18 
19 #include <map>
20 #include <mutex>
21 #include <string>
22 #include <vector>
23 
24 #include "access_token.h"
25 #include "atm_tools_param_info.h"
26 #include "accesstoken_death_recipient.h"
27 #include "hap_base_info_parcel.h"
28 #include "hap_info_parcel.h"
29 #include "hap_policy_parcel.h"
30 #include "hap_token_info.h"
31 #include "i_accesstoken_manager.h"
32 #include "native_token_info.h"
33 #include "nocopyable.h"
34 #include "permission_def.h"
35 #include "permission_grant_info.h"
36 #include "accesstoken_callbacks.h"
37 #include "permission_state_full.h"
38 #include "perm_state_change_callback_customize.h"
39 #ifdef TOKEN_SYNC_ENABLE
40 #include "token_sync_kit_interface.h"
41 #endif // TOKEN_SYNC_ENABLE
42 
43 namespace OHOS {
44 namespace Security {
45 namespace AccessToken {
46 class AccessTokenManagerClient final {
47 public:
48     static AccessTokenManagerClient& GetInstance();
49 
50     virtual ~AccessTokenManagerClient();
51 
52     PermUsedTypeEnum GetUserGrantedPermissionUsedType(AccessTokenID tokenID, const std::string& permissionName);
53     int VerifyAccessToken(AccessTokenID tokenID, const std::string& permissionName);
54     int GetDefPermission(const std::string& permissionName, PermissionDef& permissionDefResult);
55     int GetDefPermissions(AccessTokenID tokenID, std::vector<PermissionDef>& permList);
56     int GetReqPermissions(
57         AccessTokenID tokenID, std::vector<PermissionStateFull>& reqPermList, bool isSystemGrant);
58     int GetPermissionFlag(AccessTokenID tokenID, const std::string& permissionName, uint32_t& flag);
59     int32_t SetPermissionRequestToggleStatus(const std::string& permissionName, uint32_t status, int32_t userID);
60     int32_t GetPermissionRequestToggleStatus(const std::string& permissionName, uint32_t& status, int32_t userID);
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     AccessTokenIDEx AllocHapToken(const HapInfoParams& info, const HapPolicyParams& policy);
70     int32_t InitHapToken(const HapInfoParams& info, HapPolicyParams& policy, AccessTokenIDEx& fullTokenId);
71     int DeleteToken(AccessTokenID tokenID);
72     ATokenTypeEnum GetTokenType(AccessTokenID tokenID);
73     int CheckNativeDCap(AccessTokenID tokenID, const std::string& dcap);
74     AccessTokenIDEx GetHapTokenID(int32_t userID, const std::string& bundleName, int32_t instIndex);
75     AccessTokenID AllocLocalTokenID(const std::string& remoteDeviceID, AccessTokenID remoteTokenID);
76     int32_t UpdateHapToken(AccessTokenIDEx& tokenIdEx, const UpdateHapInfoParams& info, const HapPolicyParams& policy);
77     int GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfo& hapTokenInfoRes);
78     int GetNativeTokenInfo(AccessTokenID tokenID, NativeTokenInfo& nativeTokenInfoRes);
79 #ifndef ATM_BUILD_VARIANT_USER_ENABLE
80     int32_t ReloadNativeTokenInfo();
81 #endif
82     AccessTokenID GetNativeTokenId(const std::string& processName);
83     int32_t RegisterPermStateChangeCallback(
84         const std::shared_ptr<PermStateChangeCallbackCustomize>& customizedCb);
85     int32_t UnRegisterPermStateChangeCallback(
86         const std::shared_ptr<PermStateChangeCallbackCustomize>& customizedCb);
87 
88 #ifdef TOKEN_SYNC_ENABLE
89     int GetHapTokenInfoFromRemote(AccessTokenID tokenID, HapTokenInfoForSync& hapSync);
90     int SetRemoteHapTokenInfo(const std::string& deviceID, const HapTokenInfoForSync& hapSync);
91     int DeleteRemoteToken(const std::string& deviceID, AccessTokenID tokenID);
92     AccessTokenID GetRemoteNativeTokenID(const std::string& deviceID, AccessTokenID tokenID);
93     int DeleteRemoteDeviceTokens(const std::string& deviceID);
94     int32_t RegisterTokenSyncCallback(const std::shared_ptr<TokenSyncKitInterface>& syncCallback);
95     int32_t UnRegisterTokenSyncCallback();
96 #endif
97 
98     void DumpTokenInfo(const AtmToolsParamInfo& info, std::string& dumpInfo);
99     int32_t GetVersion(uint32_t& version);
100     void OnRemoteDiedHandle();
101     int32_t SetPermDialogCap(const HapBaseInfo& hapBaseInfo, bool enable);
102     void GetPermissionManagerInfo(PermissionGrantInfo& info);
103 
104 private:
105     AccessTokenManagerClient();
106     int32_t CreatePermStateChangeCallback(
107         const std::shared_ptr<PermStateChangeCallbackCustomize>& customizedCb,
108         sptr<PermissionStateChangeCallback>& callback);
109 
110     DISALLOW_COPY_AND_MOVE(AccessTokenManagerClient);
111     std::mutex proxyMutex_;
112     sptr<IAccessTokenManager> proxy_ = nullptr;
113     sptr<AccessTokenDeathRecipient> serviceDeathObserver_ = nullptr;
114     void InitProxy();
115     sptr<IAccessTokenManager> GetProxy();
116     void ReleaseProxy();
117     std::mutex callbackMutex_;
118     std::map<std::shared_ptr<PermStateChangeCallbackCustomize>, sptr<PermissionStateChangeCallback>> callbackMap_;
119 
120 #ifdef TOKEN_SYNC_ENABLE
121     std::mutex tokenSyncCallbackMutex_;
122     std::shared_ptr<TokenSyncKitInterface> syncCallbackImpl_ = nullptr;
123     sptr<TokenSyncCallback> tokenSyncCallback_ = nullptr;
124 #endif // TOKEN_SYNC_ENABLE
125 };
126 } // namespace AccessToken
127 } // namespace Security
128 } // namespace OHOS
129 #endif // ACCESSTOKEN_MANAGER_CLIENT_H
130