1 /*
2 * Copyright (c) 2022 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 "trace.h"
17
18 #include <cinttypes>
19 #include <sstream>
20 #include "auth_event_listener_manager.h"
21 #include "iam_logger.h"
22 #include "iam_time.h"
23 #include "hisysevent_adapter.h"
24
25 #define LOG_TAG "USER_AUTH_SA"
26
27 using namespace OHOS::UserIam::UserAuth;
28
29 namespace OHOS {
30 namespace UserIam {
31 namespace UserAuth {
32 Trace Trace::trace;
33
Trace()34 Trace::Trace()
35 {
36 ContextCallbackNotifyListener::GetInstance().AddNotifier(ProcessCredChangeEvent);
37 ContextCallbackNotifyListener::GetInstance().AddNotifier(ProcessCredManagerEvent);
38 ContextCallbackNotifyListener::GetInstance().AddNotifier(ProcessUserAuthEvent);
39 ContextCallbackNotifyListener::GetInstance().AddNotifier(ProcessUserAuthFwkEvent);
40 }
41
~Trace()42 Trace::~Trace()
43 {
44 }
45
ProcessCredChangeEvent(const ContextCallbackNotifyListener::MetaData & metaData,TraceFlag flag)46 void Trace::ProcessCredChangeEvent(const ContextCallbackNotifyListener::MetaData &metaData, TraceFlag flag)
47 {
48 static_cast<void>(flag);
49 if (!(metaData.operationType == TRACE_ADD_CREDENTIAL ||
50 metaData.operationType == TRACE_DELETE_CREDENTIAL ||
51 metaData.operationType == TRACE_UPDATE_CREDENTIAL ||
52 metaData.operationType == TRACE_DELETE_USER ||
53 metaData.operationType == TRACE_ENFORCE_DELETE_USER ||
54 metaData.operationType == TRACE_DELETE_REDUNDANCY)) {
55 return;
56 }
57 UserCredChangeTrace securityInfo = {};
58 if (metaData.callerName.has_value()) {
59 securityInfo.callerName = metaData.callerName.value();
60 }
61 if (metaData.requestContextId.has_value()) {
62 securityInfo.requestContextId = metaData.requestContextId.value();
63 }
64 if (metaData.userId.has_value()) {
65 securityInfo.userId = metaData.userId.value();
66 }
67 if (metaData.authType.has_value()) {
68 securityInfo.authType = metaData.authType.value();
69 }
70 securityInfo.operationType = metaData.operationType;
71 securityInfo.operationResult = metaData.operationResult;
72 uint64_t timeSpan = std::chrono::duration_cast<std::chrono::milliseconds>(metaData.endTime -
73 metaData.startTime).count();
74 securityInfo.timeSpan = timeSpan;
75 ReportSecurityCredChange(securityInfo);
76 IAM_LOGI("start to process cred change event");
77 }
78
ProcessCredManagerEvent(const ContextCallbackNotifyListener::MetaData & metaData,TraceFlag flag)79 void Trace::ProcessCredManagerEvent(const ContextCallbackNotifyListener::MetaData &metaData, TraceFlag flag)
80 {
81 static_cast<void>(flag);
82 if (!(metaData.operationType == TRACE_ADD_CREDENTIAL ||
83 metaData.operationType == TRACE_DELETE_CREDENTIAL ||
84 metaData.operationType == TRACE_UPDATE_CREDENTIAL ||
85 metaData.operationType == TRACE_DELETE_USER ||
86 metaData.operationType == TRACE_ENFORCE_DELETE_USER)) {
87 return;
88 }
89 UserCredManagerTrace info = {};
90 if (metaData.callerName.has_value()) {
91 info.callerName = metaData.callerName.value();
92 }
93 if (metaData.userId.has_value()) {
94 info.userId = metaData.userId.value();
95 }
96 if (metaData.authType.has_value()) {
97 info.authType = metaData.authType.value();
98 }
99 info.operationType = metaData.operationType;
100 info.operationResult = metaData.operationResult;
101 ReportBehaviorCredManager(info);
102 IAM_LOGI("start to process cred manager event");
103 }
104
ProcessUserAuthEvent(const ContextCallbackNotifyListener::MetaData & metaData,TraceFlag flag)105 void Trace::ProcessUserAuthEvent(const ContextCallbackNotifyListener::MetaData &metaData, TraceFlag flag)
106 {
107 if (!(metaData.operationType == TRACE_AUTH_USER_ALL ||
108 metaData.operationType == TRACE_AUTH_USER_BEHAVIOR) ||
109 (flag == TRACE_FLAG_NO_NEED_BEHAVIOR)) {
110 return;
111 }
112 UserAuthTrace info = {};
113 if (metaData.callerName.has_value()) {
114 info.callerName = metaData.callerName.value();
115 }
116 if (metaData.sdkVersion.has_value()) {
117 info.sdkVersion = metaData.sdkVersion.value();
118 }
119 if (metaData.atl.has_value()) {
120 info.atl = metaData.atl.value();
121 }
122 if (metaData.authType.has_value() && metaData.operationResult == SUCCESS) {
123 info.authType = metaData.authType.value();
124 }
125 if (metaData.userId.has_value()) {
126 info.userId = metaData.userId.value();
127 }
128 if (metaData.callerType.has_value()) {
129 info.callerType = metaData.callerType.value();
130 }
131 info.authResult = metaData.operationResult;
132 info.authtimeSpan = static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(metaData.endTime -
133 metaData.startTime).count());
134 if (metaData.authWidgetType.has_value()) {
135 info.authWidgetType = metaData.authWidgetType.value();
136 }
137 if (metaData.reuseUnlockResultMode.has_value()) {
138 info.reuseUnlockResultMode = metaData.reuseUnlockResultMode.value();
139 }
140 if (metaData.reuseUnlockResultDuration.has_value()) {
141 info.reuseUnlockResultDuration = metaData.reuseUnlockResultDuration.value();
142 }
143 if (metaData.isBackgroundApplication.has_value()) {
144 info.isBackgroundApplication = metaData.isBackgroundApplication.value();
145 }
146 ReportUserAuth(info);
147 if (info.authResult == SUCCESS) {
148 AuthEventListenerManager::GetInstance().OnNotifyAuthSuccessEvent(info.userId,
149 static_cast<AuthType>(info.authType), info.callerType, info.callerName);
150 }
151 IAM_LOGI("start to process user auth event");
152 }
153
ProcessUserAuthFwkEvent(const ContextCallbackNotifyListener::MetaData & metaData,TraceFlag flag)154 void Trace::ProcessUserAuthFwkEvent(const ContextCallbackNotifyListener::MetaData &metaData, TraceFlag flag)
155 {
156 static_cast<void>(flag);
157 if (!(metaData.operationType == TRACE_AUTH_USER_ALL ||
158 metaData.operationType == TRACE_AUTH_USER_SECURITY)) {
159 return;
160 }
161 UserAuthFwkTrace securityInfo = {};
162 if (metaData.callerName.has_value()) {
163 securityInfo.callerName = metaData.callerName.value();
164 }
165 if (metaData.requestContextId.has_value()) {
166 securityInfo.requestContextId = metaData.requestContextId.value();
167 }
168 if (metaData.authContextId.has_value()) {
169 securityInfo.authContextId = metaData.authContextId.value();
170 }
171 if (metaData.atl.has_value()) {
172 securityInfo.atl = metaData.atl.value();
173 }
174 if (metaData.authType.has_value()) {
175 securityInfo.authType = metaData.authType.value();
176 }
177 securityInfo.authResult = metaData.operationResult;
178 uint64_t timeSpan = std::chrono::duration_cast<std::chrono::milliseconds>(metaData.endTime -
179 metaData.startTime).count();
180 securityInfo.authtimeSpan = timeSpan;
181 ReportSecurityUserAuthFwk(securityInfo);
182 IAM_LOGI("start to process user auth fwk event");
183 }
184 } // namespace UserAuth
185 } // namespace UserIam
186 } // namespace OHOS