diff --git a/README_ZH.md b/README_ZH.md index 0b898f8cbfe5ac7a97ad6870103f923e6a2573f7..cce87d8bc0a1e89657a82bd5750b0991e89fcd20 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -45,7 +45,7 @@ | 接口名 | 描述 | | ------ | -------------------------------- | -| getAvailabeStatus(authType : AuthType, authTrustLevel : AuthTurstLevel) : number; | 指定ATL,查询是否支持目标认证方式 | +| getAvailableStatus(authType : AuthType, authTrustLevel : AuthTurstLevel) : number; | 指定ATL,查询是否支持目标认证方式 | | auth(challenge: BigInt, authType : AuthType, authTrustLevel: AuthTurstLevel, callback: IUserAuthCallback): BigInt; | 指定ATL和认证方式,完成用户身份认证 | ### 使用说明 diff --git a/bundle.json b/bundle.json index 9112a9ba8f563c2320dba8d87c16187f22401e98..4cbefc74e11e831bd10322cdd6a6b2cb6c288da0 100644 --- a/bundle.json +++ b/bundle.json @@ -1,83 +1,102 @@ -{ - "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"], - "features": [], - "rom": "200KB", - "ram": "1500KB", - "deps": { - "components": [ - "hiviewdfx_hilog_native", - "ipc", - "safwk", - "samgr_standard", - "hilog", - "auth_executor_mgr" - ], - "third_party": [ - "openssl", - "googletest" - ] - }, - "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" - } - } - ], - "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_api7_callback.h", + "iuserauth_callback.h", + "user_auth.h", + "userauth_api7_callback.h", + "userauth_api7_datamgr.h", + "userauth_api7_info.h", + "userauth_api7.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/iuserauth_api7_callback.cpp b/frameworks/kitsimpl/src/iuserauth_api7_callback.cpp new file mode 100644 index 0000000000000000000000000000000000000000..250b604bd98fe33a93b104665fc0da6c58de13e8 --- /dev/null +++ b/frameworks/kitsimpl/src/iuserauth_api7_callback.cpp @@ -0,0 +1,121 @@ +/* + * 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 "userauth_api7_datamgr.h" +#include "userauth_api7_info.h" +#include "iuserauth_api7_callback.h" +namespace OHOS { +namespace UserIAM { +namespace API7UserAuth { +std::map g_convertResult = { + {UserAuth::SUCCESS, API7UserAuth::SUCCESS}, + {UserAuth::FAIL, API7UserAuth::GENERAL_ERROR}, + {UserAuth::GENERAL_ERROR, API7UserAuth::GENERAL_ERROR}, + {UserAuth::CANCELED, API7UserAuth::CANCELED}, + {UserAuth::TIMEOUT, API7UserAuth::TIMEOUT}, + {UserAuth::TYPE_NOT_SUPPORT, API7UserAuth::NO_SUPPORT}, + {UserAuth::TRUST_LEVEL_NOT_SUPPORT, API7UserAuth::NO_SUPPORT}, + {UserAuth::BUSY, API7UserAuth::BUSY}, + {UserAuth::INVALID_PARAMETERS, API7UserAuth::INVALID_PARAMETERS}, + {UserAuth::LOCKED, API7UserAuth::LOCKED}, + {UserAuth::NOT_ENROLLED, API7UserAuth::NOT_ENROLLED}, + {UserAuth::IPC_ERROR, API7UserAuth::GENERAL_ERROR}, + {UserAuth::INVALID_CONTEXTID, API7UserAuth::GENERAL_ERROR}, + {UserAuth::E_WRITE_PARCEL_ERROR, API7UserAuth::GENERAL_ERROR}, + {UserAuth::E_READ_PARCEL_ERROR, API7UserAuth::GENERAL_ERROR}, + {UserAuth::E_GET_POWER_SERVICE_FAILED, API7UserAuth::GENERAL_ERROR}, + {UserAuth::E_RET_UNDONE, API7UserAuth::GENERAL_ERROR}, + {UserAuth::E_RET_NOSERVER, API7UserAuth::GENERAL_ERROR}, +}; + +std::map g_convertTipInfo = { + { FACE_AUTH_TIP_TOO_BRIGHT, "the obtained facial image is too bright due to high illumination" }, + { FACE_AUTH_TIP_TOO_DARK, "the obtained facial image is too dark due to low illumination" }, + { FACE_AUTH_TIP_TOO_CLOSE, "the face is too close to the device" }, + { FACE_AUTH_TIP_TOO_FAR, "the face is too far away from the device" }, + { FACE_AUTH_TIP_TOO_HIGH, "the device is too high, and that only the upper part of the face is captured" }, + { FACE_AUTH_TIP_TOO_LOW, "the device is too low, and that only the lower part of the face is captured" }, + { FACE_AUTH_TIP_TOO_RIGHT, + "the device is deviated to the right, and that only the right part of the face is captured" }, + { FACE_AUTH_TIP_TOO_LEFT, "the device is deviated to the left, and that only the left part of the face is captured" }, + { FACE_AUTH_TIP_TOO_MUCH_MOTION, "the face moves too fast during facial information collection" }, + { FACE_AUTH_TIP_POOR_GAZE, "the face is not facing the device" }, + { FACE_AUTH_TIP_NOT_DETECTED, "no face is detected" }, +}; + +IUserAuthAPI7Callback::IUserAuthAPI7Callback(std::shared_ptr& impl) +{ + callback_ = impl; +} + +void IUserAuthAPI7Callback::onAcquireInfo(const int32_t module, const uint32_t acquireInfo, const int32_t extraInfo) +{ + using namespace UserAuth; + USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAPI7 onAcquireInfo is start"); + std::string tipOfInfo = ""; + if (UserAuthAPI7Datamgr::GetInstance().GetAPI7Tip()) { + USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAPI7 GetAPI7Tip is on"); + API7UserAuth::Tip tipInfo; + tipInfo.errorCode = API7UserAuth::SUCCESS; + tipInfo.tipEvent = API7UserAuth::ACQUIRE; + tipInfo.tipCode = acquireInfo; + if (g_convertTipInfo.count(static_cast(acquireInfo)) == 0) { + USERAUTH_HILOGE(MODULE_INNERKIT, "userauthAPI7 g_convertTipInfo is 0"); + tipOfInfo = "other error"; + } else { + tipOfInfo = g_convertTipInfo[static_cast(acquireInfo)]; + } + callback_->TipCallback(tipInfo); + USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAPI7 GetAPI7Tip is end"); + } + USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAPI7 onAcquireInfo is end"); +} + +void IUserAuthAPI7Callback::onResult(const int32_t result, const UserAuth::AuthResult extraInfo) +{ + using namespace UserAuth; + USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAPI7 onResult is start"); + AuthenticationResult authResult; + UserAuthAPI7Datamgr::GetInstance().API7DeleteContextID(); + callback_->API7OnResult(result); + if (UserAuthAPI7Datamgr::GetInstance().GetAPI7Tip()) { + USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAPI7 GetAPI7Tip is on"); + API7UserAuth::Tip tipInfo; + if (result != UserAuth::SUCCESS) { + USERAUTH_HILOGE(MODULE_INNERKIT, "userauthAPI7 result is failed"); + tipInfo.errorCode = API7UserAuth::FAILED; + tipInfo.tipInfo = "FAILED"; + } else { + tipInfo.errorCode = API7UserAuth::SUCCESSED; + tipInfo.tipInfo = "SUCCESSED"; + } + tipInfo.tipEvent = API7UserAuth::RESULT; + if (g_convertResult.count(static_cast(result)) == 0) { + USERAUTH_HILOGE(MODULE_INNERKIT, "userauthAPI7 g_convertResult is 0"); + authResult = static_cast(result); + } else { + authResult = g_convertResult[static_cast(result)]; + } + tipInfo.tipCode = authResult; + callback_->TipCallback(tipInfo); + USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAPI7 GetAPI7Tip is end"); + } + USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAPI7 onResult is end"); +} +} +} +} \ No newline at end of file diff --git a/frameworks/kitsimpl/src/userauth.cpp b/frameworks/kitsimpl/src/userauth.cpp index 9715e70902d644e8836f1cd5e6cd9e4bca9be2c1..88ad3c87b22e48f0e96cb6917aff69da49da04b0 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 @@ -95,51 +97,63 @@ int32_t UserAuth::GetAvailableStatus(const AuthType authType, const AuthTurstLev return ret; } -void UserAuth::GetProperty(const GetPropertyRequest request, std::shared_ptr callback) +void UserAuth::GetProperty(const GetPropertyRequest request, std::shared_ptr callback) { USERAUTH_HILOGD(MODULE_INNERKIT, "userauth GetProperty is start"); + if (callback == nullptr) { + USERAUTH_HILOGE(MODULE_INNERKIT, "userauth GetProperty callback is Null"); + return; + } auto proxy = GetProxy(); if (proxy == nullptr) { - callback->onSetExecutorProperty(E_RET_NOSERVER); - return ; + ExecutorProperty result = {}; + result.result = E_RET_NOSERVER; + callback->onGetProperty(result); + return; } - if (callback == nullptr) { - USERAUTH_HILOGE(MODULE_INNERKIT, "userauth GetProperty callback is Null"); + 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) +void UserAuth::SetProperty(const SetPropertyRequest request, std::shared_ptr callback) { USERAUTH_HILOGD(MODULE_INNERKIT, "userauth SetProperty is start"); + if (callback == nullptr) { + USERAUTH_HILOGE(MODULE_INNERKIT, "userauth SetProperty callback is Null"); + return; + } auto proxy = GetProxy(); if (proxy == nullptr) { - ExecutorProperty result; - result.result = E_RET_NOSERVER; - callback->onExecutorPropertyInfo(result); - return ; + callback->onSetProperty(E_RET_NOSERVER); + return; } - if (callback == nullptr) { - USERAUTH_HILOGE(MODULE_INNERKIT, "userauth SetProperty callback is Null"); + 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, std::shared_ptr callback) { USERAUTH_HILOGD(MODULE_INNERKIT, "userauth Auth is start"); + if (callback == nullptr) { + USERAUTH_HILOGE(MODULE_INNERKIT, "userauth Auth callback is Null"); + return INVALID_PARAMETERS; + } auto proxy = GetProxy(); if (proxy == nullptr) { return E_RET_NOSERVER; } - if (callback == nullptr) { - USERAUTH_HILOGE(MODULE_INNERKIT, "userauth Auth callback is Null"); - return INVALID_PARAMETERS; + sptr asyncStub = new (std::nothrow) UserAuthAsyncStub(callback); + if (asyncStub == nullptr) { + USERAUTH_HILOGE(MODULE_INNERKIT, "UserAuthAsyncStub failed, Auth IUserAuthCallback is nullptr"); + return GENERAL_ERROR; } - sptr asyncStub = new UserAuthAsyncStub(callback); uint64_t ret = proxy_->Auth(challenge, authType, authTurstLevel, asyncStub); return ret; } @@ -147,16 +161,19 @@ uint64_t UserAuth::AuthUser(const int32_t userId, const uint64_t challenge, cons const AuthTurstLevel authTurstLevel, std::shared_ptr callback) { USERAUTH_HILOGD(MODULE_INNERKIT, "userauth AuthUser is start"); + if (callback == nullptr) { + USERAUTH_HILOGE(MODULE_INNERKIT, "userauth AuthUser callback is Null"); + return INVALID_PARAMETERS; + } auto proxy = GetProxy(); if (proxy == nullptr) { return E_RET_NOSERVER; } - if (callback == nullptr) { - USERAUTH_HILOGE(MODULE_INNERKIT, "userauth AuthUser callback is Null"); - return INVALID_PARAMETERS; + sptr asyncStub = new (std::nothrow) UserAuthAsyncStub(callback); + if (asyncStub == nullptr) { + USERAUTH_HILOGE(MODULE_INNERKIT, "UserAuthAsyncStub failed, AuthUser IUserAuthCallback is nullptr"); + return GENERAL_ERROR; } - - sptr asyncStub = new UserAuthAsyncStub(callback); uint64_t ret = proxy_->AuthUser(userId, challenge, authType, authTurstLevel, asyncStub); return ret; } diff --git a/frameworks/kitsimpl/src/userauth_api7.cpp b/frameworks/kitsimpl/src/userauth_api7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4b59008d41378b09a70d1d1fb67cb98f12a2560f --- /dev/null +++ b/frameworks/kitsimpl/src/userauth_api7.cpp @@ -0,0 +1,156 @@ +/* + * 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_info.h" +#include "user_auth.h" +#include "userauth_hilog_wrapper.h" +#include "userauth_api7_datamgr.h" +#include "iuserauth_api7_callback.h" +#include "userauth_api7.h" + +namespace OHOS { +namespace UserIAM { +namespace API7UserAuth { +std::map g_convertAuthType = { + { "FACE_ONLY", UserAuth::FACE }, +}; +std::map g_convertAuthTurstLevel = { + { "S1", UserAuth::ATL1 }, + { "S2", UserAuth::ATL2 }, + { "S3", UserAuth::ATL3 }, + { "S4", UserAuth::ATL4 }, +}; + +UserAuthAPI7 &UserAuthAPI7::GetAuthenticator() +{ + static UserAuthAPI7 instance; + return instance; +} + +void UserAuthAPI7::Execute(std::string authtype, std::string trustLevel, std::shared_ptr callback) +{ + using namespace UserAuth; + USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAPI7 execute is start"); + AuthType exeAuthType; + AuthTurstLevel authTurstLevel; + uint64_t contextID = 0; + + if (callback == nullptr) { + USERAUTH_HILOGE(MODULE_INNERKIT, "userauthAPI7 execute callback is Null"); + return; + } + + if (g_convertAuthType.count(authtype) == 0) { + USERAUTH_HILOGE(MODULE_INNERKIT, "userauthAPI7 execute g_convertAuthType is 0"); + callback->API7OnResult(NO_SUPPORT); + return; + } else { + exeAuthType = g_convertAuthType[authtype]; + } + + if (g_convertAuthTurstLevel.count(trustLevel) == 0) { + USERAUTH_HILOGE(MODULE_INNERKIT, "userauthAPI7 execute g_convertAuthTurstLevel is 0"); + callback->API7OnResult(NO_SUPPORT); + return; + } else { + authTurstLevel = g_convertAuthTurstLevel[trustLevel]; + } + + std::shared_ptr asyncStub = std::make_shared(callback); + if (asyncStub == nullptr) { + USERAUTH_HILOGE(MODULE_INNERKIT, "UserAuthCallback failed, IUserAuthAPI7Callback is nullptr"); + return; + } + contextID = UserIAM::UserAuth::UserAuth::GetInstance().Auth(CHALLENGE_INIT, exeAuthType, authTurstLevel, asyncStub); + if (contextID != CONTEXTID_INIT) { + UserAuthAPI7Datamgr::GetInstance().API7AddContextID(contextID); + } + USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAPI7 execute is end"); +} + +int32_t UserAuthAPI7::Cancel() +{ + using namespace UserAuth; + USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAPI7 cancel is start"); + uint64_t contextID = 0; + int32_t ret = API7UserAuth::GENERAL_ERROR; + + UserAuthAPI7Datamgr::GetInstance().API7GetContextID(contextID); + if (contextID != CONTEXTID_INIT) { + ret = UserIAM::UserAuth::UserAuth::GetInstance().CancelAuth(contextID); + if (ret != API7UserAuth::SUCCESS) { + USERAUTH_HILOGE(MODULE_INNERKIT, "userauthAPI7 cancel is error"); + } + UserAuthAPI7Datamgr::GetInstance().API7DeleteContextID(); + } + USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAPI7 cancel is end"); + return ret; +} + +int32_t UserAuthAPI7::CheckAvailability(std::string authtype, std::string trustLevel) +{ + using namespace UserAuth; + USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAPI7 checkAvailability is start"); + AuthType checkAuthType; + AuthTurstLevel authTurstLevel; + + if (g_convertAuthType.count(authtype) == 0) { + USERAUTH_HILOGE(MODULE_INNERKIT, "userauthAPI7 checkAvailability g_convertAuthType is 0"); + return AUTH_TYPE_NOT_SUPPORT; + } else { + checkAuthType = g_convertAuthType[authtype]; + } + + if (g_convertAuthTurstLevel.count(trustLevel) == 0) { + USERAUTH_HILOGE(MODULE_INNERKIT, "userauthAPI7 checkAvailability g_convertAuthTurstLevel is 0"); + return SECURE_LEVEL_NOT_SUPPORT; + } else { + authTurstLevel = g_convertAuthTurstLevel[trustLevel]; + } + + int32_t ret = UserIAM::UserAuth::UserAuth::GetInstance().GetAvailableStatus(checkAuthType, authTurstLevel); + if (ret != SUCCESS) { + USERAUTH_HILOGE(MODULE_INNERKIT, "userauthAPI7 checkAvailability is error "); + } + USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAPI7 checkAvailability is end"); + return ret; +} + +void UserAuthAPI7::On(std::string tip, std::shared_ptr tipCallback) +{ + using namespace UserAuth; + USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAPI7 On is start"); + std::string tipInfo = "tip"; + + if (tipInfo.compare(tip)) { + USERAUTH_HILOGE(MODULE_INNERKIT, "userauthAPI7 On is not tip"); + UserAuthAPI7Datamgr::GetInstance().SetAPI7Tip(false); + return; + } + UserAuthAPI7Datamgr::GetInstance().SetAPI7Tip(true); + USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAPI7 On is end"); +} + +void UserAuthAPI7::Off(std::string tip, std::shared_ptr tipCallback) +{ + using namespace UserAuth; + USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAPI7 Off is start"); + UserAuthAPI7Datamgr::GetInstance().SetAPI7Tip(false); + USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAPI7 Off is end"); +} +} // namespace userAuth +} // namespace UserIam +} // namespace OHOS diff --git a/interfaces/kits/napi/src/auth_native_module.cpp b/frameworks/kitsimpl/src/userauth_api7_datamgr.cpp old mode 100755 new mode 100644 similarity index 47% rename from interfaces/kits/napi/src/auth_native_module.cpp rename to frameworks/kitsimpl/src/userauth_api7_datamgr.cpp index b826a2a11d437e75457c493e4a248b1a1d451257..ea3dbcf887583e7be6baeadb735e341ff0b6386e --- a/interfaces/kits/napi/src/auth_native_module.cpp +++ b/frameworks/kitsimpl/src/userauth_api7_datamgr.cpp @@ -13,29 +13,42 @@ * limitations under the License. */ -#include - -#include "user_auth_helper_north.h" +#include "userauth_info.h" +#include "userauth_api7_info.h" +#include "userauth_api7_datamgr.h" namespace OHOS { -namespace UserAuthNorth { -static napi_value ModuleInit(napi_env env, napi_value exports) +namespace UserIAM { +namespace API7UserAuth { +UserAuthAPI7Datamgr &UserAuthAPI7Datamgr::GetInstance() +{ + static UserAuthAPI7Datamgr instance; + return instance; +} + +void UserAuthAPI7Datamgr::API7AddContextID(uint64_t contextID) +{ + contextID_ = contextID; +} + +void UserAuthAPI7Datamgr::API7GetContextID(uint64_t &contextID) +{ + contextID = contextID_; +} + +void UserAuthAPI7Datamgr::API7DeleteContextID() +{ + contextID_= CONTEXTID_INIT; +} + +void UserAuthAPI7Datamgr::SetAPI7Tip(bool isTipOn) { - OHOS::UserAuthNorth::Init(env, exports); - return exports; + isTipOn_ = isTipOn; } -extern "C" __attribute__((constructor)) void RegisterModule(void) +bool UserAuthAPI7Datamgr::GetAPI7Tip() { - 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); + return isTipOn_; } -} // namespace UserAuthNorth +} // namespace UserAuth +} // namespace UserIam } // namespace OHOS diff --git a/frameworks/kitsimpl/src/userauth_async_stub.cpp b/frameworks/kitsimpl/src/userauth_async_stub.cpp index 3e2fbaf654f519d36ca3f5813d38ccf0382493c9..6d1832fa5c181b33174dec28a26f741e8fff8316 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" @@ -24,7 +25,15 @@ namespace UserIAM { namespace UserAuth { UserAuthAsyncStub::UserAuthAsyncStub(std::shared_ptr& impl) { - callback_ = impl; + authCallback_ = impl; +} +UserAuthAsyncStub::UserAuthAsyncStub(std::shared_ptr& impl) +{ + getPropCallback_ = impl; +} +UserAuthAsyncStub::UserAuthAsyncStub(std::shared_ptr& impl) +{ + setPropCallback_ = impl; } int32_t UserAuthAsyncStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) @@ -189,56 +198,58 @@ int32_t UserAuthAsyncStub::onSetExecutorPropertyStub(MessageParcel& data, Messag void UserAuthAsyncStub::onAcquireInfo(const int32_t module, const uint32_t acquireInfo, const int32_t extraInfo) { - USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAsyncStub onAcquireInfo enter"); - USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAsyncStub module:%{public}d", module); - USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAsyncStub acquireInfo:%{public}d", acquireInfo); - USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAsyncStub extraInfo:%{public}d", extraInfo); - if (callback_ == nullptr) { - USERAUTH_HILOGE(MODULE_INNERKIT, "userauthAsyncStub onAcquireInfo callback_ is Null"); - return ; - } - callback_->onAcquireInfo(module, acquireInfo, extraInfo); + USERAUTH_HILOGD(MODULE_INNERKIT, "UserAuthAsyncStub onAcquireInfo enter"); + + if (authCallback_ == nullptr) { + USERAUTH_HILOGE(MODULE_INNERKIT, "userauthAsyncStub onAcquireInfo callback is Null"); + return; + } + + USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAsyncStub module:%{public}d, acquireInfo:%{public}u", + module, acquireInfo); + + authCallback_->onAcquireInfo(module, acquireInfo, extraInfo); } -void UserAuthAsyncStub::onResult(const int32_t result, const AuthResult extraInfot) +void UserAuthAsyncStub::onResult(const int32_t result, const AuthResult extraInfo) { - USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAsyncStub onResult enter"); - USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAsyncStub result:%{public}d", result); - USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAsyncStub remainTimes:%{public}d", extraInfot.remainTimes); - USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAsyncStub freezingTime:%{public}d", extraInfot.freezingTime); + USERAUTH_HILOGD(MODULE_INNERKIT, "UserAuthAsyncStub onResult enter"); - if (callback_ == nullptr) { - USERAUTH_HILOGE(MODULE_INNERKIT, "userauthAsyncStub onResult callback_ is Null"); - return ; + if (authCallback_ == nullptr) { + USERAUTH_HILOGE(MODULE_INNERKIT, "userauthAsyncStub onResult callback is Null"); + return; } - callback_->onResult(result, extraInfot); + + USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAsyncStub result:%{public}d, remain:%{public}u, freeze:%{public}u", + result, extraInfo.remainTimes, extraInfo.freezingTime); + authCallback_->onResult(result, extraInfo); } void UserAuthAsyncStub::onExecutorPropertyInfo(const ExecutorProperty result) { USERAUTH_HILOGD(MODULE_INNERKIT, "UserAuthAsyncStub onExecutorPropertyInfo enter"); - if (callback_ == nullptr) { - USERAUTH_HILOGE(MODULE_INNERKIT, "UserAuthAsyncStub onExecutorPropertyInfo callback_ is Null"); - return ; + if (getPropCallback_ == nullptr) { + USERAUTH_HILOGE(MODULE_INNERKIT, "UserAuthAsyncStub onExecutorPropertyInfo callback is Null"); + return; } - USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAsyncStub result:%{public}d", result.result); - USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAsyncStub authSubType:%{public}llu", result.authSubType); - USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAsyncStub remainTimes:%{public}d", result.freezingTime); - USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAsyncStub freezingTime:%{public}d", result.freezingTime); - callback_->onExecutorPropertyInfo(result); + + USERAUTH_HILOGD(MODULE_INNERKIT, + "userauthAsyncStub result:%{public}d, sub:%{public}" PRIu64 ", remain:%{public}u, freeze:%{public}u", + result.result, result.authSubType, result.remainTimes, result.freezingTime); + getPropCallback_->onGetProperty(result); } void UserAuthAsyncStub::onSetExecutorProperty(const int32_t result) { USERAUTH_HILOGD(MODULE_INNERKIT, "UserAuthAsyncStub onSetExecutorProperty enter"); - if (callback_ == nullptr) { - USERAUTH_HILOGE(MODULE_INNERKIT, "UserAuthAsyncStub onSetExecutorProperty callback_ is Null"); - return ; + if (setPropCallback_ == nullptr) { + USERAUTH_HILOGE(MODULE_INNERKIT, "UserAuthAsyncStub onSetExecutorProperty callback is Null"); + return; } - USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAsyncStub result:%{public}d", result); - callback_->onSetExecutorProperty(result); + USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAsyncStub result:%{public}d", result); + setPropCallback_->onSetProperty(result); } } // namespace UserAuth } // namespace UserIAM diff --git a/frameworks/kitsimpl/src/userauth_proxy.cpp b/frameworks/kitsimpl/src/userauth_proxy.cpp index 65c9093aca2e7059913c7f16b0316ef22f993ef5..cbab777097ab9d9ad1f5a8485c461bda98257e9d 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/js/ohos.UserIAM.userAuth.d.ts b/interfaces/innerkits/js/ohos.UserIAM.userAuth.d.ts index 527826a6d058aa35232e86e463a06c9b5edfe7d2..3599a1b85fe608daa1b329a3ec6db442b519eece 100755 --- a/interfaces/innerkits/js/ohos.UserIAM.userAuth.d.ts +++ b/interfaces/innerkits/js/ohos.UserIAM.userAuth.d.ts @@ -49,9 +49,9 @@ declare namespace userIAM *

