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