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
18 namespace OHOS {
19 namespace Security {
20 namespace AccessToken {
21 namespace {
22 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "DeviceInfoManager"};
23 }
GetInstance()24 DeviceInfoManager &DeviceInfoManager::GetInstance()
25 {
26 static DeviceInfoManager instance;
27 return instance;
28 }
29
GetDeviceInfo(const std::string & nodeId,DeviceIdType deviceIdType,DeviceInfo & deviceInfo) const30 bool DeviceInfoManager::GetDeviceInfo(
31 const std::string &nodeId, DeviceIdType deviceIdType, DeviceInfo &deviceInfo) const
32 {
33 return DeviceInfoRepository::GetInstance().FindDeviceInfo(nodeId, deviceIdType, deviceInfo);
34 }
35
ExistDeviceInfo(const std::string & nodeId,DeviceIdType deviceIdType) const36 bool DeviceInfoManager::ExistDeviceInfo(const std::string &nodeId, DeviceIdType deviceIdType) const
37 {
38 DeviceInfo deviceInfo;
39 return DeviceInfoRepository::GetInstance().FindDeviceInfo(nodeId, deviceIdType, deviceInfo);
40 }
41
AddDeviceInfo(const std::string & networkId,const std::string & universallyUniqueId,const std::string & uniqueDeviceId,const std::string & deviceName,const std::string & deviceType)42 void DeviceInfoManager::AddDeviceInfo(const std::string &networkId, const std::string &universallyUniqueId,
43 const std::string &uniqueDeviceId, const std::string &deviceName, const std::string &deviceType)
44 {
45 if (!DataValidator::IsDeviceIdValid(networkId) ||
46 !DataValidator::IsDeviceIdValid(universallyUniqueId) ||
47 !DataValidator::IsDeviceIdValid(uniqueDeviceId) || deviceName.empty() || deviceType.empty()) {
48 ACCESSTOKEN_LOG_ERROR(LABEL, "addDeviceInfo: input param is invalid");
49 return;
50 }
51 DeviceInfoRepository::GetInstance().SaveDeviceInfo(
52 networkId, universallyUniqueId, uniqueDeviceId, deviceName, deviceType);
53 }
54
RemoveAllRemoteDeviceInfo()55 void DeviceInfoManager::RemoveAllRemoteDeviceInfo()
56 {
57 char deviceIdCharArray[Constant::DEVICE_UUID_LENGTH] = {0};
58 GetDevUdid(deviceIdCharArray, Constant::DEVICE_UUID_LENGTH);
59 DeviceInfo localDeviceInfoOpt;
60 if (DeviceInfoRepository::GetInstance().FindDeviceInfo(
61 deviceIdCharArray, DeviceIdType::UNIQUE_DISABILITY_ID, localDeviceInfoOpt)) {
62 DeviceInfoRepository::GetInstance().DeleteAllDeviceInfoExceptOne(localDeviceInfoOpt);
63 }
64 }
65
RemoveRemoteDeviceInfo(const std::string & nodeId,DeviceIdType deviceIdType)66 void DeviceInfoManager::RemoveRemoteDeviceInfo(const std::string &nodeId, DeviceIdType deviceIdType)
67 {
68 if (!DataValidator::IsDeviceIdValid(nodeId)) {
69 ACCESSTOKEN_LOG_ERROR(LABEL, "removeDeviceInfoByNetworkId: nodeId is invalid");
70 } else {
71 DeviceInfo deviceInfo;
72 char deviceIdCharArray[Constant::DEVICE_UUID_LENGTH] = {0};
73 GetDevUdid(deviceIdCharArray, Constant::DEVICE_UUID_LENGTH);
74 if (DeviceInfoRepository::GetInstance().FindDeviceInfo(nodeId, deviceIdType, deviceInfo)) {
75 if (deviceInfo.deviceId.uniqueDeviceId != deviceIdCharArray) {
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 Constant::EncryptDevId(nodeId).c_str());
123 }
124 } else {
125 ACCESSTOKEN_LOG_DEBUG(LABEL,
126 "FindDeviceInfo succeed, udid is empty, nodeId(%{public}s) ",
127 Constant::EncryptDevId(nodeId).c_str());
128 result = uniqueDeviceId;
129 }
130 } else {
131 ACCESSTOKEN_LOG_DEBUG(
132 LABEL, "FindDeviceInfo failed, nodeId(%{public}s)", Constant::EncryptDevId(nodeId).c_str());
133 auto list = DeviceInfoRepository::GetInstance().ListDeviceInfo();
134 auto iter = list.begin();
135 for (; iter != list.end(); iter++) {
136 DeviceInfo info = (*iter);
137 ACCESSTOKEN_LOG_DEBUG(
138 LABEL, ">>> DeviceInfoRepository device name: %{public}s", info.deviceName.c_str());
139 ACCESSTOKEN_LOG_DEBUG(
140 LABEL, ">>> DeviceInfoRepository device type: %{public}s", info.deviceType.c_str());
141 ACCESSTOKEN_LOG_DEBUG(LABEL,
142 ">>> DeviceInfoRepository device network id: %{public}s",
143 Constant::EncryptDevId(info.deviceId.networkId).c_str());
144 }
145 }
146 return result;
147 }
148
IsDeviceUniversallyUniqueId(const std::string & nodeId) const149 bool DeviceInfoManager::IsDeviceUniversallyUniqueId(const std::string &nodeId) const
150 {
151 if (!DataValidator::IsDeviceIdValid(nodeId)) {
152 ACCESSTOKEN_LOG_ERROR(LABEL, "IsDeviceUniversallyUniqueId: nodeId is invalid");
153 return false;
154 }
155 DeviceInfo deviceInfo;
156 if (DeviceInfoRepository::GetInstance().FindDeviceInfo(nodeId, DeviceIdType::UNIVERSALLY_UNIQUE_ID, deviceInfo)) {
157 return deviceInfo.deviceId.universallyUniqueId == nodeId;
158 }
159 return false;
160 }
161 } // namespace AccessToken
162 } // namespace Security
163 } // namespace OHOS
164