1 /*
2 * Copyright (c) 2022-2024 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 "user_auth_client_impl.h"
17
18 #include "system_ability_definition.h"
19
20 #include "auth_common.h"
21 #include "callback_manager.h"
22 #include "load_mode_client_util.h"
23 #include "iam_check.h"
24 #include "iam_defines.h"
25 #include "iam_logger.h"
26 #include "iam_para2str.h"
27 #include "iam_ptr.h"
28 #include "ipc_client_utils.h"
29 #include "modal_callback_service.h"
30 #include "user_auth_callback_service.h"
31 #include "user_auth_modal_inner_callback.h"
32 #include "widget_callback_service.h"
33
34 #define LOG_TAG "USER_AUTH_SDK"
35 namespace OHOS {
36 namespace UserIam {
37 namespace UserAuth {
38 namespace {
39 const uint32_t MAX_ATTR_COUNT = 512;
40 class NorthAuthenticationCallback : public AuthenticationCallback, public NoCopyable {
41 public:
42 explicit NorthAuthenticationCallback(std::shared_ptr<AuthenticationCallback> innerCallback);
43 void OnAcquireInfo(int32_t module, uint32_t acquireInfo, const Attributes &extraInfo) override;
44 void OnResult(int32_t result, const Attributes &extraInfo) override;
45
46 private:
47 std::shared_ptr<AuthenticationCallback> innerCallback_ = nullptr;
48 };
49
NorthAuthenticationCallback(std::shared_ptr<AuthenticationCallback> innerCallback)50 NorthAuthenticationCallback::NorthAuthenticationCallback(std::shared_ptr<AuthenticationCallback> innerCallback)
51 : innerCallback_(innerCallback) {};
52
OnAcquireInfo(int32_t module,uint32_t acquireInfo,const Attributes & extraInfo)53 void NorthAuthenticationCallback::OnAcquireInfo(int32_t module, uint32_t acquireInfo, const Attributes &extraInfo)
54 {
55 if (innerCallback_ == nullptr) {
56 IAM_LOGE("callback is nullptr");
57 return;
58 }
59
60 if (module == AuthType::FACE) {
61 if (acquireInfo == 0 || acquireInfo > FACE_AUTH_TIP_MAX) {
62 IAM_LOGI("skip undefined face auth tip %{public}u", acquireInfo);
63 return;
64 }
65 } else if (module == AuthType::FINGERPRINT) {
66 if (acquireInfo > FINGERPRINT_AUTH_TIP_MAX) {
67 IAM_LOGI("skip undefined fingerprint auth tip %{public}u", acquireInfo);
68 return;
69 }
70 }
71
72 innerCallback_->OnAcquireInfo(module, acquireInfo, extraInfo);
73 }
74
OnResult(int32_t result,const Attributes & extraInfo)75 void NorthAuthenticationCallback::OnResult(int32_t result, const Attributes &extraInfo)
76 {
77 if (innerCallback_ == nullptr) {
78 IAM_LOGE("callback is nullptr");
79 return;
80 }
81
82 innerCallback_->OnResult(result, extraInfo);
83 }
84 } // namespace
85
GetAvailableStatus(AuthType authType,AuthTrustLevel authTrustLevel)86 int32_t UserAuthClientImpl::GetAvailableStatus(AuthType authType, AuthTrustLevel authTrustLevel)
87 {
88 IAM_LOGI("start, authType:%{public}d authTrustLevel:%{public}u", authType, authTrustLevel);
89 auto proxy = GetProxy();
90 if (!proxy) {
91 return LoadModeUtil::GetProxyNullResultCode(__func__, std::vector<std::string>({
92 ACCESS_USER_AUTH_INTERNAL_PERMISSION,
93 ACCESS_BIOMETRIC_PERMISSION
94 }));
95 }
96
97 int32_t funcResult = SUCCESS;
98 int32_t ret = proxy->GetAvailableStatus(INNER_API_VERSION_10000, static_cast<int32_t>(authType),
99 static_cast<uint32_t>(authTrustLevel), funcResult);
100 if (ret != SUCCESS) {
101 IAM_LOGE("ipc call return fail, ret:%{public}d", ret);
102 return GENERAL_ERROR;
103 }
104 if (funcResult != SUCCESS) {
105 IAM_LOGI("service call return fail, ret:%{public}d", funcResult);
106 return funcResult;
107 }
108 return SUCCESS;
109 }
110
GetNorthAvailableStatus(int32_t apiVersion,AuthType authType,AuthTrustLevel authTrustLevel)111 int32_t UserAuthClientImpl::GetNorthAvailableStatus(int32_t apiVersion, AuthType authType,
112 AuthTrustLevel authTrustLevel)
113 {
114 IAM_LOGI("start, apiVersion:%{public}d authType:%{public}d authTrustLevel:%{public}u",
115 apiVersion, authType, authTrustLevel);
116 auto proxy = GetProxy();
117 if (!proxy) {
118 return LoadModeUtil::GetProxyNullResultCode(__func__, std::vector<std::string>({
119 ACCESS_USER_AUTH_INTERNAL_PERMISSION,
120 ACCESS_BIOMETRIC_PERMISSION
121 }));
122 }
123 int32_t funcResult = SUCCESS;
124 int32_t ret = proxy->GetAvailableStatus(apiVersion, static_cast<int32_t>(authType),
125 static_cast<uint32_t>(authTrustLevel), funcResult);
126 if (ret != SUCCESS) {
127 IAM_LOGE("ipc call return fail, ret:%{public}d", ret);
128 return GENERAL_ERROR;
129 }
130 if (funcResult != SUCCESS) {
131 IAM_LOGI("service call return fail, ret:%{public}d", funcResult);
132 return funcResult;
133 }
134 return SUCCESS;
135 }
136
GetAvailableStatus(int32_t userId,AuthType authType,AuthTrustLevel authTrustLevel)137 int32_t UserAuthClientImpl::GetAvailableStatus(int32_t userId, AuthType authType, AuthTrustLevel authTrustLevel)
138 {
139 IAM_LOGI("start, userId:%{public}d authType:%{public}d authTrustLevel:%{public}u",
140 userId, authType, authTrustLevel);
141 auto proxy = GetProxy();
142 if (!proxy) {
143 return LoadModeUtil::GetProxyNullResultCode(__func__, std::vector<std::string>({
144 ACCESS_USER_AUTH_INTERNAL_PERMISSION
145 }));
146 }
147 int32_t funcResult = SUCCESS;
148 int32_t ret = proxy->GetAvailableStatus(INNER_API_VERSION_10000, userId, static_cast<int32_t>(authType),
149 static_cast<uint32_t>(authTrustLevel), funcResult);
150 if (ret != SUCCESS) {
151 IAM_LOGE("ipc call return fail, ret:%{public}d", ret);
152 return GENERAL_ERROR;
153 }
154 if (funcResult != SUCCESS) {
155 IAM_LOGI("service call return fail, ret:%{public}d", funcResult);
156 return funcResult;
157 }
158 return SUCCESS;
159 }
160
GetProperty(int32_t userId,const GetPropertyRequest & request,const std::shared_ptr<GetPropCallback> & callback)161 void UserAuthClientImpl::GetProperty(int32_t userId, const GetPropertyRequest &request,
162 const std::shared_ptr<GetPropCallback> &callback)
163 {
164 IAM_LOGI("start, userId:%{public}d authType:%{public}d", userId, request.authType);
165 if (!callback) {
166 IAM_LOGE("get prop callback is nullptr");
167 return;
168 }
169
170 auto proxy = GetProxy();
171 if (!proxy) {
172 Attributes extraInfo;
173 int32_t result = LoadModeUtil::GetProxyNullResultCode(__func__, std::vector<std::string>({
174 ACCESS_USER_AUTH_INTERNAL_PERMISSION
175 }));
176 callback->OnResult(result, extraInfo);
177 return;
178 }
179
180 sptr<IGetExecutorPropertyCallback> wrapper(
181 new (std::nothrow) GetExecutorPropertyCallbackService(callback));
182 if (wrapper == nullptr) {
183 IAM_LOGE("failed to create wrapper");
184 Attributes extraInfo;
185 callback->OnResult(GENERAL_ERROR, extraInfo);
186 return;
187 }
188
189 if (request.keys.empty() || request.keys.size() > MAX_ATTR_COUNT) {
190 IAM_LOGE("the attribute key vector is bad param, key size:%{public}zu", request.keys.size());
191 Attributes attr;
192 callback->OnResult(INVALID_PARAMETERS, attr);
193 return;
194 }
195
196 std::vector<uint32_t> attrkeys;
197 attrkeys.resize(request.keys.size());
198 std::transform(request.keys.begin(), request.keys.end(), attrkeys.begin(), [](Attributes::AttributeKey key) {
199 return static_cast<uint32_t>(key);
200 });
201 auto ret = proxy->GetProperty(userId, static_cast<int32_t>(request.authType), attrkeys, wrapper);
202 if (ret != SUCCESS) {
203 IAM_LOGE("GetProperty fail, ret:%{public}d", ret);
204 Attributes extraInfo;
205 callback->OnResult(GENERAL_ERROR, extraInfo);
206 return;
207 }
208 }
209
GetPropertyById(uint64_t credentialId,const std::vector<Attributes::AttributeKey> & keys,const std::shared_ptr<GetPropCallback> & callback)210 void UserAuthClientImpl::GetPropertyById(uint64_t credentialId, const std::vector<Attributes::AttributeKey> &keys,
211 const std::shared_ptr<GetPropCallback> &callback)
212 {
213 IAM_LOGD("start");
214 if (!callback) {
215 IAM_LOGE("get prop callback is nullptr");
216 return;
217 }
218
219 auto proxy = GetProxy();
220 if (!proxy) {
221 Attributes extraInfo;
222 int32_t result = LoadModeUtil::GetProxyNullResultCode(__func__, std::vector<std::string>({
223 ACCESS_USER_AUTH_INTERNAL_PERMISSION
224 }));
225 callback->OnResult(result, extraInfo);
226 return;
227 }
228
229 sptr<IGetExecutorPropertyCallback> wrapper(
230 new (std::nothrow) GetExecutorPropertyCallbackService(callback));
231 if (wrapper == nullptr) {
232 IAM_LOGE("failed to create wrapper");
233 Attributes extraInfo;
234 callback->OnResult(GENERAL_ERROR, extraInfo);
235 return;
236 }
237 if (keys.empty() || keys.size() > MAX_ATTR_COUNT) {
238 IAM_LOGE("the attribute key vector is bad param, key size:%{public}zu", keys.size());
239 Attributes attr;
240 callback->OnResult(INVALID_PARAMETERS, attr);
241 return;
242 }
243 std::vector<uint32_t> attrkeys;
244 attrkeys.resize(keys.size());
245 std::transform(keys.begin(), keys.end(), attrkeys.begin(), [](Attributes::AttributeKey key) {
246 return static_cast<uint32_t>(key);
247 });
248 auto ret = proxy->GetPropertyById(credentialId, attrkeys, wrapper);
249 if (ret != SUCCESS) {
250 IAM_LOGE("GetPropertyById fail, ret:%{public}d", ret);
251 Attributes extraInfo;
252 callback->OnResult(GENERAL_ERROR, extraInfo);
253 return;
254 }
255 }
256
SetPropertyInner(int32_t userId,const SetPropertyRequest & request,const std::shared_ptr<SetPropCallback> & callback)257 ResultCode UserAuthClientImpl::SetPropertyInner(int32_t userId, const SetPropertyRequest &request,
258 const std::shared_ptr<SetPropCallback> &callback)
259 {
260 auto proxy = GetProxy();
261 if (!proxy) {
262 int32_t result = LoadModeUtil::GetProxyNullResultCode(__func__, std::vector<std::string>({
263 ACCESS_USER_AUTH_INTERNAL_PERMISSION
264 }));
265 return (ResultCode)result;
266 }
267
268 auto keys = request.attrs.GetKeys();
269 IF_FALSE_LOGE_AND_RETURN_VAL(keys.size() == 1, GENERAL_ERROR);
270
271 Attributes::AttributeKey key = keys[0];
272 Attributes attr;
273
274 std::vector<uint8_t> extraInfo;
275 bool getArrayRet = request.attrs.GetUint8ArrayValue(static_cast<Attributes::AttributeKey>(key), extraInfo);
276 IF_FALSE_LOGE_AND_RETURN_VAL(getArrayRet, GENERAL_ERROR);
277
278 bool setModeRet = attr.SetUint32Value(Attributes::ATTR_PROPERTY_MODE, static_cast<uint32_t>(key));
279 IF_FALSE_LOGE_AND_RETURN_VAL(setModeRet, GENERAL_ERROR);
280
281 bool setArrayRet = attr.SetUint8ArrayValue(Attributes::ATTR_EXTRA_INFO, extraInfo);
282 IF_FALSE_LOGE_AND_RETURN_VAL(setArrayRet, GENERAL_ERROR);
283
284 sptr<ISetExecutorPropertyCallback> wrapper(
285 new (std::nothrow) SetExecutorPropertyCallbackService(callback));
286 IF_FALSE_LOGE_AND_RETURN_VAL(wrapper != nullptr, GENERAL_ERROR);
287 auto ret = proxy->SetProperty(userId, static_cast<int32_t>(request.authType), attr.Serialize(), wrapper);
288 if (ret != SUCCESS) {
289 IAM_LOGE("SetProperty fail, ret:%{public}d", ret);
290 return (ResultCode)ret;
291 }
292 return SUCCESS;
293 }
294
SetProperty(int32_t userId,const SetPropertyRequest & request,const std::shared_ptr<SetPropCallback> & callback)295 void UserAuthClientImpl::SetProperty(int32_t userId, const SetPropertyRequest &request,
296 const std::shared_ptr<SetPropCallback> &callback)
297 {
298 IAM_LOGI("start, userId:%{public}d authType:%{public}d", userId, request.authType);
299 if (!callback) {
300 IAM_LOGE("set prop callback is nullptr");
301 return;
302 }
303
304 ResultCode result = SetPropertyInner(userId, request, callback);
305 if (result != SUCCESS) {
306 IAM_LOGE("result is not success");
307 Attributes retExtraInfo;
308 callback->OnResult(GENERAL_ERROR, retExtraInfo);
309 return;
310 }
311 }
312
BeginAuthentication(const AuthParam & authParam,const std::shared_ptr<AuthenticationCallback> & callback)313 uint64_t UserAuthClientImpl::BeginAuthentication(const AuthParam &authParam,
314 const std::shared_ptr<AuthenticationCallback> &callback)
315 {
316 IAM_LOGI("start, userId:%{public}d, authType:%{public}d, atl:%{public}u, authIntent:%{public}u,"
317 "remoteAuthParamHasValue:%{public}s", authParam.userId, authParam.authType, authParam.authTrustLevel,
318 authParam.authIntent, Common::GetBoolStr(authParam.remoteAuthParam.has_value()));
319 if (authParam.remoteAuthParam.has_value()) {
320 IAM_LOGI("verifierNetworkIdHasValue:%{public}s collectorNetworkIdHasValue:%{public}s "
321 "collectorTokenIdHasValue:%{public}s",
322 Common::GetBoolStr(authParam.remoteAuthParam->verifierNetworkId.has_value()),
323 Common::GetBoolStr(authParam.remoteAuthParam->collectorNetworkId.has_value()),
324 Common::GetBoolStr(authParam.remoteAuthParam->collectorTokenId.has_value()));
325 }
326
327 if (!callback) {
328 IAM_LOGE("auth callback is nullptr");
329 return BAD_CONTEXT_ID;
330 }
331
332 auto proxy = GetProxy();
333 if (!proxy) {
334 Attributes extraInfo;
335 int32_t result = LoadModeUtil::GetProxyNullResultCode(__func__, std::vector<std::string>({
336 ACCESS_USER_AUTH_INTERNAL_PERMISSION
337 }));
338 callback->OnResult(result, extraInfo);
339 return BAD_CONTEXT_ID;
340 }
341
342 sptr<IIamCallback> wrapper(new (std::nothrow) UserAuthCallbackService(callback));
343 if (wrapper == nullptr) {
344 IAM_LOGE("failed to create wrapper");
345 Attributes extraInfo;
346 callback->OnResult(GENERAL_ERROR, extraInfo);
347 return BAD_CONTEXT_ID;
348 }
349 uint64_t contextId = BAD_CONTEXT_ID;
350 IpcAuthParamInner ipcAuthParamInner = {
351 .userId = authParam.userId,
352 .challenge = authParam.challenge,
353 .authType = static_cast<int32_t>(authParam.authType),
354 .authTrustLevel = static_cast<int32_t>(authParam.authTrustLevel),
355 .authIntent = static_cast<int32_t>(authParam.authIntent),
356 };
357 IpcRemoteAuthParam ipcRemoteAuthParam = {};
358 InitIpcRemoteAuthParam(authParam.remoteAuthParam, ipcRemoteAuthParam);
359 auto ret = proxy->AuthUser(ipcAuthParamInner, ipcRemoteAuthParam, wrapper, contextId);
360 if (ret != SUCCESS) {
361 IAM_LOGE("AuthUser fail, ret:%{public}d", ret);
362 return BAD_CONTEXT_ID;
363 }
364 return contextId;
365 }
366
BeginNorthAuthentication(int32_t apiVersion,const std::vector<uint8_t> & challenge,AuthType authType,AuthTrustLevel atl,const std::shared_ptr<AuthenticationCallback> & callback)367 uint64_t UserAuthClientImpl::BeginNorthAuthentication(int32_t apiVersion, const std::vector<uint8_t> &challenge,
368 AuthType authType, AuthTrustLevel atl, const std::shared_ptr<AuthenticationCallback> &callback)
369 {
370 IAM_LOGI("start, apiVersion:%{public}d authType:%{public}d atl:%{public}u", apiVersion, authType, atl);
371 if (!callback) {
372 IAM_LOGE("auth callback is nullptr");
373 return BAD_CONTEXT_ID;
374 }
375
376 auto northCallback = Common::MakeShared<NorthAuthenticationCallback>(callback);
377 if (!northCallback) {
378 IAM_LOGE("auth callback is nullptr");
379 Attributes extraInfo;
380 callback->OnResult(GENERAL_ERROR, extraInfo);
381 return BAD_CONTEXT_ID;
382 }
383
384 auto proxy = GetProxy();
385 if (!proxy) {
386 Attributes extraInfo;
387 int32_t result = LoadModeUtil::GetProxyNullResultCode(__func__, std::vector<std::string>({
388 ACCESS_BIOMETRIC_PERMISSION
389 }));
390 callback->OnResult(result, extraInfo);
391 return BAD_CONTEXT_ID;
392 }
393
394 sptr<IIamCallback> wrapper(new (std::nothrow) UserAuthCallbackService(northCallback));
395 if (wrapper == nullptr) {
396 IAM_LOGE("failed to create wrapper");
397 Attributes extraInfo;
398 callback->OnResult(GENERAL_ERROR, extraInfo);
399 return BAD_CONTEXT_ID;
400 }
401 uint64_t contextId = BAD_CONTEXT_ID;
402 IpcAuthParamInner authParamInner = {
403 .challenge = challenge,
404 .authType = static_cast<int32_t>(authType),
405 .authTrustLevel = static_cast<int32_t>(atl)
406 };
407 auto ret = proxy->Auth(apiVersion, authParamInner, wrapper, contextId);
408 if (ret != SUCCESS) {
409 IAM_LOGE("Auth fail, ret:%{public}d", ret);
410 return BAD_CONTEXT_ID;
411 }
412 return contextId;
413 }
414
CancelAuthentication(uint64_t contextId)415 int32_t UserAuthClientImpl::CancelAuthentication(uint64_t contextId)
416 {
417 IAM_LOGI("start");
418 auto proxy = GetProxy();
419 if (!proxy) {
420 IAM_LOGE("proxy is nullptr");
421 return GENERAL_ERROR;
422 }
423
424 return proxy->CancelAuthOrIdentify(contextId, CancelReason::ORIGINAL_CANCEL);
425 }
426
BeginIdentification(const std::vector<uint8_t> & challenge,AuthType authType,const std::shared_ptr<IdentificationCallback> & callback)427 uint64_t UserAuthClientImpl::BeginIdentification(const std::vector<uint8_t> &challenge, AuthType authType,
428 const std::shared_ptr<IdentificationCallback> &callback)
429 {
430 IAM_LOGI("start, authType:%{public}d", authType);
431 if (!callback) {
432 IAM_LOGE("identify callback is nullptr");
433 return BAD_CONTEXT_ID;
434 }
435
436 auto proxy = GetProxy();
437 if (!proxy) {
438 Attributes extraInfo;
439 int32_t result = LoadModeUtil::GetProxyNullResultCode(__func__, std::vector<std::string>({
440 ACCESS_USER_AUTH_INTERNAL_PERMISSION
441 }));
442 callback->OnResult(result, extraInfo);
443 return BAD_CONTEXT_ID;
444 }
445
446 sptr<IIamCallback> wrapper(new (std::nothrow) UserAuthCallbackService(callback));
447 if (wrapper == nullptr) {
448 IAM_LOGE("failed to create wrapper");
449 Attributes extraInfo;
450 callback->OnResult(GENERAL_ERROR, extraInfo);
451 return BAD_CONTEXT_ID;
452 }
453 uint64_t contextId = BAD_CONTEXT_ID;
454 auto ret = proxy->Identify(challenge, static_cast<int32_t>(authType), wrapper, contextId);
455 if (ret != SUCCESS) {
456 IAM_LOGE("Identify fail, ret:%{public}d", ret);
457 return BAD_CONTEXT_ID;
458 }
459 return contextId;
460 }
461
CancelIdentification(uint64_t contextId)462 int32_t UserAuthClientImpl::CancelIdentification(uint64_t contextId)
463 {
464 IAM_LOGI("start");
465 auto proxy = GetProxy();
466 if (!proxy) {
467 IAM_LOGE("proxy is nullptr");
468 return GENERAL_ERROR;
469 }
470
471 return proxy->CancelAuthOrIdentify(contextId, CancelReason::ORIGINAL_CANCEL);
472 }
473
GetVersion(int32_t & version)474 int32_t UserAuthClientImpl::GetVersion(int32_t &version)
475 {
476 IAM_LOGI("start");
477 auto proxy = GetProxy();
478 if (!proxy) {
479 IAM_LOGE("proxy is nullptr");
480 return GENERAL_ERROR;
481 }
482
483 return proxy->GetVersion(version);
484 }
485
SetGlobalConfigParam(const GlobalConfigParam & param)486 int32_t UserAuthClientImpl::SetGlobalConfigParam(const GlobalConfigParam ¶m)
487 {
488 IAM_LOGI("start");
489 auto proxy = GetProxy();
490 if (!proxy) {
491 IAM_LOGE("proxy is nullptr");
492 return GENERAL_ERROR;
493 }
494 IpcGlobalConfigParam ipcGlobalConfigParam = {};
495 InitIpcGlobalConfigParam(param, ipcGlobalConfigParam);
496 return proxy->SetGlobalConfigParam(ipcGlobalConfigParam);
497 }
498
GetProxy()499 sptr<IUserAuth> UserAuthClientImpl::GetProxy()
500 {
501 std::lock_guard<std::mutex> lock(mutex_);
502 if (proxy_ != nullptr) {
503 return proxy_;
504 }
505 sptr<IRemoteObject> obj = IpcClientUtils::GetRemoteObject(SUBSYS_USERIAM_SYS_ABILITY_USERAUTH);
506 if (obj == nullptr) {
507 IAM_LOGE("remote object is null");
508 return proxy_;
509 }
510 sptr<IRemoteObject::DeathRecipient> dr(new (std::nothrow) UserAuthImplDeathRecipient());
511 if ((dr == nullptr) || (obj->IsProxyObject() && !obj->AddDeathRecipient(dr))) {
512 IAM_LOGE("add death recipient fail");
513 return proxy_;
514 }
515
516 proxy_ = iface_cast<IUserAuth>(obj);
517 deathRecipient_ = dr;
518 return proxy_;
519 }
520
ResetProxy(const wptr<IRemoteObject> & remote)521 void UserAuthClientImpl::ResetProxy(const wptr<IRemoteObject> &remote)
522 {
523 IAM_LOGI("start");
524 std::lock_guard<std::mutex> lock(mutex_);
525 if (proxy_ == nullptr) {
526 IAM_LOGE("proxy_ is null");
527 return;
528 }
529 auto serviceRemote = proxy_->AsObject();
530 if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
531 IAM_LOGI("need reset");
532 serviceRemote->RemoveDeathRecipient(deathRecipient_);
533 proxy_ = nullptr;
534 deathRecipient_ = nullptr;
535 }
536 IAM_LOGI("end reset proxy");
537 }
538
OnRemoteDied(const wptr<IRemoteObject> & remote)539 void UserAuthClientImpl::UserAuthImplDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
540 {
541 IAM_LOGI("start");
542 if (remote == nullptr) {
543 IAM_LOGE("remote is nullptr");
544 return;
545 }
546 CallbackManager::GetInstance().OnServiceDeath();
547 EventListenerCallbackManager::GetInstance().OnServiceDeath();
548 UserAuthClientImpl::Instance().ResetProxy(remote);
549 }
550
Instance()551 UserAuthClientImpl &UserAuthClientImpl::Instance()
552 {
553 static UserAuthClientImpl impl;
554 return impl;
555 }
556
GetInstance()557 UserAuthClient &UserAuthClient::GetInstance()
558 {
559 return UserAuthClientImpl::Instance();
560 }
561
BeginWidgetAuth(const WidgetAuthParam & authParam,const WidgetParam & widgetParam,const std::shared_ptr<AuthenticationCallback> & callback)562 uint64_t UserAuthClientImpl::BeginWidgetAuth(const WidgetAuthParam &authParam, const WidgetParam &widgetParam,
563 const std::shared_ptr<AuthenticationCallback> &callback)
564 {
565 IAM_LOGI("start, authTypeSize:%{public}zu authTrustLevel:%{public}u", authParam.authTypes.size(),
566 authParam.authTrustLevel);
567 AuthParamInner authParamInner = {
568 .userId = authParam.userId,
569 .isUserIdSpecified = true,
570 .challenge = authParam.challenge,
571 .authTypes = authParam.authTypes,
572 .authTrustLevel = authParam.authTrustLevel,
573 .reuseUnlockResult = authParam.reuseUnlockResult,
574 };
575 WidgetParamInner widgetParamInner = {
576 .title = widgetParam.title,
577 .navigationButtonText = widgetParam.navigationButtonText,
578 .windowMode = widgetParam.windowMode,
579 .hasContext = false,
580 };
581 return BeginWidgetAuthInner(INNER_API_VERSION_20000, authParamInner, widgetParamInner, callback);
582 }
583
BeginWidgetAuth(int32_t apiVersion,const WidgetAuthParam & authParam,const WidgetParam & widgetParam,const std::shared_ptr<AuthenticationCallback> & callback)584 uint64_t UserAuthClientImpl::BeginWidgetAuth(int32_t apiVersion, const WidgetAuthParam &authParam,
585 const WidgetParam &widgetParam, const std::shared_ptr<AuthenticationCallback> &callback)
586 {
587 IAM_LOGI("start, apiVersion:%{public}d authTypeSize:%{public}zu authTrustLevel:%{public}u",
588 apiVersion, authParam.authTypes.size(), authParam.authTrustLevel);
589
590 AuthParamInner authParamInner = {
591 .isUserIdSpecified = false,
592 .challenge = authParam.challenge,
593 .authTypes = authParam.authTypes,
594 .authTrustLevel = authParam.authTrustLevel,
595 .reuseUnlockResult = authParam.reuseUnlockResult,
596 };
597 WidgetParamInner widgetParamInner = {
598 .title = widgetParam.title,
599 .navigationButtonText = widgetParam.navigationButtonText,
600 .windowMode = widgetParam.windowMode,
601 .hasContext = false,
602 };
603 return BeginWidgetAuthInner(apiVersion, authParamInner, widgetParamInner, callback);
604 }
605
BeginWidgetAuthInner(int32_t apiVersion,const AuthParamInner & authParam,const WidgetParamInner & widgetParam,const std::shared_ptr<AuthenticationCallback> & callback)606 uint64_t UserAuthClientImpl::BeginWidgetAuthInner(int32_t apiVersion, const AuthParamInner &authParam,
607 const WidgetParamInner &widgetParam, const std::shared_ptr<AuthenticationCallback> &callback)
608 {
609 if (!callback) {
610 IAM_LOGE("auth callback is nullptr");
611 return BAD_CONTEXT_ID;
612 }
613 auto proxy = GetProxy();
614 if (!proxy) {
615 Attributes extraInfo;
616 int32_t result = LoadModeUtil::GetProxyNullResultCode(__func__, std::vector<std::string>({
617 ACCESS_USER_AUTH_INTERNAL_PERMISSION,
618 ACCESS_BIOMETRIC_PERMISSION
619 }));
620 callback->OnResult(result, extraInfo);
621 return BAD_CONTEXT_ID;
622 }
623
624 sptr<IIamCallback> wrapper(new (std::nothrow) UserAuthCallbackService(callback));
625 if (wrapper == nullptr) {
626 IAM_LOGE("failed to create wrapper");
627 Attributes extraInfo;
628 callback->OnResult(static_cast<int32_t>(ResultCode::GENERAL_ERROR), extraInfo);
629 return BAD_CONTEXT_ID;
630 }
631
632 // modal
633 const std::shared_ptr<UserAuthModalInnerCallback> &modalCallback = Common::MakeShared<UserAuthModalInnerCallback>();
634 sptr<IModalCallback> wrapperModal(new (std::nothrow) ModalCallbackService(modalCallback));
635 if (wrapperModal == nullptr) {
636 IAM_LOGE("failed to create wrapper for modal");
637 Attributes extraInfo;
638 callback->OnResult(static_cast<int32_t>(ResultCode::GENERAL_ERROR), extraInfo);
639 return BAD_CONTEXT_ID;
640 }
641 IpcAuthParamInner ipcAuthParamInner = {};
642 IpcWidgetParamInner ipcWidgetParamInner = {};
643 InitIpcAuthParam(authParam, ipcAuthParamInner);
644 InitIpcWidgetParam(widgetParam, ipcWidgetParamInner);
645 uint64_t contextId = BAD_CONTEXT_ID;
646 auto ret = proxy->AuthWidget(apiVersion, ipcAuthParamInner, ipcWidgetParamInner, wrapper, wrapperModal, contextId);
647 if (ret != SUCCESS) {
648 IAM_LOGE("AuthWidget fail, ret:%{public}d", ret);
649 return BAD_CONTEXT_ID;
650 }
651 return contextId;
652 }
653
SetWidgetCallback(int32_t version,const std::shared_ptr<IUserAuthWidgetCallback> & callback)654 int32_t UserAuthClientImpl::SetWidgetCallback(int32_t version, const std::shared_ptr<IUserAuthWidgetCallback> &callback)
655 {
656 IAM_LOGI("start, version:%{public}d", version);
657 if (!callback) {
658 IAM_LOGE("widget callback is nullptr");
659 return GENERAL_ERROR;
660 }
661 auto proxy = GetProxy();
662 if (!proxy) {
663 IAM_LOGE("proxy is nullptr");
664 return GENERAL_ERROR;
665 }
666
667 sptr<IWidgetCallback> wrapper(new (std::nothrow) WidgetCallbackService(callback));
668 if (wrapper == nullptr) {
669 IAM_LOGE("failed to create wrapper");
670 return GENERAL_ERROR;
671 }
672 return proxy->RegisterWidgetCallback(version, wrapper);
673 }
674
Notice(NoticeType noticeType,const std::string & eventData)675 int32_t UserAuthClientImpl::Notice(NoticeType noticeType, const std::string &eventData)
676 {
677 IAM_LOGI("start, noticeType:%{public}d", noticeType);
678 auto proxy = GetProxy();
679 if (!proxy) {
680 IAM_LOGE("proxy is nullptr");
681 return GENERAL_ERROR;
682 }
683 IAM_LOGI("UserAuthClientImpl::Notice noticeType:%{public}d, eventDat:%{public}s",
684 static_cast<int32_t>(noticeType), eventData.c_str());
685 return proxy->Notice(static_cast<int32_t>(noticeType), eventData);
686 }
687
GetEnrolledState(int32_t apiVersion,AuthType authType,EnrolledState & enrolledState)688 int32_t UserAuthClientImpl::GetEnrolledState(int32_t apiVersion, AuthType authType, EnrolledState &enrolledState)
689 {
690 IAM_LOGI("start, apiVersion:%{public}d authType:%{public}d ", apiVersion, authType);
691 auto proxy = GetProxy();
692 if (!proxy) {
693 return LoadModeUtil::GetProxyNullResultCode(__func__, std::vector<std::string>({
694 ACCESS_BIOMETRIC_PERMISSION
695 }));
696 }
697 IpcEnrolledState ipcEnrolledState = {};
698 int32_t funcResult = SUCCESS;
699 int32_t ret = proxy->GetEnrolledState(apiVersion, static_cast<int32_t>(authType), ipcEnrolledState, funcResult);
700 if (ret != SUCCESS) {
701 IAM_LOGE("ipc call return fail, ret:%{public}d", ret);
702 return GENERAL_ERROR;
703 }
704 if (funcResult != SUCCESS) {
705 IAM_LOGI("service call return fail, ret:%{public}d", funcResult);
706 return funcResult;
707 }
708 enrolledState.credentialCount = ipcEnrolledState.credentialCount;
709 enrolledState.credentialDigest = ipcEnrolledState.credentialDigest;
710 return SUCCESS;
711 }
712
RegistUserAuthSuccessEventListener(const std::vector<AuthType> & authTypes,const std::shared_ptr<AuthSuccessEventListener> & listener)713 int32_t UserAuthClientImpl::RegistUserAuthSuccessEventListener(const std::vector<AuthType> &authTypes,
714 const std::shared_ptr<AuthSuccessEventListener> &listener)
715 {
716 IAM_LOGI("start");
717
718 auto proxy = GetProxy();
719 IF_FALSE_LOGE_AND_RETURN_VAL(proxy != nullptr, GENERAL_ERROR);
720
721 return EventListenerCallbackManager::GetInstance().AddUserAuthSuccessEventListener(proxy, authTypes, listener);
722 }
723
UnRegistUserAuthSuccessEventListener(const std::shared_ptr<AuthSuccessEventListener> & listener)724 int32_t UserAuthClientImpl::UnRegistUserAuthSuccessEventListener(
725 const std::shared_ptr<AuthSuccessEventListener> &listener)
726 {
727 IAM_LOGI("start");
728
729 auto proxy = GetProxy();
730 IF_FALSE_LOGE_AND_RETURN_VAL(proxy != nullptr, GENERAL_ERROR);
731
732 return EventListenerCallbackManager::GetInstance().RemoveUserAuthSuccessEventListener(proxy, listener);
733 }
734
PrepareRemoteAuth(const std::string & networkId,const std::shared_ptr<PrepareRemoteAuthCallback> & callback)735 int32_t UserAuthClientImpl::PrepareRemoteAuth(const std::string &networkId,
736 const std::shared_ptr<PrepareRemoteAuthCallback> &callback)
737 {
738 IAM_LOGI("start");
739 if (!callback) {
740 IAM_LOGE("prepare remote auth callback is nullptr");
741 return GENERAL_ERROR;
742 }
743
744 auto proxy = GetProxy();
745 if (!proxy) {
746 IAM_LOGE("proxy is nullptr");
747 callback->OnResult(GENERAL_ERROR);
748 return GENERAL_ERROR;
749 }
750
751 sptr<IIamCallback> wrapper(new (std::nothrow) UserAuthCallbackService(callback));
752 if (wrapper == nullptr) {
753 IAM_LOGE("failed to create wrapper");
754 callback->OnResult(GENERAL_ERROR);
755 return GENERAL_ERROR;
756 }
757
758 return proxy->PrepareRemoteAuth(networkId, wrapper);
759 }
760
QueryReusableAuthResult(const WidgetAuthParam & authParam,std::vector<uint8_t> & token)761 int32_t UserAuthClientImpl::QueryReusableAuthResult(const WidgetAuthParam &authParam, std::vector<uint8_t> &token)
762 {
763 IAM_LOGI("start");
764 auto proxy = GetProxy();
765 if (!proxy) {
766 IAM_LOGE("proxy is nullptr");
767 return GENERAL_ERROR;
768 }
769
770 IpcAuthParamInner ipcAuthParamInner = {};
771 ipcAuthParamInner.userId = authParam.userId;
772 ipcAuthParamInner.isUserIdSpecified = (authParam.userId != INVALID_USER_ID);
773 ipcAuthParamInner.challenge = authParam.challenge;
774 for (auto &authType : authParam.authTypes) {
775 ipcAuthParamInner.authTypes.push_back(static_cast<int32_t>(authType));
776 }
777 ipcAuthParamInner.authTrustLevel = authParam.authTrustLevel,
778 ipcAuthParamInner.reuseUnlockResult.isReuse = authParam.reuseUnlockResult.isReuse;
779 ipcAuthParamInner.reuseUnlockResult.reuseMode = authParam.reuseUnlockResult.reuseMode;
780 ipcAuthParamInner.reuseUnlockResult.reuseDuration = authParam.reuseUnlockResult.reuseDuration;
781
782 return proxy->QueryReusableAuthResult(ipcAuthParamInner, token);
783 }
784
InitIpcRemoteAuthParam(const std::optional<RemoteAuthParam> & remoteAuthParam,IpcRemoteAuthParam & ipcRemoteAuthParam)785 void UserAuthClientImpl::InitIpcRemoteAuthParam(const std::optional<RemoteAuthParam> &remoteAuthParam,
786 IpcRemoteAuthParam &ipcRemoteAuthParam)
787 {
788 ipcRemoteAuthParam.isHasRemoteAuthParam = false;
789 ipcRemoteAuthParam.isHasVerifierNetworkId = false;
790 ipcRemoteAuthParam.isHasCollectorNetworkId = false;
791 ipcRemoteAuthParam.isHasCollectorTokenId = false;
792 if (remoteAuthParam.has_value()) {
793 ipcRemoteAuthParam.isHasRemoteAuthParam = true;
794 if (remoteAuthParam.value().verifierNetworkId.has_value()) {
795 ipcRemoteAuthParam.isHasVerifierNetworkId = true;
796 ipcRemoteAuthParam.verifierNetworkId = remoteAuthParam.value().verifierNetworkId.value();
797 }
798 if (remoteAuthParam.value().collectorNetworkId.has_value()) {
799 ipcRemoteAuthParam.isHasCollectorNetworkId = true;
800 ipcRemoteAuthParam.collectorNetworkId = remoteAuthParam.value().collectorNetworkId.value();
801 }
802 if (remoteAuthParam.value().collectorTokenId.has_value()) {
803 ipcRemoteAuthParam.isHasCollectorTokenId = true;
804 ipcRemoteAuthParam.collectorTokenId = remoteAuthParam.value().collectorTokenId.value();
805 }
806 }
807 }
808
InitIpcGlobalConfigParam(const GlobalConfigParam & globalConfigParam,IpcGlobalConfigParam & ipcGlobalConfigParam)809 void UserAuthClientImpl::InitIpcGlobalConfigParam(const GlobalConfigParam &globalConfigParam,
810 IpcGlobalConfigParam &ipcGlobalConfigParam)
811 {
812 ipcGlobalConfigParam.type = static_cast<int32_t>(globalConfigParam.type);
813 if (globalConfigParam.type == PIN_EXPIRED_PERIOD) {
814 ipcGlobalConfigParam.value.pinExpiredPeriod = globalConfigParam.value.pinExpiredPeriod;
815 } else if (globalConfigParam.type == ENABLE_STATUS) {
816 ipcGlobalConfigParam.value.enableStatus = globalConfigParam.value.enableStatus;
817 }
818 ipcGlobalConfigParam.userIds = globalConfigParam.userIds;
819 ipcGlobalConfigParam.authTypes.resize(globalConfigParam.authTypes.size());
820 std::transform(globalConfigParam.authTypes.begin(), globalConfigParam.authTypes.end(),
821 ipcGlobalConfigParam.authTypes.begin(), [](AuthType authType) {
822 return static_cast<int32_t>(authType);
823 });
824 }
825
InitIpcAuthParam(const AuthParamInner & authParam,IpcAuthParamInner & ipcAuthParamInner)826 void UserAuthClientImpl::InitIpcAuthParam(const AuthParamInner &authParam,
827 IpcAuthParamInner &ipcAuthParamInner)
828 {
829 ipcAuthParamInner.userId = authParam.userId;
830 ipcAuthParamInner.isUserIdSpecified = authParam.isUserIdSpecified;
831 ipcAuthParamInner.challenge = authParam.challenge;
832 ipcAuthParamInner.authType = static_cast<int32_t>(authParam.authType);
833 ipcAuthParamInner.authTypes.resize(authParam.authTypes.size());
834 std::transform(authParam.authTypes.begin(), authParam.authTypes.end(),
835 ipcAuthParamInner.authTypes.begin(), [](AuthType authType) {
836 return static_cast<int32_t>(authType);
837 });
838 ipcAuthParamInner.authTrustLevel = static_cast<uint32_t>(authParam.authTrustLevel);
839 ipcAuthParamInner.authIntent = static_cast<int32_t>(authParam.authIntent);
840 ipcAuthParamInner.skipLockedBiometricAuth = authParam.skipLockedBiometricAuth;
841 ipcAuthParamInner.reuseUnlockResult.isReuse = authParam.reuseUnlockResult.isReuse;
842 ipcAuthParamInner.reuseUnlockResult.reuseMode = authParam.reuseUnlockResult.reuseMode;
843 ipcAuthParamInner.reuseUnlockResult.reuseDuration = authParam.reuseUnlockResult.reuseDuration;
844 }
845
InitIpcWidgetParam(const WidgetParamInner & widgetParam,IpcWidgetParamInner & ipcWidgetParamInner)846 void UserAuthClientImpl::InitIpcWidgetParam(const WidgetParamInner &widgetParam,
847 IpcWidgetParamInner &ipcWidgetParamInner)
848 {
849 ipcWidgetParamInner.title = widgetParam.title;
850 ipcWidgetParamInner.navigationButtonText = widgetParam.navigationButtonText;
851 ipcWidgetParamInner.windowMode = static_cast<int32_t>(widgetParam.windowMode);
852 ipcWidgetParamInner.hasContext = widgetParam.hasContext;
853 }
854
CleanUpResource()855 void UserAuthClientImpl::CleanUpResource()
856 {
857 IAM_LOGI("start");
858 IF_FALSE_LOGE_AND_RETURN(proxy_ != nullptr);
859 ResetProxy(proxy_->AsObject());
860 }
861
~UserAuthClientImpl()862 UserAuthClientImpl::~UserAuthClientImpl()
863 {
864 IAM_LOGI("start");
865 CleanUpResource();
866 }
867
CleanUp()868 extern "C" __attribute__((destructor)) void CleanUp()
869 {
870 UserAuthClientImpl::Instance().CleanUpResource();
871 }
872 } // namespace UserAuth
873 } // namespace UserIam
874 } // namespace OHOS
875