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 class NorthAuthenticationCallback : public AuthenticationCallback, public NoCopyable {
40 public:
41 explicit NorthAuthenticationCallback(std::shared_ptr<AuthenticationCallback> innerCallback);
42 void OnAcquireInfo(int32_t module, uint32_t acquireInfo, const Attributes &extraInfo) override;
43 void OnResult(int32_t result, const Attributes &extraInfo) override;
44
45 private:
46 std::shared_ptr<AuthenticationCallback> innerCallback_ = nullptr;
47 };
48
NorthAuthenticationCallback(std::shared_ptr<AuthenticationCallback> innerCallback)49 NorthAuthenticationCallback::NorthAuthenticationCallback(std::shared_ptr<AuthenticationCallback> innerCallback)
50 : innerCallback_(innerCallback) {};
51
OnAcquireInfo(int32_t module,uint32_t acquireInfo,const Attributes & extraInfo)52 void NorthAuthenticationCallback::OnAcquireInfo(int32_t module, uint32_t acquireInfo, const Attributes &extraInfo)
53 {
54 if (innerCallback_ == nullptr) {
55 IAM_LOGE("callback is nullptr");
56 return;
57 }
58
59 if (module == AuthType::FACE) {
60 if (acquireInfo == 0 || acquireInfo > FACE_AUTH_TIP_MAX) {
61 IAM_LOGI("skip undefined face auth tip %{public}u", acquireInfo);
62 return;
63 }
64 } else if (module == AuthType::FINGERPRINT) {
65 if (acquireInfo > FINGERPRINT_AUTH_TIP_MAX) {
66 IAM_LOGI("skip undefined fingerprint auth tip %{public}u", acquireInfo);
67 return;
68 }
69 }
70
71 innerCallback_->OnAcquireInfo(module, acquireInfo, extraInfo);
72 }
73
OnResult(int32_t result,const Attributes & extraInfo)74 void NorthAuthenticationCallback::OnResult(int32_t result, const Attributes &extraInfo)
75 {
76 if (innerCallback_ == nullptr) {
77 IAM_LOGE("callback is nullptr");
78 return;
79 }
80
81 innerCallback_->OnResult(result, extraInfo);
82 }
83 } // namespace
84
GetAvailableStatus(AuthType authType,AuthTrustLevel authTrustLevel)85 int32_t UserAuthClientImpl::GetAvailableStatus(AuthType authType, AuthTrustLevel authTrustLevel)
86 {
87 IAM_LOGI("start, authType:%{public}d authTrustLevel:%{public}u", authType, authTrustLevel);
88 auto proxy = GetProxy();
89 if (!proxy) {
90 return LoadModeUtil::GetProxyNullResultCode(__func__, std::vector<std::string>({
91 ACCESS_USER_AUTH_INTERNAL_PERMISSION,
92 ACCESS_BIOMETRIC_PERMISSION
93 }));
94 }
95 return proxy->GetAvailableStatus(INNER_API_VERSION_10000, authType, authTrustLevel);
96 }
97
GetNorthAvailableStatus(int32_t apiVersion,AuthType authType,AuthTrustLevel authTrustLevel)98 int32_t UserAuthClientImpl::GetNorthAvailableStatus(int32_t apiVersion, AuthType authType,
99 AuthTrustLevel authTrustLevel)
100 {
101 IAM_LOGI("start, apiVersion:%{public}d authType:%{public}d authTrustLevel:%{public}u",
102 apiVersion, authType, authTrustLevel);
103 auto proxy = GetProxy();
104 if (!proxy) {
105 return LoadModeUtil::GetProxyNullResultCode(__func__, std::vector<std::string>({
106 ACCESS_USER_AUTH_INTERNAL_PERMISSION,
107 ACCESS_BIOMETRIC_PERMISSION
108 }));
109 }
110 return proxy->GetAvailableStatus(apiVersion, authType, authTrustLevel);
111 }
112
GetAvailableStatus(int32_t userId,AuthType authType,AuthTrustLevel authTrustLevel)113 int32_t UserAuthClientImpl::GetAvailableStatus(int32_t userId, AuthType authType, AuthTrustLevel authTrustLevel)
114 {
115 IAM_LOGI("start, userId:%{public}d authType:%{public}d authTrustLevel:%{public}u",
116 userId, authType, authTrustLevel);
117 auto proxy = GetProxy();
118 if (!proxy) {
119 return LoadModeUtil::GetProxyNullResultCode(__func__, std::vector<std::string>({
120 ACCESS_USER_AUTH_INTERNAL_PERMISSION
121 }));
122 }
123 return proxy->GetAvailableStatus(INNER_API_VERSION_10000, userId, authType, authTrustLevel);
124 }
125
GetProperty(int32_t userId,const GetPropertyRequest & request,const std::shared_ptr<GetPropCallback> & callback)126 void UserAuthClientImpl::GetProperty(int32_t userId, const GetPropertyRequest &request,
127 const std::shared_ptr<GetPropCallback> &callback)
128 {
129 IAM_LOGI("start, userId:%{public}d authType:%{public}d", userId, request.authType);
130 if (!callback) {
131 IAM_LOGE("get prop callback is nullptr");
132 return;
133 }
134
135 auto proxy = GetProxy();
136 if (!proxy) {
137 Attributes extraInfo;
138 int32_t result = LoadModeUtil::GetProxyNullResultCode(__func__, std::vector<std::string>({
139 ACCESS_USER_AUTH_INTERNAL_PERMISSION
140 }));
141 callback->OnResult(result, extraInfo);
142 return;
143 }
144
145 sptr<GetExecutorPropertyCallbackInterface> wrapper(
146 new (std::nothrow) GetExecutorPropertyCallbackService(callback));
147 if (wrapper == nullptr) {
148 IAM_LOGE("failed to create wrapper");
149 Attributes extraInfo;
150 callback->OnResult(GENERAL_ERROR, extraInfo);
151 return;
152 }
153 proxy->GetProperty(userId, request.authType, request.keys, wrapper);
154 }
155
GetPropertyById(uint64_t credentialId,const std::vector<Attributes::AttributeKey> & keys,const std::shared_ptr<GetPropCallback> & callback)156 void UserAuthClientImpl::GetPropertyById(uint64_t credentialId, const std::vector<Attributes::AttributeKey> &keys,
157 const std::shared_ptr<GetPropCallback> &callback)
158 {
159 IAM_LOGI("start");
160 if (!callback) {
161 IAM_LOGE("get prop callback is nullptr");
162 return;
163 }
164
165 auto proxy = GetProxy();
166 if (!proxy) {
167 Attributes extraInfo;
168 int32_t result = LoadModeUtil::GetProxyNullResultCode(__func__, std::vector<std::string>({
169 ACCESS_USER_AUTH_INTERNAL_PERMISSION
170 }));
171 callback->OnResult(result, extraInfo);
172 return;
173 }
174
175 sptr<GetExecutorPropertyCallbackInterface> wrapper(
176 new (std::nothrow) GetExecutorPropertyCallbackService(callback));
177 if (wrapper == nullptr) {
178 IAM_LOGE("failed to create wrapper");
179 Attributes extraInfo;
180 callback->OnResult(GENERAL_ERROR, extraInfo);
181 return;
182 }
183 proxy->GetPropertyById(credentialId, keys, wrapper);
184 }
185
SetPropertyInner(int32_t userId,const SetPropertyRequest & request,const std::shared_ptr<SetPropCallback> & callback)186 ResultCode UserAuthClientImpl::SetPropertyInner(int32_t userId, const SetPropertyRequest &request,
187 const std::shared_ptr<SetPropCallback> &callback)
188 {
189 auto proxy = GetProxy();
190 if (!proxy) {
191 int32_t result = LoadModeUtil::GetProxyNullResultCode(__func__, std::vector<std::string>({
192 ACCESS_USER_AUTH_INTERNAL_PERMISSION
193 }));
194 return (ResultCode)result;
195 }
196
197 auto keys = request.attrs.GetKeys();
198 IF_FALSE_LOGE_AND_RETURN_VAL(keys.size() == 1, GENERAL_ERROR);
199
200 Attributes::AttributeKey key = keys[0];
201 Attributes attr;
202
203 std::vector<uint8_t> extraInfo;
204 bool getArrayRet = request.attrs.GetUint8ArrayValue(static_cast<Attributes::AttributeKey>(key), extraInfo);
205 IF_FALSE_LOGE_AND_RETURN_VAL(getArrayRet, GENERAL_ERROR);
206
207 bool setModeRet = attr.SetUint32Value(Attributes::ATTR_PROPERTY_MODE, static_cast<uint32_t>(key));
208 IF_FALSE_LOGE_AND_RETURN_VAL(setModeRet, GENERAL_ERROR);
209
210 bool setArrayRet = attr.SetUint8ArrayValue(Attributes::ATTR_EXTRA_INFO, extraInfo);
211 IF_FALSE_LOGE_AND_RETURN_VAL(setArrayRet, GENERAL_ERROR);
212
213 sptr<SetExecutorPropertyCallbackInterface> wrapper(
214 new (std::nothrow) SetExecutorPropertyCallbackService(callback));
215 IF_FALSE_LOGE_AND_RETURN_VAL(wrapper != nullptr, GENERAL_ERROR);
216 proxy->SetProperty(userId, request.authType, attr, wrapper);
217 return SUCCESS;
218 }
219
SetProperty(int32_t userId,const SetPropertyRequest & request,const std::shared_ptr<SetPropCallback> & callback)220 void UserAuthClientImpl::SetProperty(int32_t userId, const SetPropertyRequest &request,
221 const std::shared_ptr<SetPropCallback> &callback)
222 {
223 IAM_LOGI("start, userId:%{public}d authType:%{public}d", userId, request.authType);
224 if (!callback) {
225 IAM_LOGE("set prop callback is nullptr");
226 return;
227 }
228
229 ResultCode result = SetPropertyInner(userId, request, callback);
230 if (result != SUCCESS) {
231 IAM_LOGE("result is not success");
232 Attributes retExtraInfo;
233 callback->OnResult(GENERAL_ERROR, retExtraInfo);
234 return;
235 }
236 }
237
BeginAuthentication(const AuthParam & authParam,const std::shared_ptr<AuthenticationCallback> & callback)238 uint64_t UserAuthClientImpl::BeginAuthentication(const AuthParam &authParam,
239 const std::shared_ptr<AuthenticationCallback> &callback)
240 {
241 IAM_LOGI("start, userId:%{public}d, authType:%{public}d, atl:%{public}u, authIntent:%{public}u,"
242 "remoteAuthParamHasValue:%{public}s", authParam.userId, authParam.authType, authParam.authTrustLevel,
243 authParam.authIntent, Common::GetBoolStr(authParam.remoteAuthParam.has_value()));
244 if (authParam.remoteAuthParam.has_value()) {
245 IAM_LOGI("verifierNetworkIdHasValue:%{public}s collectorNetworkIdHasValue:%{public}s "
246 "collectorTokenIdHasValue:%{public}s",
247 Common::GetBoolStr(authParam.remoteAuthParam->verifierNetworkId.has_value()),
248 Common::GetBoolStr(authParam.remoteAuthParam->collectorNetworkId.has_value()),
249 Common::GetBoolStr(authParam.remoteAuthParam->collectorTokenId.has_value()));
250 }
251
252 if (!callback) {
253 IAM_LOGE("auth callback is nullptr");
254 return BAD_CONTEXT_ID;
255 }
256
257 auto proxy = GetProxy();
258 if (!proxy) {
259 Attributes extraInfo;
260 int32_t result = LoadModeUtil::GetProxyNullResultCode(__func__, std::vector<std::string>({
261 ACCESS_USER_AUTH_INTERNAL_PERMISSION
262 }));
263 callback->OnResult(result, extraInfo);
264 return BAD_CONTEXT_ID;
265 }
266
267 sptr<UserAuthCallbackInterface> wrapper(new (std::nothrow) UserAuthCallbackService(callback));
268 if (wrapper == nullptr) {
269 IAM_LOGE("failed to create wrapper");
270 Attributes extraInfo;
271 callback->OnResult(GENERAL_ERROR, extraInfo);
272 return BAD_CONTEXT_ID;
273 }
274 AuthParamInner authParamInner = {
275 .userId = authParam.userId,
276 .challenge = authParam.challenge,
277 .authType = authParam.authType,
278 .authTrustLevel = authParam.authTrustLevel,
279 .authIntent = authParam.authIntent
280 };
281 std::optional<RemoteAuthParam> remoteAuthParam = authParam.remoteAuthParam;
282 return proxy->AuthUser(authParamInner, remoteAuthParam, wrapper);
283 }
284
BeginNorthAuthentication(int32_t apiVersion,const std::vector<uint8_t> & challenge,AuthType authType,AuthTrustLevel atl,const std::shared_ptr<AuthenticationCallback> & callback)285 uint64_t UserAuthClientImpl::BeginNorthAuthentication(int32_t apiVersion, const std::vector<uint8_t> &challenge,
286 AuthType authType, AuthTrustLevel atl, const std::shared_ptr<AuthenticationCallback> &callback)
287 {
288 IAM_LOGI("start, apiVersion:%{public}d authType:%{public}d atl:%{public}u", apiVersion, authType, atl);
289 if (!callback) {
290 IAM_LOGE("auth callback is nullptr");
291 return BAD_CONTEXT_ID;
292 }
293
294 auto northCallback = Common::MakeShared<NorthAuthenticationCallback>(callback);
295 if (!northCallback) {
296 IAM_LOGE("auth callback is nullptr");
297 Attributes extraInfo;
298 callback->OnResult(GENERAL_ERROR, extraInfo);
299 return BAD_CONTEXT_ID;
300 }
301
302 auto proxy = GetProxy();
303 if (!proxy) {
304 Attributes extraInfo;
305 int32_t result = LoadModeUtil::GetProxyNullResultCode(__func__, std::vector<std::string>({
306 ACCESS_BIOMETRIC_PERMISSION
307 }));
308 callback->OnResult(result, extraInfo);
309 return BAD_CONTEXT_ID;
310 }
311
312 sptr<UserAuthCallbackInterface> wrapper(new (std::nothrow) UserAuthCallbackService(northCallback));
313 if (wrapper == nullptr) {
314 IAM_LOGE("failed to create wrapper");
315 Attributes extraInfo;
316 callback->OnResult(GENERAL_ERROR, extraInfo);
317 return BAD_CONTEXT_ID;
318 }
319 return proxy->Auth(apiVersion, challenge, authType, atl, wrapper);
320 }
321
CancelAuthentication(uint64_t contextId)322 int32_t UserAuthClientImpl::CancelAuthentication(uint64_t contextId)
323 {
324 IAM_LOGI("start");
325 auto proxy = GetProxy();
326 if (!proxy) {
327 IAM_LOGE("proxy is nullptr");
328 return GENERAL_ERROR;
329 }
330
331 return proxy->CancelAuthOrIdentify(contextId, CancelReason::ORIGINAL_CANCEL);
332 }
333
BeginIdentification(const std::vector<uint8_t> & challenge,AuthType authType,const std::shared_ptr<IdentificationCallback> & callback)334 uint64_t UserAuthClientImpl::BeginIdentification(const std::vector<uint8_t> &challenge, AuthType authType,
335 const std::shared_ptr<IdentificationCallback> &callback)
336 {
337 IAM_LOGI("start, authType:%{public}d", authType);
338 if (!callback) {
339 IAM_LOGE("identify callback is nullptr");
340 return BAD_CONTEXT_ID;
341 }
342
343 auto proxy = GetProxy();
344 if (!proxy) {
345 Attributes extraInfo;
346 int32_t result = LoadModeUtil::GetProxyNullResultCode(__func__, std::vector<std::string>({
347 ACCESS_USER_AUTH_INTERNAL_PERMISSION
348 }));
349 callback->OnResult(result, extraInfo);
350 return BAD_CONTEXT_ID;
351 }
352
353 sptr<UserAuthCallbackInterface> wrapper(new (std::nothrow) UserAuthCallbackService(callback));
354 if (wrapper == nullptr) {
355 IAM_LOGE("failed to create wrapper");
356 Attributes extraInfo;
357 callback->OnResult(GENERAL_ERROR, extraInfo);
358 return BAD_CONTEXT_ID;
359 }
360 return proxy->Identify(challenge, authType, wrapper);
361 }
362
CancelIdentification(uint64_t contextId)363 int32_t UserAuthClientImpl::CancelIdentification(uint64_t contextId)
364 {
365 IAM_LOGI("start");
366 auto proxy = GetProxy();
367 if (!proxy) {
368 IAM_LOGE("proxy is nullptr");
369 return GENERAL_ERROR;
370 }
371
372 return proxy->CancelAuthOrIdentify(contextId, CancelReason::ORIGINAL_CANCEL);
373 }
374
GetVersion(int32_t & version)375 int32_t UserAuthClientImpl::GetVersion(int32_t &version)
376 {
377 IAM_LOGI("start");
378 auto proxy = GetProxy();
379 if (!proxy) {
380 IAM_LOGE("proxy is nullptr");
381 return GENERAL_ERROR;
382 }
383
384 return proxy->GetVersion(version);
385 }
386
SetGlobalConfigParam(const GlobalConfigParam & param)387 int32_t UserAuthClientImpl::SetGlobalConfigParam(const GlobalConfigParam ¶m)
388 {
389 IAM_LOGI("start");
390 auto proxy = GetProxy();
391 if (!proxy) {
392 IAM_LOGE("proxy is nullptr");
393 return GENERAL_ERROR;
394 }
395
396 return proxy->SetGlobalConfigParam(param);
397 }
398
GetProxy()399 sptr<UserAuthInterface> UserAuthClientImpl::GetProxy()
400 {
401 std::lock_guard<std::mutex> lock(mutex_);
402 if (proxy_ != nullptr) {
403 return proxy_;
404 }
405 sptr<IRemoteObject> obj = IpcClientUtils::GetRemoteObject(SUBSYS_USERIAM_SYS_ABILITY_USERAUTH);
406 if (obj == nullptr) {
407 IAM_LOGE("remote object is null");
408 return proxy_;
409 }
410 sptr<IRemoteObject::DeathRecipient> dr(new (std::nothrow) UserAuthImplDeathRecipient());
411 if ((dr == nullptr) || (obj->IsProxyObject() && !obj->AddDeathRecipient(dr))) {
412 IAM_LOGE("add death recipient fail");
413 return proxy_;
414 }
415
416 proxy_ = iface_cast<UserAuthInterface>(obj);
417 deathRecipient_ = dr;
418 return proxy_;
419 }
420
ResetProxy(const wptr<IRemoteObject> & remote)421 void UserAuthClientImpl::ResetProxy(const wptr<IRemoteObject> &remote)
422 {
423 IAM_LOGI("start");
424 std::lock_guard<std::mutex> lock(mutex_);
425 if (proxy_ == nullptr) {
426 IAM_LOGE("proxy_ is null");
427 return;
428 }
429 auto serviceRemote = proxy_->AsObject();
430 if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
431 IAM_LOGI("need reset");
432 serviceRemote->RemoveDeathRecipient(deathRecipient_);
433 proxy_ = nullptr;
434 deathRecipient_ = nullptr;
435 }
436 IAM_LOGI("end reset proxy");
437 }
438
OnRemoteDied(const wptr<IRemoteObject> & remote)439 void UserAuthClientImpl::UserAuthImplDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
440 {
441 IAM_LOGI("start");
442 if (remote == nullptr) {
443 IAM_LOGE("remote is nullptr");
444 return;
445 }
446 CallbackManager::GetInstance().OnServiceDeath();
447 UserAuthClientImpl::Instance().ResetProxy(remote);
448 }
449
Instance()450 UserAuthClientImpl &UserAuthClientImpl::Instance()
451 {
452 static UserAuthClientImpl impl;
453 return impl;
454 }
455
GetInstance()456 UserAuthClient &UserAuthClient::GetInstance()
457 {
458 return UserAuthClientImpl::Instance();
459 }
460
BeginWidgetAuth(const WidgetAuthParam & authParam,const WidgetParam & widgetParam,const std::shared_ptr<AuthenticationCallback> & callback)461 uint64_t UserAuthClientImpl::BeginWidgetAuth(const WidgetAuthParam &authParam, const WidgetParam &widgetParam,
462 const std::shared_ptr<AuthenticationCallback> &callback)
463 {
464 IAM_LOGI("start, authTypeSize:%{public}zu authTrustLevel:%{public}u", authParam.authTypes.size(),
465 authParam.authTrustLevel);
466 AuthParamInner authParamInner = {
467 .userId = authParam.userId,
468 .isUserIdSpecified = true,
469 .challenge = authParam.challenge,
470 .authTypes = authParam.authTypes,
471 .authTrustLevel = authParam.authTrustLevel,
472 .reuseUnlockResult = authParam.reuseUnlockResult,
473 };
474 WidgetParamInner widgetParamInner = {
475 .title = widgetParam.title,
476 .navigationButtonText = widgetParam.navigationButtonText,
477 .windowMode = widgetParam.windowMode,
478 .hasContext = false,
479 };
480 return BeginWidgetAuthInner(INNER_API_VERSION_20000, authParamInner, widgetParamInner, callback);
481 }
482
BeginWidgetAuth(int32_t apiVersion,const WidgetAuthParam & authParam,const WidgetParam & widgetParam,const std::shared_ptr<AuthenticationCallback> & callback)483 uint64_t UserAuthClientImpl::BeginWidgetAuth(int32_t apiVersion, const WidgetAuthParam &authParam,
484 const WidgetParam &widgetParam, const std::shared_ptr<AuthenticationCallback> &callback)
485 {
486 IAM_LOGI("start, apiVersion:%{public}d authTypeSize:%{public}zu authTrustLevel:%{public}u",
487 apiVersion, authParam.authTypes.size(), authParam.authTrustLevel);
488
489 AuthParamInner authParamInner = {
490 .isUserIdSpecified = false,
491 .challenge = authParam.challenge,
492 .authTypes = authParam.authTypes,
493 .authTrustLevel = authParam.authTrustLevel,
494 .reuseUnlockResult = authParam.reuseUnlockResult,
495 };
496 WidgetParamInner widgetParamInner = {
497 .title = widgetParam.title,
498 .navigationButtonText = widgetParam.navigationButtonText,
499 .windowMode = widgetParam.windowMode,
500 .hasContext = false,
501 };
502 return BeginWidgetAuthInner(apiVersion, authParamInner, widgetParamInner, callback);
503 }
504
BeginWidgetAuthInner(int32_t apiVersion,const AuthParamInner & authParam,const WidgetParamInner & widgetParam,const std::shared_ptr<AuthenticationCallback> & callback)505 uint64_t UserAuthClientImpl::BeginWidgetAuthInner(int32_t apiVersion, const AuthParamInner &authParam,
506 const WidgetParamInner &widgetParam, const std::shared_ptr<AuthenticationCallback> &callback)
507 {
508 if (!callback) {
509 IAM_LOGE("auth callback is nullptr");
510 return BAD_CONTEXT_ID;
511 }
512 auto proxy = GetProxy();
513 if (!proxy) {
514 Attributes extraInfo;
515 int32_t result = LoadModeUtil::GetProxyNullResultCode(__func__, std::vector<std::string>({
516 ACCESS_USER_AUTH_INTERNAL_PERMISSION,
517 ACCESS_BIOMETRIC_PERMISSION
518 }));
519 callback->OnResult(result, extraInfo);
520 return BAD_CONTEXT_ID;
521 }
522
523 sptr<UserAuthCallbackInterface> wrapper(new (std::nothrow) UserAuthCallbackService(callback));
524 if (wrapper == nullptr) {
525 IAM_LOGE("failed to create wrapper");
526 Attributes extraInfo;
527 callback->OnResult(static_cast<int32_t>(ResultCode::GENERAL_ERROR), extraInfo);
528 return BAD_CONTEXT_ID;
529 }
530
531 // modal
532 const std::shared_ptr<UserAuthModalInnerCallback> &modalCallback = Common::MakeShared<UserAuthModalInnerCallback>();
533 sptr<ModalCallbackInterface> wrapperModal(new (std::nothrow) ModalCallbackService(modalCallback));
534 if (wrapperModal == nullptr) {
535 IAM_LOGE("failed to create wrapper for modal");
536 Attributes extraInfo;
537 callback->OnResult(static_cast<int32_t>(ResultCode::GENERAL_ERROR), extraInfo);
538 return BAD_CONTEXT_ID;
539 }
540 return proxy->AuthWidget(apiVersion, authParam, widgetParam, wrapper, wrapperModal);
541 }
542
SetWidgetCallback(int32_t version,const std::shared_ptr<IUserAuthWidgetCallback> & callback)543 int32_t UserAuthClientImpl::SetWidgetCallback(int32_t version, const std::shared_ptr<IUserAuthWidgetCallback> &callback)
544 {
545 IAM_LOGI("start, version:%{public}d", version);
546 if (!callback) {
547 IAM_LOGE("widget callback is nullptr");
548 return GENERAL_ERROR;
549 }
550 auto proxy = GetProxy();
551 if (!proxy) {
552 IAM_LOGE("proxy is nullptr");
553 return GENERAL_ERROR;
554 }
555
556 sptr<WidgetCallbackInterface> wrapper(new (std::nothrow) WidgetCallbackService(callback));
557 if (wrapper == nullptr) {
558 IAM_LOGE("failed to create wrapper");
559 return GENERAL_ERROR;
560 }
561 return proxy->RegisterWidgetCallback(version, wrapper);
562 }
563
Notice(NoticeType noticeType,const std::string & eventData)564 int32_t UserAuthClientImpl::Notice(NoticeType noticeType, const std::string &eventData)
565 {
566 IAM_LOGI("start, noticeType:%{public}d", noticeType);
567 auto proxy = GetProxy();
568 if (!proxy) {
569 IAM_LOGE("proxy is nullptr");
570 return GENERAL_ERROR;
571 }
572 IAM_LOGI("UserAuthClientImpl::Notice noticeType:%{public}d, eventDat:%{public}s",
573 static_cast<int32_t>(noticeType), eventData.c_str());
574 return proxy->Notice(noticeType, eventData);
575 }
576
GetEnrolledState(int32_t apiVersion,AuthType authType,EnrolledState & enrolledState)577 int32_t UserAuthClientImpl::GetEnrolledState(int32_t apiVersion, AuthType authType, EnrolledState &enrolledState)
578 {
579 IAM_LOGI("start, apiVersion:%{public}d authType:%{public}d ", apiVersion, authType);
580 auto proxy = GetProxy();
581 if (!proxy) {
582 return LoadModeUtil::GetProxyNullResultCode(__func__, std::vector<std::string>({
583 ACCESS_BIOMETRIC_PERMISSION
584 }));
585 }
586 int32_t ret = proxy->GetEnrolledState(apiVersion, authType, enrolledState);
587 if (ret != SUCCESS) {
588 IAM_LOGE("proxy GetEnrolledState failed");
589 return ret;
590 }
591 return ret;
592 }
593
RegistUserAuthSuccessEventListener(const std::vector<AuthType> & authType,const sptr<AuthEventListenerInterface> & listener)594 int32_t UserAuthClientImpl::RegistUserAuthSuccessEventListener(const std::vector<AuthType> &authType,
595 const sptr<AuthEventListenerInterface> &listener)
596 {
597 IAM_LOGI("start");
598 if (!listener) {
599 IAM_LOGE("listener is nullptr");
600 return GENERAL_ERROR;
601 }
602
603 auto proxy = GetProxy();
604 if (!proxy) {
605 IAM_LOGE("proxy is nullptr");
606 return GENERAL_ERROR;
607 }
608
609 int32_t ret = proxy->RegistUserAuthSuccessEventListener(authType, listener);
610 if (ret != SUCCESS) {
611 IAM_LOGE("Regist userAuth success event listener failed");
612 return ret;
613 }
614
615 return SUCCESS;
616 }
617
UnRegistUserAuthSuccessEventListener(const sptr<AuthEventListenerInterface> & listener)618 int32_t UserAuthClientImpl::UnRegistUserAuthSuccessEventListener(const sptr<AuthEventListenerInterface> &listener)
619 {
620 IAM_LOGI("start");
621 if (!listener) {
622 IAM_LOGE("listener is nullptr");
623 return GENERAL_ERROR;
624 }
625
626 auto proxy = GetProxy();
627 if (!proxy) {
628 IAM_LOGE("proxy is nullptr");
629 return GENERAL_ERROR;
630 }
631
632 int32_t ret = proxy->UnRegistUserAuthSuccessEventListener(listener);
633 if (ret != SUCCESS) {
634 IAM_LOGE("unRegist userAuth success event listener failed");
635 return ret;
636 }
637
638 return SUCCESS;
639 }
640
PrepareRemoteAuth(const std::string & networkId,const std::shared_ptr<PrepareRemoteAuthCallback> & callback)641 int32_t UserAuthClientImpl::PrepareRemoteAuth(const std::string &networkId,
642 const std::shared_ptr<PrepareRemoteAuthCallback> &callback)
643 {
644 IAM_LOGI("start");
645 if (!callback) {
646 IAM_LOGE("prepare remote auth callback is nullptr");
647 return GENERAL_ERROR;
648 }
649
650 auto proxy = GetProxy();
651 if (!proxy) {
652 IAM_LOGE("proxy is nullptr");
653 callback->OnResult(GENERAL_ERROR);
654 return GENERAL_ERROR;
655 }
656
657 sptr<UserAuthCallbackInterface> wrapper(new (std::nothrow) UserAuthCallbackService(callback));
658 if (wrapper == nullptr) {
659 IAM_LOGE("failed to create wrapper");
660 callback->OnResult(GENERAL_ERROR);
661 return GENERAL_ERROR;
662 }
663
664 return proxy->PrepareRemoteAuth(networkId, wrapper);
665 }
666 } // namespace UserAuth
667 } // namespace UserIam
668 } // namespace OHOS
669