From 2f70805dfbf5687b465e3b8c96f9f95c1e157772 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9F=A9=E9=9C=87?= Date: Fri, 18 Feb 2022 21:23:04 +0800 Subject: [PATCH] fix codex MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 韩震 --- bundle.json | 196 ++- frameworks/kitsimpl/src/userauth.cpp | 30 +- .../kitsimpl/src/userauth_async_stub.cpp | 13 +- frameworks/kitsimpl/src/userauth_proxy.cpp | 31 +- interfaces/innerkits/napi/BUILD.gn | 2 - .../innerkits/napi/include/auth_common.h | 280 ++-- .../napi/include/auth_hilog_wrapper.h | 61 - .../innerkits/napi/include/auth_object.h | 108 +- .../innerkits/napi/include/user_auth_helper.h | 252 ++-- interfaces/innerkits/napi/src/auth_build.cpp | 256 ++-- .../innerkits/napi/src/auth_native_module.cpp | 45 - .../innerkits/napi/src/authapi_callback.cpp | 1125 ++++++++------- .../innerkits/napi/src/result_convert.cpp | 449 +++--- .../innerkits/napi/src/user_auth_helper.cpp | 494 +++---- .../innerkits/napi/src/user_auth_impl.cpp | 1245 +++++++++-------- interfaces/kits/napi/BUILD.gn | 4 +- .../napi/include/user_auth_helper_north.h | 1 + .../kits/napi/src/auth_native_module.cpp | 41 - .../kits/napi/src/user_auth_helper_north.cpp | 26 +- utils/native/include/userauth_errors.h | 36 - 20 files changed, 2283 insertions(+), 2412 deletions(-) delete mode 100755 interfaces/innerkits/napi/include/auth_hilog_wrapper.h delete mode 100755 interfaces/innerkits/napi/src/auth_native_module.cpp delete mode 100755 interfaces/kits/napi/src/auth_native_module.cpp delete mode 100644 utils/native/include/userauth_errors.h diff --git a/bundle.json b/bundle.json index 0c3e3fb18..245d83d41 100644 --- a/bundle.json +++ b/bundle.json @@ -1,99 +1,97 @@ -{ - "name": "@openharmony/user_auth", - "version": "3.1.0", - "description": "User Authentication capability", - "homePage": "https://gitee.com/openharmony", - "author": {}, - "repository": "", - "license": "Apache License 2.0", - "publishAs": "code-segment", - "segment": { - "destPath" : "base/user_iam/user_auth" - }, - "dirs": {}, - "scripts": {}, - "component": { - "name": "user_auth", - "subsystem": "useriam", - "adapted_system_type": [ "standard" ], - "syscap": ["SystemCapability.UserIAM.UserAuth.Core"], - "features": [], - "rom": "2400KB", - "ram": "1500KB", - "deps": { - "components": [ - "hiviewdfx_hilog_native", - "ipc", - "safwk", - "samgr_standard", - "hilog", - "auth_executor_mgr" - ], - "third_party": [ - "openssl" - ] - }, - "build": { - "sub_component": [ - "//base/user_iam/user_auth/sa_profile:userauth_sa_profile", - "//base/user_iam/user_auth/services:userauthservice", - "//base/user_iam/user_auth/interfaces/kits/napi:userauthnorth" - ], - "inner_kits": [ - { - "type": "so", - "name": "//base/user_iam/user_auth/interfaces/innerkits/native:userauth_framework", - "header": { - "header_files": [ - "iuser_auth.h", - "iuserauth_callback.h", - "user_auth.h", - "userauth_async_stub.h", - "userauth_callback.h", - "userauth_info.h", - "userauth_proxy.h" - ], - "header_base": "//base/user_iam/user_auth/interfaces/innerkits/native/include" - } - }, - { - "type": "so", - "name": "//base/user_iam/user_auth/interfaces/innerkits/napi:userauth", - "header": { - "header_files": [ - "auth_build.h", - "auth_common.h", - "auth_hilog_wrapper.h", - "auth_object.h", - "authapi_callback.h", - "result_convert.h", - "user_auth_helper.h", - "user_auth_impl.h" - ], - "header_base": "//base/user_iam/user_auth/interfaces/innerkits/napi/include" - } - }, - { - "type": "so", - "name": "//base/user_iam/user_auth/interfaces/innerkits/napi:userauth_napi", - "header": { - "header_files": [ - "auth_build.h", - "auth_common.h", - "auth_hilog_wrapper.h", - "auth_object.h", - "authapi_callback.h", - "result_convert.h", - "user_auth_helper.h", - "user_auth_impl.h" - ], - "header_base": "//base/user_iam/user_auth/interfaces/innerkits/napi/include" - } - } - ], - "test": [ - "//base/user_iam/user_auth/test:userauth_native_test" - ] - } - } - } +{ + "name": "@openharmony/user_auth", + "version": "3.1.0", + "description": "User Authentication capability", + "homePage": "https://gitee.com/openharmony", + "author": {}, + "repository": "", + "license": "Apache License 2.0", + "publishAs": "code-segment", + "segment": { + "destPath" : "base/user_iam/user_auth" + }, + "dirs": {}, + "scripts": {}, + "component": { + "name": "user_auth", + "subsystem": "useriam", + "adapted_system_type": [ "standard" ], + "syscap": ["SystemCapability.UserIAM.UserAuth.Core"], + "features": [], + "rom": "2400KB", + "ram": "1500KB", + "deps": { + "components": [ + "hiviewdfx_hilog_native", + "ipc", + "safwk", + "samgr_standard", + "hilog", + "auth_executor_mgr" + ], + "third_party": [ + "openssl" + ] + }, + "build": { + "sub_component": [ + "//base/user_iam/user_auth/sa_profile:userauth_sa_profile", + "//base/user_iam/user_auth/services:userauthservice", + "//base/user_iam/user_auth/interfaces/kits/napi:userauthnorth" + ], + "inner_kits": [ + { + "type": "so", + "name": "//base/user_iam/user_auth/interfaces/innerkits/native:userauth_framework", + "header": { + "header_files": [ + "iuser_auth.h", + "iuserauth_callback.h", + "user_auth.h", + "userauth_async_stub.h", + "userauth_callback.h", + "userauth_info.h", + "userauth_proxy.h" + ], + "header_base": "//base/user_iam/user_auth/interfaces/innerkits/native/include" + } + }, + { + "type": "so", + "name": "//base/user_iam/user_auth/interfaces/innerkits/napi:userauth", + "header": { + "header_files": [ + "auth_build.h", + "auth_common.h", + "auth_object.h", + "authapi_callback.h", + "result_convert.h", + "user_auth_helper.h", + "user_auth_impl.h" + ], + "header_base": "//base/user_iam/user_auth/interfaces/innerkits/napi/include" + } + }, + { + "type": "so", + "name": "//base/user_iam/user_auth/interfaces/innerkits/napi:userauth_napi", + "header": { + "header_files": [ + "auth_build.h", + "auth_common.h", + "auth_object.h", + "authapi_callback.h", + "result_convert.h", + "user_auth_helper.h", + "user_auth_impl.h" + ], + "header_base": "//base/user_iam/user_auth/interfaces/innerkits/napi/include" + } + } + ], + "test": [ + "//base/user_iam/user_auth/test:userauth_native_test" + ] + } + } + } diff --git a/frameworks/kitsimpl/src/userauth.cpp b/frameworks/kitsimpl/src/userauth.cpp index 796592ab0..88ad3c87b 100644 --- a/frameworks/kitsimpl/src/userauth.cpp +++ b/frameworks/kitsimpl/src/userauth.cpp @@ -12,6 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include + #include #include #include @@ -107,9 +109,13 @@ void UserAuth::GetProperty(const GetPropertyRequest request, std::shared_ptronGetProperty(result); - return ; + return; + } + sptr asyncStub = new (std::nothrow) UserAuthAsyncStub(callback); + if (asyncStub == nullptr) { + USERAUTH_HILOGE(MODULE_INNERKIT, "UserAuthAsyncStub failed, GetProperty IUserAuthCallback is nullptr"); + return; } - sptr asyncStub = new UserAuthAsyncStub(callback); proxy_->GetProperty(request, asyncStub); } void UserAuth::SetProperty(const SetPropertyRequest request, std::shared_ptr callback) @@ -122,9 +128,13 @@ void UserAuth::SetProperty(const SetPropertyRequest request, std::shared_ptronSetProperty(E_RET_NOSERVER); - return ; + return; + } + sptr asyncStub = new (std::nothrow) UserAuthAsyncStub(callback); + if (asyncStub == nullptr) { + USERAUTH_HILOGE(MODULE_INNERKIT, "UserAuthAsyncStub failed, SetProperty IUserAuthCallback is nullptr"); + return; } - sptr asyncStub = new UserAuthAsyncStub(callback); proxy_->SetProperty(request, asyncStub); } uint64_t UserAuth::Auth(const uint64_t challenge, const AuthType authType, const AuthTurstLevel authTurstLevel, @@ -139,7 +149,11 @@ uint64_t UserAuth::Auth(const uint64_t challenge, const AuthType authType, const if (proxy == nullptr) { return E_RET_NOSERVER; } - sptr asyncStub = new UserAuthAsyncStub(callback); + sptr asyncStub = new (std::nothrow) UserAuthAsyncStub(callback); + if (asyncStub == nullptr) { + USERAUTH_HILOGE(MODULE_INNERKIT, "UserAuthAsyncStub failed, Auth IUserAuthCallback is nullptr"); + return GENERAL_ERROR; + } uint64_t ret = proxy_->Auth(challenge, authType, authTurstLevel, asyncStub); return ret; } @@ -155,7 +169,11 @@ uint64_t UserAuth::AuthUser(const int32_t userId, const uint64_t challenge, cons if (proxy == nullptr) { return E_RET_NOSERVER; } - sptr asyncStub = new UserAuthAsyncStub(callback); + sptr asyncStub = new (std::nothrow) UserAuthAsyncStub(callback); + if (asyncStub == nullptr) { + USERAUTH_HILOGE(MODULE_INNERKIT, "UserAuthAsyncStub failed, AuthUser IUserAuthCallback is nullptr"); + return GENERAL_ERROR; + } uint64_t ret = proxy_->AuthUser(userId, challenge, authType, authTurstLevel, asyncStub); return ret; } diff --git a/frameworks/kitsimpl/src/userauth_async_stub.cpp b/frameworks/kitsimpl/src/userauth_async_stub.cpp index 4a7ee0545..fde0fd829 100644 --- a/frameworks/kitsimpl/src/userauth_async_stub.cpp +++ b/frameworks/kitsimpl/src/userauth_async_stub.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ +#include #include #include "userauth_hilog_wrapper.h" @@ -201,7 +202,7 @@ void UserAuthAsyncStub::onAcquireInfo(const int32_t module, const uint32_t acqui if (authCallback_ == nullptr) { USERAUTH_HILOGE(MODULE_INNERKIT, "userauthAsyncStub onAcquireInfo callback is Null"); - return ; + return; } USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAsyncStub module:%{public}d, acquireInfo:%{public}u", @@ -216,7 +217,7 @@ void UserAuthAsyncStub::onResult(const int32_t result, const AuthResult extraInf if (authCallback_ == nullptr) { USERAUTH_HILOGE(MODULE_INNERKIT, "userauthAsyncStub onResult callback is Null"); - return ; + return; } USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAsyncStub result:%{public}d, remain:%{public}u, freeze:%{public}u", @@ -230,12 +231,12 @@ void UserAuthAsyncStub::onExecutorPropertyInfo(const ExecutorProperty result) if (getPropCallback_ == nullptr) { USERAUTH_HILOGE(MODULE_INNERKIT, "UserAuthAsyncStub onExecutorPropertyInfo callback is Null"); - return ; + return; } USERAUTH_HILOGD(MODULE_INNERKIT, - "userauthAsyncStub result:%{public}d, sub:%{public}llu, remain:%{public}u, freeze:%{public}u", - result.result, result.authSubType, result.remainTimes, result.freezingTime); + "userauthAsyncStub result:%{public}d, sub:%{public}" PRIu64 ", remain:%{public}u, freeze:%{public}u", + result.result, result.authSubType, result.remainTimes, result.freezingTime); getPropCallback_->onGetProperty(result); } @@ -245,7 +246,7 @@ void UserAuthAsyncStub::onSetExecutorProperty(const int32_t result) if (setPropCallback_ == nullptr) { USERAUTH_HILOGE(MODULE_INNERKIT, "UserAuthAsyncStub onSetExecutorProperty callback is Null"); - return ; + return; } USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAsyncStub result:%{public}d", result); setPropCallback_->onSetProperty(result); diff --git a/frameworks/kitsimpl/src/userauth_proxy.cpp b/frameworks/kitsimpl/src/userauth_proxy.cpp index 65c9093ac..b3cdb7a98 100644 --- a/frameworks/kitsimpl/src/userauth_proxy.cpp +++ b/frameworks/kitsimpl/src/userauth_proxy.cpp @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include #include #include #include "userauth_hilog_wrapper.h" @@ -44,7 +45,7 @@ int32_t UserAuthProxy::GetAvailableStatus(const AuthType authType, const AuthTur } bool ret = SendRequest(static_cast(IUserAuth::USER_AUTH_GET_AVAILABLE_STATUS), data, reply, option); if (!ret) { - USERAUTH_HILOGE(MODULE_INNERKIT, "userauth SendRequest is failed, error code: %d", ret); + USERAUTH_HILOGE(MODULE_INNERKIT, "userauth SendRequest is failed, error code: %{public}d", ret); return IPC_ERROR; } if (!reply.ReadInt32(result)) { @@ -63,7 +64,7 @@ void UserAuthProxy::GetProperty(const GetPropertyRequest request, sptr(request.authType))) { USERAUTH_HILOGE(MODULE_INNERKIT, "failed to WriteUint32(static_cast(request.authType))."); @@ -82,8 +83,8 @@ void UserAuthProxy::GetProperty(const GetPropertyRequest request, sptronExecutorPropertyInfo(result); - USERAUTH_HILOGE(MODULE_INNERKIT, "userauth SendRequest is failed, error code: %d", ret); - return ; + USERAUTH_HILOGE(MODULE_INNERKIT, "userauth SendRequest is failed, error code: %{public}d", ret); + return; } } void UserAuthProxy::SetProperty(const SetPropertyRequest request, sptr& callback) @@ -95,7 +96,7 @@ void UserAuthProxy::SetProperty(const SetPropertyRequest request, sptr(request.authType))) { USERAUTH_HILOGE(MODULE_INNERKIT, "failed to WriteUint32(static_cast(request.authType))."); @@ -117,8 +118,8 @@ void UserAuthProxy::SetProperty(const SetPropertyRequest request, sptronSetExecutorProperty(result); - USERAUTH_HILOGE(MODULE_INNERKIT, "userauth SendRequest is failed, error code: %d", ret); - return ; + USERAUTH_HILOGE(MODULE_INNERKIT, "userauth SendRequest is failed, error code: %{public}d", ret); + return; } } @@ -129,13 +130,13 @@ uint64_t UserAuthProxy::Auth(const uint64_t challenge, const AuthType authType, u_int64_t result = SUCCESS; MessageParcel data; MessageParcel reply; - MessageOption option; + MessageOption option(MessageOption::TF_SYNC); if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) { USERAUTH_HILOGE(MODULE_INNERKIT, "userauth write descriptor failed!"); return result; } - USERAUTH_HILOGE(MODULE_SERVICE, "UserAuthProxy::Auth challenge = %{public}llu", challenge); + USERAUTH_HILOGE(MODULE_SERVICE, "UserAuthProxy::Auth challenge = %{public}" PRIu64 "", challenge); if (!data.WriteUint64(challenge)) { USERAUTH_HILOGE(MODULE_INNERKIT, "failed to WriteUint64(challenge)."); return E_READ_PARCEL_ERROR; @@ -154,12 +155,11 @@ uint64_t UserAuthProxy::Auth(const uint64_t challenge, const AuthType authType, } bool ret = SendRequest(IUserAuth::USER_AUTH_AUTH, data, reply, option); if (!ret) { - USERAUTH_HILOGE(MODULE_INNERKIT, "userauth SendRequest is failed, error code: %d", ret); + USERAUTH_HILOGE(MODULE_INNERKIT, "userauth SendRequest is failed, error code: %{public}d", ret); return result; } if (!reply.ReadUint64(result)) { USERAUTH_HILOGE(MODULE_INNERKIT, "userauth Readback fail!"); - return result; } return result; @@ -172,7 +172,7 @@ uint64_t UserAuthProxy::AuthUser(const int32_t userId, const uint64_t challenge, u_int64_t result = SUCCESS; MessageParcel data; MessageParcel reply; - MessageOption option; + MessageOption option(MessageOption::TF_SYNC); if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) { USERAUTH_HILOGE(MODULE_INNERKIT, "write descriptor failed!"); @@ -200,12 +200,11 @@ uint64_t UserAuthProxy::AuthUser(const int32_t userId, const uint64_t challenge, } bool ret = SendRequest(IUserAuth::USER_AUTH_AUTH_USER, data, reply, option); if (!ret) { - USERAUTH_HILOGE(MODULE_INNERKIT, "SendRequest is failed, error code: %d", ret); + USERAUTH_HILOGE(MODULE_INNERKIT, "SendRequest is failed, error code: %{public}d", ret); return result; } if (!reply.ReadUint64(result)) { USERAUTH_HILOGE(MODULE_INNERKIT, "userauth Readback fail!"); - return result; } return result; @@ -229,7 +228,7 @@ int32_t UserAuthProxy::CancelAuth(const uint64_t contextId) } bool ret = SendRequest(IUserAuth::USER_AUTH_CANCEL_AUTH, data, reply, option); if (!ret) { - USERAUTH_HILOGE(MODULE_INNERKIT, "SendRequest is failed, error code: %d", ret); + USERAUTH_HILOGE(MODULE_INNERKIT, "SendRequest is failed, error code: %{public}d", ret); return IPC_ERROR; } if (!reply.ReadInt32(result)) { @@ -254,7 +253,7 @@ int32_t UserAuthProxy::GetVersion() bool ret = SendRequest(IUserAuth::USER_AUTH_GET_VERSION, data, reply, option); if (!ret) { - USERAUTH_HILOGE(MODULE_INNERKIT, "SendRequest is failed, error code: %d", ret); + USERAUTH_HILOGE(MODULE_INNERKIT, "SendRequest is failed, error code: %{public}d", ret); return IPC_ERROR; } if (!reply.ReadInt32(result)) { diff --git a/interfaces/innerkits/napi/BUILD.gn b/interfaces/innerkits/napi/BUILD.gn index a9a5e15fc..2e616129e 100755 --- a/interfaces/innerkits/napi/BUILD.gn +++ b/interfaces/innerkits/napi/BUILD.gn @@ -29,7 +29,6 @@ ohos_shared_library("userauth") { sources = [ "src/auth_build.cpp", - "src/auth_native_module.cpp", "src/authapi_callback.cpp", "src/result_convert.cpp", "src/user_auth_helper.cpp", @@ -69,7 +68,6 @@ ohos_shared_library("userauth_napi") { sources = [ "src/auth_build.cpp", - "src/auth_native_module.cpp", "src/authapi_callback.cpp", "src/result_convert.cpp", "src/user_auth_helper.cpp", diff --git a/interfaces/innerkits/napi/include/auth_common.h b/interfaces/innerkits/napi/include/auth_common.h index 7ea72267b..945ffba8b 100755 --- a/interfaces/innerkits/napi/include/auth_common.h +++ b/interfaces/innerkits/napi/include/auth_common.h @@ -1,140 +1,140 @@ -/* - * Copyright (c) 2021 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. - */ -#ifndef OHOS_USERAUTH_COMMON_H -#define OHOS_USERAUTH_COMMON_H - -#include -#include - -#include "napi/native_api.h" -#include "napi/native_common.h" - -namespace OHOS { -namespace UserIAM { -namespace UserAuth { -#define ARGS_MAX_COUNT 10 -#define ARGS_ASYNC_COUNT 1 -#define GET_VALUE_ERROR (-1) -#define NAPI_GET_STRING_SIZE 128 - -#define ARGS_ONE 1 -#define ARGS_TWO 2 -#define ARGS_THREE 3 -#define ARGS_FOUR 4 -#define ARGS_FIVE 5 -#define ARGS_SIX 6 -#define ARGS_SEVEN 7 -#define ARGS_EIGHT 8 -#define ARGS_NINE 9 -#define ARGS_TEN 10 - -#define PARAM0 0 -#define PARAM1 1 -#define PARAM2 2 -#define PARAM3 3 -#define PARAM4 4 -#define PARAM5 5 -#define PARAM6 6 -#define PARAM7 7 -#define PARAM8 8 -#define PARAM9 9 -#define PARAM10 10 - -struct CallBackInfo { - napi_env env; - napi_ref callBack = 0; - napi_deferred deferred; -}; - -struct GetPropertyInfo { - GetPropertyInfo() : result(nullptr) {}; - CallBackInfo callBackInfo; - napi_value result; - int32_t authType; - std::vector keys; - int32_t getResult; - uint64_t authSubType; - uint32_t remainTimes; - uint32_t freezingTime; -}; - -struct SetPropertyInfo { - SetPropertyInfo() : asyncWork(nullptr), result(nullptr) {}; - CallBackInfo callBackInfo; - napi_async_work asyncWork; - napi_value result; - int32_t authType; - int32_t key; - std::vector setInfo; - int32_t setResult; -}; - -struct ExecuteInfo { - bool isPromise; - napi_env env; - std::string type; - std::string level; - napi_ref callbackRef; - napi_deferred deferred; - napi_value promise; - int32_t result; -}; - -struct AuthInfo { - AuthInfo() : asyncWork(nullptr) {}; - CallBackInfo callBackInfo; - napi_callback_info info; - napi_async_work asyncWork; - napi_value jsFunction; - napi_value onResultCallBack; - napi_value onAcquireInfoCallBack; - napi_ref onResult; - napi_ref onAcquireInfo; - napi_value onResultData[ARGS_TWO]; - napi_value onAcquireInfoData[ARGS_THREE]; - uint64_t challenge; - int32_t authType; - int32_t authTrustLevel; - int32_t result; - std::vector token; - uint32_t remainTimes; - uint32_t freezingTime; -}; - -struct AuthUserInfo { - AuthUserInfo() : asyncWork(nullptr) {}; - CallBackInfo callBackInfo; - napi_callback_info info; - napi_async_work asyncWork; - napi_value jsFunction; - napi_ref onResult; - napi_ref onAcquireInfo; - napi_value onResultCallBack; - napi_value onAcquireInfoCallBack; - napi_value onResultData[ARGS_TWO]; - napi_value onAcquireInfoData[ARGS_THREE]; - int32_t userId; - 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 -} // namespace OHOS -#endif /* OHOS_USERAUTH_COMMON_H */ +/* + * Copyright (c) 2021 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. + */ +#ifndef OHOS_USERAUTH_COMMON_H +#define OHOS_USERAUTH_COMMON_H + +#include +#include + +#include "napi/native_api.h" +#include "napi/native_common.h" + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +#define ARGS_MAX_COUNT 10 +#define ARGS_ASYNC_COUNT 1 +#define GET_VALUE_ERROR (-1) +#define NAPI_GET_STRING_SIZE 128 + +#define ARGS_ONE 1 +#define ARGS_TWO 2 +#define ARGS_THREE 3 +#define ARGS_FOUR 4 +#define ARGS_FIVE 5 +#define ARGS_SIX 6 +#define ARGS_SEVEN 7 +#define ARGS_EIGHT 8 +#define ARGS_NINE 9 +#define ARGS_TEN 10 + +#define PARAM0 0 +#define PARAM1 1 +#define PARAM2 2 +#define PARAM3 3 +#define PARAM4 4 +#define PARAM5 5 +#define PARAM6 6 +#define PARAM7 7 +#define PARAM8 8 +#define PARAM9 9 +#define PARAM10 10 + +struct CallBackInfo { + napi_env env; + napi_ref callBack = 0; + napi_deferred deferred; +}; + +struct GetPropertyInfo { + GetPropertyInfo() : result(nullptr) {}; + CallBackInfo callBackInfo; + napi_value result; + int32_t authType; + std::vector keys; + int32_t getResult; + uint64_t authSubType; + uint32_t remainTimes; + uint32_t freezingTime; +}; + +struct SetPropertyInfo { + SetPropertyInfo() : asyncWork(nullptr), result(nullptr) {}; + CallBackInfo callBackInfo; + napi_async_work asyncWork; + napi_value result; + int32_t authType; + uint32_t key; + std::vector setInfo; + int32_t setResult; +}; + +struct ExecuteInfo { + bool isPromise; + napi_env env; + std::string type; + std::string level; + napi_ref callbackRef; + napi_deferred deferred; + napi_value promise; + int32_t result; +}; + +struct AuthInfo { + AuthInfo() : asyncWork(nullptr) {}; + CallBackInfo callBackInfo; + napi_callback_info info; + napi_async_work asyncWork; + napi_value jsFunction; + napi_value onResultCallBack; + napi_value onAcquireInfoCallBack; + napi_ref onResult; + napi_ref onAcquireInfo; + napi_value onResultData[ARGS_TWO]; + napi_value onAcquireInfoData[ARGS_THREE]; + uint64_t challenge; + int32_t authType; + int32_t authTrustLevel; + int32_t result; + std::vector token; + uint32_t remainTimes; + uint32_t freezingTime; +}; + +struct AuthUserInfo { + AuthUserInfo() : asyncWork(nullptr) {}; + CallBackInfo callBackInfo; + napi_callback_info info; + napi_async_work asyncWork; + napi_value jsFunction; + napi_ref onResult; + napi_ref onAcquireInfo; + napi_value onResultCallBack; + napi_value onAcquireInfoCallBack; + napi_value onResultData[ARGS_TWO]; + napi_value onAcquireInfoData[ARGS_THREE]; + int32_t userId; + 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 +} // namespace OHOS +#endif /* OHOS_USERAUTH_COMMON_H */ diff --git a/interfaces/innerkits/napi/include/auth_hilog_wrapper.h b/interfaces/innerkits/napi/include/auth_hilog_wrapper.h deleted file mode 100755 index f8e944e1c..000000000 --- a/interfaces/innerkits/napi/include/auth_hilog_wrapper.h +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (C) 2021 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. - */ - -#ifndef HILOG_WRAPPER_H -#define HILOG_WRAPPER_H - -#define CONFIG_HILOG -#ifdef CONFIG_HILOG - -#include "hilog/log.h" - -#ifdef HILOG_FATAL -#undef HILOG_FATAL -#endif - -#ifdef HILOG_ERROR -#undef HILOG_ERROR -#endif - -#ifdef HILOG_WARN -#undef HILOG_WARN -#endif - -#ifdef HILOG_INFO -#undef HILOG_INFO -#endif - -#ifdef HILOG_DEBUG -#undef HILOG_DEBUG -#endif - -static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = {LOG_CORE, 0, "UserAuthNapi===>"}; - -#define HILOG_FATAL(...) (void)OHOS::HiviewDFX::HiLog::Fatal(LOG_LABEL, __VA_ARGS__) -#define HILOG_ERROR(...) (void)OHOS::HiviewDFX::HiLog::Error(LOG_LABEL, __VA_ARGS__) -#define HILOG_WARN(...) (void)OHOS::HiviewDFX::HiLog::Warn(LOG_LABEL, __VA_ARGS__) -#define HILOG_INFO(...) (void)OHOS::HiviewDFX::HiLog::Info(LOG_LABEL, __VA_ARGS__) -#define HILOG_DEBUG(...) (void)OHOS::HiviewDFX::HiLog::Debug(LOG_LABEL, __VA_ARGS__) -#else - -#define HILOG_FATAL(...) -#define HILOG_ERROR(...) -#define HILOG_WARN(...) -#define HILOG_INFO(...) -#define HILOG_DEBUG(...) - -#endif // CONFIG_HILOG - -#endif // HILOG_WRAPPER_H \ No newline at end of file diff --git a/interfaces/innerkits/napi/include/auth_object.h b/interfaces/innerkits/napi/include/auth_object.h index c1fbf65c9..985c80b5d 100755 --- a/interfaces/innerkits/napi/include/auth_object.h +++ b/interfaces/innerkits/napi/include/auth_object.h @@ -1,54 +1,54 @@ -/* - * Copyright (c) 2021 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. - */ - -#ifndef AUTH_OBJECT_H -#define AUTH_OBJECT_H - -#include - -namespace OHOS { -namespace UserIAM { -namespace UserAuth { -class Napi_ExecutorProperty { -public: - int32_t result_ = 0; - uint64_t authSubType_ = 0; - uint32_t remainTimes_ = 0; - uint32_t freezingTime_ = 0; -}; - -class Napi_SetPropertyRequest { -public: - int32_t authType_ = 0; - int32_t key_ = 0; - std::vector setInfo_; -}; - -class Napi_GetPropertyRequest { -public: - int32_t authType_ = 0; - std::vector keys_; -}; - -class Napi_AuthResult { -public: - std::vector token_; - uint32_t remainTimes_; - uint32_t freezingTime_; -}; -} // namespace UserAuth -} // namespace UserIAM -} // namespace OHOS -#endif // AUTH_OBJECT_H +/* + * Copyright (c) 2021 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. + */ + +#ifndef AUTH_OBJECT_H +#define AUTH_OBJECT_H + +#include + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +class Napi_ExecutorProperty { +public: + int32_t result_ = 0; + uint64_t authSubType_ = 0; + uint32_t remainTimes_ = 0; + uint32_t freezingTime_ = 0; +}; + +class Napi_SetPropertyRequest { +public: + int32_t authType_ = 0; + uint32_t key_ = 0; + std::vector setInfo_; +}; + +class Napi_GetPropertyRequest { +public: + int32_t authType_ = 0; + std::vector keys_; +}; + +class Napi_AuthResult { +public: + std::vector token_; + uint32_t remainTimes_; + uint32_t freezingTime_; +}; +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS +#endif // AUTH_OBJECT_H diff --git a/interfaces/innerkits/napi/include/user_auth_helper.h b/interfaces/innerkits/napi/include/user_auth_helper.h index ebe0b46e1..7ec227d74 100755 --- a/interfaces/innerkits/napi/include/user_auth_helper.h +++ b/interfaces/innerkits/napi/include/user_auth_helper.h @@ -1,126 +1,126 @@ -/* - * Copyright (c) 2021 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. - */ -#ifndef FACERECOGNITION_PIN_AUTH_HELPER_H -#define FACERECOGNITION_PIN_AUTH_HELPER_H - -#include "napi/native_api.h" -#include "napi/native_common.h" - -namespace OHOS { -namespace UserIAM { -namespace UserAuth { -/** - * @brief Napi initialization - * - * @param env - * @param exports - */ -void Init(napi_env env, napi_value exports); - -/** - * @brief Get the Ctor object - * - * @param env - * @return napi_value UserAuth Instance - */ -napi_value GetCtor(napi_env env); -/** - * @brief Construction method - * - * @param env - * @param info - * @return napi_value UserAuth Instance - */ -napi_value Constructor(napi_env env, napi_callback_info info); - -/** - * @brief Instance passed to context - * - * @param env - * @param info - * @return napi_value Instance - */ -napi_value UserAuthServiceConstructor(napi_env env, napi_callback_info info); - -/** - * @brief Get the Version object - * - * @param env - * @param info - * @return napi_value Specific version number results - */ -napi_value GetVersion(napi_env env, napi_callback_info info); - -/** - * @brief Get the Availabe Status object - * - * @param env - * @param info - * @return napi_value Verify that the certification capability is available - */ -napi_value GetAvailableStatus(napi_env env, napi_callback_info info); - -/** - * @brief Get the Property object - * - * @param env - * @param info - * @return napi_value It supports querying subclasses / remaining authentication times / freezing time - */ -napi_value GetProperty(napi_env env, napi_callback_info info); - -/** - * @brief Set the Property object - * - * @param env - * @param info - * @return napi_value Set properties: can be used to initialize algorithms - */ -napi_value SetProperty(napi_env env, napi_callback_info info); - -/** - * @brief user authentication - * - * @param env - * @param info - * @return napi_value Enter the challenge value, authentication method, trust level and callback, and return the result - * and acquireinfo through the callback - */ -napi_value Auth(napi_env env, napi_callback_info info); - -napi_value Execute(napi_env env, napi_callback_info info); - -/** - * @brief user authentication - * - * @param env - * @param info - * @return napi_value Pass in the user ID, challenge value, authentication method, trust level and callback, and return - * the result acquireinfo through the callback - */ -napi_value AuthUser(napi_env env, napi_callback_info info); - -/** - * @brief Cancel authentication - * - * @param env - * @param info - * @return napi_value success or fail - */ -napi_value CancelAuth(napi_env env, napi_callback_info info); -} // namespace UserAuth -} // namespace UserIAM -} // namespace OHOS -#endif // FACERECOGNITION_PIN_AUTH_HELPER_H +/* + * Copyright (c) 2021 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. + */ +#ifndef FACERECOGNITION_PIN_AUTH_HELPER_H +#define FACERECOGNITION_PIN_AUTH_HELPER_H + +#include "napi/native_api.h" +#include "napi/native_common.h" + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +/** + * @brief Napi initialization + * + * @param env + * @param exports + */ +void Init(napi_env env, napi_value exports); + +/** + * @brief Get the Ctor object + * + * @param env + * @return napi_value UserAuth Instance + */ +napi_value GetCtor(napi_env env); +/** + * @brief Construction method + * + * @param env + * @param info + * @return napi_value UserAuth Instance + */ +napi_value Constructor(napi_env env, napi_callback_info info); + +/** + * @brief Instance passed to context + * + * @param env + * @param info + * @return napi_value UserAuth Instance + */ +napi_value UserAuthServiceConstructor(napi_env env, napi_callback_info info); + +/** + * @brief Get the Version object + * + * @param env + * @param info + * @return napi_value Specific version number results + */ +napi_value GetVersion(napi_env env, napi_callback_info info); + +/** + * @brief Get the Availabe Status object + * + * @param env + * @param info + * @return napi_value Verify that the certification capability is available + */ +napi_value GetAvailableStatus(napi_env env, napi_callback_info info); + +/** + * @brief Get the Property object + * + * @param env + * @param info + * @return napi_value It supports querying subclasses / remaining authentication times / freezing time + */ +napi_value GetProperty(napi_env env, napi_callback_info info); + +/** + * @brief Set the Property object + * + * @param env + * @param info + * @return napi_value Set properties: can be used to initialize algorithms + */ +napi_value SetProperty(napi_env env, napi_callback_info info); + +/** + * @brief user authentication + * + * @param env + * @param info + * @return napi_value Enter the challenge value, authentication method, trust level and callback, and return the result + * and acquireinfo through the callback + */ +napi_value Auth(napi_env env, napi_callback_info info); + +napi_value Execute(napi_env env, napi_callback_info info); + +/** + * @brief user authentication + * + * @param env + * @param info + * @return napi_value Pass in the user ID, challenge value, authentication method, trust level and callback, and return + * the result acquireinfo through the callback + */ +napi_value AuthUser(napi_env env, napi_callback_info info); + +/** + * @brief Cancel authentication + * + * @param env + * @param info + * @return napi_value success or fail + */ +napi_value CancelAuth(napi_env env, napi_callback_info info); +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS +#endif // FACERECOGNITION_PIN_AUTH_HELPER_H diff --git a/interfaces/innerkits/napi/src/auth_build.cpp b/interfaces/innerkits/napi/src/auth_build.cpp index cd5c85bda..f31343337 100755 --- a/interfaces/innerkits/napi/src/auth_build.cpp +++ b/interfaces/innerkits/napi/src/auth_build.cpp @@ -1,128 +1,128 @@ -/* - * Copyright (c) 2021 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 "auth_build.h" - -#include "auth_common.h" -#include "auth_hilog_wrapper.h" -#include "auth_object.h" - -namespace OHOS { -namespace UserIAM { -namespace UserAuth { -AuthBuild::AuthBuild(void) -{ -} -AuthBuild::~AuthBuild() -{ -} - -Napi_SetPropertyRequest AuthBuild::SetPropertyRequestBuild(napi_env env, napi_value object) -{ - Napi_SetPropertyRequest request; - if (object == nullptr) { - HILOG_ERROR("SetPropertyRequestBuild object is null "); - return request; - } - request.authType_ = convert.GetInt32ValueByKey(env, object, "authType"); - request.key_ = convert.GetInt32ValueByKey(env, object, "key"); - request.setInfo_ = convert.NapiGetValueUint8Array(env, object, "setInfo"); - HILOG_INFO(" AuthBuild::SetPropertyRequestBuild authType = %{public}d", request.authType_); - HILOG_INFO(" AuthBuild::SetPropertyRequestBuild key = %{public}d", request.key_); - return request; -} - -Napi_GetPropertyRequest AuthBuild::GetPropertyRequestBuild(napi_env env, napi_value object) -{ - Napi_GetPropertyRequest request; - if (object == nullptr) { - HILOG_ERROR("GetPropertyRequestBuild object is null "); - return request; - } - request.authType_ = convert.GetInt32ValueByKey(env, object, "authType"); - request.keys_ = convert.GetInt32ArrayValueByKey(env, object, "keys"); - HILOG_INFO(" AuthBuild::GetPropertyRequestBuild authType = %{public}d", request.authType_); - return request; -} - -bool AuthBuild::NapiTypeObject(napi_env env, napi_value value) -{ - if (value == nullptr) { - return false; - } - napi_valuetype isObject = convert.GetType(env, value); - if (isObject == napi_object) { - return true; - } - return false; -} - -bool AuthBuild::NapiTypeNumber(napi_env env, napi_value value) -{ - if (value == nullptr) { - return false; - } - napi_valuetype isNumber = convert.GetType(env, value); - if (isNumber == napi_number) { - return true; - } - return false; -} - -uint64_t AuthBuild::GetUint8ArrayTo64(napi_env env, napi_value value) -{ - napi_typedarray_type arraytype; - size_t length = 0; - napi_value buffer = nullptr; - size_t offset = 0; - uint8_t *data = nullptr; - bool isTypedArray = false; - napi_is_typedarray(env, value, &isTypedArray); - if (!isTypedArray) { - HILOG_ERROR("GetUint8ArrayTo64 value is not typedarray"); - return 0; - } - napi_get_typedarray_info(env, value, &arraytype, &length, reinterpret_cast(&data), &buffer, &offset); - if (arraytype != napi_uint8_array) { - HILOG_ERROR("GetUint8ArrayTo64 js value is not uint8Array"); - return 0; - } - if (offset != 0) { - HILOG_ERROR("offset is %{public}d", offset); - return 0; - } - std::vector result(data, data + length); - uint8_t tmp[sizeof(uint64_t)]; - for (uint32_t i = 0; i < sizeof(uint64_t); i++) { - tmp[i] = result[i]; - HILOG_INFO("GetUint8ArrayTo64 result is %{public}d", (unsigned)result[i]); - } - uint64_t *re = static_cast(static_cast(tmp)); - HILOG_INFO("GetUint8ArrayTo64 resultUint64 is %{public}llu", *re); - return *re; -} - -int AuthBuild::NapiGetValueInt(napi_env env, napi_value value) -{ - return convert.NapiGetValueInt(env, value); -} - -napi_value AuthBuild::Uint64ToUint8Array(napi_env env, uint64_t value) -{ - return convert.Uint64ToUint8Napi(env, value); -} -} // namespace UserAuth -} // namespace UserIAM -} // namespace OHOS +/* + * Copyright (c) 2021 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 + +#include "auth_common.h" +#include "userauth_hilog_wrapper.h" +#include "auth_object.h" +#include "auth_build.h" + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +AuthBuild::AuthBuild(void) +{ +} +AuthBuild::~AuthBuild() +{ +} + +Napi_SetPropertyRequest AuthBuild::SetPropertyRequestBuild(napi_env env, napi_value object) +{ + Napi_SetPropertyRequest request; + if (object == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "SetPropertyRequestBuild object is null"); + return request; + } + request.authType_ = convert.GetInt32ValueByKey(env, object, "authType"); + request.key_ = convert.GetInt32ValueByKey(env, object, "key"); + request.setInfo_ = convert.NapiGetValueUint8Array(env, object, "setInfo"); + USERAUTH_HILOGI(MODULE_JS_NAPI, "AuthBuild::SetPropertyRequestBuild authType = %{public}d, key = %{public}d", + request.authType_, request.key_); + return request; +} + +Napi_GetPropertyRequest AuthBuild::GetPropertyRequestBuild(napi_env env, napi_value object) +{ + Napi_GetPropertyRequest request; + if (object == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "GetPropertyRequestBuild object is null"); + return request; + } + request.authType_ = convert.GetInt32ValueByKey(env, object, "authType"); + request.keys_ = convert.GetInt32ArrayValueByKey(env, object, "keys"); + USERAUTH_HILOGI(MODULE_JS_NAPI, "AuthBuild::GetPropertyRequestBuild authType = %{public}d", request.authType_); + return request; +} + +bool AuthBuild::NapiTypeObject(napi_env env, napi_value value) +{ + if (value == nullptr) { + return false; + } + napi_valuetype isObject = convert.GetType(env, value); + if (isObject == napi_object) { + return true; + } + return false; +} + +bool AuthBuild::NapiTypeNumber(napi_env env, napi_value value) +{ + if (value == nullptr) { + return false; + } + napi_valuetype isNumber = convert.GetType(env, value); + if (isNumber == napi_number) { + return true; + } + return false; +} + +uint64_t AuthBuild::GetUint8ArrayTo64(napi_env env, napi_value value) +{ + napi_typedarray_type arraytype; + size_t length = 0; + napi_value buffer = nullptr; + size_t offset = 0; + uint8_t *data = nullptr; + bool isTypedArray = false; + napi_is_typedarray(env, value, &isTypedArray); + if (!isTypedArray) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "GetUint8ArrayTo64 value is not typedarray"); + return 0; + } + napi_get_typedarray_info(env, value, &arraytype, &length, reinterpret_cast(&data), &buffer, &offset); + if (arraytype != napi_uint8_array) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "GetUint8ArrayTo64 js value is not uint8Array"); + return 0; + } + if (offset != 0) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "offset is %{public}zu", offset); + return 0; + } + std::vector result(data, data + length); + uint8_t tmp[sizeof(uint64_t)]; + for (uint32_t i = 0; i < sizeof(uint64_t); i++) { + tmp[i] = result[i]; + USERAUTH_HILOGI(MODULE_JS_NAPI, "GetUint8ArrayTo64 result is %{public}u", (unsigned)result[i]); + } + uint64_t *re = static_cast(static_cast(tmp)); + USERAUTH_HILOGI(MODULE_JS_NAPI, "GetUint8ArrayTo64 resultUint64 is %{public}" PRIu64 "", *re); + return *re; +} + +int AuthBuild::NapiGetValueInt(napi_env env, napi_value value) +{ + return convert.NapiGetValueInt(env, value); +} + +napi_value AuthBuild::Uint64ToUint8Array(napi_env env, uint64_t value) +{ + return convert.Uint64ToUint8Napi(env, value); +} +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS diff --git a/interfaces/innerkits/napi/src/auth_native_module.cpp b/interfaces/innerkits/napi/src/auth_native_module.cpp deleted file mode 100755 index 4b9b1abb0..000000000 --- a/interfaces/innerkits/napi/src/auth_native_module.cpp +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (c) 2021 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 - -#include "user_auth_helper.h" - -namespace OHOS { -namespace UserIAM { -namespace UserAuth { -static napi_value ModuleInit(napi_env env, napi_value exports) -{ - OHOS::UserIAM::UserAuth::Init(env, exports); - return exports; -} -extern "C" __attribute__((constructor)) void RegisterModule(void) -{ - napi_module module = {.nm_version = 1, // NAPI v1 - .nm_flags = 0, // normal - .nm_filename = nullptr, - .nm_register_func = ModuleInit, -#ifdef USER_AUTH_FOR_API6 - .nm_modname = "userIAM.userAuth", -#else - .nm_modname = "UserAuth", -#endif - .nm_priv = nullptr, - .reserved = {}}; - napi_module_register(&module); -} -} // namespace UserAuth -} // namespace UserIAM -} // namespace OHOS diff --git a/interfaces/innerkits/napi/src/authapi_callback.cpp b/interfaces/innerkits/napi/src/authapi_callback.cpp index 80bd4459b..31318456b 100755 --- a/interfaces/innerkits/napi/src/authapi_callback.cpp +++ b/interfaces/innerkits/napi/src/authapi_callback.cpp @@ -1,565 +1,560 @@ -/* - * Copyright (c) 2021 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 "authapi_callback.h" - -#include -#include "securec.h" - -#include "auth_hilog_wrapper.h" - -namespace OHOS { -namespace UserIAM { -namespace UserAuth { -AuthApiCallback::AuthApiCallback(AuthInfo *authInfo) -{ - authInfo_ = authInfo; - userInfo_ = nullptr; - executeInfo_ = nullptr; -} - -AuthApiCallback::AuthApiCallback(AuthUserInfo *userInfo) -{ - authInfo_ = nullptr; - userInfo_ = userInfo; - executeInfo_ = nullptr; -} - -AuthApiCallback::AuthApiCallback(ExecuteInfo *executeInfo) -{ - authInfo_ = nullptr; - userInfo_ = nullptr; - executeInfo_ = executeInfo; -} - -AuthApiCallback::~AuthApiCallback() -{ -} - -napi_value AuthApiCallback::Uint8ArrayToNapi(napi_env env, std::vector value) -{ - int size = value.size(); - HILOG_INFO("Uint8ArrayToNapi size = %{public}d", size); - napi_value out = nullptr; - void *data = nullptr; - napi_value buffer = nullptr; - NAPI_CALL(env, napi_create_arraybuffer(env, value.size(), &data, &buffer)); - if (memcpy_s(data, value.size(), value.data(), value.size()) != 0) { - HILOG_ERROR("AuthApiCallback Uint8ArrayToNapi error"); - } - NAPI_CALL(env, napi_create_typedarray(env, napi_uint8_array, value.size(), buffer, 0, &out)); - return out; -} - -napi_value AuthApiCallback::BuildOnResult( - napi_env env, uint32_t remainTimes, uint32_t freezingTime, std::vector token) -{ - napi_value jsObject = nullptr; - NAPI_CALL(env, napi_create_object(env, &jsObject)); - - napi_value remainTimesValue = 0; - NAPI_CALL(env, napi_create_uint32(env, remainTimes, &remainTimesValue)); - NAPI_CALL(env, napi_set_named_property(env, jsObject, "remainTimes", remainTimesValue)); - - napi_value freezingTimeValue = 0; - NAPI_CALL(env, napi_create_uint32(env, freezingTime, &freezingTimeValue)); - NAPI_CALL(env, napi_set_named_property(env, jsObject, "freezingTime", freezingTimeValue)); - - napi_value jsToken = Uint8ArrayToNapi(env, token); - NAPI_CALL(env, napi_set_named_property(env, jsObject, "token", jsToken)); - return jsObject; -} - -void AuthApiCallback::OnAuthAcquireInfo(AcquireInfoInner *acquireInfoInner) -{ - 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; - 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_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"); - } - 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) { - AcquireInfoInner *acquireInfoInner = new (std::nothrow) AcquireInfoInner(); - if (acquireInfoInner == nullptr) { - HILOG_ERROR("acquireInfoInner is null"); - return; - } - 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); -} - -static void OnExecuteResultWork(uv_work_t *work, int status) -{ - HILOG_INFO("Do OnExecuteResultWork"); - ExecuteInfo *executeInfo = reinterpret_cast(work->data); - if (executeInfo == nullptr) { - HILOG_ERROR("executeInfo is null"); - delete work; - return; - } - napi_env env = executeInfo->env; - napi_value result; - if (napi_create_int32(env, executeInfo->result, &result) != napi_ok) { - HILOG_ERROR("napi_create_int32 faild"); - delete work; - delete executeInfo; - return; - } - napi_value undefined; - napi_get_undefined(env, &undefined); - if (executeInfo->isPromise) { - HILOG_INFO("do promise %{public}d", napi_resolve_deferred(env, executeInfo->deferred, result)); - } else { - napi_value callback; - napi_get_reference_value(env, executeInfo->callbackRef, &callback); - napi_value callResult = nullptr; - HILOG_INFO("do callback %{public}d", napi_call_function(env, undefined, callback, 1, &result, &callResult)); - } - delete executeInfo; - delete work; -} - -void AuthApiCallback::OnExecuteResult(const int32_t result) -{ - HILOG_INFO("AuthApiCallback OnExecuteResult start"); - uv_loop_s *loop(nullptr); - napi_get_uv_event_loop(executeInfo_->env, &loop); - if (loop == nullptr) { - HILOG_ERROR("loop is null"); - delete executeInfo_; - executeInfo_ = nullptr; - return; - } - uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { - HILOG_ERROR("work is null"); - delete executeInfo_; - executeInfo_ = nullptr; - return; - } - executeInfo_->result = result; - work->data = reinterpret_cast(executeInfo_); - executeInfo_ = nullptr; - uv_queue_work(loop, work, [] (uv_work_t *work) {}, OnExecuteResultWork); -} - -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); - if (userInfo_ != nullptr) { - OnUserAuthResult(result, extraInfo); - } else { - HILOG_ERROR("AuthApiCallback onResult userInfo_ is nullptr "); - } - if (authInfo_ != nullptr) { - OnAuthResult(result, extraInfo); - } else { - HILOG_ERROR("AuthApiCallback onResult authInfo_ is nullptr "); - } - if (executeInfo_ != nullptr) { - OnExecuteResult(result); - } else { - HILOG_ERROR("AuthApiCallback onResult executeInfo_ is nullptr "); - } - HILOG_INFO("AuthApiCallback onResult end"); -} - -GetPropApiCallback::GetPropApiCallback(GetPropertyInfo *getPropertyInfo) -{ - getPropertyInfo_ = getPropertyInfo; -} - -GetPropApiCallback::~GetPropApiCallback() -{ -} - -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] = GetPropApiCallback::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; -} - -napi_value GetPropApiCallback::BuildExecutorProperty( - napi_env env, int32_t result, uint32_t remainTimes, uint32_t freezingTime, uint64_t authSubType) -{ - napi_value jsObject = nullptr; - NAPI_CALL(env, napi_create_object(env, &jsObject)); - - napi_value resultValue = 0; - NAPI_CALL(env, napi_create_int32(env, result, &resultValue)); - NAPI_CALL(env, napi_set_named_property(env, jsObject, "result", resultValue)); - - napi_value remainTimesValue = 0; - NAPI_CALL(env, napi_create_uint32(env, remainTimes, &remainTimesValue)); - NAPI_CALL(env, napi_set_named_property(env, jsObject, "remainTimes", remainTimesValue)); - - napi_value freezingTimeValue = 0; - NAPI_CALL(env, napi_create_uint32(env, freezingTime, &freezingTimeValue)); - NAPI_CALL(env, napi_set_named_property(env, jsObject, "freezingTime", freezingTimeValue)); - - 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; -} - -void GetPropApiCallback::onGetProperty(const ExecutorProperty result) -{ - HILOG_INFO("GetPropApiCallback onGetProperty 1 = %{public}d", result.result); - HILOG_INFO("GetPropApiCallback onGetProperty 2 = %{public}llu", result.authSubType); - HILOG_INFO("GetPropApiCallback onGetProperty 3 = %{public}u", result.remainTimes); - HILOG_INFO("GetPropApiCallback onGetProperty 4 = %{public}u", result.freezingTime); - if (getPropertyInfo_ == nullptr) { - HILOG_ERROR("GetPropApiCallback onGetProperty 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; - 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); -} - -SetPropApiCallback::SetPropApiCallback(SetPropertyInfo *setPropertyInfo) -{ - setPropertyInfo_ = setPropertyInfo; -} - -SetPropApiCallback::~SetPropApiCallback() -{ -} - -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 SetPropApiCallback::onSetProperty(const int32_t result) -{ - HILOG_INFO("onSetProperty start = %{public}d", result); - if (setPropertyInfo_ != nullptr) { - 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; - } - setPropertyInfo_->setResult = result; - work->data = reinterpret_cast(setPropertyInfo_); - setPropertyInfo_ = nullptr; - uv_queue_work(loop, work, [] (uv_work_t *work) {}, SetExecutorPropertyCallback); -} -} // namespace UserAuth -} // namespace UserIAM -} // namespace OHOS +/* + * Copyright (c) 2021 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 +#include + +#include "securec.h" +#include "userauth_hilog_wrapper.h" +#include "authapi_callback.h" + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +AuthApiCallback::AuthApiCallback(AuthInfo *authInfo) +{ + authInfo_ = authInfo; + userInfo_ = nullptr; + executeInfo_ = nullptr; +} + +AuthApiCallback::AuthApiCallback(AuthUserInfo *userInfo) +{ + authInfo_ = nullptr; + userInfo_ = userInfo; + executeInfo_ = nullptr; +} + +AuthApiCallback::AuthApiCallback(ExecuteInfo *executeInfo) +{ + authInfo_ = nullptr; + userInfo_ = nullptr; + executeInfo_ = executeInfo; +} + +AuthApiCallback::~AuthApiCallback() +{ +} + +napi_value AuthApiCallback::Uint8ArrayToNapi(napi_env env, std::vector value) +{ + int size = value.size(); + USERAUTH_HILOGI(MODULE_JS_NAPI, "Uint8ArrayToNapi size = %{public}d", size); + napi_value out = nullptr; + void *data = nullptr; + napi_value buffer = nullptr; + NAPI_CALL(env, napi_create_arraybuffer(env, value.size(), &data, &buffer)); + if (memcpy_s(data, value.size(), value.data(), value.size()) != 0) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "AuthApiCallback Uint8ArrayToNapi error"); + } + NAPI_CALL(env, napi_create_typedarray(env, napi_uint8_array, value.size(), buffer, 0, &out)); + return out; +} + +napi_value AuthApiCallback::BuildOnResult( + napi_env env, uint32_t remainTimes, uint32_t freezingTime, std::vector token) +{ + napi_value jsObject = nullptr; + NAPI_CALL(env, napi_create_object(env, &jsObject)); + + napi_value remainTimesValue = 0; + NAPI_CALL(env, napi_create_uint32(env, remainTimes, &remainTimesValue)); + NAPI_CALL(env, napi_set_named_property(env, jsObject, "remainTimes", remainTimesValue)); + + napi_value freezingTimeValue = 0; + NAPI_CALL(env, napi_create_uint32(env, freezingTime, &freezingTimeValue)); + NAPI_CALL(env, napi_set_named_property(env, jsObject, "freezingTime", freezingTimeValue)); + + napi_value jsToken = Uint8ArrayToNapi(env, token); + NAPI_CALL(env, napi_set_named_property(env, jsObject, "token", jsToken)); + return jsObject; +} + +void AuthApiCallback::OnAuthAcquireInfo(AcquireInfoInner *acquireInfoInner) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "AuthApiCallback OnAuthAcquireInfo start"); + uv_loop_s *loop(nullptr); + napi_get_uv_event_loop(authInfo_->callBackInfo.env, &loop); + if (loop == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "loop is null"); + delete acquireInfoInner; + return; + } + uv_work_t *work = new (std::nothrow) uv_work_t; + if (work == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "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) { + USERAUTH_HILOGI(MODULE_JS_NAPI, "Do OnAuthAcquireInfo work"); + AcquireInfoInner *acquireInfoInner = reinterpret_cast(work->data); + if (acquireInfoInner == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "authInfo is null"); + delete work; + return; + } + napi_env env = acquireInfoInner->env; + napi_value returnOnAcquire = nullptr; + napi_value callback; + napi_status napiStatus = napi_get_reference_value(env, acquireInfoInner->onAcquireInfo, &callback); + if (napiStatus != napi_ok) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "napi_get_reference_value faild"); + delete acquireInfoInner; + delete work; + return; + } + 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) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "napi_call_function faild"); + } + 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) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "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 { + USERAUTH_HILOGE(MODULE_JS_NAPI, "AuthApiCallback onAcquireInfo userInfo_ is nullptr"); + } + + if (authInfo_ != nullptr) { + AcquireInfoInner *acquireInfoInner = new (std::nothrow) AcquireInfoInner(); + if (acquireInfoInner == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "acquireInfoInner is null"); + return; + } + 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 { + USERAUTH_HILOGE(MODULE_JS_NAPI, "AuthApiCallback onAcquireInfo authInfo_ is nullptr"); + } + USERAUTH_HILOGI(MODULE_JS_NAPI, "AuthApiCallback onAcquireInfo end"); +} + +static void OnUserAuthResultWork(uv_work_t *work, int status) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "Do OnUserAuthResult work"); + AuthUserInfo *userInfo = reinterpret_cast(work->data); + if (userInfo == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "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) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "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) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "AuthApiCallback OnUserAuthResult start"); + uv_loop_s *loop(nullptr); + napi_get_uv_event_loop(userInfo_->callBackInfo.env, &loop); + if (loop == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "loop is null"); + delete userInfo_; + userInfo_ = nullptr; + return; + } + uv_work_t *work = new (std::nothrow) uv_work_t; + if (work == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "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) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "Do OnAuthResult work"); + AuthInfo *authInfo = reinterpret_cast(work->data); + if (authInfo == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "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) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "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) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "AuthApiCallback OnAuthResult start"); + uv_loop_s *loop(nullptr); + napi_get_uv_event_loop(authInfo_->callBackInfo.env, &loop); + if (loop == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "loop is null"); + delete authInfo_; + authInfo_ = nullptr; + return; + } + uv_work_t *work = new (std::nothrow) uv_work_t; + if (work == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "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); +} + +static void OnExecuteResultWork(uv_work_t *work, int status) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "Do OnExecuteResultWork"); + ExecuteInfo *executeInfo = reinterpret_cast(work->data); + if (executeInfo == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "executeInfo is null"); + delete work; + return; + } + napi_env env = executeInfo->env; + napi_value result; + if (napi_create_int32(env, executeInfo->result, &result) != napi_ok) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "napi_create_int32 faild"); + delete work; + delete executeInfo; + return; + } + napi_value undefined; + napi_get_undefined(env, &undefined); + if (executeInfo->isPromise) { + USERAUTH_HILOGE(MODULE_JS_NAPI, + "do promise %{public}d", napi_resolve_deferred(env, executeInfo->deferred, result)); + } else { + napi_value callback; + napi_get_reference_value(env, executeInfo->callbackRef, &callback); + napi_value callResult = nullptr; + USERAUTH_HILOGI(MODULE_JS_NAPI, + "do callback %{public}d", napi_call_function(env, undefined, callback, 1, &result, &callResult)); + } + delete executeInfo; + delete work; +} + +void AuthApiCallback::OnExecuteResult(const int32_t result) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "AuthApiCallback OnExecuteResult start"); + uv_loop_s *loop(nullptr); + napi_get_uv_event_loop(executeInfo_->env, &loop); + if (loop == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "loop is null"); + delete executeInfo_; + executeInfo_ = nullptr; + return; + } + uv_work_t *work = new (std::nothrow) uv_work_t; + if (work == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "work is null"); + delete executeInfo_; + executeInfo_ = nullptr; + return; + } + executeInfo_->result = result; + work->data = reinterpret_cast(executeInfo_); + executeInfo_ = nullptr; + uv_queue_work(loop, work, [] (uv_work_t *work) {}, OnExecuteResultWork); +} + +void AuthApiCallback::onResult(const int32_t result, const AuthResult extraInfo) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "AuthApiCallback onResult start result = %{public}d", result); + if (userInfo_ != nullptr) { + OnUserAuthResult(result, extraInfo); + } else { + USERAUTH_HILOGE(MODULE_JS_NAPI, "AuthApiCallback onResult userInfo_ is nullptr"); + } + if (authInfo_ != nullptr) { + OnAuthResult(result, extraInfo); + } else { + USERAUTH_HILOGE(MODULE_JS_NAPI, "AuthApiCallback onResult authInfo_ is nullptr"); + } + if (executeInfo_ != nullptr) { + OnExecuteResult(result); + } else { + USERAUTH_HILOGE(MODULE_JS_NAPI, "AuthApiCallback onResult executeInfo_ is nullptr "); + } + USERAUTH_HILOGI(MODULE_JS_NAPI, "AuthApiCallback onResult end"); +} + +GetPropApiCallback::GetPropApiCallback(GetPropertyInfo *getPropertyInfo) +{ + getPropertyInfo_ = getPropertyInfo; +} + +GetPropApiCallback::~GetPropApiCallback() +{ +} + +static void GetPropertyInfoCallback(uv_work_t* work, int status) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "Do OnAuthAcquireInfo work"); + GetPropertyInfo *getPropertyInfo = reinterpret_cast(work->data); + if (getPropertyInfo == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "getPropertyInfo is null"); + delete work; + return; + } + napi_env env = getPropertyInfo->callBackInfo.env; + napi_value resultData[PARAM1]; + resultData[PARAM0] = GetPropApiCallback::BuildExecutorProperty(env, getPropertyInfo->getResult, + getPropertyInfo->remainTimes, getPropertyInfo->freezingTime, getPropertyInfo->authSubType); + if (getPropertyInfo->callBackInfo.callBack != nullptr) { + USERAUTH_HILOGI(MODULE_JS_NAPI, "onExecutorPropertyInfo async"); + napi_value global = nullptr; + napi_status napiStatus = napi_get_global(env, &global); + if (napiStatus != napi_ok) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "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) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "napi_get_reference_value faild"); + goto EXIT; + } + napiStatus = napi_call_function(env, global, callBack, PARAM1, resultData, &resultValue); + if (napiStatus != napi_ok) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "napi_call_function faild"); + goto EXIT; + } + } else { + USERAUTH_HILOGI(MODULE_JS_NAPI, "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) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "napi_resolve_deferred faild"); + goto EXIT; + } + } +EXIT: + delete getPropertyInfo; + delete work; +} + +napi_value GetPropApiCallback::BuildExecutorProperty( + napi_env env, int32_t result, uint32_t remainTimes, uint32_t freezingTime, uint64_t authSubType) +{ + napi_value jsObject = nullptr; + NAPI_CALL(env, napi_create_object(env, &jsObject)); + + napi_value resultValue = 0; + NAPI_CALL(env, napi_create_int32(env, result, &resultValue)); + NAPI_CALL(env, napi_set_named_property(env, jsObject, "result", resultValue)); + + napi_value remainTimesValue = 0; + NAPI_CALL(env, napi_create_uint32(env, remainTimes, &remainTimesValue)); + NAPI_CALL(env, napi_set_named_property(env, jsObject, "remainTimes", remainTimesValue)); + + napi_value freezingTimeValue = 0; + NAPI_CALL(env, napi_create_uint32(env, freezingTime, &freezingTimeValue)); + NAPI_CALL(env, napi_set_named_property(env, jsObject, "freezingTime", freezingTimeValue)); + + 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; +} + +void GetPropApiCallback::onGetProperty(const ExecutorProperty result) +{ + if (getPropertyInfo_ == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "GetPropApiCallback onGetProperty getPropertyInfo_ is nullptr"); + return; + } + uv_loop_s *loop(nullptr); + napi_get_uv_event_loop(getPropertyInfo_->callBackInfo.env, &loop); + if (loop == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "loop is null"); + delete getPropertyInfo_; + getPropertyInfo_ = nullptr; + return; + } + uv_work_t *work = new (std::nothrow) uv_work_t; + if (work == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "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; + USERAUTH_HILOGI(MODULE_JS_NAPI, "Before GetPropertyInfoCallback"); + uv_queue_work(loop, work, [] (uv_work_t *work) {}, GetPropertyInfoCallback); +} + +SetPropApiCallback::SetPropApiCallback(SetPropertyInfo *setPropertyInfo) +{ + setPropertyInfo_ = setPropertyInfo; +} + +SetPropApiCallback::~SetPropApiCallback() +{ +} + +static void SetExecutorPropertyCallback(uv_work_t *work, int status) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "Do SetExecutorPropertyCallback work"); + SetPropertyInfo *setPropertyInfo = reinterpret_cast(work->data); + if (setPropertyInfo == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "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) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "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) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "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) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "napi_get_reference_value faild"); + goto EXIT; + } + napiStatus = napi_call_function(env, global, callBack, PARAM1, resultData, &result); + if (napiStatus != napi_ok) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "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) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "napi_call_function faild"); + goto EXIT; + } + } +EXIT: + delete setPropertyInfo; + delete work; +} + +void SetPropApiCallback::onSetProperty(const int32_t result) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "onSetProperty start = %{public}d", result); + if (setPropertyInfo_ != nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "setPropertyInfo is null"); + return; + } + uv_loop_s *loop(nullptr); + napi_get_uv_event_loop(setPropertyInfo_->callBackInfo.env, &loop); + if (loop == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "loop is null"); + delete setPropertyInfo_; + setPropertyInfo_ = nullptr; + return; + } + uv_work_t *work = new (std::nothrow) uv_work_t; + if (work == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "work is null"); + delete setPropertyInfo_; + setPropertyInfo_ = nullptr; + return; + } + setPropertyInfo_->setResult = result; + work->data = reinterpret_cast(setPropertyInfo_); + setPropertyInfo_ = nullptr; + uv_queue_work(loop, work, [] (uv_work_t *work) {}, SetExecutorPropertyCallback); +} +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS diff --git a/interfaces/innerkits/napi/src/result_convert.cpp b/interfaces/innerkits/napi/src/result_convert.cpp index 77abb2f6d..89c9de9fd 100644 --- a/interfaces/innerkits/napi/src/result_convert.cpp +++ b/interfaces/innerkits/napi/src/result_convert.cpp @@ -1,225 +1,224 @@ -/* - * Copyright (c) 2021 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 "result_convert.h" - -#include "securec.h" - -#include "auth_hilog_wrapper.h" - -namespace OHOS { -namespace UserIAM { -namespace UserAuth { -ResultConvert::ResultConvert(void) -{ -} - -ResultConvert::~ResultConvert() -{ -} - -napi_value ResultConvert::Uint64ToUint8Napi(napi_env env, uint64_t value) -{ - HILOG_INFO("ResultConvert Uint64ToUint8Napi uint64_t %{public}llu", value); - size_t length = sizeof(value); - void *data = nullptr; - napi_value arrayBuffer = nullptr; - size_t bufferSize = length; - NAPI_CALL(env, napi_create_arraybuffer(env, bufferSize, &data, &arrayBuffer)); - memcpy_s(data, bufferSize, reinterpret_cast(&value), bufferSize); - napi_value result = nullptr; - NAPI_CALL(env, napi_create_typedarray(env, napi_uint8_array, bufferSize, arrayBuffer, 0, &result)); - return result; -} - -std::vector ResultConvert::NapiGetValueUint8Array(napi_env env, napi_value jsObject, std::string key) -{ - napi_value jsValue = GetNapiValue(env, key.c_str(), jsObject); - std::vector RetNull; - if (jsValue == nullptr) { - return RetNull; - } - napi_typedarray_type arraytype; - size_t length = 0; - napi_value buffer = nullptr; - size_t offset = 0; - uint8_t *data = nullptr; - bool isTypedArray = false; - napi_is_typedarray(env, jsValue, &isTypedArray); - if (!isTypedArray) { - HILOG_ERROR("NapiGetValueUint8Array jsValue is not typedarray"); - return RetNull; - } - napi_get_typedarray_info(env, jsValue, &arraytype, &length, reinterpret_cast(&data), &buffer, &offset); - if (arraytype != napi_uint8_array) { - HILOG_ERROR("NapiGetValueUint8Array js jsValue is not uint8Array"); - return RetNull; - } - if (offset != 0) { - HILOG_ERROR("offset is %{public}d", offset); - return RetNull; - } - std::vector result(data, data + length); - return result; -} - -napi_valuetype ResultConvert::GetType(napi_env env, napi_value value) -{ - napi_status status; - if (value == nullptr) { - return napi_null; - } - napi_valuetype type; - status = napi_typeof(env, value, &type); - if (status != napi_ok) { - HILOG_ERROR("napi_typeof faild"); - } - return type; -} - -std::string ResultConvert::GetStringValueByKey(napi_env env, napi_value jsObject, std::string key) -{ - napi_value value = GetNapiValue(env, key.c_str(), jsObject); - std::string result = NapiGetValueString(env, value); - return result; -} - -int32_t ResultConvert::GetInt32ValueByKey(napi_env env, napi_value jsObject, std::string key) -{ - napi_value value = GetNapiValue(env, key.c_str(), jsObject); - return NapiGetValueInt32(env, value); -} - -std::vector ResultConvert::GetCppArrayUint32(napi_env env, napi_value value) -{ - napi_status status; - uint32_t arrayLength = 0; - napi_get_array_length(env, value, &arrayLength); - if (arrayLength <= 0) { - HILOG_ERROR("%{public}s The array is empty.", __func__); - return std::vector(); - } - std::vector paramArrays; - for (size_t i = 0; i < arrayLength; i++) { - napi_value napiElement = nullptr; - napi_get_element(env, value, i, &napiElement); - napi_valuetype napiValueType = napi_undefined; - napi_typeof(env, napiElement, &napiValueType); - if (napiValueType != napi_number) { - HILOG_ERROR("%{public}s Wrong argument type. Numbers expected.", __func__); - return std::vector(); - } - uint32_t napiValue = 0; - status = napi_get_value_uint32(env, napiElement, &napiValue); - if (status != napi_ok) { - return std::vector(); - } - paramArrays.push_back(napiValue); - } - return paramArrays; -} - -std::vector ResultConvert::GetInt32ArrayValueByKey(napi_env env, napi_value jsObject, std::string key) -{ - napi_status status; - napi_value array = GetNapiValue(env, key.c_str(), jsObject); - if (array == nullptr) { - return std::vector(); - } - bool isArray = false; - status = napi_is_array(env, array, &isArray); - if (status != napi_ok) { - HILOG_INFO("napi_is_array is failed"); - return std::vector(); - } - if (isArray) { - HILOG_INFO("args[PIN_PARAMS_ONE] is a array"); - } else { - HILOG_INFO("args[PIN_PARAMS_ONE] is not a array"); - return std::vector(); - } - return GetCppArrayUint32(env, array); -} - -std::string ResultConvert::NapiGetValueString(napi_env env, napi_value value) -{ - napi_status status; - if (value == nullptr) { - HILOG_ERROR("AuthBuild NapiGetValueString value is nullptr"); - return ""; - } - std::string resultValue = ""; - char valueString[NAPI_GET_STRING_SIZE]; - size_t valueSize = NAPI_GET_STRING_SIZE; - size_t resultSize = 0; - status = napi_get_value_string_utf8(env, value, valueString, valueSize, &resultSize); - if (status != napi_ok) { - HILOG_ERROR("napi_get_value_string_utf8 faild"); - } - resultValue = valueString; - if (resultValue == "") { - HILOG_ERROR("ContactsBuild NapiGetValueString Data error"); - return ""; - } - return resultValue; -} - -int32_t ResultConvert::NapiGetValueInt32(napi_env env, napi_value value) -{ - if (value == nullptr) { - return GET_VALUE_ERROR; - } - int32_t result; - napi_status status = napi_get_value_int32(env, value, &result); - if (status != napi_ok) { - return GET_VALUE_ERROR; - } - return result; -} - -int ResultConvert::NapiGetValueInt(napi_env env, napi_value value) -{ - if (value == nullptr) { - return GET_VALUE_ERROR; - } - int32_t result; - napi_status status = napi_get_value_int32(env, value, &result); - if (status != napi_ok) { - return GET_VALUE_ERROR; - } - int ret = result; - return ret; -} - -napi_value ResultConvert::GetNapiValue(napi_env env, const std::string keyChar, napi_value object) -{ - if (object == nullptr) { - HILOG_ERROR("ResultConvert::GetNapiValue object is nullptr"); - return nullptr; - } - napi_value key = nullptr; - NAPI_CALL(env, napi_create_string_utf8(env, keyChar.c_str(), NAPI_AUTO_LENGTH, &key)); - bool result = false; - NAPI_CALL(env, napi_has_property(env, object, key, &result)); - if (result) { - napi_value value = nullptr; - NAPI_CALL(env, napi_get_property(env, object, key, &value)); - return value; - } - return nullptr; -} -} // namespace UserAuth -} // namespace UserIAM -} // namespace OHOS +/* + * Copyright (c) 2021 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 + +#include "securec.h" +#include "userauth_hilog_wrapper.h" +#include "result_convert.h" + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +ResultConvert::ResultConvert(void) +{ +} + +ResultConvert::~ResultConvert() +{ +} + +napi_value ResultConvert::Uint64ToUint8Napi(napi_env env, uint64_t value) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "ResultConvert Uint64ToUint8Napi uint64_t %{public}" PRIu64 "", value); + size_t length = sizeof(value); + void *data = nullptr; + napi_value arrayBuffer = nullptr; + size_t bufferSize = length; + NAPI_CALL(env, napi_create_arraybuffer(env, bufferSize, &data, &arrayBuffer)); + memcpy_s(data, bufferSize, reinterpret_cast(&value), bufferSize); + napi_value result = nullptr; + NAPI_CALL(env, napi_create_typedarray(env, napi_uint8_array, bufferSize, arrayBuffer, 0, &result)); + return result; +} + +std::vector ResultConvert::NapiGetValueUint8Array(napi_env env, napi_value jsObject, std::string key) +{ + napi_value jsValue = GetNapiValue(env, key.c_str(), jsObject); + std::vector RetNull; + if (jsValue == nullptr) { + return RetNull; + } + napi_typedarray_type arraytype; + size_t length = 0; + napi_value buffer = nullptr; + size_t offset = 0; + uint8_t *data = nullptr; + bool isTypedArray = false; + napi_is_typedarray(env, jsValue, &isTypedArray); + if (!isTypedArray) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "NapiGetValueUint8Array jsValue is not typedarray"); + return RetNull; + } + napi_get_typedarray_info(env, jsValue, &arraytype, &length, reinterpret_cast(&data), &buffer, &offset); + if (arraytype != napi_uint8_array) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "NapiGetValueUint8Array js jsValue is not uint8Array"); + return RetNull; + } + if (offset != 0) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "offset is %{public}zu", offset); + return RetNull; + } + std::vector result(data, data + length); + return result; +} + +napi_valuetype ResultConvert::GetType(napi_env env, napi_value value) +{ + napi_status status; + if (value == nullptr) { + return napi_null; + } + napi_valuetype type; + status = napi_typeof(env, value, &type); + if (status != napi_ok) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "napi_typeof faild"); + } + return type; +} + +std::string ResultConvert::GetStringValueByKey(napi_env env, napi_value jsObject, std::string key) +{ + napi_value value = GetNapiValue(env, key.c_str(), jsObject); + std::string result = NapiGetValueString(env, value); + return result; +} + +int32_t ResultConvert::GetInt32ValueByKey(napi_env env, napi_value jsObject, std::string key) +{ + napi_value value = GetNapiValue(env, key.c_str(), jsObject); + return NapiGetValueInt32(env, value); +} + +std::vector ResultConvert::GetCppArrayUint32(napi_env env, napi_value value) +{ + napi_status status; + uint32_t arrayLength = 0; + napi_get_array_length(env, value, &arrayLength); + if (arrayLength <= 0) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "%{public}s The array is empty.", __func__); + return std::vector(); + } + std::vector paramArrays; + for (size_t i = 0; i < arrayLength; i++) { + napi_value napiElement = nullptr; + napi_get_element(env, value, i, &napiElement); + napi_valuetype napiValueType = napi_undefined; + napi_typeof(env, napiElement, &napiValueType); + if (napiValueType != napi_number) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "%{public}s Wrong argument type. Numbers expected.", __func__); + return std::vector(); + } + uint32_t napiValue = 0; + status = napi_get_value_uint32(env, napiElement, &napiValue); + if (status != napi_ok) { + return std::vector(); + } + paramArrays.push_back(napiValue); + } + return paramArrays; +} + +std::vector ResultConvert::GetInt32ArrayValueByKey(napi_env env, napi_value jsObject, std::string key) +{ + napi_status status; + napi_value array = GetNapiValue(env, key.c_str(), jsObject); + if (array == nullptr) { + return std::vector(); + } + bool isArray = false; + status = napi_is_array(env, array, &isArray); + if (status != napi_ok) { + USERAUTH_HILOGI(MODULE_JS_NAPI, "napi_is_array is failed"); + return std::vector(); + } + if (isArray) { + USERAUTH_HILOGI(MODULE_JS_NAPI, "args[PIN_PARAMS_ONE] is a array"); + } else { + USERAUTH_HILOGI(MODULE_JS_NAPI, "args[PIN_PARAMS_ONE] is not a array"); + return std::vector(); + } + return GetCppArrayUint32(env, array); +} + +std::string ResultConvert::NapiGetValueString(napi_env env, napi_value value) +{ + napi_status status; + if (value == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "AuthBuild NapiGetValueString value is nullptr"); + return ""; + } + std::string resultValue = ""; + char valueString[NAPI_GET_STRING_SIZE]; + size_t valueSize = NAPI_GET_STRING_SIZE; + size_t resultSize = 0; + status = napi_get_value_string_utf8(env, value, valueString, valueSize, &resultSize); + if (status != napi_ok) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "napi_get_value_string_utf8 faild"); + } + resultValue = valueString; + if (resultValue == "") { + USERAUTH_HILOGE(MODULE_JS_NAPI, "ContactsBuild NapiGetValueString Data error"); + return ""; + } + return resultValue; +} + +int32_t ResultConvert::NapiGetValueInt32(napi_env env, napi_value value) +{ + if (value == nullptr) { + return GET_VALUE_ERROR; + } + uint32_t result; + napi_status status = napi_get_value_uint32(env, value, &result); + if (status != napi_ok) { + return GET_VALUE_ERROR; + } + return result; +} + +int ResultConvert::NapiGetValueInt(napi_env env, napi_value value) +{ + if (value == nullptr) { + return GET_VALUE_ERROR; + } + int32_t result; + napi_status status = napi_get_value_int32(env, value, &result); + if (status != napi_ok) { + return GET_VALUE_ERROR; + } + int ret = result; + return ret; +} + +napi_value ResultConvert::GetNapiValue(napi_env env, const std::string keyChar, napi_value object) +{ + if (object == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "ResultConvert::GetNapiValue object is nullptr"); + return nullptr; + } + napi_value key = nullptr; + NAPI_CALL(env, napi_create_string_utf8(env, keyChar.c_str(), NAPI_AUTO_LENGTH, &key)); + bool result = false; + NAPI_CALL(env, napi_has_property(env, object, key, &result)); + if (result) { + napi_value value = nullptr; + NAPI_CALL(env, napi_get_property(env, object, key, &value)); + return value; + } + return nullptr; +} +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS diff --git a/interfaces/innerkits/napi/src/user_auth_helper.cpp b/interfaces/innerkits/napi/src/user_auth_helper.cpp index 2d656c160..ee186af67 100755 --- a/interfaces/innerkits/napi/src/user_auth_helper.cpp +++ b/interfaces/innerkits/napi/src/user_auth_helper.cpp @@ -1,245 +1,249 @@ -/* - * Copyright (c) 2021 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_helper.h" - -#include "auth_hilog_wrapper.h" -#include "user_auth_impl.h" - -namespace OHOS { -namespace UserIAM { -namespace UserAuth { -/** - * @brief Instance passed to context - * - * @param env - * @param info - * @return napi_value Instance - */ -napi_value UserAuthServiceConstructor(napi_env env, napi_callback_info info) -{ - std::shared_ptr userAuthImpl; - userAuthImpl.reset(new UserAuthImpl()); - napi_value thisVar = nullptr; - size_t argc = ARGS_ONE; - napi_value argv[ARGS_ONE] = {nullptr}; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); - NAPI_CALL(env, napi_wrap(env, thisVar, userAuthImpl.get(), - [](napi_env env, void *data, void *hint) { - UserAuthImpl *userAuthImpl = static_cast(data); - if (userAuthImpl != nullptr) { - delete userAuthImpl; - } - }, - nullptr, nullptr)); - // Pull up the face service process - return thisVar; -} - -/** - * @brief Get the Version object - * - * @param env - * @param info - * @return napi_value Specific version number results - */ -napi_value GetVersion(napi_env env, napi_callback_info info) -{ - napi_value thisVar = nullptr; - size_t argcAsync = 0; - napi_value args[ARGS_MAX_COUNT] = {nullptr}; - NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, &thisVar, nullptr)); - UserAuthImpl *userAuthImpl = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&userAuthImpl)); - HILOG_INFO("UserAuthHelper, GetVersion"); - return userAuthImpl->GetVersion(env, info); -} - -/** - * @brief Get the Availabe Status object - * - * @param env - * @param info - * @return napi_value Verify that the certification capability is available - */ -napi_value GetAvailableStatus(napi_env env, napi_callback_info info) -{ - napi_value thisVar = nullptr; - size_t argcAsync = 0; - napi_value args[ARGS_MAX_COUNT] = {nullptr}; - NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, &thisVar, nullptr)); - UserAuthImpl *userAuthImpl = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&userAuthImpl)); - HILOG_INFO("UserAuthHelper, getAvailableStatus"); - return userAuthImpl->GetAvailableStatus(env, info); -} - -/** - * @brief Get the Property object - * - * @param env - * @param info - * @return napi_value It supports querying subclasses / remaining authentication times / freezing time - */ -napi_value GetProperty(napi_env env, napi_callback_info info) -{ - napi_value thisVar = nullptr; - size_t argcAsync = 0; - napi_value args[ARGS_MAX_COUNT] = {nullptr}; - NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, &thisVar, nullptr)); - UserAuthImpl *userAuthImpl = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&userAuthImpl)); - HILOG_INFO("UserAuthHelper, GetProperty"); - return userAuthImpl->GetProperty(env, info); -} - -/** - * @brief Set the Property object - * - * @param env - * @param info - * @return napi_value Set properties: can be used to initialize algorithms - */ -napi_value SetProperty(napi_env env, napi_callback_info info) -{ - napi_value thisVar = nullptr; - size_t argcAsync = 0; - napi_value args[ARGS_MAX_COUNT] = {nullptr}; - NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, &thisVar, nullptr)); - UserAuthImpl *userAuthImpl = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&userAuthImpl)); - HILOG_INFO("UserAuthHelper, SetProperty"); - return userAuthImpl->SetProperty(env, info); -} - -/** - * @brief user authentication - * - * @param env - * @param info - * @return napi_value Enter the challenge value, authentication method, trust level and callback, and return the result - * and acquireinfo through the callback - */ -napi_value Auth(napi_env env, napi_callback_info info) -{ - napi_value thisVar = nullptr; - size_t argcAsync = 0; - napi_value args[ARGS_MAX_COUNT] = {nullptr}; - NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, &thisVar, nullptr)); - UserAuthImpl *userAuthImpl = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&userAuthImpl)); - HILOG_INFO("UserAuthHelper, Auth"); - return userAuthImpl->Auth(env, info); -} - -napi_value Execute(napi_env env, napi_callback_info info) -{ - napi_value thisVar = nullptr; - size_t argcAsync = 0; - napi_value args[ARGS_MAX_COUNT] = {nullptr}; - NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, &thisVar, nullptr)); - UserAuthImpl *userAuthImpl = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&userAuthImpl)); - HILOG_INFO("UserAuthHelper, Execute"); - return userAuthImpl->Execute(env, info); -} - -/** - * @brief user authentication - * - * @param env - * @param info - * @return napi_value Pass in the user ID, challenge value, authentication method, trust level and callback, and return - * the result acquireinfo through the callback - */ -napi_value AuthUser(napi_env env, napi_callback_info info) -{ - napi_value thisVar = nullptr; - size_t argcAsync = 0; - napi_value args[ARGS_MAX_COUNT] = {nullptr}; - NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, &thisVar, nullptr)); - UserAuthImpl *userAuthImpl = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&userAuthImpl)); - HILOG_INFO("UserAuthHelper, AuthUser"); - return userAuthImpl->AuthUser(env, info); -} - -/** - * @brief Cancel authentication - * - * @param env - * @param info - * @return napi_value success or fail - */ -napi_value CancelAuth(napi_env env, napi_callback_info info) -{ - napi_value thisVar = nullptr; - size_t argcAsync = 0; - napi_value args[ARGS_MAX_COUNT] = {nullptr}; - NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, &thisVar, nullptr)); - UserAuthImpl *userAuthImpl = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&userAuthImpl)); - HILOG_INFO("UserAuthHelper, CancelAuth"); - return userAuthImpl->CancelAuth(env, info); -} - -/** - * @brief Napi initialization - * - * @param env - * @param exports - */ -void Init(napi_env env, napi_value exports) -{ - napi_status status; - napi_property_descriptor exportFuncs[] = { - DECLARE_NAPI_FUNCTION("constructor", UserAuth::Constructor), - DECLARE_NAPI_FUNCTION("getAuthenticator", UserAuth::Constructor), - }; - status = napi_define_properties(env, exports, sizeof(exportFuncs) / sizeof(*exportFuncs), exportFuncs); - if (status != napi_ok) { - HILOG_ERROR("napi_define_properties faild"); - } -} - -napi_value Constructor(napi_env env, napi_callback_info info) -{ - napi_value thisVar = nullptr; - napi_value userAuth = nullptr; - NAPI_CALL(env, napi_new_instance(env, GetCtor(env), 0, nullptr, &userAuth)); - NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - return userAuth; -} - -napi_value GetCtor(napi_env env) -{ - napi_value cons = nullptr; - napi_property_descriptor clzDes[] = { - DECLARE_NAPI_FUNCTION("getVersion", UserAuth::GetVersion), - DECLARE_NAPI_FUNCTION("getAvailableStatus", UserAuth::GetAvailableStatus), - DECLARE_NAPI_FUNCTION("getProperty", UserAuth::GetProperty), - DECLARE_NAPI_FUNCTION("setProperty", UserAuth::SetProperty), - DECLARE_NAPI_FUNCTION("auth", UserAuth::Auth), - DECLARE_NAPI_FUNCTION("authUser", UserAuth::AuthUser), - DECLARE_NAPI_FUNCTION("cancelAuth", UserAuth::CancelAuth), - DECLARE_NAPI_FUNCTION("execute", UserAuth::Execute), - }; - NAPI_CALL(env, napi_define_class(env, "UserAuth", NAPI_AUTO_LENGTH, UserAuthServiceConstructor, nullptr, - sizeof(clzDes) / sizeof(napi_property_descriptor), clzDes, &cons)); - return cons; -} -} // namespace UserAuth -} // namespace UserIAM -} // namespace OHOS +/* + * Copyright (c) 2021 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 + +#include "userauth_hilog_wrapper.h" +#include "user_auth_impl.h" +#include "user_auth_helper.h" + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +/** + * @brief Instance passed to context + * + * @param env + * @param info + * @return napi_value Instance + */ +napi_value UserAuthServiceConstructor(napi_env env, napi_callback_info info) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s, start.", __func__); + std::shared_ptr userAuthImpl; + userAuthImpl.reset(new UserAuthImpl()); + napi_value thisVar = nullptr; + size_t argc = ARGS_ONE; + napi_value argv[ARGS_ONE] = {nullptr}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); + NAPI_CALL(env, napi_wrap(env, thisVar, userAuthImpl.get(), + [](napi_env env, void *data, void *hint) { + UserAuthImpl *userAuthImpl = static_cast(data); + if (userAuthImpl != nullptr) { + delete userAuthImpl; + } + }, + nullptr, nullptr)); + // Pull up the face service process + return thisVar; +} + +/** + * @brief Get the Version object + * + * @param env + * @param info + * @return napi_value Specific version number results + */ +napi_value GetVersion(napi_env env, napi_callback_info info) +{ + napi_value thisVar = nullptr; + size_t argcAsync = 0; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, &thisVar, nullptr)); + UserAuthImpl *userAuthImpl = nullptr; + NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&userAuthImpl)); + USERAUTH_HILOGI(MODULE_JS_NAPI, "UserAuthHelper, GetVersion"); + return userAuthImpl->GetVersion(env, info); +} + +/** + * @brief Get the Availabe Status object + * + * @param env + * @param info + * @return napi_value Verify that the certification capability is available + */ +napi_value GetAvailableStatus(napi_env env, napi_callback_info info) +{ + napi_value thisVar = nullptr; + size_t argcAsync = 0; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, &thisVar, nullptr)); + UserAuthImpl *userAuthImpl = nullptr; + NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&userAuthImpl)); + USERAUTH_HILOGI(MODULE_JS_NAPI, "UserAuthHelper, getAvailableStatus"); + return userAuthImpl->GetAvailableStatus(env, info); +} + +/** + * @brief Get the Property object + * + * @param env + * @param info + * @return napi_value It supports querying subclasses / remaining authentication times / freezing time + */ +napi_value GetProperty(napi_env env, napi_callback_info info) +{ + napi_value thisVar = nullptr; + size_t argcAsync = 0; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, &thisVar, nullptr)); + UserAuthImpl *userAuthImpl = nullptr; + NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&userAuthImpl)); + USERAUTH_HILOGI(MODULE_JS_NAPI, "UserAuthHelper, GetProperty"); + return userAuthImpl->GetProperty(env, info); +} + +/** + * @brief Set the Property object + * + * @param env + * @param info + * @return napi_value Set properties: can be used to initialize algorithms + */ +napi_value SetProperty(napi_env env, napi_callback_info info) +{ + napi_value thisVar = nullptr; + size_t argcAsync = 0; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, &thisVar, nullptr)); + UserAuthImpl *userAuthImpl = nullptr; + NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&userAuthImpl)); + USERAUTH_HILOGI(MODULE_JS_NAPI, "UserAuthHelper, SetProperty"); + return userAuthImpl->SetProperty(env, info); +} + +/** + * @brief user authentication + * + * @param env + * @param info + * @return napi_value Enter the challenge value, authentication method, trust level and callback, and return the result + * and acquireinfo through the callback + */ +napi_value Auth(napi_env env, napi_callback_info info) +{ + napi_value thisVar = nullptr; + size_t argcAsync = 0; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, &thisVar, nullptr)); + UserAuthImpl *userAuthImpl = nullptr; + NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&userAuthImpl)); + USERAUTH_HILOGI(MODULE_JS_NAPI, "UserAuthHelper, Auth"); + return userAuthImpl->Auth(env, info); +} + +napi_value Execute(napi_env env, napi_callback_info info) +{ + napi_value thisVar = nullptr; + size_t argcAsync = 0; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, &thisVar, nullptr)); + UserAuthImpl *userAuthImpl = nullptr; + NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&userAuthImpl)); + USERAUTH_HILOGI(MODULE_JS_NAPI, "UserAuthHelper, Execute"); + return userAuthImpl->Execute(env, info); +} + +/** + * @brief user authentication + * + * @param env + * @param info + * @return napi_value Pass in the user ID, challenge value, authentication method, trust level and callback, and return + * the result acquireinfo through the callback + */ +napi_value AuthUser(napi_env env, napi_callback_info info) +{ + napi_value thisVar = nullptr; + size_t argcAsync = 0; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, &thisVar, nullptr)); + UserAuthImpl *userAuthImpl = nullptr; + NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&userAuthImpl)); + USERAUTH_HILOGI(MODULE_JS_NAPI, "UserAuthHelper, AuthUser"); + return userAuthImpl->AuthUser(env, info); +} + +/** + * @brief Cancel authentication + * + * @param env + * @param info + * @return napi_value success or fail + */ +napi_value CancelAuth(napi_env env, napi_callback_info info) +{ + napi_value thisVar = nullptr; + size_t argcAsync = 0; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, &thisVar, nullptr)); + UserAuthImpl *userAuthImpl = nullptr; + NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&userAuthImpl)); + USERAUTH_HILOGI(MODULE_JS_NAPI, "UserAuthHelper, CancelAuth"); + return userAuthImpl->CancelAuth(env, info); +} + +/** + * @brief Napi initialization + * + * @param env + * @param exports + */ +void Init(napi_env env, napi_value exports) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s, start.", __func__); + napi_status status; + napi_property_descriptor exportFuncs[] = { + DECLARE_NAPI_FUNCTION("constructor", UserAuth::Constructor), + DECLARE_NAPI_FUNCTION("getAuthenticator", UserAuth::Constructor), + }; + status = napi_define_properties(env, exports, sizeof(exportFuncs) / sizeof(*exportFuncs), exportFuncs); + if (status != napi_ok) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "napi_define_properties faild"); + } +} + +napi_value Constructor(napi_env env, napi_callback_info info) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s, start.", __func__); + napi_value thisVar = nullptr; + napi_value userAuth = nullptr; + NAPI_CALL(env, napi_new_instance(env, GetCtor(env), 0, nullptr, &userAuth)); + NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); + return userAuth; +} + +napi_value GetCtor(napi_env env) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s, start.", __func__); + napi_value cons = nullptr; + napi_property_descriptor clzDes[] = { + DECLARE_NAPI_FUNCTION("getVersion", UserAuth::GetVersion), + DECLARE_NAPI_FUNCTION("getAvailableStatus", UserAuth::GetAvailableStatus), + DECLARE_NAPI_FUNCTION("getProperty", UserAuth::GetProperty), + DECLARE_NAPI_FUNCTION("setProperty", UserAuth::SetProperty), + DECLARE_NAPI_FUNCTION("auth", UserAuth::Auth), + DECLARE_NAPI_FUNCTION("authUser", UserAuth::AuthUser), + DECLARE_NAPI_FUNCTION("cancelAuth", UserAuth::CancelAuth), + DECLARE_NAPI_FUNCTION("execute", UserAuth::Execute), + }; + NAPI_CALL(env, napi_define_class(env, "UserAuth", NAPI_AUTO_LENGTH, UserAuthServiceConstructor, nullptr, + sizeof(clzDes) / sizeof(napi_property_descriptor), clzDes, &cons)); + return cons; +} +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS diff --git a/interfaces/innerkits/napi/src/user_auth_impl.cpp b/interfaces/innerkits/napi/src/user_auth_impl.cpp index 06e504a47..e15344c3d 100755 --- a/interfaces/innerkits/napi/src/user_auth_impl.cpp +++ b/interfaces/innerkits/napi/src/user_auth_impl.cpp @@ -1,612 +1,633 @@ -/* - * Copyright (c) 2021 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_impl.h" - -#include - -#include "user_auth.h" -#include "userauth_callback.h" -#include "userauth_info.h" - -#include "auth_hilog_wrapper.h" -#include "authapi_callback.h" - -namespace OHOS { -namespace UserIAM { -namespace UserAuth { -UserAuthImpl::UserAuthImpl() -{ -} - -UserAuthImpl::~UserAuthImpl() -{ -} - -napi_value UserAuthImpl::GetVersion(napi_env env, napi_callback_info info) -{ - int32_t result = UserAuth::GetInstance().GetVersion(); - HILOG_INFO("GetVersion result = %{public}d ", result); - napi_value version = 0; - NAPI_CALL(env, napi_create_int32(env, result, &version)); - return version; -} - -napi_value UserAuthImpl::GetAvailableStatus(napi_env env, napi_callback_info info) -{ - napi_value argv[ARGS_MAX_COUNT] = {nullptr}; - size_t argc = ARGS_MAX_COUNT; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); - if (argc != ARGS_TWO) { - HILOG_ERROR("%{public}s, parms error.", __func__); - return nullptr; - } - int type = authBuild.NapiGetValueInt(env, argv[PARAM0]); - if (type == GET_VALUE_ERROR) { - HILOG_ERROR("%{public}s, argv[PARAM0] error.", __func__); - return nullptr; - } - int level = authBuild.NapiGetValueInt(env, argv[PARAM1]); - if (level == GET_VALUE_ERROR) { - HILOG_ERROR("%{public}s, argv[PARAM1] error.", __func__); - return nullptr; - } - AuthType authType = AuthType(type); - AuthTurstLevel authTurstLevel = AuthTurstLevel(level); - int32_t result = UserAuth::GetInstance().GetAvailableStatus(authType, authTurstLevel); - HILOG_INFO("GetAvailableStatus result = %{public}d", result); - napi_value ret = 0; - NAPI_CALL(env, napi_create_int32(env, result, &ret)); - return ret; -} - -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; - 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, 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; - napi_value args[ARGS_MAX_COUNT] = {nullptr}; - NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); - if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { - HILOG_ERROR("%{public}s, Wrong argument count.", __func__); - return nullptr; - } - if (argcAsync > PARAM1) { - napi_valuetype valuetype = napi_undefined; - napi_typeof(env, args[PARAM1], &valuetype); - if (valuetype == napi_function) { - 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 = nullptr; - if (argcAsync > argcPromise) { - ret = GetPropertyAsync(env, asyncHolder); - } else { - ret = GetPropertyPromise(env, asyncHolder); - } - HILOG_INFO("%{public}s,end.", __func__); - return ret; -} - -void UserAuthImpl::GetPropertyExecute(napi_env env, void *data) -{ - HILOG_INFO("GetPropertyExecute, worker pool thread execute."); - 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"); - GetPropApiCallback *object = new GetPropApiCallback(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"); - 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"); - 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, AsyncHolder *asyncHolder) -{ - HILOG_INFO("%{public}s, asyncCallback.", __func__); - 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 *)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, AsyncHolder *asyncHolder) -{ - HILOG_INFO("%{public}s, promise.", __func__); - 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; - napi_value promise = nullptr; - 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 *)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; - 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, 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; - napi_value args[ARGS_MAX_COUNT] = {nullptr}; - NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); - if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { - HILOG_ERROR("%{public}s, Wrong argument count.", __func__); - return nullptr; - } - if (argcAsync > PARAM1) { - napi_valuetype valuetype = napi_undefined; - napi_typeof(env, args[PARAM1], &valuetype); - if (valuetype == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[PARAM1], 1, &(setPropertyInfo->callBackInfo.callBack))); - } - } - - if (authBuild.NapiTypeObject(env, args[PARAM0])) { - Napi_SetPropertyRequest request = authBuild.SetPropertyRequestBuild(env, args[0]); - setPropertyInfo->authType = request.authType_; - setPropertyInfo->key = request.key_; - setPropertyInfo->setInfo = request.setInfo_; - } - - napi_value ret = 0; - if (argcAsync > argcPromise) { - ret = SetPropertyAsync(env, asyncHolder); - } else { - ret = SetPropertyPromise(env, asyncHolder); - } - HILOG_INFO("%{public}s,end.", __func__); - return ret; -} - -void UserAuthImpl::SetPropertyExecute(napi_env env, void *data) -{ - HILOG_INFO("setPropertyExecute, worker pool thread execute."); - 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; - SetPropApiCallback *object = new SetPropApiCallback(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"); - 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"); - 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, AsyncHolder *asyncHolder) -{ - HILOG_INFO("%{public}s, asyncCallback.", __func__); - 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 *)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, AsyncHolder *asyncHolder) -{ - HILOG_INFO("%{public}s, promise.", __func__); - 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 = 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 *)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::BuildAuthInfo(napi_env env, AuthInfo *authInfo) -{ - 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}; - NAPI_CALL(env, napi_get_cb_info(env, authInfo->info, &argc, argv, nullptr, nullptr)); - if (argc != ARGS_FOUR) { - HILOG_ERROR("%{public}s, parms error.", __func__); - return nullptr; - } - authInfo->challenge = authBuild.GetUint8ArrayTo64(env, argv[0]); - - if (authBuild.NapiTypeNumber(env, argv[PARAM1])) { - int64_t type; - NAPI_CALL(env, napi_get_value_int64(env, argv[PARAM1], &type)); - authInfo->authType = type; - } - - if (authBuild.NapiTypeNumber(env, argv[PARAM2])) { - int64_t level; - NAPI_CALL(env, napi_get_value_int64(env, argv[PARAM2], &level)); - authInfo->authTrustLevel = level; - } - - if (authBuild.NapiTypeObject(env, argv[PARAM3])) { - authInfo->jsFunction = argv[PARAM3]; - NAPI_CALL(env, napi_get_named_property(env, argv[PARAM3], "onResult", &authInfo->onResultCallBack)); - NAPI_CALL(env, napi_create_reference(env, authInfo->onResultCallBack, PARAM1, &authInfo->onResult)); - 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 result; -} - -napi_value UserAuthImpl::DoExecute(ExecuteInfo* executeInfo) -{ - AuthApiCallback *object = new (std::nothrow) AuthApiCallback(executeInfo); - if (object == nullptr) { - delete executeInfo; - return nullptr; - } - std::shared_ptr callback; - callback.reset(object); - std::map convertAuthTurstLevel = { - { "S1", ATL1 }, - { "S2", ATL2 }, - { "S3", ATL3 }, - { "S4", ATL4 }, - }; - if (convertAuthTurstLevel.count(executeInfo->level) == 0) { - HILOG_ERROR("execute convertAuthTurstLevel is 0"); - delete executeInfo; - return nullptr; - } - UserAuth::GetInstance().Auth(0, FACE, convertAuthTurstLevel[executeInfo->level], callback); - if (executeInfo->isPromise) { - return executeInfo->promise; - } else { - napi_value result = nullptr; - NAPI_CALL(executeInfo->env, napi_get_null(executeInfo->env, &result)); - return result; - } -} - -napi_value UserAuthImpl::Execute(napi_env env, napi_callback_info info) -{ - HILOG_INFO("%{public}s, start", __func__); - ExecuteInfo *executeInfo = new (std::nothrow) ExecuteInfo(); - if (executeInfo == nullptr) { - HILOG_ERROR("%{public}s executeInfo nullptr", __func__); - return nullptr; - } - executeInfo->env = env; - size_t argc = ARGS_MAX_COUNT; - size_t callbackIndex = 0; - napi_value argv[ARGS_MAX_COUNT] = {nullptr}; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); - if (argc < PARAM2) { - HILOG_ERROR("%{public}s argc check fail", __func__); - delete executeInfo; - return nullptr; - } - if (!GetExecuteInfo(env, argv, executeInfo)) { - HILOG_ERROR("%{public}s GetExecuteInfo fail", __func__); - delete executeInfo; - return nullptr; - } - callbackIndex = argc - 1; - napi_valuetype valuetype; - NAPI_CALL(env, napi_typeof(env, argv[callbackIndex], &valuetype)); - if (valuetype == napi_function) { - executeInfo->isPromise = false; - NAPI_CALL(env, napi_create_reference(env, argv[callbackIndex], 1, &executeInfo->callbackRef)); - } else { - executeInfo->isPromise = true; - NAPI_CALL(env, napi_create_promise(env, &executeInfo->deferred, &executeInfo->promise)); - } - return DoExecute(executeInfo); -} - -bool UserAuthImpl::GetExecuteInfo(napi_env env, napi_value* argv, ExecuteInfo* executeInfo) -{ - HILOG_INFO("%{public}s, start.", __func__); - napi_valuetype valuetype; - napi_typeof(env, argv[PARAM0], &valuetype); - if (valuetype != napi_string) { - return false; - } - char *str = nullptr; - size_t len = 0; - napi_get_value_string_utf8(env, argv[PARAM0], nullptr, 0, &len); - if (len > 0) { - str = new char[len + 1](); - napi_get_value_string_utf8(env, argv[PARAM0], str, len + 1, &len); - executeInfo->type = str; - delete[] str; - } - napi_typeof(env, argv[PARAM1], &valuetype); - if (valuetype != napi_string) { - return false; - } - napi_get_value_string_utf8(env, argv[PARAM1], nullptr, 0, &len); - if (len > 0) { - str = new char[len + 1](); - napi_get_value_string_utf8(env, argv[PARAM1], str, len + 1, &len); - executeInfo->level = str; - delete[] str; - } - if (executeInfo->type.compare("FACE_ONLY") != 0) { - HILOG_ERROR("%{public}s check type fail.", __func__); - return false; - } - return true; -} - -napi_value UserAuthImpl::Auth(napi_env env, napi_callback_info info) -{ - HILOG_INFO("%{public}s, start", __func__); - AuthInfo *authInfo = new (std::nothrow) AuthInfo(); - if (authInfo == nullptr) { - 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(authInfo); - std::shared_ptr callback; - callback.reset(object); - uint64_t result = UserAuth::GetInstance().Auth( - authInfo->challenge, AuthType(authInfo->authType), AuthTurstLevel(authInfo->authTrustLevel), callback); - HILOG_INFO("UserAuth::GetInstance().Auth.result = %{public}llu", result); - napi_value key = authBuild.Uint64ToUint8Array(env, result); - HILOG_INFO("%{public}s, end.", __func__); - return key; -} - -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)); - if (argc != ARGS_FIVE) { - HILOG_ERROR("%{public}s, parms error.", __func__); - return nullptr; - } - if (authBuild.NapiTypeNumber(env, argv[PARAM0])) { - int32_t id = 0; - NAPI_CALL(env, napi_get_value_int32(env, argv[PARAM0], &id)); - userInfo->userId = id; - } - userInfo->challenge = authBuild.GetUint8ArrayTo64(env, argv[PARAM1]); - if (authBuild.NapiTypeNumber(env, argv[PARAM2])) { - int32_t type = 0; - napi_get_value_int32(env, argv[PARAM2], &type); - userInfo->authType = type; - } - if (authBuild.NapiTypeNumber(env, argv[PARAM3])) { - int32_t level = 0; - NAPI_CALL(env, napi_get_value_int32(env, argv[PARAM3], &level)); - userInfo->authTrustLevel = level; - } - if (authBuild.NapiTypeObject(env, argv[PARAM4])) { - HILOG_INFO("AuthUser is Object"); - userInfo->jsFunction = argv[PARAM4]; - NAPI_CALL(env, napi_get_named_property(env, argv[PARAM4], "onResult", &userInfo->onResultCallBack)); - NAPI_CALL(env, napi_create_reference(env, userInfo->onResultCallBack, PARAM1, &userInfo->onResult)); - 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 result; -} - -napi_value UserAuthImpl::AuthUserWrap(napi_env env, AuthUserInfo *userInfo) -{ - HILOG_INFO("%{public}s, start.", __func__); - AuthApiCallback *object = new AuthApiCallback(userInfo); - std::shared_ptr callback; - callback.reset(object); - uint64_t result = UserAuth::GetInstance().AuthUser(userInfo->userId, userInfo->challenge, - AuthType(userInfo->authType), AuthTurstLevel(userInfo->authTrustLevel), callback); - HILOG_INFO("UserAuth::GetInstance().AuthUser. result = %{public}llu", result); - napi_value key = authBuild.Uint64ToUint8Array(env, result); - HILOG_INFO("%{public}s, end.", __func__); - return key; -} - -napi_value UserAuthImpl::CancelAuth(napi_env env, napi_callback_info info) -{ - size_t argc = ARGS_MAX_COUNT; - napi_value argv[ARGS_MAX_COUNT] = {nullptr}; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); - - uint64_t contextId = authBuild.GetUint8ArrayTo64(env, argv[0]); - HILOG_INFO("CancelAuth contextId = %{public}llu", contextId); - if (contextId == 0) { - return nullptr; - } - int32_t result = UserAuth::GetInstance().CancelAuth(contextId); - HILOG_INFO("CancelAuth result = %{public}d", result); - napi_value key = 0; - NAPI_CALL(env, napi_create_int32(env, result, &key)); - return key; -} -} // namespace UserAuth -} // namespace UserIAM -} // namespace OHOS +/* + * Copyright (c) 2021 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 + +#include "user_auth_impl.h" + +#include + +#include "user_auth.h" +#include "userauth_callback.h" +#include "userauth_info.h" +#include "userauth_hilog_wrapper.h" +#include "authapi_callback.h" +#include "user_auth_helper.h" + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +UserAuthImpl::UserAuthImpl() +{ +} + +UserAuthImpl::~UserAuthImpl() +{ +} + +napi_value UserAuthImpl::GetVersion(napi_env env, napi_callback_info info) +{ + int32_t result = UserAuth::GetInstance().GetVersion(); + USERAUTH_HILOGI(MODULE_JS_NAPI, "GetVersion result = %{public}d", result); + napi_value version = 0; + NAPI_CALL(env, napi_create_int32(env, result, &version)); + return version; +} + +napi_value UserAuthImpl::GetAvailableStatus(napi_env env, napi_callback_info info) +{ + napi_value argv[ARGS_MAX_COUNT] = {nullptr}; + size_t argc = ARGS_MAX_COUNT; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); + if (argc != ARGS_TWO) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "%{public}s, parms error.", __func__); + return nullptr; + } + int type = authBuild.NapiGetValueInt(env, argv[PARAM0]); + if (type == GET_VALUE_ERROR) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "%{public}s, argv[PARAM0] error.", __func__); + return nullptr; + } + int level = authBuild.NapiGetValueInt(env, argv[PARAM1]); + if (level == GET_VALUE_ERROR) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "%{public}s, argv[PARAM1] error.", __func__); + return nullptr; + } + AuthType authType = AuthType(type); + AuthTurstLevel authTurstLevel = AuthTurstLevel(level); + int32_t result = UserAuth::GetInstance().GetAvailableStatus(authType, authTurstLevel); + USERAUTH_HILOGI(MODULE_JS_NAPI, "GetAvailabeStatus result = %{public}d", result); + napi_value ret = 0; + NAPI_CALL(env, napi_create_int32(env, result, &ret)); + return ret; +} + +napi_value UserAuthImpl::GetProperty(napi_env env, napi_callback_info info) +{ + AsyncHolder *asyncHolder = new (std::nothrow) AsyncHolder(); + if (asyncHolder == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "%{public}s asyncHolder nullptr", __func__); + return nullptr; + } + GetPropertyInfo *getPropertyInfo = new (std::nothrow) GetPropertyInfo(); + if (getPropertyInfo == nullptr) { + delete asyncHolder; + USERAUTH_HILOGE(MODULE_JS_NAPI, "%{public}s getPropertyInfo nullptr", __func__); + return nullptr; + } + getPropertyInfo->callBackInfo.env = env; + asyncHolder->data = getPropertyInfo; + napi_value ret = GetPropertyWrap(env, info, asyncHolder); + if (ret == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "%{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, AsyncHolder *asyncHolder) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{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; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); + if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "%{public}s, Wrong argument count.", __func__); + return nullptr; + } + if (argcAsync > PARAM1) { + napi_valuetype valuetype = napi_undefined; + napi_typeof(env, args[PARAM1], &valuetype); + if (valuetype == napi_function) { + 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 = nullptr; + if (argcAsync > argcPromise) { + ret = GetPropertyAsync(env, asyncHolder); + } else { + ret = GetPropertyPromise(env, asyncHolder); + } + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s,end.", __func__); + return ret; +} + +void UserAuthImpl::GetPropertyExecute(napi_env env, void *data) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "GetPropertyExecute, worker pool thread execute."); + AsyncHolder *asyncHolder = reinterpret_cast(data); + if (asyncHolder == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "GetPropertyExecute, asyncHolder == nullptr"); + return; + } + GetPropertyInfo *getPropertyInfo = reinterpret_cast(asyncHolder->data); + if (getPropertyInfo == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "GetPropertyExecute, getPropertyInfo == nullptr"); + return; + } + AuthType authTypeGet = AuthType(getPropertyInfo->authType); + + GetPropertyRequest request; + request.authType = authTypeGet; + request.keys = getPropertyInfo->keys; + USERAUTH_HILOGI(MODULE_JS_NAPI, "GetPropertyExecute start 1"); + GetPropApiCallback *object = new GetPropApiCallback(getPropertyInfo); + std::shared_ptr callback; + callback.reset(object); + UserAuth::GetInstance().GetProperty(request, callback); + USERAUTH_HILOGI(MODULE_JS_NAPI, "GetPropertyExecute, worker pool thread execute end."); +} + +void UserAuthImpl::GetPropertyPromiseExecuteDone(napi_env env, napi_status status, void *data) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "GetPropertyPromiseExecuteDone, start"); + AsyncHolder *asyncHolder = reinterpret_cast(data); + napi_delete_async_work(env, asyncHolder->asyncWork); + delete asyncHolder; + USERAUTH_HILOGI(MODULE_JS_NAPI, "GetPropertyPromiseExecuteDone, end"); +} + +void UserAuthImpl::GetPropertyAsyncExecuteDone(napi_env env, napi_status status, void *data) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "GetPropertyAsyncExecuteDone, start"); + AsyncHolder *asyncHolder = reinterpret_cast(data); + napi_delete_async_work(env, asyncHolder->asyncWork); + delete asyncHolder; + USERAUTH_HILOGI(MODULE_JS_NAPI, "GetPropertyAsyncExecuteDone, end"); +} + +napi_value UserAuthImpl::GetPropertyAsync(napi_env env, AsyncHolder *asyncHolder) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s, asyncCallback.", __func__); + 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 *)asyncHolder, &asyncHolder->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, asyncHolder->asyncWork)); + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s, asyncCallback end.", __func__); + return result; +} + +napi_value UserAuthImpl::GetPropertyPromise(napi_env env, AsyncHolder *asyncHolder) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s, promise.", __func__); + 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; + napi_value promise = nullptr; + 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 *)asyncHolder, &asyncHolder->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, asyncHolder->asyncWork)); + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{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) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "%{public}s asyncHolder nullptr", __func__); + return nullptr; + } + SetPropertyInfo *setPropertyInfo = new (std::nothrow) SetPropertyInfo(); + if (setPropertyInfo == nullptr) { + delete asyncHolder; + USERAUTH_HILOGE(MODULE_JS_NAPI, "%{public}s setPropertyInfo nullptr", __func__); + return nullptr; + } + setPropertyInfo->callBackInfo.env = env; + asyncHolder->data = setPropertyInfo; + napi_value ret = SetPropertyWrap(env, info, asyncHolder); + if (ret == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "%{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, AsyncHolder *asyncHolder) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{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; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); + if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "%{public}s, Wrong argument count.", __func__); + return nullptr; + } + if (argcAsync > PARAM1) { + napi_valuetype valuetype = napi_undefined; + napi_typeof(env, args[PARAM1], &valuetype); + if (valuetype == napi_function) { + NAPI_CALL(env, napi_create_reference(env, args[PARAM1], 1, &(setPropertyInfo->callBackInfo.callBack))); + } + } + + if (authBuild.NapiTypeObject(env, args[PARAM0])) { + Napi_SetPropertyRequest request = authBuild.SetPropertyRequestBuild(env, args[0]); + setPropertyInfo->authType = request.authType_; + setPropertyInfo->key = request.key_; + setPropertyInfo->setInfo = request.setInfo_; + } + + napi_value ret = 0; + if (argcAsync > argcPromise) { + ret = SetPropertyAsync(env, asyncHolder); + } else { + ret = SetPropertyPromise(env, asyncHolder); + } + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s,end.", __func__); + return ret; +} + +void UserAuthImpl::SetPropertyExecute(napi_env env, void *data) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "setPropertyExecute, worker pool thread execute."); + AsyncHolder *asyncHolder = reinterpret_cast(data); + if (asyncHolder == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "SetPropertyExecute, asyncHolder == nullptr"); + return; + } + SetPropertyInfo *setPropertyInfo = reinterpret_cast(asyncHolder->data); + if (setPropertyInfo == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "SetPropertyExecute, setPropertyInfo == nullptr"); + return; + } + AuthType authTypeGet = AuthType(setPropertyInfo->authType); + SetPropertyRequest request; + request.authType = authTypeGet; + request.key = SetPropertyType(setPropertyInfo->key); + request.setInfo = setPropertyInfo->setInfo; + SetPropApiCallback *object = new SetPropApiCallback(setPropertyInfo); + std::shared_ptr callback; + callback.reset(object); + UserAuth::GetInstance().SetProperty(request, callback); + USERAUTH_HILOGI(MODULE_JS_NAPI, "setPropertyExecute, worker pool thread execute end."); +} + +void UserAuthImpl::SetPropertyPromiseExecuteDone(napi_env env, napi_status status, void *data) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "SetPropertyPromiseExecuteDone, start"); + AsyncHolder *asyncHolder = reinterpret_cast(data); + napi_delete_async_work(env, asyncHolder->asyncWork); + delete asyncHolder; + USERAUTH_HILOGI(MODULE_JS_NAPI, "SetPropertyPromiseExecuteDone, end"); +} + +void UserAuthImpl::SetPropertyAsyncExecuteDone(napi_env env, napi_status status, void *data) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "SetPropertyAsyncExecuteDone, start"); + AsyncHolder *asyncHolder = reinterpret_cast(data); + napi_delete_async_work(env, asyncHolder->asyncWork); + delete asyncHolder; + USERAUTH_HILOGI(MODULE_JS_NAPI, "SetPropertyAsyncExecuteDone, end"); +} + +napi_value UserAuthImpl::SetPropertyAsync(napi_env env, AsyncHolder *asyncHolder) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s, asyncCallback.", __func__); + 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 *)asyncHolder, &asyncHolder->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, asyncHolder->asyncWork)); + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s, asyncCallback end.", __func__); + return result; +} + +napi_value UserAuthImpl::SetPropertyPromise(napi_env env, AsyncHolder *asyncHolder) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s, promise.", __func__); + 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 = 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 *)asyncHolder, &asyncHolder->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, asyncHolder->asyncWork)); + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s, promise end.", __func__); + return promise; +} + +napi_value UserAuthImpl::BuildAuthInfo(napi_env env, AuthInfo *authInfo) +{ + 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}; + NAPI_CALL(env, napi_get_cb_info(env, authInfo->info, &argc, argv, nullptr, nullptr)); + if (argc != ARGS_FOUR) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "%{public}s, parms error.", __func__); + return nullptr; + } + authInfo->challenge = authBuild.GetUint8ArrayTo64(env, argv[0]); + + if (authBuild.NapiTypeNumber(env, argv[PARAM1])) { + int64_t type; + NAPI_CALL(env, napi_get_value_int64(env, argv[PARAM1], &type)); + authInfo->authType = type; + } + + if (authBuild.NapiTypeNumber(env, argv[PARAM2])) { + int64_t level; + NAPI_CALL(env, napi_get_value_int64(env, argv[PARAM2], &level)); + authInfo->authTrustLevel = level; + } + + if (authBuild.NapiTypeObject(env, argv[PARAM3])) { + authInfo->jsFunction = argv[PARAM3]; + NAPI_CALL(env, napi_get_named_property(env, argv[PARAM3], "onResult", &authInfo->onResultCallBack)); + NAPI_CALL(env, napi_create_reference(env, authInfo->onResultCallBack, PARAM1, &authInfo->onResult)); + 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 result; +} + +napi_value UserAuthImpl::DoExecute(ExecuteInfo* executeInfo) +{ + AuthApiCallback *object = new (std::nothrow) AuthApiCallback(executeInfo); + if (object == nullptr) { + delete executeInfo; + return nullptr; + } + std::shared_ptr callback; + callback.reset(object); + std::map convertAuthTurstLevel = { + { "S1", ATL1 }, + { "S2", ATL2 }, + { "S3", ATL3 }, + { "S4", ATL4 }, + }; + if (convertAuthTurstLevel.count(executeInfo->level) == 0) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "execute convertAuthTurstLevel is 0"); + delete executeInfo; + return nullptr; + } + UserAuth::GetInstance().Auth(0, FACE, convertAuthTurstLevel[executeInfo->level], callback); + if (executeInfo->isPromise) { + return executeInfo->promise; + } else { + napi_value result = nullptr; + NAPI_CALL(executeInfo->env, napi_get_null(executeInfo->env, &result)); + return result; + } +} + +napi_value UserAuthImpl::Execute(napi_env env, napi_callback_info info) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s, start", __func__); + ExecuteInfo *executeInfo = new (std::nothrow) ExecuteInfo(); + if (executeInfo == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "%{public}s executeInfo nullptr", __func__); + return nullptr; + } + executeInfo->env = env; + size_t argc = ARGS_MAX_COUNT; + size_t callbackIndex = 0; + napi_value argv[ARGS_MAX_COUNT] = {nullptr}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); + if (argc < PARAM2) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "%{public}s argc check fail", __func__); + delete executeInfo; + return nullptr; + } + if (!GetExecuteInfo(env, argv, executeInfo)) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "%{public}s GetExecuteInfo fail", __func__); + delete executeInfo; + return nullptr; + } + callbackIndex = argc - 1; + napi_valuetype valuetype; + NAPI_CALL(env, napi_typeof(env, argv[callbackIndex], &valuetype)); + if (valuetype == napi_function) { + executeInfo->isPromise = false; + NAPI_CALL(env, napi_create_reference(env, argv[callbackIndex], 1, &executeInfo->callbackRef)); + } else { + executeInfo->isPromise = true; + NAPI_CALL(env, napi_create_promise(env, &executeInfo->deferred, &executeInfo->promise)); + } + return DoExecute(executeInfo); +} + +bool UserAuthImpl::GetExecuteInfo(napi_env env, napi_value* argv, ExecuteInfo* executeInfo) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s, start.", __func__); + napi_valuetype valuetype; + napi_typeof(env, argv[PARAM0], &valuetype); + if (valuetype != napi_string) { + return false; + } + char *str = nullptr; + size_t len = 0; + napi_get_value_string_utf8(env, argv[PARAM0], nullptr, 0, &len); + if (len > 0) { + str = new char[len + 1](); + napi_get_value_string_utf8(env, argv[PARAM0], str, len + 1, &len); + executeInfo->type = str; + delete[] str; + } + napi_typeof(env, argv[PARAM1], &valuetype); + if (valuetype != napi_string) { + return false; + } + napi_get_value_string_utf8(env, argv[PARAM1], nullptr, 0, &len); + if (len > 0) { + str = new char[len + 1](); + napi_get_value_string_utf8(env, argv[PARAM1], str, len + 1, &len); + executeInfo->level = str; + delete[] str; + } + if (executeInfo->type.compare("FACE_ONLY") != 0) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "%{public}s check type fail.", __func__); + return false; + } + return true; +} + +napi_value UserAuthImpl::Auth(napi_env env, napi_callback_info info) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s, start", __func__); + AuthInfo *authInfo = new (std::nothrow) AuthInfo(); + if (authInfo == nullptr) { + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s authInfo nullptr", __func__); + return nullptr; + } + authInfo->info = info; + napi_value ret = BuildAuthInfo(env, authInfo); + if (ret == nullptr) { + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s BuildAuthInfo fail", __func__); + delete authInfo; + return ret; + } + return AuthWrap(env, authInfo); +} + +napi_value UserAuthImpl::AuthWrap(napi_env env, AuthInfo *authInfo) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s, start.", __func__); + AuthApiCallback *object = new AuthApiCallback(authInfo); + std::shared_ptr callback; + callback.reset(object); + uint64_t result = UserAuth::GetInstance().Auth( + authInfo->challenge, AuthType(authInfo->authType), AuthTurstLevel(authInfo->authTrustLevel), callback); + USERAUTH_HILOGI(MODULE_JS_NAPI, "UserAuth::GetInstance().Auth.result = %{public}" PRIu64 "", result); + napi_value key = authBuild.Uint64ToUint8Array(env, result); + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s, end.", __func__); + return key; +} + +napi_value UserAuthImpl::AuthUser(napi_env env, napi_callback_info info) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s, start.", __func__); + AuthUserInfo *userInfo = new (std::nothrow) AuthUserInfo(); + if (userInfo == nullptr) { + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s userInfo nullptr", __func__); + return nullptr; + } + userInfo->callBackInfo.env = env; + userInfo->info = info; + napi_value ret = BuildAuthUserInfo(env, userInfo); + if (ret == nullptr) { + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{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)); + if (argc != ARGS_FIVE) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "%{public}s, parms error.", __func__); + return nullptr; + } + if (authBuild.NapiTypeNumber(env, argv[PARAM0])) { + int32_t id = 0; + NAPI_CALL(env, napi_get_value_int32(env, argv[PARAM0], &id)); + userInfo->userId = id; + } + userInfo->challenge = authBuild.GetUint8ArrayTo64(env, argv[PARAM1]); + if (authBuild.NapiTypeNumber(env, argv[PARAM2])) { + int32_t type = 0; + napi_get_value_int32(env, argv[PARAM2], &type); + userInfo->authType = type; + } + if (authBuild.NapiTypeNumber(env, argv[PARAM3])) { + int32_t level = 0; + NAPI_CALL(env, napi_get_value_int32(env, argv[PARAM3], &level)); + userInfo->authTrustLevel = level; + } + if (authBuild.NapiTypeObject(env, argv[PARAM4])) { + USERAUTH_HILOGI(MODULE_JS_NAPI, "AuthUser is Object"); + userInfo->jsFunction = argv[PARAM4]; + NAPI_CALL(env, napi_get_named_property(env, argv[PARAM4], "onResult", &userInfo->onResultCallBack)); + NAPI_CALL(env, napi_create_reference(env, userInfo->onResultCallBack, PARAM1, &userInfo->onResult)); + 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 result; +} + +napi_value UserAuthImpl::AuthUserWrap(napi_env env, AuthUserInfo *userInfo) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s, start.", __func__); + AuthApiCallback *object = new AuthApiCallback(userInfo); + std::shared_ptr callback; + callback.reset(object); + uint64_t result = UserAuth::GetInstance().AuthUser(userInfo->userId, userInfo->challenge, + AuthType(userInfo->authType), AuthTurstLevel(userInfo->authTrustLevel), callback); + USERAUTH_HILOGI(MODULE_JS_NAPI, "UserAuth::GetInstance().AuthUser. result = %{public}" PRIu64 "", result); + napi_value key = authBuild.Uint64ToUint8Array(env, result); + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s, end.", __func__); + return key; +} + +napi_value UserAuthImpl::CancelAuth(napi_env env, napi_callback_info info) +{ + size_t argc = ARGS_MAX_COUNT; + napi_value argv[ARGS_MAX_COUNT] = {nullptr}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); + + uint64_t contextId = authBuild.GetUint8ArrayTo64(env, argv[0]); + USERAUTH_HILOGI(MODULE_JS_NAPI, "CancelAuth contextId = %{public}" PRIu64 "", contextId); + if (contextId == 0) { + return nullptr; + } + int32_t result = UserAuth::GetInstance().CancelAuth(contextId); + USERAUTH_HILOGI(MODULE_JS_NAPI, "CancelAuth result = %{public}d", result); + napi_value key = 0; + NAPI_CALL(env, napi_create_int32(env, result, &key)); + return key; +} + +static napi_value ModuleInit(napi_env env, napi_value exports) +{ + OHOS::UserIAM::UserAuth::Init(env, exports); + return exports; +} +extern "C" __attribute__((constructor)) void RegisterModule(void) +{ + napi_module module = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = ModuleInit, + .nm_modname = "UserAuth", + .nm_priv = nullptr, + .reserved = {} + }; + napi_module_register(&module); +} +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS diff --git a/interfaces/kits/napi/BUILD.gn b/interfaces/kits/napi/BUILD.gn index a84551cd3..777be8efe 100755 --- a/interfaces/kits/napi/BUILD.gn +++ b/interfaces/kits/napi/BUILD.gn @@ -24,7 +24,6 @@ config("userauth_public_config") { ohos_shared_library("userauthnorth") { sources = [ - "src/auth_native_module.cpp", "src/user_auth_helper_north.cpp", ] @@ -36,6 +35,7 @@ ohos_shared_library("userauthnorth") { "//foundation/ace/napi:ace_napi", ] + configs = [ "${userauth_utils_path}:utils_config" ] public_configs = [ ":userauth_public_config" ] external_deps = [ "ipc:ipc_core" ] @@ -48,5 +48,5 @@ ohos_shared_library("userauthnorth") { public_deps = [] relative_install_dir = "module" part_name = "user_auth" - subsystem_name = "useriam" + subsystem_name = "user_iam" } diff --git a/interfaces/kits/napi/include/user_auth_helper_north.h b/interfaces/kits/napi/include/user_auth_helper_north.h index c6434c206..4f203d767 100755 --- a/interfaces/kits/napi/include/user_auth_helper_north.h +++ b/interfaces/kits/napi/include/user_auth_helper_north.h @@ -17,6 +17,7 @@ #include "napi/native_api.h" #include "napi/native_common.h" +#include "napi/native_node_api.h" namespace OHOS { namespace UserAuthNorth { diff --git a/interfaces/kits/napi/src/auth_native_module.cpp b/interfaces/kits/napi/src/auth_native_module.cpp deleted file mode 100755 index b826a2a11..000000000 --- a/interfaces/kits/napi/src/auth_native_module.cpp +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2021 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 - -#include "user_auth_helper_north.h" - -namespace OHOS { -namespace UserAuthNorth { -static napi_value ModuleInit(napi_env env, napi_value exports) -{ - OHOS::UserAuthNorth::Init(env, exports); - return exports; -} -extern "C" __attribute__((constructor)) void RegisterModule(void) -{ - napi_module module = { - .nm_version = 1, // NAPI v1 - .nm_flags = 0, // normal - .nm_filename = nullptr, - .nm_register_func = ModuleInit, - .nm_modname = "UserAuthNorth", - .nm_priv = nullptr, - .reserved = {} - }; - napi_module_register(&module); -} -} // namespace UserAuthNorth -} // namespace OHOS diff --git a/interfaces/kits/napi/src/user_auth_helper_north.cpp b/interfaces/kits/napi/src/user_auth_helper_north.cpp index 3ccc72517..ed0a34fe7 100755 --- a/interfaces/kits/napi/src/user_auth_helper_north.cpp +++ b/interfaces/kits/napi/src/user_auth_helper_north.cpp @@ -15,10 +15,11 @@ #include "user_auth_helper_north.h" -#include "auth_hilog_wrapper.h" #include "user_auth_impl.h" #include "user_auth_helper.h" +#include "userauth_hilog_wrapper.h" +using namespace OHOS::UserIAM::UserAuth; namespace OHOS { namespace UserAuthNorth { napi_value Constructor(napi_env env, napi_callback_info info) @@ -27,7 +28,7 @@ napi_value Constructor(napi_env env, napi_callback_info info) napi_value userAuth = nullptr; NAPI_CALL(env, napi_new_instance(env, GetCtor(env), 0, nullptr, &userAuth)); NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); - HILOG_INFO("UserAuthNorth, Constructor start"); + USERAUTH_HILOGI(MODULE_JS_NAPI, "UserAuthNorth, Constructor start"); return userAuth; } @@ -53,8 +54,27 @@ void Init(napi_env env, napi_value exports) }; status = napi_define_properties(env, exports, sizeof(exportFuncs) / sizeof(*exportFuncs), exportFuncs); if (status != napi_ok) { - HILOG_ERROR("napi_define_properties faild"); + USERAUTH_HILOGE(MODULE_JS_NAPI, "napi_define_properties faild"); } } + +static napi_value ModuleInit(napi_env env, napi_value exports) +{ + OHOS::UserAuthNorth::Init(env, exports); + return exports; +} +extern "C" __attribute__((constructor)) void RegisterModule(void) +{ + napi_module module = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = ModuleInit, + .nm_modname = "UserAuthNorth", + .nm_priv = nullptr, + .reserved = {} + }; + napi_module_register(&module); +} } // namespace UserAuthNorth } // namespace OHOS \ No newline at end of file diff --git a/utils/native/include/userauth_errors.h b/utils/native/include/userauth_errors.h deleted file mode 100644 index ebe2c4547..000000000 --- a/utils/native/include/userauth_errors.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2021 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. - */ - -#ifndef USERAUTH_ERRORS_H -#define USERAUTH_ERRORS_H - -#include - -namespace OHOS { -namespace UserIAM { -namespace UserAuth { -enum { - E_WRITE_PARCEL_ERROR = 0, - E_READ_PARCEL_ERROR, - E_GET_SYSTEM_ABILITY_MANAGER_FAILED, - E_GET_POWER_SERVICE_FAILED, - E_ADD_DEATH_RECIPIENT_FAILED, - E_INNER_ERR -}; -} // namespace UserAuth -} // namespace UserIam -} // namespace OHOS - -#endif // USERAUTH_ERRORS_H -- Gitee