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 "json_utils.h"
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 auto jsonResult = CreateJson();
29 #ifdef SECURITY_GUARDE_ENABLE
30 AddIntToJson(jsonResult, "type", 0);
31 AddIntToJson(jsonResult, "subType", static_cast<int32_t>(event));
32 auto userJson = CreateJson();
33 AddStringToJson(userJson, "userName", user);
34 AddIntToJson(userJson, "userId", id);
35
36 AddObjToJson(jsonResult, "caller", userJson);
37 AddStringToJson(jsonResult, "bootTime",
38 std::to_string(MiscServices::TimeServiceClient::GetInstance()->GetBootTimeNs()));
39 AddStringToJson(jsonResult, "wallTime",
40 std::to_string(MiscServices::TimeServiceClient::GetInstance()->GetWallTimeNs()));
41 AddStringToJson(jsonResult, "outcome", (result == 0) ? "Success" : "Fail");
42 AddStringToJson(jsonResult, "sourceInfo", "");
43 AddStringToJson(jsonResult, "targetInfo", "");
44 AddStringToJson(jsonResult, "extra", "");
45 #endif
46 return PackJsonToString(jsonResult);
47 }
48
ReportSecurityInfo(const std::string & user,int32_t id,ReportEvent event,int32_t result)49 void AccountInfoReport::ReportSecurityInfo(const std::string &user, int32_t id, ReportEvent event, int32_t result)
50 {
51 #ifdef SECURITY_GUARDE_ENABLE
52 using namespace Security::SecurityGuard;
53 std::string userName = user;
54 if (user.empty()) {
55 OsAccountInfo osAccountInfo;
56 (void)IInnerOsAccountManager::GetInstance().GetRealOsAccountInfoById(id, osAccountInfo);
57 userName = osAccountInfo.GetLocalName();
58 }
59 int64_t eventId = 1011015001; // 1011015001: report event id
60 std::string content = TransformIntoJson(userName, id, event, result);
61 std::shared_ptr<EventInfo> eventInfo = std::make_shared<EventInfo>(eventId, "1.0", content);
62 NativeDataCollectKit::ReportSecurityInfoAsync(eventInfo);
63 #endif
64 }
65 } // namespace AccountSA
66 } // namespace OHOS
67