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