Permissions required: {@code ohos.permission.ACCESS_USER_AUTH_INTERNAL} * @param authType Credential type for authentication. * @param authTrustLevel Credibility level of certification results. - * @return Returns a check result, which is specified by getAvailabeStatus. + * @return Returns a check result, which is specified by getAvailableStatus. */ - getAvailabeStatus(authType : AuthType, authTrustLevel : AuthTrustLevel) : number; + getAvailableStatus(authType : AuthType, authTrustLevel : AuthTrustLevel) : number; /** * Get the attribute, pass in the credential type and the key to get, and return the value corresponding to the key. diff --git a/interfaces/innerkits/js/ohos.UserIAM.userAuth_API7.d.ts b/interfaces/innerkits/js/ohos.UserIAM.userAuth_API7.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..5eca79f7b3ad0ddaed8745c2c8e4795eb85974a1 --- /dev/null +++ b/interfaces/innerkits/js/ohos.UserIAM.userAuth_API7.d.ts @@ -0,0 +1,336 @@ +/* + * 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. + */ + +import{AsyncCallback} from './basic'; + +/** + * User authentication + * @since 8 + * @sysCap SystemCapability.UserIAM.UserAuth.BiometricAuth + * @devices phone, tablet + * @permission ohos.permission.ACCESS_BIOMETRIC + */ +declare namespace userIAM +{ + class Authenticator + { + /** + * Execute authentication. + * @since 8 + * @SysCap SystemCapability.UserIAM.UserAuth.BiometricAuth + * @param type Indicates the authentication type. + * @param level Indicates the security level. + * @return Returns authentication result, which is specified by AuthenticationResult. + */ + execute(type: string, level: string, callback: AsyncCallback): void; + execute(type:string, level:string): Promise; + + /** + * Cancels the current authentication. + * @since 8 + * @SysCap SystemCapability.UserIAM.UserAuth.BiometricAuth + * @return Returns a cancel result, which is specified by Result. + */ + cancel(): number; + + /** + * Checks whether the device supports the specified authentication type and security level. + * @since 8 + * @SysCap SystemCapability.UserIAM.UserAuth.BiometricAuth + * @param type Indicates the authentication type. + * @param level Indicates the security level. + * @return Returns a check result, which is specified by CheckAvailabilityResult. + */ + checkAvailability(type: AuthType, level: SecureLevel): number + + /** + * Registers an event callback. + * @since 8 + * @SysCap SystemCapability.UserIAM.UserAuth.BiometricAuth + * @param type Indicates the event type. + * @param callback Indicates the authentication tip callback to register. + */ + on(type: "tip", callback: Callback) : void; + + /** + * Unregisters an event callback. + * @since 8 + * @SysCap SystemCapability.UserIAM.UserAuth.BiometricAuth + * @param type Indicates the event type. + * @param callback Indicates the authentication tip callback to unregister. + */ + off(type: "tip", callback?: Callback) : void; + } + + /** + * Get Authenticator instance. + * @SysCap SystemCapability.UserIAM.UserAuth.BiometricAuth + * @return Returns an Authenticator. + */ + function getAuthenticator(): Authenticator; + + /** + * Auth types + */ + type AuthType = "ALL" | "FACE_ONLY"; + + /** + * Secure levels + */ + type SecureLevel = "S1" | "S2" | "S3" | "S4"; + + interface Tip { + /** + * Indicates whether authentication tip is obtained, defined in Result. + * @since 8 + */ + errorCode: number; + + /** + * Indicates an authentication tip event, defined in TipEvent. + * @since 8 + */ + tipEvent: number; + + /** + * Indicates the return code of an authentication tip, defined in TipCode. + * @since 8 + */ + tipCode: number; + + /** + * Indicates the description of an authentication tip code. + * @since 8 + */ + tipInfo: string; + } + + export enum AuthenticationResult { + /** + * Indicates that the device does not support authentication. + */ + NO_SUPPORT = -1, + + /** + * Indicates that authentication is success. + */ + SUCCESS = 0, + + /** + * Indicates the authenticator fails to identify user. + */ + COMPARE_FAILURE = 1, + + /** + * Indicates that authentication has been canceled. + */ + CANCELED = 2, + + /** + * Indicates that authentication has timed out. + */ + TIMEOUT = 3, + + /** + * Indicates a failure to open the camera. + */ + CAMERA_FAIL = 4, + + /** + * Indicates that the authentication task is busy. Wait for a few seconds and try again. + */ + BUSY = 5, + + /** + * Indicates incorrect parameters. + */ + INVALID_PARAMETERS = 6, + + /** + * Indicates that the authenticator is locked. + */ + LOCKED = 7, + + /** + * Indicates that the user has not enrolled the authenticator. + */ + NOT_ENROLLED = 8, + + /** + * Indicates other errors. + */ + GENERAL_ERROR = 100, + } + + export enum Result { + /** + * Indicates that execute is success. + * @since 8 + */ + SUCCESS = 0, + + /** + * Indicates that execute is failed. + * @since 8 + */ + FAILED = 1, + } + + export enum CheckAvailabilityResult { + /** + * Indicates that the device supports the specified authentication type and security level. + * @since 8 + */ + SUPPORTED = 0, + + /** + * Indicates that the device does not support the specified authentication type. + * @since 8 + */ + AUTH_TYPE_NOT_SUPPORT = 1, + + /** + * Indicates that the device does not support the specified authentication security level. + * @since 8 + */ + SECURE_LEVEL_NOT_SUPPORT = 2, + + /** + * Indicates that the device does not support distributed authentication. + * @since 8 + */ + DISTRIBUTED_AUTH_NOT_SUPPORT = 3, + + /** + * Indicates that the device does not have the authentication credential. + * @since 8 + */ + NOT_ENROLLED = 4, + + /** + * Indicates that the number of parameters is incorrect. + * @since 8 + */ + PARAM_NUM_ERROR = 5, + } + + export enum TipEvent { + /** + * Enroll or authenticate result. + * @since 8 + */ + RESULT = 1, + + /** + * Cancel result. + * @since 8 + */ + CANCEL = 2, + + /** + * Prompt for user enroll and authentication. + * @since 8 + */ + ACQUIRE = 3, + + /** + * Busy. + * @since 8 + */ + BUSY = 4, + + /** + * Out of memory. + * @since 8 + */ + OUT_OF_MEM = 5, + + /** + * Index of a face authentication credential. + * @since 8 + */ + FACE_ID = 6, + } + + export enum TipCode { + /** + * Indicates that the obtained facial image is too bright due to high illumination. + * @since 8 + */ + FACE_AUTH_TIP_TOO_BRIGHT = 1, + + /** + * Indicates that the obtained facial image is too dark due to low illumination. + * @since 8 + */ + FACE_AUTH_TIP_TOO_DARK = 2, + + /** + * Indicates that the face is too close to the device. + * @since 8 + */ + FACE_AUTH_TIP_TOO_CLOSE = 3, + + /** + * Indicates that the face is too far away from the device. + * @since 8 + */ + FACE_AUTH_TIP_TOO_FAR = 4, + + /** + * Indicates that the device is too high, and that only the upper part of the face is captured. + * @since 8 + */ + FACE_AUTH_TIP_TOO_HIGH = 5, + + /** + * Indicates that the device is too low, and that only the lower part of the face is captured. + * @since 8 + */ + FACE_AUTH_TIP_TOO_LOW = 6, + + /** + * Indicates that the device is deviated to the right, and that only the right part of the face is captured. + * @since 8 + */ + FACE_AUTH_TIP_TOO_RIGHT = 7, + + /** + * Indicates that the device is deviated to the left, and that only the left part of the face is captured. + * @since 8 + */ + FACE_AUTH_TIP_TOO_LEFT = 8, + + /** + * Indicates that the face moves too fast during facial information collection. + * @since 8 + */ + FACE_AUTH_TIP_TOO_MUCH_MOTION = 9, + + /** + * Indicates that the face is not facing the device. + * @since 8 + */ + FACE_AUTH_TIP_POOR_GAZE = 10, + + /** + * Indicates that no face is detected. + * @since 8 + */ + FACE_AUTH_TIP_NOT_DETECTED = 11, + } +} + +export default userIAM; \ No newline at end of file diff --git a/interfaces/innerkits/napi/BUILD.gn b/interfaces/innerkits/napi/BUILD.gn index 66db8e7cc3a3816cb5bcee7a49010eb55a1a16ea..2e616129e3e8150edc5c73347f57034ef705b21e 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", @@ -58,3 +57,42 @@ ohos_shared_library("userauth") { part_name = "user_auth" subsystem_name = "user_iam" } + +ohos_shared_library("userauth_napi") { + cflags = [ + "-fPIC", + "-g3", + ] + + defines = [ "USER_AUTH_FOR_API6" ] + + sources = [ + "src/auth_build.cpp", + "src/authapi_callback.cpp", + "src/result_convert.cpp", + "src/user_auth_helper.cpp", + "src/user_auth_impl.cpp", + ] + + deps = [ + "${userauth_innerkits_path}/native:userauth_framework", + "//foundation/ace/napi:ace_napi", + "//utils/native/base:utils", + ] + + configs = [ "${userauth_utils_path}:utils_config" ] + + public_configs = [ ":userauth_public_config" ] + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_standard:samgr_proxy", + ] + + relative_install_dir = "module/useriam" + + part_name = "user_auth" + subsystem_name = "user_iam" +} diff --git a/interfaces/innerkits/napi/include/auth_common.h b/interfaces/innerkits/napi/include/auth_common.h index dcecddd6092b8aa70753c73984bf1a3039994148..232e62058b7e54a8ae2ab416c52aab829954e19d 100755 --- a/interfaces/innerkits/napi/include/auth_common.h +++ b/interfaces/innerkits/napi/include/auth_common.h @@ -1,128 +1,167 @@ -/* - * 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 "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 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 + +#include "napi/native_api.h" +#include "napi/native_common.h" +#include "napi/native_node_api.h" + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +#define RESULTVAR 0 +#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; +}; +struct NapiExecuteInfo { + NapiExecuteInfo() : asyncWork(nullptr){}; + napi_env env; + std::string type; + std::string level; + napi_async_work asyncWork; + napi_ref callbackRef; + napi_deferred deferred; + napi_value promise; + napi_value result; +}; + +struct CheckAvailabilityInfo { + napi_env env; + std::string type; + std::string level; +}; + +struct FunctionOnAndOffInfo { + napi_env env; + std::string tip; + napi_ref callbackRef; + napi_value param[PARAM1] = {0}; +}; +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS +#endif /* OHOS_USERAUTH_COMMON_H */ diff --git a/interfaces/innerkits/napi/include/auth_object.h b/interfaces/innerkits/napi/include/auth_object.h index c1fbf65c93f6582cd685d5fcec67a16bfaac404e..985c80b5db8d91129204c379e0bc7aaa25823c1d 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/authapi_callback.h b/interfaces/innerkits/napi/include/authapi_callback.h index 87fbf6de14e2a95dcb8da747e2f0ad0ba23f13c6..4be4001387bfcc2d5aefe2baf5c9415b85b4afdb 100755 --- a/interfaces/innerkits/napi/include/authapi_callback.h +++ b/interfaces/innerkits/napi/include/authapi_callback.h @@ -1,66 +1,106 @@ -/* - * 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 AUTHAPI_CALLBACK_H -#define AUTHAPI_CALLBACK_H - -#include "napi/native_api.h" -#include "napi/native_common.h" - -#include "userauth_callback.h" -#include "userauth_info.h" - -#include "auth_common.h" - -namespace OHOS { -namespace UserIAM { -namespace UserAuth { -typedef struct AcquireInfoInner { - napi_env env; - napi_ref onAcquireInfo; - napi_value jsFunction; - int32_t module; - uint32_t acquireInfo; - int32_t extraInfo; -} AcquireInfoInner; -class AuthApiCallback : public UserAuthCallback { -public: - AuthApiCallback(); - virtual ~AuthApiCallback(); - GetPropertyInfo *getPropertyInfo_; - SetPropertyInfo *setPropertyInfo_; - AuthInfo *authInfo_; - AuthUserInfo *userInfo_; - void onExecutorPropertyInfo(const ExecutorProperty result) override; - void onAcquireInfo(const int32_t module, const uint32_t acquireInfo, const int32_t extraInfo) override; - void onResult(const int32_t result, const AuthResult extraInfo) override; - void onSetExecutorProperty(const int32_t result) override; - - static napi_value BuildExecutorProperty( - napi_env env, int32_t result, uint32_t remainTimes, uint32_t freezingTime, uint64_t authSubType); - static napi_value Uint64ToNapi(napi_env env, uint64_t value); - static napi_value BuildOnResult( - napi_env env, uint32_t remainTimes, uint32_t freezingTime, std::vector token); - static napi_value Uint8ArrayToNapi(napi_env env, std::vector value); - -private: - void OnAuthAcquireInfo(AcquireInfoInner *acquireInfoInner); - void OnUserAuthResult(const int32_t result, const AuthResult extraInfo); - void OnAuthResult(const int32_t result, const AuthResult extraInfo); -}; -} // namespace UserAuth -} // namespace UserIAM -} // namespace OHOS -#endif // AUTHAPI_CALLBACK_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 AUTHAPI_CALLBACK_H +#define AUTHAPI_CALLBACK_H + +#include +#include "napi/native_api.h" +#include "napi/native_common.h" + +#include "userauth_callback.h" +#include "userauth_info.h" + +#include "userauth_api7_info.h" +#include "userauth_api7_callback.h" +#include "auth_common.h" + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +typedef struct AcquireInfoInner { + napi_env env; + napi_ref onAcquireInfo; + napi_value jsFunction; + int32_t module; + uint32_t acquireInfo; + int32_t extraInfo; +} AcquireInfoInner; +class AuthApiCallback : public UserAuthCallback { +public: + AuthApiCallback(AuthInfo *authInfo); + AuthApiCallback(AuthUserInfo *userInfo); + AuthApiCallback(ExecuteInfo *executeInfo); + virtual ~AuthApiCallback(); + void onAcquireInfo(const int32_t module, const uint32_t acquireInfo, const int32_t extraInfo) override; + void onResult(const int32_t result, const AuthResult extraInfo) override; + + static napi_value BuildOnResult( + napi_env env, uint32_t remainTimes, uint32_t freezingTime, std::vector token); + static napi_value Uint8ArrayToNapi(napi_env env, std::vector value); + +private: + void OnAuthAcquireInfo(AcquireInfoInner *acquireInfoInner); + void OnUserAuthResult(const int32_t result, const AuthResult extraInfo); + void OnAuthResult(const int32_t result, const AuthResult extraInfo); + void OnExecuteResult(const int32_t result); + + AuthInfo *authInfo_; + AuthUserInfo *userInfo_; + ExecuteInfo *executeInfo_; +}; + +class GetPropApiCallback : public GetPropCallback { +public: + GetPropApiCallback(GetPropertyInfo *getPropertyInfo); + virtual ~GetPropApiCallback(); + void onGetProperty(const ExecutorProperty result) override; + + static napi_value BuildExecutorProperty( + napi_env env, int32_t result, uint32_t remainTimes, uint32_t freezingTime, uint64_t authSubType); + +private: + GetPropertyInfo *getPropertyInfo_; +}; + +class SetPropApiCallback : public SetPropCallback { +public: + SetPropApiCallback(SetPropertyInfo *setPropertyInfo); + virtual ~SetPropApiCallback(); + void onSetProperty(const int32_t result) override; + +private: + SetPropertyInfo *setPropertyInfo_; +}; + +class API7callback : public API7UserAuth::UserAuthAPI7Callback { +public: + explicit API7callback(NapiExecuteInfo* executeInfo); + explicit API7callback(FunctionOnAndOffInfo* functionOnAndOffInfo); + virtual ~API7callback() = default; + NapiExecuteInfo* executeInfo_; + FunctionOnAndOffInfo* functionOnAndOffInfo_; + void API7OnResult(const int32_t result) override; + void TipCallback(const API7UserAuth::Tip result) override; +private: + napi_value ResultTips(napi_env env, const API7UserAuth::Tip result); + void API7OnResultWrap(uv_loop_s *loop, uv_work_t *work); + void TipCallbackWrap(uv_loop_s *loop, uv_work_t *work); + std::mutex mutex_; +}; +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS +#endif // AUTHAPI_CALLBACK_H diff --git a/interfaces/innerkits/napi/include/user_auth_helper.h b/interfaces/innerkits/napi/include/user_auth_helper.h index a75856cbf264ab01185f2adae8abc7d7d7c04826..fff968ab3fc6a14414bad592bc6e858ed1acc9a5 100755 --- a/interfaces/innerkits/napi/include/user_auth_helper.h +++ b/interfaces/innerkits/napi/include/user_auth_helper.h @@ -1,124 +1,195 @@ -/* - * 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 GetAvailabeStatus(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); - -/** - * @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" +#include "napi/native_node_api.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 Get the Ctor object + * + * @param env + * @return napi_value AuthenticatorImpl Instance + */ +napi_value GetCtorAPI7(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 Get the Ctor object + * + * @param env + * @return napi_value AuthenticatorImpl Instance + */ +napi_value GetAuthenticator(napi_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 Instance passed to context + * + * @param env + * @param info + * @return napi_value AuthenticatorImpl Instance + */ +napi_value AuthenticatorServiceConstructor(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); + +/** + * @brief Perform user authentication + * + * @param env + * @return Returns the result of successful authentication + */ +napi_value NapiExecute(napi_env, napi_callback_info info); + +/** + * @brief Cancel the current authentication process + * + * @param env + * @return Results of canceling the current authentication process + */ +napi_value Cancel(napi_env, napi_callback_info info); + +/** + * @brief Check whether the current equipment + * supports the corresponding authentication capability + * according to the specified authentication type and security level. + * + * @param env + * @return Returns whether the current device supports the corresponding authentication capability. + */ +napi_value CheckAvailability(napi_env, napi_callback_info info); + +/** + * @brief Subscribe to events of the specified type + * + * @param env + * @return nullptr + */ +napi_value On(napi_env, napi_callback_info info); + +/** + * @brief Unsubscribe from events of the specified type + * + * @param env + * @return nullptr + */ +napi_value Off(napi_env, napi_callback_info info); +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS +#endif // FACERECOGNITION_PIN_AUTH_HELPER_H diff --git a/interfaces/innerkits/napi/include/user_auth_impl.h b/interfaces/innerkits/napi/include/user_auth_impl.h index 877dececd54ecb0b129d4b0604055f37a651b96a..bbee3e59e4699c205ad6a54da37b3e5fa239d4b2 100755 --- a/interfaces/innerkits/napi/include/user_auth_impl.h +++ b/interfaces/innerkits/napi/include/user_auth_impl.h @@ -1,69 +1,92 @@ -/* - * 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_USER_AUTH_H -#define FACERECOGNITION_USER_AUTH_H - -#include "napi/native_common.h" - -#include "auth_build.h" -#include "auth_common.h" - -namespace OHOS { -namespace UserIAM { -namespace UserAuth { -typedef struct AsyncHolder { - AsyncHolder() : data(nullptr), asyncWork(nullptr) {}; - void *data; - napi_async_work asyncWork; -} AsyncHolder; - -class UserAuthImpl { -public: - UserAuthImpl(); - ~UserAuthImpl(); - AuthBuild authBuild; - napi_value GetVersion(napi_env env, napi_callback_info info); - napi_value GetAvailabeStatus(napi_env env, napi_callback_info info); - napi_value GetProperty(napi_env env, napi_callback_info info); - napi_value SetProperty(napi_env env, napi_callback_info info); - napi_value Auth(napi_env env, napi_callback_info info); - napi_value AuthUser(napi_env env, napi_callback_info info); - napi_value CancelAuth(napi_env env, napi_callback_info info); - -private: - napi_value GetPropertyWrap(napi_env env, napi_callback_info info, AsyncHolder *asyncHolder); - napi_value GetPropertyAsync(napi_env env, AsyncHolder *asyncHolder); - napi_value GetPropertyPromise(napi_env env, AsyncHolder *asyncHolder); - - napi_value SetPropertyWrap(napi_env env, napi_callback_info info, AsyncHolder *asyncHolder); - napi_value SetPropertyAsync(napi_env env, AsyncHolder *asyncHolder); - napi_value SetPropertyPromise(napi_env env, AsyncHolder *asyncHolder); - - napi_value AuthWrap(napi_env env, AuthInfo *authInfo); - napi_value BuildAuthInfo(napi_env env, AuthInfo *authInfo); - napi_value AuthUserWrap(napi_env env, AuthUserInfo *userInfo); - napi_value BuildAuthUserInfo(napi_env env, AuthUserInfo *userInfo); - - static void SetPropertyExecute(napi_env env, void *data); - static void SetPropertyPromiseExecuteDone(napi_env env, napi_status status, void *data); - static void SetPropertyAsyncExecuteDone(napi_env env, napi_status status, void *data); - static void GetPropertyExecute(napi_env env, void *data); - static void GetPropertyPromiseExecuteDone(napi_env env, napi_status status, void *data); - static void GetPropertyAsyncExecuteDone(napi_env env, napi_status status, void *data); -}; -} // namespace UserAuth -} // namespace UserIAM -} // namespace OHOS -#endif // FACERECOGNITION_USER_AUTH_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_USER_AUTH_H +#define FACERECOGNITION_USER_AUTH_H + +#include "napi/native_common.h" +#include +#include "user_auth_helper.h" + +#include "auth_build.h" +#include "auth_common.h" +#include "userauth_api7.h" +#include "userauth_api7_callback.h" + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +typedef struct AsyncHolder { + AsyncHolder() : data(nullptr), asyncWork(nullptr) {}; + void *data; + napi_async_work asyncWork; +} AsyncHolder; + +class UserAuthImpl { +public: + UserAuthImpl(); + ~UserAuthImpl(); + AuthBuild authBuild; + napi_value GetVersion(napi_env env, napi_callback_info info); + napi_value GetAvailableStatus(napi_env env, napi_callback_info info); + napi_value GetProperty(napi_env env, napi_callback_info info); + napi_value SetProperty(napi_env env, napi_callback_info info); + napi_value Auth(napi_env env, napi_callback_info info); + napi_value Execute(napi_env env, napi_callback_info info); + napi_value AuthUser(napi_env env, napi_callback_info info); + napi_value CancelAuth(napi_env env, napi_callback_info info); + +private: + napi_value GetPropertyWrap(napi_env env, napi_callback_info info, AsyncHolder *asyncHolder); + napi_value GetPropertyAsync(napi_env env, AsyncHolder *asyncHolder); + napi_value GetPropertyPromise(napi_env env, AsyncHolder *asyncHolder); + + napi_value SetPropertyWrap(napi_env env, napi_callback_info info, AsyncHolder *asyncHolder); + napi_value SetPropertyAsync(napi_env env, AsyncHolder *asyncHolder); + napi_value SetPropertyPromise(napi_env env, AsyncHolder *asyncHolder); + + napi_value AuthWrap(napi_env env, AuthInfo *authInfo); + napi_value BuildAuthInfo(napi_env env, AuthInfo *authInfo); + napi_value AuthUserWrap(napi_env env, AuthUserInfo *userInfo); + napi_value BuildAuthUserInfo(napi_env env, AuthUserInfo *userInfo); + + bool GetExecuteInfo(napi_env env, napi_value* argv, ExecuteInfo* executeInfo); + napi_value DoExecute(ExecuteInfo* executeInfo); + + static void SetPropertyExecute(napi_env env, void *data); + static void SetPropertyPromiseExecuteDone(napi_env env, napi_status status, void *data); + static void SetPropertyAsyncExecuteDone(napi_env env, napi_status status, void *data); + static void GetPropertyExecute(napi_env env, void *data); + static void GetPropertyPromiseExecuteDone(napi_env env, napi_status status, void *data); + static void GetPropertyAsyncExecuteDone(napi_env env, napi_status status, void *data); +}; +class AuthenticatorImpl { +public: + AuthenticatorImpl(); + ~AuthenticatorImpl(); + napi_value NapiExecute(napi_env env, napi_callback_info info); + napi_value Cancel(napi_env env, napi_callback_info info); + napi_value CheckAvailability(napi_env env, napi_callback_info info); + napi_value On(napi_env env, napi_callback_info info); + napi_value Off(napi_env env, napi_callback_info info); +private: + napi_value ExecuteWrap(napi_env env, napi_callback_info info, NapiExecuteInfo* executeInfo); + napi_value ExecuteAsyncallback(napi_env env, napi_value* argv, NapiExecuteInfo* executeInfo); + napi_value ExecutePromise(napi_env env, napi_value* argv, NapiExecuteInfo* executeInfo); + void JudgeString(napi_env env, napi_value* argv, NapiExecuteInfo* executeInfo); +}; +} // namespace UserAuth +} // namespace UserIAM +} // namespace OHOS +#endif // FACERECOGNITION_USER_AUTH_H diff --git a/interfaces/innerkits/napi/src/auth_build.cpp b/interfaces/innerkits/napi/src/auth_build.cpp index cd5c85bda4ba79eba6efd57597cc252cdf81c135..b4e22b606afbe7be7b6cc75887f6aedc4ff3152f 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_build.h" +#include "auth_common.h" +#include "userauth_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) { + 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/authapi_callback.cpp b/interfaces/innerkits/napi/src/authapi_callback.cpp index 7abfba6b2ee5e2562a5c7088188bf3dc21e7e5e1..81c198a06571afe79fa69523d92180923bc7f765 100755 --- a/interfaces/innerkits/napi/src/authapi_callback.cpp +++ b/interfaces/innerkits/napi/src/authapi_callback.cpp @@ -1,483 +1,733 @@ -/* - * 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() -{ -} - -AuthApiCallback::~AuthApiCallback() -{ -} - -napi_value AuthApiCallback::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; -} - -napi_value AuthApiCallback::Uint64ToNapi(napi_env env, uint64_t value) -{ - size_t length = sizeof(value); - napi_value out = nullptr; - 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_CALL(env, napi_create_typedarray(env, napi_uint8_array, bufferSize, arrayBuffer, 0, &out)); - return out; -} - -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; -} - -static void GetPropertyInfoCallback(uv_work_t* work, int status) -{ - HILOG_INFO("Do OnAuthAcquireInfo work"); - GetPropertyInfo *getPropertyInfo = reinterpret_cast(work->data); - if (getPropertyInfo == nullptr) { - HILOG_ERROR("getPropertyInfo is null"); - delete work; - return; - } - napi_env env = getPropertyInfo->callBackInfo.env; - napi_value resultData[PARAM1]; - resultData[PARAM0] = AuthApiCallback::BuildExecutorProperty(env, getPropertyInfo->getResult, - getPropertyInfo->remainTimes, getPropertyInfo->freezingTime, getPropertyInfo->authSubType); - if (getPropertyInfo->callBackInfo.callBack != nullptr) { - HILOG_INFO("onExecutorPropertyInfo async"); - napi_value global = nullptr; - napi_status napiStatus = napi_get_global(env, &global); - if (napiStatus != napi_ok) { - HILOG_INFO("napi_get_global faild "); - goto EXIT; - } - napi_value resultValue = nullptr; - napi_value callBack = nullptr; - napiStatus = napi_get_reference_value(env, getPropertyInfo->callBackInfo.callBack, &callBack); - if (napiStatus != napi_ok) { - HILOG_INFO("napi_get_reference_value faild "); - goto EXIT; - } - napiStatus = napi_call_function(env, global, callBack, PARAM1, resultData, &resultValue); - if (napiStatus != napi_ok) { - HILOG_INFO("napi_call_function faild "); - goto EXIT; - } - } else { - HILOG_INFO("onExecutorPropertyInfo promise"); - napi_value resultValue = resultData[PARAM0]; - napi_deferred deferred = getPropertyInfo->callBackInfo.deferred; - napi_status napiStatus = napi_resolve_deferred(env, deferred, resultValue); - if (napiStatus != napi_ok) { - HILOG_INFO("napi_resolve_deferred faild "); - goto EXIT; - } - } -EXIT: - delete getPropertyInfo; - delete work; -} - -void AuthApiCallback::onExecutorPropertyInfo(const ExecutorProperty result) -{ - HILOG_INFO("AuthApiCallback onExecutorPropertyInfo 1 = %{public}d", result.result); - HILOG_INFO("AuthApiCallback onExecutorPropertyInfo 2 = %{public}llu", result.authSubType); - HILOG_INFO("AuthApiCallback onExecutorPropertyInfo 3 = %{public}u", result.remainTimes); - HILOG_INFO("AuthApiCallback onExecutorPropertyInfo 4 = %{public}u", result.freezingTime); - if (getPropertyInfo_ == nullptr) { - HILOG_ERROR("AuthApiCallback onExecutorPropertyInfo getPropertyInfo_ is nullptr"); - return; - } - uv_loop_s *loop(nullptr); - napi_get_uv_event_loop(getPropertyInfo_->callBackInfo.env, &loop); - if (loop == nullptr) { - HILOG_ERROR("loop is null"); - delete getPropertyInfo_; - getPropertyInfo_ = nullptr; - return; - } - uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { - HILOG_ERROR("work is null"); - delete getPropertyInfo_; - getPropertyInfo_ = nullptr; - return; - } - getPropertyInfo_->getResult = result.result; - getPropertyInfo_->authSubType = static_cast(result.authSubType); - getPropertyInfo_->remainTimes = result.remainTimes; - getPropertyInfo_->freezingTime = result.freezingTime; - work->data = reinterpret_cast(getPropertyInfo_); - getPropertyInfo_ = nullptr; - HILOG_INFO("Before GetPropertyInfoCallback"); - uv_queue_work(loop, work, [] (uv_work_t *work) {}, GetPropertyInfoCallback); -} - -void AuthApiCallback::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); -} - -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 "); - } - HILOG_INFO("AuthApiCallback onResult end"); -} - -static void SetExecutorPropertyCallback(uv_work_t *work, int status) -{ - HILOG_INFO("Do SetExecutorPropertyCallback work"); - SetPropertyInfo *setPropertyInfo = reinterpret_cast(work->data); - if (setPropertyInfo == nullptr) { - HILOG_ERROR("authInfo is null"); - delete work; - return; - } - napi_env env = setPropertyInfo->callBackInfo.env; - napi_status napiStatus = napi_create_int32(env, setPropertyInfo->setResult, &setPropertyInfo->result); - if (napiStatus != napi_ok) { - HILOG_ERROR("napi_create_int32 faild"); - goto EXIT; - } - if (setPropertyInfo->callBackInfo.callBack != nullptr) { - napi_value global = nullptr; - napiStatus = napi_get_global(env, &global); - if (napiStatus != napi_ok) { - HILOG_ERROR("napi_get_global faild"); - goto EXIT; - } - napi_value resultData[PARAM1]; - resultData[PARAM0] = setPropertyInfo->result; - setPropertyInfo->result = nullptr; - napi_value result = nullptr; - napi_value callBack = nullptr; - napiStatus = napi_get_reference_value(env, setPropertyInfo->callBackInfo.callBack, &callBack); - if (napiStatus != napi_ok) { - HILOG_ERROR("napi_get_reference_value faild"); - goto EXIT; - } - napiStatus = napi_call_function(env, global, callBack, PARAM1, resultData, &result); - if (napiStatus != napi_ok) { - HILOG_ERROR("napi_call_function faild"); - goto EXIT; - } - } else { - napi_value result = setPropertyInfo->result; - napi_deferred deferred = setPropertyInfo->callBackInfo.deferred; - napiStatus = napi_resolve_deferred(env, deferred, result); - if (napiStatus != napi_ok) { - HILOG_ERROR("napi_call_function faild"); - goto EXIT; - } - } -EXIT: - delete setPropertyInfo; - delete work; -} - -void AuthApiCallback::onSetExecutorProperty(const int32_t result) -{ - HILOG_INFO("onSetExecutorProperty start = %{public}d", result); - 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 "authapi_callback.h" +#include "securec.h" +#include "userauth_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() +{ +} + + +API7callback::API7callback(NapiExecuteInfo* executeInfo) +{ + executeInfo_ = executeInfo; +} + +API7callback::API7callback(FunctionOnAndOffInfo* functionOnAndOffInfo) +{ + functionOnAndOffInfo_ = functionOnAndOffInfo; +} + +void API7callback::API7OnResultWrap(uv_loop_s *loop, uv_work_t *work) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "authFace : %{public}s, start.", __func__); + uv_queue_work(loop, work, [] (uv_work_t *work) { }, [] (uv_work_t *work, int status) { + NapiExecuteInfo *executeInfo = reinterpret_cast(work->data); + if (executeInfo == nullptr) { + delete work; + work = nullptr; + return; + } + napi_env env = executeInfo->env; + napi_value undefined; + napi_get_undefined(env, &undefined); + if (executeInfo->callbackRef == nullptr && executeInfo->deferred != nullptr) { + napi_resolve_deferred(env, executeInfo->deferred, executeInfo->result); + delete executeInfo; + executeInfo = nullptr; + delete work; + work = nullptr; + return; + } + napi_value callback; + napi_value resultData[PARAM1]; + resultData[PARAM0] = executeInfo->result; + napi_get_reference_value(env, executeInfo->callbackRef, &callback); + napi_value callResult = nullptr; + napi_call_function(env, undefined, callback, 1, resultData, &callResult); + delete executeInfo; + executeInfo = nullptr; + delete work; + work = nullptr; + }); +} + +void API7callback::API7OnResult(const int32_t result) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "authFace : %{public}s, start.", __func__); + if (executeInfo_ != nullptr) { + napi_env env = executeInfo_->env; + napi_status status = napi_create_int32(env, result, &executeInfo_->result); + if (status != napi_ok) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "napi_create_int32 faild"); + } + uv_loop_s *loop(nullptr); + status = napi_get_uv_event_loop(env, &loop); + if (status != napi_ok) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "napi_get_uv_event_loop faild"); + } + 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; + } + NapiExecuteInfo *executeInfo = new (std::nothrow) NapiExecuteInfo(); + executeInfo = executeInfo_; + if (executeInfo == nullptr) { + delete work; + work = nullptr; + return; + } + work->data = reinterpret_cast(executeInfo); + API7OnResultWrap(loop, work); + } else { + USERAUTH_HILOGE(MODULE_JS_NAPI, "asyncCallbackContext_ is nullptr"); + } +} + +void API7callback::TipCallbackWrap(uv_loop_s *loop, uv_work_t *work) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "authFace : %{public}s, start.", __func__); + uv_queue_work(loop, work, [] (uv_work_t *work) { }, [] (uv_work_t *work, int status) { + FunctionOnAndOffInfo *functionOnAndOffInfo = reinterpret_cast(work->data); + if (functionOnAndOffInfo == nullptr) { + delete work; + work = nullptr; + return; + } + napi_env env = functionOnAndOffInfo->env; + napi_value undefined; + napi_get_undefined(env, &undefined); + if (functionOnAndOffInfo->callbackRef == nullptr) { + delete work; + work = nullptr; + return; + } + napi_value callback; + napi_get_reference_value(env, functionOnAndOffInfo->callbackRef, &callback); + napi_value callResult = nullptr; + napi_call_function(env, undefined, callback, 1, functionOnAndOffInfo->param, &callResult); + delete functionOnAndOffInfo; + functionOnAndOffInfo = nullptr; + delete work; + work = nullptr; + }); +} + +void API7callback::TipCallback(const API7UserAuth::Tip result) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "authFace : %{public}s, start.", __func__); + if (functionOnAndOffInfo_ != nullptr) { + napi_env env = functionOnAndOffInfo_->env; + functionOnAndOffInfo_->param[PARAM0] = ResultTips(env,result); + uv_loop_s *loop(nullptr); + napi_status status = napi_get_uv_event_loop(env, &loop); + if (status != napi_ok) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "napi_get_uv_event_loop faild"); + } + if (loop == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "loop is null"); + delete functionOnAndOffInfo_; + functionOnAndOffInfo_ = nullptr; + return; + } + uv_work_t *work = new(std::nothrow) uv_work_t; + if (work == nullptr) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "work is null"); + delete functionOnAndOffInfo_; + functionOnAndOffInfo_ = nullptr; + return; + } + FunctionOnAndOffInfo *functionOnAndOffInfo = new (std::nothrow) FunctionOnAndOffInfo(); + functionOnAndOffInfo = functionOnAndOffInfo_; + if (functionOnAndOffInfo == nullptr) { + delete work; + work = nullptr; + return; + } + work->data = reinterpret_cast(functionOnAndOffInfo); + TipCallbackWrap(loop, work); + } else { + USERAUTH_HILOGE(MODULE_JS_NAPI, "asyncCallbackContext_ is nullptr"); + } + USERAUTH_HILOGI(MODULE_JS_NAPI, "TipCallback is end"); +} + +napi_value API7callback::ResultTips(napi_env env, const API7UserAuth::Tip result) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "authFace : %{public}s, start.", __func__); + napi_value obj; + napi_value errorCode; + napi_value tipCode; + napi_value tipInfo; + napi_value tipEvent; + const char* tipInfo_ = result.tipInfo.c_str(); + NAPI_CALL(env, napi_create_object(env, &obj)); + NAPI_CALL(env, napi_create_int32(env, result.errorCode, &errorCode)); + NAPI_CALL(env, napi_create_int32(env, result.tipCode, &tipCode)); + NAPI_CALL(env, napi_create_int32(env, static_cast(result.tipEvent), &tipEvent)); + NAPI_CALL(env, napi_create_string_utf8(env, tipInfo_, NAPI_AUTO_LENGTH, &tipInfo)); + NAPI_CALL(env, napi_set_named_property(env, obj, "errorCode", errorCode)); + NAPI_CALL(env, napi_set_named_property(env, obj, "tipCode", tipCode)); + NAPI_CALL(env, napi_set_named_property(env, obj, "tipInfo", tipInfo)); + NAPI_CALL(env, napi_set_named_property(env, obj, "tipEvent", tipEvent)); + return obj; +} + +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 77abb2f6d021c40efe503c85a3707fb1314f27c0..d55f9ea49eabf67e7ff1f195bf7c5bc9488309c5 100644 --- a/interfaces/innerkits/napi/src/result_convert.cpp +++ b/interfaces/innerkits/napi/src/result_convert.cpp @@ -1,225 +1,225 @@ -/* - * 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 "result_convert.h" +#include "securec.h" +#include "userauth_hilog_wrapper.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 e5982a1bef57cb7c249759f1bb83d9812446c96e..f31f74a805a4087a05ef33c35891164381b67c71 100755 --- a/interfaces/innerkits/napi/src/user_auth_helper.cpp +++ b/interfaces/innerkits/napi/src/user_auth_helper.cpp @@ -1,231 +1,367 @@ -/* - * 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 GetAvailabeStatus(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, getAvailabeStatus"); - return userAuthImpl->GetAvailabeStatus(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); -} - -/** - * @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), - }; - 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("getAvailabeStatus", UserAuth::GetAvailabeStatus), - 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), - }; - 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 "user_auth_helper.h" +#include "userauth_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) +{ + 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 Instance passed to context + * + * @param env + * @param info + * @return napi_value Instance + */ +napi_value AuthenticatorServiceConstructor(napi_env env, napi_callback_info info) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s, start.", __func__); + std::shared_ptr authenticatorImpl; + authenticatorImpl.reset(new AuthenticatorImpl()); + 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, authenticatorImpl.get(), + [](napi_env env, void *data, void *hint) { + AuthenticatorImpl *authenticatorImpl = static_cast(data); + if (authenticatorImpl != nullptr) { + delete authenticatorImpl; + } + }, + 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); +} + +napi_value NapiExecute(napi_env env, napi_callback_info info) +{ + napi_value thisVar = nullptr; + size_t argcAsync = ARGS_THREE; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, &thisVar, nullptr)); + AuthenticatorImpl *authenticatorImpl = nullptr; + NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&authenticatorImpl)); + USERAUTH_HILOGI(MODULE_JS_NAPI, "authenticatorImpl, Execute"); + return authenticatorImpl->NapiExecute(env, info); +} + +napi_value Cancel(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)); + AuthenticatorImpl *authenticatorImpl = nullptr; + NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&authenticatorImpl)); + USERAUTH_HILOGI(MODULE_JS_NAPI, "authenticatorImpl, Cancel"); + return authenticatorImpl->Cancel(env, info); +} + +napi_value CheckAvailability(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)); + AuthenticatorImpl *authenticatorImpl = nullptr; + NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&authenticatorImpl)); + USERAUTH_HILOGI(MODULE_JS_NAPI, "authenticatorImpl, CheckAvailability"); + return authenticatorImpl->CheckAvailability(env, info); +} + +napi_value On(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)); + AuthenticatorImpl *authenticatorImpl = nullptr; + NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&authenticatorImpl)); + USERAUTH_HILOGI(MODULE_JS_NAPI, "authenticatorImpl, On"); + return authenticatorImpl->On(env, info); +} + +napi_value Off(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)); + AuthenticatorImpl *authenticatorImpl = nullptr; + NAPI_CALL(env, napi_unwrap(env, thisVar, (void **)&authenticatorImpl)); + USERAUTH_HILOGI(MODULE_JS_NAPI, "authenticatorImpl, Off"); + return authenticatorImpl->Off(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::GetAuthenticator), + }; + status = napi_define_properties(env, exports, sizeof(exportFuncs) / sizeof(*exportFuncs), exportFuncs); + if (status != napi_ok) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "napi_define_properties faild"); + } + status = napi_set_named_property(env, exports, "UserAuth", GetCtor(env)); + if (status != napi_ok) { + USERAUTH_HILOGE(MODULE_JS_NAPI, "napi_set_named_property 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 GetAuthenticator(napi_env env, napi_callback_info info) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s, start.", __func__); + napi_value thisVar = nullptr; + napi_value authenticator = nullptr; + NAPI_CALL(env, napi_new_instance(env, GetCtorAPI7(env), 0, nullptr, &authenticator)); + NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); + return authenticator; +} + +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; +} + +napi_value GetCtorAPI7(napi_env env) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s, start.", __func__); + napi_value cons = nullptr; + napi_property_descriptor clzDes[] = { + DECLARE_NAPI_FUNCTION("execute", UserAuth::NapiExecute), + DECLARE_NAPI_FUNCTION("cancel", UserAuth::Cancel), + DECLARE_NAPI_FUNCTION("checkAvailability", UserAuth::CheckAvailability), + DECLARE_NAPI_FUNCTION("on", UserAuth::On), + DECLARE_NAPI_FUNCTION("off", UserAuth::Off), + }; + NAPI_CALL(env, napi_define_class(env, "Authenticator", NAPI_AUTO_LENGTH, AuthenticatorServiceConstructor, 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 50bbc87a7040ae13b63d60b7d80a5e8ec7c9f0a1..ee67d063a0670797be7b0f5b8f5b130694680b13 100755 --- a/interfaces/innerkits/napi/src/user_auth_impl.cpp +++ b/interfaces/innerkits/napi/src/user_auth_impl.cpp @@ -1,515 +1,835 @@ -/* - * 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 "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::GetAvailabeStatus(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("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) { - 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"); - AuthApiCallback *object = new AuthApiCallback(); - object->getPropertyInfo_ = getPropertyInfo; - std::shared_ptr callback; - callback.reset(object); - UserAuth::GetInstance().GetProperty(request, callback); - HILOG_INFO("GetPropertyExecute, worker pool thread execute end."); -} - -void UserAuthImpl::GetPropertyPromiseExecuteDone(napi_env env, napi_status status, void *data) -{ - HILOG_INFO("GetPropertyPromiseExecuteDone, start"); - 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; - AuthApiCallback *object = new AuthApiCallback(); - object->setPropertyInfo_ = setPropertyInfo; - std::shared_ptr callback; - callback.reset(object); - UserAuth::GetInstance().SetProperty(request, callback); - HILOG_INFO("setPropertyExecute, worker pool thread execute end."); -} - -void UserAuthImpl::SetPropertyPromiseExecuteDone(napi_env env, napi_status status, void *data) -{ - HILOG_INFO("SetPropertyPromiseExecuteDone, start"); - 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::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(); - object->authInfo_ = authInfo; - object->userInfo_ = nullptr; - 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(); - object->authInfo_ = nullptr; - object->userInfo_ = 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" + +namespace OHOS { +namespace UserIAM { +namespace UserAuth { +UserAuthImpl::UserAuthImpl() +{ +} + +UserAuthImpl::~UserAuthImpl() +{ +} + +AuthenticatorImpl::AuthenticatorImpl() +{ +} + +AuthenticatorImpl::~AuthenticatorImpl() +{ +} + +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; +} + +napi_value AuthenticatorImpl::NapiExecute(napi_env env, napi_callback_info info) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s, start.", __func__); + NapiExecuteInfo *executeInfo = new (std::nothrow) NapiExecuteInfo(); + executeInfo->env = env; + return ExecuteWrap(env, info, executeInfo); +} + +napi_value AuthenticatorImpl::ExecuteWrap(napi_env env, napi_callback_info info, NapiExecuteInfo* executeInfo) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s, start.", __func__); + 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)); + callbackIndex = argc - 1; + napi_valuetype valuetype; + NAPI_CALL(env, napi_typeof(env, argv[callbackIndex], &valuetype)); + if (valuetype == napi_function) { + NAPI_CALL(env, napi_create_reference(env, argv[callbackIndex], 1, &executeInfo->callbackRef)); + return ExecuteAsyncallback(env, argv, executeInfo); + } else { + NAPI_CALL(env,napi_create_promise(env, &executeInfo->deferred, &executeInfo->promise)); + return ExecutePromise(env, argv, executeInfo); + } +} + +void AuthenticatorImpl::JudgeString(napi_env env, napi_value* argv, NapiExecuteInfo* executeInfo) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s, start.", __func__); + napi_valuetype valuetype; + napi_typeof(env, argv[0], &valuetype); + if (valuetype == napi_string) { + char *type = nullptr; + size_t typelen = 0; + napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen); + if (typelen > 0) { + type = new char[typelen + 1]; + napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen); + executeInfo->type = type; + delete[] type; + } + } + napi_typeof(env, argv[1], &valuetype); + if (valuetype == napi_string) { + char *type1 = nullptr; + size_t typelen1 = 0; + napi_get_value_string_utf8(env, argv[1], nullptr, 0, &typelen1); + if (typelen1 > 0) { + type1 = new char[typelen1 + 1]; + napi_get_value_string_utf8(env, argv[1], type1, typelen1 + 1, &typelen1); + executeInfo->level = type1; + delete[] type1; + } + } + napi_value resourceName = nullptr; + napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); + napi_create_async_work( + env, nullptr, resourceName, + [](napi_env env, void *data) { + NapiExecuteInfo *executeInfo = (NapiExecuteInfo *)data; + std::shared_ptr userauthAPI7callback = std::make_shared(executeInfo); + API7UserAuth::UserAuthAPI7::GetAuthenticator().Execute(executeInfo->type, executeInfo->level, userauthAPI7callback); + }, + [](napi_env env, napi_status status, void *data) { + NapiExecuteInfo *executeInfo = (NapiExecuteInfo *)data; + napi_delete_async_work(env, executeInfo->asyncWork); + }, + (void *)executeInfo, &executeInfo->asyncWork); + napi_queue_async_work(env, executeInfo->asyncWork); +} + +napi_value AuthenticatorImpl::ExecuteAsyncallback(napi_env env, napi_value* argv, NapiExecuteInfo* executeInfo) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s, start.", __func__); + JudgeString(env,argv,executeInfo); + napi_value result = RESULTVAR; + NAPI_CALL(env, napi_get_null(env, &result)); + return result; +} + +napi_value AuthenticatorImpl::ExecutePromise(napi_env env, napi_value* argv, NapiExecuteInfo* executeInfo) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s, start.", __func__); + JudgeString(env,argv,executeInfo); + return executeInfo->promise; +} + +napi_value AuthenticatorImpl::Cancel(napi_env env, napi_callback_info info) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s, start.", __func__); + napi_value ret; + int32_t result = API7UserAuth::UserAuthAPI7::GetAuthenticator().Cancel(); + NAPI_CALL(env,napi_create_int32(env, result, &ret)); + return ret; +} + +napi_value AuthenticatorImpl::CheckAvailability(napi_env env, napi_callback_info info) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s, start.", __func__); + napi_value ret; + int32_t result = 0; + size_t argc = PARAM2; + napi_value argv[PARAM2] = {0}; + CheckAvailabilityInfo *checkAvailabilityInfo = new (std::nothrow) CheckAvailabilityInfo(); + checkAvailabilityInfo->env = env; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); + napi_valuetype valuetype; + NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype)); + if (valuetype == napi_string) { + char *type = nullptr; + size_t typelen = 0; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen)); + if (typelen > 0) { + type = new char[typelen + 1]; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen)); + checkAvailabilityInfo->type = type; + delete[] type; + } + } + NAPI_CALL(env, napi_typeof(env, argv[1], &valuetype)); + if (valuetype == napi_string) { + char *type1 = nullptr; + size_t typelen1 = 0; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[1], nullptr, 0, &typelen1)); + if (typelen1 > 0) { + type1 = new char[typelen1 + 1]; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[1], type1, typelen1 + 1, &typelen1)); + checkAvailabilityInfo->level = type1; + delete[] type1; + } + } + result = API7UserAuth::UserAuthAPI7::GetAuthenticator().CheckAvailability(checkAvailabilityInfo->type, checkAvailabilityInfo->level); + NAPI_CALL(env,napi_create_int32(env, result, &ret)); + return ret; +} + +napi_value AuthenticatorImpl::On(napi_env env, napi_callback_info info) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s, start.", __func__); + size_t argc = PARAM2; + napi_value argv[PARAM2] = {0}; + FunctionOnAndOffInfo* functionOnAndOffInfo = new (std::nothrow) FunctionOnAndOffInfo(); + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); + napi_valuetype valuetype; + NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype)); + if (valuetype == napi_string) { + char *type = nullptr; + size_t typelen = 0; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen)); + if (typelen > 0) { + type = new char[typelen + 1]; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen)); + functionOnAndOffInfo->tip = type; + delete[] type; + } + } + NAPI_CALL(env, napi_typeof(env, argv[1], &valuetype)); + if (valuetype == napi_function) { + NAPI_CALL(env, napi_create_reference(env, argv[1], 1, &functionOnAndOffInfo->callbackRef)); + } + std::shared_ptr userauthAPI7callback = std::make_shared(functionOnAndOffInfo); + API7UserAuth::UserAuthAPI7::GetAuthenticator().On(functionOnAndOffInfo->tip, userauthAPI7callback); + napi_value result = RESULTVAR; + NAPI_CALL(env, napi_get_null(env, &result)); + return result; +} + +napi_value AuthenticatorImpl::Off(napi_env env, napi_callback_info info) +{ + USERAUTH_HILOGI(MODULE_JS_NAPI, "%{public}s, start.", __func__); + size_t argc = PARAM2; + napi_value argv[PARAM2] = {0}; + FunctionOnAndOffInfo* functionOnAndOffInfo = new (std::nothrow) FunctionOnAndOffInfo(); + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr)); + napi_valuetype valuetype; + NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype)); + if (valuetype == napi_string) { + char *type = nullptr; + size_t typelen = 0; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen)); + if (typelen > 0) { + type = new char[typelen + 1]; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen)); + functionOnAndOffInfo->tip = type; + delete[] type; + } + } + std::shared_ptr userauthAPI7callback = std::make_shared(functionOnAndOffInfo); + API7UserAuth::UserAuthAPI7::GetAuthenticator().Off(functionOnAndOffInfo->tip, userauthAPI7callback); + napi_value result = RESULTVAR; + NAPI_CALL(env, napi_get_null(env, &result)); + return result; +} + +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/innerkits/native/BUILD.gn b/interfaces/innerkits/native/BUILD.gn index e07ff2c54359d07e3119e446b0f339ce76e02bef..de98274ee294ed985144eeb1f272bb807f34fd7c 100755 --- a/interfaces/innerkits/native/BUILD.gn +++ b/interfaces/innerkits/native/BUILD.gn @@ -29,6 +29,9 @@ ohos_shared_library("userauth_framework") { "../../../frameworks/kitsimpl/src/userauth.cpp", "../../../frameworks/kitsimpl/src/userauth_async_stub.cpp", "../../../frameworks/kitsimpl/src/userauth_proxy.cpp", + "../../../frameworks/kitsimpl/src/userauth_api7.cpp", + "../../../frameworks/kitsimpl/src/userauth_api7_datamgr.cpp", + "../../../frameworks/kitsimpl/src/iuserauth_api7_callback.cpp", ] configs = [ diff --git a/interfaces/innerkits/napi/include/auth_hilog_wrapper.h b/interfaces/innerkits/native/include/iuserauth_api7_callback.h similarity index 33% rename from interfaces/innerkits/napi/include/auth_hilog_wrapper.h rename to interfaces/innerkits/native/include/iuserauth_api7_callback.h index f8e944e1ca75120c2af6eefdb3f3f5c74c054763..2c5f1c4b4aedb0d535b7c974a7e4446bb1d3a504 100755 --- a/interfaces/innerkits/napi/include/auth_hilog_wrapper.h +++ b/interfaces/innerkits/native/include/iuserauth_api7_callback.h @@ -1,61 +1,41 @@ -/* - * 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 +/* + * 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 IUSERAUTH_API7_CALLBACK_H +#define IUSERAUTH_API7_CALLBACK_H + +#include +#include "userauth_info.h" +#include "userauth_callback.h" +#include "userauth_api7_callback.h" + + +namespace OHOS { +namespace UserIAM { +namespace API7UserAuth { +class IUserAuthAPI7Callback : public UserAuth::UserAuthCallback { +public: + explicit IUserAuthAPI7Callback(std::shared_ptr& impl); + virtual ~IUserAuthAPI7Callback() = default; + + void onAcquireInfo(const int32_t module, const uint32_t acquireInfo, const int32_t extraInfo) override; + void onResult(const int32_t result, const UserAuth::AuthResult extraInfo) override; +private: + std::shared_ptr callback_ {nullptr}; +}; +} // namespace API7UserAuth +} // namespace UserIAM +} // namespace OHOS +#endif // IUSERAUTH_API7_CALLBACK_H \ No newline at end of file diff --git a/interfaces/innerkits/native/include/user_auth.h b/interfaces/innerkits/native/include/user_auth.h index be7198471cd8989568e2871fb4bd689734502a9d..49348c4e19f2bc4a237b01404573142a44431ca9 100755 --- a/interfaces/innerkits/native/include/user_auth.h +++ b/interfaces/innerkits/native/include/user_auth.h @@ -32,8 +32,8 @@ public: DISALLOW_COPY_AND_MOVE(UserAuth); int32_t GetAvailableStatus(const AuthType authType, const AuthTurstLevel authTurstLevel); - void GetProperty(const GetPropertyRequest request, std::shared_ptr callback); - void SetProperty(const SetPropertyRequest request, std::shared_ptr callback); + void GetProperty(const GetPropertyRequest request, std::shared_ptr callback); + void SetProperty(const SetPropertyRequest request, std::shared_ptr callback); uint64_t Auth(const uint64_t challenge, const AuthType authType, const AuthTurstLevel authTurstLevel, std::shared_ptr callback); uint64_t AuthUser(const int32_t userId, const uint64_t challenge, const AuthType authType, @@ -44,9 +44,9 @@ public: private: class UserAuthDeathRecipient : public IRemoteObject::DeathRecipient { public: - UserAuthDeathRecipient() = default; - ~UserAuthDeathRecipient() = default; - void OnRemoteDied(const wptr& remote) override; + UserAuthDeathRecipient() = default; + ~UserAuthDeathRecipient() = default; + void OnRemoteDied(const wptr& remote) override; private: DISALLOW_COPY_AND_MOVE(UserAuthDeathRecipient); @@ -61,4 +61,4 @@ private: } // namespace UserAuth } // namespace UserIam } // namespace OHOS -#endif // USERAUTH_CLIENT_H \ No newline at end of file +#endif // USERAUTH_CLIENT_H diff --git a/interfaces/innerkits/native/include/userauth_api7.h b/interfaces/innerkits/native/include/userauth_api7.h new file mode 100755 index 0000000000000000000000000000000000000000..070fce2e869c2fa0992a1a48867f7b5c1ec23e4b --- /dev/null +++ b/interfaces/innerkits/native/include/userauth_api7.h @@ -0,0 +1,41 @@ +/* + * 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_API7_H +#define USERAUTH_API7_H + +#include + +#include "userauth_api7_info.h" +#include "userauth_api7_callback.h" + +namespace OHOS { +namespace UserIAM { +namespace API7UserAuth { +class UserAuthAPI7 { +public: + UserAuthAPI7() = default; + ~UserAuthAPI7() = default; + static UserAuthAPI7 &GetAuthenticator(); + void Execute(std::string authtype, std::string trustLevel, std::shared_ptr callback); + int32_t Cancel(); + int32_t CheckAvailability(std::string authtype, std::string trustLevel); + void On(std::string tip, std::shared_ptr tipCallback); + void Off(std::string tip, std::shared_ptr tipCallback); +}; +} // namespace API7UserAuth +} // namespace UserIAM +} // namespace OHOS +#endif // USERAUTH_API7_H \ No newline at end of file diff --git a/utils/native/include/userauth_errors.h b/interfaces/innerkits/native/include/userauth_api7_callback.h old mode 100644 new mode 100755 similarity index 65% rename from utils/native/include/userauth_errors.h rename to interfaces/innerkits/native/include/userauth_api7_callback.h index ebe2c454721b72eef34a687e06533e85f6244a82..c9d3dcb7abbed938356a384083fc255a45e02b6f --- a/utils/native/include/userauth_errors.h +++ b/interfaces/innerkits/native/include/userauth_api7_callback.h @@ -13,24 +13,20 @@ * limitations under the License. */ -#ifndef USERAUTH_ERRORS_H -#define USERAUTH_ERRORS_H +#ifndef USERAUTH_API7_CALLBACK_H +#define USERAUTH_API7_CALLBACK_H -#include +#include "userauth_api7_info.h" 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 API7UserAuth { +class UserAuthAPI7Callback { +public: + virtual void API7OnResult(const int32_t result) = 0; + virtual void TipCallback(const Tip result) = 0; }; -} // namespace UserAuth -} // namespace UserIam +} // namespace API7UserAuth +} // namespace UserIAM } // namespace OHOS - -#endif // USERAUTH_ERRORS_H +#endif // USERAUTH_API7_CALLBACK_H \ No newline at end of file diff --git a/interfaces/innerkits/napi/src/auth_native_module.cpp b/interfaces/innerkits/native/include/userauth_api7_datamgr.h similarity index 51% rename from interfaces/innerkits/napi/src/auth_native_module.cpp rename to interfaces/innerkits/native/include/userauth_api7_datamgr.h index d6277d4a0da37ed43f5d5a80f3d75366c7a53b46..40d52c116665e7ae54607221f8831d4649e73ce8 100755 --- a/interfaces/innerkits/napi/src/auth_native_module.cpp +++ b/interfaces/innerkits/native/include/userauth_api7_datamgr.h @@ -1,41 +1,41 @@ -/* - * 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, - .nm_modname = "UserAuth", - .nm_priv = nullptr, - .reserved = {}}; - napi_module_register(&module); -} -} // 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. + */ + +#ifndef USERAUTH_API7_DATAMGR_H +#define USERAUTH_API7_DATAMGR_H + +#include "userauth_api7_info.h" + +namespace OHOS { +namespace UserIAM { +namespace API7UserAuth { +class UserAuthAPI7Datamgr { +public: + static UserAuthAPI7Datamgr &GetInstance(); + void API7AddContextID(uint64_t contextID); + void API7GetContextID(uint64_t &contextID); + void API7DeleteContextID(); + void SetAPI7Tip(bool isTipOn); + bool GetAPI7Tip(); +private: + UserAuthAPI7Datamgr() = default; + ~UserAuthAPI7Datamgr() = default; + uint64_t contextID_ = CONTEXTID_INIT; + bool isTipOn_ = false; +}; +} // namespace API7UserAuth +} // namespace UserIAM +} // namespace OHOS +#endif // USERAUTH_API7_DATAMGR_H \ No newline at end of file diff --git a/interfaces/innerkits/native/include/userauth_api7_info.h b/interfaces/innerkits/native/include/userauth_api7_info.h new file mode 100755 index 0000000000000000000000000000000000000000..445ce9bdbcd543f7c1df828052aa314ad59d6c4c --- /dev/null +++ b/interfaces/innerkits/native/include/userauth_api7_info.h @@ -0,0 +1,150 @@ +/* + * 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_API7_INFO_H +#define USERAUTH_API7_INFO_H + +#include + +#define CONTEXTID_INIT 0 +#define CHALLENGE_INIT 0 + +namespace OHOS { +namespace UserIAM { +namespace API7UserAuth { +// Authentication Result +enum AuthenticationResult { + /** + * The device does not support the current authentication method. + */ + NO_SUPPORT = -1, + /** + * Authentication succeeded. + */ + SUCCESS = 0, + /** + * Alignment failed. + */ + COMPARE_FAILURE = 1, + /** + * User deauthentication. + */ + CANCELED = 2, + /** + * Authentication timed out. + */ + TIMEOUT = 3, + /** + * Failed to turn on camera. + */ + CAMERA_FAIL = 4, + /** + * The authentication service is busy, please try again later. + */ + BUSY = 5, + /** + * Authentication parameters are invalid. + */ + INVALID_PARAMETERS = 6, + /** + * Too many authentication failures, locked. + */ + LOCKED = 7, + /** + * Authentication credentials not entered. + */ + NOT_ENROLLED = 78, + /** + * other errors. + */ + GENERAL_ERROR = 100, +}; +// Device Certification Capability +enum CheckAvailabilityResult { + // The device supports the specified authentication type and authentication security level + SUPPORTED = 0, + // The device does not support the specified authentication type + AUTH_TYPE_NOT_SUPPORT = 1, + // The device does not support the specified authentication security level + SECURE_LEVEL_NOT_SUPPORT = 2, + // The device does not support distributed authentication + DISTRIBUTED_AUTH_NOT_SUPPORT = 3, + // Authentication credentials are not entered in the device + NO_ENROLLED = 4, + // wrong number of parameters + PARAM_NUM_ERROR = 5, +}; +// Prompt events during authentication +enum TipEvent { + // Entry or Certification Results + RESULT = 1, + // Entry or Certification Cancellation + CANCEL = 2, + // Prompt during entry or authentication + ACQUIRE = 3, + // Entry or authentication functions are occupied + ISBUSY = 4, + // Not enough storage + OUT_OF_MEM = 5, + // Face authentication credential index number + FACE_ID = 6, +}; +// Result +enum Result { + // SUCCESSED + SUCCESSED = 0, + // FAILED + FAILED = 1, +}; +// Prompt code during authentication +enum TipCode { + // The light is too strong, the acquired image is too bright + FACE_AUTH_TIP_TOO_BRIGHT = 1, + // The light is too dim, the acquired image is too dark + FACE_AUTH_TIP_TOO_DARK = 2, + // The face is too close to the device + FACE_AUTH_TIP_TOO_CLOSE = 3, + // The face is too far from the device + FACE_AUTH_TIP_TOO_FAR = 4, + // The device is too high, only the face obtained + FACE_AUTH_TIP_TOO_HIGH = 5, + // The device is too low, only the lower part of the face is obtained + FACE_AUTH_TIP_TOO_LOW = 6, + // The device is too far to the right, only the right part of the face is obtained + FACE_AUTH_TIP_TOO_RIGHT = 7, + // The device is too far to the left, only the left part of the face is obtained + FACE_AUTH_TIP_TOO_LEFT = 8, + // During image acquisition, the user's face moves too fast + FACE_AUTH_TIP_TOO_MUCH_MOTION = 9, + // no facing camera + FACE_AUTH_TIP_POOR_GAZE = 10, + // No face information detected + FACE_AUTH_TIP_NOT_DETECTED = 11, +}; +// Prompt information during the authentication process +struct Tip { + // Whether the prompt information was successfully obtained + int32_t errorCode; + // Authentication prompt event + int32_t tipEvent; + // Authentication prompt event + int32_t tipCode; + // Description of the event prompt code of the authentication prompt + std::string tipInfo; +}; +} // namespace API7UserAuth +} // namespace UserIam +} // namespace OHOS +#endif // USERAUTH_API7_INFO_H diff --git a/interfaces/innerkits/native/include/userauth_async_stub.h b/interfaces/innerkits/native/include/userauth_async_stub.h index 27ea1b6cc481e63ecb609fdecfd00dd8152b9301..980b29b384e98be0fd8c4669577e1fad0f26c94d 100755 --- a/interfaces/innerkits/native/include/userauth_async_stub.h +++ b/interfaces/innerkits/native/include/userauth_async_stub.h @@ -26,6 +26,8 @@ namespace UserAuth { class UserAuthAsyncStub : public IRemoteStub { public: explicit UserAuthAsyncStub(std::shared_ptr& impl); + explicit UserAuthAsyncStub(std::shared_ptr& impl); + explicit UserAuthAsyncStub(std::shared_ptr& impl); ~UserAuthAsyncStub() = default; int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; @@ -39,10 +41,12 @@ private: int32_t onResultStub(MessageParcel& data, MessageParcel& reply); int32_t onExecutorPropertyInfoStub(MessageParcel& data, MessageParcel& reply); int32_t onSetExecutorPropertyStub(MessageParcel& data, MessageParcel& reply); - std::shared_ptr callback_ {nullptr}; + std::shared_ptr authCallback_ {nullptr}; + std::shared_ptr setPropCallback_ {nullptr}; + std::shared_ptr getPropCallback_ {nullptr}; }; } // namespace UserAuth } // namespace UserIAM } // namespace OHOS -#endif // USERAUTH_ASYNC_STUB_H \ No newline at end of file +#endif // USERAUTH_ASYNC_STUB_H diff --git a/interfaces/innerkits/native/include/userauth_callback.h b/interfaces/innerkits/native/include/userauth_callback.h index 9982c24cb28188b01d4c3ec7f73316037001a19a..c451c1b87b56349477b4e371c8dd3267297ecc11 100755 --- a/interfaces/innerkits/native/include/userauth_callback.h +++ b/interfaces/innerkits/native/include/userauth_callback.h @@ -23,13 +23,18 @@ namespace UserIAM { namespace UserAuth { class UserAuthCallback { public: - virtual void onAcquireInfo(const int32_t module, const uint32_t acquireInfo, const int32_t extraInfo) = 0; virtual void onResult(const int32_t result, const AuthResult extraInfo) = 0; - virtual void onExecutorPropertyInfo(const ExecutorProperty result) = 0; - virtual void onSetExecutorProperty(const int32_t result) = 0; +}; +class GetPropCallback { +public: + virtual void onGetProperty(const ExecutorProperty result) = 0; +}; +class SetPropCallback { +public: + virtual void onSetProperty(const int32_t result) = 0; }; } // namespace UserAuth } // namespace UserIam } // namespace OHOS -#endif // IUSERAUTH_CALLBACK_H \ No newline at end of file +#endif // IUSERAUTH_CALLBACK_H diff --git a/interfaces/innerkits/native/include/userauth_info.h b/interfaces/innerkits/native/include/userauth_info.h index 4284e9b699c31795a60c8b8ff3cde64e44b397e0..e826536d787c303332c1d4821e3a53e59273cbb0 100755 --- a/interfaces/innerkits/native/include/userauth_info.h +++ b/interfaces/innerkits/native/include/userauth_info.h @@ -23,12 +23,12 @@ namespace OHOS { namespace UserIAM { namespace UserAuth { -// 认证的类型(口令,人脸) +// Type of authentication (password, face) enum AuthType: uint32_t { PIN = 1, FACE = 2, }; -// 认证子类型(2D人脸,3D人脸...) +// Authentication subtype (2D face, 3D face...) enum AuthSubType: uint64_t { /** * Authentication sub type six number pin. @@ -51,7 +51,7 @@ enum AuthSubType: uint64_t { */ FACE_3D = 20001 }; -// 认证结果可信等级 +// Certification result confidence level enum AuthTurstLevel: uint32_t { // level 1-4 ATL1 = 10000, @@ -59,48 +59,50 @@ enum AuthTurstLevel: uint32_t { ATL3 = 30000, ATL4 = 40000 }; -// 执行器属性列表 +// Actuator get property list enum GetPropertyType: uint32_t { - // 认证子类型(此时认证类型已确认) + // Authentication subtype (at this point the authentication type has been confirmed) AUTH_SUB_TYPE = 1, - // 剩余认证次数 + // Remaining authentication times REMAIN_TIMES = 2, - // 冻结时间 + // Freeze time FREEZING_TIME = 3, }; -// 获得属性请求 +// get attribute request struct GetPropertyRequest { AuthType authType; // GetPropertyType std::vector keys; }; -// 执行器属性 +// Actuator properties struct ExecutorProperty { int32_t result; AuthSubType authSubType; uint32_t remainTimes; uint32_t freezingTime; }; -// 执行器属性列表 +// Actuator property mode list enum AuthPropertyMode: uint32_t { - PROPERMODE_DELETE = 0, - PROPERMODE_GET = 1, - PROPERMODE_SET = 2, - PROPERMODE_FREEZE = 3, - PROPERMODE_UNFREEZE = 4, + PROPERMODE_DELETE = 0, + PROPERMODE_GET = 1, + PROPERMODE_SET = 2, + PROPERMODE_FREEZE = 3, + PROPERMODE_UNFREEZE = 4, + PROPERMODE_INIT_ALGORITHM = 5, + PROPERMODE_RELEASE_ALGORITHM = 6, }; -// 执行器属性列表 +// Actuator property list enum SetPropertyType: uint32_t { - INIT_ALGORITHM = 1, - FREEZE_TEMPLATE = 2, - THAW_TEMPLATE = 3, + INIT_ALGORITHM = 1, + FREEZE_TEMPLATE = 2, + THAW_TEMPLATE = 3, }; struct SetPropertyRequest { AuthType authType; SetPropertyType key; std::vector setInfo; }; -// 认证结果 +// Authentication Result struct AuthResult { std::vector token; uint32_t remainTimes; @@ -110,6 +112,7 @@ struct CoAuthInfo { AuthType authType; uint64_t callerID; uint64_t contextID; + int32_t userID; std::string pkgName; std::vector sessionIds; }; @@ -121,7 +124,7 @@ struct FreezInfo { AuthType authType; }; -// 结果码 +// Result Code enum ResultCode: int32_t { /** * Indicates that authentication is success or ability is supported. diff --git a/interfaces/kits/napi/BUILD.gn b/interfaces/kits/napi/BUILD.gn index a84551cd3745db738f581e68fa0c6c9c18d69741..777be8efe5f52ba4c50bb42daeb92a2f89c933a9 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 c6434c20604a04f4d80562622f3794287808a32f..4f203d76766f9eba1de25e3dc098c4a737886655 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/user_auth_helper_north.cpp b/interfaces/kits/napi/src/user_auth_helper_north.cpp index 2b4f842ab11c3ab353a14d9fe4eb57bfff323a4f..816e88a71406f622e533ceb0013d3ae554fad57e 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; } @@ -36,7 +37,7 @@ napi_value GetCtor(napi_env env) napi_value cons = nullptr; napi_property_descriptor clzDes[] = { DECLARE_NAPI_FUNCTION("getVersion", UserIAM::UserAuth::GetVersion), - DECLARE_NAPI_FUNCTION("getAvailabeStatus", UserIAM::UserAuth::GetAvailabeStatus), + DECLARE_NAPI_FUNCTION("getAvailableStatus", UserIAM::UserAuth::GetAvailableStatus), DECLARE_NAPI_FUNCTION("auth", UserIAM::UserAuth::Auth), }; NAPI_CALL(env, napi_define_class(env, "UserAuthNorth", NAPI_AUTO_LENGTH, @@ -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 +} // namespace OHOS diff --git a/services/BUILD.gn b/services/BUILD.gn index db18f921d140955bc2014b6dcc7322265fb8f247..dae1c3a0fe109d3b9eeca2f7ebab899c8a08e306 100755 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -18,6 +18,7 @@ config("userauth_private_config") { "include", "//base/user_iam/auth_executor_mgr/common/interface", "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy/include", + "//base/security/access_token/interfaces/innerkits/accesstoken/include", ] } @@ -40,6 +41,7 @@ ohos_shared_library("userauthservice") { deps = [ "${userauth_innerkits_path}/native:userauth_framework", + "//base/security/access_token/interfaces/innerkits/accesstoken:libaccesstoken_sdk", "//base/startup/syspara_lite/interfaces/innerkits/native/syspara:syspara", "//base/user_iam/auth_executor_mgr/common:useriam_common_lib", "//base/user_iam/auth_executor_mgr/interfaces/innerkits:coauth_framework", diff --git a/services/include/userauth_adapter.h b/services/include/userauth_adapter.h index 3f2b5a61f2219ccb7fd635d3bc00b19207d81484..d445d59c9ecafd67529dcc3cb7f545df700befd1 100644 --- a/services/include/userauth_adapter.h +++ b/services/include/userauth_adapter.h @@ -35,30 +35,32 @@ public: int32_t GetAuthTrustLevel(int32_t userId, uint32_t authType, uint32_t &authTrustLevel); /* Get the executor authentication properties */ - void GetPropAuthInfo(uint64_t callerUID, std::string pkgName, GetPropertyRequest requset, + void GetPropAuthInfo(int32_t userID, uint64_t callerUID, std::string pkgName, GetPropertyRequest requset, sptr& callback); /** This method is called to set the executor properties * after the callback of the coAuth is called to obtain the scheduling token * and the authentication result is successful. * */ - void CoauthSetPropAuthInfo(int32_t resultCode, uint64_t callerUID, std::string pkgName, UserAuthToken authToken, - SetPropertyRequest requset, sptr& callback); + void CoauthSetPropAuthInfo(int32_t userID, int32_t resultCode, uint64_t callerUID, std::string pkgName, + UserAuthToken authToken, SetPropertyRequest requset, sptr& callback); - /* Set the executor authentication properties */ + /* Set the executor authentication properties for freez or unfreez */ void SetPropAuthInfo(uint64_t callerUID, std::string pkgName, int32_t resultCode, UserAuthToken authToken, SetPropertyRequest requset, std::vector templateIds, sptr& callback); - void GetPropAuthInfoCoauth(uint64_t callerUID, std::string pkgName, int32_t resultCode, UserAuthToken authToken, - GetPropertyRequest requset, sptr& callback); - int32_t GetUserID(int32_t &userID); + /* get the executor authentication properties for Coauth */ + void GetPropAuthInfoCoauth(int32_t userID, uint64_t callerUID, std::string pkgName, int32_t resultCode, + UserAuthToken authToken, GetPropertyRequest requset, sptr& callback); int32_t GenerateSolution(AuthSolution param, std::vector &sessionIds); int32_t RequestAuthResult(uint64_t contextId, std::vector scheduleToken, UserAuthToken &authToken, std::vector &sessionIds); int32_t CancelContext(uint64_t contextId, std::vector &sessionIds); int32_t Cancel(uint64_t sessionId); + /* get the executor authentication properties */ int32_t GetExecutorProp(uint64_t callerUID, std::string pkgName, uint64_t templateId, GetPropertyRequest requset, ExecutorProperty &result); + /* Set the executor authentication properties */ int32_t SetExecutorProp(uint64_t callerUID, std::string pkgName, SetPropertyRequest requset, sptr& callback); int32_t GetVersion(); diff --git a/services/include/userauth_controller.h b/services/include/userauth_controller.h index 079c765d73ca4d6b322ed00d1a3fb4df07d0322a..b2589333263c8edb817554822b9673c4f0d27edd 100644 --- a/services/include/userauth_controller.h +++ b/services/include/userauth_controller.h @@ -27,11 +27,10 @@ namespace UserAuth { class UserAuthController { public: int32_t GetAuthTrustLevel(int32_t userId, uint32_t authType, uint32_t &authTrustLevel); - void GetPropAuthInfo(std::string pkgName, uint64_t callerUID, GetPropertyRequest getPropertyRequest, + void GetPropAuthInfo(int32_t userID, std::string pkgName, uint64_t callerUID, GetPropertyRequest getPropertyRequest, sptr& callback); int32_t SetExecutorProp(uint64_t callerUID, std::string pkgName, SetPropertyRequest setPropertyrequest, sptr& callback); - int32_t GetUserID(int32_t &userID); int32_t AddContextID(uint64_t contextID); int32_t IsContextIDExist(uint64_t contextID); int32_t GenerateContextID(uint64_t &contextID); diff --git a/services/include/userauth_datamgr.h b/services/include/userauth_datamgr.h index 73e60ae373fe04239e55eb02ea01fba0d49d7b75..6c6094ff9524f76c570ec206f720154b10bc3480 100755 --- a/services/include/userauth_datamgr.h +++ b/services/include/userauth_datamgr.h @@ -26,7 +26,7 @@ namespace OHOS { namespace UserIAM { namespace UserAuth { -class UserAuthDataMgr : public Singleton { +class UserAuthDataMgr { public: static UserAuthDataMgr &GetInstance(); int32_t AddContextID(uint64_t contextID); diff --git a/services/include/userauth_excallback_impl.h b/services/include/userauth_excallback_impl.h index 65a2a9c441d941324c768939d7070e38f1802d18..fa9a7c024a0e6282a1c46183e1a609a66ac4ffc4 100644 --- a/services/include/userauth_excallback_impl.h +++ b/services/include/userauth_excallback_impl.h @@ -46,14 +46,15 @@ private: int32_t callbackNowCount_ = 0; uint64_t callbackContextID_ = 0; std::string pkgName_ = ""; + int32_t userID_ = 0; uint64_t callerUid_ = 0; - bool callbackResultFlag_ = false; + bool isResultDoneFlag_ = false; sptr callback_ { nullptr }; AuthType authType_; std::mutex mutex_; static std::mutex coauthCallbackmutex_; static std::map> saveCoauthCallback_; - void OnFinishHandleExtend(SetPropertyRequest setPropertyRequest, AuthResult authResult, int32_t ret, + void OnFinishHandleExtend(int32_t userID, SetPropertyRequest setPropertyRequest, AuthResult authResult, int32_t ret, UserAuthToken authToken); }; diff --git a/services/include/userauth_service.h b/services/include/userauth_service.h index 03e22009a8cda3784e41f80e2abbfd6fc7e6dcd3..1311866f24565f6a0c2efd957a4f573da7173cdc 100644 --- a/services/include/userauth_service.h +++ b/services/include/userauth_service.h @@ -45,6 +45,7 @@ public: int32_t GetVersion() override; private: + int32_t GetCallingUserID(int32_t &userID); int32_t GetControllerData(sptr& callback, AuthResult &extraInfo, const AuthTurstLevel authTurstLevel, uint64_t &callerID, std::string &callerName, uint64_t &contextID); diff --git a/services/src/userauth_adapter.cpp b/services/src/userauth_adapter.cpp index 4dff5c26e86a491f9364355bb11c8b3935189aff..d3847843b8b45355d6af26b2064b789f97a0399e 100644 --- a/services/src/userauth_adapter.cpp +++ b/services/src/userauth_adapter.cpp @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include #include "userauth_adapter.h" #include "userauth_hilog_wrapper.h" @@ -26,8 +27,7 @@ namespace OHOS { namespace UserIAM { namespace UserAuth { -const int32_t cUserId = 0; - +const int g_userAuthVersion = 1235; UserAuthAdapter &UserAuthAdapter::GetInstance() { static UserAuthAdapter instance; @@ -45,15 +45,15 @@ int32_t UserAuthAdapter::GetAuthTrustLevel(int32_t userId, uint32_t authType, ui return ret; } -void UserAuthAdapter::GetPropAuthInfo(uint64_t callerUID, std::string pkgName, GetPropertyRequest requset, - sptr& callback) +void UserAuthAdapter::GetPropAuthInfo(int32_t userID, uint64_t callerUID, std::string pkgName, + GetPropertyRequest requset, sptr& callback) { USERAUTH_HILOGD(MODULE_SERVICE, "UserAuth GetPropAuthInfo is start!"); - - std::shared_ptr getInfoCallback = + using namespace UserIDM; + std::shared_ptr getInfoCallback = std::make_shared(callback, requset, callerUID, pkgName); - int32_t ret = UserIDM::UserIDMClient::GetInstance().GetAuthInfo(static_cast(requset.authType), - getInfoCallback); + int32_t ret = UserIDMClient::GetInstance().GetAuthInfo(userID, + static_cast(requset.authType), getInfoCallback); if (ret != SUCCESS) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuth GetPropAuthInfo ERROR!"); } @@ -66,14 +66,16 @@ void UserAuthAdapter::SetPropAuthInfo(uint64_t callerUID, std::string pkgName, i sptr& callback) { USERAUTH_HILOGD(MODULE_SERVICE, "UserAuth SetPropAuthInfo is start!"); + using namespace AuthResPool; FreezInfo freezInfo; freezInfo.callerID = callerUID; freezInfo.authType = requset.authType; freezInfo.pkgName = pkgName; freezInfo.resultCode = resultCode; + std::shared_ptr setPropCallback = std::make_shared(callback, templateIds, authToken, freezInfo); - OHOS::UserIAM::AuthResPool::AuthAttributes authAttributes; + AuthAttributes authAttributes; int32_t ret = SetProPropAuthInfo(authAttributes, callerUID, pkgName, requset, templateIds, setPropCallback); if (ret != SUCCESS) { return; @@ -94,7 +96,7 @@ int32_t UserAuthAdapter::SetProPropAuthInfo(OHOS::UserIAM::AuthResPool::AuthAttr setPropCallback->OnResult(ret, extraInfo); return ret; } - value = requset.key ==SetPropertyType::FREEZE_TEMPLATE ? + value = requset.key == SetPropertyType::FREEZE_TEMPLATE ? static_cast(AuthPropertyMode::PROPERMODE_FREEZE) : static_cast(AuthPropertyMode::PROPERMODE_UNFREEZE); ret = authAttributes.SetUint32Value(AUTH_PROPERTY_MODE, value); @@ -122,9 +124,6 @@ int32_t UserAuthAdapter::SetProPropAuthInfo(OHOS::UserIAM::AuthResPool::AuthAttr setPropCallback->OnResult(ret, extraInfo); return ret; } - if (ret != SUCCESS) { - return ret; - } ret = authAttributes.SetUint64ArrayValue(AUTH_TEMPLATE_ID_LIST, templateIds); if (ret != SUCCESS) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuth SetUint64ArrayValue AUTH_TEMPLATE_ID_LIST ERROR!"); @@ -134,47 +133,40 @@ int32_t UserAuthAdapter::SetProPropAuthInfo(OHOS::UserIAM::AuthResPool::AuthAttr } return ret; } -void UserAuthAdapter::GetPropAuthInfoCoauth(uint64_t callerUID, std::string pkgName, int32_t resultCode, +void UserAuthAdapter::GetPropAuthInfoCoauth(int32_t userID, uint64_t callerUID, std::string pkgName, int32_t resultCode, UserAuthToken authToken, GetPropertyRequest requset, sptr& callback) { USERAUTH_HILOGD(MODULE_SERVICE, "UserAuth GetPropAuthInfoCoauth is start!"); - - std::shared_ptr getInfoCallback = + using namespace UserIDM; + std::shared_ptr getInfoCallback = std::make_shared(callback, callerUID, pkgName, resultCode, authToken, requset); - int32_t ret = UserIDM::UserIDMClient::GetInstance().GetAuthInfo(static_cast(requset.authType), - getInfoCallback); + int32_t ret = UserIDMClient::GetInstance().GetAuthInfo(userID, + static_cast(requset.authType), getInfoCallback); if (ret != SUCCESS) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuth GetPropAuthInfoCoauth ERROR!"); } USERAUTH_HILOGD(MODULE_SERVICE, "UserAuth GetPropAuthInfoCoauth is end!"); } -void UserAuthAdapter::CoauthSetPropAuthInfo(int32_t resultCode, uint64_t callerUID, std::string pkgName, +void UserAuthAdapter::CoauthSetPropAuthInfo(int32_t userID, int32_t resultCode, uint64_t callerUID, std::string pkgName, UserAuthToken authToken, SetPropertyRequest requset, sptr& callback) { USERAUTH_HILOGD(MODULE_SERVICE, "UserAuth CoauthSetPropAuthInfo is start!"); - std::shared_ptr setPropCallback = + using namespace UserIDM; + std::shared_ptr setPropCallback = std::make_shared(callback, callerUID, pkgName, resultCode, authToken, requset); - int32_t ret = UserIDM::UserIDMClient::GetInstance().GetAuthInfo(static_cast(requset.authType), - setPropCallback); + int32_t ret = UserIDMClient::GetInstance().GetAuthInfo(userID, + static_cast(requset.authType), setPropCallback); if (ret != SUCCESS) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuth CoauthSetPropAuthInfo ERROR!"); } USERAUTH_HILOGD(MODULE_SERVICE, "UserAuth CoauthSetPropAuthInfo is end!"); } -int32_t UserAuthAdapter::GetUserID(int32_t &userID) -{ - USERAUTH_HILOGD(MODULE_SERVICE, "UserAuth GetUserID is start!"); - // 打桩 IAMTA - userID = cUserId; - return SUCCESS; -} - int32_t UserAuthAdapter::GenerateSolution(AuthSolution param, std::vector &sessionIds) { USERAUTH_HILOGD(MODULE_SERVICE, "UserAuth GenerateSolution is start!"); @@ -193,6 +185,7 @@ int32_t UserAuthAdapter::RequestAuthResult(uint64_t contextId, std::vector()); - OHOS::UserIAM::AuthResPool::AuthAttributes cAuthAttributes; + auto pAuthAttributes (std::make_shared()); + AuthAttributes cAuthAttributes; int32_t ret = cAuthAttributes.SetUint32Value(AUTH_TYPE, requset.authType); if (ret != SUCCESS) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuth SetUint32Value AUTH_TYPE ERROR!"); @@ -286,8 +280,8 @@ int32_t UserAuthAdapter::GetEachExecutorProp(GetPropertyRequest &requset, Execut result.freezingTime = 0; result.remainTimes = 0; result.authSubType = UserAuth::PIN_SIX; - for (std::vector::const_iterator iter = requset.keys.begin(); iter != requset.keys.end(); ++iter) { - switch (*iter) { + for (auto const &item : requset.keys) { + switch (item) { case AUTH_SUB_TYPE: ret = pAuthAttributes->GetUint64Value(AUTH_SUBTYPE, tmpValue); if (ret != SUCCESS) { @@ -319,7 +313,7 @@ int32_t UserAuthAdapter::GetEachExecutorProp(GetPropertyRequest &requset, Execut return INVALID_PARAMETERS; } } - USERAUTH_HILOGE(MODULE_SERVICE, "UserAuthAdapter::GetEachExecutorProp %{public}llu:%{public}u:%{public}u", + USERAUTH_HILOGE(MODULE_SERVICE, "UserAuthAdapter::GetEachExecutorProp %{public}" PRIu64 ":%{public}u:%{public}u", result.authSubType, result.remainTimes, result.freezingTime); return ret; } @@ -328,10 +322,15 @@ int32_t UserAuthAdapter::SetExecutorProp(uint64_t callerUID, std::string pkgName sptr& callback) { USERAUTH_HILOGD(MODULE_SERVICE, "UserAuth SetExecutorProp is start!"); + using namespace AuthResPool; + uint32_t value; std::shared_ptr setPropCallback = std::make_shared(callback); - OHOS::UserIAM::AuthResPool::AuthAttributes pAuthAttributes; - int32_t ret = pAuthAttributes.SetUint32Value(AUTH_PROPERTY_MODE, PROPERMODE_SET); + AuthAttributes pAuthAttributes; + value = requset.key == SetPropertyType::INIT_ALGORITHM ? + static_cast(AuthPropertyMode::PROPERMODE_INIT_ALGORITHM) + : static_cast(AuthPropertyMode::PROPERMODE_RELEASE_ALGORITHM); + int32_t ret = pAuthAttributes.SetUint32Value(AUTH_PROPERTY_MODE, value); if (ret != SUCCESS) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuth SetUint32Value SET_AUTH_PROPERTY_MODE ERROR!"); return ret; @@ -355,7 +354,7 @@ int32_t UserAuthAdapter::SetExecutorProp(uint64_t callerUID, std::string pkgName USERAUTH_HILOGE(MODULE_SERVICE, "UserAuth SetUint32Value AUTH_TYPE ERROR!"); return ret; } - ret = pAuthAttributes.SetUint8ArrayValue(AUTH_SIGNATURE, requset.setInfo); + ret = pAuthAttributes.SetUint8ArrayValue(ALGORITHM_INFO, requset.setInfo); if (ret != SUCCESS) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuth SetUint8ArrayValue init ERROR!"); return ret; @@ -366,29 +365,29 @@ int32_t UserAuthAdapter::SetExecutorProp(uint64_t callerUID, std::string pkgName int32_t UserAuthAdapter::coAuth(CoAuthInfo coAuthInfo, sptr& callback) { - USERAUTH_HILOGD(MODULE_SERVICE, "UserAuth coAuthHandle is start!"); + USERAUTH_HILOGD(MODULE_SERVICE, "UserAuth coAuth is start!"); std::shared_ptr coAuthCallback = std::make_shared(callback, coAuthInfo, false); OHOS::UserIAM::CoAuth::AuthInfo authInfo; int32_t ret = authInfo.SetPkgName(coAuthInfo.pkgName); if (ret != SUCCESS) { - USERAUTH_HILOGE(MODULE_SERVICE, "UserAuth pAuthInfo_ SetPkgName ERROR!"); + USERAUTH_HILOGE(MODULE_SERVICE, "UserAuth authInfo SetPkgName ERROR!"); return ret; } ret = authInfo.SetCallerUid(coAuthInfo.callerID); if (ret != SUCCESS) { - USERAUTH_HILOGE(MODULE_SERVICE, "UserAuth pAuthInfo_ SetCallerUid ERROR!"); + USERAUTH_HILOGE(MODULE_SERVICE, "UserAuth authInfo SetCallerUid ERROR!"); return ret; } + ret = UserAuthCallbackImplCoAuth::SaveCoauthCallback(coAuthInfo.contextID, coAuthCallback); if (ret != SUCCESS) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuth SaveCoauthCallback ERROR!"); return ret; } - for (std::vector::iterator iter = coAuthInfo.sessionIds.begin(); - iter != coAuthInfo.sessionIds.end(); ++iter) { - CoAuth::CoAuth::GetInstance().coAuth(*iter, authInfo, coAuthCallback); + for (auto const &item : coAuthInfo.sessionIds) { + CoAuth::CoAuth::GetInstance().BeginSchedule(item, authInfo, coAuthCallback); } return SUCCESS; diff --git a/services/src/userauth_async_proxy.cpp b/services/src/userauth_async_proxy.cpp index d12862b1da120f24169130e8c58c71fca20aeade..30660bb26fad289be9431ec9fcc370eb9dde9179 100755 --- a/services/src/userauth_async_proxy.cpp +++ b/services/src/userauth_async_proxy.cpp @@ -29,7 +29,7 @@ void UserAuthAsyncProxy::onAcquireInfo(const int32_t module, const uint32_t acqu if (!data.WriteInterfaceToken(UserAuthAsyncProxy::GetDescriptor())) { USERAUTH_HILOGE(MODULE_SERVICE, "userauth write descriptor failed!"); - return ; + return; } if (!data.WriteInt32(module)) { USERAUTH_HILOGE(MODULE_SERVICE, "failed to WriteInt32(module)."); @@ -59,7 +59,7 @@ void UserAuthAsyncProxy::onResult(const int32_t result, const AuthResult extraIn MessageParcel reply; if (!data.WriteInterfaceToken(UserAuthAsyncProxy::GetDescriptor())) { USERAUTH_HILOGI(MODULE_SERVICE, "userauth write descriptor failed!"); - return ; + return; } if (!data.WriteInt32(result)) { USERAUTH_HILOGE(MODULE_SERVICE, "failed to WriteInt32(result)."); @@ -92,7 +92,7 @@ void UserAuthAsyncProxy::onExecutorPropertyInfo(const ExecutorProperty result) MessageParcel reply; if (!data.WriteInterfaceToken(UserAuthAsyncProxy::GetDescriptor())) { USERAUTH_HILOGI(MODULE_SERVICE, "userauth write descriptor failed!"); - return ; + return; } if (!data.WriteInt32(result.result)) { USERAUTH_HILOGE(MODULE_SERVICE, "failed to WriteInt32(result.result)."); @@ -126,7 +126,7 @@ void UserAuthAsyncProxy::onSetExecutorProperty(const int32_t result) MessageParcel reply; if (!data.WriteInterfaceToken(UserAuthAsyncProxy::GetDescriptor())) { USERAUTH_HILOGE(MODULE_SERVICE, "userauth write descriptor failed!"); - return ; + return; } if (!data.WriteInt32(result)) { USERAUTH_HILOGE(MODULE_SERVICE, "failed to WriteInt32(result)."); diff --git a/services/src/userauth_controller.cpp b/services/src/userauth_controller.cpp index 2beb1145f5089da13cfd057deaeb1bbd8916ff40..8a28a8ea39cac27248918061923b570a534fe0ab 100644 --- a/services/src/userauth_controller.cpp +++ b/services/src/userauth_controller.cpp @@ -29,15 +29,11 @@ int32_t UserAuthController::SetExecutorProp(uint64_t callerUID, std::string pkgN { return UserAuthAdapter::GetInstance().SetExecutorProp(callerUID, pkgName, setPropertyrequest, callback); } -void UserAuthController::GetPropAuthInfo(std::string pkgName, uint64_t callerUID, +void UserAuthController::GetPropAuthInfo(int32_t userID, std::string pkgName, uint64_t callerUID, GetPropertyRequest getPropertyRequest, sptr& callback) { - UserAuthAdapter::GetInstance().GetPropAuthInfo(callerUID, pkgName, getPropertyRequest, callback); -} -int32_t UserAuthController::GetUserID(int32_t &userID) -{ - return UserAuthAdapter::GetInstance().GetUserID(userID); + UserAuthAdapter::GetInstance().GetPropAuthInfo(userID, callerUID, pkgName, getPropertyRequest, callback); } int32_t UserAuthController::GenerateSolution(AuthSolution param, std::vector &sessionIds) { diff --git a/services/src/userauth_excallback_impl.cpp b/services/src/userauth_excallback_impl.cpp index 695f81c01c77a590663bb676e440e1776c1d3454..26556452f3f38f47a092411473334f6a534eea41 100644 --- a/services/src/userauth_excallback_impl.cpp +++ b/services/src/userauth_excallback_impl.cpp @@ -20,6 +20,7 @@ #include "userauth_async_proxy.h" #include "securec.h" +#include #include #include @@ -32,7 +33,7 @@ UserAuthCallbackImplSetProp::UserAuthCallbackImplSetProp(const sptr templateIds, UserAuthToken authToken, FreezInfo freezInfo) { + if (impl == nullptr) { + USERAUTH_HILOGE(MODULE_SERVICE, "UserAuthCallbackImplSetPropFreez impl callback is Null"); + return; + } callback_ = impl; templateIds_.clear(); templateIds_.assign(templateIds.begin(), templateIds.end()); @@ -105,15 +110,16 @@ UserAuthCallbackImplCoAuth::UserAuthCallbackImplCoAuth(const sptr &scheduleToken) { @@ -126,7 +132,7 @@ void UserAuthCallbackImplCoAuth::OnFinish(uint32_t resultCode, std::vectoronResult(BUSY, authResult); - callbackResultFlag_ = true; + isResultDoneFlag_ = true; return; } } @@ -138,18 +144,18 @@ void UserAuthCallbackImplCoAuth::OnAcquireInfo(uint32_t acquire) bool ret = ContextThreadPool::GetInstance().AddTask(callbackContextID_, task); if (!ret) { USERAUTH_HILOGE(MODULE_SERVICE, "OnAcquireInfoHandle ContextThreadPool is BUSY"); - callbackResultFlag_ = true; + isResultDoneFlag_ = true; return; } } -void UserAuthCallbackImplCoAuth::OnFinishHandleExtend(SetPropertyRequest setPropertyRequest, AuthResult authResult, - int32_t ret, UserAuthToken authToken) +void UserAuthCallbackImplCoAuth::OnFinishHandleExtend(int32_t userID, SetPropertyRequest setPropertyRequest, + AuthResult authResult, int32_t ret, UserAuthToken authToken) { if (authType_ == UserAuth::PIN) { USERAUTH_HILOGD(MODULE_SERVICE, "RequestAuthResult SUCCESS"); setPropertyRequest.authType = authType_; setPropertyRequest.key = SetPropertyType::THAW_TEMPLATE; - UserAuthAdapter::GetInstance().CoauthSetPropAuthInfo(ret, callerUid_, pkgName_, + UserAuthAdapter::GetInstance().CoauthSetPropAuthInfo(userID, ret, callerUid_, pkgName_, authToken, setPropertyRequest, callback_); } else { USERAUTH_HILOGD(MODULE_SERVICE, "RequestAuthResult SUCCESS NOT INFO"); @@ -170,56 +176,58 @@ void UserAuthCallbackImplCoAuth::OnFinishHandle(uint32_t resultCode, std::vector GetPropertyRequest getPropertyRequest; AuthResult authResult; std::lock_guard lock(mutex_); - USERAUTH_HILOGD(MODULE_SERVICE, "OnFinishHandle scheduleTokensize:%{public}d, resultCode:%{public}d", + USERAUTH_HILOGD(MODULE_SERVICE, "OnFinishHandle scheduleTokensize:%{public}d, resultCode:%{public}u", scheduleToken.size(), resultCode); callbackNowCount_++; - if (callbackResultFlag_) { + if (isResultDoneFlag_) { return; } int32_t ret = UserAuthAdapter::GetInstance().RequestAuthResult(callbackContextID_, scheduleToken, authToken, sessionIds); + USERAUTH_HILOGD(MODULE_SERVICE, "RequestAuthResult ret:%{public}d", ret); + if (ret == E_RET_UNDONE) { + if (callbackNowCount_ == callbackCount_) { + USERAUTH_HILOGD(MODULE_SERVICE, "UserAuthCallbackImplCoAuth E_RET_UNDONE"); + UserAuthDataMgr::GetInstance().DeleteContextID(callbackContextID_); + UserAuthCallbackImplCoAuth::DeleteCoauthCallback(callbackContextID_); + callback_->onResult(GENERAL_ERROR, authResult); + isResultDoneFlag_ = true; + } + return; + } if (resultCode != LOCKED) { - if (ret == E_RET_UNDONE) { - if (callbackNowCount_ == callbackCount_) { - USERAUTH_HILOGD(MODULE_SERVICE, "UserAuthCallbackImplCoAuth E_RET_UNDONE"); - UserAuthDataMgr::GetInstance().DeleteContextID(callbackContextID_); - UserAuthCallbackImplCoAuth::DeleteCoauthCallback(callbackContextID_); - callback_->onResult(GENERAL_ERROR, authResult); - callbackResultFlag_ = true; - } - return ; - } else if (ret == SUCCESS) { - OnFinishHandleExtend(setPropertyRequest, authResult, ret, authToken); + if (ret == SUCCESS) { + OnFinishHandleExtend(userID_, setPropertyRequest, authResult, ret, authToken); } else { USERAUTH_HILOGD(MODULE_SERVICE, "RequestAuthResult NOT SUCCESS"); getPropertyRequest.authType = authType_; getPropertyRequest.keys.push_back(UserAuth::REMAIN_TIMES); getPropertyRequest.keys.push_back(UserAuth::FREEZING_TIME); - UserAuthAdapter::GetInstance().GetPropAuthInfoCoauth(callerUid_, pkgName_, ret, + UserAuthAdapter::GetInstance().GetPropAuthInfoCoauth(userID_, callerUid_, pkgName_, ret, authToken, getPropertyRequest, callback_); } } else { USERAUTH_HILOGD(MODULE_SERVICE, "UserAuthCallbackImplCoAuth resultCode == LOCKED"); setPropertyRequest.authType = authType_; setPropertyRequest.key = SetPropertyType::FREEZE_TEMPLATE; - UserAuthAdapter::GetInstance().CoauthSetPropAuthInfo(ret, callerUid_, pkgName_, authToken, setPropertyRequest, - callback_); + UserAuthAdapter::GetInstance().CoauthSetPropAuthInfo(userID_, ret, callerUid_, pkgName_, authToken, + setPropertyRequest, callback_); } if (sessionIds.size() != 0) { - for (std::vector::iterator iter = sessionIds.begin(); iter != sessionIds.end(); iter++) { - UserAuthAdapter::GetInstance().Cancel(*iter); + for (auto const &item : sessionIds) { + UserAuthAdapter::GetInstance().Cancel(item); } } UserAuthDataMgr::GetInstance().DeleteContextID(callbackContextID_); UserAuthCallbackImplCoAuth::DeleteCoauthCallback(callbackContextID_); - callbackResultFlag_ = true; + isResultDoneFlag_ = true; } void UserAuthCallbackImplCoAuth::OnAcquireInfoHandle(uint32_t acquire) { USERAUTH_HILOGD(MODULE_SERVICE, "UserAuthCallbackImplCoAuth OnAcquireInfoHandle"); std::lock_guard lock(mutex_); - if (callbackResultFlag_) { + if (isResultDoneFlag_) { return; } int32_t module = static_cast(authType_); @@ -252,7 +260,7 @@ int32_t UserAuthCallbackImplCoAuth::DeleteCoauthCallback(uint64_t contextId) if (iter != saveCoauthCallback_.end()) { saveCoauthCallback_.erase(iter); resultCode = SUCCESS; - USERAUTH_HILOGD(MODULE_SERVICE, "contextId XXXX%{public}04llx is deleted", contextId); + USERAUTH_HILOGD(MODULE_SERVICE, "contextId XXXX%{public}04" PRIx64 " is deleted", contextId); } else { resultCode = FAIL; USERAUTH_HILOGE(MODULE_SERVICE, "contextId is not found and do not delete callback"); @@ -265,8 +273,8 @@ UserAuthCallbackImplIDMGetPorp::UserAuthCallbackImplIDMGetPorp(const sptr& info) { - USERAUTH_HILOGD(MODULE_SERVICE, "UserAuthCallbackImplIDMGetPorp OnGetInfo enter"); + USERAUTH_HILOGD(MODULE_SERVICE, "UserAuthCallbackImplIDMGetPorp OnGetInfo enter"); ExecutorProperty executorProperty = {}; if (info.size() == 0) { executorProperty.result = GENERAL_ERROR; @@ -298,7 +306,7 @@ UserAuthCallbackImplIDMCothGetPorpFreez::UserAuthCallbackImplIDMCothGetPorpFreez { if (impl == nullptr) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuthCallbackImplIDMCothGetPorpFreez impl callback is Null"); - return ; + return; } callback_ = impl; authToken_ = authToken; @@ -309,7 +317,7 @@ UserAuthCallbackImplIDMCothGetPorpFreez::UserAuthCallbackImplIDMCothGetPorpFreez } void UserAuthCallbackImplIDMCothGetPorpFreez::OnGetInfo(std::vector& info) { - USERAUTH_HILOGD(MODULE_SERVICE, "UserAuthCallbackImplIDMCothGetPorpFreez OnGetInfo enter"); + USERAUTH_HILOGD(MODULE_SERVICE, "UserAuthCallbackImplIDMCothGetPorpFreez OnGetInfo enter"); std::vector templateIds; AuthResult authResult; if (info.size() == 0) { @@ -324,8 +332,8 @@ void UserAuthCallbackImplIDMCothGetPorpFreez::OnGetInfo(std::vector::const_iterator iter = info.begin(); iter != info.end(); ++iter) { - templateIds.push_back((*iter).templateId); + for (auto const &item : info) { + templateIds.push_back(item.templateId); } UserAuthAdapter::GetInstance().SetPropAuthInfo(callerUid_, pkgName_, resultCode_, authToken_, requset_, templateIds, callback_); @@ -337,7 +345,7 @@ UserAuthCallbackImplIDMGetPorpCoauth::UserAuthCallbackImplIDMGetPorpCoauth( { if (impl == nullptr) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuthCallbackImplIDMGetPorpCoauth impl callback is Null"); - return ; + return; } callback_ = impl; authToken_ = authToken; diff --git a/services/src/userauth_service.cpp b/services/src/userauth_service.cpp index 4f827b15050cf69a6c1d507ea46db6469329ef17..c584614f4cb908546c006d857c09aa517c0fd0c8 100644 --- a/services/src/userauth_service.cpp +++ b/services/src/userauth_service.cpp @@ -17,8 +17,10 @@ #include #include "userauth_hilog_wrapper.h" #include "useriam_common.h" +#include "accesstoken_kit.h" #include "userauth_service.h" +#define AUTHTURSTLEVEL_SYS 1 namespace OHOS { namespace UserIAM { namespace UserAuth { @@ -66,21 +68,22 @@ int32_t UserAuthService::GetAvailableStatus(const AuthType authType, const AuthT int ret = GENERAL_ERROR; int result = TRUST_LEVEL_NOT_SUPPORT; int32_t userID = 0; - uint32_t authTurstLevelFromSys = 1; + uint32_t authTurstLevelFromSys = AUTHTURSTLEVEL_SYS; if (authTurstLevel > ATL4 || authTurstLevel < ATL1) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuthService GetAvailableStatus AuthTurstLevel is NOT SUPPORT!"); return result; } - ret = userauthController_.GetUserID(userID); + ret = this->GetCallingUserID(userID); if (ret != SUCCESS) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuthService GetAvailableStatus GetUserID is ERROR!"); return result; } + ret = userauthController_.GetAuthTrustLevel(userID, authType, authTurstLevelFromSys); if (ret == SUCCESS) { - USERAUTH_HILOGD(MODULE_INNERKIT, "UserAuthService iAuthTurstLevel_:%{public}d", authTurstLevelFromSys); - USERAUTH_HILOGD(MODULE_INNERKIT, "UserAuthService authTurstLevel:%{public}d", authTurstLevel); + USERAUTH_HILOGD(MODULE_SERVICE, "UserAuthService iAuthTurstLevel_:%{public}u", authTurstLevelFromSys); + USERAUTH_HILOGD(MODULE_SERVICE, "UserAuthService authTurstLevel:%{public}u", authTurstLevel); if (authTurstLevelFromSys < authTurstLevel) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuthService authTurstLevel is ERROR!"); return result; @@ -97,18 +100,26 @@ void UserAuthService::GetProperty(const GetPropertyRequest request, sptrGetCallingUid(); - callerName = std::to_string(callerID); + int32_t ret = this->GetCallingUserID(userID); + if (ret != SUCCESS) { + AuthResult extraInfo; + callback->onResult(ret, extraInfo); + return; + } sptr dr = new UserAuthServiceCallbackDeathRecipient(callback); if ((!callback->AsObject()->AddDeathRecipient(dr))) { USERAUTH_HILOGE(MODULE_SERVICE, "Failed to add death recipient UserAuthServiceCallbackDeathRecipient"); } - userauthController_.GetPropAuthInfo(callerName, callerID, request, callback); + callerID = this->GetCallingUid(); + callerName = std::to_string(callerID); + + userauthController_.GetPropAuthInfo(userID, callerName, callerID, request, callback); } void UserAuthService::SetProperty(const SetPropertyRequest request, sptr& callback) @@ -119,24 +130,47 @@ void UserAuthService::SetProperty(const SetPropertyRequest request, sptrGetCallingUid(); - callerName = std::to_string(callerID); - sptr dr = new UserAuthServiceCallbackDeathRecipient(callback); if ((!callback->AsObject()->AddDeathRecipient(dr))) { USERAUTH_HILOGE(MODULE_SERVICE, "Failed to add death recipient UserAuthServiceCallbackDeathRecipient"); } + + callerID = this->GetCallingUid(); + callerName = std::to_string(callerID); + ret = userauthController_.SetExecutorProp(callerID, callerName, request, callback); if (ret != SUCCESS) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuthService SetExecutorProp getUserID is ERROR!"); callback->onSetExecutorProperty(ret); - return ; + return; } } +int32_t UserAuthService::GetCallingUserID(int32_t &userID) +{ + uint32_t tokenID = this->GetFirstTokenID(); + if (tokenID == 0) { + tokenID = this->GetCallingTokenID(); + } + Security::AccessToken::ATokenTypeEnum callingType = Security::AccessToken::AccessTokenKit::GetTokenType(tokenID); + if (callingType != Security::AccessToken::TOKEN_HAP) { + USERAUTH_HILOGE(MODULE_SERVICE, "CallingType is not hap."); + return TYPE_NOT_SUPPORT; + } + Security::AccessToken::HapTokenInfo hapTokenInfo; + int result = Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenID, hapTokenInfo); + if (result != SUCCESS) { + USERAUTH_HILOGE(MODULE_SERVICE, "Get hap token info failed."); + return TYPE_NOT_SUPPORT; + } + userID = (int32_t)hapTokenInfo.userID; + USERAUTH_HILOGE(MODULE_SERVICE, "GetCallingUserID is %{public}d", userID); + return SUCCESS; +} + uint64_t UserAuthService::Auth(const uint64_t challenge, const AuthType authType, const AuthTurstLevel authTurstLevel, sptr& callback) @@ -151,15 +185,22 @@ uint64_t UserAuthService::Auth(const uint64_t challenge, const AuthType authType AuthSolution authSolutionParam; CoAuthInfo coAuthInfo; AuthResult extraInfo; + + sptr dr = new UserAuthServiceCallbackDeathRecipient(callback); + if ((!callback->AsObject()->AddDeathRecipient(dr))) { + USERAUTH_HILOGE(MODULE_SERVICE, "Failed to add death recipient UserAuthServiceCallbackDeathRecipient"); + } + if (GetControllerData(callback, extraInfo, authTurstLevel, callerID, callerName, contextID) == FAIL) { return ret; } - result = userauthController_.GetUserID(userID); + result = this->GetCallingUserID(userID); if (result != SUCCESS) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuthService Auth GetUserID is ERROR!"); callback->onResult(FAIL, extraInfo); return ret; } + authSolutionParam.contextId = contextID; authSolutionParam.userId = userID; authSolutionParam.authTrustLevel = authTurstLevel; @@ -171,14 +212,12 @@ uint64_t UserAuthService::Auth(const uint64_t challenge, const AuthType authType callback->onResult(result, extraInfo); return ret; } + coAuthInfo.authType = authType; coAuthInfo.callerID = callerID; coAuthInfo.contextID = contextID; coAuthInfo.pkgName = callerName; - sptr dr = new UserAuthServiceCallbackDeathRecipient(callback); - if ((!callback->AsObject()->AddDeathRecipient(dr))) { - USERAUTH_HILOGE(MODULE_SERVICE, "Failed to add death recipient UserAuthServiceCallbackDeathRecipient"); - } + coAuthInfo.userID = userID; result = userauthController_.coAuth(coAuthInfo, callback); if (result != SUCCESS) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuthService Auth coAuth is ERROR!"); @@ -201,9 +240,16 @@ uint64_t UserAuthService::AuthUser(const int32_t userId, const uint64_t challeng AuthSolution authSolutionParam; CoAuthInfo coAuthInfo; AuthResult extraInfo; + + sptr dr = new UserAuthServiceCallbackDeathRecipient(callback); + if ((!callback->AsObject()->AddDeathRecipient(dr))) { + USERAUTH_HILOGE(MODULE_SERVICE, "Failed to add death recipient UserAuthServiceCallbackDeathRecipient"); + } + if (GetControllerData(callback, extraInfo, authTurstLevel, callerID, callerName, contextID) == FAIL) { return ret; } + authSolutionParam.contextId = contextID; authSolutionParam.userId = userId; authSolutionParam.authTrustLevel = authTurstLevel; @@ -216,14 +262,11 @@ uint64_t UserAuthService::AuthUser(const int32_t userId, const uint64_t challeng callback->onResult(result, extraInfo); return ret; } + coAuthInfo.authType = authType; coAuthInfo.callerID = callerID; coAuthInfo.contextID = contextID; coAuthInfo.pkgName = callerName; - sptr dr = new UserAuthServiceCallbackDeathRecipient(callback); - if ((!callback->AsObject()->AddDeathRecipient(dr))) { - USERAUTH_HILOGE(MODULE_SERVICE, "Failed to add death recipient UserAuthServiceCallbackDeathRecipient"); - } result = userauthController_.coAuth(coAuthInfo, callback); if (result != SUCCESS) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuthService AuthUser coAuth is ERROR!"); @@ -242,7 +285,6 @@ int32_t UserAuthService::GetControllerData(sptr& callback, Au int result = SUCCESS; if (callback == nullptr) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuthService AuthUser IUserAuthCallback is NULL!"); - callback->onResult(INVALID_PARAMETERS, extraInfo); ret = FAIL; return ret; } @@ -287,11 +329,10 @@ int32_t UserAuthService::CancelAuth(const uint64_t contextId) result = userauthController_.CancelContext(contextId, sessionIds); if (result == SUCCESS) { - for (std::vector::iterator iter = sessionIds.begin(); iter != sessionIds.end(); iter++) { - result = userauthController_.Cancel(*iter); + for (auto const &item : sessionIds) { + result = userauthController_.Cancel(item); if (result != SUCCESS) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuthService CancelAuth Cancel is ERROR!"); - return result; } } userauthController_.DeleteContextID(contextId); diff --git a/services/src/userauth_stub.cpp b/services/src/userauth_stub.cpp index ee2345a3a2f3e5a67ba61477606046df4e3e483d..f5f9253d21500524a0576acede984c08c7244181 100644 --- a/services/src/userauth_stub.cpp +++ b/services/src/userauth_stub.cpp @@ -103,7 +103,7 @@ int32_t UserAuthStub::GetPropertyStub(MessageParcel& data, MessageParcel& reply) sptr callback = iface_cast(obj); if (callback == nullptr) { - return E_READ_PARCEL_ERROR; + return FAIL; } GetProperty(getPropertyRequest, callback); @@ -142,7 +142,7 @@ int32_t UserAuthStub::SetPropertyStub(MessageParcel& data, MessageParcel& reply) sptr callback = iface_cast(obj); if (callback == nullptr) { - return E_READ_PARCEL_ERROR; + return FAIL; } SetProperty(setPropertyRequest, callback); @@ -177,7 +177,7 @@ int32_t UserAuthStub::AuthStub(MessageParcel& data, MessageParcel& reply) sptr callback = iface_cast(obj); if (callback == nullptr) { - return E_READ_PARCEL_ERROR; + return FAIL; } ret = Auth(challenge, static_cast(authType), static_cast(authTurstLevel), callback); @@ -221,7 +221,7 @@ int32_t UserAuthStub::AuthUserStub(MessageParcel& data, MessageParcel& reply) sptr callback = iface_cast(obj); if (callback == nullptr) { - return E_READ_PARCEL_ERROR; + return FAIL; } ret = AuthUser(userID, challenge, static_cast(authType), diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 28cdb28e042dd160e2e9dc57c7c1bd93b2c58005..33399633fa2b23aba723a32e0c45840afad56389 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -20,10 +20,7 @@ module_output_path = "${userauth_native_part_name}/UserAuth_test" config("module_private_config") { visibility = [ ":*" ] - include_dirs = [ - "include", - "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy/include", - ] + include_dirs = [ "include" ] } ##############################unittest########################################## @@ -34,30 +31,15 @@ ohos_unittest("unittest_test") { include_dirs = [ "include", - - #"//base/user_iam/user_idm/unittest/include", - "//out/hi3516dv300/innerkits/ohos-arm/ipc/ipc_single/include", "//base/user_iam/user_auth/interfaces/innerkits/include", - - #"//base/user_iam/user_idm/services/include", ] - configs = [ - #"${userauth_utils_path}:utils_config", - ":module_private_config", - ] + configs = [ ":module_private_config" ] deps = [ "${userauth_innerkits_path}/native:userauth_framework", - "//drivers/peripheral/display/hal:hdi_display_device", - "//third_party/googletest:gtest_main", "//utils/native/base:utils", ] - external_deps = [ - "hiviewdfx_hilog_native:libhilog", - "ipc:ipc_core", - "safwk:system_ability_fwk", - "samgr_standard:samgr_proxy", - ] + external_deps = [ "ipc:ipc_core" ] } diff --git a/test/unittest/include/userauth_test.h b/test/unittest/include/userauth_test.h index eb0908a704951e98e7480ed07e7e0e68c4b2fee1..e020c97aa0d1e5ecf66d18d81e6fe2e0324f7af8 100644 --- a/test/unittest/include/userauth_test.h +++ b/test/unittest/include/userauth_test.h @@ -25,6 +25,11 @@ void UseriamUtTest_003(); void UseriamUtTest_004(); void UseriamUtTest_005(); void UseriamUtTest_006(); +void UseriamUtTest_007(); +void UseriamUtTest_008(); +void UseriamUtTest_009(); +void UseriamUtTest_010(); +void UseriamUtTest_011(); } } } diff --git a/test/unittest/src/userauth_test.cpp b/test/unittest/src/userauth_test.cpp index 980deb6083c1b3f24cb42bda2bdd7f6ec6b5322d..8d58ee5b8b017f7321d3823a62b9e5148043cb16 100644 --- a/test/unittest/src/userauth_test.cpp +++ b/test/unittest/src/userauth_test.cpp @@ -17,6 +17,8 @@ #include #include #include "user_auth.h" +#include "userauth_api7.h" +#include "userauth_api7_datamgr.h" #include "userauth_callback.h" #include "userauth_info.h" #include "userauth_test.h" @@ -58,8 +60,22 @@ public: void onAcquireInfo(const int32_t module, const uint32_t acquireInfo, const int32_t extraInfo) override; void onResult(const int32_t result, const AuthResult extraInfo) override; - void onExecutorPropertyInfo(const ExecutorProperty result) override; - void onSetExecutorProperty(const int32_t result) override; +}; + +class TestGetPropCallback : public GetPropCallback { +public: + TestGetPropCallback() = default; + virtual ~TestGetPropCallback() = default; + + void onGetProperty(const ExecutorProperty result) override; +}; + +class TestSetPropCallback : public SetPropCallback { +public: + TestSetPropCallback() = default; + virtual ~TestSetPropCallback() = default; + + void onSetProperty(const int32_t result) override; }; void TestUserAuthCallback::onAcquireInfo(const int32_t module, const uint32_t acquireInfo, const int32_t extraInfo) @@ -70,13 +86,32 @@ void TestUserAuthCallback::onResult(const int32_t result, const AuthResult extra { std::cout << "onResult callback" << std::endl; } -void TestUserAuthCallback::onExecutorPropertyInfo(const ExecutorProperty result) +void TestGetPropCallback::onGetProperty(const ExecutorProperty result) { - std::cout << "onExecutorPropertyInfo callback" << std::endl; + std::cout << "onGetProperty callback" << std::endl; } -void TestUserAuthCallback::onSetExecutorProperty(const int32_t result) +void TestSetPropCallback::onSetProperty(const int32_t result) { - std::cout << "onSetExecutorProperty callback" << std::endl; + std::cout << "onSetProperty callback" << std::endl; +} + +class TestUserAuthAPI7Callback : public API7UserAuth::UserAuthAPI7Callback { +public: + TestUserAuthAPI7Callback() = default; + virtual ~TestUserAuthAPI7Callback() = default; + + void API7OnResult(const int32_t result) override; + void TipCallback(const API7UserAuth::Tip result) override; +}; + +void TestUserAuthAPI7Callback::API7OnResult(const int32_t result) +{ + std::cout << "API7OnResult callback" << std::endl; +} + +void TestUserAuthAPI7Callback::TipCallback(const API7UserAuth::Tip result) +{ + std::cout << "TipCallback callback" << std::endl; } HWTEST_F(UseriamUtTest, UseriamUtTest_001, TestSize.Level1) @@ -92,7 +127,7 @@ HWTEST_F(UseriamUtTest, UseriamUtTest_002, TestSize.Level1) request.authType = FACE; request.keys.push_back(1); request.keys.push_back(3); - std::shared_ptr callback = std::make_shared(); + std::shared_ptr callback = std::make_shared(); UserAuth::GetInstance().GetProperty(request, callback); } @@ -103,7 +138,7 @@ HWTEST_F(UseriamUtTest, UseriamUtTest_003, TestSize.Level1) request.key = INIT_ALGORITHM; uint8_t i = 123; request.setInfo.push_back(i); - std::shared_ptr callback = std::make_shared(); + std::shared_ptr callback = std::make_shared(); UserAuth::GetInstance().SetProperty(request, callback); } @@ -113,7 +148,7 @@ HWTEST_F(UseriamUtTest, UseriamUtTest_004, TestSize.Level1) AuthType authType = FACE; AuthTurstLevel authTurstLevel = ATL1; std::shared_ptr callback = std::make_shared(); - EXPECT_EQ(123, UserAuth::GetInstance().Auth(challenge, authType, authTurstLevel, callback)); + EXPECT_NE(0, UserAuth::GetInstance().Auth(challenge, authType, authTurstLevel, callback)); } HWTEST_F(UseriamUtTest, UseriamUtTest_005, TestSize.Level1) @@ -123,14 +158,100 @@ HWTEST_F(UseriamUtTest, UseriamUtTest_005, TestSize.Level1) AuthType authType = FACE; AuthTurstLevel authTurstLevel = ATL1; std::shared_ptr callback = std::make_shared(); - EXPECT_EQ(123, UserAuth::GetInstance().AuthUser(userId, challenge, authType, authTurstLevel, callback)); + EXPECT_NE(0, UserAuth::GetInstance().AuthUser(userId, challenge, authType, authTurstLevel, callback)); } HWTEST_F(UseriamUtTest, UseriamUtTest_006, TestSize.Level1) { - uint64_t contextId = 123; - EXPECT_EQ(0, UserAuth::GetInstance().CancelAuth(contextId)); + int32_t userId = 100; + uint64_t challenge = 001; + AuthType authType = FACE; + AuthTurstLevel authTurstLevel = ATL1; + std::shared_ptr callback = std::make_shared(); + uint64_t contextID = UserAuth::GetInstance().AuthUser(userId, challenge, authType, authTurstLevel, callback); + if (contextID != 0) { + EXPECT_EQ(0, UserAuth::GetInstance().CancelAuth(contextID)); + } else { + EXPECT_NE(0, contextID); + } +} + +HWTEST_F(UseriamUtTest, UseriamUtTest_007, TestSize.Level1) +{ + std::shared_ptr callback = + std::make_shared(); + API7UserAuth::UserAuthAPI7::GetAuthenticator().Execute("FACE_ONLY", "S1", callback); + uint64_t contextID = 0; + uint32_t trytimes = 100; + while (contextID == 0 && trytimes != 0) { + API7UserAuth::UserAuthAPI7Datamgr::GetInstance().API7GetContextID(contextID); + trytimes--; + } + EXPECT_NE(0, contextID); +} + +HWTEST_F(UseriamUtTest, UseriamUtTest_008, TestSize.Level1) +{ + std::shared_ptr callback = + std::make_shared(); + API7UserAuth::UserAuthAPI7::GetAuthenticator().Execute("FACE_ONLY", "S1", callback); + uint64_t contextID = 0; + uint32_t trytimes = 100; + while (contextID == 0 && trytimes != 0) { + API7UserAuth::UserAuthAPI7Datamgr::GetInstance().API7GetContextID(contextID); + trytimes--; + } + if (contextID != 0) { + EXPECT_EQ(0, API7UserAuth::UserAuthAPI7::GetAuthenticator().Cancel()); + } else { + EXPECT_NE(0, contextID); + } +} + +HWTEST_F(UseriamUtTest, UseriamUtTest_009, TestSize.Level1) +{ + EXPECT_EQ(0, API7UserAuth::UserAuthAPI7::GetAuthenticator().CheckAvailability("FACE_ONLY", "S1")); +} + +HWTEST_F(UseriamUtTest, UseriamUtTest_010, TestSize.Level1) +{ + std::shared_ptr callback = + std::make_shared(); + API7UserAuth::UserAuthAPI7::GetAuthenticator().On("tip", callback); + uint32_t trytimes = 100; + bool tipFlag = false; + while (tipFlag == false && trytimes != 0) { + tipFlag = API7UserAuth::UserAuthAPI7Datamgr::GetInstance().GetAPI7Tip(); + trytimes--; + } + EXPECT_TRUE(tipFlag); +} + +HWTEST_F(UseriamUtTest, UseriamUtTest_011, TestSize.Level1) +{ + std::shared_ptr callback = + std::make_shared(); + API7UserAuth::UserAuthAPI7::GetAuthenticator().On("tip", callback); + uint32_t trytimes = 100; + bool tipFlag = false; + while (tipFlag == false && trytimes != 0) { + tipFlag = API7UserAuth::UserAuthAPI7Datamgr::GetInstance().GetAPI7Tip(); + trytimes--; + } + if (tipFlag) { + std::shared_ptr callback = + std::make_shared(); + API7UserAuth::UserAuthAPI7::GetAuthenticator().Off("tip", callback); + trytimes = 100; + while (tipFlag == true && trytimes != 0) { + tipFlag = API7UserAuth::UserAuthAPI7Datamgr::GetInstance().GetAPI7Tip(); + trytimes--; + } + EXPECT_FALSE(tipFlag); + } else { + EXPECT_TRUE(tipFlag); + } +} } } } -} \ No newline at end of file