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 "dm_credential_manager.h"
17
18 #include "dm_anonymous.h"
19 #include "dm_constants.h"
20 #include "dm_log.h"
21 #include "dm_random.h"
22 #include "parameter.h"
23
24 namespace OHOS {
25 namespace DistributedHardware {
26 const int32_t LOCAL_CREDENTIAL_DEAL_TYPE = 1;
27 const int32_t REMOTE_CREDENTIAL_DEAL_TYPE = 2;
28 const int32_t NONSYMMETRY_CREDENTIAL_TYPE = 2;
29 const int32_t SYMMETRY_CREDENTIAL_TYPE = 1;
30 const int32_t UNKNOWN_CREDENTIAL_TYPE = 0;
31 const int32_t SAME_ACCOUNT_TYPE = 1;
32 const int32_t CROSS_ACCOUNT_TYPE = 2;
33
34 constexpr const char* FIELD_CREDENTIAL_VERSION = "version";
35 constexpr const char* FIELD_DEVICE_PK = "devicePk";
36 constexpr const char* FIELD_SERVER_PK = "serverPk";
37 constexpr const char* FIELD_PKINFO_SIGNATURE = "pkInfoSignature";
38 constexpr const char* FIELD_PKINFO = "pkInfo";
39 constexpr const char* FIELD_PROCESS_TYPE = "processType";
40 constexpr const char* FIELD_AUTH_TYPE = "authType";
41 constexpr const char* FIELD_CREDENTIAL_DATA = "credentialData";
42 constexpr const char* FIELD_CREDENTIAL_ID = "credentialId";
43 constexpr const char* FIELD_PEER_CREDENTIAL_INFO = "peerCredentialInfo";
44
45 struct CredentialDataInfo {
46 int32_t credentialType;
47 std::string credentailId;
48 std::string serverPk;
49 std::string pkInfoSignature;
50 std::string pkInfo;
51 std::string authCode;
52 std::string peerDeviceId;
53 std::string userId;
CredentialDataInfoOHOS::DistributedHardware::CredentialDataInfo54 CredentialDataInfo() : credentialType(UNKNOWN_CREDENTIAL_TYPE)
55 {
56 }
57 };
58
59 struct PeerCredentialInfo {
60 std::string peerDeviceId;
61 std::string peerCredentialId;
62 };
63
from_json(const nlohmann::json & jsonObject,CredentialData & credentialData)64 void from_json(const nlohmann::json &jsonObject, CredentialData &credentialData)
65 {
66 if (!IsInt32(jsonObject, FIELD_CREDENTIAL_TYPE) || !IsString(jsonObject, FIELD_CREDENTIAL_ID) ||
67 !IsString(jsonObject, FIELD_SERVER_PK) || !IsString(jsonObject, FIELD_PKINFO_SIGNATURE) ||
68 !IsString(jsonObject, FIELD_PKINFO) || !IsString(jsonObject, FIELD_AUTH_CODE) ||
69 !IsString(jsonObject, FIELD_PEER_DEVICE_ID)) {
70 LOGE("CredentialData json key not complete");
71 return;
72 }
73 credentialData.credentialType = jsonObject[FIELD_CREDENTIAL_TYPE].get<int32_t>();
74 credentialData.credentialId = jsonObject[FIELD_CREDENTIAL_ID].get<std::string>();
75 credentialData.serverPk = jsonObject[FIELD_SERVER_PK].get<std::string>();
76 credentialData.pkInfoSignature = jsonObject[FIELD_PKINFO_SIGNATURE].get<std::string>();
77 credentialData.pkInfo = jsonObject[FIELD_PKINFO].get<std::string>();
78 credentialData.authCode = jsonObject[FIELD_AUTH_CODE].get<std::string>();
79 credentialData.peerDeviceId = jsonObject[FIELD_PEER_DEVICE_ID].get<std::string>();
80 }
81
DmCredentialManager(std::shared_ptr<HiChainConnector> hiChainConnector,std::shared_ptr<IDeviceManagerServiceListener> listener)82 DmCredentialManager::DmCredentialManager(std::shared_ptr<HiChainConnector> hiChainConnector,
83 std::shared_ptr<IDeviceManagerServiceListener> listener)
84 : hiChainConnector_(hiChainConnector), listener_(listener)
85 {
86 LOGI("DmCredentialManager constructor");
87 }
88
~DmCredentialManager()89 DmCredentialManager::~DmCredentialManager()
90 {
91 LOGI("DmCredentialManager destructor");
92 }
93
RequestCredential(const std::string & reqJsonStr,std::string & returnJsonStr)94 int32_t DmCredentialManager::RequestCredential(const std::string &reqJsonStr, std::string &returnJsonStr)
95 {
96 LOGI("start to request credential.");
97 char localDeviceId[DEVICE_UUID_LENGTH] = {0};
98 GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH);
99 nlohmann::json jsonObject = nlohmann::json::parse(reqJsonStr, nullptr, false);
100 if (jsonObject.is_discarded()) {
101 LOGE("reqJsonStr string not a json type.");
102 return ERR_DM_FAILED;
103 }
104 if (!IsString(jsonObject, FIELD_USER_ID) || !IsString(jsonObject, FIELD_CREDENTIAL_VERSION)) {
105 LOGE("user id or credential version string key not exist!");
106 return ERR_DM_FAILED;
107 }
108 std::string userId = jsonObject[FIELD_USER_ID].get<std::string>();
109 std::string credentialVersion = jsonObject[FIELD_CREDENTIAL_VERSION].get<std::string>();
110 nlohmann::json jsonObj;
111 jsonObj[FIELD_CREDENTIAL_VERSION] = credentialVersion;
112 jsonObj[FIELD_USER_ID] = userId;
113 jsonObj[FIELD_DEVICE_ID] = localDeviceId;
114 std::string tmpStr = jsonObj.dump();
115 return hiChainConnector_->getRegisterInfo(tmpStr.c_str(), returnJsonStr);
116 }
117
ImportCredential(const std::string & pkgName,const std::string & credentialInfo)118 int32_t DmCredentialManager::ImportCredential(const std::string &pkgName, const std::string &credentialInfo)
119 {
120 std::lock_guard<std::mutex> autoLock(locks_);
121 std::vector<std::string>::iterator iter = std::find(credentialVec_.begin(), credentialVec_.end(), pkgName);
122 if (iter == credentialVec_.end()) {
123 LOGE("credentialInfo not found by pkgName %s", GetAnonyString(pkgName).c_str());
124 return ERR_DM_FAILED;
125 }
126 pkgName_ = pkgName;
127 nlohmann::json jsonObject = nlohmann::json::parse(credentialInfo, nullptr, false);
128 if (jsonObject.is_discarded()) {
129 LOGE("credentialInfo string not a json type.");
130 return ERR_DM_FAILED;
131 }
132 if (!IsInt32(jsonObject, FIELD_PROCESS_TYPE)) {
133 LOGE("credential type string key not exist!");
134 return ERR_DM_FAILED;
135 }
136 int32_t processType = jsonObject[FIELD_PROCESS_TYPE].get<int32_t>();
137 if (IsString(jsonObject, FIELD_TYPE) && processType == REMOTE_CREDENTIAL_DEAL_TYPE) {
138 int32_t ret = ImportRemoteCredentialExt(credentialInfo);
139 if (ret == DM_OK) {
140 OnGroupResultExt(ret, "success");
141 } else {
142 OnGroupResultExt(ret, "failed");
143 }
144 return ret;
145 }
146 if (processType == REMOTE_CREDENTIAL_DEAL_TYPE) {
147 return ImportRemoteCredential(credentialInfo);
148 } else if (processType == LOCAL_CREDENTIAL_DEAL_TYPE) {
149 return ImportLocalCredential(credentialInfo);
150 } else {
151 LOGE("credential type error!");
152 }
153 return ERR_DM_FAILED;
154 }
155
ImportRemoteCredentialExt(const std::string & credentialInfo)156 int32_t DmCredentialManager::ImportRemoteCredentialExt(const std::string &credentialInfo)
157 {
158 LOGI("ImportRemoteCredentialExt start.");
159 if (hiChainConnector_->addMultiMembersExt(credentialInfo) != DM_OK) {
160 LOGE("Failed to add member to group.");
161 return ERR_DM_FAILED;
162 }
163 return DM_OK;
164 }
165
ImportLocalCredential(const std::string & credentialInfo)166 int32_t DmCredentialManager::ImportLocalCredential(const std::string &credentialInfo)
167 {
168 LOGI("ImportLocalCredential start");
169 nlohmann::json jsonObject = nlohmann::json::parse(credentialInfo, nullptr, false);
170 if (jsonObject.is_discarded()) {
171 LOGE("credentialInfo string not a json type.");
172 return ERR_DM_FAILED;
173 }
174 if (!IsInt32(jsonObject, FIELD_AUTH_TYPE) || !IsString(jsonObject, FIELD_USER_ID) ||
175 !IsArray(jsonObject, FIELD_CREDENTIAL_DATA)) {
176 LOGE("auth type or user id or credential data string key not exist!");
177 return ERR_DM_FAILED;
178 }
179 int32_t authType = jsonObject[FIELD_AUTH_TYPE].get<int32_t>();
180 if (authType == SAME_ACCOUNT_TYPE) {
181 authType = IDENTICAL_ACCOUNT_GROUP;
182 }
183 if (authType == CROSS_ACCOUNT_TYPE) {
184 authType = ACROSS_ACCOUNT_AUTHORIZE_GROUP;
185 }
186 std::string userId = jsonObject[FIELD_USER_ID].get<std::string>();
187 requestId_ = GenRandLongLong(MIN_REQUEST_ID, MAX_REQUEST_ID);
188
189 std::vector<CredentialData> vecCredentialData =
190 jsonObject[FIELD_CREDENTIAL_DATA].get<std::vector<CredentialData>>();
191 if (vecCredentialData.size() != 1) {
192 LOGI("ImportLocalCredential credentialData err");
193 return ERR_DM_FAILED;
194 }
195 LOGI("ImportLocalCredential get credentialData success!");
196 nlohmann::json jsonOutObj;
197 if (GetCredentialData(credentialInfo, vecCredentialData[0], jsonOutObj) != DM_OK) {
198 LOGE("failed to get credentialData field from input credential.");
199 return ERR_DM_FAILED;
200 }
201 if (hiChainConnector_->CreateGroup(requestId_, authType, userId, jsonOutObj) != DM_OK) {
202 LOGE("failed to create hichain group function.");
203 return ERR_DM_FAILED;
204 }
205 return DM_OK;
206 }
207
DeleteCredential(const std::string & pkgName,const std::string & deleteInfo)208 int32_t DmCredentialManager::DeleteCredential(const std::string &pkgName, const std::string &deleteInfo)
209 {
210 std::lock_guard<std::mutex> autoLock(locks_);
211 std::vector<std::string>::iterator iter = std::find(credentialVec_.begin(), credentialVec_.end(), pkgName);
212 if (iter == credentialVec_.end()) {
213 LOGE("credentialInfo not found by pkgName %s", GetAnonyString(pkgName).c_str());
214 return ERR_DM_FAILED;
215 }
216 pkgName_ = pkgName;
217 nlohmann::json jsonObject = nlohmann::json::parse(deleteInfo, nullptr, false);
218 if (jsonObject.is_discarded()) {
219 LOGE("deleteInfo string not a json type.");
220 return ERR_DM_FAILED;
221 }
222 if (!IsInt32(jsonObject, FIELD_PROCESS_TYPE) || !IsInt32(jsonObject, FIELD_AUTH_TYPE) ||
223 !IsString(jsonObject, FIELD_USER_ID)) {
224 LOGE("DmCredentialManager::DeleteCredential err json string!");
225 return ERR_DM_FAILED;
226 }
227 int32_t processType = jsonObject[FIELD_PROCESS_TYPE].get<int32_t>();
228 int32_t authType = jsonObject[FIELD_AUTH_TYPE].get<int32_t>();
229 if (authType == SAME_ACCOUNT_TYPE) {
230 authType = IDENTICAL_ACCOUNT_GROUP;
231 }
232 if (authType == CROSS_ACCOUNT_TYPE) {
233 authType = ACROSS_ACCOUNT_AUTHORIZE_GROUP;
234 }
235 std::string userId = jsonObject[FIELD_USER_ID].get<std::string>();
236 requestId_ = GenRandLongLong(MIN_REQUEST_ID, MAX_REQUEST_ID);
237 if (processType == LOCAL_CREDENTIAL_DEAL_TYPE) {
238 return hiChainConnector_->DeleteGroup(requestId_, userId, authType);
239 } else if (processType == REMOTE_CREDENTIAL_DEAL_TYPE) {
240 return DeleteRemoteCredential(deleteInfo);
241 } else {
242 LOGE("credential type error!");
243 }
244 return ERR_DM_FAILED;
245 }
246
OnGroupResultExt(int32_t action,const std::string & resultInfo)247 void DmCredentialManager::OnGroupResultExt(int32_t action, const std::string &resultInfo)
248 {
249 LOGI("DmCredentialManager::OnGroupResultExt action %d, resultInfo %s.", action, resultInfo.c_str());
250 listener_->OnCredentialResult(pkgName_, action, resultInfo);
251 }
252
OnGroupResult(int64_t requestId,int32_t action,const std::string & resultInfo)253 void DmCredentialManager::OnGroupResult(int64_t requestId, int32_t action,
254 const std::string &resultInfo)
255 {
256 LOGI("DmCredentialManager::OnImportResult");
257 if (requestId_ != requestId) {
258 return;
259 }
260 listener_->OnCredentialResult(pkgName_, action, resultInfo);
261 }
262
RegisterCredentialCallback(const std::string & pkgName)263 int32_t DmCredentialManager::RegisterCredentialCallback(const std::string &pkgName)
264 {
265 if (pkgName.empty()) {
266 LOGE("DmCredentialManager::RegisterCredentialCallback input param is empty");
267 return ERR_DM_FAILED;
268 }
269 LOGI("DmCredentialManager::RegisterCredentialCallback pkgName = %s", GetAnonyString(pkgName).c_str());
270 {
271 std::lock_guard<std::mutex> autoLock(locks_);
272 credentialVec_.push_back(pkgName);
273 }
274 return hiChainConnector_->RegisterHiChainGroupCallback(std::shared_ptr<IDmGroupResCallback>(shared_from_this()));
275 }
276
UnRegisterCredentialCallback(const std::string & pkgName)277 int32_t DmCredentialManager::UnRegisterCredentialCallback(const std::string &pkgName)
278 {
279 if (pkgName.empty()) {
280 LOGE("DmCredentialManager::UnRegisterCredentialStateCallback input param is empty");
281 return ERR_DM_FAILED;
282 }
283 LOGI("DmCredentialManager::UnRegisterCredentialStateCallback pkgName = %s", GetAnonyString(pkgName).c_str());
284 {
285 std::lock_guard<std::mutex> autoLock(locks_);
286 std::vector<std::string>::iterator iter = std::find(credentialVec_.begin(), credentialVec_.end(), pkgName);
287 if (iter != credentialVec_.end()) {
288 credentialVec_.erase(iter);
289 }
290 }
291 return hiChainConnector_->UnRegisterHiChainGroupCallback();
292 }
293
GetCredentialData(const std::string & credentialInfo,const CredentialData & inputCreData,nlohmann::json & jsonOutObj)294 int32_t DmCredentialManager::GetCredentialData(const std::string &credentialInfo, const CredentialData &inputCreData,
295 nlohmann::json &jsonOutObj)
296 {
297 nlohmann::json jsonCreObj;
298 jsonCreObj[FIELD_CREDENTIAL_TYPE] = inputCreData.credentialType;
299 int32_t credentialType = inputCreData.credentialType;
300 if (credentialType == NONSYMMETRY_CREDENTIAL_TYPE) {
301 nlohmann::json jsonObject = nlohmann::json::parse(credentialInfo, nullptr, false);
302 if (jsonObject.is_discarded()) {
303 LOGE("credentialInfo string not a json type.");
304 return ERR_DM_FAILED;
305 }
306 if (!IsString(jsonObject, FIELD_USER_ID) || !IsString(jsonObject, FIELD_CREDENTIAL_VERSION) ||
307 !IsString(jsonObject, FIELD_DEVICE_ID) || !IsString(jsonObject, FIELD_DEVICE_PK)) {
308 LOGE("DmCredentialManager::GetCredentialData err json string!");
309 return ERR_DM_FAILED;
310 }
311 std::string userId = jsonObject[FIELD_USER_ID].get<std::string>();
312 std::string deviceId = jsonObject[FIELD_DEVICE_ID].get<std::string>();
313 std::string verSion = jsonObject[FIELD_CREDENTIAL_VERSION].get<std::string>();
314 std::string devicePk = jsonObject[FIELD_DEVICE_PK].get<std::string>();
315 nlohmann::json jsonPkInfo;
316 jsonPkInfo[FIELD_USER_ID] = userId;
317 jsonPkInfo[FIELD_DEVICE_ID] = deviceId;
318 jsonPkInfo[FIELD_CREDENTIAL_VERSION] = verSion;
319 jsonPkInfo[FIELD_DEVICE_PK] = devicePk;
320 jsonCreObj[FIELD_PKINFO] = jsonPkInfo;
321 jsonCreObj[FIELD_SERVER_PK] = inputCreData.serverPk;
322 jsonCreObj[FIELD_PKINFO_SIGNATURE] = inputCreData.pkInfoSignature;
323 } else if (credentialType == SYMMETRY_CREDENTIAL_TYPE) {
324 jsonCreObj[FIELD_AUTH_CODE] = inputCreData.authCode;
325 } else {
326 LOGE("invalid credentialType field!");
327 return ERR_DM_FAILED;
328 }
329 jsonOutObj = jsonCreObj;
330 return DM_OK;
331 }
332
from_json(const nlohmann::json & jsonObject,CredentialDataInfo & credentialDataInfo)333 void from_json(const nlohmann::json &jsonObject, CredentialDataInfo &credentialDataInfo)
334 {
335 if (!IsInt32(jsonObject, FIELD_CREDENTIAL_TYPE)) {
336 LOGE("credentialType json key not exist");
337 return;
338 }
339 credentialDataInfo.credentialType = jsonObject[FIELD_CREDENTIAL_TYPE].get<int32_t>();
340 if (IsString(jsonObject, FIELD_CREDENTIAL_ID)) {
341 credentialDataInfo.credentailId = jsonObject[FIELD_CREDENTIAL_ID].get<std::string>();
342 }
343 if (credentialDataInfo.credentialType == NONSYMMETRY_CREDENTIAL_TYPE) {
344 if (IsString(jsonObject, FIELD_SERVER_PK)) {
345 credentialDataInfo.serverPk = jsonObject[FIELD_SERVER_PK].get<std::string>();
346 }
347 if (IsString(jsonObject, FIELD_PKINFO_SIGNATURE)) {
348 credentialDataInfo.pkInfoSignature = jsonObject[FIELD_PKINFO_SIGNATURE].get<std::string>();
349 }
350 if (IsString(jsonObject, FIELD_PKINFO)) {
351 nlohmann::json jsonPkInfo = jsonObject[FIELD_PKINFO];
352 credentialDataInfo.pkInfo = jsonPkInfo.dump();
353 }
354 } else if (credentialDataInfo.credentialType == SYMMETRY_CREDENTIAL_TYPE) {
355 if (IsString(jsonObject, FIELD_AUTH_CODE)) {
356 credentialDataInfo.authCode = jsonObject[FIELD_AUTH_CODE].get<std::string>();
357 }
358 } else {
359 LOGE("credentialType john key is unknown");
360 return;
361 }
362 if (IsString(jsonObject, FIELD_PEER_DEVICE_ID)) {
363 credentialDataInfo.peerDeviceId = jsonObject[FIELD_PEER_DEVICE_ID].get<std::string>();
364 }
365 }
366
to_json(nlohmann::json & jsonObject,const CredentialDataInfo & credentialDataInfo)367 void to_json(nlohmann::json &jsonObject, const CredentialDataInfo &credentialDataInfo)
368 {
369 jsonObject[FIELD_DEVICE_ID] = credentialDataInfo.peerDeviceId;
370 jsonObject[FIELD_UDID] =credentialDataInfo.peerDeviceId;
371 jsonObject[FIELD_USER_ID] = credentialDataInfo.userId;
372 jsonObject[FIELD_CREDENTIAL_TYPE] = credentialDataInfo.credentialType;
373 jsonObject[FIELD_CREDENTIAL_ID] = atoi(credentialDataInfo.credentailId.c_str());
374 if (credentialDataInfo.credentialType == NONSYMMETRY_CREDENTIAL_TYPE) {
375 jsonObject[FIELD_SERVER_PK] = credentialDataInfo.serverPk;
376 jsonObject[FIELD_PKINFO_SIGNATURE] = credentialDataInfo.pkInfoSignature;
377 jsonObject[FIELD_PKINFO] = credentialDataInfo.pkInfo;
378 } else if (credentialDataInfo.credentialType == SYMMETRY_CREDENTIAL_TYPE) {
379 jsonObject[FIELD_AUTH_CODE] = credentialDataInfo.authCode;
380 }
381 }
382
GetAddDeviceList(const nlohmann::json & jsonObject,nlohmann::json & jsonDeviceList)383 int32_t DmCredentialManager::GetAddDeviceList(const nlohmann::json &jsonObject, nlohmann::json &jsonDeviceList)
384 {
385 if (!jsonObject.contains(FIELD_CREDENTIAL_DATA) || !jsonObject[FIELD_CREDENTIAL_DATA].is_object() ||
386 !IsInt32(jsonObject, FIELD_AUTH_TYPE)) {
387 LOGE("credentaildata or authType string key not exist!");
388 return ERR_DM_FAILED;
389 }
390 nlohmann::json credentialJson = jsonObject[FIELD_CREDENTIAL_DATA];
391 auto credentialDataList = credentialJson.get<std::vector<CredentialDataInfo>>();
392 int32_t authType = jsonObject[FIELD_AUTH_TYPE].get<int32_t>();
393
394 for (auto &credentialData : credentialDataList) {
395 if (authType == SAME_ACCOUNT_TYPE) {
396 if (IsString(jsonObject, FIELD_USER_ID)) {
397 credentialData.userId = jsonObject[FIELD_USER_ID].get<std::string>();
398 }
399 } else if (authType == CROSS_ACCOUNT_TYPE) {
400 if (IsString(jsonObject, FIELD_PEER_USER_ID)) {
401 credentialData.userId = jsonObject[FIELD_PEER_USER_ID].get<std::string>();
402 }
403 }
404 }
405 for (size_t i = 0; i < credentialDataList.size(); i++) {
406 jsonDeviceList[FIELD_DEVICE_LIST][i] = credentialDataList.at(i);
407 }
408 return DM_OK;
409 }
410
ImportRemoteCredential(const std::string & credentialInfo)411 int32_t DmCredentialManager::ImportRemoteCredential(const std::string &credentialInfo)
412 {
413 nlohmann::json jsonObject = nlohmann::json::parse(credentialInfo, nullptr, false);
414 if (jsonObject.is_discarded()) {
415 LOGE("credentialInfo string not a json type.");
416 return ERR_DM_FAILED;
417 }
418 if (!IsInt32(jsonObject, FIELD_AUTH_TYPE)) {
419 LOGE("auth type string key not exist!");
420 return ERR_DM_FAILED;
421 }
422 int32_t authType = jsonObject[FIELD_AUTH_TYPE].get<int32_t>();
423 std::string userId;
424 int32_t groupType = 0;
425 if (authType == SAME_ACCOUNT_TYPE) {
426 groupType = IDENTICAL_ACCOUNT_GROUP;
427 if (!IsString(jsonObject, FIELD_USER_ID)) {
428 LOGE("userId string key not exist!");
429 return ERR_DM_FAILED;
430 } else {
431 userId = jsonObject[FIELD_USER_ID].get<std::string>();
432 }
433 } else if (authType == CROSS_ACCOUNT_TYPE) {
434 groupType = ACROSS_ACCOUNT_AUTHORIZE_GROUP;
435 if (!IsString(jsonObject, FIELD_PEER_USER_ID)) {
436 LOGE("peerUserId string key not exist!");
437 return ERR_DM_FAILED;
438 } else {
439 userId = jsonObject[FIELD_PEER_USER_ID].get<std::string>();
440 }
441 }
442 nlohmann::json jsonDeviceList;
443 if (GetAddDeviceList(jsonObject, jsonDeviceList) != DM_OK) {
444 LOGE("failed to get add DeviceList.");
445 return ERR_DM_FAILED;
446 }
447 if (hiChainConnector_->addMultiMembers(groupType, userId, jsonDeviceList) != DM_OK) {
448 LOGE("failed to add members to group.");
449 return ERR_DM_FAILED;
450 }
451 return DM_OK;
452 }
453
from_json(const nlohmann::json & jsonObject,PeerCredentialInfo & peerCredentialInfo)454 void from_json(const nlohmann::json &jsonObject, PeerCredentialInfo &peerCredentialInfo)
455 {
456 if (IsString(jsonObject, FIELD_PEER_USER_ID)) {
457 peerCredentialInfo.peerDeviceId = jsonObject[FIELD_PEER_USER_ID].get<std::string>();
458 }
459 }
460
to_json(nlohmann::json & jsonObject,const PeerCredentialInfo & peerCredentialInfo)461 void to_json(nlohmann::json &jsonObject, const PeerCredentialInfo &peerCredentialInfo)
462 {
463 jsonObject[FIELD_DEVICE_ID] = peerCredentialInfo.peerDeviceId;
464 }
465
GetDeleteDeviceList(const nlohmann::json & jsonObject,nlohmann::json & deviceList)466 int32_t GetDeleteDeviceList(const nlohmann::json &jsonObject, nlohmann::json &deviceList)
467 {
468 if (!IsArray(jsonObject, FIELD_PEER_CREDENTIAL_INFO)) {
469 LOGE("devicelist string key not exist!");
470 return ERR_DM_FAILED;
471 }
472 auto peerCredentialInfo = jsonObject[FIELD_PEER_CREDENTIAL_INFO].get<std::vector<PeerCredentialInfo>>();
473 for (size_t i = 0; i < peerCredentialInfo.size(); i++) {
474 deviceList[FIELD_DEVICE_LIST][i] = peerCredentialInfo[i];
475 }
476 return DM_OK;
477 }
478
DeleteRemoteCredential(const std::string & deleteInfo)479 int32_t DmCredentialManager::DeleteRemoteCredential(const std::string &deleteInfo)
480 {
481 nlohmann::json jsonObject = nlohmann::json::parse(deleteInfo, nullptr, false);
482 if (jsonObject.is_discarded()) {
483 LOGE("credentialInfo string not a json type.");
484 return ERR_DM_FAILED;
485 }
486 if (!IsInt32(jsonObject, FIELD_AUTH_TYPE)) {
487 LOGE("authType, peerCredential or peerUserId string key not exist!");
488 return ERR_DM_FAILED;
489 }
490 int32_t authType = jsonObject[FIELD_AUTH_TYPE].get<int32_t>();
491 std::string userId;
492 int32_t groupType = 0;
493 if (authType == SAME_ACCOUNT_TYPE) {
494 if (!IsString(jsonObject, FIELD_USER_ID)) {
495 LOGE("userId string key not exist.");
496 return ERR_DM_FAILED;
497 } else {
498 userId = jsonObject[FIELD_USER_ID].get<std::string>();
499 }
500 groupType = IDENTICAL_ACCOUNT_GROUP;
501 } else if (authType == CROSS_ACCOUNT_TYPE) {
502 if (!IsString(jsonObject, FIELD_PEER_USER_ID)) {
503 LOGE("peerUserId string key not exist.");
504 return ERR_DM_FAILED;
505 } else {
506 userId = jsonObject[FIELD_PEER_USER_ID].get<std::string>();
507 }
508 groupType = ACROSS_ACCOUNT_AUTHORIZE_GROUP;
509 }
510 nlohmann::json jsonDeviceList;
511 if (GetDeleteDeviceList(jsonObject, jsonDeviceList) != DM_OK) {
512 LOGE("failed to get delete DeviceList.");
513 return ERR_DM_FAILED;
514 }
515 if (hiChainConnector_->deleteMultiMembers(groupType, userId, jsonDeviceList) != DM_OK) {
516 LOGE("failed to delete members from group.");
517 return ERR_DM_FAILED;
518 }
519 return DM_OK;
520 }
521 } // namespace DistributedHardware
522 } // namespace OHOS