From 513079c04daa666e36e0c340935fcad2ce4517b6 Mon Sep 17 00:00:00 2001 From: hejiaogirl Date: Fri, 27 Jun 2025 19:29:06 +0800 Subject: [PATCH] fix : query middle auth result Signed-off-by: hejiaogirl --- .../src/user_access_ctrl_callback_v16.cpp | 2 +- .../js/napi/user_auth/inc/auth_common.h | 11 + .../user_auth/inc/user_auth_callback_v10.h | 6 + .../user_auth/inc/user_auth_instance_v10.h | 6 +- .../user_auth/inc/user_auth_napi_helper.h | 1 + .../user_auth/inc/user_auth_param_utils.h | 2 + .../user_auth/src/user_auth_callback_v10.cpp | 111 ++- .../user_auth/src/user_auth_callback_v6.cpp | 2 +- .../user_auth/src/user_auth_callback_v8.cpp | 4 +- .../user_auth/src/user_auth_callback_v9.cpp | 4 +- .../js/napi/user_auth/src/user_auth_impl.cpp | 4 +- .../user_auth/src/user_auth_instance_v10.cpp | 116 +++- .../user_auth/src/user_auth_napi_helper.cpp | 641 ------------------ .../user_auth/src/user_auth_param_utils.cpp | 23 + .../src/user_auth_widget_callback_v10.cpp | 2 +- .../src/user_auth_widget_mgr_v10.cpp | 2 +- .../client/src/user_auth_client_impl.cpp | 1 + .../client/src/user_auth_napi_client_impl.cpp | 2 + .../common_defines/user_auth_common_defines.h | 2 + frameworks/native/ipc/idl/UserAuthTypes.idl | 2 + interfaces/inner_api/iam_common_defines.h | 2 +- services/context/inc/context_callback.h | 2 + services/context/inc/context_factory.h | 1 + services/context/inc/widget_client.h | 5 +- services/context/inc/widget_context.h | 12 + services/context/inc/widget_json.h | 1 + services/context/src/auth_widget_helper.cpp | 1 + .../context/src/context_callback_impl.cpp | 44 +- services/context/src/context_callback_impl.h | 2 + services/context/src/widget_client.cpp | 19 +- services/context/src/widget_context.cpp | 145 +++- services/context/src/widget_json.cpp | 2 + services/core/inc/authentication.h | 1 + services/core/inc/widget_schedule_node.h | 5 +- .../core/inc/widget_schedule_node_callback.h | 2 + .../core/src/widget_schedule_node_impl.cpp | 28 + services/core/src/widget_schedule_node_impl.h | 4 + services/ipc/inc/user_auth_service.h | 4 +- services/ipc/src/user_auth_service.cpp | 40 +- test/unittest/services/mocks/mock_context.h | 2 + .../mocks/mock_widget_schedule_node.h | 2 + .../mock_widget_schedule_node_callback.h | 2 + .../services/src/widget_client_test.cpp | 38 +- .../services/src/widget_context_test.cpp | 63 ++ 44 files changed, 636 insertions(+), 735 deletions(-) diff --git a/frameworks/js/napi/user_access_ctrl/src/user_access_ctrl_callback_v16.cpp b/frameworks/js/napi/user_access_ctrl/src/user_access_ctrl_callback_v16.cpp index 0bfa75d84..a7bc7947d 100644 --- a/frameworks/js/napi/user_access_ctrl/src/user_access_ctrl_callback_v16.cpp +++ b/frameworks/js/napi/user_access_ctrl/src/user_access_ctrl_callback_v16.cpp @@ -141,7 +141,7 @@ void GetCallbackResult(const UserAuth::Attributes &extraInfo, std::shared_ptr &token, int32_t authType, EnrolledState enrolledState); + napi_status DoTipInfoCallBack(int32_t tipType, uint32_t tipCode); void SetResultCallback(const std::shared_ptr &resultCallback); void ClearResultCallback(); bool HasResultCallback(); + void SetTipCallback(const std::shared_ptr &tipCallback); + void ClearTipCallback(); + bool HasTipCallback(); private: std::shared_ptr GetResultCallback(); + std::shared_ptr GetTipCallback(); napi_env env_ = nullptr; std::mutex mutex_; std::shared_ptr resultCallback_ = nullptr; + std::shared_ptr tipCallback_ = nullptr; }; } // namespace UserAuth } // namespace UserIam diff --git a/frameworks/js/napi/user_auth/inc/user_auth_instance_v10.h b/frameworks/js/napi/user_auth/inc/user_auth_instance_v10.h index fa005491e..aa0ae552c 100644 --- a/frameworks/js/napi/user_auth/inc/user_auth_instance_v10.h +++ b/frameworks/js/napi/user_auth/inc/user_auth_instance_v10.h @@ -44,7 +44,11 @@ public: UserAuthResultCode Cancel(napi_env env, napi_callback_info info); private: - std::shared_ptr GetCallback(napi_env env, napi_value value); + std::shared_ptr GetCallback(napi_env env, napi_value value, const char* propertyName); + UserAuthResultCode SetResultCallback(napi_env env, napi_value value); + UserAuthResultCode SetTipCallback(napi_env env, napi_value value); + UserAuthResultCode ClearResultCallback(napi_env env, size_t argc, napi_value *value); + UserAuthResultCode ClearTipCallback(napi_env env, size_t argc, napi_value *value); AuthParamInner authParam_ = {}; UserAuthNapiClientImpl::WidgetParamNapi widgetParam_ = {}; diff --git a/frameworks/js/napi/user_auth/inc/user_auth_napi_helper.h b/frameworks/js/napi/user_auth/inc/user_auth_napi_helper.h index 8bd7459a8..a074c9a98 100644 --- a/frameworks/js/napi/user_auth/inc/user_auth_napi_helper.h +++ b/frameworks/js/napi/user_auth/inc/user_auth_napi_helper.h @@ -39,6 +39,7 @@ public: static UserAuthResultCode ThrowErrorMsg(napi_env env, UserAuthResultCode errorCode, std::string errorMsg); static bool CheckReuseUnlockResult(ReuseUnlockResult reuseUnlockResult); static napi_status CheckNapiType(napi_env env, napi_value value, napi_valuetype type); + static napi_status GetBoolValue(napi_env env, napi_value value, bool &out); static napi_status GetInt32Value(napi_env env, napi_value value, int32_t &out); static napi_status GetUint32Value(napi_env env, napi_value value, uint32_t &out); static napi_status GetStrValue(napi_env env, napi_value value, char *out, size_t &len); diff --git a/frameworks/js/napi/user_auth/inc/user_auth_param_utils.h b/frameworks/js/napi/user_auth/inc/user_auth_param_utils.h index 4bc6859ec..aeb3ab189 100644 --- a/frameworks/js/napi/user_auth/inc/user_auth_param_utils.h +++ b/frameworks/js/napi/user_auth/inc/user_auth_param_utils.h @@ -49,6 +49,8 @@ public: UserAuthNapiClientImpl::WidgetParamNapi &widgetParam, std::shared_ptr &abilityContext); static bool CheckUIContext(const std::shared_ptr context); + static UserAuthResultCode ProcessSkipLockedBiometricAuth(napi_env env, napi_value value, + AuthParamInner &authParam); }; } // namespace UserAuth } // namespace UserIam diff --git a/frameworks/js/napi/user_auth/src/user_auth_callback_v10.cpp b/frameworks/js/napi/user_auth/src/user_auth_callback_v10.cpp index 997c54a65..e0e7a23f5 100644 --- a/frameworks/js/napi/user_auth/src/user_auth_callback_v10.cpp +++ b/frameworks/js/napi/user_auth/src/user_auth_callback_v10.cpp @@ -37,6 +37,13 @@ struct ResultCallbackV10Holder { EnrolledState enrolledState {}; napi_env env {nullptr}; }; + +struct AuthTipInfoCallbackHolder { + std::shared_ptr callback {nullptr}; + int32_t tipCode {0}; + int32_t tipType {0}; + napi_env env {nullptr}; +}; } UserAuthCallbackV10::UserAuthCallbackV10(napi_env env) : env_(env) @@ -74,11 +81,36 @@ bool UserAuthCallbackV10::HasResultCallback() return resultCallback_ != nullptr; } +void UserAuthCallbackV10::SetTipCallback(const std::shared_ptr &tipCallback) +{ + std::lock_guard guard(mutex_); + tipCallback_ = tipCallback; +} + +void UserAuthCallbackV10::ClearTipCallback() +{ + std::lock_guard guard(mutex_); + tipCallback_ = nullptr; +} + +std::shared_ptr UserAuthCallbackV10::GetTipCallback() +{ + std::lock_guard guard(mutex_); + return tipCallback_; +} + +bool UserAuthCallbackV10::HasTipCallback() +{ + std::lock_guard guard(mutex_); + return tipCallback_ != nullptr; +} + napi_status UserAuthCallbackV10::DoResultCallback(int32_t result, const std::vector &token, int32_t authType, EnrolledState enrolledState) { auto resultCallback = GetResultCallback(); if (resultCallback == nullptr) { + IAM_LOGE("resultCallback is null"); return napi_ok; } IAM_LOGD("start"); @@ -119,15 +151,87 @@ napi_status UserAuthCallbackV10::DoResultCallback(int32_t result, return UserAuthNapiHelper::CallVoidNapiFunc(env_, resultCallback->Get(), ARGS_ONE, &eventInfo); } +napi_status UserAuthCallbackV10::DoTipInfoCallBack(int32_t tipType, uint32_t tipCode) +{ + IAM_LOGI("DoTipInfoCallBack start, authType:%{public}d, tipCode:%{public}u", tipType, tipCode); + auto tipCallback = GetTipCallback(); + if (tipCallback == nullptr) { + IAM_LOGE("tipCallback is null"); + return napi_ok; + } + napi_value authTipInfo; + napi_status ret = napi_create_object(env_, &authTipInfo); + if (ret != napi_ok) { + IAM_LOGE("napi_create_object failed %{public}d", ret); + return ret; + } + ret = UserAuthNapiHelper::SetInt32Property(env_, authTipInfo, "tipType", tipType); + if (ret != napi_ok) { + IAM_LOGE("SetInt32Property tipType failed %{public}d", ret); + return ret; + } + ret = UserAuthNapiHelper::SetInt32Property(env_, authTipInfo, "tipCode", tipCode); + if (ret != napi_ok) { + IAM_LOGE("napi_create_int32 tipCode failed %{public}d", ret); + return ret; + } + ret = UserAuthNapiHelper::CallVoidNapiFunc(env_, tipCallback->Get(), ARGS_ONE, &authTipInfo); + if (ret != napi_ok) { + IAM_LOGE("CallVoidNapiFunc failed %{public}d", ret); + return ret; + } + return ret; +} + void UserAuthCallbackV10::OnAcquireInfo(int32_t module, uint32_t acquireInfo, const UserIam::UserAuth::Attributes &extraInfo) { + IAM_LOGI("OnAcquireInfo start, authType:%{public}d, tipCode:%{public}u", module, acquireInfo); + uv_loop_s *loop = nullptr; + napi_status napiStatus = napi_get_uv_event_loop(env_, &loop); + if (napiStatus != napi_ok || loop == nullptr) { + IAM_LOGE("napi_get_uv_event_loop fail"); + return; + } + std::shared_ptr authTipInfoCallbackHolder = + Common::MakeShared(); + if (authTipInfoCallbackHolder == nullptr) { + IAM_LOGE("resultHolder is null"); + return; + } + authTipInfoCallbackHolder->callback = shared_from_this(); + authTipInfoCallbackHolder->tipType = module; + authTipInfoCallbackHolder->tipCode = acquireInfo; + authTipInfoCallbackHolder->env = env_; + auto task = [authTipInfoCallbackHolder] () { + IAM_LOGD("start"); + if (authTipInfoCallbackHolder == nullptr || authTipInfoCallbackHolder->callback == nullptr) { + IAM_LOGE("authTipInfoCallbackHolder is invalid"); + return; + } + napi_handle_scope scope = nullptr; + napi_open_handle_scope(authTipInfoCallbackHolder->env, &scope); + if (scope == nullptr) { + IAM_LOGE("scope is invalid"); + return; + } + napi_status ret = authTipInfoCallbackHolder->callback->DoTipInfoCallBack(authTipInfoCallbackHolder->tipType, + authTipInfoCallbackHolder->tipCode); + if (ret != napi_ok) { + IAM_LOGE("DoTipInfoCallBack ret = %{public}d", ret); + return; + } + napi_close_handle_scope(authTipInfoCallbackHolder->env, scope); + }; + if (napi_send_event(env_, task, napi_eprio_immediate) != napi_status::napi_ok) { + IAM_LOGE("napi_send_event: Failed to SendEvent"); + } } void UserAuthCallbackV10::OnResult(int32_t result, const Attributes &extraInfo) { IAM_LOGD("start, result:%{public}d", result); - uv_loop_s *loop; + uv_loop_s *loop = nullptr; napi_status napiStatus = napi_get_uv_event_loop(env_, &loop); if (napiStatus != napi_ok || loop == nullptr) { IAM_LOGE("napi_get_uv_event_loop fail"); @@ -168,7 +272,10 @@ void UserAuthCallbackV10::OnResult(int32_t result, const Attributes &extraInfo) } napi_status ret = resultHolder->callback->DoResultCallback(resultHolder->result, resultHolder->token, resultHolder->authType, resultHolder->enrolledState); - IAM_LOGD("DoResultCallback ret = %{public}d", ret); + if (ret != napi_ok) { + IAM_LOGE("DoResultCallback ret = %{public}d", ret); + return; + } napi_close_handle_scope(resultHolder->env, scope); }; if (napi_status::napi_ok != napi_send_event(env_, task, napi_eprio_immediate)) { diff --git a/frameworks/js/napi/user_auth/src/user_auth_callback_v6.cpp b/frameworks/js/napi/user_auth/src/user_auth_callback_v6.cpp index b2eaf4f46..1329677a7 100644 --- a/frameworks/js/napi/user_auth/src/user_auth_callback_v6.cpp +++ b/frameworks/js/napi/user_auth/src/user_auth_callback_v6.cpp @@ -149,7 +149,7 @@ void OnCallbackV6Work(std::shared_ptr resultHolder) void UserAuthCallbackV6::OnResult(int32_t result, const Attributes &extraInfo) { IAM_LOGI("start, result:%{public}d", result); - uv_loop_s *loop; + uv_loop_s *loop = nullptr; napi_status napiStatus = napi_get_uv_event_loop(env_, &loop); if (napiStatus != napi_ok || loop == nullptr) { IAM_LOGE("napi_get_uv_event_loop fail"); diff --git a/frameworks/js/napi/user_auth/src/user_auth_callback_v8.cpp b/frameworks/js/napi/user_auth/src/user_auth_callback_v8.cpp index 6dc6feacf..333e2c8ab 100644 --- a/frameworks/js/napi/user_auth/src/user_auth_callback_v8.cpp +++ b/frameworks/js/napi/user_auth/src/user_auth_callback_v8.cpp @@ -126,7 +126,7 @@ void UserAuthCallbackV8::OnAcquireInfo(int32_t module, uint32_t acquireInfo, const UserIam::UserAuth::Attributes &extraInfo) { IAM_LOGI("start module:%{public}d acquireInfo:%{public}u", module, acquireInfo); - uv_loop_s *loop; + uv_loop_s *loop = nullptr; napi_status napiStatus = napi_get_uv_event_loop(env_, &loop); if (napiStatus != napi_ok || loop == nullptr) { IAM_LOGE("napi_get_uv_event_loop fail"); @@ -192,7 +192,7 @@ void OnResultV8Work(std::shared_ptr resultHolder) void UserAuthCallbackV8::OnResult(int32_t result, const Attributes &extraInfo) { IAM_LOGI("start, result:%{public}d", UserAuthNapiHelper::GetResultCodeV8(result)); - uv_loop_s *loop; + uv_loop_s *loop = nullptr; napi_status napiStatus = napi_get_uv_event_loop(env_, &loop); if (napiStatus != napi_ok || loop == nullptr) { IAM_LOGE("napi_get_uv_event_loop fail"); diff --git a/frameworks/js/napi/user_auth/src/user_auth_callback_v9.cpp b/frameworks/js/napi/user_auth/src/user_auth_callback_v9.cpp index 74d3b2410..1b77a9077 100644 --- a/frameworks/js/napi/user_auth/src/user_auth_callback_v9.cpp +++ b/frameworks/js/napi/user_auth/src/user_auth_callback_v9.cpp @@ -167,7 +167,7 @@ void UserAuthCallbackV9::OnAcquireInfo(int32_t module, uint32_t acquireInfo, const UserIam::UserAuth::Attributes &extraInfo) { IAM_LOGI("start module:%{public}d acquireInfo:%{public}u", module, acquireInfo); - uv_loop_s *loop; + uv_loop_s *loop = nullptr; napi_status napiStatus = napi_get_uv_event_loop(env_, &loop); if (napiStatus != napi_ok || loop == nullptr) { IAM_LOGE("napi_get_uv_event_loop fail"); @@ -233,7 +233,7 @@ void OnResultV9Work(std::shared_ptr resultHolder) void UserAuthCallbackV9::OnResult(int32_t result, const Attributes &extraInfo) { IAM_LOGI("start, result:%{public}d", result); - uv_loop_s *loop; + uv_loop_s *loop = nullptr; napi_status napiStatus = napi_get_uv_event_loop(env_, &loop); if (napiStatus != napi_ok || loop == nullptr) { IAM_LOGE("napi_get_uv_event_loop fail"); diff --git a/frameworks/js/napi/user_auth/src/user_auth_impl.cpp b/frameworks/js/napi/user_auth/src/user_auth_impl.cpp index cc3e04229..b75f0e911 100644 --- a/frameworks/js/napi/user_auth/src/user_auth_impl.cpp +++ b/frameworks/js/napi/user_auth/src/user_auth_impl.cpp @@ -363,8 +363,8 @@ UserAuthResultCode UserAuthImpl::ParseReusableAuthResultParam(napi_env env, napi if (argc != ARGS_ONE) { IAM_LOGE("parms error"); std::string msgStr = "Parameter vefification failed. The number of parameters should be 1."; - napi_throw(env, UserAuthNapiHelper::GenerateErrorMsg(env, UserAuthResultCode::PARAM_VERIFIED_FAILED, msgStr)); - return UserAuthResultCode::PARAM_VERIFIED_FAILED; + napi_throw(env, UserAuthNapiHelper::GenerateErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr)); + return UserAuthResultCode::OHOS_INVALID_PARAM; } AuthParamInner authParamInner = {}; UserAuthResultCode errCode = UserAuthParamUtils::InitAuthParam(env, argv[PARAM0], authParamInner); diff --git a/frameworks/js/napi/user_auth/src/user_auth_instance_v10.cpp b/frameworks/js/napi/user_auth/src/user_auth_instance_v10.cpp index 22d2cd68e..3bc0c4bc8 100644 --- a/frameworks/js/napi/user_auth/src/user_auth_instance_v10.cpp +++ b/frameworks/js/napi/user_auth/src/user_auth_instance_v10.cpp @@ -36,7 +36,9 @@ namespace OHOS { namespace UserIam { namespace UserAuth { namespace { - const std::string AUTH_EVENT_RESULT = "result"; + const char *AUTH_EVENT_RESULT = "result"; + const char *AUTH_EVENT_AUTH_TIP = "authTip"; + const char *PROPERTY_NAME_ON_RESULT = "onResult"; } UserAuthInstanceV10::UserAuthInstanceV10(napi_env env) : callback_(Common::MakeShared(env)) @@ -46,6 +48,7 @@ UserAuthInstanceV10::UserAuthInstanceV10(napi_env env) : callback_(Common::MakeS } authParam_.authTrustLevel = AuthTrustLevel::ATL1; authParam_.userId = INVALID_USER_ID; + authParam_.skipLockedBiometricAuth = false; widgetParam_.navigationButtonText = ""; widgetParam_.title = ""; widgetParam_.windowMode = WindowModeType::UNKNOWN_WINDOW_MODE; @@ -86,7 +89,7 @@ UserAuthResultCode UserAuthInstanceV10::Init(napi_env env, napi_callback_info in return UserAuthResultCode::SUCCESS; } -std::shared_ptr UserAuthInstanceV10::GetCallback(napi_env env, napi_value value) +std::shared_ptr UserAuthInstanceV10::GetCallback(napi_env env, napi_value value, const char* propertyName) { napi_status ret = UserAuthNapiHelper::CheckNapiType(env, value, napi_object); if (ret != napi_ok) { @@ -94,7 +97,7 @@ std::shared_ptr UserAuthInstanceV10::GetCallback(napi_env env, napi return nullptr; } napi_value callbackValue; - ret = napi_get_named_property(env, value, "onResult", &callbackValue); + ret = napi_get_named_property(env, value, propertyName, &callbackValue); if (ret != napi_ok) { IAM_LOGE("napi_get_named_property fail:%{public}d", ret); return nullptr; @@ -102,6 +105,74 @@ std::shared_ptr UserAuthInstanceV10::GetCallback(napi_env env, napi return Common::MakeShared(env, callbackValue); } +UserAuthResultCode UserAuthInstanceV10::SetResultCallback(napi_env env, napi_value value) +{ + IAM_LOGI("start"); + std::lock_guard guard(mutex_); + auto callbackRef = GetCallback(env, value, PROPERTY_NAME_ON_RESULT); + if (callbackRef == nullptr || !callbackRef->IsValid()) { + IAM_LOGE("getAuthInstance on GetCallback fail"); + return UserAuthResultCode::OHOS_INVALID_PARAM; + } + if (callback_->HasResultCallback()) { + IAM_LOGE("callback has been registerred"); + return UserAuthResultCode::GENERAL_ERROR; + } + callback_->SetResultCallback(callbackRef); + IAM_LOGI("SetResultCallback success"); + return UserAuthResultCode::SUCCESS; +} + +UserAuthResultCode UserAuthInstanceV10::ClearResultCallback(napi_env env, size_t argc, napi_value *value) +{ + IAM_LOGI("start"); + std::lock_guard guard(mutex_); + if (argc == ARGS_TWO) { + auto callbackRef = GetCallback(env, value[PARAM1], PROPERTY_NAME_ON_RESULT); + if (callbackRef == nullptr || !callbackRef->IsValid()) { + IAM_LOGE("GetCallback fail"); + return UserAuthResultCode::OHOS_INVALID_PARAM; + } + } + if (!callback_->HasResultCallback()) { + IAM_LOGE("no callback registerred yet"); + return UserAuthResultCode::GENERAL_ERROR; + } + callback_->ClearResultCallback(); + return UserAuthResultCode::SUCCESS; +} + +UserAuthResultCode UserAuthInstanceV10::SetTipCallback(napi_env env, napi_value value) +{ + IAM_LOGI("start"); + std::lock_guard guard(mutex_); + if (callback_->HasTipCallback()) { + IAM_LOGE("callback has been registerred"); + return UserAuthResultCode::GENERAL_ERROR; + } + auto callbackRef = Common::MakeShared(env, value); + if (callbackRef == nullptr || !callbackRef->IsValid()) { + IAM_LOGE("malloc tip callback fail"); + return UserAuthResultCode::OHOS_INVALID_PARAM; + } + callback_->SetTipCallback(Common::MakeShared(env, value)); + IAM_LOGI("SetTipCallback success"); + return UserAuthResultCode::SUCCESS; +} + +UserAuthResultCode UserAuthInstanceV10::ClearTipCallback(napi_env env, size_t argc, napi_value *value) +{ + IAM_LOGI("start"); + std::lock_guard guard(mutex_); + if (!callback_->HasTipCallback()) { + IAM_LOGE("no callback registerred yet"); + return UserAuthResultCode::GENERAL_ERROR; + } + callback_->ClearTipCallback(); + IAM_LOGI("UserAuthResultCode off clear tip callback"); + return UserAuthResultCode::SUCCESS; +} + UserAuthResultCode UserAuthInstanceV10::On(napi_env env, napi_callback_info info) { if (callback_ == nullptr) { @@ -129,20 +200,10 @@ UserAuthResultCode UserAuthInstanceV10::On(napi_env env, napi_callback_info info std::string msgStr = "Parameter error. The type of \"type\" must be string."; return UserAuthNapiHelper::ThrowErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr); } - auto callbackRef = GetCallback(env, argv[PARAM1]); - if (callbackRef == nullptr || !callbackRef->IsValid()) { - IAM_LOGE("getAuthInstance on GetCallback fail"); - std::string msgStr = "Parameter error. The type of \"callback\" must be IAuthCallback."; - return UserAuthNapiHelper::ThrowErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr); - } - if (str == AUTH_EVENT_RESULT) { - IAM_LOGI("getAuthInstance on SetResultCallback"); - if (callback_->HasResultCallback()) { - IAM_LOGE("callback has been registerred"); - return UserAuthResultCode::GENERAL_ERROR; - } - callback_->SetResultCallback(callbackRef); - return UserAuthResultCode::SUCCESS; + if (strcmp(str, AUTH_EVENT_RESULT) == 0) { + return SetResultCallback(env, argv[PARAM1]); + } else if (strcmp(str, AUTH_EVENT_AUTH_TIP) == 0) { + return SetTipCallback(env, argv[PARAM1]); } else { IAM_LOGE("getAuthInstance on invalid event:%{public}s", str); std::string msgStr = "Parameter error. The value of \"type\" must be \"result\"."; @@ -178,23 +239,10 @@ UserAuthResultCode UserAuthInstanceV10::Off(napi_env env, napi_callback_info inf return UserAuthNapiHelper::ThrowErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr); } - if (argc == ARGS_TWO) { - auto callbackRef = GetCallback(env, argv[PARAM1]); - if (callbackRef == nullptr || !callbackRef->IsValid()) { - IAM_LOGE("GetCallback fail"); - std::string msgStr = "Parameter error. The type of \"callback\" must be IAuthCallback."; - return UserAuthNapiHelper::ThrowErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr); - } - } - - if (str == AUTH_EVENT_RESULT) { - if (!callback_->HasResultCallback()) { - IAM_LOGE("no callback registerred yet"); - return UserAuthResultCode::GENERAL_ERROR; - } - callback_->ClearResultCallback(); - IAM_LOGI("UserAuthResultCode off clear result callback"); - return UserAuthResultCode::SUCCESS; + if (strcmp(str, AUTH_EVENT_RESULT) == 0) { + return ClearResultCallback(env, argc, argv); + } else if (strcmp(str, AUTH_EVENT_AUTH_TIP) == 0) { + return ClearTipCallback(env, argc, argv); } else { IAM_LOGE("invalid event:%{public}s", str); std::string msgStr = "Parameter error. The value of \"type\" must be \"result\"."; diff --git a/frameworks/js/napi/user_auth/src/user_auth_napi_helper.cpp b/frameworks/js/napi/user_auth/src/user_auth_napi_helper.cpp index a0f832ae3..e69de29bb 100644 --- a/frameworks/js/napi/user_auth/src/user_auth_napi_helper.cpp +++ b/frameworks/js/napi/user_auth/src/user_auth_napi_helper.cpp @@ -1,641 +0,0 @@ -/* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "user_auth_napi_helper.h" - -#include -#include - -#include "securec.h" - -#include "napi/native_api.h" -#include "napi/native_common.h" -#include "napi/native_node_api.h" - -#include "iam_logger.h" -#include "iam_ptr.h" - -#define LOG_TAG "USER_AUTH_NAPI" - -namespace OHOS { -namespace UserIam { -namespace UserAuth { -namespace { -static constexpr const int MAX_STRING_LENGTH = 65536; -const std::map g_resultV92Str = { - {UserAuthResultCode::OHOS_INVALID_PARAM, "Invalid authentication parameters."}, - {UserAuthResultCode::OHOS_CHECK_PERMISSION_FAILED, "Permission denied."}, - {UserAuthResultCode::OHOS_CHECK_SYSTEM_APP_FAILED, "The caller is not a system application."}, - {UserAuthResultCode::SUCCESS, "Authentication succeeded."}, - {UserAuthResultCode::FAIL, "Authentication failed."}, - {UserAuthResultCode::GENERAL_ERROR, "Unknown errors."}, - {UserAuthResultCode::CANCELED, "Authentication canceled."}, - {UserAuthResultCode::TIMEOUT, "Authentication timeout."}, - {UserAuthResultCode::TYPE_NOT_SUPPORT, "Unsupport authentication type."}, - {UserAuthResultCode::TRUST_LEVEL_NOT_SUPPORT, "Unsupport authentication trust level."}, - {UserAuthResultCode::BUSY, "Authentication service is busy."}, - {UserAuthResultCode::PARAM_VERIFIED_FAILED, "Parameter verification failed."}, - {UserAuthResultCode::LOCKED, "Authentication is lockout."}, - {UserAuthResultCode::NOT_ENROLLED, "Authentication template has not been enrolled."}, - {UserAuthResultCode::CANCELED_FROM_WIDGET, "Authentication is canceled from widget."}, - {UserAuthResultCode::PIN_EXPIRED, "Operation failed because of PIN expired."}, - {UserAuthResultCode::AUTH_TOKEN_CHECK_FAILED, "Operation failed because of authToken integrity check failed."}, - {UserAuthResultCode::AUTH_TOKEN_EXPIRED, "Operation failed because of authToken has expired."} -}; - -struct DeleteRefHolder { - napi_env env {nullptr}; - napi_ref ref {nullptr}; -}; -} - -JsRefHolder::JsRefHolder(napi_env env, napi_value value) -{ - if (env == nullptr || value == nullptr) { - IAM_LOGE("get null ptr"); - return; - } - napi_status ret = UserAuthNapiHelper::GetFunctionRef(env, value, ref_); - if (ret != napi_ok) { - IAM_LOGE("GetFunctionRef fail %{public}d", ret); - ref_ = nullptr; - return; - } - env_ = env; -} - -JsRefHolder::~JsRefHolder() -{ - if (!IsValid()) { - IAM_LOGI("invalid"); - return; - } - IAM_LOGD("delete reference"); - uv_loop_s *loop; - napi_status napiStatus = napi_get_uv_event_loop(env_, &loop); - if (napiStatus != napi_ok || loop == nullptr) { - IAM_LOGE("napi_get_uv_event_loop fail"); - return; - } - std::shared_ptr deleteRefHolder = Common::MakeShared(); - if (deleteRefHolder == nullptr) { - IAM_LOGE("deleteRefHolder is null"); - return; - } - deleteRefHolder->env = env_; - deleteRefHolder->ref = ref_; - auto task = [deleteRefHolder] () { - IAM_LOGI("start"); - if (deleteRefHolder == nullptr) { - IAM_LOGE("deleteRefHolder is invalid"); - return; - } - napi_status ret = napi_delete_reference(deleteRefHolder->env, deleteRefHolder->ref); - if (ret != napi_ok) { - IAM_LOGE("napi_delete_reference fail %{public}d", ret); - return; - } - }; - if (napi_status::napi_ok != napi_send_event(env_, task, napi_eprio_immediate)) { - IAM_LOGE("napi_send_event: Failed to SendEvent"); - } -} - -bool JsRefHolder::IsValid() const -{ - return (env_ != nullptr && ref_ != nullptr); -} - -napi_ref JsRefHolder::Get() const -{ - return ref_; -} - -int32_t UserAuthNapiHelper::GetResultCodeV8(int32_t result) -{ - if (result == CHECK_PERMISSION_FAILED) { - return static_cast(UserAuthResultCode::OHOS_CHECK_PERMISSION_FAILED); - } - if (result == PIN_EXPIRED) { - return GENERAL_ERROR; - } - if ((result < SUCCESS) || (result > NOT_ENROLLED)) { - return GENERAL_ERROR; - } - return result; -} - -int32_t UserAuthNapiHelper::GetResultCodeV9(int32_t result) -{ - if (result == CHECK_PERMISSION_FAILED) { - return static_cast(UserAuthResultCode::OHOS_CHECK_PERMISSION_FAILED); - } - if (result == INVALID_PARAMETERS) { - return static_cast(UserAuthResultCode::OHOS_INVALID_PARAM); - } - if (result == PIN_EXPIRED) { - return static_cast(UserAuthResultCode::GENERAL_ERROR); - } - if (result > (INT32_MAX - static_cast(UserAuthResultCode::RESULT_CODE_V9_MIN))) { - return static_cast(UserAuthResultCode::GENERAL_ERROR); - } - int32_t resultCodeV9 = result + static_cast(UserAuthResultCode::RESULT_CODE_V9_MIN); - if (resultCodeV9 >= static_cast(UserAuthResultCode::RESULT_CODE_V9_MIN) && - resultCodeV9 <= static_cast(UserAuthResultCode::RESULT_CODE_V9_MAX)) { - return resultCodeV9; - } - return static_cast(UserAuthResultCode::GENERAL_ERROR); -} - -int32_t UserAuthNapiHelper::GetResultCodeV10(int32_t result) -{ - if (result == CHECK_PERMISSION_FAILED) { - return static_cast(UserAuthResultCode::OHOS_CHECK_PERMISSION_FAILED); - } - if (result == INVALID_PARAMETERS) { - return static_cast(UserAuthResultCode::OHOS_INVALID_PARAM); - } - if (result == CHECK_SYSTEM_APP_FAILED) { - return static_cast(UserAuthResultCode::OHOS_CHECK_SYSTEM_APP_FAILED); - } - if (result == HARDWARE_NOT_SUPPORTED) { - return static_cast(UserAuthResultCode::GENERAL_ERROR); - } - if (result > (INT32_MAX - static_cast(UserAuthResultCode::RESULT_CODE_V10_MIN))) { - return static_cast(UserAuthResultCode::GENERAL_ERROR); - } - int32_t resultCodeV10 = result + static_cast(UserAuthResultCode::RESULT_CODE_V10_MIN); - if (resultCodeV10 >= static_cast(UserAuthResultCode::RESULT_CODE_V10_MIN) && - resultCodeV10 <= static_cast(UserAuthResultCode::RESULT_CODE_V10_MAX)) { - IAM_LOGI("version GetResultCodeV10 resultCodeV10 result: %{public}d", resultCodeV10); - return resultCodeV10; - } - IAM_LOGE("version GetResultCodeV10 resultCodeV10 error"); - return static_cast(UserAuthResultCode::GENERAL_ERROR); -} - -int32_t UserAuthNapiHelper::GetResultCodeV20(int32_t result) -{ - if (result == INVALID_PARAMETERS) { - return static_cast(UserAuthResultCode::PARAM_VERIFIED_FAILED); - } - if (result == AUTH_TOKEN_CHECK_FAILED) { - return static_cast(UserAuthResultCode::AUTH_TOKEN_CHECK_FAILED); - } - if (result == AUTH_TOKEN_EXPIRED) { - return static_cast(UserAuthResultCode::AUTH_TOKEN_EXPIRED); - } - return GetResultCodeV10(result); -} - -napi_value UserAuthNapiHelper::GenerateBusinessErrorV9(napi_env env, UserAuthResultCode result) -{ - napi_value code; - std::string msgStr; - auto res = g_resultV92Str.find(result); - if (res == g_resultV92Str.end()) { - IAM_LOGE("result %{public}d not found", static_cast(result)); - msgStr = g_resultV92Str.at(UserAuthResultCode::GENERAL_ERROR); - NAPI_CALL(env, napi_create_int32(env, static_cast(UserAuthResultCode::GENERAL_ERROR), &code)); - } else { - msgStr = res->second; - NAPI_CALL(env, napi_create_int32(env, static_cast(result), &code)); - } - IAM_LOGI("get msg %{public}s", msgStr.c_str()); - - napi_value msg; - NAPI_CALL(env, napi_create_string_utf8(env, msgStr.c_str(), NAPI_AUTO_LENGTH, &msg)); - - napi_value businessError; - NAPI_CALL(env, napi_create_error(env, nullptr, msg, &businessError)); - NAPI_CALL(env, napi_set_named_property(env, businessError, "code", code)); - - return businessError; -} - -napi_value UserAuthNapiHelper::GenerateErrorMsg(napi_env env, UserAuthResultCode result, std::string errorMsg) -{ - napi_value code; - std::string msgStr; - auto res = g_resultV92Str.find(result); - if (res == g_resultV92Str.end()) { - IAM_LOGE("result %{public}d not found", static_cast(result)); - msgStr = g_resultV92Str.at(UserAuthResultCode::GENERAL_ERROR); - NAPI_CALL(env, napi_create_int32(env, static_cast(UserAuthResultCode::GENERAL_ERROR), &code)); - } else { - msgStr = errorMsg; - NAPI_CALL(env, napi_create_int32(env, static_cast(result), &code)); - } - IAM_LOGI("error msg %{public}s", msgStr.c_str()); - - napi_value msg; - NAPI_CALL(env, napi_create_string_utf8(env, msgStr.c_str(), NAPI_AUTO_LENGTH, &msg)); - - napi_value businessError; - NAPI_CALL(env, napi_create_error(env, nullptr, msg, &businessError)); - NAPI_CALL(env, napi_set_named_property(env, businessError, "code", code)); - - return businessError; -} - -UserAuthResultCode UserAuthNapiHelper::ThrowErrorMsg(napi_env env, UserAuthResultCode errorCode, std::string errorMsg) -{ - napi_throw(env, UserAuthNapiHelper::GenerateErrorMsg(env, errorCode, errorMsg)); - return errorCode; -} - -napi_status UserAuthNapiHelper::CheckNapiType(napi_env env, napi_value value, napi_valuetype type) -{ - napi_valuetype valuetype; - napi_status result = napi_typeof(env, value, &valuetype); - if (result != napi_ok) { - IAM_LOGE("napi_typeof fail"); - return result; - } - if (valuetype != type) { - IAM_LOGE("check valuetype fail"); - return napi_generic_failure; - } - return napi_ok; -} - -napi_status UserAuthNapiHelper::GetInt32Value(napi_env env, napi_value value, int32_t &out) -{ - napi_status result = CheckNapiType(env, value, napi_number); - if (result != napi_ok) { - IAM_LOGE("CheckNapiType fail"); - return result; - } - result = napi_get_value_int32(env, value, &out); - if (result != napi_ok) { - IAM_LOGE("napi_get_value_int32 fail"); - } - return result; -} - -napi_status UserAuthNapiHelper::GetUint32Value(napi_env env, napi_value value, uint32_t &out) -{ - napi_status result = CheckNapiType(env, value, napi_number); - if (result != napi_ok) { - IAM_LOGE("CheckNapiType fail"); - return result; - } - result = napi_get_value_uint32(env, value, &out); - if (result != napi_ok) { - IAM_LOGE("napi_get_value_uint32 fail"); - } - return result; -} - -napi_status UserAuthNapiHelper::GetStrValue(napi_env env, napi_value value, char *out, size_t &len) -{ - if (out == nullptr) { - IAM_LOGE("invalid out parameter"); - return napi_invalid_arg; - } - napi_status result = CheckNapiType(env, value, napi_string); - if (result != napi_ok) { - IAM_LOGE("CheckNapiType fail"); - return result; - } - size_t maxLen = len; - result = napi_get_value_string_utf8(env, value, out, maxLen, &len); - if (result != napi_ok) { - IAM_LOGE("napi_get_value_string_utf8 fail"); - } - if (maxLen > 0) { - out[maxLen - 1] = '\0'; - } - return result; -} - -napi_status UserAuthNapiHelper::GetFunctionRef(napi_env env, napi_value value, napi_ref &ref) -{ - napi_status result = CheckNapiType(env, value, napi_function); - if (result != napi_ok) { - IAM_LOGE("CheckNapiType fail"); - return result; - } - result = napi_create_reference(env, value, 1, &ref); - if (result != napi_ok) { - IAM_LOGE("napi_create_reference fail"); - } - return result; -} - -napi_status UserAuthNapiHelper::GetUint8ArrayValue(napi_env env, napi_value value, - size_t limitLen, std::vector &array) -{ - bool isTypedarray; - napi_status result = napi_is_typedarray(env, value, &isTypedarray); - if (result != napi_ok) { - IAM_LOGE("napi_is_typedarray fail"); - return result; - } - if (!isTypedarray) { - IAM_LOGE("value is not typedarray"); - return napi_array_expected; - } - napi_typedarray_type type; - size_t length; - void *data; - napi_value buffer; - size_t offset; - result = napi_get_typedarray_info(env, value, &type, &length, &data, &buffer, &offset); - if (result != napi_ok) { - IAM_LOGE("napi_get_typedarray_info fail"); - return result; - } - if (type != napi_uint8_array) { - IAM_LOGE("value is not napi_uint8_array"); - return napi_invalid_arg; - } - if (length > limitLen) { - IAM_LOGE("array length reach limit"); - return napi_generic_failure; - } - array.resize(length); - if (memcpy_s(array.data(), length, data, length) != EOK) { - IAM_LOGE("memcpy_s fail"); - return napi_generic_failure; - } - return result; -} - -napi_value UserAuthNapiHelper::Uint64ToNapiUint8Array(napi_env env, uint64_t value) -{ - void *data = nullptr; - napi_value arraybuffer = nullptr; - size_t length = sizeof(value); - NAPI_CALL(env, napi_create_arraybuffer(env, length, &data, &arraybuffer)); - if (memcpy_s(data, length, reinterpret_cast(&value), length) != EOK) { - IAM_LOGE("memcpy_s fail"); - return nullptr; - } - napi_value result = nullptr; - NAPI_CALL(env, napi_create_typedarray(env, napi_uint8_array, length, arraybuffer, 0, &result)); - return result; -} - -napi_value UserAuthNapiHelper::Uint8VectorToNapiUint8Array(napi_env env, std::vector &value) -{ - void *data = nullptr; - napi_value arraybuffer = nullptr; - size_t length = value.size(); - NAPI_CALL(env, napi_create_arraybuffer(env, length, &data, &arraybuffer)); - if (memcpy_s(data, length, reinterpret_cast(value.data()), length) != EOK) { - IAM_LOGE("memcpy_s fail"); - return nullptr; - } - napi_value result = nullptr; - NAPI_CALL(env, napi_create_typedarray(env, napi_uint8_array, length, arraybuffer, 0, &result)); - return result; -} - -napi_status UserAuthNapiHelper::CallVoidNapiFunc(napi_env env, napi_ref funcRef, size_t argc, const napi_value *argv) -{ - napi_value funcVal; - napi_status ret = napi_get_reference_value(env, funcRef, &funcVal); - if (ret != napi_ok) { - IAM_LOGE("napi_get_reference_value failed %{public}d", ret); - return ret; - } - napi_value undefined; - ret = napi_get_undefined(env, &undefined); - if (ret != napi_ok) { - IAM_LOGE("napi_get_undefined failed %{public}d", ret); - return ret; - } - napi_value callResult; - ret = napi_call_function(env, undefined, funcVal, argc, argv, &callResult); - if (ret != napi_ok) { - IAM_LOGE("napi_call_function failed %{public}d", ret); - } - return ret; -} - -napi_status UserAuthNapiHelper::SetInt32Property(napi_env env, napi_value obj, const char *name, int32_t value) -{ - napi_value napiValue = nullptr; - napi_status ret = napi_create_int32(env, value, &napiValue); - if (ret != napi_ok) { - IAM_LOGE("napi_create_int32 failed %{public}d", ret); - return ret; - } - ret = napi_set_named_property(env, obj, name, napiValue); - if (ret != napi_ok) { - IAM_LOGE("napi_set_named_property failed %{public}d", ret); - } - return ret; -} - -napi_status UserAuthNapiHelper::SetUint32Property(napi_env env, napi_value obj, const char *name, uint32_t value) -{ - napi_value napiValue = nullptr; - napi_status ret = napi_create_uint32(env, value, &napiValue); - if (ret != napi_ok) { - IAM_LOGE("napi_create_uint32 failed %{public}d", ret); - return ret; - } - ret = napi_set_named_property(env, obj, name, napiValue); - if (ret != napi_ok) { - IAM_LOGE("napi_set_named_property failed %{public}d", ret); - } - return ret; -} - -napi_status UserAuthNapiHelper::SetUint8ArrayProperty(napi_env env, - napi_value obj, const char *name, const std::vector &value) -{ - size_t size = value.size(); - void *data; - napi_value buffer; - napi_status ret = napi_create_arraybuffer(env, size, &data, &buffer); - if (ret != napi_ok) { - IAM_LOGE("napi_create_arraybuffer failed %{public}d", ret); - return ret; - } - if (size != 0) { - if (memcpy_s(data, size, value.data(), value.size()) != EOK) { - IAM_LOGE("memcpy_s failed"); - return napi_generic_failure; - } - } - napi_value napiValue; - ret = napi_create_typedarray(env, napi_uint8_array, size, buffer, 0, &napiValue); - if (ret != napi_ok) { - IAM_LOGE("napi_create_typedarray failed %{public}d", ret); - return ret; - } - ret = napi_set_named_property(env, obj, name, napiValue); - if (ret != napi_ok) { - IAM_LOGE("napi_set_named_property failed %{public}d", ret); - } - return ret; -} - -napi_status UserAuthNapiHelper::SetEnrolledStateProperty(napi_env env, napi_value obj, - const char *name, EnrolledState &value) -{ - napi_value napiValue = nullptr; - napi_status ret = napi_create_object(env, &napiValue); - if (ret != napi_ok) { - IAM_LOGE("napi_create_object failed %{public}d", ret); - return ret; - } - int32_t credentialDigest = static_cast(value.credentialDigest); - int32_t credentialCount = static_cast(value.credentialCount); - IAM_LOGI("get enrolled state success, credentialDigest = %{public}d, credentialCount = %{public}d", - credentialDigest, credentialCount); - ret = UserAuthNapiHelper::SetInt32Property(env, napiValue, "credentialDigest", credentialDigest); - if (ret != napi_ok) { - IAM_LOGE("napi_create_int32 failed %{public}d", ret); - return ret; - } - ret = UserAuthNapiHelper::SetInt32Property(env, napiValue, "credentialCount", credentialCount); - if (ret != napi_ok) { - IAM_LOGE("napi_create_int32 failed %{public}d", ret); - return ret; - } - ret = napi_set_named_property(env, obj, name, napiValue); - if (ret != napi_ok) { - IAM_LOGE("napi_set_named_property failed %{public}d", ret); - } - return ret; -} - -napi_value UserAuthNapiHelper::GetNamedProperty(napi_env env, napi_value object, const std::string &propertyName) -{ - napi_value value = nullptr; - bool hasProperty = false; - NAPI_CALL(env, napi_has_named_property(env, object, propertyName.c_str(), &hasProperty)); - if (!hasProperty) { - return value; - } - NAPI_CALL(env, napi_get_named_property(env, object, propertyName.c_str(), &value)); - return value; -} - -std::string UserAuthNapiHelper::GetStringFromValueUtf8(napi_env env, napi_value value) -{ - if (CheckNapiType(env, value, napi_string) != napi_ok) { - return ""; - } - std::string result; - std::vector str(MAX_STRING_LENGTH + 1, '\0'); - size_t length = 0; - NAPI_CALL(env, napi_get_value_string_utf8(env, value, &str[0], MAX_STRING_LENGTH, &length)); - if (length > 0) { - return result.append(&str[0], length); - } - return result; -} - -bool UserAuthNapiHelper::HasNamedProperty(napi_env env, napi_value object, const std::string &propertyName) -{ - bool hasProperty = false; - NAPI_CALL_BASE(env, napi_has_named_property(env, object, propertyName.c_str(), &hasProperty), false); - return hasProperty; -} - -std::string UserAuthNapiHelper::GetStringPropertyUtf8(napi_env env, napi_value object, const std::string &propertyName) -{ - napi_value value = GetNamedProperty(env, object, propertyName); - napi_status result = CheckNapiType(env, value, napi_string); - if (result != napi_ok) { - return ""; - } - return GetStringFromValueUtf8(env, value); -} - -bool UserAuthNapiHelper::SetStringPropertyUtf8( - napi_env env, napi_value object, const std::string &name, const std::string &value) -{ - napi_value jsValue = nullptr; - if (napi_create_string_utf8(env, value.c_str(), strlen(value.c_str()), &jsValue) != napi_ok) { - IAM_LOGE("get string error"); - return false; - } - napi_valuetype valueType = napi_undefined; - NAPI_CALL_BASE(env, napi_typeof(env, jsValue, &valueType), napi_undefined); - napi_set_named_property(env, object, name.c_str(), jsValue); - return true; -} - -bool UserAuthNapiHelper::GetInt32Array(napi_env env, napi_value obj, std::vector vec) -{ - vec.clear(); - uint32_t len; - napi_get_array_length(env, obj, &len); - IAM_LOGI("GetInt32Array length: %{public}d", len); - for (uint32_t index = 0; index < len; index++) { - napi_value value; - uint32_t getValue; - NAPI_CALL_BASE(env, napi_get_element(env, obj, index, &value), napi_undefined); - NAPI_CALL_BASE(env, napi_get_value_uint32(env, value, &getValue), napi_undefined); - IAM_LOGI("vec[%{public}d]: %{public}d", index, len); - vec.emplace_back(getValue); - } - return true; -} - -bool UserAuthNapiHelper::CheckAuthType(int32_t authType) -{ - if (authType != AuthType::FACE && authType != AuthType::FINGERPRINT) { - IAM_LOGE("authType check fail:%{public}d", authType); - return false; - } - return true; -} - -bool UserAuthNapiHelper::CheckUserAuthType(int32_t authType) -{ - const std::set WIDGET_AUTH_TYPE_VALID_SET = {AuthType::PIN, AuthType::FACE, AuthType::FINGERPRINT, - AuthType::PRIVATE_PIN}; - if (WIDGET_AUTH_TYPE_VALID_SET.find(static_cast(authType)) == WIDGET_AUTH_TYPE_VALID_SET.end()) { - IAM_LOGE("authType check fail:%{public}d", authType); - return false; - } - return true; -} - -bool UserAuthNapiHelper::CheckAuthTrustLevel(uint32_t authTrustLevel) -{ - if (authTrustLevel != AuthTrustLevel::ATL1 && authTrustLevel != AuthTrustLevel::ATL2 && - authTrustLevel != AuthTrustLevel::ATL3 && authTrustLevel != AuthTrustLevel::ATL4) { - IAM_LOGE("authTrustLevel check fail:%{public}d", authTrustLevel); - return false; - } - return true; -} - -bool UserAuthNapiHelper::CheckReuseUnlockResult(ReuseUnlockResult reuseUnlockResult) -{ - if (reuseUnlockResult.reuseMode != ReuseMode::AUTH_TYPE_RELEVANT && - reuseUnlockResult.reuseMode != ReuseMode::AUTH_TYPE_IRRELEVANT && - reuseUnlockResult.reuseMode != ReuseMode::CALLER_IRRELEVANT_AUTH_TYPE_RELEVANT && - reuseUnlockResult.reuseMode != ReuseMode::CALLER_IRRELEVANT_AUTH_TYPE_IRRELEVANT) { - IAM_LOGE("reuseMode check fail:%{public}u", reuseUnlockResult.reuseMode); - return false; - } - if (reuseUnlockResult.reuseDuration <= 0 || reuseUnlockResult.reuseDuration > MAX_ALLOWABLE_REUSE_DURATION) { - IAM_LOGE("reuseDuration check fail:%{public}" PRIu64, reuseUnlockResult.reuseDuration); - return false; - } - return true; -} -} // namespace UserAuth -} // namespace UserIam -} // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/user_auth/src/user_auth_param_utils.cpp b/frameworks/js/napi/user_auth/src/user_auth_param_utils.cpp index 5825bd293..bb223639b 100644 --- a/frameworks/js/napi/user_auth/src/user_auth_param_utils.cpp +++ b/frameworks/js/napi/user_auth/src/user_auth_param_utils.cpp @@ -41,6 +41,7 @@ namespace { const std::string AUTH_PARAM_AUTHTRUSTLEVEL = "authTrustLevel"; const std::string AUTH_PARAM_REUSEUNLOCKRESULT = "reuseUnlockResult"; const std::string AUTH_PARAM_USER_ID = "userId"; + const std::string AUTH_PARAM_SKIP_LOCKED_BIOMETRIC_AUTH = "skipLockedBiometricAuth"; const std::string WIDGET_PARAM_TITLE = "title"; const std::string WIDGET_PARAM_NAVIBTNTEXT = "navigationButtonText"; const std::string WIDGET_PARAM_WINDOWMODE = "windowMode"; @@ -270,6 +271,11 @@ UserAuthResultCode UserAuthParamUtils::InitAuthParam(napi_env env, napi_value va IAM_LOGE("ProcessAuthTrustLevelAndUserId fail:%{public}d", errorCode); return errorCode; } + errorCode = ProcessSkipLockedBiometricAuth(env, value, authParam); + if (errorCode != UserAuthResultCode::SUCCESS) { + IAM_LOGE("ProcessSkipLockedBiometricAuth fail:%{public}d", errorCode); + return errorCode; + } return UserAuthResultCode::SUCCESS; } @@ -425,6 +431,23 @@ bool UserAuthParamUtils::CheckUIContext(const std::shared_ptrHasCommandCallback()) { IAM_LOGE("no command callback register yet"); return UserAuthResultCode::GENERAL_ERROR; diff --git a/frameworks/native/client/src/user_auth_client_impl.cpp b/frameworks/native/client/src/user_auth_client_impl.cpp index a94e4bb6d..e10d8a422 100644 --- a/frameworks/native/client/src/user_auth_client_impl.cpp +++ b/frameworks/native/client/src/user_auth_client_impl.cpp @@ -801,6 +801,7 @@ void UserAuthClientImpl::InitIpcAuthParam(const AuthParamInner &authParam, }); ipcAuthParamInner.authTrustLevel = static_cast(authParam.authTrustLevel); ipcAuthParamInner.authIntent = static_cast(authParam.authIntent); + ipcAuthParamInner.skipLockedBiometricAuth = authParam.skipLockedBiometricAuth; ipcAuthParamInner.reuseUnlockResult.isReuse = authParam.reuseUnlockResult.isReuse; ipcAuthParamInner.reuseUnlockResult.reuseMode = authParam.reuseUnlockResult.reuseMode; ipcAuthParamInner.reuseUnlockResult.reuseDuration = authParam.reuseUnlockResult.reuseDuration; diff --git a/frameworks/native/client/src/user_auth_napi_client_impl.cpp b/frameworks/native/client/src/user_auth_napi_client_impl.cpp index e24b26005..4a06469dd 100644 --- a/frameworks/native/client/src/user_auth_napi_client_impl.cpp +++ b/frameworks/native/client/src/user_auth_napi_client_impl.cpp @@ -101,6 +101,7 @@ uint64_t UserAuthNapiClientImpl::BeginWidgetAuth(int32_t apiVersion, const AuthP .authTypes = authParam.authTypes, .authTrustLevel = authParam.authTrustLevel, .reuseUnlockResult = authParam.reuseUnlockResult, + .skipLockedBiometricAuth = authParam.skipLockedBiometricAuth, }; WidgetParamInner widgetParamInner = { .title = widgetParam.title, @@ -186,6 +187,7 @@ void UserAuthNapiClientImpl::InitIpcAuthParam(const AuthParamInner &authParam, }); ipcAuthParamInner.authTrustLevel = static_cast(authParam.authTrustLevel); ipcAuthParamInner.authIntent = static_cast(authParam.authIntent); + ipcAuthParamInner.skipLockedBiometricAuth = authParam.skipLockedBiometricAuth; ipcAuthParamInner.reuseUnlockResult.isReuse = authParam.reuseUnlockResult.isReuse; ipcAuthParamInner.reuseUnlockResult.reuseMode = authParam.reuseUnlockResult.reuseMode; ipcAuthParamInner.reuseUnlockResult.reuseDuration = authParam.reuseUnlockResult.reuseDuration; diff --git a/frameworks/native/ipc/common_defines/user_auth_common_defines.h b/frameworks/native/ipc/common_defines/user_auth_common_defines.h index d88006f38..283a5414d 100644 --- a/frameworks/native/ipc/common_defines/user_auth_common_defines.h +++ b/frameworks/native/ipc/common_defines/user_auth_common_defines.h @@ -71,6 +71,8 @@ struct AuthParamInner { ReuseUnlockResult reuseUnlockResult; /** Auth intention. */ AuthIntent authIntent; + /** Skip locked biometric authentication */ + bool skipLockedBiometricAuth {false}; }; /** diff --git a/frameworks/native/ipc/idl/UserAuthTypes.idl b/frameworks/native/ipc/idl/UserAuthTypes.idl index be3edc4cc..97ae356aa 100644 --- a/frameworks/native/ipc/idl/UserAuthTypes.idl +++ b/frameworks/native/ipc/idl/UserAuthTypes.idl @@ -123,6 +123,8 @@ struct IpcAuthParamInner { IpcReuseUnlockResult reuseUnlockResult; /** Auth intention. */ int authIntent; + /** Skip locked biometric authentication */ + boolean skipLockedBiometricAuth; }; /** diff --git a/interfaces/inner_api/iam_common_defines.h b/interfaces/inner_api/iam_common_defines.h index bdf3ce177..daf93e7e3 100644 --- a/interfaces/inner_api/iam_common_defines.h +++ b/interfaces/inner_api/iam_common_defines.h @@ -230,7 +230,7 @@ enum ResultCode : int32_t { /** * @brief The auth intent. */ -enum AuthIntent : int { +enum AuthIntent : int32_t { /**< The auth intention is default. */ DEFAULT = 0, /**< The auth intention is unlock. */ diff --git a/services/context/inc/context_callback.h b/services/context/inc/context_callback.h index c926cb7b6..f96ce4945 100644 --- a/services/context/inc/context_callback.h +++ b/services/context/inc/context_callback.h @@ -95,6 +95,8 @@ public: virtual void SetTraceConnectionName(const std::string &connectionName) = 0; virtual void SetTraceAuthFinishReason(const std::string &authFinishReason) = 0; virtual void SetTraceIsBackgroundApplication(bool isBackgroundApplication) = 0; + virtual int32_t ParseAuthTipInfo(int32_t tip, const std::vector &extraInfo, + int32_t &authResult, int32_t &freezingTime) = 0; virtual void ProcessAuthResult(int32_t tip, const std::vector &extraInfo) = 0; virtual sptr GetIamCallback() = 0; virtual std::string GetCallerName() = 0; diff --git a/services/context/inc/context_factory.h b/services/context/inc/context_factory.h index 8203c4e34..21a89f7e0 100644 --- a/services/context/inc/context_factory.h +++ b/services/context/inc/context_factory.h @@ -59,6 +59,7 @@ public: bool isPinExpired {false}; bool isOsAccountVerified {false}; bool isBackgroundApplication {false}; + bool skipLockedBiometricAuth {false}; }; static std::shared_ptr CreateSimpleAuthContext(const Authentication::AuthenticationPara ¶, diff --git a/services/context/inc/widget_client.h b/services/context/inc/widget_client.h index 75181f281..f11aaf733 100644 --- a/services/context/inc/widget_client.h +++ b/services/context/inc/widget_client.h @@ -46,8 +46,9 @@ public: // interaction with widget ResultCode OnNotice(NoticeType type, const std::string &eventData); void ReportWidgetResult(int32_t result, AuthType authType, - int32_t lockoutDuration, int32_t remainAttempts); - void ReportWidgetTip(int32_t tipType, AuthType authType, std::vector tipInfo); + int32_t lockoutDuration, int32_t remainAttempts, bool skipLockedBiometricAuth); + void ReportWidgetTip(int32_t tipType, AuthType authType, std::vector tipInfo, + bool skipLockedBiometricAuth); // others void SetPinSubType(const PinSubType &subType); diff --git a/services/context/inc/widget_context.h b/services/context/inc/widget_context.h index a3dd9b374..d08c2c24a 100644 --- a/services/context/inc/widget_context.h +++ b/services/context/inc/widget_context.h @@ -75,12 +75,15 @@ public: void EndAuthAsWidgetParaInvalid() override; void StopAuthList(const std::vector &authTypeList) override; void SuccessAuth(AuthType authType) override; + void FailAuth(AuthType authType) override; bool AuthWidgetReload(uint32_t orientation, uint32_t needRotate, uint32_t alreadyLoad, AuthType &rotateAuthType) override; void AuthWidgetReloadInit() override; void AuthResult(int32_t resultCode, int32_t authType, const Attributes &finalResult); void AuthTipInfo(int32_t tipInfo, int32_t authType, const Attributes &extraInfo); + void SendAuthTipInfo(int32_t authType, int32_t tipInfo) override; + protected: virtual bool OnStart(); virtual void OnResult(int32_t resultCode, const std::shared_ptr &scheduleResultAttr); @@ -112,6 +115,15 @@ private: std::string GetCallingBundleName(); bool IsSupportFollowCallerUi(); void SetSysDialogZOrder(WidgetCmdParameters &widgetCmdParameters); + UserAuthTipCode CaclAuthTipCode(int32_t authResult, int32_t freezingTime); + void SendMidAuthResult(AuthType authType, int32_t resultCode, int32_t freezingTime); + int32_t ParseAuthTipInfo(int32_t tip, int32_t authType, const std::vector &extraInfo, + int32_t &authResult, int32_t &freezingTime); + void ProcAuthTipInfo(int32_t tip, AuthType authType, const std::vector &extraInfo); + void ProcSuccAuthResult(int32_t resultCode, AuthType authType, const Attributes &finalResult); + void ProcFailAuthResult(int32_t resultCode, AuthType authType, int32_t freezingTime); + bool IsSingleFaceOrFingerPrintAuth(); + bool IsNavigationAuth(); private: struct TaskInfo { diff --git a/services/context/inc/widget_json.h b/services/context/inc/widget_json.h index 028f8804f..a16ef5fb9 100644 --- a/services/context/inc/widget_json.h +++ b/services/context/inc/widget_json.h @@ -89,6 +89,7 @@ struct WidgetCommand { std::string callingProcessName {""}; std::string widgetContextIdStr {""}; int32_t userId {-1}; + bool skipLockedBiometricAuth {false}; }; void to_json(nlohmann::json &jsonCommand, const WidgetCommand &command); diff --git a/services/context/src/auth_widget_helper.cpp b/services/context/src/auth_widget_helper.cpp index 5090fa954..45fed9675 100644 --- a/services/context/src/auth_widget_helper.cpp +++ b/services/context/src/auth_widget_helper.cpp @@ -49,6 +49,7 @@ bool AuthWidgetHelper::InitWidgetContextParam(const AuthParamInner &authParam, s para.challenge = std::move(authParam.challenge); para.authTypeList = std::move(validType); para.atl = authParam.authTrustLevel; + para.skipLockedBiometricAuth = authParam.skipLockedBiometricAuth; para.widgetParam = widgetParam; if (widgetParam.windowMode == WindowModeType::UNKNOWN_WINDOW_MODE) { para.widgetParam.windowMode = WindowModeType::DIALOG_BOX; diff --git a/services/context/src/context_callback_impl.cpp b/services/context/src/context_callback_impl.cpp index 16410bc6f..27f575c71 100644 --- a/services/context/src/context_callback_impl.cpp +++ b/services/context/src/context_callback_impl.cpp @@ -59,32 +59,54 @@ void ContextCallbackImpl::OnAcquireInfo(ExecutorRole src, int32_t moduleType, iamCallback_->OnAcquireInfo(moduleType, acquireInfo, acquireMsg); } -void ContextCallbackImpl::ProcessAuthResult(int32_t tip, const std::vector &extraInfo) +int32_t ContextCallbackImpl::ParseAuthTipInfo(int32_t tip, const std::vector &extraInfo, + int32_t &authResult, int32_t &freezingTime) { - if (tip != USER_AUTH_TIP_SINGLE_AUTH_RESULT) { - return; - } - if (extraInfo.empty()) { - return; + IAM_LOGI("tip:%{public}d", tip); + if (tip != USER_AUTH_TIP_SINGLE_AUTH_RESULT || extraInfo.empty()) { + return ResultCode::GENERAL_ERROR; } + std::string tipJson(reinterpret_cast(extraInfo.data()), extraInfo.size()); if (!nlohmann::json::accept(tipJson)) { IAM_LOGE("invalid format"); - return; + return ResultCode::GENERAL_ERROR; } - IAM_LOGI("tipJson:%{public}s", tipJson.c_str()); + auto root = nlohmann::json::parse(tipJson.c_str()); if (root.is_null() || root.is_discarded()) { IAM_LOGE("root is null"); - return; + return ResultCode::GENERAL_ERROR; } + const std::string tipJsonKeyAuthResult = "authResult"; - int32_t authResult = 0; if (root.find(tipJsonKeyAuthResult) == root.end() || !(root[tipJsonKeyAuthResult].is_number())) { IAM_LOGE("authResult is null or is not number"); - return; + return ResultCode::GENERAL_ERROR; } root.at(tipJsonKeyAuthResult).get_to(authResult); + if (authResult == SUCCESS) { + IAM_LOGI("authResult is success"); + return ResultCode::SUCCESS; + } + const std::string tipJsonKeyFreezTime = "lockoutDuration"; + if (root.find(tipJsonKeyFreezTime) == root.end() || !(root[tipJsonKeyFreezTime].is_number())) { + IAM_LOGE("freezingTime is null or is not number"); + return ResultCode::GENERAL_ERROR; + } + root.at(tipJsonKeyFreezTime).get_to(freezingTime); + return ResultCode::SUCCESS; +} + +void ContextCallbackImpl::ProcessAuthResult(int32_t tip, const std::vector &extraInfo) +{ + int32_t authResult = SUCCESS; + int32_t freezingTime = -1; + int32_t ret = ParseAuthTipInfo(tip, extraInfo, authResult, freezingTime); + if (ret != SUCCESS) { + IAM_LOGE("ParseAuthTipInfo fali, ret:%{public}d", ret); + return; + } if (authResult == SUCCESS) { IAM_LOGI("authResult is success"); return; diff --git a/services/context/src/context_callback_impl.h b/services/context/src/context_callback_impl.h index b82b522f4..d87d7a025 100644 --- a/services/context/src/context_callback_impl.h +++ b/services/context/src/context_callback_impl.h @@ -50,6 +50,8 @@ public: void SetTraceAuthFinishReason(const std::string &authFinishReason) override; void SetTraceIsBackgroundApplication(bool isBackgroundApplication) override; void SetCleaner(Context::ContextStopCallback callback) override; + int32_t ParseAuthTipInfo(int32_t tip, const std::vector &extraInfo, + int32_t &authResult, int32_t &freezingTime) override; void ProcessAuthResult(int32_t tip, const std::vector &extraInfo) override; sptr GetIamCallback() override; std::string GetCallerName() override; diff --git a/services/context/src/widget_client.cpp b/services/context/src/widget_client.cpp index 6e943e719..395b5ee7e 100644 --- a/services/context/src/widget_client.cpp +++ b/services/context/src/widget_client.cpp @@ -116,6 +116,10 @@ void WidgetClient::ProcessNotice(const WidgetNotice ¬ice, std::vectorWidgetReload(notice.orientation, notice.needRotate, notice.alreadyLoad, authTypeList[0]); } + } else if (notice.event == EVENT_AUTH_WIDGET_LOADED) { + schedule_->SendAuthTipInfo(authTypeList, TIP_CODE_WIDGET_LOADED); + } else if (notice.event == EVENT_AUTH_WIDGET_RELEASED) { + schedule_->SendAuthTipInfo(authTypeList, TIP_CODE_WIDGET_RELEASED); } } @@ -132,7 +136,7 @@ void WidgetClient::SendCommand(const WidgetCommand &command) } void WidgetClient::ReportWidgetResult(int32_t result, AuthType authType, - int32_t lockoutDuration, int32_t remainAttempts) + int32_t lockoutDuration, int32_t remainAttempts, bool skipLockedBiometricAuth) { WidgetCommand::ExtraInfo extraInfo { .callingBundleName = callingBundleName_, @@ -156,7 +160,8 @@ void WidgetClient::ReportWidgetResult(int32_t result, AuthType authType, .title = widgetParam_.title, .windowModeType = WinModeType2Str(widgetParam_.windowMode), .navigationButtonText = widgetParam_.navigationButtonText, - .cmdList = { cmd } + .cmdList = { cmd }, + .skipLockedBiometricAuth = skipLockedBiometricAuth }; for (auto &type : authTypeList_) { widgetCmd.typeList.emplace_back(AuthType2Str(type)); @@ -167,7 +172,8 @@ void WidgetClient::ReportWidgetResult(int32_t result, AuthType authType, SendCommand(widgetCmd); } -void WidgetClient::ReportWidgetTip(int32_t tipType, AuthType authType, std::vector tipInfo) +void WidgetClient::ReportWidgetTip(int32_t tipType, AuthType authType, std::vector tipInfo, + bool skipLockedBiometricAuth) { // sendCommand of CMD_NOTIFY_AUTH_TIP WidgetCommand::Cmd cmd { @@ -179,7 +185,8 @@ void WidgetClient::ReportWidgetTip(int32_t tipType, AuthType authType, std::vect }; WidgetCommand widgetCmd { .widgetContextId = widgetContextId_, - .cmdList = { cmd } + .cmdList = { cmd }, + .skipLockedBiometricAuth = skipLockedBiometricAuth }; SendCommand(widgetCmd); } @@ -310,7 +317,9 @@ bool WidgetClient::IsValidNoticeType(const WidgetNotice ¬ice) notice.event != NOTICE_EVENT_USER_NAVIGATION && notice.event != NOTICE_EVENT_WIDGET_PARA_INVALID && notice.event != NOTICE_EVENT_RELOAD && - notice.event != NOTICE_EVENT_END) { + notice.event != NOTICE_EVENT_END && + notice.event != EVENT_AUTH_WIDGET_LOADED && + notice.event != EVENT_AUTH_WIDGET_RELEASED) { return false; } return true; diff --git a/services/context/src/widget_context.cpp b/services/context/src/widget_context.cpp index 651d5d38e..25916eede 100644 --- a/services/context/src/widget_context.cpp +++ b/services/context/src/widget_context.cpp @@ -195,6 +195,7 @@ std::shared_ptr WidgetContext::BuildTask(const std::vector &ch para.callerType = para_.callerType; para.sdkVersion = para_.sdkVersion; para.authIntent = authIntent; + para.skipLockedBiometricAuth = para_.skipLockedBiometricAuth; para.isOsAccountVerified = para_.isOsAccountVerified; auto context = ContextFactory::CreateSimpleAuthContext(para, widgetCallback, true); if (context == nullptr || !ContextPool::Instance().Insert(context)) { @@ -256,25 +257,15 @@ void WidgetContext::AuthResult(int32_t resultCode, int32_t authType, const Attri IAM_LOGI("get freezingTime failed."); } AuthType authTypeTmp = static_cast(authType); - WidgetClient::Instance().ReportWidgetResult(resultCode, authTypeTmp, freezingTime, remainTimes); - IF_FALSE_LOGE_AND_RETURN(schedule_ != nullptr); + WidgetClient::Instance().ReportWidgetResult(resultCode, authTypeTmp, freezingTime, remainTimes, + para_.skipLockedBiometricAuth); IF_FALSE_LOGE_AND_RETURN(callerCallback_ != nullptr); callerCallback_->SetTraceAuthType(authTypeTmp); IAM_LOGD("call schedule:"); if (resultCode == ResultCode::SUCCESS || resultCode == ResultCode::COMPLEXITY_CHECK_FAILED) { - finalResult.GetUint8ArrayValue(Attributes::ATTR_SIGNATURE, authResultInfo_.token); - finalResult.GetUint64Value(Attributes::ATTR_CREDENTIAL_DIGEST, authResultInfo_.credentialDigest); - finalResult.GetUint16Value(Attributes::ATTR_CREDENTIAL_COUNT, authResultInfo_.credentialCount); - authResultInfo_.authType = authTypeTmp; - IAM_LOGD("widget token size: %{public}zu.", authResultInfo_.token.size()); - if (resultCode != ResultCode::SUCCESS) { - SetLatestError(resultCode); - } - schedule_->SuccessAuth(authTypeTmp); + ProcSuccAuthResult(resultCode, authTypeTmp, finalResult); } else { - // failed - SetLatestError(resultCode); - schedule_->StopAuthList({authTypeTmp}); + ProcFailAuthResult(resultCode, authTypeTmp, freezingTime); } } @@ -285,7 +276,9 @@ void WidgetContext::AuthTipInfo(int32_t tipType, int32_t authType, const Attribu std::vector tipInfo; bool getTipInfoRet = extraInfo.GetUint8ArrayValue(Attributes::ATTR_EXTRA_INFO, tipInfo); IF_FALSE_LOGE_AND_RETURN(getTipInfoRet); - WidgetClient::Instance().ReportWidgetTip(tipType, static_cast(authType), tipInfo); + WidgetClient::Instance().ReportWidgetTip(tipType, static_cast(authType), tipInfo, + para_.skipLockedBiometricAuth); + ProcAuthTipInfo(tipType, static_cast(authType), tipInfo); } // WidgetScheduleNodeCallback @@ -317,7 +310,8 @@ void WidgetContext::ExecuteAuthList(const std::set &authTypeList, bool if (!task->Start()) { IAM_LOGE("BeginAuthentication failed"); static const int32_t INVALID_VAL = -1; - WidgetClient::Instance().ReportWidgetResult(task->GetLatestError(), authType, INVALID_VAL, INVALID_VAL); + WidgetClient::Instance().ReportWidgetResult(task->GetLatestError(), authType, INVALID_VAL, INVALID_VAL, + para_.skipLockedBiometricAuth); return; } if (authType == FACE) { @@ -440,6 +434,13 @@ void WidgetContext::SuccessAuth(AuthType authType) End(ResultCode::SUCCESS); } +void WidgetContext::FailAuth(AuthType authType) +{ + IAM_LOGI("fail auth. authType:%{public}d", static_cast(authType)); + std::lock_guard lock(mutex_); + End(ResultCode::LOCKED); +} + int32_t WidgetContext::ConnectExtensionAbility(const AAFwk::Want &want, const std::string commandStr) { IAM_LOGD("ConnectExtensionAbility start"); @@ -498,6 +499,14 @@ bool WidgetContext::ConnectExtension(const WidgetRotatePara &widgetRotatePara) para_.authProfileMap[authType] = profile; } } + + if (IsSingleFaceOrFingerPrintAuth() && para_.skipLockedBiometricAuth && + para_.authProfileMap[para_.authTypeList[0]].remainTimes == 0) { + Attributes attr; + callerCallback_->OnResult(ResultCode::LOCKED, attr); + return true; + } + std::string commandData = BuildStartCommand(widgetRotatePara); IAM_LOGI("start command: %{public}s", commandData.c_str()); @@ -667,6 +676,7 @@ std::string WidgetContext::BuildStartCommand(const WidgetRotatePara &widgetRotat widgetCmdParameters.useriamCmdData.typeList = typeList; widgetCmdParameters.useriamCmdData.callingAppID = para_.callingAppID; widgetCmdParameters.useriamCmdData.userId = para_.userId; + widgetCmdParameters.useriamCmdData.skipLockedBiometricAuth = para_.skipLockedBiometricAuth; ProcessRotatePara(widgetCmdParameters, widgetRotatePara); nlohmann::json root = widgetCmdParameters; std::string cmdData = root.dump(); @@ -705,6 +715,109 @@ std::string WidgetContext::GetCallingBundleName() } return ""; } + +bool WidgetContext::IsSingleFaceOrFingerPrintAuth() +{ + if (!para_.widgetParam.navigationButtonText.empty()) { + return false; + } + if (para_.authTypeList.size() == 1 && (para_.authTypeList[0] == FACE || para_.authTypeList[0] == FINGERPRINT)) { + return true; + } + return false; +} + +bool WidgetContext::IsNavigationAuth() +{ + if (para_.widgetParam.navigationButtonText.empty()) { + return false; + } + return false; +} + +void WidgetContext::SendAuthTipInfo(int32_t authType, int32_t tipCode) +{ + IAM_LOGI("authType:%{public}d, tipCode:%{public}d", authType, tipCode); + Attributes attr; + bool setTipInfoRet = attr.SetInt32Value(Attributes::ATTR_TIP_INFO, tipCode); + if (!setTipInfoRet) { + IAM_LOGE("set tipInfo fail"); + return; + } + + IF_FALSE_LOGE_AND_RETURN(callerCallback_ != nullptr); + callerCallback_->OnAcquireInfo(ALL_IN_ONE, authType, attr.Serialize()); +} + +UserAuthTipCode WidgetContext::CaclAuthTipCode(int32_t authResult, int32_t freezingTime) +{ + UserAuthTipCode tipCode = TIP_CODE_FAIL; + if (authResult == ResultCode::TIMEOUT) { + tipCode = TIP_CODE_TIMEOUT; + } else if (freezingTime == INT32_MAX) { + tipCode = TIP_CODE_PERMANENTLY_LOCKED; + } else if (freezingTime > 0) { + tipCode = TIP_CODE_TEMPORARILY_LOCKED; + } else { + tipCode = TIP_CODE_FAIL; + } + return tipCode; +} + +void WidgetContext::SendMidAuthResult(AuthType authType, int32_t resultCode, int32_t freezingTime) +{ + UserAuthTipCode tipCode = CaclAuthTipCode(resultCode, freezingTime); + SendAuthTipInfo(authType, tipCode); + if (para_.skipLockedBiometricAuth && freezingTime > 0) { + if (IsSingleFaceOrFingerPrintAuth()) { + schedule_->FailAuth(authType); + } else if (IsNavigationAuth()) { + schedule_->NaviPinAuth(); + } + } +} + +void WidgetContext::ProcAuthTipInfo(int32_t tip, AuthType authType, const std::vector &extraInfo) +{ + IAM_LOGI("authType:%{public}d, tip:%{public}d", authType, tip); + IF_FALSE_LOGE_AND_RETURN(callerCallback_ != nullptr); + int32_t authResult = ResultCode::GENERAL_ERROR; + int32_t freezingTime = -1; + int32_t ret = callerCallback_->ParseAuthTipInfo(tip, extraInfo, authResult, freezingTime); + if (ret != ResultCode::SUCCESS) { + IAM_LOGE("ParseAuthTipInfo fail"); + return; + } + if (authResult == ResultCode::SUCCESS) { + return; + } + SendMidAuthResult(authType, authResult, freezingTime); +} + +void WidgetContext::ProcSuccAuthResult(int32_t resultCode, AuthType authType, const Attributes &finalResult) +{ + IAM_LOGI("recv task result: %{public}d, authType: %{public}d", resultCode, authType); + IF_FALSE_LOGE_AND_RETURN(schedule_ != nullptr); + finalResult.GetUint8ArrayValue(Attributes::ATTR_SIGNATURE, authResultInfo_.token); + finalResult.GetUint64Value(Attributes::ATTR_CREDENTIAL_DIGEST, authResultInfo_.credentialDigest); + finalResult.GetUint16Value(Attributes::ATTR_CREDENTIAL_COUNT, authResultInfo_.credentialCount); + authResultInfo_.authType = authType; + IAM_LOGD("widget token size: %{public}zu.", authResultInfo_.token.size()); + if (resultCode != ResultCode::SUCCESS) { + SetLatestError(resultCode); + } + schedule_->SuccessAuth(authType); +} + +void WidgetContext::ProcFailAuthResult(int32_t resultCode, AuthType authType, int32_t freezingTime) +{ + IAM_LOGI("recv task result: %{public}d, authType: %{public}d", resultCode, authType); + SetLatestError(resultCode); + SendMidAuthResult(authType, resultCode, freezingTime); + if (!para_.skipLockedBiometricAuth || freezingTime == 0) { + schedule_->StopAuthList({authType}); + } +} } // namespace UserAuth } // namespace UserIam } // namespace OHOS diff --git a/services/context/src/widget_json.cpp b/services/context/src/widget_json.cpp index 100bab628..2b68200d0 100644 --- a/services/context/src/widget_json.cpp +++ b/services/context/src/widget_json.cpp @@ -69,6 +69,7 @@ const std::string JSON_WIDGET_ROTATE_AUTH_TYPE = "rotateAuthType"; const std::string JSON_WIDGET_CALLING_PROCESS_NAME = "callingProcessName"; const std::string JSON_WIDGET_CALLING_APP_ID = "callingAppID"; const std::string JSON_WIDGET_USER_ID = "userId"; +const std::string JSON_WIDGET_SKIP_LOCKED_BIOMETRIC_AUTH = "skipLockedBiometricAuth"; const std::string JSON_UI_EXTENSION_TYPE = "ability.want.params.uiExtensionType"; const std::string JSON_UI_EXT_NODE_ANGLE = "ability.want.params.uiExtNodeAngle"; @@ -140,6 +141,7 @@ void GetJsonCmd(nlohmann::json &jsonCommand, const WidgetCommand &command) jsonCommand[JSON_WIDGET_CALLING_APP_ID] = command.callingAppID; jsonCommand[JSON_WIDGET_CALLING_PROCESS_NAME] = command.callingProcessName; jsonCommand[JSON_WIDGET_USER_ID] = command.userId; + jsonCommand[JSON_WIDGET_SKIP_LOCKED_BIOMETRIC_AUTH] = command.skipLockedBiometricAuth; } } diff --git a/services/core/inc/authentication.h b/services/core/inc/authentication.h index f1d0edf7b..4f5c7ec10 100644 --- a/services/core/inc/authentication.h +++ b/services/core/inc/authentication.h @@ -39,6 +39,7 @@ public: int32_t callerType; int32_t authIntent; bool isOsAccountVerified; + bool skipLockedBiometricAuth; }; struct AuthResultInfo { diff --git a/services/core/inc/widget_schedule_node.h b/services/core/inc/widget_schedule_node.h index 9b301c448..5b3b41253 100644 --- a/services/core/inc/widget_schedule_node.h +++ b/services/core/inc/widget_schedule_node.h @@ -49,7 +49,8 @@ public: E_COMPLETE_AUTH = 4, E_NAVI_PIN_AUTH = 5, E_WIDGET_PARA_INVALID = 6, - E_WIDGET_RELOAD = 7 + E_WIDGET_RELOAD = 7, + E_STOP_AUTH = 8, }; virtual ~WidgetScheduleNode() = default; @@ -59,11 +60,13 @@ public: AuthIntent authIntent) = 0; virtual bool StopAuthList(const std::vector &authTypeList) = 0; virtual bool SuccessAuth(AuthType authType) = 0; + virtual bool FailAuth(AuthType authType) = 0; virtual bool NaviPinAuth() = 0; virtual bool WidgetParaInvalid() = 0; virtual bool WidgetReload(uint32_t orientation, uint32_t needRotate, uint32_t alreadyLoad, AuthType &rotateAuthType) = 0; virtual void SetCallback(std::shared_ptr callback) = 0; + virtual void SendAuthTipInfo(const std::vector &authTypeList, int32_t tipCode) = 0; }; } // namespace UserAuth } // namespace UserIam diff --git a/services/core/inc/widget_schedule_node_callback.h b/services/core/inc/widget_schedule_node_callback.h index 0173d1be4..56c79196c 100644 --- a/services/core/inc/widget_schedule_node_callback.h +++ b/services/core/inc/widget_schedule_node_callback.h @@ -39,9 +39,11 @@ public: virtual void EndAuthAsWidgetParaInvalid() = 0; virtual void StopAuthList(const std::vector &authTypeList) = 0; virtual void SuccessAuth(AuthType authType) = 0; + virtual void FailAuth(AuthType authType) = 0; virtual bool AuthWidgetReload(uint32_t orientation, uint32_t needRotate, uint32_t alreadyLoad, AuthType &rotateAuthType) = 0; virtual void AuthWidgetReloadInit() = 0; + virtual void SendAuthTipInfo(int32_t authType, int32_t tipInfo) = 0; }; } // namespace UserAuth } // namespace UserIam diff --git a/services/core/src/widget_schedule_node_impl.cpp b/services/core/src/widget_schedule_node_impl.cpp index 918f371b0..a50cc1ca8 100644 --- a/services/core/src/widget_schedule_node_impl.cpp +++ b/services/core/src/widget_schedule_node_impl.cpp @@ -58,6 +58,8 @@ std::shared_ptr WidgetScheduleNodeImpl::MakeFiniteStateMachi [this](FiniteStateMachine &machine, uint32_t event) { OnNaviPinAuth(machine, event); }); builder->MakeTransition(S_WIDGET_AUTH_RUNNING, E_COMPLETE_AUTH, S_WIDGET_AUTH_FINISHED, [this](FiniteStateMachine &machine, uint32_t event) { OnSuccessAuth(machine, event); }); + builder->MakeTransition(S_WIDGET_AUTH_RUNNING, E_STOP_AUTH, S_WIDGET_AUTH_FINISHED, + [this](FiniteStateMachine &machine, uint32_t event) { OnFailAuth(machine, event); }); builder->MakeTransition(S_WIDGET_AUTH_RUNNING, E_CANCEL_AUTH, S_WIDGET_AUTH_FINISHED, [this](FiniteStateMachine &machine, uint32_t event) { OnStopSchedule(machine, event); }); builder->MakeTransition(S_WIDGET_AUTH_RUNNING, E_NAVI_PIN_AUTH, S_WIDGET_AUTH_FINISHED, @@ -138,6 +140,14 @@ bool WidgetScheduleNodeImpl::SuccessAuth(AuthType authType) return TryKickMachine(E_COMPLETE_AUTH); } +bool WidgetScheduleNodeImpl::FailAuth(AuthType authType) +{ + std::lock_guard lock(mutex_); + failAuthType_ = authType; + IAM_LOGI("fail %{public}d.", E_COMPLETE_AUTH); + return TryKickMachine(E_STOP_AUTH); +} + bool WidgetScheduleNodeImpl::NaviPinAuth() { std::lock_guard lock(mutex_); @@ -215,6 +225,14 @@ void WidgetScheduleNodeImpl::OnSuccessAuth(FiniteStateMachine &machine, uint32_t callback->SuccessAuth(successAuthType_); } +void WidgetScheduleNodeImpl::OnFailAuth(FiniteStateMachine &machine, uint32_t event) +{ + auto callback = callback_.lock(); + IF_FALSE_LOGE_AND_RETURN(callback != nullptr); + runningAuthTypeSet_.erase(successAuthType_); + callback->FailAuth(failAuthType_); +} + void WidgetScheduleNodeImpl::OnNaviPinAuth(FiniteStateMachine &machine, uint32_t event) { auto callback = callback_.lock(); @@ -250,6 +268,16 @@ void WidgetScheduleNodeImpl::OnWidgetReload(FiniteStateMachine &machine, uint32_ StopSchedule(); } } + +void WidgetScheduleNodeImpl::SendAuthTipInfo(const std::vector &authTypeList, int32_t tipCode) +{ + auto callback = callback_.lock(); + IF_FALSE_LOGE_AND_RETURN(callback != nullptr); + IAM_LOGI("send mid auth result"); + for (auto &authType : authTypeList) { + callback->SendAuthTipInfo(authType, tipCode); + } +} } // namespace UserAuth } // namespace UserIam } // namespace OHOS \ No newline at end of file diff --git a/services/core/src/widget_schedule_node_impl.h b/services/core/src/widget_schedule_node_impl.h index b1e114230..8d342ea32 100644 --- a/services/core/src/widget_schedule_node_impl.h +++ b/services/core/src/widget_schedule_node_impl.h @@ -43,11 +43,13 @@ public: AuthIntent authIntent) override; bool StopAuthList(const std::vector &authTypeList) override; bool SuccessAuth(AuthType authType) override; + bool FailAuth(AuthType authType) override; bool NaviPinAuth() override; bool WidgetParaInvalid() override; bool WidgetReload(uint32_t orientation, uint32_t needRotate, uint32_t alreadyLoad, AuthType &rotateAuthType) override; void SetCallback(std::shared_ptr callback) override; + void SendAuthTipInfo(const std::vector &authTypeList, int32_t tipCode) override; protected: void OnStartSchedule(FiniteStateMachine &machine, uint32_t event); @@ -59,6 +61,7 @@ protected: void OnWidgetParaInvalid(FiniteStateMachine &machine, uint32_t event); void OnWidgetReload(FiniteStateMachine &machine, uint32_t event); void OnWidgetReloadInit(FiniteStateMachine &machine, uint32_t event); + void OnFailAuth(FiniteStateMachine &machine, uint32_t event); private: std::shared_ptr MakeFiniteStateMachine(); @@ -71,6 +74,7 @@ private: std::shared_ptr iamHitraceHelper_ {nullptr}; std::weak_ptr callback_; AuthType successAuthType_ {0}; + AuthType failAuthType_ {0}; std::vector startAuthTypeList_; bool endAfterFirstFail_ {false}; std::vector stopAuthTypeList_; diff --git a/services/ipc/inc/user_auth_service.h b/services/ipc/inc/user_auth_service.h index 94dabd9f5..550cceaa7 100644 --- a/services/ipc/inc/user_auth_service.h +++ b/services/ipc/inc/user_auth_service.h @@ -140,7 +140,9 @@ private: void InitAuthParam(const IpcAuthParamInner &ipcAuthParam, AuthParamInner &authParam); void InitRemoteAuthParam(const IpcRemoteAuthParam &ipcRemoteAuthParam, std::optional &remoteAuthParam); - void InitWidgetParam(const IpcWidgetParamInner &ipcWidgetParam, WidgetParamInner &widgetParam); + void InitWidgetParam(const IpcWidgetParamInner &ipcWidgetParam, WidgetParamInner &widgetParam); + int32_t CheckSkipLockedBiometricAuth(int32_t userId, const AuthParamInner &authParam, + const WidgetParamInner &widgetParam, std::vector &validType); static std::mutex mutex_; static std::shared_ptr instance_; }; diff --git a/services/ipc/src/user_auth_service.cpp b/services/ipc/src/user_auth_service.cpp index 8b6f40ca4..f546f94a6 100644 --- a/services/ipc/src/user_auth_service.cpp +++ b/services/ipc/src/user_auth_service.cpp @@ -36,6 +36,7 @@ #include "service_init_manager.h" #include "soft_bus_manager.h" #include "widget_client.h" +#include "auth_widget_helper.h" #include "remote_msg_util.h" #include "remote_iam_callback.h" #include "remote_auth_service.h" @@ -671,6 +672,7 @@ int32_t UserAuthService::AuthUser(const IpcAuthParamInner &ipcAuthParamInner, para.endAfterFirstFail = false; para.sdkVersion = INNER_API_VERSION_10000; para.authIntent = authParam.authIntent; + para.skipLockedBiometricAuth = authParam.skipLockedBiometricAuth; para.isOsAccountVerified = IpcCommon::IsOsAccountVerified(authParam.userId); return StartAuthUser(authParam, remoteAuthParam, para, contextCallback, contextId); } @@ -1135,6 +1137,37 @@ uint64_t UserAuthService::StartWidgetContext(const std::shared_ptrGetContextId(); } +int32_t UserAuthService::CheckSkipLockedBiometricAuth(int32_t userId, const AuthParamInner &authParam, + const WidgetParamInner &widgetParam, std::vector &validType) +{ + if (!authParam.skipLockedBiometricAuth) { + return SUCCESS; + } + + std::vector authTypeList; + for (auto &type : validType) { + ContextFactory::AuthProfile profile = {}; + if (!AuthWidgetHelper::GetUserAuthProfile(userId, type, profile)) { + IAM_LOGE("get user auth profile failed"); + continue; + } + if (profile.remainTimes != 0) { + authTypeList.push_back(type); + } + } + + if (authTypeList.empty()) { + if (!widgetParam.navigationButtonText.empty()) { + IAM_LOGE("authTypeList is null, return cancel from widget"); + return CANCELED_FROM_WIDGET; + } + IAM_LOGE("authTypeList is null, return lock"); + return LOCKED; + } + validType = std::move(authTypeList); + return SUCCESS; +} + int32_t UserAuthService::CheckValidSolution(int32_t userId, const AuthParamInner &authParam, const WidgetParamInner &widgetParam, std::vector &validType) { @@ -1162,11 +1195,7 @@ int32_t UserAuthService::CheckValidSolution(int32_t userId, const AuthParamInner return authType != AuthType::PIN && authType != AuthType::PRIVATE_PIN; }), validType.end()); } - if (validType.empty()) { - IAM_LOGE("validType size is 0"); - return TYPE_NOT_SUPPORT; - } - return SUCCESS; + return CheckSkipLockedBiometricAuth(userId, authParam, widgetParam, validType); } int32_t UserAuthService::GetCallerInfo(bool isUserIdSpecified, int32_t userId, @@ -1699,6 +1728,7 @@ void UserAuthService::InitAuthParam(const IpcAuthParamInner &ipcAuthParam, AuthP }); authParam.authTrustLevel = static_cast(ipcAuthParam.authTrustLevel); authParam.authIntent = static_cast(ipcAuthParam.authIntent); + authParam.skipLockedBiometricAuth = ipcAuthParam.skipLockedBiometricAuth; authParam.reuseUnlockResult.isReuse = ipcAuthParam.reuseUnlockResult.isReuse; authParam.reuseUnlockResult.reuseMode = static_cast(ipcAuthParam.reuseUnlockResult.reuseMode); authParam.reuseUnlockResult.reuseDuration = ipcAuthParam.reuseUnlockResult.reuseDuration; diff --git a/test/unittest/services/mocks/mock_context.h b/test/unittest/services/mocks/mock_context.h index d4bef94f2..a1045daa4 100644 --- a/test/unittest/services/mocks/mock_context.h +++ b/test/unittest/services/mocks/mock_context.h @@ -50,6 +50,8 @@ public: MOCK_METHOD1(SetTraceReuseUnlockResultMode, void(uint32_t reuseUnlockResultMode)); MOCK_METHOD1(SetTraceReuseUnlockResultDuration, void(uint64_t reuseUnlockResultDuration)); MOCK_METHOD1(SetCleaner, void(Context::ContextStopCallback callback)); + MOCK_METHOD4(ParseAuthTipInfo, int32_t(int32_t tip, const std::vector &extraInfo, + int32_t &authResult, int32_t &freezingTime)); MOCK_METHOD2(ProcessAuthResult, void(int32_t tip, const std::vector &extraInfo)); MOCK_METHOD0(GetIamCallback, sptr()); MOCK_METHOD0(GetCallerName, std::string()); diff --git a/test/unittest/services/mocks/mock_widget_schedule_node.h b/test/unittest/services/mocks/mock_widget_schedule_node.h index 732624851..999121f68 100644 --- a/test/unittest/services/mocks/mock_widget_schedule_node.h +++ b/test/unittest/services/mocks/mock_widget_schedule_node.h @@ -30,11 +30,13 @@ public: AuthIntent authIntent)); MOCK_METHOD1(StopAuthList, bool(const std::vector &)); MOCK_METHOD1(SuccessAuth, bool(AuthType)); + MOCK_METHOD1(FailAuth, bool(AuthType)); MOCK_METHOD0(NaviPinAuth, bool()); MOCK_METHOD0(WidgetParaInvalid, bool()); MOCK_METHOD1(SetCallback, void(std::shared_ptr)); MOCK_METHOD4(WidgetReload, bool(uint32_t orientation, uint32_t needRotate, uint32_t alreadyLoad, AuthType &rotateAuthType)); + MOCK_METHOD2(SendAuthTipInfo, void(const std::vector &authTypeList, int32_t tipCode)); }; } // namespace UserAuth } // namespace UserIam diff --git a/test/unittest/services/mocks/mock_widget_schedule_node_callback.h b/test/unittest/services/mocks/mock_widget_schedule_node_callback.h index 3733061b0..453947cf8 100644 --- a/test/unittest/services/mocks/mock_widget_schedule_node_callback.h +++ b/test/unittest/services/mocks/mock_widget_schedule_node_callback.h @@ -35,6 +35,8 @@ public: MOCK_METHOD0(EndAuthAsWidgetParaInvalid, void()); MOCK_METHOD1(StopAuthList, void(const std::vector &authTypeList)); MOCK_METHOD1(SuccessAuth, void(AuthType authType)); + MOCK_METHOD1(FailAuth, void(AuthType authType)); + MOCK_METHOD2(SendAuthTipInfo, void(int32_t authType, int32_t tipInfo)); MOCK_METHOD4(AuthWidgetReload, bool(uint32_t orientation, uint32_t needRotate, uint32_t alreadyLoad, AuthType &rotateAuthType)); MOCK_METHOD0(AuthWidgetReloadInit, void()); diff --git a/test/unittest/services/src/widget_client_test.cpp b/test/unittest/services/src/widget_client_test.cpp index c1828ca12..16879eacf 100644 --- a/test/unittest/services/src/widget_client_test.cpp +++ b/test/unittest/services/src/widget_client_test.cpp @@ -351,14 +351,14 @@ HWTEST_F(WidgetClientTest, WidgetClientTestReportWidgetResult_0001, TestSize.Lev { WidgetClient::Instance().Reset(); WidgetClient::Instance().SetSensorInfo("fake senor info"); - WidgetClient::Instance().ReportWidgetResult(1, AuthType::FINGERPRINT, 1, 1); + WidgetClient::Instance().ReportWidgetResult(1, AuthType::FINGERPRINT, 1, 1, false); EXPECT_EQ(WidgetClient::Instance().GetAuthTokenId(), 0); } HWTEST_F(WidgetClientTest, WidgetClientTestReportWidgetResult_0002, TestSize.Level0) { WidgetClient::Instance().Reset(); - WidgetClient::Instance().ReportWidgetResult(1, AuthType::FINGERPRINT, 1, 1); + WidgetClient::Instance().ReportWidgetResult(1, AuthType::FINGERPRINT, 1, 1, false); EXPECT_EQ(WidgetClient::Instance().GetAuthTokenId(), 0); } @@ -366,7 +366,7 @@ HWTEST_F(WidgetClientTest, WidgetClientTestReportWidgetResult_0003, TestSize.Lev { WidgetClient::Instance().Reset(); WidgetClient::Instance().SetSensorInfo("fake senor info"); - WidgetClient::Instance().ReportWidgetResult(1, AuthType::PIN, 1, 1); + WidgetClient::Instance().ReportWidgetResult(1, AuthType::PIN, 1, 1, false); EXPECT_EQ(WidgetClient::Instance().GetAuthTokenId(), 0); } @@ -377,7 +377,7 @@ HWTEST_F(WidgetClientTest, WidgetClientTestReportWidgetResult_0004, TestSize.Lev authTypeList.emplace_back(AuthType::PIN); WidgetClient::Instance().SetAuthTypeList(authTypeList); WidgetClient::Instance().SetPinSubType(PinSubType::PIN_NUMBER); - WidgetClient::Instance().ReportWidgetResult(1, AuthType::PIN, 1, 1); + WidgetClient::Instance().ReportWidgetResult(1, AuthType::PIN, 1, 1, false); EXPECT_EQ(WidgetClient::Instance().GetAuthTokenId(), 0); } @@ -392,7 +392,7 @@ HWTEST_F(WidgetClientTest, WidgetClientTestReportWidgetResult_0005, TestSize.Lev EXPECT_NE(widgetCallback, nullptr); EXPECT_CALL(*widgetCallback, SendCommand); WidgetClient::Instance().SetWidgetCallback(widgetCallback); - WidgetClient::Instance().ReportWidgetResult(1, AuthType::PIN, 1, 1); + WidgetClient::Instance().ReportWidgetResult(1, AuthType::PIN, 1, 1, false); EXPECT_EQ(WidgetClient::Instance().GetAuthTokenId(), 0); } @@ -494,6 +494,34 @@ HWTEST_F(WidgetClientTest, WidgetClientTestProcessNotice_005, TestSize.Level0) EXPECT_NO_THROW(WidgetClient::Instance().ProcessNotice(widgetNotice, authTypeList)); } +HWTEST_F(WidgetClientTest, WidgetClientTestProcessNotice_006, TestSize.Level0) +{ + WidgetNotice widgetNotice; + widgetNotice.widgetContextId = 1; + widgetNotice.event = EVENT_AUTH_WIDGET_LOADED; + widgetNotice.typeList.push_back("pin"); + WidgetClient::Instance().Reset(); + std::vector authTypeList; + authTypeList.emplace_back(AuthType::PIN); + WidgetClient::Instance().SetAuthTypeList(authTypeList); + WidgetClient::Instance().SetWidgetSchedule(BuildSchedule()); + EXPECT_NO_THROW(WidgetClient::Instance().ProcessNotice(widgetNotice, authTypeList)); +} + +HWTEST_F(WidgetClientTest, WidgetClientTestProcessNotice_007, TestSize.Level0) +{ + WidgetNotice widgetNotice; + widgetNotice.widgetContextId = 1; + widgetNotice.event = EVENT_AUTH_WIDGET_RELEASED; + widgetNotice.typeList.push_back("pin"); + WidgetClient::Instance().Reset(); + std::vector authTypeList; + authTypeList.emplace_back(AuthType::PIN); + WidgetClient::Instance().SetAuthTypeList(authTypeList); + WidgetClient::Instance().SetWidgetSchedule(BuildSchedule()); + EXPECT_NO_THROW(WidgetClient::Instance().ProcessNotice(widgetNotice, authTypeList)); +} + HWTEST_F(WidgetClientTest, WidgetClientTestSetPinSubType_001, TestSize.Level0) { WidgetClient::Instance().Reset(); diff --git a/test/unittest/services/src/widget_context_test.cpp b/test/unittest/services/src/widget_context_test.cpp index 82a78c519..9ac8058ed 100644 --- a/test/unittest/services/src/widget_context_test.cpp +++ b/test/unittest/services/src/widget_context_test.cpp @@ -608,6 +608,69 @@ HWTEST_F(WidgetContextTest, WidgetContextTestOnResult, TestSize.Level0) int32_t resultCode = 2; widgetContext->OnResult(resultCode, scheduleResultAttr); } + +HWTEST_F(WidgetContextTest, WidgetContextTestIsSingleFaceOrFingerPrintAuth_001, TestSize.Level0) +{ + uint64_t contextId = 1; + ContextFactory::AuthWidgetContextPara para; + para.authTypeList.push_back(FACE); + auto widgetContext = CreateWidgetContext(contextId, para); + EXPECT_NE(widgetContext, nullptr); + EXPECT_TRUE(widgetContext->IsSingleFaceOrFingerPrintAuth()); +} + +HWTEST_F(WidgetContextTest, WidgetContextTestIsSingleFaceOrFingerPrintAuth_002, TestSize.Level0) +{ + uint64_t contextId = 1; + ContextFactory::AuthWidgetContextPara para; + para.authTypeList.push_back(FINGERPRINT); + auto widgetContext = CreateWidgetContext(contextId, para); + EXPECT_NE(widgetContext, nullptr); + EXPECT_TRUE(widgetContext->IsSingleFaceOrFingerPrintAuth()); +} + +HWTEST_F(WidgetContextTest, WidgetContextTestIsSingleFaceOrFingerPrintAuth_003, TestSize.Level0) +{ + uint64_t contextId = 1; + ContextFactory::AuthWidgetContextPara para; + para.authTypeList.push_back(FACE); + para.widgetParam.navigationButtonText = "111"; + auto widgetContext = CreateWidgetContext(contextId, para); + EXPECT_NE(widgetContext, nullptr); + EXPECT_TRUE(!widgetContext->IsSingleFaceOrFingerPrintAuth()); +} + +HWTEST_F(WidgetContextTest, WidgetContextTestIsSingleFaceOrFingerPrintAuth_004, TestSize.Level0) +{ + uint64_t contextId = 1; + ContextFactory::AuthWidgetContextPara para; + para.authTypeList.push_back(FACE); + para.authTypeList.push_back(FINGERPRINT); + auto widgetContext = CreateWidgetContext(contextId, para); + EXPECT_NE(widgetContext, nullptr); + EXPECT_TRUE(!widgetContext->IsSingleFaceOrFingerPrintAuth()); +} + +HWTEST_F(WidgetContextTest, WidgetContextTestIsNavigationAuth_001, TestSize.Level0) +{ + uint64_t contextId = 1; + ContextFactory::AuthWidgetContextPara para; + para.authTypeList.push_back(FACE); + para.widgetParam.navigationButtonText = "111"; + auto widgetContext = CreateWidgetContext(contextId, para); + EXPECT_NE(widgetContext, nullptr); + EXPECT_TRUE(widgetContext->IsNavigationAuth()); +} + +HWTEST_F(WidgetContextTest, WidgetContextTestIsNavigationAuth_002, TestSize.Level0) +{ + uint64_t contextId = 1; + ContextFactory::AuthWidgetContextPara para; + para.authTypeList.push_back(FACE); + auto widgetContext = CreateWidgetContext(contextId, para); + EXPECT_NE(widgetContext, nullptr); + EXPECT_TRUE(!widgetContext->IsNavigationAuth()); +} } // namespace UserAuth } // namespace UserIam } // namespace OHOS -- Gitee