From 2fd731da007c590cefffbd85d84c9f775f20f553 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9F=A9=E9=9C=87?= Date: Thu, 27 Jan 2022 10:16:42 +0800 Subject: [PATCH] fix bug MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 韩震 --- interfaces/innerkits/napi/BUILD.gn | 5 +- .../innerkits/napi/include/auth_common.h | 16 +- .../innerkits/napi/include/authapi_callback.h | 23 +- .../innerkits/napi/include/user_auth_impl.h | 20 +- .../innerkits/napi/src/authapi_callback.cpp | 456 +++++++++++++----- .../innerkits/napi/src/user_auth_impl.cpp | 263 ++++++---- .../innerkits/native/include/user_auth.h | 6 +- .../innerkits/native/include/userauth_info.h | 44 +- services/include/userauth_adapter.h | 6 +- services/include/userauth_async_proxy.h | 4 +- services/src/userauth_adapter.cpp | 9 +- services/src/userauth_async_proxy.cpp | 53 +- services/src/userauth_excallback_impl.cpp | 2 +- 13 files changed, 590 insertions(+), 317 deletions(-) diff --git a/interfaces/innerkits/napi/BUILD.gn b/interfaces/innerkits/napi/BUILD.gn index d41f5f669..66db8e7cc 100755 --- a/interfaces/innerkits/napi/BUILD.gn +++ b/interfaces/innerkits/napi/BUILD.gn @@ -15,7 +15,10 @@ import("//base/user_iam/user_auth/userauth.gni") import("//build/ohos.gni") config("userauth_public_config") { - include_dirs = [ "include" ] + include_dirs = [ + "include", + "//third_party/libuv/include", + ] } ohos_shared_library("userauth") { diff --git a/interfaces/innerkits/napi/include/auth_common.h b/interfaces/innerkits/napi/include/auth_common.h index b46418430..dcecddd60 100755 --- a/interfaces/innerkits/napi/include/auth_common.h +++ b/interfaces/innerkits/napi/include/auth_common.h @@ -58,12 +58,15 @@ struct CallBackInfo { }; struct GetPropertyInfo { - GetPropertyInfo() : asyncWork(nullptr), result(nullptr) {}; + GetPropertyInfo() : result(nullptr) {}; CallBackInfo callBackInfo; - napi_async_work asyncWork; napi_value result; int32_t authType; std::vector keys; + int32_t getResult; + uint64_t authSubType; + uint32_t remainTimes; + uint32_t freezingTime; }; struct SetPropertyInfo { @@ -74,6 +77,7 @@ struct SetPropertyInfo { int32_t authType; int32_t key; std::vector setInfo; + int32_t setResult; }; struct AuthInfo { @@ -91,6 +95,10 @@ struct AuthInfo { uint64_t challenge; int32_t authType; int32_t authTrustLevel; + int32_t result; + std::vector token; + uint32_t remainTimes; + uint32_t freezingTime; }; struct AuthUserInfo { @@ -109,6 +117,10 @@ struct AuthUserInfo { uint64_t challenge; int32_t authType; int32_t authTrustLevel; + int32_t result; + std::vector token; + uint32_t remainTimes; + uint32_t freezingTime; }; } // namespace UserAuth } // namespace UserIAM diff --git a/interfaces/innerkits/napi/include/authapi_callback.h b/interfaces/innerkits/napi/include/authapi_callback.h index 51ff125a8..87fbf6de1 100755 --- a/interfaces/innerkits/napi/include/authapi_callback.h +++ b/interfaces/innerkits/napi/include/authapi_callback.h @@ -27,6 +27,14 @@ namespace OHOS { namespace UserIAM { namespace UserAuth { +typedef struct AcquireInfoInner { + napi_env env; + napi_ref onAcquireInfo; + napi_value jsFunction; + int32_t module; + uint32_t acquireInfo; + int32_t extraInfo; +} AcquireInfoInner; class AuthApiCallback : public UserAuthCallback { public: AuthApiCallback(); @@ -40,12 +48,17 @@ public: void onResult(const int32_t result, const AuthResult extraInfo) override; void onSetExecutorProperty(const int32_t result) override; -private: - napi_value BuildExecutorProperty( + static napi_value BuildExecutorProperty( napi_env env, int32_t result, uint32_t remainTimes, uint32_t freezingTime, uint64_t authSubType); - napi_value Uint64ToNapi(napi_env env, uint64_t value); - napi_value BuildOnResult(napi_env env, uint32_t remainTimes, uint32_t freezingTime, std::vector token); - napi_value Uint8ArrayToNapi(napi_env env, std::vector value); + static napi_value Uint64ToNapi(napi_env env, uint64_t value); + static napi_value BuildOnResult( + napi_env env, uint32_t remainTimes, uint32_t freezingTime, std::vector token); + static napi_value Uint8ArrayToNapi(napi_env env, std::vector value); + +private: + void OnAuthAcquireInfo(AcquireInfoInner *acquireInfoInner); + void OnUserAuthResult(const int32_t result, const AuthResult extraInfo); + void OnAuthResult(const int32_t result, const AuthResult extraInfo); }; } // namespace UserAuth } // namespace UserIAM diff --git a/interfaces/innerkits/napi/include/user_auth_impl.h b/interfaces/innerkits/napi/include/user_auth_impl.h index 80b469cd2..877dececd 100755 --- a/interfaces/innerkits/napi/include/user_auth_impl.h +++ b/interfaces/innerkits/napi/include/user_auth_impl.h @@ -23,6 +23,12 @@ namespace OHOS { namespace UserIAM { namespace UserAuth { +typedef struct AsyncHolder { + AsyncHolder() : data(nullptr), asyncWork(nullptr) {}; + void *data; + napi_async_work asyncWork; +} AsyncHolder; + class UserAuthImpl { public: UserAuthImpl(); @@ -37,16 +43,18 @@ public: napi_value CancelAuth(napi_env env, napi_callback_info info); private: - napi_value GetPropertyWrap(napi_env env, napi_callback_info info, GetPropertyInfo *getPropertyInfo); - napi_value GetPropertyAsync(napi_env env, GetPropertyInfo *getPropertyInfo); - napi_value GetPropertyPromise(napi_env env, GetPropertyInfo *getPropertyInfo); + napi_value GetPropertyWrap(napi_env env, napi_callback_info info, AsyncHolder *asyncHolder); + napi_value GetPropertyAsync(napi_env env, AsyncHolder *asyncHolder); + napi_value GetPropertyPromise(napi_env env, AsyncHolder *asyncHolder); - napi_value SetPropertyWrap(napi_env env, napi_callback_info info, SetPropertyInfo *setPropertyInfo); - napi_value SetPropertyAsync(napi_env env, SetPropertyInfo *setPropertyInfo); - napi_value SetPropertyPromise(napi_env env, SetPropertyInfo *setPropertyInfo); + napi_value SetPropertyWrap(napi_env env, napi_callback_info info, AsyncHolder *asyncHolder); + napi_value SetPropertyAsync(napi_env env, AsyncHolder *asyncHolder); + napi_value SetPropertyPromise(napi_env env, AsyncHolder *asyncHolder); napi_value AuthWrap(napi_env env, AuthInfo *authInfo); + napi_value BuildAuthInfo(napi_env env, AuthInfo *authInfo); napi_value AuthUserWrap(napi_env env, AuthUserInfo *userInfo); + napi_value BuildAuthUserInfo(napi_env env, AuthUserInfo *userInfo); static void SetPropertyExecute(napi_env env, void *data); static void SetPropertyPromiseExecuteDone(napi_env env, napi_status status, void *data); diff --git a/interfaces/innerkits/napi/src/authapi_callback.cpp b/interfaces/innerkits/napi/src/authapi_callback.cpp index 4e6b4741a..7abfba6b2 100755 --- a/interfaces/innerkits/napi/src/authapi_callback.cpp +++ b/interfaces/innerkits/napi/src/authapi_callback.cpp @@ -15,6 +15,7 @@ #include "authapi_callback.h" +#include #include "securec.h" #include "auth_hilog_wrapper.h" @@ -48,8 +49,9 @@ napi_value AuthApiCallback::BuildExecutorProperty( NAPI_CALL(env, napi_create_uint32(env, freezingTime, &freezingTimeValue)); NAPI_CALL(env, napi_set_named_property(env, jsObject, "freezingTime", freezingTimeValue)); - napi_value jsType = Uint64ToNapi(env, authSubType); - NAPI_CALL(env, napi_set_named_property(env, jsObject, "authSubType", jsType)); + napi_value authSubTypeValue = nullptr; + NAPI_CALL(env, napi_create_uint32(env, static_cast(authSubType), &authSubTypeValue)); + NAPI_CALL(env, napi_set_named_property(env, jsObject, "authSubType", authSubTypeValue)); return jsObject; } @@ -100,187 +102,381 @@ napi_value AuthApiCallback::BuildOnResult( return jsObject; } +static void GetPropertyInfoCallback(uv_work_t* work, int status) +{ + HILOG_INFO("Do OnAuthAcquireInfo work"); + GetPropertyInfo *getPropertyInfo = reinterpret_cast(work->data); + if (getPropertyInfo == nullptr) { + HILOG_ERROR("getPropertyInfo is null"); + delete work; + return; + } + napi_env env = getPropertyInfo->callBackInfo.env; + napi_value resultData[PARAM1]; + resultData[PARAM0] = AuthApiCallback::BuildExecutorProperty(env, getPropertyInfo->getResult, + getPropertyInfo->remainTimes, getPropertyInfo->freezingTime, getPropertyInfo->authSubType); + if (getPropertyInfo->callBackInfo.callBack != nullptr) { + HILOG_INFO("onExecutorPropertyInfo async"); + napi_value global = nullptr; + napi_status napiStatus = napi_get_global(env, &global); + if (napiStatus != napi_ok) { + HILOG_INFO("napi_get_global faild "); + goto EXIT; + } + napi_value resultValue = nullptr; + napi_value callBack = nullptr; + napiStatus = napi_get_reference_value(env, getPropertyInfo->callBackInfo.callBack, &callBack); + if (napiStatus != napi_ok) { + HILOG_INFO("napi_get_reference_value faild "); + goto EXIT; + } + napiStatus = napi_call_function(env, global, callBack, PARAM1, resultData, &resultValue); + if (napiStatus != napi_ok) { + HILOG_INFO("napi_call_function faild "); + goto EXIT; + } + } else { + HILOG_INFO("onExecutorPropertyInfo promise"); + napi_value resultValue = resultData[PARAM0]; + napi_deferred deferred = getPropertyInfo->callBackInfo.deferred; + napi_status napiStatus = napi_resolve_deferred(env, deferred, resultValue); + if (napiStatus != napi_ok) { + HILOG_INFO("napi_resolve_deferred faild "); + goto EXIT; + } + } +EXIT: + delete getPropertyInfo; + delete work; +} + void AuthApiCallback::onExecutorPropertyInfo(const ExecutorProperty result) { - napi_status status; HILOG_INFO("AuthApiCallback onExecutorPropertyInfo 1 = %{public}d", result.result); HILOG_INFO("AuthApiCallback onExecutorPropertyInfo 2 = %{public}llu", result.authSubType); HILOG_INFO("AuthApiCallback onExecutorPropertyInfo 3 = %{public}u", result.remainTimes); HILOG_INFO("AuthApiCallback onExecutorPropertyInfo 4 = %{public}u", result.freezingTime); - if (getPropertyInfo_ != nullptr) { - napi_env env = getPropertyInfo_->callBackInfo.env; - HILOG_INFO("AuthApiCallback onExecutorPropertyInfo 5 "); - napi_value resultData[PARAM1]; - resultData[PARAM0] = BuildExecutorProperty( - env, result.result, result.remainTimes, result.freezingTime, static_cast(result.authSubType)); - if (getPropertyInfo_->callBackInfo.callBack != nullptr) { - HILOG_INFO("AuthApiCallback onExecutorPropertyInfo async 6"); - napi_value global = nullptr; - status = napi_get_global(env, &global); - if (status != napi_ok) { - HILOG_INFO("napi_get_global faild "); - } - napi_value resultValue = nullptr; - napi_value callBack = nullptr; - status = napi_get_reference_value(env, getPropertyInfo_->callBackInfo.callBack, &callBack); - if (status != napi_ok) { - HILOG_INFO("napi_get_reference_value faild "); - } - status = napi_call_function(env, global, callBack, PARAM1, resultData, &resultValue); - if (status != napi_ok) { - HILOG_INFO("napi_call_function faild "); - } - } else { - HILOG_INFO("AuthApiCallback onExecutorPropertyInfo promise 6"); - napi_value resultValue = resultData[PARAM0]; - napi_deferred deferred = getPropertyInfo_->callBackInfo.deferred; - status = napi_resolve_deferred(env, deferred, resultValue); - if (status != napi_ok) { - HILOG_INFO("napi_resolve_deferred faild "); - } - } + if (getPropertyInfo_ == nullptr) { + HILOG_ERROR("AuthApiCallback onExecutorPropertyInfo getPropertyInfo_ is nullptr"); + return; + } + uv_loop_s *loop(nullptr); + napi_get_uv_event_loop(getPropertyInfo_->callBackInfo.env, &loop); + if (loop == nullptr) { + HILOG_ERROR("loop is null"); delete getPropertyInfo_; getPropertyInfo_ = nullptr; - } else { - HILOG_ERROR("AuthApiCallback onExecutorPropertyInfo getPropertyInfo_ is nullptr"); + return; + } + uv_work_t *work = new (std::nothrow) uv_work_t; + if (work == nullptr) { + HILOG_ERROR("work is null"); + delete getPropertyInfo_; + getPropertyInfo_ = nullptr; + return; } + getPropertyInfo_->getResult = result.result; + getPropertyInfo_->authSubType = static_cast(result.authSubType); + getPropertyInfo_->remainTimes = result.remainTimes; + getPropertyInfo_->freezingTime = result.freezingTime; + work->data = reinterpret_cast(getPropertyInfo_); + getPropertyInfo_ = nullptr; + HILOG_INFO("Before GetPropertyInfoCallback"); + uv_queue_work(loop, work, [] (uv_work_t *work) {}, GetPropertyInfoCallback); } -void AuthApiCallback::onAcquireInfo(const int32_t module, const uint32_t acquireInfo, const int32_t extraInfo) +void AuthApiCallback::OnAuthAcquireInfo(AcquireInfoInner *acquireInfoInner) { - napi_status status; - napi_value callback; - napi_value params[PARAM3]; - if (userInfo_ != nullptr) { - napi_env env = userInfo_->callBackInfo.env; - HILOG_INFO("AuthApiCallback onAcquireInfo userInfo_ start 1"); + HILOG_INFO("AuthApiCallback OnAuthAcquireInfo start"); + uv_loop_s *loop(nullptr); + napi_get_uv_event_loop(authInfo_->callBackInfo.env, &loop); + if (loop == nullptr) { + HILOG_ERROR("loop is null"); + delete acquireInfoInner; + return; + } + uv_work_t *work = new (std::nothrow) uv_work_t; + if (work == nullptr) { + HILOG_ERROR("work is null"); + delete acquireInfoInner; + return; + } + work->data = reinterpret_cast(acquireInfoInner); + uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) { + HILOG_INFO("Do OnAuthAcquireInfo work"); + AcquireInfoInner *acquireInfoInner = reinterpret_cast(work->data); + if (acquireInfoInner == nullptr) { + HILOG_ERROR("authInfo is null"); + delete work; + return; + } + napi_env env = acquireInfoInner->env; napi_value returnOnAcquire = nullptr; - status = napi_get_reference_value(env, userInfo_->onAcquireInfo, &callback); - if (status != napi_ok) { + napi_value callback; + napi_status napiStatus = napi_get_reference_value(env, acquireInfoInner->onAcquireInfo, &callback); + if (napiStatus != napi_ok) { HILOG_INFO("napi_get_reference_value faild "); + delete acquireInfoInner; + delete work; + return; } - napi_create_int32(env, module, ¶ms[PARAM0]); - napi_create_uint32(env, acquireInfo, ¶ms[PARAM1]); - napi_create_int32(env, extraInfo, ¶ms[PARAM2]); - status = napi_call_function(env, userInfo_->jsFunction, callback, PARAM3, params, &returnOnAcquire); - if (status != napi_ok) { + napi_value params[PARAM3]; + napi_create_int32(env, acquireInfoInner->module, ¶ms[PARAM0]); + napi_create_uint32(env, acquireInfoInner->acquireInfo, ¶ms[PARAM1]); + napi_create_int32(env, acquireInfoInner->extraInfo, ¶ms[PARAM2]); + napiStatus = napi_call_function(env, acquireInfoInner->jsFunction, callback, PARAM3, params, &returnOnAcquire); + if (napiStatus != napi_ok) { HILOG_ERROR("napi_call_function faild"); } - HILOG_INFO("AuthApiCallback onAcquireInfo userInfo_ start 4"); + delete acquireInfoInner; + delete work; + }); +} + +void AuthApiCallback::onAcquireInfo(const int32_t module, const uint32_t acquireInfo, const int32_t extraInfo) +{ + if (userInfo_ != nullptr) { + AcquireInfoInner *acquireInfoInner = new (std::nothrow) AcquireInfoInner(); + if (acquireInfoInner == nullptr) { + HILOG_ERROR("acquireInfoInner is null"); + return; + } + acquireInfoInner->env = userInfo_->callBackInfo.env; + acquireInfoInner->onAcquireInfo = userInfo_->onAcquireInfo; + acquireInfoInner->jsFunction = userInfo_->jsFunction; + acquireInfoInner->module = module; + acquireInfoInner->acquireInfo = acquireInfo; + acquireInfoInner->extraInfo = extraInfo; + OnAuthAcquireInfo(acquireInfoInner); } else { HILOG_INFO("AuthApiCallback onAcquireInfo userInfo_ is nullptr "); } if (authInfo_ != nullptr) { - napi_env env = authInfo_->callBackInfo.env; - napi_value returnOnAcquire = nullptr; - status = napi_get_reference_value(env, authInfo_->onAcquireInfo, &callback); - if (status != napi_ok) { - HILOG_INFO("napi_get_reference_value faild "); - } - napi_create_int32(env, module, ¶ms[PARAM0]); - napi_create_uint32(env, acquireInfo, ¶ms[PARAM1]); - napi_create_int32(env, extraInfo, ¶ms[PARAM2]); - status = napi_call_function(env, authInfo_->jsFunction, callback, PARAM3, params, &returnOnAcquire); - if (status != napi_ok) { - HILOG_ERROR("napi_call_function faild"); + AcquireInfoInner *acquireInfoInner = new (std::nothrow) AcquireInfoInner(); + if (acquireInfoInner == nullptr) { + HILOG_ERROR("acquireInfoInner is null"); + return; } - HILOG_INFO("AuthApiCallback onAcquireInfo authInfo_ start 4"); + acquireInfoInner->env = authInfo_->callBackInfo.env; + acquireInfoInner->onAcquireInfo = authInfo_->onAcquireInfo; + acquireInfoInner->jsFunction = authInfo_->jsFunction; + acquireInfoInner->module = module; + acquireInfoInner->acquireInfo = acquireInfo; + acquireInfoInner->extraInfo = extraInfo; + OnAuthAcquireInfo(acquireInfoInner); } else { HILOG_INFO("AuthApiCallback onAcquireInfo authInfo_ is nullptr "); } HILOG_INFO("AuthApiCallback onAcquireInfo end"); } +static void OnUserAuthResultWork(uv_work_t *work, int status) +{ + HILOG_INFO("Do OnUserAuthResult work"); + AuthUserInfo *userInfo = reinterpret_cast(work->data); + if (userInfo == nullptr) { + HILOG_ERROR("authInfo is null"); + delete work; + return; + } + napi_env env = userInfo->callBackInfo.env; + napi_value callback; + napi_status napiStatus = napi_get_reference_value(env, userInfo->onResult, &callback); + if (napiStatus != napi_ok) { + HILOG_INFO("napi_get_reference_value faild "); + delete userInfo; + delete work; + return; + } + napi_value params[PARAM2]; + napi_create_int32(env, userInfo->result, ¶ms[PARAM0]); + params[PARAM1] = AuthApiCallback::BuildOnResult( + env, userInfo->remainTimes, userInfo->freezingTime, userInfo->token); + napi_value return_val = nullptr; + napi_call_function(env, userInfo->jsFunction, callback, PARAM2, params, &return_val); + delete userInfo; + delete work; +} + +void AuthApiCallback::OnUserAuthResult(const int32_t result, const AuthResult extraInfo) +{ + HILOG_INFO("AuthApiCallback OnUserAuthResult start"); + uv_loop_s *loop(nullptr); + napi_get_uv_event_loop(userInfo_->callBackInfo.env, &loop); + if (loop == nullptr) { + HILOG_ERROR("loop is null"); + delete userInfo_; + userInfo_ = nullptr; + return; + } + uv_work_t *work = new (std::nothrow) uv_work_t; + if (work == nullptr) { + HILOG_ERROR("work is null"); + delete userInfo_; + userInfo_ = nullptr; + return; + } + userInfo_->result = result; + userInfo_->token = extraInfo.token; + userInfo_->freezingTime = extraInfo.freezingTime; + userInfo_->remainTimes = extraInfo.remainTimes; + work->data = reinterpret_cast(userInfo_); + userInfo_ = nullptr; + uv_queue_work(loop, work, [] (uv_work_t *work) {}, OnUserAuthResultWork); +} + +static void OnAuthResultWork(uv_work_t *work, int status) +{ + HILOG_INFO("Do OnAuthResult work"); + AuthInfo *authInfo = reinterpret_cast(work->data); + if (authInfo == nullptr) { + HILOG_ERROR("authInfo is null"); + delete work; + return; + } + napi_env env = authInfo->callBackInfo.env; + napi_value callback; + napi_status napiStatus = napi_get_reference_value(env, authInfo->onResult, &callback); + if (napiStatus != napi_ok) { + HILOG_INFO("napi_get_reference_value faild "); + delete authInfo; + delete work; + return; + } + napi_value params[PARAM2]; + napi_create_int32(env, authInfo->result, ¶ms[PARAM0]); + params[PARAM1] = AuthApiCallback::BuildOnResult( + env, authInfo->remainTimes, authInfo->freezingTime, authInfo->token); + napi_value return_val = nullptr; + napi_call_function(env, authInfo->jsFunction, callback, PARAM2, params, &return_val); + delete authInfo; + delete work; +} + +void AuthApiCallback::OnAuthResult(const int32_t result, const AuthResult extraInfo) +{ + HILOG_INFO("AuthApiCallback OnAuthResult start"); + uv_loop_s *loop(nullptr); + napi_get_uv_event_loop(authInfo_->callBackInfo.env, &loop); + if (loop == nullptr) { + HILOG_ERROR("loop is null"); + delete authInfo_; + authInfo_ = nullptr; + return; + } + uv_work_t *work = new (std::nothrow) uv_work_t; + if (work == nullptr) { + HILOG_ERROR("work is null"); + delete authInfo_; + authInfo_ = nullptr; + return; + } + authInfo_->result = result; + authInfo_->token = extraInfo.token; + authInfo_->freezingTime = extraInfo.freezingTime; + authInfo_->remainTimes = extraInfo.remainTimes; + work->data = reinterpret_cast(authInfo_); + authInfo_ = nullptr; + uv_queue_work(loop, work, [] (uv_work_t *work) {}, OnAuthResultWork); +} + void AuthApiCallback::onResult(const int32_t result, const AuthResult extraInfo) { HILOG_INFO("AuthApiCallback onResult start result = %{public}d", result); HILOG_INFO("AuthApiCallback onResult start token.length = %{public}d", extraInfo.token.size()); HILOG_INFO("AuthApiCallback onResult start extraInfo.remainTimes = %{public}u", extraInfo.remainTimes); HILOG_INFO("AuthApiCallback onResult start extraInfo.freezingTime = %{public}u", extraInfo.freezingTime); - napi_status status; - napi_value callback; if (userInfo_ != nullptr) { - napi_env env = userInfo_->callBackInfo.env; - status = napi_get_reference_value(env, userInfo_->onResult, &callback); - if (status != napi_ok) { - HILOG_INFO("napi_get_reference_value faild "); - } - napi_value params[PARAM2]; - napi_create_int32(env, result, ¶ms[PARAM0]); - params[PARAM1] = BuildOnResult(env, extraInfo.remainTimes, extraInfo.freezingTime, extraInfo.token); - napi_value return_val = nullptr; - HILOG_INFO("AuthApiCallback onResult userInfo_ 5"); - napi_call_function(env, userInfo_->jsFunction, callback, PARAM2, params, &return_val); - delete userInfo_; - userInfo_ = nullptr; + OnUserAuthResult(result, extraInfo); } else { HILOG_ERROR("AuthApiCallback onResult userInfo_ is nullptr "); } if (authInfo_ != nullptr) { - HILOG_INFO("AuthApiCallback onResult authInfo_ 1"); - napi_env env = authInfo_->callBackInfo.env; - HILOG_INFO("AuthApiCallback onResult authInfo_ 2"); - status = napi_get_reference_value(authInfo_->callBackInfo.env, authInfo_->onResult, &callback); - if (status != napi_ok) { - HILOG_INFO("napi_get_reference_value faild "); - } - napi_value params[PARAM2]; - napi_create_int32(env, result, ¶ms[PARAM0]); - params[PARAM1] = BuildOnResult(env, extraInfo.remainTimes, extraInfo.freezingTime, extraInfo.token); - napi_value return_val = nullptr; - HILOG_INFO("AuthApiCallback onResult userInfo_ 5"); - napi_call_function(env, authInfo_->jsFunction, callback, PARAM2, params, &return_val); - HILOG_INFO("AuthApiCallback onResult authInfo_ 6"); - delete authInfo_; - authInfo_ = nullptr; + OnAuthResult(result, extraInfo); } else { HILOG_ERROR("AuthApiCallback onResult authInfo_ is nullptr "); } HILOG_INFO("AuthApiCallback onResult end"); } +static void SetExecutorPropertyCallback(uv_work_t *work, int status) +{ + HILOG_INFO("Do SetExecutorPropertyCallback work"); + SetPropertyInfo *setPropertyInfo = reinterpret_cast(work->data); + if (setPropertyInfo == nullptr) { + HILOG_ERROR("authInfo is null"); + delete work; + return; + } + napi_env env = setPropertyInfo->callBackInfo.env; + napi_status napiStatus = napi_create_int32(env, setPropertyInfo->setResult, &setPropertyInfo->result); + if (napiStatus != napi_ok) { + HILOG_ERROR("napi_create_int32 faild"); + goto EXIT; + } + if (setPropertyInfo->callBackInfo.callBack != nullptr) { + napi_value global = nullptr; + napiStatus = napi_get_global(env, &global); + if (napiStatus != napi_ok) { + HILOG_ERROR("napi_get_global faild"); + goto EXIT; + } + napi_value resultData[PARAM1]; + resultData[PARAM0] = setPropertyInfo->result; + setPropertyInfo->result = nullptr; + napi_value result = nullptr; + napi_value callBack = nullptr; + napiStatus = napi_get_reference_value(env, setPropertyInfo->callBackInfo.callBack, &callBack); + if (napiStatus != napi_ok) { + HILOG_ERROR("napi_get_reference_value faild"); + goto EXIT; + } + napiStatus = napi_call_function(env, global, callBack, PARAM1, resultData, &result); + if (napiStatus != napi_ok) { + HILOG_ERROR("napi_call_function faild"); + goto EXIT; + } + } else { + napi_value result = setPropertyInfo->result; + napi_deferred deferred = setPropertyInfo->callBackInfo.deferred; + napiStatus = napi_resolve_deferred(env, deferred, result); + if (napiStatus != napi_ok) { + HILOG_ERROR("napi_call_function faild"); + goto EXIT; + } + } +EXIT: + delete setPropertyInfo; + delete work; +} + void AuthApiCallback::onSetExecutorProperty(const int32_t result) { HILOG_INFO("onSetExecutorProperty start = %{public}d", result); - napi_status status; if (setPropertyInfo_ != nullptr) { - napi_env env = setPropertyInfo_->callBackInfo.env; - status = napi_create_int32(env, result, &setPropertyInfo_->result); - if (status != napi_ok) { - HILOG_ERROR("napi_create_int32 faild"); - } - if (setPropertyInfo_->callBackInfo.callBack != nullptr) { - napi_value global = nullptr; - status = napi_get_global(env, &global); - if (status != napi_ok) { - HILOG_ERROR("napi_get_global faild"); - } - napi_value resultData[PARAM1]; - resultData[PARAM0] = setPropertyInfo_->result; - setPropertyInfo_->result = nullptr; - napi_value result = nullptr; - napi_value callBack = nullptr; - status = napi_get_reference_value(env, setPropertyInfo_->callBackInfo.callBack, &callBack); - if (status != napi_ok) { - HILOG_ERROR("napi_get_reference_value faild"); - } - status = napi_call_function(env, global, callBack, PARAM1, resultData, &result); - if (status != napi_ok) { - HILOG_ERROR("napi_call_function faild"); - } - } else { - napi_value result = setPropertyInfo_->result; - napi_deferred deferred = setPropertyInfo_->callBackInfo.deferred; - status = napi_resolve_deferred(env, deferred, result); - if (status != napi_ok) { - HILOG_ERROR("napi_call_function faild"); - } - } + HILOG_ERROR("setPropertyInfo is null"); + return; + } + uv_loop_s *loop(nullptr); + napi_get_uv_event_loop(setPropertyInfo_->callBackInfo.env, &loop); + if (loop == nullptr) { + HILOG_ERROR("loop is null"); + delete setPropertyInfo_; + setPropertyInfo_ = nullptr; + return; + } + uv_work_t *work = new (std::nothrow) uv_work_t; + if (work == nullptr) { + HILOG_ERROR("work is null"); delete setPropertyInfo_; setPropertyInfo_ = nullptr; + return; } - HILOG_INFO("onSetExecutorProperty end"); + setPropertyInfo_->setResult = result; + work->data = reinterpret_cast(setPropertyInfo_); + setPropertyInfo_ = nullptr; + uv_queue_work(loop, work, [] (uv_work_t *work) {}, SetExecutorPropertyCallback); } } // namespace UserAuth } // namespace UserIAM diff --git a/interfaces/innerkits/napi/src/user_auth_impl.cpp b/interfaces/innerkits/napi/src/user_auth_impl.cpp index ceeb649a9..50bbc87a7 100755 --- a/interfaces/innerkits/napi/src/user_auth_impl.cpp +++ b/interfaces/innerkits/napi/src/user_auth_impl.cpp @@ -72,14 +72,35 @@ napi_value UserAuthImpl::GetAvailabeStatus(napi_env env, napi_callback_info info napi_value UserAuthImpl::GetProperty(napi_env env, napi_callback_info info) { + AsyncHolder *asyncHolder = new (std::nothrow) AsyncHolder(); + if (asyncHolder == nullptr) { + HILOG_ERROR("%{public}s asyncHolder nullptr", __func__); + return nullptr; + } GetPropertyInfo *getPropertyInfo = new (std::nothrow) GetPropertyInfo(); + if (getPropertyInfo == nullptr) { + delete asyncHolder; + HILOG_ERROR("%{public}s getPropertyInfo nullptr", __func__); + return nullptr; + } getPropertyInfo->callBackInfo.env = env; - return GetPropertyWrap(env, info, getPropertyInfo); + asyncHolder->data = getPropertyInfo; + napi_value ret = GetPropertyWrap(env, info, asyncHolder); + if (ret == nullptr) { + HILOG_ERROR("%{public}s GetPropertyWrap fail", __func__); + delete getPropertyInfo; + delete asyncHolder; + if (asyncHolder->asyncWork != nullptr) { + napi_delete_async_work(env, asyncHolder->asyncWork); + } + } + return ret; } -napi_value UserAuthImpl::GetPropertyWrap(napi_env env, napi_callback_info info, GetPropertyInfo *getPropertyInfo) +napi_value UserAuthImpl::GetPropertyWrap(napi_env env, napi_callback_info info, AsyncHolder *asyncHolder) { HILOG_INFO("%{public}s, called", __func__); + GetPropertyInfo *getPropertyInfo = reinterpret_cast(asyncHolder->data); size_t argcAsync = ARGS_TWO; const size_t argcPromise = ARGS_ONE; const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; @@ -96,18 +117,16 @@ napi_value UserAuthImpl::GetPropertyWrap(napi_env env, napi_callback_info info, NAPI_CALL(env, napi_create_reference(env, args[PARAM1], 1, &(getPropertyInfo->callBackInfo.callBack))); } } - if (authBuild.NapiTypeObject(env, args[PARAM0])) { Napi_GetPropertyRequest request = authBuild.GetPropertyRequestBuild(env, args[0]); getPropertyInfo->authType = request.authType_; getPropertyInfo->keys = request.keys_; } - - napi_value ret = 0; + napi_value ret = nullptr; if (argcAsync > argcPromise) { - ret = GetPropertyAsync(env, getPropertyInfo); + ret = GetPropertyAsync(env, asyncHolder); } else { - ret = GetPropertyPromise(env, getPropertyInfo); + ret = GetPropertyPromise(env, asyncHolder); } HILOG_INFO("%{public}s,end.", __func__); return ret; @@ -116,66 +135,66 @@ napi_value UserAuthImpl::GetPropertyWrap(napi_env env, napi_callback_info info, void UserAuthImpl::GetPropertyExecute(napi_env env, void *data) { HILOG_INFO("GetPropertyExecute, worker pool thread execute."); - GetPropertyInfo *getPropertyInfo = static_cast(data); - if (getPropertyInfo != nullptr) { - AuthType authTypeGet = AuthType(getPropertyInfo->authType); - - GetPropertyRequest request; - request.authType = authTypeGet; - request.keys = getPropertyInfo->keys; - HILOG_INFO("GetPropertyExecute start 1"); - AuthApiCallback *object = new AuthApiCallback(); - object->getPropertyInfo_ = getPropertyInfo; - std::shared_ptr callback; - callback.reset(object); - UserAuth::GetInstance().GetProperty(request, callback); - } else { - HILOG_ERROR("GetPropertyExecute, asynccallBackInfo == nullptr"); + AsyncHolder *asyncHolder = reinterpret_cast(data); + if (asyncHolder == nullptr) { + HILOG_ERROR("GetPropertyExecute, asyncHolder == nullptr"); + return; + } + GetPropertyInfo *getPropertyInfo = reinterpret_cast(asyncHolder->data); + if (getPropertyInfo == nullptr) { + HILOG_ERROR("GetPropertyExecute, getPropertyInfo == nullptr"); + return; } + AuthType authTypeGet = AuthType(getPropertyInfo->authType); + + GetPropertyRequest request; + request.authType = authTypeGet; + request.keys = getPropertyInfo->keys; + HILOG_INFO("GetPropertyExecute start 1"); + AuthApiCallback *object = new AuthApiCallback(); + object->getPropertyInfo_ = getPropertyInfo; + std::shared_ptr callback; + callback.reset(object); + UserAuth::GetInstance().GetProperty(request, callback); HILOG_INFO("GetPropertyExecute, worker pool thread execute end."); } void UserAuthImpl::GetPropertyPromiseExecuteDone(napi_env env, napi_status status, void *data) { HILOG_INFO("GetPropertyPromiseExecuteDone, start"); - GetPropertyInfo *getPropertyInfo = static_cast(data); - napi_delete_async_work(env, getPropertyInfo->asyncWork); + AsyncHolder *asyncHolder = reinterpret_cast(data); + napi_delete_async_work(env, asyncHolder->asyncWork); + delete asyncHolder; HILOG_INFO("GetPropertyPromiseExecuteDone, end"); } void UserAuthImpl::GetPropertyAsyncExecuteDone(napi_env env, napi_status status, void *data) { HILOG_INFO("GetPropertyAsyncExecuteDone, start"); - GetPropertyInfo *getPropertyInfo = static_cast(data); - napi_delete_async_work(env, getPropertyInfo->asyncWork); + AsyncHolder *asyncHolder = reinterpret_cast(data); + napi_delete_async_work(env, asyncHolder->asyncWork); + delete asyncHolder; HILOG_INFO("GetPropertyAsyncExecuteDone, end"); } -napi_value UserAuthImpl::GetPropertyAsync(napi_env env, GetPropertyInfo *getPropertyInfo) +napi_value UserAuthImpl::GetPropertyAsync(napi_env env, AsyncHolder *asyncHolder) { HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (getPropertyInfo == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = 0; + napi_value result = nullptr; + NAPI_CALL(env, napi_get_null(env, &result)); + napi_value resourceName = nullptr; NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName, GetPropertyExecute, GetPropertyAsyncExecuteDone, - (void *)getPropertyInfo, &getPropertyInfo->asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, getPropertyInfo->asyncWork)); - napi_value result = 0; - NAPI_CALL(env, napi_get_null(env, &result)); + (void *)asyncHolder, &asyncHolder->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, asyncHolder->asyncWork)); HILOG_INFO("%{public}s, asyncCallback end.", __func__); return result; } -napi_value UserAuthImpl::GetPropertyPromise(napi_env env, GetPropertyInfo *getPropertyInfo) +napi_value UserAuthImpl::GetPropertyPromise(napi_env env, AsyncHolder *asyncHolder) { HILOG_INFO("%{public}s, promise.", __func__); - if (getPropertyInfo == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } + GetPropertyInfo *getPropertyInfo = reinterpret_cast(asyncHolder->data); napi_value resourceName = 0; NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); napi_deferred deferred; @@ -183,24 +202,44 @@ napi_value UserAuthImpl::GetPropertyPromise(napi_env env, GetPropertyInfo *getPr NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); getPropertyInfo->callBackInfo.callBack = nullptr; getPropertyInfo->callBackInfo.deferred = deferred; - NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName, GetPropertyExecute, GetPropertyPromiseExecuteDone, - (void *)getPropertyInfo, &getPropertyInfo->asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, getPropertyInfo->asyncWork)); + (void *)asyncHolder, &asyncHolder->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, asyncHolder->asyncWork)); HILOG_INFO("%{public}s, promise end.", __func__); return promise; } napi_value UserAuthImpl::SetProperty(napi_env env, napi_callback_info info) { + AsyncHolder *asyncHolder = new (std::nothrow) AsyncHolder(); + if (asyncHolder == nullptr) { + HILOG_ERROR("%{public}s asyncHolder nullptr", __func__); + return nullptr; + } SetPropertyInfo *setPropertyInfo = new (std::nothrow) SetPropertyInfo(); + if (setPropertyInfo == nullptr) { + delete asyncHolder; + HILOG_ERROR("%{public}s setPropertyInfo nullptr", __func__); + return nullptr; + } setPropertyInfo->callBackInfo.env = env; - return SetPropertyWrap(env, info, setPropertyInfo); + asyncHolder->data = setPropertyInfo; + napi_value ret = SetPropertyWrap(env, info, asyncHolder); + if (ret == nullptr) { + HILOG_ERROR("%{public}s SetPropertyWrap fail", __func__); + delete setPropertyInfo; + delete asyncHolder; + if (asyncHolder->asyncWork != nullptr) { + napi_delete_async_work(env, asyncHolder->asyncWork); + } + } + return ret; } -napi_value UserAuthImpl::SetPropertyWrap(napi_env env, napi_callback_info info, SetPropertyInfo *setPropertyInfo) +napi_value UserAuthImpl::SetPropertyWrap(napi_env env, napi_callback_info info, AsyncHolder *asyncHolder) { HILOG_INFO("%{public}s, called", __func__); + SetPropertyInfo *setPropertyInfo = reinterpret_cast(asyncHolder->data); size_t argcAsync = ARGS_TWO; const size_t argcPromise = ARGS_ONE; const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; @@ -227,9 +266,9 @@ napi_value UserAuthImpl::SetPropertyWrap(napi_env env, napi_callback_info info, napi_value ret = 0; if (argcAsync > argcPromise) { - ret = SetPropertyAsync(env, setPropertyInfo); + ret = SetPropertyAsync(env, asyncHolder); } else { - ret = SetPropertyPromise(env, setPropertyInfo); + ret = SetPropertyPromise(env, asyncHolder); } HILOG_INFO("%{public}s,end.", __func__); return ret; @@ -238,86 +277,83 @@ napi_value UserAuthImpl::SetPropertyWrap(napi_env env, napi_callback_info info, void UserAuthImpl::SetPropertyExecute(napi_env env, void *data) { HILOG_INFO("setPropertyExecute, worker pool thread execute."); - SetPropertyInfo *setPropertyInfo = static_cast(data); - if (setPropertyInfo != nullptr) { - AuthType authTypeGet = AuthType(setPropertyInfo->authType); - - SetPropertyRequest request; - request.authType = authTypeGet; - request.key = SetPropertyType(setPropertyInfo->key); - request.setInfo = setPropertyInfo->setInfo; - AuthApiCallback *object = new AuthApiCallback(); - object->setPropertyInfo_ = setPropertyInfo; - std::shared_ptr callback; - callback.reset(object); - UserAuth::GetInstance().SetProperty(request, callback); - } else { - HILOG_ERROR("setPropertyExecute, asynccallBackInfo == nullptr"); + AsyncHolder *asyncHolder = reinterpret_cast(data); + if (asyncHolder == nullptr) { + HILOG_ERROR("SetPropertyExecute, asyncHolder == nullptr"); + return; + } + SetPropertyInfo *setPropertyInfo = reinterpret_cast(asyncHolder->data); + if (setPropertyInfo == nullptr) { + HILOG_ERROR("SetPropertyExecute, setPropertyInfo == nullptr"); + return; } + AuthType authTypeGet = AuthType(setPropertyInfo->authType); + SetPropertyRequest request; + request.authType = authTypeGet; + request.key = SetPropertyType(setPropertyInfo->key); + request.setInfo = setPropertyInfo->setInfo; + AuthApiCallback *object = new AuthApiCallback(); + object->setPropertyInfo_ = setPropertyInfo; + std::shared_ptr callback; + callback.reset(object); + UserAuth::GetInstance().SetProperty(request, callback); HILOG_INFO("setPropertyExecute, worker pool thread execute end."); } void UserAuthImpl::SetPropertyPromiseExecuteDone(napi_env env, napi_status status, void *data) { HILOG_INFO("SetPropertyPromiseExecuteDone, start"); - SetPropertyInfo *setPropertyInfo = static_cast(data); - napi_delete_async_work(env, setPropertyInfo->asyncWork); + AsyncHolder *asyncHolder = reinterpret_cast(data); + napi_delete_async_work(env, asyncHolder->asyncWork); + delete asyncHolder; HILOG_INFO("SetPropertyPromiseExecuteDone, end"); } void UserAuthImpl::SetPropertyAsyncExecuteDone(napi_env env, napi_status status, void *data) { HILOG_INFO("SetPropertyAsyncExecuteDone, start"); - SetPropertyInfo *setPropertyInfo = static_cast(data); - napi_delete_async_work(env, setPropertyInfo->asyncWork); + AsyncHolder *asyncHolder = reinterpret_cast(data); + napi_delete_async_work(env, asyncHolder->asyncWork); + delete asyncHolder; HILOG_INFO("SetPropertyAsyncExecuteDone, end"); } -napi_value UserAuthImpl::SetPropertyAsync(napi_env env, SetPropertyInfo *setPropertyInfo) +napi_value UserAuthImpl::SetPropertyAsync(napi_env env, AsyncHolder *asyncHolder) { HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (setPropertyInfo == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = 0; + napi_value result = nullptr; + NAPI_CALL(env, napi_get_null(env, &result)); + napi_value resourceName = nullptr; NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName, SetPropertyExecute, SetPropertyAsyncExecuteDone, - (void *)setPropertyInfo, &setPropertyInfo->asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, setPropertyInfo->asyncWork)); - napi_value result = 0; - NAPI_CALL(env, napi_get_null(env, &result)); + (void *)asyncHolder, &asyncHolder->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, asyncHolder->asyncWork)); HILOG_INFO("%{public}s, asyncCallback end.", __func__); return result; } -napi_value UserAuthImpl::SetPropertyPromise(napi_env env, SetPropertyInfo *setPropertyInfo) +napi_value UserAuthImpl::SetPropertyPromise(napi_env env, AsyncHolder *asyncHolder) { HILOG_INFO("%{public}s, promise.", __func__); - if (setPropertyInfo == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } + SetPropertyInfo *setPropertyInfo = reinterpret_cast(asyncHolder->data); napi_value resourceName = 0; NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); napi_deferred deferred; - napi_value promise = 0; + napi_value promise = nullptr; NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); setPropertyInfo->callBackInfo.callBack = nullptr; setPropertyInfo->callBackInfo.deferred = deferred; - - NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName, SetPropertyExecute, SetPropertyPromiseExecuteDone, - (void *)setPropertyInfo, &setPropertyInfo->asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, setPropertyInfo->asyncWork)); + NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName, + SetPropertyExecute, SetPropertyPromiseExecuteDone, (void *)asyncHolder, &asyncHolder->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, asyncHolder->asyncWork)); HILOG_INFO("%{public}s, promise end.", __func__); return promise; } -napi_value UserAuthImpl::Auth(napi_env env, napi_callback_info info) +napi_value UserAuthImpl::BuildAuthInfo(napi_env env, AuthInfo *authInfo) { - HILOG_INFO("%{public}s, start", __func__); - AuthInfo *authInfo = new (std::nothrow) AuthInfo(); - authInfo->info = info; + napi_value result = nullptr; + NAPI_CALL(env, napi_get_null(env, &result)); authInfo->callBackInfo.env = env; size_t argc = ARGS_MAX_COUNT; napi_value argv[ARGS_MAX_COUNT] = {nullptr}; @@ -347,16 +383,30 @@ napi_value UserAuthImpl::Auth(napi_env env, napi_callback_info info) NAPI_CALL(env, napi_get_named_property(env, argv[PARAM3], "onAcquireInfo", &authInfo->onAcquireInfoCallBack)); NAPI_CALL(env, napi_create_reference(env, authInfo->onAcquireInfoCallBack, PARAM1, &authInfo->onAcquireInfo)); } - return AuthWrap(env, authInfo); + return result; } -napi_value UserAuthImpl::AuthWrap(napi_env env, AuthInfo *authInfo) +napi_value UserAuthImpl::Auth(napi_env env, napi_callback_info info) { - HILOG_INFO("%{public}s, start.", __func__); + HILOG_INFO("%{public}s, start", __func__); + AuthInfo *authInfo = new (std::nothrow) AuthInfo(); if (authInfo == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); + HILOG_INFO("%{public}s authInfo nullptr", __func__); return nullptr; } + authInfo->info = info; + napi_value ret = BuildAuthInfo(env, authInfo); + if (ret == nullptr) { + HILOG_INFO("%{public}s BuildAuthInfo fail", __func__); + delete authInfo; + return ret; + } + return AuthWrap(env, authInfo); +} + +napi_value UserAuthImpl::AuthWrap(napi_env env, AuthInfo *authInfo) +{ + HILOG_INFO("%{public}s, start.", __func__); AuthApiCallback *object = new AuthApiCallback(); object->authInfo_ = authInfo; object->userInfo_ = nullptr; @@ -374,8 +424,25 @@ napi_value UserAuthImpl::AuthUser(napi_env env, napi_callback_info info) { HILOG_INFO("%{public}s, start.", __func__); AuthUserInfo *userInfo = new (std::nothrow) AuthUserInfo(); + if (userInfo == nullptr) { + HILOG_INFO("%{public}s userInfo nullptr", __func__); + return nullptr; + } userInfo->callBackInfo.env = env; userInfo->info = info; + napi_value ret = BuildAuthUserInfo(env, userInfo); + if (ret == nullptr) { + HILOG_INFO("%{public}s BuildAuthUserInfo fail", __func__); + delete userInfo; + return ret; + } + return AuthUserWrap(env, userInfo); +} + +napi_value UserAuthImpl::BuildAuthUserInfo(napi_env env, AuthUserInfo *userInfo) +{ + napi_value result = nullptr; + NAPI_CALL(env, napi_get_null(env, &result)); size_t argc = ARGS_MAX_COUNT; napi_value argv[ARGS_MAX_COUNT] = {nullptr}; NAPI_CALL(env, napi_get_cb_info(env, userInfo->info, &argc, argv, nullptr, nullptr)); @@ -407,16 +474,12 @@ napi_value UserAuthImpl::AuthUser(napi_env env, napi_callback_info info) NAPI_CALL(env, napi_get_named_property(env, argv[PARAM4], "onAcquireInfo", &userInfo->onAcquireInfoCallBack)); NAPI_CALL(env, napi_create_reference(env, userInfo->onAcquireInfoCallBack, PARAM1, &userInfo->onAcquireInfo)); } - return AuthUserWrap(env, userInfo); + return result; } napi_value UserAuthImpl::AuthUserWrap(napi_env env, AuthUserInfo *userInfo) { HILOG_INFO("%{public}s, start.", __func__); - if (userInfo == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } AuthApiCallback *object = new AuthApiCallback(); object->authInfo_ = nullptr; object->userInfo_ = userInfo; diff --git a/interfaces/innerkits/native/include/user_auth.h b/interfaces/innerkits/native/include/user_auth.h index be7198471..4c875e446 100755 --- a/interfaces/innerkits/native/include/user_auth.h +++ b/interfaces/innerkits/native/include/user_auth.h @@ -44,9 +44,9 @@ public: private: class UserAuthDeathRecipient : public IRemoteObject::DeathRecipient { public: - UserAuthDeathRecipient() = default; - ~UserAuthDeathRecipient() = default; - void OnRemoteDied(const wptr& remote) override; + UserAuthDeathRecipient() = default; + ~UserAuthDeathRecipient() = default; + void OnRemoteDied(const wptr& remote) override; private: DISALLOW_COPY_AND_MOVE(UserAuthDeathRecipient); diff --git a/interfaces/innerkits/native/include/userauth_info.h b/interfaces/innerkits/native/include/userauth_info.h index 4284e9b69..7a60d349d 100755 --- a/interfaces/innerkits/native/include/userauth_info.h +++ b/interfaces/innerkits/native/include/userauth_info.h @@ -23,12 +23,12 @@ namespace OHOS { namespace UserIAM { namespace UserAuth { -// 认证的类型(口令,人脸) +// Type of authentication (password, face) enum AuthType: uint32_t { PIN = 1, FACE = 2, }; -// 认证子类型(2D人脸,3D人脸...) +// Authentication subtype (2D face, 3D face...) enum AuthSubType: uint64_t { /** * Authentication sub type six number pin. @@ -51,7 +51,7 @@ enum AuthSubType: uint64_t { */ FACE_3D = 20001 }; -// 认证结果可信等级 +// Certification result confidence level enum AuthTurstLevel: uint32_t { // level 1-4 ATL1 = 10000, @@ -59,48 +59,50 @@ enum AuthTurstLevel: uint32_t { ATL3 = 30000, ATL4 = 40000 }; -// 执行器属性列表 +// Actuator get property list enum GetPropertyType: uint32_t { - // 认证子类型(此时认证类型已确认) + // Authentication subtype (at this point the authentication type has been confirmed) AUTH_SUB_TYPE = 1, - // 剩余认证次数 + // Remaining authentication times REMAIN_TIMES = 2, - // 冻结时间 + // Freeze time FREEZING_TIME = 3, }; -// 获得属性请求 +// get attribute request struct GetPropertyRequest { AuthType authType; // GetPropertyType std::vector keys; }; -// 执行器属性 +// Actuator properties struct ExecutorProperty { int32_t result; AuthSubType authSubType; uint32_t remainTimes; uint32_t freezingTime; }; -// 执行器属性列表 +// Actuator property mode list enum AuthPropertyMode: uint32_t { - PROPERMODE_DELETE = 0, - PROPERMODE_GET = 1, - PROPERMODE_SET = 2, - PROPERMODE_FREEZE = 3, - PROPERMODE_UNFREEZE = 4, + PROPERMODE_DELETE = 0, + PROPERMODE_GET = 1, + PROPERMODE_SET = 2, + PROPERMODE_FREEZE = 3, + PROPERMODE_UNFREEZE = 4, + PROPERMODE_INIT_ALGORITHM = 5, + PROPERMODE_RELEASE_ALGORITHM = 6, }; -// 执行器属性列表 +// Actuator property list enum SetPropertyType: uint32_t { - INIT_ALGORITHM = 1, - FREEZE_TEMPLATE = 2, - THAW_TEMPLATE = 3, + INIT_ALGORITHM = 1, + FREEZE_TEMPLATE = 2, + THAW_TEMPLATE = 3, }; struct SetPropertyRequest { AuthType authType; SetPropertyType key; std::vector setInfo; }; -// 认证结果 +// Authentication Result struct AuthResult { std::vector token; uint32_t remainTimes; @@ -121,7 +123,7 @@ struct FreezInfo { AuthType authType; }; -// 结果码 +// Result Code enum ResultCode: int32_t { /** * Indicates that authentication is success or ability is supported. diff --git a/services/include/userauth_adapter.h b/services/include/userauth_adapter.h index 3f2b5a61f..3476d81ff 100644 --- a/services/include/userauth_adapter.h +++ b/services/include/userauth_adapter.h @@ -45,20 +45,24 @@ public: void CoauthSetPropAuthInfo(int32_t resultCode, uint64_t callerUID, std::string pkgName, UserAuthToken authToken, SetPropertyRequest requset, sptr& callback); - /* Set the executor authentication properties */ + /* Set the executor authentication properties for freez or unfreez*/ void SetPropAuthInfo(uint64_t callerUID, std::string pkgName, int32_t resultCode, UserAuthToken authToken, SetPropertyRequest requset, std::vector templateIds, sptr& callback); + /* get the executor authentication properties for Coauth */ void GetPropAuthInfoCoauth(uint64_t callerUID, std::string pkgName, int32_t resultCode, UserAuthToken authToken, GetPropertyRequest requset, sptr& callback); + /* get userID */ int32_t GetUserID(int32_t &userID); int32_t GenerateSolution(AuthSolution param, std::vector &sessionIds); int32_t RequestAuthResult(uint64_t contextId, std::vector scheduleToken, UserAuthToken &authToken, std::vector &sessionIds); int32_t CancelContext(uint64_t contextId, std::vector &sessionIds); int32_t Cancel(uint64_t sessionId); + /* get the executor authentication properties */ int32_t GetExecutorProp(uint64_t callerUID, std::string pkgName, uint64_t templateId, GetPropertyRequest requset, ExecutorProperty &result); + /* Set the executor authentication properties */ int32_t SetExecutorProp(uint64_t callerUID, std::string pkgName, SetPropertyRequest requset, sptr& callback); int32_t GetVersion(); diff --git a/services/include/userauth_async_proxy.h b/services/include/userauth_async_proxy.h index e3b3f2738..f34f67f50 100755 --- a/services/include/userauth_async_proxy.h +++ b/services/include/userauth_async_proxy.h @@ -37,8 +37,6 @@ public: private: bool SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply); - void WriteFailResult(const ExecutorProperty &result, ExecutorProperty &resultFail, - MessageParcel &data); private: static inline BrokerDelegator delegator_; @@ -46,4 +44,4 @@ private: } // namespace UserAuth } // namespace UserIAM } // namespace OHOS -#endif // USERAUTH_ASYNC_PROXY_H \ No newline at end of file +#endif // USERAUTH_ASYNC_PROXY_H diff --git a/services/src/userauth_adapter.cpp b/services/src/userauth_adapter.cpp index 4dff5c26e..af1dc5c17 100644 --- a/services/src/userauth_adapter.cpp +++ b/services/src/userauth_adapter.cpp @@ -94,7 +94,7 @@ int32_t UserAuthAdapter::SetProPropAuthInfo(OHOS::UserIAM::AuthResPool::AuthAttr setPropCallback->OnResult(ret, extraInfo); return ret; } - value = requset.key ==SetPropertyType::FREEZE_TEMPLATE ? + value = requset.key == SetPropertyType::FREEZE_TEMPLATE ? static_cast(AuthPropertyMode::PROPERMODE_FREEZE) : static_cast(AuthPropertyMode::PROPERMODE_UNFREEZE); ret = authAttributes.SetUint32Value(AUTH_PROPERTY_MODE, value); @@ -331,7 +331,10 @@ int32_t UserAuthAdapter::SetExecutorProp(uint64_t callerUID, std::string pkgName std::shared_ptr setPropCallback = std::make_shared(callback); OHOS::UserIAM::AuthResPool::AuthAttributes pAuthAttributes; - int32_t ret = pAuthAttributes.SetUint32Value(AUTH_PROPERTY_MODE, PROPERMODE_SET); + uint32_t value = requset.key == SetPropertyType::INIT_ALGORITHM ? + static_cast(AuthPropertyMode::PROPERMODE_INIT_ALGORITHM) + : static_cast(AuthPropertyMode::PROPERMODE_RELEASE_ALGORITHM); + int32_t ret = pAuthAttributes.SetUint32Value(AUTH_PROPERTY_MODE, value); if (ret != SUCCESS) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuth SetUint32Value SET_AUTH_PROPERTY_MODE ERROR!"); return ret; @@ -355,7 +358,7 @@ int32_t UserAuthAdapter::SetExecutorProp(uint64_t callerUID, std::string pkgName USERAUTH_HILOGE(MODULE_SERVICE, "UserAuth SetUint32Value AUTH_TYPE ERROR!"); return ret; } - ret = pAuthAttributes.SetUint8ArrayValue(AUTH_SIGNATURE, requset.setInfo); + ret = pAuthAttributes.SetUint8ArrayValue(ALGORITHM_INFO, requset.setInfo); if (ret != SUCCESS) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuth SetUint8ArrayValue init ERROR!"); return ret; diff --git a/services/src/userauth_async_proxy.cpp b/services/src/userauth_async_proxy.cpp index da472454d..d12862b1d 100755 --- a/services/src/userauth_async_proxy.cpp +++ b/services/src/userauth_async_proxy.cpp @@ -90,61 +90,32 @@ void UserAuthAsyncProxy::onExecutorPropertyInfo(const ExecutorProperty result) USERAUTH_HILOGD(MODULE_SERVICE, "userauthAsyncProxy onExecutorPropertyInfo enter"); MessageParcel data; MessageParcel reply; - ExecutorProperty resultFail; if (!data.WriteInterfaceToken(UserAuthAsyncProxy::GetDescriptor())) { USERAUTH_HILOGI(MODULE_SERVICE, "userauth write descriptor failed!"); return ; } - if (result.result != SUCCESS) { - WriteFailResult(result, resultFail, data); - } else { - if (!data.WriteInt32(result.result)) { - USERAUTH_HILOGE(MODULE_SERVICE, "failed to WriteInt32(result.result)."); - return; - } - if (!data.WriteUint64(result.authSubType)) { - USERAUTH_HILOGE(MODULE_SERVICE, "failed to WriteUint64(result.authSubType)."); - return; - } - if (!data.WriteUint32(result.remainTimes)) { - USERAUTH_HILOGE(MODULE_SERVICE, "failed to WriteUint32(result.remainTimes)."); - return; - } - if (!data.WriteUint32(result.freezingTime)) { - USERAUTH_HILOGE(MODULE_SERVICE, "failed to WriteUint32(result.freezingTime)."); - return; - } - } - bool ret = SendRequest(IUserAuth::USER_AUTH_GETEXPORP, data, reply); - if (ret) { - int32_t result = reply.ReadInt32(); - USERAUTH_HILOGE(MODULE_SERVICE, "userauth result = %{public}d", result); - } - return; -} - -void UserAuthAsyncProxy::WriteFailResult(const ExecutorProperty &result, ExecutorProperty &resultFail, - MessageParcel &data) -{ - resultFail.authSubType = PIN_SIX; - resultFail.freezingTime = 0; - resultFail.remainTimes = 0; if (!data.WriteInt32(result.result)) { USERAUTH_HILOGE(MODULE_SERVICE, "failed to WriteInt32(result.result)."); return; } - if (!data.WriteUint64(resultFail.authSubType)) { - USERAUTH_HILOGE(MODULE_SERVICE, "failed to WriteUint64(resultFail.authSubType)."); + if (!data.WriteUint64(result.authSubType)) { + USERAUTH_HILOGE(MODULE_SERVICE, "failed to WriteUint64(result.authSubType)."); return; } - if (!data.WriteUint32(resultFail.remainTimes)) { - USERAUTH_HILOGE(MODULE_SERVICE, "failed to WriteUint32(resultFail.remainTimes)."); + if (!data.WriteUint32(result.remainTimes)) { + USERAUTH_HILOGE(MODULE_SERVICE, "failed to WriteUint32(result.remainTimes)."); return; } - if (!data.WriteUint32(resultFail.freezingTime)) { - USERAUTH_HILOGE(MODULE_SERVICE, "failed to WriteUint32(resultFail.freezingTime)."); + if (!data.WriteUint32(result.freezingTime)) { + USERAUTH_HILOGE(MODULE_SERVICE, "failed to WriteUint32(result.freezingTime)."); return; } + bool ret = SendRequest(IUserAuth::USER_AUTH_GETEXPORP, data, reply); + if (ret) { + int32_t result = reply.ReadInt32(); + USERAUTH_HILOGE(MODULE_SERVICE, "userauth result = %{public}d", result); + } + return; } void UserAuthAsyncProxy::onSetExecutorProperty(const int32_t result) diff --git a/services/src/userauth_excallback_impl.cpp b/services/src/userauth_excallback_impl.cpp index 6f15a822c..695f81c01 100644 --- a/services/src/userauth_excallback_impl.cpp +++ b/services/src/userauth_excallback_impl.cpp @@ -278,7 +278,7 @@ UserAuthCallbackImplIDMGetPorp::UserAuthCallbackImplIDMGetPorp(const sptr& info) { USERAUTH_HILOGD(MODULE_SERVICE, "UserAuthCallbackImplIDMGetPorp OnGetInfo enter"); - ExecutorProperty executorProperty; + ExecutorProperty executorProperty = {}; if (info.size() == 0) { executorProperty.result = GENERAL_ERROR; callback_->onExecutorPropertyInfo(executorProperty); -- Gitee