1 /*
2 * Copyright (C) 2023-2025 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 #include "cJSON.h"
16 #include "domain_json_util.h"
17 #include "agent_constants.h"
18 #include "app_domain_verify_hilog.h"
19
20 namespace OHOS {
21 namespace AppDomainVerify {
22
Parse(const std::string & assetJsonsStr,AssetJsonObj & assetJsonObj)23 bool JsonUtil::Parse(const std::string &assetJsonsStr, AssetJsonObj &assetJsonObj)
24 {
25 if (assetJsonsStr.empty()) {
26 return false;
27 }
28 cJSON *jsonObj = cJSON_Parse(assetJsonsStr.c_str());
29 if (jsonObj == nullptr) {
30 APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "assetJsonsStr can not be parsed.");
31 return false;
32 }
33 if (!cJSON_IsObject(jsonObj)) {
34 APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "assetLinksStr can not be parsed into obj.");
35 cJSON_Delete(jsonObj);
36 return false;
37 }
38 cJSON *applinkingObj = cJSON_GetObjectItemCaseSensitive(jsonObj, ApplinkingAssetKeys::APP_LINKING.c_str());
39 if (applinkingObj == nullptr || !cJSON_IsObject(applinkingObj)) {
40 APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "can not parsed applinking into obj.");
41 cJSON_Delete(jsonObj);
42 return false;
43 }
44 cJSON *appsArray = cJSON_GetObjectItemCaseSensitive(applinkingObj, ApplinkingAssetKeys::APPS.c_str());
45 if (appsArray != nullptr && cJSON_IsArray(appsArray)) {
46 int arraySize = cJSON_GetArraySize(appsArray);
47 for (int i = 0; i < arraySize; i++) {
48 cJSON *arrayItem = cJSON_GetArrayItem(appsArray, i);
49 if (!cJSON_IsObject(arrayItem)) {
50 continue;
51 }
52 AppVerifyBaseInfo appVerifyBaseInfo;
53 cJSON *appIdentifier = cJSON_GetObjectItemCaseSensitive(arrayItem,
54 ApplinkingAssetKeys::APP_IDENTIFIER.c_str());
55 if (appIdentifier != nullptr && cJSON_IsString(appIdentifier) && appIdentifier->valuestring != nullptr) {
56 appVerifyBaseInfo.appIdentifier = appIdentifier->valuestring;
57 }
58 cJSON *bundleName = cJSON_GetObjectItemCaseSensitive(arrayItem, ApplinkingAssetKeys::BUNDLE_NAME.c_str());
59 if (bundleName != nullptr && cJSON_IsString(bundleName) && bundleName->valuestring != nullptr) {
60 appVerifyBaseInfo.bundleName = bundleName->valuestring;
61 }
62 cJSON *fingerprint = cJSON_GetObjectItemCaseSensitive(arrayItem, ApplinkingAssetKeys::FINGERPRINT.c_str());
63 if (fingerprint != nullptr && cJSON_IsString(fingerprint) && fingerprint->valuestring != nullptr) {
64 appVerifyBaseInfo.fingerprint = fingerprint->valuestring;
65 }
66 assetJsonObj.applinking.apps.emplace_back(appVerifyBaseInfo);
67 }
68 cJSON_Delete(jsonObj);
69 return true;
70 }
71 cJSON_Delete(jsonObj);
72 return false;
73 }
74 }
75 }
76