From 5af7dd0a2d626b6527cb4c6a1c0ab498c0cf9c16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E8=9C=9C?= Date: Tue, 10 Jun 2025 19:12:50 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E4=B8=AD=E9=97=B4=E8=AE=A4?= =?UTF-8?q?=E8=AF=81=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李蜜 --- .../user_auth/inc/user_auth_callback_v10.h | 9 +- .../user_auth/inc/user_auth_instance_v10.h | 3 +- .../user_auth/inc/user_auth_napi_helper.h | 1 + .../user_auth/src/user_auth_callback_v10.cpp | 62 ++++++++++++++ .../user_auth/src/user_auth_instance_v10.cpp | 82 +++++++++++++++---- .../user_auth/src/user_auth_napi_helper.cpp | 14 ++++ .../common_defines/user_auth_common_defines.h | 2 + 7 files changed, 153 insertions(+), 20 deletions(-) diff --git a/frameworks/js/napi/user_auth/inc/user_auth_callback_v10.h b/frameworks/js/napi/user_auth/inc/user_auth_callback_v10.h index 7ed0e876f..e13a12091 100644 --- a/frameworks/js/napi/user_auth/inc/user_auth_callback_v10.h +++ b/frameworks/js/napi/user_auth/inc/user_auth_callback_v10.h @@ -43,13 +43,18 @@ public: 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 59c22965b..383e22b47 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 @@ -52,10 +52,11 @@ private: UserAuthResultCode InitUserId(napi_env env, napi_value value); UserAuthResultCode ProcessAuthTrustLevelAndUserId(napi_env env, napi_value value); UserAuthResultCode ProcessReuseUnlockResult(napi_env env, napi_value value); + UserAuthResultCode ProcessIsSkipLockedBiometricAuth(napi_env env, napi_value value); UserAuthResultCode InitAuthParam(napi_env env, napi_value value); UserAuthResultCode ProcessWindowMode(napi_env env, napi_value value); UserAuthResultCode InitWidgetParam(napi_env env, napi_value value); - std::shared_ptr GetCallback(napi_env env, napi_value value); + std::shared_ptr GetCallback(napi_env env, napi_value value, const char* propertyName); static napi_value DoGetEnrolledStateResult(napi_env env, EnrolledState enrolledState, UserAuthApiEventReporter &reporter); UserAuthResultCode ProcessContext(napi_env env, napi_value value); 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 740b3d37a..bbc710711 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/src/user_auth_callback_v10.cpp b/frameworks/js/napi/user_auth/src/user_auth_callback_v10.cpp index 997c54a65..d2d130df7 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 @@ -74,6 +74,30 @@ 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) { @@ -122,6 +146,44 @@ napi_status UserAuthCallbackV10::DoResultCallback(int32_t result, void UserAuthCallbackV10::OnAcquireInfo(int32_t module, uint32_t acquireInfo, const UserIam::UserAuth::Attributes &extraInfo) { + IAM_LOGI("SendTipInfo start"); + std::lock_guard guard(mutex_); + auto tipCallback = GetTipCallback(); + if (tipCallback == nullptr) { + return; + } + std::shared_ptr resultHolder = Common::MakeShared(); + if (resultHolder == nullptr) { + IAM_LOGE("resultHolder is null"); + return; + } + if (!extraInfo.GetInt32Value(Attributes::ATTR_AUTH_TYPE, resultHolder->authType)) { + IAM_LOGE("ATTR_AUTH_TYPE is null"); + } + const uint32_t tipType = extraInfo.GetInt32Value(Attributes::ATTR_AUTH_TYPE, resultHolder->authType); + 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 = UserAuthNapiHelper::SetInt32Property(env_, authTipInfo, "tipType", tipType); + if (ret != napi_ok) { + IAM_LOGE("napi_create_int32 failed %{public}d", ret); + return; + } + + ret = UserAuthNapiHelper::SetInt32Property(env_, authTipInfo, "tipCode", acquireInfo); + if (ret != napi_ok) { + IAM_LOGE("napi_create_int32 failed %{public}d", ret); + return; + } + ret = UserAuthNapiHelper::CallVoidNapiFunc(env_, tipCallback_->Get(), ARGS_ONE, &authTipInfo); + if (ret != napi_ok) { + IAM_LOGE("CallVoidNapiFunc failed %{public}d", ret); + return; + } + return; } void UserAuthCallbackV10::OnResult(int32_t result, const Attributes &extraInfo) 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 91cafb177..2588a6410 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 @@ -38,11 +38,13 @@ namespace OHOS { namespace UserIam { namespace UserAuth { const std::string AUTH_EVENT_RESULT = "result"; +const std::string AUTH_EVENT_AUTH_TIP = "authTip"; const std::string AUTH_PARAM_CHALLENGE = "challenge"; const std::string AUTH_PARAM_AUTHTYPE = "authType"; 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_IS_SKIP_LOCKED_BIOMETRIC_AUTH = "isSkipLockedBiometricAuth"; const std::string WIDGET_PARAM_TITLE = "title"; const std::string WIDGET_PARAM_NAVIBTNTEXT = "navigationButtonText"; const std::string WIDGET_PARAM_WINDOWMODE = "windowMode"; @@ -50,6 +52,8 @@ const std::string WIDGET_PARAM_CONTEXT = "uiContext"; const std::string NOTICETYPE = "noticeType"; const std::string REUSEMODE = "reuseMode"; const std::string REUSEDURATION = "reuseDuration"; +const char* PROPERTY_NAME_ON_RESULT = "onResult"; +const char* PROPERTY_NAME_ON_AUTH_TIP = "onAuthTip"; namespace WidgetType { constexpr int32_t TITLE_MAX = 500; @@ -136,6 +140,7 @@ UserAuthInstanceV10::UserAuthInstanceV10(napi_env env) : callback_(Common::MakeS } authParam_.authTrustLevel = AuthTrustLevel::ATL1; authParam_.userId = INVALID_USER_ID; + authParam_.isSkipLockedBiometricAuth = false; widgetParam_.navigationButtonText = ""; widgetParam_.title = ""; widgetParam_.windowMode = WindowModeType::UNKNOWN_WINDOW_MODE; @@ -313,6 +318,20 @@ UserAuthResultCode UserAuthInstanceV10::ProcessAuthTrustLevelAndUserId(napi_env return UserAuthResultCode::SUCCESS; } +UserAuthResultCode UserAuthInstanceV10::ProcessIsSkipLockedBiometricAuth(napi_env env, napi_value value) +{ + if (UserAuthNapiHelper::HasNamedProperty(env, value, AUTH_PARAM_IS_SKIP_LOCKED_BIOMETRIC_AUTH)) { + napi_status ret = UserAuthNapiHelper::GetBoolValue(env, value, authParam_.isSkipLockedBiometricAuth); + if (ret != napi_ok) { + IAM_LOGE("GetBoolValue fail:%{public}d", ret); + std::string msgStr = "Parameter error. The type of \"isSkipLockedBiometricAuth\" must be boolean."; + return UserAuthNapiHelper::ThrowErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr); + } + IAM_LOGI("Init isSkipLockedBiometricAuth: %{public}d", authParam_.isSkipLockedBiometricAuth); + } + return UserAuthResultCode::SUCCESS; +} + UserAuthResultCode UserAuthInstanceV10::InitAuthParam(napi_env env, napi_value value) { napi_status ret = UserAuthNapiHelper::CheckNapiType(env, value, napi_null); @@ -355,6 +374,11 @@ UserAuthResultCode UserAuthInstanceV10::InitAuthParam(napi_env env, napi_value v IAM_LOGE("ProcessAuthTrustLevelAndUserId fail:%{public}d", errorCode); return errorCode; } + errorCode = ProcessIsSkipLockedBiometricAuth(env, value); + if (errorCode != UserAuthResultCode::SUCCESS) { + IAM_LOGE("ProcessIsSkipLockedBiometricAuth fail:%{public}d", errorCode); + return errorCode; + } return UserAuthResultCode::SUCCESS; } @@ -542,7 +566,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) { @@ -550,7 +574,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; @@ -585,13 +609,13 @@ 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) { + auto callbackRef = GetCallback(env, argv[PARAM1], PROPERTY_NAME_ON_RESULT); + 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); + } IAM_LOGI("getAuthInstance on SetResultCallback"); if (callback_->HasResultCallback()) { IAM_LOGE("callback has been registerred"); @@ -599,6 +623,15 @@ UserAuthResultCode UserAuthInstanceV10::On(napi_env env, napi_callback_info info } callback_->SetResultCallback(callbackRef); return UserAuthResultCode::SUCCESS; + } else if (str == AUTH_EVENT_AUTH_TIP) { + auto callbackRef = GetCallback(env, argv[PARAM1], PROPERTY_NAME_ON_AUTH_TIP); + if (callbackRef == nullptr || !callbackRef->IsValid()) { + IAM_LOGE("getAuthInstance on GetCallback fail"); + std::string msgStr = "Parameter error. The type of \"callback\" must be AuthTipInfo."; + return UserAuthNapiHelper::ThrowErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr); + } + callback_->SetTipCallback(callbackRef); + return UserAuthResultCode::SUCCESS; } else { IAM_LOGE("getAuthInstance on invalid event:%{public}s", str); std::string msgStr = "Parameter error. The value of \"type\" must be \"result\"."; @@ -634,16 +667,15 @@ 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 (argc == ARGS_TWO) { + auto callbackRef = GetCallback(env, argv[PARAM1], PROPERTY_NAME_ON_RESULT); + 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 (!callback_->HasResultCallback()) { IAM_LOGE("no callback registerred yet"); return UserAuthResultCode::GENERAL_ERROR; @@ -651,6 +683,22 @@ UserAuthResultCode UserAuthInstanceV10::Off(napi_env env, napi_callback_info inf callback_->ClearResultCallback(); IAM_LOGI("UserAuthResultCode off clear result callback"); return UserAuthResultCode::SUCCESS; + } else if (str == AUTH_EVENT_AUTH_TIP) { + if (argc == ARGS_TWO) { + auto callbackRef = GetCallback(env, argv[PARAM1], PROPERTY_NAME_ON_AUTH_TIP); + if (callbackRef == nullptr || !callbackRef->IsValid()) { + IAM_LOGE("GetCallback fail"); + std::string msgStr = "Parameter error. The type of \"callback\" must be AuthTypeInfo."; + return UserAuthNapiHelper::ThrowErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr); + } + } + 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; } 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 e20742303..1473a98eb 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 @@ -256,6 +256,20 @@ napi_status UserAuthNapiHelper::CheckNapiType(napi_env env, napi_value value, na return napi_ok; } +napi_status UserAuthNapiHelper::GetBoolValue(napi_env env, napi_value value, bool &out) +{ + napi_status result = CheckNapiType(env, value, napi_boolean); + if (result != napi_ok) { + IAM_LOGE("CheckNapiType fail"); + return result; + } + result = napi_get_value_bool(env, value, &out); + if (result != napi_ok) { + IAM_LOGE("napi_get_value_bool fail"); + } + return result; +} + napi_status UserAuthNapiHelper::GetInt32Value(napi_env env, napi_value value, int32_t &out) { napi_status result = CheckNapiType(env, value, napi_number); 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..be15ae54a 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; + /** Is skip locked biometric authentication */ + bool isSkipLockedBiometricAuth; }; /** -- Gitee