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 %{public}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 %{public}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 %{public}d, resultInfo %{public}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 = %{public}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 = %{public}s",
284         GetAnonyString(pkgName).c_str());
285     {
286         std::lock_guard<std::mutex> autoLock(locks_);
287         std::vector<std::string>::iterator iter = std::find(credentialVec_.begin(), credentialVec_.end(), pkgName);
288         if (iter != credentialVec_.end()) {
289             credentialVec_.erase(iter);
290         }
291     }
292     return hiChainConnector_->UnRegisterHiChainGroupCallback();
293 }
294 
GetCredentialData(const std::string & credentialInfo,const CredentialData & inputCreData,nlohmann::json & jsonOutObj)295 int32_t DmCredentialManager::GetCredentialData(const std::string &credentialInfo, const CredentialData &inputCreData,
296     nlohmann::json &jsonOutObj)
297 {
298     nlohmann::json jsonCreObj;
299     jsonCreObj[FIELD_CREDENTIAL_TYPE] = inputCreData.credentialType;
300     int32_t credentialType = inputCreData.credentialType;
301     if (credentialType == NONSYMMETRY_CREDENTIAL_TYPE) {
302         nlohmann::json jsonObject = nlohmann::json::parse(credentialInfo, nullptr, false);
303         if (jsonObject.is_discarded()) {
304             LOGE("credentialInfo string not a json type.");
305             return ERR_DM_FAILED;
306         }
307         if (!IsString(jsonObject, FIELD_USER_ID) || !IsString(jsonObject, FIELD_CREDENTIAL_VERSION) ||
308             !IsString(jsonObject, FIELD_DEVICE_ID) || !IsString(jsonObject, FIELD_DEVICE_PK)) {
309             LOGE("DmCredentialManager::GetCredentialData err json string!");
310             return ERR_DM_FAILED;
311         }
312         std::string userId = jsonObject[FIELD_USER_ID].get<std::string>();
313         std::string deviceId = jsonObject[FIELD_DEVICE_ID].get<std::string>();
314         std::string verSion = jsonObject[FIELD_CREDENTIAL_VERSION].get<std::string>();
315         std::string devicePk = jsonObject[FIELD_DEVICE_PK].get<std::string>();
316         nlohmann::json jsonPkInfo;
317         jsonPkInfo[FIELD_USER_ID] = userId;
318         jsonPkInfo[FIELD_DEVICE_ID] = deviceId;
319         jsonPkInfo[FIELD_CREDENTIAL_VERSION] = verSion;
320         jsonPkInfo[FIELD_DEVICE_PK] = devicePk;
321         jsonCreObj[FIELD_PKINFO] = jsonPkInfo;
322         jsonCreObj[FIELD_SERVER_PK] = inputCreData.serverPk;
323         jsonCreObj[FIELD_PKINFO_SIGNATURE] = inputCreData.pkInfoSignature;
324     } else if (credentialType == SYMMETRY_CREDENTIAL_TYPE) {
325         jsonCreObj[FIELD_AUTH_CODE] = inputCreData.authCode;
326     } else {
327         LOGE("invalid credentialType field!");
328         return ERR_DM_FAILED;
329     }
330     jsonOutObj = jsonCreObj;
331     return DM_OK;
332 }
333 
from_json(const nlohmann::json & jsonObject,CredentialDataInfo & credentialDataInfo)334 void from_json(const nlohmann::json &jsonObject, CredentialDataInfo &credentialDataInfo)
335 {
336     if (!IsInt32(jsonObject, FIELD_CREDENTIAL_TYPE)) {
337         LOGE("credentialType json key not exist");
338         return;
339     }
340     credentialDataInfo.credentialType = jsonObject[FIELD_CREDENTIAL_TYPE].get<int32_t>();
341     if (IsString(jsonObject, FIELD_CREDENTIAL_ID)) {
342         credentialDataInfo.credentailId = jsonObject[FIELD_CREDENTIAL_ID].get<std::string>();
343     }
344     if (credentialDataInfo.credentialType == NONSYMMETRY_CREDENTIAL_TYPE) {
345         if (IsString(jsonObject, FIELD_SERVER_PK)) {
346             credentialDataInfo.serverPk = jsonObject[FIELD_SERVER_PK].get<std::string>();
347         }
348         if (IsString(jsonObject, FIELD_PKINFO_SIGNATURE)) {
349             credentialDataInfo.pkInfoSignature = jsonObject[FIELD_PKINFO_SIGNATURE].get<std::string>();
350         }
351         if (IsString(jsonObject, FIELD_PKINFO)) {
352             nlohmann::json jsonPkInfo = jsonObject[FIELD_PKINFO];
353             credentialDataInfo.pkInfo = jsonPkInfo.dump();
354         }
355     } else if (credentialDataInfo.credentialType == SYMMETRY_CREDENTIAL_TYPE) {
356         if (IsString(jsonObject, FIELD_AUTH_CODE)) {
357             credentialDataInfo.authCode = jsonObject[FIELD_AUTH_CODE].get<std::string>();
358         }
359     } else {
360         LOGE("credentialType john key is unknown");
361         return;
362     }
363     if (IsString(jsonObject, FIELD_PEER_DEVICE_ID)) {
364         credentialDataInfo.peerDeviceId = jsonObject[FIELD_PEER_DEVICE_ID].get<std::string>();
365     }
366 }
367 
to_json(nlohmann::json & jsonObject,const CredentialDataInfo & credentialDataInfo)368 void to_json(nlohmann::json &jsonObject, const CredentialDataInfo &credentialDataInfo)
369 {
370     jsonObject[FIELD_DEVICE_ID] = credentialDataInfo.peerDeviceId;
371     jsonObject[FIELD_UDID] =credentialDataInfo.peerDeviceId;
372     jsonObject[FIELD_USER_ID] = credentialDataInfo.userId;
373     jsonObject[FIELD_CREDENTIAL_TYPE] = credentialDataInfo.credentialType;
374     jsonObject[FIELD_CREDENTIAL_ID] = atoi(credentialDataInfo.credentailId.c_str());
375     if (credentialDataInfo.credentialType == NONSYMMETRY_CREDENTIAL_TYPE) {
376         jsonObject[FIELD_SERVER_PK] = credentialDataInfo.serverPk;
377         jsonObject[FIELD_PKINFO_SIGNATURE] = credentialDataInfo.pkInfoSignature;
378         jsonObject[FIELD_PKINFO] = credentialDataInfo.pkInfo;
379     } else if (credentialDataInfo.credentialType == SYMMETRY_CREDENTIAL_TYPE) {
380         jsonObject[FIELD_AUTH_CODE] = credentialDataInfo.authCode;
381     }
382 }
383 
GetAddDeviceList(const nlohmann::json & jsonObject,nlohmann::json & jsonDeviceList)384 int32_t DmCredentialManager::GetAddDeviceList(const nlohmann::json &jsonObject, nlohmann::json &jsonDeviceList)
385 {
386     if (!jsonObject.contains(FIELD_CREDENTIAL_DATA) || !jsonObject[FIELD_CREDENTIAL_DATA].is_object() ||
387         !IsInt32(jsonObject, FIELD_AUTH_TYPE)) {
388         LOGE("credentaildata or authType string key not exist!");
389         return ERR_DM_FAILED;
390     }
391     nlohmann::json credentialJson = jsonObject[FIELD_CREDENTIAL_DATA];
392     auto credentialDataList = credentialJson.get<std::vector<CredentialDataInfo>>();
393     int32_t authType = jsonObject[FIELD_AUTH_TYPE].get<int32_t>();
394 
395     for (auto &credentialData : credentialDataList) {
396         if (authType == SAME_ACCOUNT_TYPE) {
397             if (IsString(jsonObject, FIELD_USER_ID)) {
398                 credentialData.userId = jsonObject[FIELD_USER_ID].get<std::string>();
399             }
400         } else if (authType == CROSS_ACCOUNT_TYPE) {
401             if (IsString(jsonObject, FIELD_PEER_USER_ID)) {
402                 credentialData.userId = jsonObject[FIELD_PEER_USER_ID].get<std::string>();
403             }
404         }
405     }
406     for (size_t i = 0; i < credentialDataList.size(); i++) {
407         jsonDeviceList[FIELD_DEVICE_LIST][i] = credentialDataList.at(i);
408     }
409     return DM_OK;
410 }
411 
ImportRemoteCredential(const std::string & credentialInfo)412 int32_t DmCredentialManager::ImportRemoteCredential(const std::string &credentialInfo)
413 {
414     nlohmann::json jsonObject = nlohmann::json::parse(credentialInfo, nullptr, false);
415     if (jsonObject.is_discarded()) {
416         LOGE("credentialInfo string not a json type.");
417         return ERR_DM_FAILED;
418     }
419     if (!IsInt32(jsonObject, FIELD_AUTH_TYPE)) {
420         LOGE("auth type string key not exist!");
421         return ERR_DM_FAILED;
422     }
423     int32_t authType = jsonObject[FIELD_AUTH_TYPE].get<int32_t>();
424     std::string userId;
425     int32_t groupType = 0;
426     if (authType == SAME_ACCOUNT_TYPE) {
427         groupType = IDENTICAL_ACCOUNT_GROUP;
428         if (!IsString(jsonObject, FIELD_USER_ID)) {
429             LOGE("userId string key not exist!");
430             return ERR_DM_FAILED;
431         } else {
432             userId = jsonObject[FIELD_USER_ID].get<std::string>();
433         }
434     } else if (authType == CROSS_ACCOUNT_TYPE) {
435         groupType = ACROSS_ACCOUNT_AUTHORIZE_GROUP;
436         if (!IsString(jsonObject, FIELD_PEER_USER_ID)) {
437             LOGE("peerUserId string key not exist!");
438             return ERR_DM_FAILED;
439         } else {
440             userId = jsonObject[FIELD_PEER_USER_ID].get<std::string>();
441         }
442     }
443     nlohmann::json jsonDeviceList;
444     if (GetAddDeviceList(jsonObject, jsonDeviceList) != DM_OK) {
445         LOGE("failed to get add DeviceList.");
446         return ERR_DM_FAILED;
447     }
448     if (hiChainConnector_->addMultiMembers(groupType, userId, jsonDeviceList) != DM_OK) {
449         LOGE("failed to add members to group.");
450         return ERR_DM_FAILED;
451     }
452     return DM_OK;
453 }
454 
from_json(const nlohmann::json & jsonObject,PeerCredentialInfo & peerCredentialInfo)455 void from_json(const nlohmann::json &jsonObject, PeerCredentialInfo &peerCredentialInfo)
456 {
457     if (IsString(jsonObject, FIELD_PEER_USER_ID)) {
458         peerCredentialInfo.peerDeviceId = jsonObject[FIELD_PEER_USER_ID].get<std::string>();
459     }
460 }
461 
to_json(nlohmann::json & jsonObject,const PeerCredentialInfo & peerCredentialInfo)462 void to_json(nlohmann::json &jsonObject, const PeerCredentialInfo &peerCredentialInfo)
463 {
464     jsonObject[FIELD_DEVICE_ID] = peerCredentialInfo.peerDeviceId;
465 }
466 
GetDeleteDeviceList(const nlohmann::json & jsonObject,nlohmann::json & deviceList)467 int32_t GetDeleteDeviceList(const nlohmann::json &jsonObject, nlohmann::json &deviceList)
468 {
469     if (!IsArray(jsonObject, FIELD_PEER_CREDENTIAL_INFO)) {
470         LOGE("devicelist string key not exist!");
471         return ERR_DM_FAILED;
472     }
473     auto peerCredentialInfo = jsonObject[FIELD_PEER_CREDENTIAL_INFO].get<std::vector<PeerCredentialInfo>>();
474     for (size_t i = 0; i < peerCredentialInfo.size(); i++) {
475         deviceList[FIELD_DEVICE_LIST][i] = peerCredentialInfo[i];
476     }
477     return DM_OK;
478 }
479 
DeleteRemoteCredential(const std::string & deleteInfo)480 int32_t DmCredentialManager::DeleteRemoteCredential(const std::string &deleteInfo)
481 {
482     nlohmann::json jsonObject = nlohmann::json::parse(deleteInfo, nullptr, false);
483     if (jsonObject.is_discarded()) {
484         LOGE("credentialInfo string not a json type.");
485         return ERR_DM_FAILED;
486     }
487     if (!IsInt32(jsonObject, FIELD_AUTH_TYPE)) {
488         LOGE("authType, peerCredential or peerUserId string key not exist!");
489         return ERR_DM_FAILED;
490     }
491     int32_t authType = jsonObject[FIELD_AUTH_TYPE].get<int32_t>();
492     std::string userId;
493     int32_t groupType = 0;
494     if (authType == SAME_ACCOUNT_TYPE) {
495         if (!IsString(jsonObject, FIELD_USER_ID)) {
496             LOGE("userId string key not exist.");
497             return ERR_DM_FAILED;
498         } else {
499             userId = jsonObject[FIELD_USER_ID].get<std::string>();
500         }
501         groupType = IDENTICAL_ACCOUNT_GROUP;
502     } else if (authType == CROSS_ACCOUNT_TYPE) {
503         if (!IsString(jsonObject, FIELD_PEER_USER_ID)) {
504             LOGE("peerUserId string key not exist.");
505             return ERR_DM_FAILED;
506         } else {
507             userId = jsonObject[FIELD_PEER_USER_ID].get<std::string>();
508         }
509         groupType = ACROSS_ACCOUNT_AUTHORIZE_GROUP;
510     }
511     nlohmann::json jsonDeviceList;
512     if (GetDeleteDeviceList(jsonObject, jsonDeviceList) != DM_OK) {
513         LOGE("failed to get delete DeviceList.");
514         return ERR_DM_FAILED;
515     }
516     if (hiChainConnector_->deleteMultiMembers(groupType, userId, jsonDeviceList) != DM_OK) {
517         LOGE("failed to delete members from group.");
518         return ERR_DM_FAILED;
519     }
520     return DM_OK;
521 }
522 } // namespace DistributedHardware
523 } // namespace OHOS