1 /*
2 * Copyright (c) 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 #include "device_info_manager.h"
17 #include "constant_common.h"
18
19 namespace OHOS {
20 namespace Security {
21 namespace AccessToken {
22 namespace {
23 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "DeviceInfoManager"};
24 }
GetInstance()25 DeviceInfoManager &DeviceInfoManager::GetInstance()
26 {
27 static DeviceInfoManager instance;
28 return instance;
29 }
30
GetDeviceInfo(const std::string & nodeId,DeviceIdType deviceIdType,DeviceInfo & deviceInfo) const31 bool DeviceInfoManager::GetDeviceInfo(
32 const std::string &nodeId, DeviceIdType deviceIdType, DeviceInfo &deviceInfo) const
33 {
34 return DeviceInfoRepository::GetInstance().FindDeviceInfo(nodeId, deviceIdType, deviceInfo);
35 }
36
ExistDeviceInfo(const std::string & nodeId,DeviceIdType deviceIdType) const37 bool DeviceInfoManager::ExistDeviceInfo(const std::string &nodeId, DeviceIdType deviceIdType) const
38 {
39 DeviceInfo deviceInfo;
40 return DeviceInfoRepository::GetInstance().FindDeviceInfo(nodeId, deviceIdType, deviceInfo);
41 }
42
AddDeviceInfo(const std::string & networkId,const std::string & universallyUniqueId,const std::string & uniqueDeviceId,const std::string & deviceName,const std::string & deviceType)43 void DeviceInfoManager::AddDeviceInfo(const std::string &networkId, const std::string &universallyUniqueId,
44 const std::string &uniqueDeviceId, const std::string &deviceName, const std::string &deviceType)
45 {
46 if (!DataValidator::IsDeviceIdValid(networkId) ||
47 !DataValidator::IsDeviceIdValid(universallyUniqueId) ||
48 !DataValidator::IsDeviceIdValid(uniqueDeviceId) || deviceName.empty() || deviceType.empty()) {
49 ACCESSTOKEN_LOG_ERROR(LABEL, "addDeviceInfo: input param is invalid");
50 return;
51 }
52 DeviceInfoRepository::GetInstance().SaveDeviceInfo(
53 networkId, universallyUniqueId, uniqueDeviceId, deviceName, deviceType);
54 }
55
RemoveAllRemoteDeviceInfo()56 void DeviceInfoManager::RemoveAllRemoteDeviceInfo()
57 {
58 std::string localDevice = ConstantCommon::GetLocalDeviceId();
59
60 DeviceInfo localDeviceInfoOpt;
61 if (DeviceInfoRepository::GetInstance().FindDeviceInfo(
62 localDevice, DeviceIdType::UNIQUE_DISABILITY_ID, localDeviceInfoOpt)) {
63 DeviceInfoRepository::GetInstance().DeleteAllDeviceInfoExceptOne(localDeviceInfoOpt);
64 }
65 }
66
RemoveRemoteDeviceInfo(const std::string & nodeId,DeviceIdType deviceIdType)67 void DeviceInfoManager::RemoveRemoteDeviceInfo(const std::string &nodeId, DeviceIdType deviceIdType)
68 {
69 if (!DataValidator::IsDeviceIdValid(nodeId)) {
70 ACCESSTOKEN_LOG_ERROR(LABEL, "removeDeviceInfoByNetworkId: nodeId is invalid");
71 } else {
72 DeviceInfo deviceInfo;
73 std::string localDevice = ConstantCommon::GetLocalDeviceId();
74 if (DeviceInfoRepository::GetInstance().FindDeviceInfo(nodeId, deviceIdType, deviceInfo)) {
75 if (deviceInfo.deviceId.uniqueDeviceId != localDevice) {
76 DeviceInfoRepository::GetInstance().DeleteDeviceInfo(nodeId, deviceIdType);
77 }
78 }
79 }
80 }
81
ConvertToUniversallyUniqueIdOrFetch(const std::string & nodeId) const82 std::string DeviceInfoManager::ConvertToUniversallyUniqueIdOrFetch(const std::string &nodeId) const
83 {
84 std::string result;
85 if (!DataValidator::IsDeviceIdValid(nodeId)) {
86 ACCESSTOKEN_LOG_ERROR(LABEL, "ConvertToUniversallyUniqueIdOrFetch: nodeId is invalid.");
87 return result;
88 }
89 DeviceInfo deviceInfo;
90 if (!(DeviceInfoRepository::GetInstance().FindDeviceInfo(nodeId, DeviceIdType::UNKNOWN, deviceInfo))) {
91 return result;
92 }
93 std::string universallyUniqueId = deviceInfo.deviceId.universallyUniqueId;
94 if (!universallyUniqueId.empty()) {
95 result = universallyUniqueId;
96 return result;
97 }
98 std::string udid = SoftBusManager::GetInstance().GetUniversallyUniqueIdByNodeId(nodeId);
99 if (!udid.empty()) {
100 result = udid;
101 }
102 return result;
103 }
104
ConvertToUniqueDeviceIdOrFetch(const std::string & nodeId) const105 std::string DeviceInfoManager::ConvertToUniqueDeviceIdOrFetch(const std::string &nodeId) const
106 {
107 std::string result;
108 if (!DataValidator::IsDeviceIdValid(nodeId)) {
109 ACCESSTOKEN_LOG_ERROR(LABEL, "ConvertToUniqueDeviceIdOrFetch: nodeId is invalid.");
110 return result;
111 }
112 DeviceInfo deviceInfo;
113 if (DeviceInfoRepository::GetInstance().FindDeviceInfo(nodeId, DeviceIdType::UNKNOWN, deviceInfo)) {
114 std::string uniqueDeviceId = deviceInfo.deviceId.uniqueDeviceId;
115 if (uniqueDeviceId.empty()) {
116 std::string udid = SoftBusManager::GetInstance().GetUniqueDeviceIdByNodeId(nodeId);
117 if (!udid.empty()) {
118 result = udid;
119 } else {
120 ACCESSTOKEN_LOG_DEBUG(LABEL,
121 "FindDeviceInfo succeed, udid and local udid is empty, nodeId(%{public}s)",
122 ConstantCommon::EncryptDevId(nodeId).c_str());
123 }
124 } else {
125 ACCESSTOKEN_LOG_DEBUG(LABEL,
126 "FindDeviceInfo succeed, udid is empty, nodeId(%{public}s) ",
127 ConstantCommon::EncryptDevId(nodeId).c_str());
128 result = uniqueDeviceId;
129 }
130 } else {
131 ACCESSTOKEN_LOG_DEBUG(
132 LABEL, "FindDeviceInfo failed, nodeId(%{public}s)",
133 ConstantCommon::EncryptDevId(nodeId).c_str());
134 auto list = DeviceInfoRepository::GetInstance().ListDeviceInfo();
135 auto iter = list.begin();
136 for (; iter != list.end(); iter++) {
137 DeviceInfo info = (*iter);
138 ACCESSTOKEN_LOG_DEBUG(
139 LABEL, ">>> DeviceInfoRepository device name: %{public}s", info.deviceName.c_str());
140 ACCESSTOKEN_LOG_DEBUG(
141 LABEL, ">>> DeviceInfoRepository device type: %{public}s", info.deviceType.c_str());
142 ACCESSTOKEN_LOG_DEBUG(LABEL,
143 ">>> DeviceInfoRepository device network id: %{public}s",
144 ConstantCommon::EncryptDevId(info.deviceId.networkId).c_str());
145 }
146 }
147 return result;
148 }
149
IsDeviceUniversallyUniqueId(const std::string & nodeId) const150 bool DeviceInfoManager::IsDeviceUniversallyUniqueId(const std::string &nodeId) const
151 {
152 if (!DataValidator::IsDeviceIdValid(nodeId)) {
153 ACCESSTOKEN_LOG_ERROR(LABEL, "IsDeviceUniversallyUniqueId: nodeId is invalid");
154 return false;
155 }
156 DeviceInfo deviceInfo;
157 if (DeviceInfoRepository::GetInstance().FindDeviceInfo(nodeId, DeviceIdType::UNIVERSALLY_UNIQUE_ID, deviceInfo)) {
158 return deviceInfo.deviceId.universallyUniqueId == nodeId;
159 }
160 return false;
161 }
162 } // namespace AccessToken
163 } // namespace Security
164 } // namespace OHOS
165