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 #include "context_callback_impl.h"
16
17 #include <sstream>
18
19 #include "iam_check.h"
20 #include "iam_common_defines.h"
21 #include "iam_logger.h"
22 #include "iam_mem.h"
23 #include "iam_ptr.h"
24 #include "nlohmann/json.hpp"
25
26 #define LOG_TAG "USER_AUTH_SA"
27 namespace OHOS {
28 namespace UserIam {
29 namespace UserAuth {
ContextCallbackImpl(sptr<IamCallbackInterface> iamCallback,OperationType operationType)30 ContextCallbackImpl::ContextCallbackImpl(sptr<IamCallbackInterface> iamCallback, OperationType operationType)
31 : iamCallback_(iamCallback)
32 {
33 metaData_.operationType = operationType;
34 metaData_.startTime = std::chrono::steady_clock::now();
35 std::ostringstream ss;
36 ss << "IDM(operation:" << operationType << ")";
37 iamHitraceHelper_ = Common::MakeShared<IamHitraceHelper>(ss.str());
38 }
39
OnAcquireInfo(ExecutorRole src,int32_t moduleType,const std::vector<uint8_t> & acquireMsg)40 void ContextCallbackImpl::OnAcquireInfo(ExecutorRole src, int32_t moduleType,
41 const std::vector<uint8_t> &acquireMsg)
42 {
43 if (iamCallback_ == nullptr) {
44 IAM_LOGE("iam callback is nullptr");
45 return;
46 }
47 int32_t acquireInfo;
48 Attributes attr(acquireMsg);
49 bool getAcquireInfoRet = attr.GetInt32Value(Attributes::ATTR_TIP_INFO, acquireInfo);
50 IF_FALSE_LOGE_AND_RETURN(getAcquireInfoRet);
51
52 std::vector<uint8_t> extraInfo;
53 bool getExtraInfoRet = attr.GetUint8ArrayValue(Attributes::ATTR_EXTRA_INFO, extraInfo);
54 if (getExtraInfoRet) {
55 ProcessAuthResult(acquireInfo, extraInfo);
56 }
57
58 iamCallback_->OnAcquireInfo(moduleType, acquireInfo, attr);
59 }
60
ProcessAuthResult(int32_t tip,const std::vector<uint8_t> & extraInfo)61 void ContextCallbackImpl::ProcessAuthResult(int32_t tip, const std::vector<uint8_t> &extraInfo)
62 {
63 if (tip != USER_AUTH_TIP_SINGLE_AUTH_RESULT) {
64 return;
65 }
66 if (extraInfo.empty()) {
67 return;
68 }
69 std::string tipJson(reinterpret_cast<const char *>(extraInfo.data()), extraInfo.size());
70 if (!nlohmann::json::accept(tipJson)) {
71 IAM_LOGE("invalid format");
72 return;
73 }
74 IAM_LOGI("tipJson:%{public}s", tipJson.c_str());
75 auto root = nlohmann::json::parse(tipJson.c_str());
76 if (root.is_null() || root.is_discarded()) {
77 IAM_LOGE("root is null");
78 return;
79 }
80 const std::string tipJsonKeyAuthResult = "authResult";
81 int32_t authResult = 0;
82 if (root.find(tipJsonKeyAuthResult) == root.end() || !(root[tipJsonKeyAuthResult].is_number())) {
83 IAM_LOGE("authResult is null or is not number");
84 return;
85 }
86 root.at(tipJsonKeyAuthResult).get_to(authResult);
87 if (authResult == SUCCESS) {
88 IAM_LOGI("authResult is success");
89 return;
90 }
91 metaData_.operationResult = authResult;
92 metaData_.endTime = std::chrono::steady_clock::now();
93 IAM_LOGI("fingerprint single auth result, tip: %{public}d, result: %{public}d", tip, authResult);
94 ContextCallbackNotifyListener::GetInstance().Process(metaData_, TRACE_FLAG_NO_NEED_BEHAVIOR);
95 return;
96 }
97
OnResult(int32_t resultCode,const Attributes & finalResult)98 void ContextCallbackImpl::OnResult(int32_t resultCode, const Attributes &finalResult)
99 {
100 int32_t remainTime;
101 int32_t freezingTime;
102 metaData_.operationResult = resultCode;
103 if (finalResult.GetInt32Value(Attributes::ATTR_REMAIN_TIMES, remainTime)) {
104 metaData_.remainTime = remainTime;
105 }
106 if (finalResult.GetInt32Value(Attributes::ATTR_FREEZING_TIME, freezingTime)) {
107 metaData_.freezingTime = freezingTime;
108 }
109 metaData_.endTime = std::chrono::steady_clock::now();
110
111 if (iamCallback_ != nullptr) {
112 iamCallback_->OnResult(resultCode, finalResult);
113 }
114
115 ContextCallbackNotifyListener::GetInstance().Process(metaData_, TRACE_FLAG_DEFAULT);
116 if (stopCallback_ != nullptr) {
117 stopCallback_();
118 }
119 }
120
SetTraceUserId(int32_t userId)121 void ContextCallbackImpl::SetTraceUserId(int32_t userId)
122 {
123 metaData_.userId = userId;
124 }
125
SetTraceRemainTime(int32_t remainTime)126 void ContextCallbackImpl::SetTraceRemainTime(int32_t remainTime)
127 {
128 metaData_.remainTime = remainTime;
129 }
130
SetTraceCallerName(const std::string & callerName)131 void ContextCallbackImpl::SetTraceCallerName(const std::string &callerName)
132 {
133 metaData_.callerName = callerName;
134 }
135
SetTraceRequestContextId(uint64_t requestContextId)136 void ContextCallbackImpl::SetTraceRequestContextId(uint64_t requestContextId)
137 {
138 metaData_.requestContextId = requestContextId;
139 }
140
SetTraceAuthContextId(uint64_t authContextId)141 void ContextCallbackImpl::SetTraceAuthContextId(uint64_t authContextId)
142 {
143 metaData_.authContextId = authContextId;
144 }
145
SetTraceFreezingTime(int32_t freezingTime)146 void ContextCallbackImpl::SetTraceFreezingTime(int32_t freezingTime)
147 {
148 metaData_.freezingTime = freezingTime;
149 }
150
SetTraceSdkVersion(int32_t version)151 void ContextCallbackImpl::SetTraceSdkVersion(int32_t version)
152 {
153 metaData_.sdkVersion = version;
154 }
155
SetTraceAuthType(int32_t authType)156 void ContextCallbackImpl::SetTraceAuthType(int32_t authType)
157 {
158 metaData_.authType = authType;
159 }
160
SetTraceAuthWidgetType(uint32_t authWidgetType)161 void ContextCallbackImpl::SetTraceAuthWidgetType(uint32_t authWidgetType)
162 {
163 metaData_.authWidgetType = authWidgetType;
164 }
165
SetTraceAuthTrustLevel(AuthTrustLevel atl)166 void ContextCallbackImpl::SetTraceAuthTrustLevel(AuthTrustLevel atl)
167 {
168 metaData_.atl = atl;
169 }
170
SetTraceCallerType(int32_t callerType)171 void ContextCallbackImpl::SetTraceCallerType(int32_t callerType)
172 {
173 metaData_.callerType = callerType;
174 }
175
SetTraceReuseUnlockResultMode(uint32_t reuseUnlockResultMode)176 void ContextCallbackImpl::SetTraceReuseUnlockResultMode(uint32_t reuseUnlockResultMode)
177 {
178 metaData_.reuseUnlockResultMode = reuseUnlockResultMode;
179 }
180
SetTraceReuseUnlockResultDuration(uint64_t reuseUnlockResultDuration)181 void ContextCallbackImpl::SetTraceReuseUnlockResultDuration(uint64_t reuseUnlockResultDuration)
182 {
183 metaData_.reuseUnlockResultDuration = reuseUnlockResultDuration;
184 }
185
SetCleaner(Context::ContextStopCallback callback)186 void ContextCallbackImpl::SetCleaner(Context::ContextStopCallback callback)
187 {
188 stopCallback_ = callback;
189 }
190
GetIamCallback()191 sptr<IamCallbackInterface> ContextCallbackImpl::GetIamCallback()
192 {
193 return iamCallback_;
194 }
195
GetCallerName()196 std::string ContextCallbackImpl::GetCallerName()
197 {
198 if (metaData_.callerName.has_value()) {
199 return metaData_.callerName.value();
200 }
201 return "";
202 }
203
GetInstance()204 ContextCallbackNotifyListener &ContextCallbackNotifyListener::GetInstance()
205 {
206 static ContextCallbackNotifyListener contextCallbackNotifyListener;
207 return contextCallbackNotifyListener;
208 }
209
AddNotifier(const Notify & notify)210 void ContextCallbackNotifyListener::AddNotifier(const Notify ¬ify)
211 {
212 notifierList_.emplace_back(notify);
213 }
214
Process(const MetaData & metaData,TraceFlag flag)215 void ContextCallbackNotifyListener::Process(const MetaData &metaData, TraceFlag flag)
216 {
217 for (const auto ¬ify : notifierList_) {
218 if (notify != nullptr) {
219 notify(metaData, flag);
220 }
221 }
222 }
223
NewInstance(sptr<IamCallbackInterface> iamCallback,OperationType operationType)224 std::shared_ptr<ContextCallback> ContextCallback::NewInstance(sptr<IamCallbackInterface> iamCallback,
225 OperationType operationType)
226 {
227 if (iamCallback == nullptr) {
228 IAM_LOGE("iam callback is nullptr, parameter is invalid");
229 return nullptr;
230 }
231 return UserIam::Common::MakeShared<ContextCallbackImpl>(iamCallback, operationType);
232 }
233
234 class IamDummyCallback : public IamCallbackInterface, public NoCopyable {
235 public:
236 explicit IamDummyCallback() = default;
237 ~IamDummyCallback() override = default;
OnResult(int32_t result,const Attributes & extraInfo)238 void OnResult(int32_t result, const Attributes &extraInfo) override
239 {
240 static_cast<void>(result);
241 static_cast<void>(extraInfo);
242 }
OnAcquireInfo(int32_t module,int32_t acquireInfo,const Attributes & extraInfo)243 void OnAcquireInfo(int32_t module, int32_t acquireInfo, const Attributes &extraInfo) override
244 {
245 static_cast<void>(module);
246 static_cast<void>(acquireInfo);
247 static_cast<void>(extraInfo);
248 }
AsObject()249 sptr<IRemoteObject> AsObject() override
250 {
251 sptr<IRemoteObject> tmp(nullptr);
252 return tmp;
253 }
254 };
255
NewDummyInstance(OperationType operationType)256 std::shared_ptr<ContextCallback> ContextCallback::NewDummyInstance(OperationType operationType)
257 {
258 sptr<IamCallbackInterface> iamDummyCallback(new (std::nothrow) IamDummyCallback());
259 if (iamDummyCallback == nullptr) {
260 IAM_LOGE("iamDummyCallback is nullptr");
261 return nullptr;
262 }
263 return UserIam::Common::MakeShared<ContextCallbackImpl>(iamDummyCallback, operationType);
264 }
265 } // namespace UserAuth
266 } // namespace UserIam
267 } // namespace OHOS