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_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()33Trace::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()41Trace::~Trace() 42 { 43 } 44 ProcessCredChangeEvent(const ContextCallbackNotifyListener::MetaData & metaData)45void Trace::ProcessCredChangeEvent(const ContextCallbackNotifyListener::MetaData &metaData) 46 { 47 if (!(metaData.operationType == TRACE_ADD_CREDENTIAL || 48 metaData.operationType == TRACE_DELETE_CREDENTIAL || 49 metaData.operationType == TRACE_UPDATE_CREDENTIAL || 50 metaData.operationType == TRACE_DELETE_USER || 51 metaData.operationType == TRACE_ENFORCE_DELETE_USER || 52 metaData.operationType == TRACE_DELETE_REDUNDANCY)) { 53 return; 54 } 55 UserCredChangeTrace securityInfo = {}; 56 if (metaData.callerName.has_value()) { 57 securityInfo.callerName = metaData.callerName.value(); 58 } 59 if (metaData.requestContextId.has_value()) { 60 securityInfo.requestContextId = metaData.requestContextId.value(); 61 } 62 if (metaData.userId.has_value()) { 63 securityInfo.userId = metaData.userId.value(); 64 } 65 if (metaData.authType.has_value()) { 66 securityInfo.authType = metaData.authType.value(); 67 } 68 securityInfo.operationType = metaData.operationType; 69 securityInfo.operationResult = metaData.operationResult; 70 uint64_t timeSpan = std::chrono::duration_cast<std::chrono::milliseconds>(metaData.endTime - 71 metaData.startTime).count(); 72 securityInfo.timeSpan = timeSpan; 73 ReportSecurityCredChange(securityInfo); 74 IAM_LOGI("start to process cred change event"); 75 } 76 ProcessCredManagerEvent(const ContextCallbackNotifyListener::MetaData & metaData)77void Trace::ProcessCredManagerEvent(const ContextCallbackNotifyListener::MetaData &metaData) 78 { 79 if (!(metaData.operationType == TRACE_ADD_CREDENTIAL || 80 metaData.operationType == TRACE_DELETE_CREDENTIAL || 81 metaData.operationType == TRACE_UPDATE_CREDENTIAL || 82 metaData.operationType == TRACE_DELETE_USER || 83 metaData.operationType == TRACE_ENFORCE_DELETE_USER)) { 84 return; 85 } 86 UserCredManagerTrace info = {}; 87 if (metaData.callerName.has_value()) { 88 info.callerName = metaData.callerName.value(); 89 } 90 if (metaData.userId.has_value()) { 91 info.userId = metaData.userId.value(); 92 } 93 if (metaData.authType.has_value()) { 94 info.authType = metaData.authType.value(); 95 } 96 info.operationType = metaData.operationType; 97 info.operationResult = metaData.operationResult; 98 ReportBehaviorCredManager(info); 99 IAM_LOGI("start to process cred manager event"); 100 } 101 ProcessUserAuthEvent(const ContextCallbackNotifyListener::MetaData & metaData)102void Trace::ProcessUserAuthEvent(const ContextCallbackNotifyListener::MetaData &metaData) 103 { 104 if (!(metaData.operationType == TRACE_AUTH_USER_ALL || 105 metaData.operationType == TRACE_AUTH_USER_BEHAVIOR)) { 106 return; 107 } 108 UserAuthTrace info = {}; 109 if (metaData.callerName.has_value()) { 110 info.callerName = metaData.callerName.value(); 111 } 112 if (metaData.sdkVersion.has_value()) { 113 info.sdkVersion = metaData.sdkVersion.value(); 114 } 115 if (metaData.atl.has_value()) { 116 info.atl = metaData.atl.value(); 117 } 118 if (metaData.authType.has_value() && metaData.operationResult == SUCCESS) { 119 info.authType = metaData.authType.value(); 120 } 121 info.authResult = metaData.operationResult; 122 uint64_t timeSpan = std::chrono::duration_cast<std::chrono::milliseconds>(metaData.endTime - 123 metaData.startTime).count(); 124 info.timeSpan = timeSpan; 125 if (metaData.authWidgetType.has_value()) { 126 info.authWidgetType = metaData.authWidgetType.value(); 127 } 128 ReportUserAuth(info); 129 IAM_LOGI("start to process user auth event"); 130 } 131 ProcessUserAuthFwkEvent(const ContextCallbackNotifyListener::MetaData & metaData)132void Trace::ProcessUserAuthFwkEvent(const ContextCallbackNotifyListener::MetaData &metaData) 133 { 134 if (!(metaData.operationType == TRACE_AUTH_USER_ALL || 135 metaData.operationType == TRACE_AUTH_USER_SECURITY)) { 136 return; 137 } 138 UserAuthFwkTrace securityInfo = {}; 139 if (metaData.callerName.has_value()) { 140 securityInfo.callerName = metaData.callerName.value(); 141 } 142 if (metaData.requestContextId.has_value()) { 143 securityInfo.requestContextId = metaData.requestContextId.value(); 144 } 145 if (metaData.authContextId.has_value()) { 146 securityInfo.authContextId = metaData.authContextId.value(); 147 } 148 if (metaData.atl.has_value()) { 149 securityInfo.atl = metaData.atl.value(); 150 } 151 if (metaData.authType.has_value()) { 152 securityInfo.authType = metaData.authType.value(); 153 } 154 securityInfo.authResult = metaData.operationResult; 155 uint64_t timeSpan = std::chrono::duration_cast<std::chrono::milliseconds>(metaData.endTime - 156 metaData.startTime).count(); 157 securityInfo.timeSpan = timeSpan; 158 ReportSecurityUserAuthFwk(securityInfo); 159 IAM_LOGI("start to process user auth fwk event"); 160 } 161 } // namespace UserAuth 162 } // namespace UserIam 163 } // namespace OHOS