1 /*
2 * Copyright (c) 2022-2024 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 "auth_message_processor.h"
17
18 #include "dm_auth_manager.h"
19 #include "dm_anonymous.h"
20 #include "dm_constants.h"
21 #include "dm_log.h"
22
23 namespace OHOS {
24 namespace DistributedHardware {
25 const int32_t MSG_MAX_SIZE = 45 * 1024;
26 const int32_t GROUP_VISIBILITY_IS_PRIVATE = 0;
27 const int32_t MAX_BINDTYPE_SIZE = 1000;
28 constexpr const char* TAG_HOST = "HOST";
29 constexpr const char* TAG_VISIBILITY = "VISIBILITY";
30 constexpr const char* TAG_APP_THUMBNAIL = "APPTHUM";
31 constexpr const char* TAG_THUMBNAIL_SIZE = "THUMSIZE";
32
AuthMessageProcessor(std::shared_ptr<DmAuthManager> authMgr)33 AuthMessageProcessor::AuthMessageProcessor(std::shared_ptr<DmAuthManager> authMgr) : authMgr_(authMgr)
34 {
35 LOGI("AuthMessageProcessor constructor");
36 }
37
~AuthMessageProcessor()38 AuthMessageProcessor::~AuthMessageProcessor()
39 {
40 authMgr_.reset();
41 }
42
GetJsonObj(nlohmann::json & jsonObj)43 void AuthMessageProcessor::GetJsonObj(nlohmann::json &jsonObj)
44 {
45 jsonObj[TAG_VER] = DM_ITF_VER;
46 jsonObj[TAG_MSG_TYPE] = MSG_TYPE_REQ_AUTH;
47 jsonObj[TAG_INDEX] = 0;
48 jsonObj[TAG_REQUESTER] = authRequestContext_->localDeviceName;
49 jsonObj[TAG_DEVICE_ID] = authRequestContext_->deviceId;
50 jsonObj[TAG_LOCAL_DEVICE_ID] = authRequestContext_->localDeviceId;
51 jsonObj[TAG_LOCAL_DEVICE_TYPE] = authRequestContext_->localDeviceTypeId;
52 jsonObj[TAG_DEVICE_TYPE] = authRequestContext_->deviceTypeId;
53 jsonObj[TAG_AUTH_TYPE] = authRequestContext_->authType;
54 jsonObj[TAG_TOKEN] = authRequestContext_->token;
55 jsonObj[TAG_VISIBILITY] = authRequestContext_->groupVisibility;
56 if (authRequestContext_->groupVisibility == GROUP_VISIBILITY_IS_PRIVATE) {
57 jsonObj[TAG_TARGET] = authRequestContext_->targetPkgName;
58 jsonObj[TAG_HOST] = authRequestContext_->hostPkgName;
59 }
60 if (authRequestContext_->authType == AUTH_TYPE_IMPORT_AUTH_CODE) {
61 jsonObj[TAG_IS_SHOW_DIALOG] = false;
62 } else {
63 jsonObj[TAG_IS_SHOW_DIALOG] = true;
64 }
65 jsonObj[TAG_APP_OPERATION] = authRequestContext_->appOperation;
66 jsonObj[TAG_CUSTOM_DESCRIPTION] = authRequestContext_->customDesc;
67 jsonObj[TAG_APP_NAME] = authRequestContext_->appName;
68 jsonObj[TAG_APP_DESCRIPTION] = authRequestContext_->appDesc;
69 jsonObj[TAG_BIND_TYPE_SIZE] = authResponseContext_->bindType.size();
70 for (uint32_t item = 0; item < authResponseContext_->bindType.size(); item++) {
71 std::string itemStr = std::to_string(item);
72 jsonObj[itemStr] = authResponseContext_->bindType[item];
73 }
74 }
75
CreateAuthRequestMessage()76 std::vector<std::string> AuthMessageProcessor::CreateAuthRequestMessage()
77 {
78 LOGI("AuthMessageProcessor::CreateAuthRequestMessage start.");
79 std::vector<std::string> jsonStrVec;
80 int32_t thumbnailSize = (int32_t)(authRequestContext_->appThumbnail.size());
81 int32_t thumbnailSlice = ((thumbnailSize / MSG_MAX_SIZE) + (thumbnailSize % MSG_MAX_SIZE) == 0 ? 0 : 1);
82 nlohmann::json jsonObj;
83 jsonObj[TAG_SLICE_NUM] = thumbnailSlice + 1;
84 jsonObj[TAG_THUMBNAIL_SIZE] = thumbnailSize;
85 GetJsonObj(jsonObj);
86 jsonStrVec.push_back(jsonObj.dump());
87 for (int32_t idx = 0; idx < thumbnailSlice; idx++) {
88 nlohmann::json jsonThumbnailObj;
89 jsonThumbnailObj[TAG_VER] = DM_ITF_VER;
90 jsonThumbnailObj[TAG_MSG_TYPE] = MSG_TYPE_REQ_AUTH;
91 jsonThumbnailObj[TAG_SLICE_NUM] = thumbnailSlice + 1;
92 jsonThumbnailObj[TAG_INDEX] = idx + 1;
93 jsonThumbnailObj[TAG_DEVICE_ID] = authRequestContext_->deviceId;
94 jsonThumbnailObj[TAG_THUMBNAIL_SIZE] = thumbnailSize;
95 int32_t leftLen = thumbnailSize - idx * MSG_MAX_SIZE;
96 int32_t sliceLen = (leftLen > MSG_MAX_SIZE) ? MSG_MAX_SIZE : leftLen;
97 LOGI("TAG_APP_THUMBNAIL encode, idx %{public}d, sliceLen %{public}d, thumbnailSize %{public}d", idx,
98 (uint32_t)sliceLen, thumbnailSize);
99 jsonObj[TAG_APP_THUMBNAIL] = authRequestContext_->appThumbnail.substr(idx * MSG_MAX_SIZE, sliceLen);
100 jsonStrVec.push_back(jsonThumbnailObj.dump());
101 }
102 return jsonStrVec;
103 }
104
CreateSimpleMessage(int32_t msgType)105 std::string AuthMessageProcessor::CreateSimpleMessage(int32_t msgType)
106 {
107 LOGI("AuthMessageProcessor::CreateSimpleMessage start. msgType is %{public}d", msgType);
108 nlohmann::json jsonObj;
109 jsonObj[TAG_VER] = DM_ITF_VER;
110 jsonObj[TAG_MSG_TYPE] = msgType;
111 switch (msgType) {
112 case MSG_TYPE_NEGOTIATE:
113 CreateNegotiateMessage(jsonObj);
114 break;
115 case MSG_TYPE_RESP_NEGOTIATE:
116 CreateRespNegotiateMessage(jsonObj);
117 break;
118 case MSG_TYPE_SYNC_GROUP:
119 CreateSyncGroupMessage(jsonObj);
120 break;
121 case MSG_TYPE_RESP_AUTH:
122 CreateResponseAuthMessage(jsonObj);
123 break;
124 case MSG_TYPE_RESP_AUTH_EXT:
125 CreateResponseAuthMessageExt(jsonObj);
126 break;
127 case MSG_TYPE_REQ_AUTH_TERMINATE:
128 case MSG_TYPE_REQ_SYNC_DELETE_DONE:
129 CreateResponseFinishMessage(jsonObj);
130 break;
131 case MSG_TYPE_REQ_PUBLICKEY:
132 case MSG_TYPE_RESP_PUBLICKEY:
133 CreatePublicKeyMessageExt(jsonObj);
134 break;
135 case MSG_TYPE_REQ_SYNC_DELETE:
136 CreateSyncDeleteMessageExt(jsonObj);
137 break;
138 default:
139 break;
140 }
141 return jsonObj.dump();
142 }
143
CreateSyncDeleteMessageExt(nlohmann::json & json)144 void AuthMessageProcessor::CreateSyncDeleteMessageExt(nlohmann::json &json)
145 {
146 json[TAG_LOCAL_DEVICE_ID] = authResponseContext_->localDeviceId;
147 json[TAG_HOST_PKGNAME] = authResponseContext_->hostPkgName;
148 json[TAG_REPLY] = DM_OK;
149 }
150
CreatePublicKeyMessageExt(nlohmann::json & json)151 void AuthMessageProcessor::CreatePublicKeyMessageExt(nlohmann::json &json)
152 {
153 json[TAG_PUBLICKEY] = authResponseContext_->publicKey;
154 }
155
CreateResponseAuthMessageExt(nlohmann::json & json)156 void AuthMessageProcessor::CreateResponseAuthMessageExt(nlohmann::json &json)
157 {
158 json[TAG_REPLY] = authResponseContext_->reply;
159 json[TAG_TOKEN] = authResponseContext_->token;
160 json[TAG_CONFIRM_OPERATION] = authResponseContext_->confirmOperation;
161 json[TAG_REQUEST_ID] = authResponseContext_->requestId;
162 }
163
CreateNegotiateMessage(nlohmann::json & json)164 void AuthMessageProcessor::CreateNegotiateMessage(nlohmann::json &json)
165 {
166 if (cryptoAdapter_ == nullptr) {
167 json[TAG_CRYPTO_SUPPORT] = false;
168 } else {
169 json[TAG_CRYPTO_SUPPORT] = true;
170 json[TAG_CRYPTO_NAME] = cryptoAdapter_->GetName();
171 json[TAG_CRYPTO_VERSION] = cryptoAdapter_->GetVersion();
172 json[TAG_DEVICE_ID] = authResponseContext_->deviceId;
173 }
174 json[TAG_AUTH_TYPE] = authResponseContext_->authType;
175 json[TAG_REPLY] = authResponseContext_->reply;
176 json[TAG_LOCAL_DEVICE_ID] = authResponseContext_->localDeviceId;
177 json[TAG_ACCOUNT_GROUPID] = authResponseContext_->accountGroupIdHash;
178
179 json[TAG_BIND_LEVEL] = authResponseContext_->bindLevel;
180 json[TAG_LOCAL_ACCOUNTID] = authResponseContext_->localAccountId;
181 json[TAG_LOCAL_USERID] = authResponseContext_->localUserId;
182 json[TAG_ISONLINE] = authResponseContext_->isOnline;
183 json[TAG_AUTHED] = authResponseContext_->authed;
184 json[TAG_DMVERSION] = authResponseContext_->dmVersion;
185 json[TAG_HOST] = authResponseContext_->hostPkgName;
186 json[TAG_TOKENID] = authResponseContext_->tokenId;
187 json[TAG_IDENTICAL_ACCOUNT] = authResponseContext_->isIdenticalAccount;
188 json[TAG_HAVE_CREDENTIAL] = authResponseContext_->haveCredential;
189 json[TAG_HOST_PKGLABEL] = authResponseContext_->hostPkgLabel;
190 json[TAG_EDITION] = authResponseContext_->edition;
191 }
192
CreateRespNegotiateMessage(nlohmann::json & json)193 void AuthMessageProcessor::CreateRespNegotiateMessage(nlohmann::json &json)
194 {
195 if (cryptoAdapter_ == nullptr) {
196 json[TAG_CRYPTO_SUPPORT] = false;
197 } else {
198 json[TAG_CRYPTO_SUPPORT] = true;
199 json[TAG_CRYPTO_NAME] = cryptoAdapter_->GetName();
200 json[TAG_CRYPTO_VERSION] = cryptoAdapter_->GetVersion();
201 json[TAG_DEVICE_ID] = authResponseContext_->deviceId;
202 }
203 json[TAG_AUTH_TYPE] = authResponseContext_->authType;
204 json[TAG_REPLY] = authResponseContext_->reply;
205 json[TAG_LOCAL_DEVICE_ID] = authResponseContext_->localDeviceId;
206 json[TAG_IS_AUTH_CODE_READY] = authResponseContext_->isAuthCodeReady;
207 json[TAG_NET_ID] = authResponseContext_->networkId;
208
209 json[TAG_LOCAL_ACCOUNTID] = authResponseContext_->localAccountId;
210 json[TAG_LOCAL_USERID] = authResponseContext_->localUserId;
211 json[TAG_ISONLINE] = authResponseContext_->isOnline;
212 json[TAG_AUTHED] = authResponseContext_->authed;
213 json[TAG_DMVERSION] = authResponseContext_->dmVersion;
214 json[TAG_BIND_LEVEL] = authResponseContext_->bindLevel;
215 json[TAG_IDENTICAL_ACCOUNT] = authResponseContext_->isIdenticalAccount;
216 json[TAG_HAVE_CREDENTIAL] = authResponseContext_->haveCredential;
217 json[TAG_BIND_TYPE_SIZE] = authResponseContext_->bindType.size();
218 json[TAG_TARGET_DEVICE_NAME] = authResponseContext_->targetDeviceName;
219 json[TAG_IMPORT_AUTH_CODE] = authResponseContext_->importAuthCode;
220 for (uint32_t item = 0; item < authResponseContext_->bindType.size(); item++) {
221 auto itemStr = std::to_string(item);
222 json[itemStr] = authResponseContext_->bindType[item];
223 }
224 }
225
CreateSyncGroupMessage(nlohmann::json & json)226 void AuthMessageProcessor::CreateSyncGroupMessage(nlohmann::json &json)
227 {
228 json[TAG_DEVICE_ID] = authRequestContext_->deviceId;
229 json[TAG_GROUPIDS] = authRequestContext_->syncGroupList;
230 }
231
CreateResponseAuthMessage(nlohmann::json & json)232 void AuthMessageProcessor::CreateResponseAuthMessage(nlohmann::json &json)
233 {
234 json[TAG_REPLY] = authResponseContext_->reply;
235 json[TAG_DEVICE_ID] = authResponseContext_->deviceId;
236 json[TAG_TOKEN] = authResponseContext_->token;
237 if (authResponseContext_->reply == 0) {
238 std::string groupId = authResponseContext_->groupId;
239 LOGI("AuthMessageProcessor::CreateResponseAuthMessage groupId %{public}s", GetAnonyString(groupId).c_str());
240 nlohmann::json jsonObject = nlohmann::json::parse(groupId, nullptr, false);
241 if (jsonObject.is_discarded()) {
242 LOGE("DecodeRequestAuth jsonStr error");
243 return;
244 }
245 if (!IsString(jsonObject, TAG_GROUP_ID)) {
246 LOGE("err json string.");
247 return;
248 }
249 groupId = jsonObject[TAG_GROUP_ID].get<std::string>();
250 json[TAG_NET_ID] = authResponseContext_->networkId;
251 json[TAG_REQUEST_ID] = authResponseContext_->requestId;
252 json[TAG_GROUP_ID] = groupId;
253 json[TAG_GROUP_NAME] = authResponseContext_->groupName;
254 json[TAG_AUTH_TOKEN] = authResponseContext_->authToken;
255 LOGI("AuthMessageProcessor::CreateResponseAuthMessage %{public}s, %{public}s", GetAnonyString(groupId).c_str(),
256 GetAnonyString(authResponseContext_->groupName).c_str());
257 }
258 }
259
CreateResponseFinishMessage(nlohmann::json & json)260 void AuthMessageProcessor::CreateResponseFinishMessage(nlohmann::json &json)
261 {
262 json[TAG_REPLY] = authResponseContext_->reply;
263 json[TAG_AUTH_FINISH] = authResponseContext_->isFinish;
264 }
265
ParseMessage(const std::string & message)266 int32_t AuthMessageProcessor::ParseMessage(const std::string &message)
267 {
268 nlohmann::json jsonObject = nlohmann::json::parse(message, nullptr, false);
269 if (jsonObject.is_discarded()) {
270 LOGE("DecodeRequestAuth jsonStr error");
271 return ERR_DM_FAILED;
272 }
273 if (!IsInt32(jsonObject, TAG_MSG_TYPE)) {
274 LOGE("err json string, first time");
275 return ERR_DM_FAILED;
276 }
277 int32_t msgType = jsonObject[TAG_MSG_TYPE].get<int32_t>();
278 authResponseContext_->msgType = msgType;
279 LOGI("AuthMessageProcessor::ParseMessage message type %{public}d", authResponseContext_->msgType);
280 switch (msgType) {
281 case MSG_TYPE_NEGOTIATE:
282 ParseNegotiateMessage(jsonObject);
283 break;
284 case MSG_TYPE_RESP_NEGOTIATE:
285 ParseRespNegotiateMessage(jsonObject);
286 break;
287 case MSG_TYPE_REQ_AUTH:
288 return ParseAuthRequestMessage(jsonObject);
289 break;
290 case MSG_TYPE_RESP_AUTH:
291 ParseAuthResponseMessage(jsonObject);
292 break;
293 case MSG_TYPE_RESP_AUTH_EXT:
294 ParseAuthResponseMessageExt(jsonObject);
295 break;
296 case MSG_TYPE_REQ_AUTH_TERMINATE:
297 case MSG_TYPE_REQ_SYNC_DELETE_DONE:
298 ParseResponseFinishMessage(jsonObject);
299 break;
300 case MSG_TYPE_REQ_PUBLICKEY:
301 case MSG_TYPE_RESP_PUBLICKEY:
302 ParsePublicKeyMessageExt(jsonObject);
303 break;
304 case MSG_TYPE_REQ_SYNC_DELETE:
305 ParseSyncDeleteMessageExt(jsonObject);
306 break;
307 default:
308 break;
309 }
310 return DM_OK;
311 }
312
ParseSyncDeleteMessageExt(nlohmann::json & json)313 void AuthMessageProcessor::ParseSyncDeleteMessageExt(nlohmann::json &json)
314 {
315 if (IsString(json, TAG_LOCAL_DEVICE_ID)) {
316 authResponseContext_->localDeviceId = json[TAG_LOCAL_DEVICE_ID].get<std::string>();
317 }
318 if (IsString(json, TAG_HOST_PKGNAME)) {
319 authResponseContext_->hostPkgName = json[TAG_HOST_PKGNAME].get<std::string>();
320 }
321 if (IsInt32(json, TAG_REPLY)) {
322 authResponseContext_->reply = json[TAG_REPLY].get<int32_t>();
323 }
324 }
325
ParsePublicKeyMessageExt(nlohmann::json & json)326 void AuthMessageProcessor::ParsePublicKeyMessageExt(nlohmann::json &json)
327 {
328 if (IsString(json, TAG_PUBLICKEY)) {
329 authResponseContext_->publicKey = json[TAG_PUBLICKEY].get<std::string>();
330 }
331 }
332
ParseAuthResponseMessageExt(nlohmann::json & json)333 void AuthMessageProcessor::ParseAuthResponseMessageExt(nlohmann::json &json)
334 {
335 LOGI("start ParseAuthResponseMessageExt");
336 if (IsInt32(json, TAG_REPLY)) {
337 authResponseContext_->reply = json[TAG_REPLY].get<int32_t>();
338 }
339 if (IsString(json, TAG_TOKEN)) {
340 authResponseContext_->token = json[TAG_TOKEN].get<std::string>();
341 }
342 if (IsInt32(json, TAG_CONFIRM_OPERATION)) {
343 authResponseContext_->confirmOperation = json[TAG_CONFIRM_OPERATION].get<int32_t>();
344 }
345 if (IsInt64(json, TAG_REQUEST_ID)) {
346 authResponseContext_->requestId = json[TAG_REQUEST_ID].get<int64_t>();
347 }
348 }
349
ParseResponseFinishMessage(nlohmann::json & json)350 void AuthMessageProcessor::ParseResponseFinishMessage(nlohmann::json &json)
351 {
352 if (IsInt32(json, TAG_REPLY)) {
353 authResponseContext_->reply = json[TAG_REPLY].get<int32_t>();
354 }
355 if (IsBool(json, TAG_AUTH_FINISH)) {
356 authResponseContext_->isFinish = json[TAG_AUTH_FINISH].get<bool>();
357 }
358 }
359
GetAuthReqMessage(nlohmann::json & json)360 void AuthMessageProcessor::GetAuthReqMessage(nlohmann::json &json)
361 {
362 if (IsInt32(json, TAG_AUTH_TYPE)) {
363 authResponseContext_->authType = json[TAG_AUTH_TYPE].get<int32_t>();
364 }
365 if (IsString(json, TAG_TOKEN)) {
366 authResponseContext_->token = json[TAG_TOKEN].get<std::string>();
367 }
368 if (IsString(json, TAG_DEVICE_ID)) {
369 authResponseContext_->deviceId = json[TAG_DEVICE_ID].get<std::string>();
370 }
371 if (IsString(json, TAG_TARGET)) {
372 authResponseContext_->targetPkgName = json[TAG_TARGET].get<std::string>();
373 }
374 if (IsString(json, TAG_LOCAL_DEVICE_ID)) {
375 authResponseContext_->localDeviceId = json[TAG_LOCAL_DEVICE_ID].get<std::string>();
376 }
377 if (IsString(json, TAG_APP_OPERATION)) {
378 authResponseContext_->appOperation = json[TAG_APP_OPERATION].get<std::string>();
379 }
380 if (IsString(json, TAG_CUSTOM_DESCRIPTION)) {
381 authResponseContext_->customDesc = json[TAG_CUSTOM_DESCRIPTION].get<std::string>();
382 }
383 if (IsString(json, TAG_REQUESTER)) {
384 authResponseContext_->deviceName = json[TAG_REQUESTER].get<std::string>();
385 }
386 if (IsInt32(json, TAG_LOCAL_DEVICE_TYPE)) {
387 authResponseContext_->deviceTypeId = json[TAG_LOCAL_DEVICE_TYPE].get<int32_t>();
388 } else {
389 authResponseContext_->deviceTypeId = DmDeviceType::DEVICE_TYPE_UNKNOWN;
390 }
391 }
392
ParseAuthRequestMessage(nlohmann::json & json)393 int32_t AuthMessageProcessor::ParseAuthRequestMessage(nlohmann::json &json)
394 {
395 LOGI("start ParseAuthRequestMessage");
396 int32_t sliceNum = 0;
397 int32_t idx = 0;
398 if (!IsInt32(json, TAG_INDEX) || !IsInt32(json, TAG_SLICE_NUM)) {
399 LOGE("AuthMessageProcessor::ParseAuthRequestMessage err json string, first.");
400 return ERR_DM_FAILED;
401 }
402 idx = json[TAG_INDEX].get<int32_t>();
403 sliceNum = json[TAG_SLICE_NUM].get<int32_t>();
404 if (idx == 0) {
405 GetAuthReqMessage(json);
406 authResponseContext_->appThumbnail = "";
407 }
408 if (idx < sliceNum && IsString(json, TAG_APP_THUMBNAIL)) {
409 std::string appSliceThumbnail = json[TAG_APP_THUMBNAIL].get<std::string>();
410 authResponseContext_->appThumbnail = authResponseContext_->appThumbnail + appSliceThumbnail;
411 return ERR_DM_AUTH_MESSAGE_INCOMPLETE;
412 }
413 if (IsBool(json, TAG_IS_SHOW_DIALOG)) {
414 authResponseContext_->isShowDialog = json[TAG_IS_SHOW_DIALOG].get<bool>();
415 } else {
416 authResponseContext_->isShowDialog = true;
417 }
418 if (IsInt32(json, TAG_BIND_TYPE_SIZE)) {
419 int32_t bindTypeSize = json[TAG_BIND_TYPE_SIZE].get<int32_t>();
420 if (bindTypeSize > MAX_BINDTYPE_SIZE) {
421 LOGE("ParseAuthRequestMessage bindTypeSize is over size.");
422 return ERR_DM_FAILED;
423 }
424 authResponseContext_->bindType.clear();
425 for (int32_t item = 0; item < bindTypeSize; item++) {
426 std::string itemStr = std::to_string(item);
427 if (IsInt32(json, itemStr)) {
428 authResponseContext_->bindType.push_back(json[itemStr].get<int32_t>());
429 }
430 }
431 }
432 return DM_OK;
433 }
434
ParseAuthResponseMessage(nlohmann::json & json)435 void AuthMessageProcessor::ParseAuthResponseMessage(nlohmann::json &json)
436 {
437 LOGI("start ParseAuthResponseMessage");
438 if (!IsInt32(json, TAG_REPLY)) {
439 LOGE("AuthMessageProcessor::ParseAuthResponseMessage err json string, first time.");
440 return;
441 }
442 authResponseContext_->reply = json[TAG_REPLY].get<int32_t>();
443 if (IsString(json, TAG_DEVICE_ID)) {
444 authResponseContext_->deviceId = json[TAG_DEVICE_ID].get<std::string>();
445 }
446 if (IsString(json, TAG_TOKEN)) {
447 authResponseContext_->token = json[TAG_TOKEN].get<std::string>();
448 }
449 if (authResponseContext_->reply == 0) {
450 if (!IsInt64(json, TAG_REQUEST_ID) || !IsString(json, TAG_GROUP_ID) ||
451 !IsString(json, TAG_GROUP_NAME) || !IsString(json, TAG_AUTH_TOKEN)) {
452 LOGE("AuthMessageProcessor::ParseAuthResponseMessage err json string, second time.");
453 return;
454 }
455 authResponseContext_->requestId = json[TAG_REQUEST_ID].get<int64_t>();
456 authResponseContext_->groupId = json[TAG_GROUP_ID].get<std::string>();
457 authResponseContext_->groupName = json[TAG_GROUP_NAME].get<std::string>();
458 authResponseContext_->authToken = json[TAG_AUTH_TOKEN].get<std::string>();
459 if (IsString(json, TAG_NET_ID)) {
460 authResponseContext_->networkId = json[TAG_NET_ID].get<std::string>();
461 }
462 LOGI("AuthMessageProcessor::ParseAuthResponseMessage groupId = %{public}s, groupName = %{public}s",
463 GetAnonyString(authResponseContext_->groupId).c_str(),
464 GetAnonyString(authResponseContext_->groupName).c_str());
465 }
466 }
467
ParsePkgNegotiateMessage(const nlohmann::json & json)468 void AuthMessageProcessor::ParsePkgNegotiateMessage(const nlohmann::json &json)
469 {
470 if (IsString(json, TAG_LOCAL_ACCOUNTID)) {
471 authResponseContext_->localAccountId = json[TAG_LOCAL_ACCOUNTID].get<std::string>();
472 }
473 if (IsInt32(json, TAG_LOCAL_USERID)) {
474 authResponseContext_->localUserId = json[TAG_LOCAL_USERID].get<int32_t>();
475 }
476 if (IsInt32(json, TAG_BIND_LEVEL)) {
477 authResponseContext_->bindLevel = json[TAG_BIND_LEVEL].get<int32_t>();
478 }
479 if (IsBool(json, TAG_ISONLINE)) {
480 authResponseContext_->isOnline = json[TAG_ISONLINE].get<bool>();
481 }
482 if (IsBool(json, TAG_IDENTICAL_ACCOUNT)) {
483 authResponseContext_->isIdenticalAccount = json[TAG_IDENTICAL_ACCOUNT].get<bool>();
484 }
485 if (IsBool(json, TAG_AUTHED)) {
486 authResponseContext_->authed = json[TAG_AUTHED].get<bool>();
487 }
488 if (IsInt64(json, TAG_TOKENID)) {
489 authResponseContext_->tokenId = json[TAG_TOKENID].get<int64_t>();
490 }
491 if (IsString(json, TAG_DMVERSION)) {
492 authResponseContext_->dmVersion = json[TAG_DMVERSION].get<std::string>();
493 } else {
494 authResponseContext_->dmVersion = "3.2";
495 }
496 if (IsBool(json, TAG_HAVECREDENTIAL)) {
497 authResponseContext_->haveCredential = json[TAG_HAVECREDENTIAL].get<bool>();
498 }
499 if (IsInt32(json, TAG_BIND_TYPE_SIZE)) {
500 int32_t bindTypeSize = json[TAG_BIND_TYPE_SIZE].get<int32_t>();
501 if (bindTypeSize > MAX_BINDTYPE_SIZE) {
502 LOGE("ParsePkgNegotiateMessage bindTypeSize is over size.");
503 return;
504 }
505 authResponseContext_->bindType.clear();
506 for (int32_t item = 0; item < bindTypeSize; item++) {
507 std::string itemStr = std::to_string(item);
508 if (IsInt32(json, itemStr)) {
509 authResponseContext_->bindType.push_back(json[itemStr].get<int32_t>());
510 }
511 }
512 }
513 if (IsString(json, TAG_HOST_PKGLABEL)) {
514 authResponseContext_->hostPkgLabel = json[TAG_HOST_PKGLABEL].get<std::string>();
515 }
516 }
517
ParseNegotiateMessage(const nlohmann::json & json)518 void AuthMessageProcessor::ParseNegotiateMessage(const nlohmann::json &json)
519 {
520 if (IsBool(json, TAG_CRYPTO_SUPPORT)) {
521 authResponseContext_->cryptoSupport = json[TAG_CRYPTO_SUPPORT].get<bool>();
522 }
523 if (IsString(json, TAG_CRYPTO_NAME)) {
524 authResponseContext_->cryptoName = json[TAG_CRYPTO_NAME].get<std::string>();
525 }
526 if (IsString(json, TAG_CRYPTO_VERSION)) {
527 authResponseContext_->cryptoVer = json[TAG_CRYPTO_VERSION].get<std::string>();
528 }
529 if (IsString(json, TAG_DEVICE_ID)) {
530 authResponseContext_->deviceId = json[TAG_DEVICE_ID].get<std::string>();
531 }
532 if (IsString(json, TAG_LOCAL_DEVICE_ID)) {
533 authResponseContext_->localDeviceId = json[TAG_LOCAL_DEVICE_ID].get<std::string>();
534 }
535 if (IsInt32(json, TAG_AUTH_TYPE)) {
536 authResponseContext_->authType = json[TAG_AUTH_TYPE].get<int32_t>();
537 }
538 if (IsInt32(json, TAG_REPLY)) {
539 authResponseContext_->reply = json[TAG_REPLY].get<int32_t>();
540 }
541 if (IsString(json, TAG_ACCOUNT_GROUPID)) {
542 authResponseContext_->accountGroupIdHash = json[TAG_ACCOUNT_GROUPID].get<std::string>();
543 } else {
544 authResponseContext_->accountGroupIdHash = OLD_VERSION_ACCOUNT;
545 }
546 if (IsString(json, TAG_HOST)) {
547 authResponseContext_->hostPkgName = json[TAG_HOST].get<std::string>();
548 }
549 if (IsString(json, TAG_EDITION)) {
550 authResponseContext_->edition = json[TAG_EDITION].get<std::string>();
551 }
552 ParsePkgNegotiateMessage(json);
553 }
554
ParseRespNegotiateMessage(const nlohmann::json & json)555 void AuthMessageProcessor::ParseRespNegotiateMessage(const nlohmann::json &json)
556 {
557 if (IsBool(json, TAG_IDENTICAL_ACCOUNT)) {
558 authResponseContext_->isIdenticalAccount = json[TAG_IDENTICAL_ACCOUNT].get<bool>();
559 }
560 if (IsInt32(json, TAG_REPLY)) {
561 authResponseContext_->reply = json[TAG_REPLY].get<int32_t>();
562 }
563 if (IsString(json, TAG_LOCAL_DEVICE_ID)) {
564 authResponseContext_->localDeviceId = json[TAG_LOCAL_DEVICE_ID].get<std::string>();
565 }
566 if (IsBool(json, TAG_IS_AUTH_CODE_READY)) {
567 authResponseContext_->isAuthCodeReady = json[TAG_IS_AUTH_CODE_READY].get<bool>();
568 }
569 if (IsString(json, TAG_ACCOUNT_GROUPID)) {
570 authResponseContext_->accountGroupIdHash = json[TAG_ACCOUNT_GROUPID].get<std::string>();
571 } else {
572 authResponseContext_->accountGroupIdHash = OLD_VERSION_ACCOUNT;
573 }
574 if (IsString(json, TAG_NET_ID)) {
575 authResponseContext_->networkId = json[TAG_NET_ID].get<std::string>();
576 }
577 if (IsString(json, TAG_TARGET_DEVICE_NAME)) {
578 authResponseContext_->targetDeviceName = json[TAG_TARGET_DEVICE_NAME].get<std::string>();
579 }
580 if (IsString(json, TAG_IMPORT_AUTH_CODE)) {
581 authResponseContext_->importAuthCode = json[TAG_IMPORT_AUTH_CODE].get<std::string>();
582 }
583
584 ParsePkgNegotiateMessage(json);
585 }
586
SetRequestContext(std::shared_ptr<DmAuthRequestContext> authRequestContext)587 void AuthMessageProcessor::SetRequestContext(std::shared_ptr<DmAuthRequestContext> authRequestContext)
588 {
589 authRequestContext_ = authRequestContext;
590 }
591
SetResponseContext(std::shared_ptr<DmAuthResponseContext> authResponseContext)592 void AuthMessageProcessor::SetResponseContext(std::shared_ptr<DmAuthResponseContext> authResponseContext)
593 {
594 authResponseContext_ = authResponseContext;
595 }
596
GetResponseContext()597 std::shared_ptr<DmAuthResponseContext> AuthMessageProcessor::GetResponseContext()
598 {
599 return authResponseContext_;
600 }
601
GetRequestContext()602 std::shared_ptr<DmAuthRequestContext> AuthMessageProcessor::GetRequestContext()
603 {
604 return authRequestContext_;
605 }
606
CreateDeviceAuthMessage(int32_t msgType,const uint8_t * data,uint32_t dataLen)607 std::string AuthMessageProcessor::CreateDeviceAuthMessage(int32_t msgType, const uint8_t *data, uint32_t dataLen)
608 {
609 LOGI("CreateDeviceAuthMessage start, msgType %{public}d.", msgType);
610 nlohmann::json jsonObj;
611 jsonObj[TAG_MSG_TYPE] = msgType;
612 std::string authDataStr = std::string(reinterpret_cast<const char *>(data), dataLen);
613 jsonObj[TAG_DATA] = authDataStr;
614 jsonObj[TAG_DATA_LEN] = dataLen;
615 return jsonObj.dump();
616 }
617 } // namespace DistributedHardware
618 } // namespace OHOS
619