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