1 /*
2  * Copyright (C) 2023 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 "json.hpp"
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 using json = nlohmann::json;
23 
Parse(const std::string & assetJsonsStr,AssetJsonObj & assetJsonObj)24 bool JsonUtil::Parse(const std::string &assetJsonsStr, AssetJsonObj &assetJsonObj)
25 {
26     if (!assetJsonsStr.empty()) {
27         json jsonObj;
28         try {
29             if (!json::accept(assetJsonsStr)) {
30                 APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "assetJsonsStr can not be accept.");
31                 return false;
32             }
33             jsonObj = json::parse(assetJsonsStr);
34         } catch (json::parse_error &e) {
35             APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "assetJsonsStr can not be parsed.");
36             return false;
37         }
38         if (jsonObj == nullptr || !jsonObj.is_object()) {
39             APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE,
40                 "assetLinksStr can not be parsed into obj.");
41             return false;
42         }
43         if (jsonObj.find(ApplinkingAssetKeys::APP_LINKING) == jsonObj.end() ||
44             !jsonObj.at(ApplinkingAssetKeys::APP_LINKING).is_object()) {
45             APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_AGENT_MODULE_SERVICE, "can not parsed applinking into obj.");
46             return false;
47         }
48         auto applinkingObj = jsonObj.at(ApplinkingAssetKeys::APP_LINKING);
49         if (applinkingObj.find(ApplinkingAssetKeys::APPS) != applinkingObj.end() &&
50             applinkingObj.at(ApplinkingAssetKeys::APPS).is_array()) {
51             auto appsArray = applinkingObj.at(ApplinkingAssetKeys::APPS);
52             for (size_t i = 0; i < appsArray.size(); i++) {
53                 AppVerifyBaseInfo appVerifyBaseInfo;
54                 auto arrayItem = appsArray[i];
55                 appVerifyBaseInfo.appIdentifier = arrayItem.find(ApplinkingAssetKeys::APP_IDENTIFIER) !=
56                             arrayItem.end() &&
57                         arrayItem.at(ApplinkingAssetKeys::APP_IDENTIFIER).is_string() ?
58                     arrayItem.at(ApplinkingAssetKeys::APP_IDENTIFIER) :
59                     "";
60                 appVerifyBaseInfo.bundleName = arrayItem.find(ApplinkingAssetKeys::BUNDLE_NAME) != arrayItem.end() &&
61                         arrayItem.at(ApplinkingAssetKeys::BUNDLE_NAME).is_string() ?
62                     arrayItem.at(ApplinkingAssetKeys::BUNDLE_NAME) :
63                     "";
64                 appVerifyBaseInfo.fingerprint = arrayItem.find(ApplinkingAssetKeys::FINGERPRINT) != arrayItem.end() &&
65                         arrayItem.at(ApplinkingAssetKeys::FINGERPRINT).is_string() ?
66                     arrayItem.at(ApplinkingAssetKeys::FINGERPRINT) :
67                     "";
68                 assetJsonObj.applinking.apps.emplace_back(appVerifyBaseInfo);
69             }
70             return true;
71         }
72     }
73     return false;
74 }
75 }
76 }
77