• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "accesstoken_death_recipient.h"
26 #include "hap_info_parcel.h"
27 #include "hap_policy_parcel.h"
28 #include "hap_token_info.h"
29 #include "i_accesstoken_manager.h"
30 #include "native_token_info.h"
31 #include "nocopyable.h"
32 #include "permission_def.h"
33 #include "permission_state_change_callback.h"
34 #include "permission_state_full.h"
35 #include "perm_state_change_callback_customize.h"
36 
37 namespace OHOS {
38 namespace Security {
39 namespace AccessToken {
40 class AccessTokenManagerClient final {
41 public:
42     static AccessTokenManagerClient& GetInstance();
43 
44     virtual ~AccessTokenManagerClient();
45 
46     int VerifyAccessToken(AccessTokenID tokenID, const std::string& permissionName);
47     int GetDefPermission(const std::string& permissionName, PermissionDef& permissionDefResult);
48     int GetDefPermissions(AccessTokenID tokenID, std::vector<PermissionDef>& permList);
49     int GetReqPermissions(
50         AccessTokenID tokenID, std::vector<PermissionStateFull>& reqPermList, bool isSystemGrant);
51     int GetPermissionFlag(AccessTokenID tokenID, const std::string& permissionName, int& flag);
52     PermissionOper GetSelfPermissionsState(std::vector<PermissionListState>& permList);
53     int GrantPermission(AccessTokenID tokenID, const std::string& permissionName, int flag);
54     int RevokePermission(AccessTokenID tokenID, const std::string& permissionName, int flag);
55     int ClearUserGrantedPermissionState(AccessTokenID tokenID);
56     AccessTokenIDEx AllocHapToken(const HapInfoParams& info, const HapPolicyParams& policy);
57     int DeleteToken(AccessTokenID tokenID);
58     ATokenTypeEnum GetTokenType(AccessTokenID tokenID);
59     int CheckNativeDCap(AccessTokenID tokenID, const std::string& dcap);
60     AccessTokenID GetHapTokenID(int userID, const std::string& bundleName, int instIndex);
61     AccessTokenID AllocLocalTokenID(const std::string& remoteDeviceID, AccessTokenID remoteTokenID);
62     int UpdateHapToken(
63         AccessTokenID tokenID, const std::string& appIDDesc, int32_t apiVersion, const HapPolicyParams& policy);
64     int GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfo& hapTokenInfoRes);
65     int GetNativeTokenInfo(AccessTokenID tokenID, NativeTokenInfo& nativeTokenInfoRes);
66     int32_t ReloadNativeTokenInfo();
67     AccessTokenID GetNativeTokenId(const std::string& processName);
68     int32_t RegisterPermStateChangeCallback(
69         const std::shared_ptr<PermStateChangeCallbackCustomize>& customizedCb);
70     int32_t UnRegisterPermStateChangeCallback(
71         const std::shared_ptr<PermStateChangeCallbackCustomize>& customizedCb);
72 
73 #ifdef TOKEN_SYNC_ENABLE
74     int GetHapTokenInfoFromRemote(AccessTokenID tokenID, HapTokenInfoForSync& hapSync);
75     int GetAllNativeTokenInfo(std::vector<NativeTokenInfoForSync>& nativeTokenInfosRes);
76     int SetRemoteHapTokenInfo(const std::string& deviceID, const HapTokenInfoForSync& hapSync);
77     int SetRemoteNativeTokenInfo(const std::string& deviceID,
78         const std::vector<NativeTokenInfoForSync>& nativeTokenInfoList);
79     int DeleteRemoteToken(const std::string& deviceID, AccessTokenID tokenID);
80     AccessTokenID GetRemoteNativeTokenID(const std::string& deviceID, AccessTokenID tokenID);
81     int DeleteRemoteDeviceTokens(const std::string& deviceID);
82 #endif
83 
84     void DumpTokenInfo(AccessTokenID tokenID, std::string& dumpInfo);
85     void OnRemoteDiedHandle();
86 
87 private:
88     AccessTokenManagerClient();
89     int32_t CreatePermStateChangeCallback(
90         const std::shared_ptr<PermStateChangeCallbackCustomize>& customizedCb,
91         sptr<PermissionStateChangeCallback>& callback);
92 
93     DISALLOW_COPY_AND_MOVE(AccessTokenManagerClient);
94     std::mutex proxyMutex_;
95     sptr<IAccessTokenManager> proxy_ = nullptr;
96     sptr<AccessTokenDeathRecipient> serviceDeathObserver_ = nullptr;
97     void InitProxy();
98     sptr<IAccessTokenManager> GetProxy();
99     std::mutex callbackMutex_;
100     std::map<std::shared_ptr<PermStateChangeCallbackCustomize>, sptr<PermissionStateChangeCallback>> callbackMap_;
101 };
102 } // namespace AccessToken
103 } // namespace Security
104 } // namespace OHOS
105 #endif // ACCESSTOKEN_MANAGER_CLIENT_H
106