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