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