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