From 4f024fc3526b23868ed628c191220c8b1827b60c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9F=A9=E9=9C=87?= Date: Wed, 26 Jan 2022 15:31:41 +0800 Subject: [PATCH] fix bug MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 韩震 --- .../kitsimpl/src/iuserauth_API7_callback.cpp | 122 +++ frameworks/kitsimpl/src/userauth_API7.cpp | 148 ++++ .../kitsimpl/src/userauth_API7_Datamgr.cpp | 55 ++ .../js/ohos.UserIAM.userAuth_API7.d.ts | 336 ++++++++ .../innerkits/napi/include/auth_common.h | 54 +- .../innerkits/napi/include/auth_object.h | 2 +- .../innerkits/napi/include/authapi_callback.h | 50 +- .../innerkits/napi/include/user_auth_helper.h | 70 +- .../innerkits/napi/include/user_auth_impl.h | 36 +- .../innerkits/napi/src/authapi_callback.cpp | 813 +++++++++++------- .../innerkits/napi/src/result_convert.cpp | 4 +- .../innerkits/napi/src/user_auth_helper.cpp | 150 +++- .../innerkits/napi/src/user_auth_impl.cpp | 504 +++++++---- interfaces/innerkits/native/BUILD.gn | 3 + .../native/include/iuserauth_API7_callback.h | 43 + .../innerkits/native/include/user_auth.h | 6 +- .../innerkits/native/include/userauth_API7.h | 40 + .../native/include/userauth_API7_Datamgr.h | 42 + .../native/include/userauth_API7_callback.h | 32 + .../include/userauth_API7_hilog_wrapper.h | 72 ++ .../native/include/userauth_API7_info.h | 150 ++++ .../innerkits/native/include/userauth_info.h | 44 +- services/include/userauth_adapter.h | 6 +- services/include/userauth_async_proxy.h | 2 + services/include/userauth_datamgr.h | 2 +- services/src/userauth_adapter.cpp | 45 +- services/src/userauth_async_proxy.cpp | 53 +- services/src/userauth_excallback_impl.cpp | 2 +- 28 files changed, 2314 insertions(+), 572 deletions(-) create mode 100644 frameworks/kitsimpl/src/iuserauth_API7_callback.cpp create mode 100644 frameworks/kitsimpl/src/userauth_API7.cpp create mode 100644 frameworks/kitsimpl/src/userauth_API7_Datamgr.cpp create mode 100644 interfaces/innerkits/js/ohos.UserIAM.userAuth_API7.d.ts create mode 100644 interfaces/innerkits/native/include/iuserauth_API7_callback.h create mode 100644 interfaces/innerkits/native/include/userauth_API7.h create mode 100644 interfaces/innerkits/native/include/userauth_API7_Datamgr.h create mode 100644 interfaces/innerkits/native/include/userauth_API7_callback.h create mode 100644 interfaces/innerkits/native/include/userauth_API7_hilog_wrapper.h create mode 100644 interfaces/innerkits/native/include/userauth_API7_info.h diff --git a/frameworks/kitsimpl/src/iuserauth_API7_callback.cpp b/frameworks/kitsimpl/src/iuserauth_API7_callback.cpp new file mode 100644 index 000000000..6abf9df7e --- /dev/null +++ b/frameworks/kitsimpl/src/iuserauth_API7_callback.cpp @@ -0,0 +1,122 @@ +/* + * 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_API7_Datamgr.h" +#include "userauth_API7_info.h" +#include "iuserauth_API7_callback.h" +#include "userauth_API7_hilog_wrapper.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) +{ + USERAUTH_API7_HILOGD(MODULE_API7, "userauthAPI7 onAcquireInfo is start"); + std::string sTipInfo = ""; + if (UserAuthAPI7Datamgr::GetInstance().GetAPI7Tip()) { + USERAUTH_API7_HILOGD(MODULE_API7, "userauthAPI7 GetAPI7Tip is on"); + API7UserAuth::Tip sTip; + sTip.errorCode = API7UserAuth::SUCCESS; + sTip.tipEvent = API7UserAuth::ACQUIRE; + sTip.tipCode = acquireInfo; + if (g_convertTipInfo.count(static_cast(acquireInfo)) == 0) { + USERAUTH_API7_HILOGE(MODULE_API7, "userauthAPI7 g_convertTipInfo is 0"); + sTipInfo = "other error"; + } else { + sTipInfo = g_convertTipInfo[static_cast(acquireInfo)]; + } + callback_->TipCallback(sTip); + USERAUTH_API7_HILOGD(MODULE_API7, "userauthAPI7 GetAPI7Tip is end"); + } + USERAUTH_API7_HILOGD(MODULE_API7, "userauthAPI7 onAcquireInfo is end"); +} + +void IUserAuthAPI7Callback::onResult(const int32_t result, const UserAuth::AuthResult extraInfo) +{ + USERAUTH_API7_HILOGD(MODULE_API7, "userauthAPI7 onResult is start"); + AuthenticationResult iResult; + UserAuthAPI7Datamgr::GetInstance().API7DeleteContextID(); + callback_->API7OnResult(result); + if (UserAuthAPI7Datamgr::GetInstance().GetAPI7Tip()) { + USERAUTH_API7_HILOGD(MODULE_API7, "userauthAPI7 GetAPI7Tip is on"); + API7UserAuth::Tip sTip; + if (result != UserAuth::SUCCESS) { + USERAUTH_API7_HILOGE(MODULE_API7, "userauthAPI7 result is failed"); + sTip.errorCode = API7UserAuth::FAILED; + sTip.tipInfo = "FAILED"; + } else { + sTip.errorCode = API7UserAuth::SUCCESSED; + sTip.tipInfo = "SUCCESSED"; + } + sTip.tipEvent = API7UserAuth::RESULT; + if (g_convertResult.count(static_cast(result)) == 0) { + USERAUTH_API7_HILOGE(MODULE_API7, "userauthAPI7 g_convertResult is 0"); + iResult = static_cast(result); + } else { + iResult = g_convertResult[static_cast(result)]; + } + sTip.tipCode = iResult; + callback_->TipCallback(sTip); + USERAUTH_API7_HILOGD(MODULE_API7, "userauthAPI7 GetAPI7Tip is end"); + } + USERAUTH_API7_HILOGD(MODULE_API7, "userauthAPI7 onResult is end"); +} +void IUserAuthAPI7Callback::onExecutorPropertyInfo(const UserAuth::ExecutorProperty result) {} +void IUserAuthAPI7Callback::onSetExecutorProperty(const int32_t result) {} +} +} +} \ No newline at end of file diff --git a/frameworks/kitsimpl/src/userauth_API7.cpp b/frameworks/kitsimpl/src/userauth_API7.cpp new file mode 100644 index 000000000..bb615c7bc --- /dev/null +++ b/frameworks/kitsimpl/src/userauth_API7.cpp @@ -0,0 +1,148 @@ +/* + * 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" +#include "userauth_API7_hilog_wrapper.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) +{ + USERAUTH_API7_HILOGD(MODULE_API7, "userauthAPI7 execute is start"); + UserAuth::AuthType uAuthType; + UserAuth::AuthTurstLevel uAuthTurstLevel; + uint64_t contextID = 0; + + if (callback == nullptr) { + USERAUTH_API7_HILOGE(MODULE_API7, "userauthAPI7 execute callback is Null"); + return; + } + + if (g_convertAuthType.count(authtype) == 0) { + USERAUTH_API7_HILOGE(MODULE_API7, "userauthAPI7 execute g_convertAuthType is 0"); + callback->API7OnResult(NO_SUPPORT); + return; + } else { + uAuthType = g_convertAuthType[authtype]; + } + + if (g_convertAuthTurstLevel.count(trustLevel) == 0) { + USERAUTH_API7_HILOGE(MODULE_API7, "userauthAPI7 execute g_convertAuthTurstLevel is 0"); + callback->API7OnResult(NO_SUPPORT); + return; + } else { + uAuthTurstLevel = g_convertAuthTurstLevel[trustLevel]; + } + + std::shared_ptr asyncStub = std::make_shared(callback); + contextID = UserAuth::UserAuth::GetInstance().Auth(CHALLENGE_INIT, uAuthType, uAuthTurstLevel, asyncStub); + if (contextID != CONTEXTID_INIT) { + UserAuthAPI7Datamgr::GetInstance().API7AddContextID(contextID); + } + USERAUTH_API7_HILOGD(MODULE_API7, "userauthAPI7 execute is end"); +} + +int32_t UserAuthAPI7::Cancel() +{ + USERAUTH_API7_HILOGD(MODULE_API7, "userauthAPI7 cancel is start"); + uint64_t contextID = 0; + int32_t ret = API7UserAuth::GENERAL_ERROR; + + UserAuthAPI7Datamgr::GetInstance().API7GetContextID(contextID); + if (contextID != CONTEXTID_INIT) { + ret = UserAuth::UserAuth::GetInstance().CancelAuth(contextID); + if (ret != API7UserAuth::SUCCESS) { + USERAUTH_API7_HILOGE(MODULE_API7, "userauthAPI7 cancel is error"); + } + UserAuthAPI7Datamgr::GetInstance().API7DeleteContextID(); + } + USERAUTH_API7_HILOGD(MODULE_API7, "userauthAPI7 cancel is end"); + return ret; +} + +int32_t UserAuthAPI7::CheckAvailability(std::string authtype, std::string trustLevel) +{ + USERAUTH_API7_HILOGD(MODULE_API7, "userauthAPI7 checkAvailability is start"); + UserAuth::AuthType uAuthType; + UserAuth::AuthTurstLevel uAuthTurstLevel; + + if (g_convertAuthType.count(authtype) == 0) { + USERAUTH_API7_HILOGE(MODULE_API7, "userauthAPI7 checkAvailability g_convertAuthType is 0"); + return AUTH_TYPE_NOT_SUPPORT; + } else { + uAuthType = g_convertAuthType[authtype]; + } + + if (g_convertAuthTurstLevel.count(trustLevel) == 0) { + USERAUTH_API7_HILOGE(MODULE_API7, "userauthAPI7 checkAvailability g_convertAuthTurstLevel is 0"); + return SECURE_LEVEL_NOT_SUPPORT; + } else { + uAuthTurstLevel = g_convertAuthTurstLevel[trustLevel]; + } + + int32_t ret = UserAuth::UserAuth::GetInstance().GetAvailableStatus(uAuthType, uAuthTurstLevel); + if (ret != UserAuth::SUCCESS) { + USERAUTH_API7_HILOGE(MODULE_API7, "userauthAPI7 checkAvailability is error "); + } + USERAUTH_API7_HILOGD(MODULE_API7, "userauthAPI7 checkAvailability is end"); + return ret; +} + +void UserAuthAPI7::On(std::string tip, std::shared_ptr tipCallback) +{ + USERAUTH_API7_HILOGD(MODULE_API7, "userauthAPI7 On is start"); + std::string sTip = "tip"; + + if (!sTip.compare(tip)) { + USERAUTH_API7_HILOGE(MODULE_API7, "userauthAPI7 On is not tip"); + UserAuthAPI7Datamgr::GetInstance().SetAPI7Tip(false); + return; + } + UserAuthAPI7Datamgr::GetInstance().SetAPI7Tip(true); + USERAUTH_API7_HILOGD(MODULE_API7, "userauthAPI7 On is end"); +} + +void UserAuthAPI7::Off(std::string tip, std::shared_ptr tipCallback) +{ + USERAUTH_API7_HILOGD(MODULE_API7, "userauthAPI7 Off is start"); + UserAuthAPI7Datamgr::GetInstance().SetAPI7Tip(false); + USERAUTH_API7_HILOGD(MODULE_API7, "userauthAPI7 Off is end"); +} +} // namespace userAuth +} // namespace UserIam +} // namespace OHOS diff --git a/frameworks/kitsimpl/src/userauth_API7_Datamgr.cpp b/frameworks/kitsimpl/src/userauth_API7_Datamgr.cpp new file mode 100644 index 000000000..79fb14ff1 --- /dev/null +++ b/frameworks/kitsimpl/src/userauth_API7_Datamgr.cpp @@ -0,0 +1,55 @@ +/* + * 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 "userauth_info.h" +#include "userauth_hilog_wrapper.h" +#include "userauth_API7_info.h" +#include "userauth_API7_Datamgr.h" + +namespace OHOS { +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) +{ + isTipOn_ = isTipOn; +} +bool UserAuthAPI7Datamgr::GetAPI7Tip() +{ + return isTipOn_; +} +} // namespace UserAuth +} // namespace UserIam +} // namespace OHOS 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 000000000..5eca79f7b --- /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/include/auth_common.h b/interfaces/innerkits/napi/include/auth_common.h index dcecddd60..1622eed87 100755 --- a/interfaces/innerkits/napi/include/auth_common.h +++ b/interfaces/innerkits/napi/include/auth_common.h @@ -16,6 +16,7 @@ #define OHOS_USERAUTH_COMMON_H #include +#include #include "napi/native_api.h" #include "napi/native_common.h" @@ -23,6 +24,7 @@ namespace OHOS { namespace UserIAM { namespace UserAuth { +#define RESULTVAR 0 #define ARGS_MAX_COUNT 10 #define ARGS_ASYNC_COUNT 1 #define GET_VALUE_ERROR (-1) @@ -58,30 +60,27 @@ struct CallBackInfo { }; struct GetPropertyInfo { - GetPropertyInfo() : result(nullptr) {}; + GetPropertyInfo() : asyncWork(nullptr), result(nullptr){}; CallBackInfo callBackInfo; + napi_async_work asyncWork; napi_value result; int32_t authType; std::vector keys; - int32_t getResult; - uint64_t authSubType; - uint32_t remainTimes; - uint32_t freezingTime; + napi_value resultData[PARAM1]; }; struct SetPropertyInfo { - SetPropertyInfo() : asyncWork(nullptr), result(nullptr) {}; + SetPropertyInfo() : asyncWork(nullptr), result(nullptr){}; CallBackInfo callBackInfo; napi_async_work asyncWork; napi_value result; int32_t authType; - int32_t key; + uint32_t key; std::vector setInfo; - int32_t setResult; }; struct AuthInfo { - AuthInfo() : asyncWork(nullptr) {}; + AuthInfo() : asyncWork(nullptr){}; CallBackInfo callBackInfo; napi_callback_info info; napi_async_work asyncWork; @@ -90,19 +89,16 @@ struct AuthInfo { 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; + napi_value extraInfo; }; struct AuthUserInfo { - AuthUserInfo() : asyncWork(nullptr) {}; + AuthUserInfo() : asyncWork(nullptr){}; CallBackInfo callBackInfo; napi_callback_info info; napi_async_work asyncWork; @@ -111,16 +107,38 @@ struct AuthUserInfo { 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; + napi_value extraInfo; +}; + +struct ExecuteInfo { + ExecuteInfo() : 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 diff --git a/interfaces/innerkits/napi/include/auth_object.h b/interfaces/innerkits/napi/include/auth_object.h index c1fbf65c9..a132515b8 100755 --- a/interfaces/innerkits/napi/include/auth_object.h +++ b/interfaces/innerkits/napi/include/auth_object.h @@ -32,7 +32,7 @@ public: class Napi_SetPropertyRequest { public: int32_t authType_ = 0; - int32_t key_ = 0; + uint32_t key_ = 0; std::vector setInfo_; }; diff --git a/interfaces/innerkits/napi/include/authapi_callback.h b/interfaces/innerkits/napi/include/authapi_callback.h index 87fbf6de1..68aa4059a 100755 --- a/interfaces/innerkits/napi/include/authapi_callback.h +++ b/interfaces/innerkits/napi/include/authapi_callback.h @@ -17,24 +17,20 @@ #define AUTHAPI_CALLBACK_H #include "napi/native_api.h" +#include +#include #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(); @@ -48,17 +44,39 @@ public: void onResult(const int32_t result, const AuthResult extraInfo) override; void onSetExecutorProperty(const int32_t result) override; - static napi_value BuildExecutorProperty( +private: + 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); + napi_value Uint64ToNapi(napi_env env, uint64_t value); + napi_value BuildOnResult(napi_env env, uint32_t remainTimes, uint32_t freezingTime, std::vector token); + napi_value Uint8ArrayToNapi(napi_env env, std::vector value); + void OnResultUserInfo(AuthUserInfo *authUserInfo); + void OnResultAuthInfo(AuthInfo *authInfo); + void OnAcquireInfoAuthInfo(const int32_t module, const uint32_t acquireInfo, const int32_t extraInfo, AuthInfo *authInfo); + void OnResultUserInfoWrap(uv_loop_s *loop, uv_work_t *work); + void OnResultAuthInfoWrap(uv_loop_s *loop, uv_work_t *work); + void OnAcquireInfoAuthInfoWrap(uv_loop_s *loop, uv_work_t *work); + void OnExecutorPropertyInfoWrap(uv_loop_s *loop, uv_work_t *work); + void OnAcquireInfoWrap(uv_loop_s *loop, uv_work_t *work); + void OnSetExecutorPropertyWrap(uv_loop_s *loop, uv_work_t *work); + std::mutex mutex_; +}; + +class API7callback : public API7UserAuth::UserAuthAPI7Callback { +public: + explicit API7callback(ExecuteInfo* executeInfo); + explicit API7callback(FunctionOnAndOffInfo* functionOnAndOffInfo); + virtual ~API7callback() = default; + ExecuteInfo* executeInfo_; + FunctionOnAndOffInfo* functionOnAndOffInfo_; + void API7OnResult(const int32_t result) override; + void TipCallback(const API7UserAuth::Tip result) override; private: - void OnAuthAcquireInfo(AcquireInfoInner *acquireInfoInner); - void OnUserAuthResult(const int32_t result, const AuthResult extraInfo); - void OnAuthResult(const int32_t result, const AuthResult extraInfo); + 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 diff --git a/interfaces/innerkits/napi/include/user_auth_helper.h b/interfaces/innerkits/napi/include/user_auth_helper.h index a75856cbf..93f62eec8 100755 --- a/interfaces/innerkits/napi/include/user_auth_helper.h +++ b/interfaces/innerkits/napi/include/user_auth_helper.h @@ -36,6 +36,15 @@ void Init(napi_env env, napi_value exports); * @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 GetCtor_API(napi_env env); + /** * @brief Construction method * @@ -45,15 +54,32 @@ napi_value GetCtor(napi_env env); */ 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 Instance + * @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 * @@ -118,6 +144,48 @@ napi_value AuthUser(napi_env env, napi_callback_info 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 Execute(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 diff --git a/interfaces/innerkits/napi/include/user_auth_impl.h b/interfaces/innerkits/napi/include/user_auth_impl.h index 877dececd..4aed26acb 100755 --- a/interfaces/innerkits/napi/include/user_auth_impl.h +++ b/interfaces/innerkits/napi/include/user_auth_impl.h @@ -19,16 +19,12 @@ #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(); @@ -43,18 +39,16 @@ public: 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 GetPropertyWrap(napi_env env, napi_callback_info info, GetPropertyInfo *getPropertyInfo); + napi_value GetPropertyAsync(napi_env env, GetPropertyInfo *getPropertyInfo); + napi_value GetPropertyPromise(napi_env env, GetPropertyInfo *getPropertyInfo); - napi_value 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 SetPropertyWrap(napi_env env, napi_callback_info info, SetPropertyInfo *setPropertyInfo); + napi_value SetPropertyAsync(napi_env env, SetPropertyInfo *setPropertyInfo); + napi_value SetPropertyPromise(napi_env env, SetPropertyInfo *setPropertyInfo); napi_value 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); @@ -63,6 +57,20 @@ private: 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 Execute(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, napi_callback_info info, ExecuteInfo* executeInfo); + napi_value ExecuteAsyncallback(napi_env, napi_value* argv, ExecuteInfo* executeInfo); + napi_value ExecutePromise(napi_env, napi_value* argv, ExecuteInfo* executeInfo); +}; } // 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 7abfba6b2..0b026f830 100755 --- a/interfaces/innerkits/napi/src/authapi_callback.cpp +++ b/interfaces/innerkits/napi/src/authapi_callback.cpp @@ -14,10 +14,7 @@ */ #include "authapi_callback.h" - -#include #include "securec.h" - #include "auth_hilog_wrapper.h" namespace OHOS { @@ -31,9 +28,172 @@ AuthApiCallback::~AuthApiCallback() { } +API7callback::API7callback(ExecuteInfo* executeInfo) +{ + executeInfo_ = executeInfo; +} + +API7callback::API7callback(FunctionOnAndOffInfo* functionOnAndOffInfo) +{ + functionOnAndOffInfo_ = functionOnAndOffInfo; +} + +void API7callback::API7OnResultWrap(uv_loop_s *loop, uv_work_t *work) +{ + HILOG_INFO("authFace : %{public}s, start.", __func__); + uv_queue_work(loop, work, [] (uv_work_t *work) { }, [] (uv_work_t *work, int status) { + ExecuteInfo *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) +{ + HILOG_INFO("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) { + HILOG_INFO("napi_create_int32 faild"); + } + uv_loop_s *loop(nullptr); + status = napi_get_uv_event_loop(env, &loop); + if (status != napi_ok) { + HILOG_INFO("napi_get_uv_event_loop faild"); + } + if (loop == nullptr) { + return; + } + uv_work_t *work = new(std::nothrow) uv_work_t; + if (work == nullptr) { + return; + } + ExecuteInfo *executeInfo = new (std::nothrow) ExecuteInfo(); + executeInfo = executeInfo_; + if (executeInfo == nullptr) { + delete work; + work = nullptr; + return; + } + work->data = reinterpret_cast(executeInfo); + API7OnResultWrap(loop, work); + } else { + HILOG_ERROR("asyncCallbackContext_ is nullptr"); + } +} + +void API7callback::TipCallbackWrap(uv_loop_s *loop, uv_work_t *work) +{ + HILOG_INFO("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) +{ + HILOG_INFO("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) { + HILOG_INFO("napi_get_uv_event_loop faild"); + } + if (loop == nullptr) { + return; + } + uv_work_t *work = new(std::nothrow) uv_work_t; + if (work == 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 { + HILOG_ERROR("asyncCallbackContext_ is nullptr"); + } + HILOG_ERROR("TipCallback is end"); +} + +napi_value API7callback::ResultTips(napi_env env, const API7UserAuth::Tip result) +{ + HILOG_INFO("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::BuildExecutorProperty( napi_env env, int32_t result, uint32_t remainTimes, uint32_t freezingTime, uint64_t authSubType) { + HILOG_INFO("authFace : %{public}s, start.", __func__); napi_value jsObject = nullptr; NAPI_CALL(env, napi_create_object(env, &jsObject)); @@ -49,14 +209,14 @@ napi_value AuthApiCallback::BuildExecutorProperty( NAPI_CALL(env, napi_create_uint32(env, freezingTime, &freezingTimeValue)); NAPI_CALL(env, napi_set_named_property(env, jsObject, "freezingTime", freezingTimeValue)); - napi_value authSubTypeValue = nullptr; - NAPI_CALL(env, napi_create_uint32(env, static_cast(authSubType), &authSubTypeValue)); - NAPI_CALL(env, napi_set_named_property(env, jsObject, "authSubType", authSubTypeValue)); + napi_value jsType = Uint64ToNapi(env, authSubType); + NAPI_CALL(env, napi_set_named_property(env, jsObject, "authSubType", jsType)); return jsObject; } napi_value AuthApiCallback::Uint64ToNapi(napi_env env, uint64_t value) { + HILOG_INFO("authFace : %{public}s, start.", __func__); size_t length = sizeof(value); napi_value out = nullptr; void *data = nullptr; @@ -70,6 +230,7 @@ napi_value AuthApiCallback::Uint64ToNapi(napi_env env, uint64_t value) napi_value AuthApiCallback::Uint8ArrayToNapi(napi_env env, std::vector value) { + HILOG_INFO("authFace : %{public}s, start.", __func__); int size = value.size(); HILOG_INFO("Uint8ArrayToNapi size = %{public}d", size); napi_value out = nullptr; @@ -86,6 +247,7 @@ napi_value AuthApiCallback::Uint8ArrayToNapi(napi_env env, std::vector napi_value AuthApiCallback::BuildOnResult( napi_env env, uint32_t remainTimes, uint32_t freezingTime, std::vector token) { + HILOG_INFO("authFace : %{public}s, start.", __func__); napi_value jsObject = nullptr; NAPI_CALL(env, napi_create_object(env, &jsObject)); @@ -102,381 +264,434 @@ napi_value AuthApiCallback::BuildOnResult( return jsObject; } -static void GetPropertyInfoCallback(uv_work_t* work, int status) +void AuthApiCallback::OnResultUserInfoWrap(uv_loop_s *loop, uv_work_t *work) { - 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; + HILOG_INFO("authFace : %{public}s, start.", __func__); + uv_queue_work(loop, work, [] (uv_work_t *work) { }, [] (uv_work_t *work, int status) { + AuthUserInfo *authUserInfo = reinterpret_cast(work->data); + if (authUserInfo == nullptr) { + delete work; + work = nullptr; + return; } - } 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; + napi_env env = authUserInfo->callBackInfo.env; + napi_value undefined; + napi_get_undefined(env, &undefined); + if (authUserInfo->onResult == nullptr) { + delete work; + work = nullptr; + return; } - } -EXIT: - delete getPropertyInfo; - delete work; + napi_value callback; + napi_get_reference_value(env, authUserInfo->onResult, &callback); + napi_value callResult = nullptr; + napi_value params[2] = {0}; + napi_create_int32(env, authUserInfo->result, ¶ms[PARAM0]); + params[PARAM1] = authUserInfo->extraInfo; + napi_call_function(env, undefined, callback, 2, params, &callResult); + delete authUserInfo; + authUserInfo = nullptr; + delete work; + work = nullptr; + }); } -void AuthApiCallback::onExecutorPropertyInfo(const ExecutorProperty result) +void AuthApiCallback::OnResultUserInfo(AuthUserInfo *authUserInfo) { - 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; - } + HILOG_INFO("authFace : %{public}s, start.", __func__); uv_loop_s *loop(nullptr); - napi_get_uv_event_loop(getPropertyInfo_->callBackInfo.env, &loop); + napi_status status = napi_get_uv_event_loop(authUserInfo->callBackInfo.env, &loop); + if (status != napi_ok) { + HILOG_INFO("napi_get_uv_event_loop faild"); + } if (loop == nullptr) { - HILOG_ERROR("loop is null"); - delete getPropertyInfo_; - getPropertyInfo_ = nullptr; return; - } - uv_work_t *work = new (std::nothrow) uv_work_t; + } + 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); + AuthUserInfo *authUserInfo_ = new (std::nothrow) AuthUserInfo(); + authUserInfo_ = authUserInfo; + if (authUserInfo_ == nullptr) { + delete work; + work = nullptr; + return; + } + work->data = reinterpret_cast(authUserInfo_); + OnResultUserInfoWrap(loop, work); + delete authUserInfo; + authUserInfo = nullptr; } -void AuthApiCallback::OnAuthAcquireInfo(AcquireInfoInner *acquireInfoInner) +void AuthApiCallback::OnResultAuthInfoWrap(uv_loop_s *loop, uv_work_t *work) { - 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"); + HILOG_INFO("authFace : %{public}s, start.", __func__); + uv_queue_work(loop, work, [] (uv_work_t *work) { }, [] (uv_work_t *work, int status) { + AuthInfo *authInfo = reinterpret_cast(work->data); + if (authInfo == nullptr) { delete work; + work = nullptr; 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; + napi_env env = authInfo->callBackInfo.env; + napi_value undefined; + napi_get_undefined(env, &undefined); + if (authInfo->onResult == nullptr) { delete work; + work = nullptr; 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; + napi_value callback; + napi_get_reference_value(env, authInfo->onResult, &callback); + napi_value callResult = nullptr; + napi_value params[2] = {0}; + napi_create_int32(env, authInfo->result, ¶ms[PARAM0]); + params[PARAM1] = authInfo->extraInfo; + napi_call_function(env, undefined, callback, 2, params, &callResult); + delete authInfo; + authInfo = nullptr; delete work; + work = nullptr; }); } -void AuthApiCallback::onAcquireInfo(const int32_t module, const uint32_t acquireInfo, const int32_t extraInfo) +void AuthApiCallback::OnResultAuthInfo(AuthInfo *authInfo) { - 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 "); + HILOG_INFO("authFace : %{public}s, start.", __func__); + uv_loop_s *loop(nullptr); + napi_status status = napi_get_uv_event_loop(authInfo->callBackInfo.env, &loop); + if (status != napi_ok) { + HILOG_INFO("napi_get_uv_event_loop faild"); } - - 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 "); + if (loop == nullptr) { + return; } - 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; + uv_work_t *work = new(std::nothrow) uv_work_t; + if (work == nullptr) { 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; + AuthInfo *authInfo_ = new (std::nothrow) AuthInfo(); + authInfo_ = authInfo; + if (authInfo_ == nullptr) { delete work; + work = nullptr; 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; + work->data = reinterpret_cast(authInfo_); + OnResultAuthInfoWrap(loop, work); + delete authInfo; + authInfo = nullptr; +} + +void AuthApiCallback::OnAcquireInfoAuthInfoWrap(uv_loop_s *loop, uv_work_t *work) +{ + HILOG_INFO("authFace : %{public}s, start.", __func__); + uv_queue_work(loop, work, [] (uv_work_t *work) { }, [] (uv_work_t *work, int status) { + AuthInfo *authInfo = reinterpret_cast(work->data); + if (authInfo == nullptr) { + delete work; + work = nullptr; + return; + } + napi_env env = authInfo->callBackInfo.env; + napi_value undefined; + napi_get_undefined(env, &undefined); + if (authInfo->onAcquireInfo == nullptr) { + delete work; + work = nullptr; + return; + } + napi_value callback; + napi_get_reference_value(env, authInfo->onAcquireInfo, &callback); + napi_value callResult = nullptr; + napi_call_function(env, undefined, callback, 3, authInfo->onAcquireInfoData, &callResult); + delete authInfo; + authInfo = nullptr; + delete work; + work = nullptr; + }); } -void AuthApiCallback::OnUserAuthResult(const int32_t result, const AuthResult extraInfo) +void AuthApiCallback::OnAcquireInfoAuthInfo(const int32_t module, const uint32_t acquireInfo, + const int32_t extraInfo, AuthInfo *authInfo) { - HILOG_INFO("AuthApiCallback OnUserAuthResult start"); + HILOG_INFO("authFace : %{public}s, start.", __func__); + napi_status status = napi_create_int32(authInfo->callBackInfo.env, module, &authInfo->onAcquireInfoData[PARAM0]); + if (status != napi_ok) { + HILOG_INFO("napi_create_int32 faild"); + } + status = napi_create_uint32(authInfo->callBackInfo.env, acquireInfo, &authInfo->onAcquireInfoData[PARAM1]); + if (status != napi_ok) { + HILOG_INFO("napi_create_uint32 faild"); + } + status = napi_create_int32(authInfo->callBackInfo.env, extraInfo, &authInfo->onAcquireInfoData[PARAM2]); + if (status != napi_ok) { + HILOG_INFO("napi_create_int32 faild"); + } uv_loop_s *loop(nullptr); - napi_get_uv_event_loop(userInfo_->callBackInfo.env, &loop); + status = napi_get_uv_event_loop(authInfo->callBackInfo.env, &loop); + if (status != napi_ok) { + HILOG_INFO("napi_get_uv_event_loop faild"); + } if (loop == nullptr) { - HILOG_ERROR("loop is null"); - delete userInfo_; - userInfo_ = nullptr; return; } - uv_work_t *work = new (std::nothrow) uv_work_t; + 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); + AuthInfo *authInfo_ = new (std::nothrow) AuthInfo(); + authInfo_ = authInfo; + if (authInfo_ == nullptr) { + delete work; + work = nullptr; + return; + } + work->data = reinterpret_cast(authInfo_); + OnAcquireInfoAuthInfoWrap(loop, work); } -static void OnAuthResultWork(uv_work_t *work, int status) +void AuthApiCallback::OnExecutorPropertyInfoWrap(uv_loop_s *loop, uv_work_t *work) { - HILOG_INFO("Do OnAuthResult work"); - AuthInfo *authInfo = reinterpret_cast(work->data); - if (authInfo == nullptr) { - HILOG_ERROR("authInfo is null"); + HILOG_INFO("authFace : %{public}s, start.", __func__); + uv_queue_work(loop, work, [] (uv_work_t *work) { }, [] (uv_work_t *work, int status) { + GetPropertyInfo *getPropertyInfo = reinterpret_cast(work->data); + if (getPropertyInfo == nullptr) { + delete work; + work = nullptr; + return; + } + napi_env env = getPropertyInfo->callBackInfo.env; + napi_value undefined; + napi_get_undefined(env, &undefined); + if (getPropertyInfo->callBackInfo.callBack == nullptr && getPropertyInfo->callBackInfo.deferred != nullptr) { + napi_resolve_deferred(env, getPropertyInfo->callBackInfo.deferred, getPropertyInfo->resultData[PARAM0]); + delete getPropertyInfo; + getPropertyInfo = nullptr; + delete work; + work = nullptr; + return; + } + napi_value callback; + napi_get_reference_value(env, getPropertyInfo->callBackInfo.callBack, &callback); + napi_value callResult = nullptr; + napi_call_function(env, undefined, callback, 3, getPropertyInfo->resultData, &callResult); + delete getPropertyInfo; + getPropertyInfo = nullptr; delete work; - return; + work = nullptr; + }); +} + +void AuthApiCallback::onExecutorPropertyInfo(const ExecutorProperty result) +{ + HILOG_INFO("authFace : %{public}s, start.", __func__); + HILOG_INFO("AuthApiCallback onExecutorPropertyInfo 1 = %{public}d", result.result); + HILOG_INFO("AuthApiCallback onExecutorPropertyInfo 2 = %{public}llu", result.authSubType); + HILOG_INFO("AuthApiCallback onExecutorPropertyInfo 3 = %{public}u", result.remainTimes); + HILOG_INFO("AuthApiCallback onExecutorPropertyInfo 4 = %{public}u", result.freezingTime); + if (getPropertyInfo_ != nullptr) { + napi_env env = getPropertyInfo_->callBackInfo.env; + getPropertyInfo_->resultData[PARAM0] = BuildExecutorProperty( + env, result.result, result.remainTimes, result.freezingTime, static_cast(result.authSubType)); + uv_loop_s *loop(nullptr); + napi_status status = napi_get_uv_event_loop(getPropertyInfo_->callBackInfo.env, &loop); + if (status != napi_ok) { + HILOG_INFO("napi_get_uv_event_loop faild"); + } + if (loop == nullptr) { + return; + } + uv_work_t *work = new(std::nothrow) uv_work_t; + if (work == nullptr) { + return; + } + GetPropertyInfo *getPropertyInfo = new (std::nothrow) GetPropertyInfo(); + getPropertyInfo = getPropertyInfo_; + if (getPropertyInfo == nullptr) { + delete work; + work = nullptr; + return; + } + work->data = reinterpret_cast(getPropertyInfo); + OnExecutorPropertyInfoWrap(loop, work); + } else { + HILOG_ERROR("AuthApiCallback onExecutorPropertyInfo getPropertyInfo_ is nullptr"); } - 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; +} + +void AuthApiCallback::OnAcquireInfoWrap(uv_loop_s *loop, uv_work_t *work) +{ + HILOG_INFO("authFace : %{public}s, start.", __func__); + uv_queue_work(loop, work, [] (uv_work_t *work) { }, [] (uv_work_t *work, int status) { + AuthUserInfo *authUserInfo = reinterpret_cast(work->data); + if (authUserInfo == nullptr) { + delete work; + work = nullptr; + return; + } + napi_env env = authUserInfo->callBackInfo.env; + napi_value undefined; + napi_get_undefined(env, &undefined); + if (authUserInfo->onAcquireInfo == nullptr) { + delete work; + work = nullptr; + return; + } + napi_value callback; + napi_get_reference_value(env, authUserInfo->onAcquireInfo, &callback); + napi_value callResult = nullptr; + napi_call_function(env, undefined, callback, 3, authUserInfo->onAcquireInfoData, &callResult); + delete authUserInfo; + authUserInfo = nullptr; 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; + work = nullptr; + }); } -void AuthApiCallback::OnAuthResult(const int32_t result, const AuthResult extraInfo) +void AuthApiCallback::onAcquireInfo(const int32_t module, const uint32_t acquireInfo, const int32_t 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; + HILOG_INFO("authFace : %{public}s, start.", __func__); + std::lock_guard idmMutexGuard(mutex_); + if (userInfo_ != nullptr) { + napi_status status = napi_create_int32(userInfo_->callBackInfo.env, module, &userInfo_->onAcquireInfoData[PARAM0]); + if (status != napi_ok) { + HILOG_INFO("napi_create_int32 faild"); + } + napi_create_uint32(userInfo_->callBackInfo.env, acquireInfo, &userInfo_->onAcquireInfoData[PARAM1]); + if (status != napi_ok) { + HILOG_INFO("napi_create_uint32 faild"); + } + napi_create_int32(userInfo_->callBackInfo.env, extraInfo, &userInfo_->onAcquireInfoData[PARAM2]); + if (status != napi_ok) { + HILOG_INFO("napi_create_int32 faild"); + } + uv_loop_s *loop(nullptr); + napi_get_uv_event_loop(userInfo_->callBackInfo.env, &loop); + if (status != napi_ok) { + HILOG_INFO("napi_get_uv_event_loop faild"); + } + if (loop == nullptr) { return; + } + uv_work_t *work = new(std::nothrow) uv_work_t; + if (work == nullptr) { + return; + } + AuthUserInfo *userInfo = new (std::nothrow) AuthUserInfo(); + userInfo = userInfo_; + if (userInfo == nullptr) { + delete work; + work = nullptr; + return; + } + work->data = reinterpret_cast(userInfo); + OnAcquireInfoWrap(loop, work); + } else { + HILOG_INFO("AuthApiCallback onAcquireInfo userInfo_ is nullptr "); } - uv_work_t *work = new (std::nothrow) uv_work_t; - if (work == nullptr) { - HILOG_ERROR("work is null"); - delete authInfo_; - authInfo_ = nullptr; - return; + if (authInfo_ != nullptr) { + OnAcquireInfoAuthInfo(module, acquireInfo, extraInfo, authInfo_); + } else { + HILOG_INFO("AuthApiCallback onAcquireInfo authInfo_ is nullptr "); } - 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); + HILOG_INFO("AuthApiCallback onAcquireInfo end"); } void AuthApiCallback::onResult(const int32_t result, const AuthResult extraInfo) { + HILOG_INFO("authFace : %{public}s, start.", __func__); 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 { + userInfo_->result = result; + userInfo_->extraInfo = BuildOnResult(userInfo_->callBackInfo.env, extraInfo.remainTimes, extraInfo.freezingTime, extraInfo.token); + OnResultUserInfo(userInfo_); + }else { HILOG_ERROR("AuthApiCallback onResult userInfo_ is nullptr "); } if (authInfo_ != nullptr) { - OnAuthResult(result, extraInfo); - } else { - HILOG_ERROR("AuthApiCallback onResult authInfo_ is nullptr "); + authInfo_->result = result; + authInfo_->extraInfo = BuildOnResult(authInfo_->callBackInfo.env, extraInfo.remainTimes, extraInfo.freezingTime, extraInfo.token); + OnResultAuthInfo(authInfo_); + }else { + HILOG_ERROR("AuthApiCallback onResult userInfo_ is nullptr "); } HILOG_INFO("AuthApiCallback onResult end"); } -static void SetExecutorPropertyCallback(uv_work_t *work, int status) +void AuthApiCallback::OnSetExecutorPropertyWrap(uv_loop_s *loop, uv_work_t *work) { - 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; + HILOG_INFO("authFace : %{public}s, start.", __func__); + uv_queue_work(loop, work, [] (uv_work_t *work) { }, [] (uv_work_t *work, int status) { + SetPropertyInfo *setPropertyInfo = reinterpret_cast(work->data); + if (setPropertyInfo == nullptr) { + delete work; + work = nullptr; + return; + } + napi_env env = setPropertyInfo->callBackInfo.env; + napi_value undefined; + napi_get_undefined(env, &undefined); + if (setPropertyInfo->callBackInfo.callBack == nullptr && setPropertyInfo->callBackInfo.deferred != nullptr) { + napi_resolve_deferred(env, setPropertyInfo->callBackInfo.deferred, setPropertyInfo->result); + delete setPropertyInfo; + setPropertyInfo = nullptr; + delete work; + work = nullptr; + return; } + napi_value callback; 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; + napi_get_reference_value(env, setPropertyInfo->callBackInfo.callBack, &callback); + napi_value callResult = nullptr; + napi_call_function(env, undefined, callback, 1, resultData, &callResult); + delete setPropertyInfo; + setPropertyInfo = nullptr; + delete work; + work = nullptr; + }); } void AuthApiCallback::onSetExecutorProperty(const int32_t result) { + HILOG_INFO("authFace : %{public}s, start.", __func__); 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; + napi_env env = setPropertyInfo_->callBackInfo.env; + napi_status status = napi_create_int32(env, result, &setPropertyInfo_->result); + if (status != napi_ok) { + HILOG_INFO("napi_create_int32 faild"); + } + uv_loop_s *loop(nullptr); + napi_get_uv_event_loop(setPropertyInfo_->callBackInfo.env, &loop); + if (status != napi_ok) { + HILOG_INFO("napi_get_uv_event_loop faild"); + } + if (loop == nullptr) { + return; + } + uv_work_t *work = new(std::nothrow) uv_work_t; + if (work == nullptr) { + return; + } + SetPropertyInfo *setPropertyInfo = new (std::nothrow) SetPropertyInfo(); + setPropertyInfo = setPropertyInfo_; + if (setPropertyInfo == nullptr) { + delete work; + work = nullptr; + return; + } + work->data = reinterpret_cast(setPropertyInfo); + OnSetExecutorPropertyWrap(loop, work); + } else { + HILOG_ERROR("setPropertyInfo_ is nullptr "); } - setPropertyInfo_->setResult = result; - work->data = reinterpret_cast(setPropertyInfo_); - setPropertyInfo_ = nullptr; - uv_queue_work(loop, work, [] (uv_work_t *work) {}, SetExecutorPropertyCallback); + HILOG_INFO("onSetExecutorProperty end"); } } // namespace UserAuth } // namespace UserIAM diff --git a/interfaces/innerkits/napi/src/result_convert.cpp b/interfaces/innerkits/napi/src/result_convert.cpp index 77abb2f6d..57da4e41f 100644 --- a/interfaces/innerkits/napi/src/result_convert.cpp +++ b/interfaces/innerkits/napi/src/result_convert.cpp @@ -181,8 +181,8 @@ 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); + uint32_t result; + napi_status status = napi_get_value_uint32(env, value, &result); if (status != napi_ok) { return GET_VALUE_ERROR; } diff --git a/interfaces/innerkits/napi/src/user_auth_helper.cpp b/interfaces/innerkits/napi/src/user_auth_helper.cpp index e5982a1be..967155f27 100755 --- a/interfaces/innerkits/napi/src/user_auth_helper.cpp +++ b/interfaces/innerkits/napi/src/user_auth_helper.cpp @@ -30,20 +30,51 @@ namespace UserAuth { */ napi_value UserAuthServiceConstructor(napi_env env, napi_callback_info info) { + HILOG_INFO("%{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)); + 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) +{ + HILOG_INFO("%{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; } @@ -57,6 +88,7 @@ napi_value UserAuthServiceConstructor(napi_env env, napi_callback_info info) */ napi_value GetVersion(napi_env env, napi_callback_info info) { + HILOG_INFO("%{public}s, start.", __func__); napi_value thisVar = nullptr; size_t argcAsync = 0; napi_value args[ARGS_MAX_COUNT] = {nullptr}; @@ -76,6 +108,7 @@ napi_value GetVersion(napi_env env, napi_callback_info info) */ napi_value GetAvailabeStatus(napi_env env, napi_callback_info info) { + HILOG_INFO("%{public}s, start.", __func__); napi_value thisVar = nullptr; size_t argcAsync = 0; napi_value args[ARGS_MAX_COUNT] = {nullptr}; @@ -95,6 +128,7 @@ napi_value GetAvailabeStatus(napi_env env, napi_callback_info info) */ napi_value GetProperty(napi_env env, napi_callback_info info) { + HILOG_INFO("%{public}s, start.", __func__); napi_value thisVar = nullptr; size_t argcAsync = 0; napi_value args[ARGS_MAX_COUNT] = {nullptr}; @@ -114,6 +148,7 @@ napi_value GetProperty(napi_env env, napi_callback_info info) */ napi_value SetProperty(napi_env env, napi_callback_info info) { + HILOG_INFO("%{public}s, start.", __func__); napi_value thisVar = nullptr; size_t argcAsync = 0; napi_value args[ARGS_MAX_COUNT] = {nullptr}; @@ -134,6 +169,7 @@ napi_value SetProperty(napi_env env, napi_callback_info info) */ napi_value Auth(napi_env env, napi_callback_info info) { + HILOG_INFO("%{public}s, start.", __func__); napi_value thisVar = nullptr; size_t argcAsync = 0; napi_value args[ARGS_MAX_COUNT] = {nullptr}; @@ -154,6 +190,7 @@ napi_value Auth(napi_env env, napi_callback_info info) */ napi_value AuthUser(napi_env env, napi_callback_info info) { + HILOG_INFO("%{public}s, start.", __func__); napi_value thisVar = nullptr; size_t argcAsync = 0; napi_value args[ARGS_MAX_COUNT] = {nullptr}; @@ -173,6 +210,7 @@ napi_value AuthUser(napi_env env, napi_callback_info info) */ napi_value CancelAuth(napi_env env, napi_callback_info info) { + HILOG_INFO("%{public}s, start.", __func__); napi_value thisVar = nullptr; size_t argcAsync = 0; napi_value args[ARGS_MAX_COUNT] = {nullptr}; @@ -183,6 +221,70 @@ napi_value CancelAuth(napi_env env, napi_callback_info info) return userAuthImpl->CancelAuth(env, info); } +napi_value Execute(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s, start.", __func__); + 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)); + HILOG_INFO("UserAuthHelper, CancelAuth"); + return authenticatorImpl->Execute(env, info); +} + +napi_value Cancel(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s, start.", __func__); + 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)); + HILOG_INFO("UserAuthHelper, CancelAuth"); + return authenticatorImpl->Cancel(env, info); +} + +napi_value CheckAvailability(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s, start.", __func__); + 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)); + HILOG_INFO("UserAuthHelper, CancelAuth"); + return authenticatorImpl->CheckAvailability(env, info); +} + +napi_value On(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s, start.", __func__); + 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)); + HILOG_INFO("UserAuthHelper, CancelAuth"); + return authenticatorImpl->On(env, info); +} + +napi_value Off(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s, start.", __func__); + 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)); + HILOG_INFO("UserAuthHelper, CancelAuth"); + return authenticatorImpl->Off(env, info); +} /** * @brief Napi initialization * @@ -191,9 +293,11 @@ napi_value CancelAuth(napi_env env, napi_callback_info info) */ void Init(napi_env env, napi_value exports) { + HILOG_INFO("%{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) { @@ -203,6 +307,7 @@ void Init(napi_env env, napi_value exports) napi_value Constructor(napi_env env, napi_callback_info info) { + HILOG_INFO("%{public}s, start.", __func__); napi_value thisVar = nullptr; napi_value userAuth = nullptr; NAPI_CALL(env, napi_new_instance(env, GetCtor(env), 0, nullptr, &userAuth)); @@ -210,8 +315,19 @@ napi_value Constructor(napi_env env, napi_callback_info info) return userAuth; } +napi_value GetAuthenticator(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s, start.", __func__); + napi_value thisVar = nullptr; + napi_value authenticator = nullptr; + NAPI_CALL(env, napi_new_instance(env, GetCtor_API(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) { + HILOG_INFO("%{public}s, start.", __func__); napi_value cons = nullptr; napi_property_descriptor clzDes[] = { DECLARE_NAPI_FUNCTION("getVersion", UserAuth::GetVersion), @@ -223,7 +339,23 @@ napi_value GetCtor(napi_env env) 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)); + sizeof(clzDes) / sizeof(napi_property_descriptor), clzDes, &cons)); + return cons; +} + +napi_value GetCtor_API(napi_env env) +{ + HILOG_INFO("%{public}s, start.", __func__); + napi_value cons = nullptr; + napi_property_descriptor clzDes[] = { + DECLARE_NAPI_FUNCTION("execute", UserAuth::Execute), + 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 diff --git a/interfaces/innerkits/napi/src/user_auth_impl.cpp b/interfaces/innerkits/napi/src/user_auth_impl.cpp index 50bbc87a7..b75a3fdc4 100755 --- a/interfaces/innerkits/napi/src/user_auth_impl.cpp +++ b/interfaces/innerkits/napi/src/user_auth_impl.cpp @@ -33,6 +33,14 @@ UserAuthImpl::~UserAuthImpl() { } +AuthenticatorImpl::AuthenticatorImpl() +{ +} + +AuthenticatorImpl::~AuthenticatorImpl() +{ +} + napi_value UserAuthImpl::GetVersion(napi_env env, napi_callback_info info) { int32_t result = UserAuth::GetInstance().GetVersion(); @@ -72,35 +80,14 @@ napi_value UserAuthImpl::GetAvailabeStatus(napi_env env, napi_callback_info info napi_value UserAuthImpl::GetProperty(napi_env env, napi_callback_info info) { - AsyncHolder *asyncHolder = new (std::nothrow) AsyncHolder(); - if (asyncHolder == nullptr) { - HILOG_ERROR("%{public}s asyncHolder nullptr", __func__); - return nullptr; - } GetPropertyInfo *getPropertyInfo = new (std::nothrow) GetPropertyInfo(); - if (getPropertyInfo == nullptr) { - delete asyncHolder; - HILOG_ERROR("%{public}s getPropertyInfo nullptr", __func__); - return nullptr; - } getPropertyInfo->callBackInfo.env = env; - 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; + return GetPropertyWrap(env, info, getPropertyInfo); } -napi_value UserAuthImpl::GetPropertyWrap(napi_env env, napi_callback_info info, AsyncHolder *asyncHolder) +napi_value UserAuthImpl::GetPropertyWrap(napi_env env, napi_callback_info info, GetPropertyInfo *getPropertyInfo) { 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; @@ -117,16 +104,18 @@ napi_value UserAuthImpl::GetPropertyWrap(napi_env env, napi_callback_info info, NAPI_CALL(env, napi_create_reference(env, args[PARAM1], 1, &(getPropertyInfo->callBackInfo.callBack))); } } + if (authBuild.NapiTypeObject(env, args[PARAM0])) { Napi_GetPropertyRequest request = authBuild.GetPropertyRequestBuild(env, args[0]); getPropertyInfo->authType = request.authType_; getPropertyInfo->keys = request.keys_; } - napi_value ret = nullptr; + + napi_value ret = 0; if (argcAsync > argcPromise) { - ret = GetPropertyAsync(env, asyncHolder); + ret = GetPropertyAsync(env, getPropertyInfo); } else { - ret = GetPropertyPromise(env, asyncHolder); + ret = GetPropertyPromise(env, getPropertyInfo); } HILOG_INFO("%{public}s,end.", __func__); return ret; @@ -135,66 +124,66 @@ napi_value UserAuthImpl::GetPropertyWrap(napi_env env, napi_callback_info info, void UserAuthImpl::GetPropertyExecute(napi_env env, void *data) { HILOG_INFO("GetPropertyExecute, worker pool thread execute."); - 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; + GetPropertyInfo *getPropertyInfo = static_cast(data); + if (getPropertyInfo != nullptr) { + AuthType authTypeGet = AuthType(getPropertyInfo->authType); + + GetPropertyRequest request; + request.authType = authTypeGet; + request.keys = getPropertyInfo->keys; + HILOG_INFO("GetPropertyExecute start 1"); + AuthApiCallback *object = new AuthApiCallback(); + object->getPropertyInfo_ = getPropertyInfo; + std::shared_ptr callback; + callback.reset(object); + UserAuth::GetInstance().GetProperty(request, callback); + } else { + HILOG_ERROR("GetPropertyExecute, asynccallBackInfo == nullptr"); } - 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; + GetPropertyInfo *getPropertyInfo = static_cast(data); + napi_delete_async_work(env, getPropertyInfo->asyncWork); 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"); + GetPropertyInfo *getPropertyInfo = static_cast(data); + napi_delete_async_work(env, getPropertyInfo->asyncWork); + HILOG_INFO("GetPropertyPromiseExecuteDone, end"); } -napi_value UserAuthImpl::GetPropertyAsync(napi_env env, AsyncHolder *asyncHolder) +napi_value UserAuthImpl::GetPropertyAsync(napi_env env, GetPropertyInfo *getPropertyInfo) { HILOG_INFO("%{public}s, asyncCallback.", __func__); - napi_value result = nullptr; - NAPI_CALL(env, napi_get_null(env, &result)); - napi_value resourceName = nullptr; + if (getPropertyInfo == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = 0; 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)); + (void *)getPropertyInfo, &getPropertyInfo->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, getPropertyInfo->asyncWork)); + napi_value result = 0; + NAPI_CALL(env, napi_get_null(env, &result)); HILOG_INFO("%{public}s, asyncCallback end.", __func__); return result; } -napi_value UserAuthImpl::GetPropertyPromise(napi_env env, AsyncHolder *asyncHolder) +napi_value UserAuthImpl::GetPropertyPromise(napi_env env, GetPropertyInfo *getPropertyInfo) { HILOG_INFO("%{public}s, promise.", __func__); - GetPropertyInfo *getPropertyInfo = reinterpret_cast(asyncHolder->data); + if (getPropertyInfo == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } napi_value resourceName = 0; NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); napi_deferred deferred; @@ -202,44 +191,24 @@ napi_value UserAuthImpl::GetPropertyPromise(napi_env env, AsyncHolder *asyncHold 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)); + (void *)getPropertyInfo, &getPropertyInfo->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, getPropertyInfo->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; + return SetPropertyWrap(env, info, setPropertyInfo); } -napi_value UserAuthImpl::SetPropertyWrap(napi_env env, napi_callback_info info, AsyncHolder *asyncHolder) +napi_value UserAuthImpl::SetPropertyWrap(napi_env env, napi_callback_info info, SetPropertyInfo *setPropertyInfo) { 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; @@ -266,9 +235,9 @@ napi_value UserAuthImpl::SetPropertyWrap(napi_env env, napi_callback_info info, napi_value ret = 0; if (argcAsync > argcPromise) { - ret = SetPropertyAsync(env, asyncHolder); + ret = SetPropertyAsync(env, setPropertyInfo); } else { - ret = SetPropertyPromise(env, asyncHolder); + ret = SetPropertyPromise(env, setPropertyInfo); } HILOG_INFO("%{public}s,end.", __func__); return ret; @@ -277,83 +246,86 @@ napi_value UserAuthImpl::SetPropertyWrap(napi_env env, napi_callback_info info, void UserAuthImpl::SetPropertyExecute(napi_env env, void *data) { HILOG_INFO("setPropertyExecute, worker pool thread execute."); - AsyncHolder *asyncHolder = reinterpret_cast(data); - if (asyncHolder == nullptr) { - HILOG_ERROR("SetPropertyExecute, asyncHolder == nullptr"); - return; + SetPropertyInfo *setPropertyInfo = static_cast(data); + if (setPropertyInfo != nullptr) { + AuthType authTypeGet = AuthType(setPropertyInfo->authType); + + SetPropertyRequest request; + request.authType = authTypeGet; + request.key = SetPropertyType(setPropertyInfo->key); + request.setInfo = setPropertyInfo->setInfo; + AuthApiCallback *object = new AuthApiCallback(); + object->setPropertyInfo_ = setPropertyInfo; + std::shared_ptr callback; + callback.reset(object); + UserAuth::GetInstance().SetProperty(request, callback); + } else { + HILOG_ERROR("setPropertyExecute, asynccallBackInfo == nullptr"); } - 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; + SetPropertyInfo *setPropertyInfo = static_cast(data); + napi_delete_async_work(env, setPropertyInfo->asyncWork); 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; + SetPropertyInfo *setPropertyInfo = static_cast(data); + napi_delete_async_work(env, setPropertyInfo->asyncWork); HILOG_INFO("SetPropertyAsyncExecuteDone, end"); } -napi_value UserAuthImpl::SetPropertyAsync(napi_env env, AsyncHolder *asyncHolder) +napi_value UserAuthImpl::SetPropertyAsync(napi_env env, SetPropertyInfo *setPropertyInfo) { HILOG_INFO("%{public}s, asyncCallback.", __func__); - napi_value result = nullptr; - NAPI_CALL(env, napi_get_null(env, &result)); - napi_value resourceName = nullptr; + if (setPropertyInfo == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = 0; 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)); + (void *)setPropertyInfo, &setPropertyInfo->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, setPropertyInfo->asyncWork)); + napi_value result = 0; + NAPI_CALL(env, napi_get_null(env, &result)); HILOG_INFO("%{public}s, asyncCallback end.", __func__); return result; } -napi_value UserAuthImpl::SetPropertyPromise(napi_env env, AsyncHolder *asyncHolder) +napi_value UserAuthImpl::SetPropertyPromise(napi_env env, SetPropertyInfo *setPropertyInfo) { HILOG_INFO("%{public}s, promise.", __func__); - SetPropertyInfo *setPropertyInfo = reinterpret_cast(asyncHolder->data); + if (setPropertyInfo == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } 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_value promise = 0; 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)); + + NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName, SetPropertyExecute, SetPropertyPromiseExecuteDone, + (void *)setPropertyInfo, &setPropertyInfo->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, setPropertyInfo->asyncWork)); HILOG_INFO("%{public}s, promise end.", __func__); return promise; } -napi_value UserAuthImpl::BuildAuthInfo(napi_env env, AuthInfo *authInfo) +napi_value UserAuthImpl::Auth(napi_env env, napi_callback_info info) { - napi_value result = nullptr; - NAPI_CALL(env, napi_get_null(env, &result)); + HILOG_INFO("%{public}s, start", __func__); + AuthInfo *authInfo = new (std::nothrow) AuthInfo(); + authInfo->info = info; authInfo->callBackInfo.env = env; size_t argc = ARGS_MAX_COUNT; napi_value argv[ARGS_MAX_COUNT] = {nullptr}; @@ -383,30 +355,16 @@ napi_value UserAuthImpl::BuildAuthInfo(napi_env env, AuthInfo *authInfo) 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__); + if (authInfo == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } AuthApiCallback *object = new AuthApiCallback(); object->authInfo_ = authInfo; object->userInfo_ = nullptr; @@ -424,25 +382,8 @@ 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)); @@ -474,12 +415,16 @@ napi_value UserAuthImpl::BuildAuthUserInfo(napi_env env, AuthUserInfo *userInfo) 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; + return AuthUserWrap(env, userInfo); } napi_value UserAuthImpl::AuthUserWrap(napi_env env, AuthUserInfo *userInfo) { HILOG_INFO("%{public}s, start.", __func__); + if (userInfo == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } AuthApiCallback *object = new AuthApiCallback(); object->authInfo_ = nullptr; object->userInfo_ = userInfo; @@ -495,10 +440,10 @@ napi_value UserAuthImpl::AuthUserWrap(napi_env env, AuthUserInfo *userInfo) napi_value UserAuthImpl::CancelAuth(napi_env env, napi_callback_info info) { + HILOG_INFO("%{public}s, start.", __func__); 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) { @@ -510,6 +455,235 @@ napi_value UserAuthImpl::CancelAuth(napi_env env, napi_callback_info info) NAPI_CALL(env, napi_create_int32(env, result, &key)); return key; } + +napi_value AuthenticatorImpl::Execute(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s, start.", __func__); + ExecuteInfo *executeInfo = new (std::nothrow) ExecuteInfo(); + executeInfo->env = env; + return ExecuteWrap(env, info, executeInfo); +} + +napi_value AuthenticatorImpl::ExecuteWrap(napi_env env, napi_callback_info info, ExecuteInfo* executeInfo) +{ + HILOG_INFO("%{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); + } +} + +napi_value AuthenticatorImpl::ExecuteAsyncallback(napi_env env, napi_value* argv, ExecuteInfo* executeInfo) +{ + HILOG_INFO("%{public}s, start.", __func__); + 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)); + executeInfo->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)); + executeInfo->level = type1; + delete[] type1; + } + } + 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, + [](napi_env env, void *data) { + ExecuteInfo *executeInfo = (ExecuteInfo *)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) { + ExecuteInfo *executeInfo = (ExecuteInfo *)data; + napi_delete_async_work(env, executeInfo->asyncWork); + }, + (void *)executeInfo, &executeInfo->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, executeInfo->asyncWork)); + napi_value result = RESULTVAR; + NAPI_CALL(env, napi_get_null(env, &result)); + return result; +} + +napi_value AuthenticatorImpl::ExecutePromise(napi_env env, napi_value* argv, ExecuteInfo* executeInfo) +{ + HILOG_INFO("%{public}s, start.", __func__); + 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)); + executeInfo->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)); + executeInfo->level = type1; + delete[] type1; + } + } + 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, + [](napi_env env, void *data) { + ExecuteInfo *executeInfo = (ExecuteInfo *)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) { + ExecuteInfo *executeInfo = (ExecuteInfo *)data; + napi_delete_async_work(env, executeInfo->asyncWork); + }, + (void *)executeInfo, &executeInfo->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, executeInfo->asyncWork)); + return executeInfo->promise; +} + +napi_value AuthenticatorImpl::Cancel(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{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) +{ + HILOG_INFO("%{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) +{ + HILOG_INFO("%{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) +{ + HILOG_INFO("%{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; +} } // namespace UserAuth } // namespace UserIAM } // namespace OHOS diff --git a/interfaces/innerkits/native/BUILD.gn b/interfaces/innerkits/native/BUILD.gn index e07ff2c54..bbdb54425 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/native/include/iuserauth_API7_callback.h b/interfaces/innerkits/native/include/iuserauth_API7_callback.h new file mode 100644 index 000000000..5eba98658 --- /dev/null +++ b/interfaces/innerkits/native/include/iuserauth_API7_callback.h @@ -0,0 +1,43 @@ +/* + * 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; + void onExecutorPropertyInfo(const UserAuth::ExecutorProperty result) override; + void onSetExecutorProperty(const int32_t result) 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 be7198471..4c875e446 100755 --- a/interfaces/innerkits/native/include/user_auth.h +++ b/interfaces/innerkits/native/include/user_auth.h @@ -44,9 +44,9 @@ public: private: class UserAuthDeathRecipient : public IRemoteObject::DeathRecipient { public: - UserAuthDeathRecipient() = default; - ~UserAuthDeathRecipient() = default; - void OnRemoteDied(const wptr& remote) override; + UserAuthDeathRecipient() = default; + ~UserAuthDeathRecipient() = default; + void OnRemoteDied(const wptr& remote) override; private: DISALLOW_COPY_AND_MOVE(UserAuthDeathRecipient); diff --git a/interfaces/innerkits/native/include/userauth_API7.h b/interfaces/innerkits/native/include/userauth_API7.h new file mode 100644 index 000000000..b3c61b855 --- /dev/null +++ b/interfaces/innerkits/native/include/userauth_API7.h @@ -0,0 +1,40 @@ +/* + * 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/interfaces/innerkits/native/include/userauth_API7_Datamgr.h b/interfaces/innerkits/native/include/userauth_API7_Datamgr.h new file mode 100644 index 000000000..084023dcf --- /dev/null +++ b/interfaces/innerkits/native/include/userauth_API7_Datamgr.h @@ -0,0 +1,42 @@ +/* + * 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 +#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_callback.h b/interfaces/innerkits/native/include/userauth_API7_callback.h new file mode 100644 index 000000000..f58696239 --- /dev/null +++ b/interfaces/innerkits/native/include/userauth_API7_callback.h @@ -0,0 +1,32 @@ +/* + * 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_CALLBACK_H +#define USERAUTH_API7_CALLBACK_H + +#include "userauth_API7_info.h" + +namespace OHOS { +namespace UserIAM { +namespace API7UserAuth { +class UserAuthAPI7Callback { +public: + virtual void API7OnResult(const int32_t result) = 0; + virtual void TipCallback(const Tip result) = 0; +}; +} // namespace API7UserAuth +} // namespace UserIAM +} // namespace OHOS +#endif // USERAUTH_API7_CALLBACK_H \ No newline at end of file diff --git a/interfaces/innerkits/native/include/userauth_API7_hilog_wrapper.h b/interfaces/innerkits/native/include/userauth_API7_hilog_wrapper.h new file mode 100644 index 000000000..18efe06ec --- /dev/null +++ b/interfaces/innerkits/native/include/userauth_API7_hilog_wrapper.h @@ -0,0 +1,72 @@ +/* + * 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_HILOG_WRAPPER_H +#define USERAUTH_API7_HILOG_WRAPPER_H + +#define CONFIG_HILOG +#ifdef CONFIG_HILOG + +#include "hilog/log.h" + +namespace OHOS { +namespace UserIAM { +namespace API7UserAuth { +#define filename__ (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__) +#define formated__(fmt, ...) "[%{public}s] %{public}s# " fmt, filename__, __FUNCTION__, ##__VA_ARGS__ + + +#ifdef USERAUTH_API7_HILOGE +#undef USERAUTH_API7_HILOGE +#endif + +#ifdef USERAUTH_API7_HILOGD +#undef USERAUTH_API7_HILOGD +#endif + +// param of log interface, such as USERAUTH_API7_HILOGF. +enum UserAuthAPI7SubModule { + MODULE_API7 = 0, + USERAUTHAPI7_MODULE_BUTT, +}; + +// 0xD002900: subsystem:distributeddatamgr module:distributedgallery, 8 bits reserved. +static constexpr unsigned int BASE_USERAUTH_API7_DOMAIN_ID = 0xD002910; + +enum UserAuthAPI7DomainId { + USERAUTH_API7_DOMAIN = BASE_USERAUTH_API7_DOMAIN_ID + MODULE_API7, + USERAUTH_API7_BUTT, +}; + +static constexpr OHOS::HiviewDFX::HiLogLabel USERAUTH_API7_LABEL[USERAUTHAPI7_MODULE_BUTT] = { + {LOG_CORE, USERAUTH_API7_DOMAIN, "UserAUTH_API7"}, +}; + +// In order to improve performance, do not check the module range. +// Besides, make sure module is less than USERAUTH_API7_MODULE_BUTT. +#define USERAUTH_API7_HILOGE(module, ...) (void)OHOS::HiviewDFX::HiLog::Error(USERAUTH_API7_LABEL[module], formated__(__VA_ARGS__)) +#define USERAUTH_API7_HILOGD(module, ...) (void)OHOS::HiviewDFX::HiLog::Debug(USERAUTH_API7_LABEL[module], formated__(__VA_ARGS__)) +} // namespace UserAUTH_API7 +} // namespace UserIAM +} // namespace OHOS + +#else + +#define USERAUTH_API7_HILOGE(...) +#define USERAUTH_API7_HILOGD(...) + +#endif // CONFIG_HILOG + +#endif // USERAUTH_API7_HILOG_WRAPPER_H diff --git a/interfaces/innerkits/native/include/userauth_API7_info.h b/interfaces/innerkits/native/include/userauth_API7_info.h new file mode 100644 index 000000000..445ce9bdb --- /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_info.h b/interfaces/innerkits/native/include/userauth_info.h index 4284e9b69..7a60d349d 100755 --- a/interfaces/innerkits/native/include/userauth_info.h +++ b/interfaces/innerkits/native/include/userauth_info.h @@ -23,12 +23,12 @@ namespace OHOS { namespace UserIAM { namespace UserAuth { -// 认证的类型(口令,人脸) +// Type of authentication (password, face) enum AuthType: uint32_t { PIN = 1, FACE = 2, }; -// 认证子类型(2D人脸,3D人脸...) +// Authentication subtype (2D face, 3D face...) enum AuthSubType: uint64_t { /** * Authentication sub type six number pin. @@ -51,7 +51,7 @@ enum AuthSubType: uint64_t { */ FACE_3D = 20001 }; -// 认证结果可信等级 +// Certification result confidence level enum AuthTurstLevel: uint32_t { // level 1-4 ATL1 = 10000, @@ -59,48 +59,50 @@ enum AuthTurstLevel: uint32_t { ATL3 = 30000, ATL4 = 40000 }; -// 执行器属性列表 +// Actuator get property list enum GetPropertyType: uint32_t { - // 认证子类型(此时认证类型已确认) + // Authentication subtype (at this point the authentication type has been confirmed) AUTH_SUB_TYPE = 1, - // 剩余认证次数 + // Remaining authentication times REMAIN_TIMES = 2, - // 冻结时间 + // Freeze time FREEZING_TIME = 3, }; -// 获得属性请求 +// get attribute request struct GetPropertyRequest { AuthType authType; // GetPropertyType std::vector keys; }; -// 执行器属性 +// Actuator properties struct ExecutorProperty { int32_t result; AuthSubType authSubType; uint32_t remainTimes; uint32_t freezingTime; }; -// 执行器属性列表 +// Actuator property mode list enum AuthPropertyMode: uint32_t { - PROPERMODE_DELETE = 0, - PROPERMODE_GET = 1, - PROPERMODE_SET = 2, - PROPERMODE_FREEZE = 3, - PROPERMODE_UNFREEZE = 4, + PROPERMODE_DELETE = 0, + PROPERMODE_GET = 1, + PROPERMODE_SET = 2, + PROPERMODE_FREEZE = 3, + PROPERMODE_UNFREEZE = 4, + PROPERMODE_INIT_ALGORITHM = 5, + PROPERMODE_RELEASE_ALGORITHM = 6, }; -// 执行器属性列表 +// Actuator property list enum SetPropertyType: uint32_t { - INIT_ALGORITHM = 1, - FREEZE_TEMPLATE = 2, - THAW_TEMPLATE = 3, + INIT_ALGORITHM = 1, + FREEZE_TEMPLATE = 2, + THAW_TEMPLATE = 3, }; struct SetPropertyRequest { AuthType authType; SetPropertyType key; std::vector setInfo; }; -// 认证结果 +// Authentication Result struct AuthResult { std::vector token; uint32_t remainTimes; @@ -121,7 +123,7 @@ struct FreezInfo { AuthType authType; }; -// 结果码 +// Result Code enum ResultCode: int32_t { /** * Indicates that authentication is success or ability is supported. diff --git a/services/include/userauth_adapter.h b/services/include/userauth_adapter.h index 3f2b5a61f..3476d81ff 100644 --- a/services/include/userauth_adapter.h +++ b/services/include/userauth_adapter.h @@ -45,20 +45,24 @@ public: void CoauthSetPropAuthInfo(int32_t resultCode, uint64_t callerUID, std::string pkgName, UserAuthToken authToken, SetPropertyRequest requset, sptr& callback); - /* Set the executor authentication properties */ + /* Set the executor authentication properties for freez or unfreez*/ void SetPropAuthInfo(uint64_t callerUID, std::string pkgName, int32_t resultCode, UserAuthToken authToken, SetPropertyRequest requset, std::vector templateIds, sptr& callback); + /* get the executor authentication properties for Coauth */ void GetPropAuthInfoCoauth(uint64_t callerUID, std::string pkgName, int32_t resultCode, UserAuthToken authToken, GetPropertyRequest requset, sptr& callback); + /* get userID */ int32_t GetUserID(int32_t &userID); int32_t GenerateSolution(AuthSolution param, std::vector &sessionIds); int32_t RequestAuthResult(uint64_t contextId, std::vector scheduleToken, UserAuthToken &authToken, std::vector &sessionIds); int32_t CancelContext(uint64_t contextId, std::vector &sessionIds); int32_t Cancel(uint64_t sessionId); + /* get the executor authentication properties */ int32_t GetExecutorProp(uint64_t callerUID, std::string pkgName, uint64_t templateId, GetPropertyRequest requset, ExecutorProperty &result); + /* Set the executor authentication properties */ int32_t SetExecutorProp(uint64_t callerUID, std::string pkgName, SetPropertyRequest requset, sptr& callback); int32_t GetVersion(); diff --git a/services/include/userauth_async_proxy.h b/services/include/userauth_async_proxy.h index f34f67f50..7bf1346cd 100755 --- a/services/include/userauth_async_proxy.h +++ b/services/include/userauth_async_proxy.h @@ -37,6 +37,8 @@ public: private: bool SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply); + void WriteFailResult(const ExecutorProperty &result, ExecutorProperty &resultFail, + MessageParcel &data); private: static inline BrokerDelegator delegator_; diff --git a/services/include/userauth_datamgr.h b/services/include/userauth_datamgr.h index 73e60ae37..6c6094ff9 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/src/userauth_adapter.cpp b/services/src/userauth_adapter.cpp index 4dff5c26e..d02b3c3af 100644 --- a/services/src/userauth_adapter.cpp +++ b/services/src/userauth_adapter.cpp @@ -26,8 +26,7 @@ namespace OHOS { namespace UserIAM { namespace UserAuth { -const int32_t cUserId = 0; - +const int USERID_BUFF_SIZE = 10; UserAuthAdapter &UserAuthAdapter::GetInstance() { static UserAuthAdapter instance; @@ -94,7 +93,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); @@ -170,8 +169,35 @@ void UserAuthAdapter::CoauthSetPropAuthInfo(int32_t resultCode, uint64_t callerU int32_t UserAuthAdapter::GetUserID(int32_t &userID) { USERAUTH_HILOGD(MODULE_SERVICE, "UserAuth GetUserID is start!"); - // 打桩 IAMTA - userID = cUserId; + + bool once = true; + int32_t ret = SUCCESS; + uint8_t buff[USERID_BUFF_SIZE]; + FILE* fp = fopen("/data/useriam/userId", "rb"); + if (fp == nullptr) { + USERAUTH_HILOGE(MODULE_SERVICE, "## fopen failed!"); + once = false; + return FAIL; + } + + if (once) { + ret = fread(buff, 1, USERID_BUFF_SIZE, fp); + if (ret > 0) { + USERAUTH_HILOGD(MODULE_SERVICE, "## reading success: %{public}s", buff); + } else { + USERAUTH_HILOGE(MODULE_SERVICE, "## reading failed: %{public}d", ret); + fclose(fp); + return FAIL; + } + } + userID = atoi((char*)buff); + USERAUTH_HILOGD(MODULE_SERVICE, "## test userId: %{public}d", userID); + + ret = fclose(fp); + if (ret != SUCCESS) { + USERAUTH_HILOGE(MODULE_SERVICE, "## fclose failed: %{public}d", ret); + } + return SUCCESS; } @@ -328,10 +354,13 @@ int32_t UserAuthAdapter::SetExecutorProp(uint64_t callerUID, std::string pkgName sptr& callback) { USERAUTH_HILOGD(MODULE_SERVICE, "UserAuth SetExecutorProp is start!"); - + 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); + 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 +384,7 @@ int32_t UserAuthAdapter::SetExecutorProp(uint64_t callerUID, std::string pkgName USERAUTH_HILOGE(MODULE_SERVICE, "UserAuth SetUint32Value AUTH_TYPE ERROR!"); return ret; } - ret = pAuthAttributes.SetUint8ArrayValue(AUTH_SIGNATURE, requset.setInfo); + ret = pAuthAttributes.SetUint8ArrayValue(ALGORITHM_INFO, requset.setInfo); if (ret != SUCCESS) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuth SetUint8ArrayValue init ERROR!"); return ret; diff --git a/services/src/userauth_async_proxy.cpp b/services/src/userauth_async_proxy.cpp index d12862b1d..da472454d 100755 --- a/services/src/userauth_async_proxy.cpp +++ b/services/src/userauth_async_proxy.cpp @@ -90,32 +90,61 @@ void UserAuthAsyncProxy::onExecutorPropertyInfo(const ExecutorProperty result) USERAUTH_HILOGD(MODULE_SERVICE, "userauthAsyncProxy onExecutorPropertyInfo enter"); MessageParcel data; MessageParcel reply; + ExecutorProperty resultFail; if (!data.WriteInterfaceToken(UserAuthAsyncProxy::GetDescriptor())) { USERAUTH_HILOGI(MODULE_SERVICE, "userauth write descriptor failed!"); return ; } + if (result.result != SUCCESS) { + WriteFailResult(result, resultFail, data); + } else { + if (!data.WriteInt32(result.result)) { + USERAUTH_HILOGE(MODULE_SERVICE, "failed to WriteInt32(result.result)."); + return; + } + if (!data.WriteUint64(result.authSubType)) { + USERAUTH_HILOGE(MODULE_SERVICE, "failed to WriteUint64(result.authSubType)."); + return; + } + if (!data.WriteUint32(result.remainTimes)) { + USERAUTH_HILOGE(MODULE_SERVICE, "failed to WriteUint32(result.remainTimes)."); + return; + } + if (!data.WriteUint32(result.freezingTime)) { + USERAUTH_HILOGE(MODULE_SERVICE, "failed to WriteUint32(result.freezingTime)."); + return; + } + } + bool ret = SendRequest(IUserAuth::USER_AUTH_GETEXPORP, data, reply); + if (ret) { + int32_t result = reply.ReadInt32(); + USERAUTH_HILOGE(MODULE_SERVICE, "userauth result = %{public}d", result); + } + return; +} + +void UserAuthAsyncProxy::WriteFailResult(const ExecutorProperty &result, ExecutorProperty &resultFail, + MessageParcel &data) +{ + resultFail.authSubType = PIN_SIX; + resultFail.freezingTime = 0; + resultFail.remainTimes = 0; if (!data.WriteInt32(result.result)) { USERAUTH_HILOGE(MODULE_SERVICE, "failed to WriteInt32(result.result)."); return; } - if (!data.WriteUint64(result.authSubType)) { - USERAUTH_HILOGE(MODULE_SERVICE, "failed to WriteUint64(result.authSubType)."); + if (!data.WriteUint64(resultFail.authSubType)) { + USERAUTH_HILOGE(MODULE_SERVICE, "failed to WriteUint64(resultFail.authSubType)."); return; } - if (!data.WriteUint32(result.remainTimes)) { - USERAUTH_HILOGE(MODULE_SERVICE, "failed to WriteUint32(result.remainTimes)."); + if (!data.WriteUint32(resultFail.remainTimes)) { + USERAUTH_HILOGE(MODULE_SERVICE, "failed to WriteUint32(resultFail.remainTimes)."); return; } - if (!data.WriteUint32(result.freezingTime)) { - USERAUTH_HILOGE(MODULE_SERVICE, "failed to WriteUint32(result.freezingTime)."); + if (!data.WriteUint32(resultFail.freezingTime)) { + USERAUTH_HILOGE(MODULE_SERVICE, "failed to WriteUint32(resultFail.freezingTime)."); return; } - bool ret = SendRequest(IUserAuth::USER_AUTH_GETEXPORP, data, reply); - if (ret) { - int32_t result = reply.ReadInt32(); - USERAUTH_HILOGE(MODULE_SERVICE, "userauth result = %{public}d", result); - } - return; } void UserAuthAsyncProxy::onSetExecutorProperty(const int32_t result) diff --git a/services/src/userauth_excallback_impl.cpp b/services/src/userauth_excallback_impl.cpp index 695f81c01..6f15a822c 100644 --- a/services/src/userauth_excallback_impl.cpp +++ b/services/src/userauth_excallback_impl.cpp @@ -278,7 +278,7 @@ UserAuthCallbackImplIDMGetPorp::UserAuthCallbackImplIDMGetPorp(const sptr& info) { USERAUTH_HILOGD(MODULE_SERVICE, "UserAuthCallbackImplIDMGetPorp OnGetInfo enter"); - ExecutorProperty executorProperty = {}; + ExecutorProperty executorProperty; if (info.size() == 0) { executorProperty.result = GENERAL_ERROR; callback_->onExecutorPropertyInfo(executorProperty); -- Gitee