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 "account_info_report.h"
16 #include "account_log_wrapper.h"
17 #include "iinner_os_account_manager.h"
18 #include "nlohmann/json.hpp"
19 #ifdef SECURITY_GUARDE_ENABLE
20 #include "sg_collect_client.h"
21 #include "time_service_client.h"
22 #endif
23 
24 namespace OHOS {
25 namespace AccountSA {
TransformIntoJson(const std::string & user,int32_t id,ReportEvent event,int32_t result)26 std::string TransformIntoJson(const std::string &user, int32_t id, ReportEvent event, int32_t result)
27 {
28     nlohmann::json jsonResult;
29 #ifdef SECURITY_GUARDE_ENABLE
30     jsonResult["type"] = 0; // default
31     jsonResult["subType"] = static_cast<int32_t>(event);
32     nlohmann::json userJson = nlohmann::json {
33         {"userName", user},
34         {"userId", id},
35     };
36     jsonResult["caller"] = userJson;
37     jsonResult["bootTime"] = std::to_string(MiscServices::TimeServiceClient::GetInstance()->GetBootTimeNs());
38     jsonResult["wallTime"] = std::to_string(MiscServices::TimeServiceClient::GetInstance()->GetWallTimeNs());
39     jsonResult["outcome"] = (result == 0) ? "Success" : "Fail";
40     jsonResult["sourceInfo"] = "";
41     jsonResult["targetInfo"] = "";
42     jsonResult["extra"] = "";
43 #endif
44     return jsonResult.dump();
45 }
46 
ReportSecurityInfo(const std::string & user,int32_t id,ReportEvent event,int32_t result)47 void AccountInfoReport::ReportSecurityInfo(const std::string &user, int32_t id, ReportEvent event, int32_t result)
48 {
49 #ifdef SECURITY_GUARDE_ENABLE
50     using namespace Security::SecurityGuard;
51     std::string userName = user;
52     if (user.empty()) {
53         OsAccountInfo osAccountInfo;
54         (void)IInnerOsAccountManager::GetInstance().QueryOsAccountById(id, osAccountInfo);
55         userName = osAccountInfo.GetLocalName();
56     }
57     int64_t eventId = 1011015001; // 1011015001: report event id
58     std::string content = TransformIntoJson(userName, id, event, result);
59     std::shared_ptr<EventInfo> eventInfo = std::make_shared<EventInfo>(eventId, "1.0", content);
60     NativeDataCollectKit::ReportSecurityInfoAsync(eventInfo);
61 #endif
62 }
63 } // namespace AccountSA
64 } // namespace OHOS
65