• 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 <sstream>
19 #include "iam_logger.h"
20 #include "iam_time.h"
21 #include "hisysevent_adapter.h"
22 
23 #define LOG_LABEL UserIam::Common::LABEL_USER_AUTH_SA
24 
25 using namespace OHOS::UserIam::UserAuth;
26 
27 namespace OHOS {
28 namespace UserIam {
29 namespace UserAuth {
30 Trace Trace::trace;
31 
Trace()32 Trace::Trace()
33 {
34     ContextCallbackNotifyListener::GetInstance().AddNotifier(ProcessCredChangeEvent);
35     ContextCallbackNotifyListener::GetInstance().AddNotifier(ProcessUserAuthEvent);
36     ContextCallbackNotifyListener::GetInstance().AddNotifier(ProcessPinAuthEvent);
37     ContextCallbackNotifyListener::GetInstance().AddNotifier(ProcessDelUserEvent);
38 }
39 
~Trace()40 Trace::~Trace()
41 {
42 }
43 
ProcessCredChangeEvent(const ContextCallbackNotifyListener::MetaData & metaData)44 void Trace::ProcessCredChangeEvent(const ContextCallbackNotifyListener::MetaData &metaData)
45 {
46     bool checkRet = metaData.operationType == TRACE_ADD_CREDENTIAL ||
47         metaData.operationType == TRACE_DELETE_CREDENTIAL ||
48         metaData.operationType == TRACE_UPDATE_CREDENTIAL;
49     if (!checkRet) {
50         return;
51     }
52     int32_t userId = 0;
53     int32_t authType = 0;
54     uint32_t operationType = metaData.operationType;
55     uint32_t optResult = 0;
56     if (metaData.userId.has_value()) {
57         userId = metaData.userId.value();
58     }
59     if (metaData.authType.has_value()) {
60         authType = metaData.authType.value();
61     }
62     if (metaData.operationResult) {
63         optResult = metaData.operationResult;
64     }
65     ReportBehaviorCredChange(userId, authType, operationType, optResult);
66     ReportSecurityCredChange(userId, authType, operationType, optResult);
67     IAM_LOGI("start to process cred change event");
68 }
69 
ProcessUserAuthEvent(const ContextCallbackNotifyListener::MetaData & metaData)70 void Trace::ProcessUserAuthEvent(const ContextCallbackNotifyListener::MetaData &metaData)
71 {
72     using namespace std::chrono;
73     bool checkRet = metaData.operationType == TRACE_AUTH_USER &&
74         (metaData.authType.has_value() || metaData.authWidgetType.has_value());
75     if (!checkRet) {
76         return;
77     }
78     UserAuthInfo info = {};
79     if (metaData.callingUid.has_value()) {
80         info.callingUid = metaData.callingUid.value();
81     }
82     if (metaData.authType.has_value()) {
83         info.authType = metaData.authType.value();
84     }
85     if (metaData.authWidgetType.has_value()) {
86         info.authWidgetType = metaData.authWidgetType.value();
87     }
88     if (metaData.atl.has_value()) {
89         info.atl = metaData.atl.value();
90     }
91     if (metaData.operationResult) {
92         info.authResult = metaData.operationResult;
93     }
94     auto timeSpan = duration_cast<milliseconds>(metaData.endTime - metaData.startTime);
95     std::ostringstream ss;
96     ss << timeSpan.count() << " ms";
97     info.timeSpanString = ss.str();
98     if (metaData.sdkVersion.has_value()) {
99         info.sdkVersion = metaData.sdkVersion.value();
100     }
101 
102     ReportUserAuth(info);
103     IAM_LOGI("start to process user auth event");
104 }
105 
ProcessPinAuthEvent(const ContextCallbackNotifyListener::MetaData & metaData)106 void Trace::ProcessPinAuthEvent(const ContextCallbackNotifyListener::MetaData &metaData)
107 {
108     bool checkRet = metaData.operationType == TRACE_AUTH_USER && metaData.authType.has_value() &&
109         metaData.authType == PIN;
110     if (!checkRet) {
111         return;
112     }
113     PinAuthInfo info = {};
114     if (metaData.userId.has_value()) {
115         info.userId = metaData.userId.value();
116     }
117     if (metaData.callingUid.has_value()) {
118         info.callingUid = metaData.callingUid.value();
119     }
120     auto timeSpan = std::chrono::duration_cast<std::chrono::milliseconds>(metaData.endTime - metaData.startTime);
121     std::ostringstream ss;
122     ss << timeSpan.count() << " ms";
123     info.authTimeString = ss.str();
124     if (metaData.operationResult) {
125         info.authResult = metaData.operationResult;
126     }
127     if (metaData.remainTime.has_value()) {
128         info.remainTime = metaData.remainTime.value();
129     }
130     if (metaData.freezingTime.has_value()) {
131         info.freezingTime = metaData.freezingTime.value();
132     }
133     ReportPinAuth(info);
134     IAM_LOGI("start to process pin auth event");
135 }
136 
ProcessDelUserEvent(const ContextCallbackNotifyListener::MetaData & metaData)137 void Trace::ProcessDelUserEvent(const ContextCallbackNotifyListener::MetaData &metaData)
138 {
139     OperationType type = metaData.operationType;
140     bool checkRet = type == TRACE_DELETE_USER || type == TRACE_ENFORCE_DELETE_USER;
141     if (!checkRet) {
142         return;
143     }
144     IAM_LOGI("start to process del user event");
145 }
146 } // namespace UserAuth
147 } // namespace UserIam
148 } // namespace OHOS