• 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_TOKEN_INFO_MANAGER_H
17 #define ACCESSTOKEN_TOKEN_INFO_MANAGER_H
18 
19 #include <map>
20 #include <memory>
21 #include <vector>
22 
23 #include "access_token.h"
24 #include "hap_token_info.h"
25 #include "hap_token_info_inner.h"
26 #include "native_token_info.h"
27 #include "native_token_info_inner.h"
28 #include "nocopyable.h"
29 #include "rwlock.h"
30 #include "thread_pool.h"
31 
32 namespace OHOS {
33 namespace Security {
34 namespace AccessToken {
35 static const int UDID_MAX_LENGTH = 128; // udid/uuid max length
36 
37 class AccessTokenInfoManager final {
38 public:
39     static AccessTokenInfoManager& GetInstance();
40     ~AccessTokenInfoManager();
41     void Init();
42     std::shared_ptr<HapTokenInfoInner> GetHapTokenInfoInner(AccessTokenID id);
43     int GetHapTokenInfo(AccessTokenID tokenID, HapTokenInfo& infoParcel);
44     std::shared_ptr<NativeTokenInfoInner> GetNativeTokenInfoInner(AccessTokenID id);
45     int GetNativeTokenInfo(AccessTokenID tokenID, NativeTokenInfo& infoParcel);
46     std::shared_ptr<PermissionPolicySet> GetNativePermissionPolicySet(AccessTokenID id);
47     std::shared_ptr<PermissionPolicySet> GetHapPermissionPolicySet(AccessTokenID id);
48     int RemoveHapTokenInfo(AccessTokenID id);
49     int RemoveNativeTokenInfo(AccessTokenID id);
50     int CreateHapTokenInfo(const HapInfoParams& info, const HapPolicyParams& policy, AccessTokenIDEx& tokenIdEx);
51     int CheckNativeDCap(AccessTokenID tokenID, const std::string& dcap);
52     AccessTokenID GetHapTokenID(int userID, const std::string& bundleName, int instIndex);
53     AccessTokenID AllocLocalTokenID(const std::string& remoteDeviceID, AccessTokenID remoteTokenID);
54     void ProcessNativeTokenInfos(const std::vector<std::shared_ptr<NativeTokenInfoInner>>& tokenInfos);
55     int UpdateHapToken(
56         AccessTokenID tokenID, const std::string& appIDDesc, int32_t apiVersion, const HapPolicyParams& policy);
57     void DumpTokenInfo(AccessTokenID tokenID, std::string& dumpInfo);
58     void RefreshTokenInfoIfNeeded();
59     bool IsTokenIdExist(AccessTokenID id);
60     AccessTokenID GetNativeTokenId(const std::string& processName);
61 
62 #ifdef TOKEN_SYNC_ENABLE
63     /* tokensync needed */
64     int GetHapTokenSync(AccessTokenID tokenID, HapTokenInfoForSync& hapSync);
65     int GetHapTokenInfoFromRemote(AccessTokenID tokenID,
66         HapTokenInfoForSync& hapSync);
67     void GetAllNativeTokenInfo(std::vector<NativeTokenInfoForSync>& nativeTokenInfosRes);
68     int SetRemoteHapTokenInfo(const std::string& deviceID, HapTokenInfoForSync& hapSync);
69     int SetRemoteNativeTokenInfo(const std::string& deviceID,
70         std::vector<NativeTokenInfoForSync>& nativeTokenInfoList);
71     bool IsRemoteHapTokenValid(const std::string& deviceID, const HapTokenInfoForSync& hapSync);
72     int DeleteRemoteToken(const std::string& deviceID, AccessTokenID tokenID);
73     AccessTokenID GetRemoteNativeTokenID(const std::string& deviceID, AccessTokenID tokenID);
74     int DeleteRemoteDeviceTokens(const std::string& deviceID);
75     std::string GetUdidByNodeId(const std::string &nodeId);
76 #endif
77 
78 private:
79     AccessTokenInfoManager();
80     DISALLOW_COPY_AND_MOVE(AccessTokenInfoManager);
81 
82     void InitHapTokenInfos();
83     void InitNativeTokenInfos();
84     int AddHapTokenInfo(const std::shared_ptr<HapTokenInfoInner>& info);
85     int AddNativeTokenInfo(const std::shared_ptr<NativeTokenInfoInner>& info);
86     std::string GetHapUniqueStr(const std::shared_ptr<HapTokenInfoInner>& info) const;
87     std::string GetHapUniqueStr(const int& userID, const std::string& bundleName, const int& instIndex) const;
88     bool TryUpdateExistNativeToken(const std::shared_ptr<NativeTokenInfoInner>& infoPtr);
89     int AllocNativeToken(const std::shared_ptr<NativeTokenInfoInner>& infoPtr);
90     void StoreAllTokenInfo();
91     int CreateRemoteHapTokenInfo(AccessTokenID mapID, HapTokenInfoForSync& hapSync);
92     int UpdateRemoteHapTokenInfo(AccessTokenID mapID, HapTokenInfoForSync& hapSync);
93 
94     OHOS::ThreadPool tokenDataWorker_;
95     bool hasInited_;
96 
97     OHOS::Utils::RWLock hapTokenInfoLock_;
98     OHOS::Utils::RWLock nativeTokenInfoLock_;
99     OHOS::Utils::RWLock managerLock_;
100 
101     std::map<int, std::shared_ptr<HapTokenInfoInner>> hapTokenInfoMap_;
102     std::map<std::string, AccessTokenID> hapTokenIdMap_;
103     std::map<int, std::shared_ptr<NativeTokenInfoInner>> nativeTokenInfoMap_;
104     std::map<std::string, AccessTokenID> nativeTokenIdMap_;
105 };
106 } // namespace AccessToken
107 } // namespace Security
108 } // namespace OHOS
109 #endif // ACCESSTOKEN_TOKEN_INFO_MANAGER_H
110