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