diff --git a/frameworks/kitsimpl/src/iuserauth_api7_callback.cpp b/frameworks/kitsimpl/src/iuserauth_api7_callback.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7b15f32269fef01bf70e4afed34bef0adda8813c --- /dev/null +++ b/frameworks/kitsimpl/src/iuserauth_api7_callback.cpp @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include + +#include "userauth_api7_hilog_wrapper.h" +#include "userauth_api7_datamgr.h" +#include "userauth_api7_info.h" +#include "iuserauth_api7_callback.h" +namespace OHOS { +namespace UserIAM { +namespace API7UserAuth { +std::map g_convertResult = { +{UserAuth::SUCCESS, API7UserAuth::SUCCESS}, +{UserAuth::FAIL, API7UserAuth::GENERAL_ERROR}, +{UserAuth::GENERAL_ERROR, API7UserAuth::GENERAL_ERROR}, +{UserAuth::CANCELED, API7UserAuth::CANCELED}, +{UserAuth::TIMEOUT, API7UserAuth::TIMEOUT}, +{UserAuth::TYPE_NOT_SUPPORT, API7UserAuth::NO_SUPPORT}, +{UserAuth::TRUST_LEVEL_NOT_SUPPORT, API7UserAuth::NO_SUPPORT}, +{UserAuth::BUSY, API7UserAuth::BUSY}, +{UserAuth::INVALID_PARAMETERS, API7UserAuth::INVALID_PARAMETERS}, +{UserAuth::LOCKED, API7UserAuth::LOCKED}, +{UserAuth::NOT_ENROLLED, API7UserAuth::NOT_ENROLLED}, +{UserAuth::IPC_ERROR, API7UserAuth::GENERAL_ERROR}, +{UserAuth::INVALID_CONTEXTID, API7UserAuth::GENERAL_ERROR}, +{UserAuth::E_WRITE_PARCEL_ERROR, API7UserAuth::GENERAL_ERROR}, +{UserAuth::E_READ_PARCEL_ERROR, API7UserAuth::GENERAL_ERROR}, +{UserAuth::E_GET_POWER_SERVICE_FAILED, API7UserAuth::GENERAL_ERROR}, +{UserAuth::E_RET_UNDONE, API7UserAuth::GENERAL_ERROR}, +{UserAuth::E_RET_NOSERVER, API7UserAuth::GENERAL_ERROR}, +}; + +std::map g_convertTipInfo = { +{ FACE_AUTH_TIP_TOO_BRIGHT, "the obtained facial image is too bright due to high illumination" }, +{ FACE_AUTH_TIP_TOO_DARK, "the obtained facial image is too dark due to low illumination" }, +{ FACE_AUTH_TIP_TOO_CLOSE, "the face is too close to the device" }, +{ FACE_AUTH_TIP_TOO_FAR, "the face is too far away from the device" }, +{ FACE_AUTH_TIP_TOO_HIGH, "the device is too high, and that only the upper part of the face is captured" }, +{ FACE_AUTH_TIP_TOO_LOW, "the device is too low, and that only the lower part of the face is captured" }, +{ FACE_AUTH_TIP_TOO_RIGHT, + "the device is deviated to the right, and that only the right part of the face is captured" }, +{ FACE_AUTH_TIP_TOO_LEFT, "the device is deviated to the left, and that only the left part of the face is captured" }, +{ FACE_AUTH_TIP_TOO_MUCH_MOTION, "the face moves too fast during facial information collection" }, +{ FACE_AUTH_TIP_POOR_GAZE, "the face is not facing the device" }, +{ FACE_AUTH_TIP_NOT_DETECTED, "no face is detected" }, +}; + +IUserAuthAPI7Callback::IUserAuthAPI7Callback(std::shared_ptr& impl) +{ + callback_ = impl; +} + +void IUserAuthAPI7Callback::onAcquireInfo(const int32_t module, const uint32_t acquireInfo, const int32_t extraInfo) +{ + 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 0000000000000000000000000000000000000000..4375deeb4e619941da4c4a60a812742a158d3296 --- /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_hilog_wrapper.h" +#include "userauth_api7_datamgr.h" +#include "iuserauth_api7_callback.h" +#include "userauth_api7.h" + +namespace OHOS { +namespace UserIAM { +namespace API7UserAuth { +std::map g_convertAuthType = { +{ "FACE_ONLY", UserAuth::FACE }, +}; +std::map g_convertAuthTurstLevel = { +{ "S1", UserAuth::ATL1 }, +{ "S2", UserAuth::ATL2 }, +{ "S3", UserAuth::ATL3 }, +{ "S4", UserAuth::ATL4 }, +}; + +UserAuthAPI7 &UserAuthAPI7::GetAuthenticator() +{ + static UserAuthAPI7 instance; + return instance; +} + +void UserAuthAPI7::Execute(std::string authtype, std::string trustLevel, std::shared_ptr callback) +{ + 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 0000000000000000000000000000000000000000..7a31984a07c4879e12dc2edd39306a015b123fcc --- /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/frameworks/kitsimpl/src/userauth_async_stub.cpp b/frameworks/kitsimpl/src/userauth_async_stub.cpp index 3e2fbaf654f519d36ca3f5813d38ccf0382493c9..e3608e808a74dc1b892b29648768c48680e6c50d 100644 --- a/frameworks/kitsimpl/src/userauth_async_stub.cpp +++ b/frameworks/kitsimpl/src/userauth_async_stub.cpp @@ -189,28 +189,30 @@ int32_t UserAuthAsyncStub::onSetExecutorPropertyStub(MessageParcel& data, Messag void UserAuthAsyncStub::onAcquireInfo(const int32_t module, const uint32_t acquireInfo, const int32_t extraInfo) { - USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAsyncStub onAcquireInfo enter"); - USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAsyncStub module:%{public}d", module); - USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAsyncStub acquireInfo:%{public}d", acquireInfo); - USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAsyncStub extraInfo:%{public}d", extraInfo); + USERAUTH_HILOGD(MODULE_INNERKIT, "UserAuthAsyncStub onAcquireInfo enter"); + if (callback_ == nullptr) { USERAUTH_HILOGE(MODULE_INNERKIT, "userauthAsyncStub onAcquireInfo callback_ is Null"); return ; } + + USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAsyncStub module:%{public}d, acquireInfo:%{public}d", + module, acquireInfo); + callback_->onAcquireInfo(module, acquireInfo, extraInfo); } void UserAuthAsyncStub::onResult(const int32_t result, const AuthResult extraInfot) { - USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAsyncStub onResult enter"); - USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAsyncStub result:%{public}d", result); - USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAsyncStub remainTimes:%{public}d", extraInfot.remainTimes); - USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAsyncStub freezingTime:%{public}d", extraInfot.freezingTime); + USERAUTH_HILOGD(MODULE_INNERKIT, "UserAuthAsyncStub onResult enter"); if (callback_ == nullptr) { USERAUTH_HILOGE(MODULE_INNERKIT, "userauthAsyncStub onResult callback_ is Null"); return ; } + + USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAsyncStub result:%{public}d, reTimes:%{public}d, freTime:%{public}d", + result, extraInfot.remainTimes, extraInfot.freezingTime); callback_->onResult(result, extraInfot); } @@ -222,10 +224,10 @@ void UserAuthAsyncStub::onExecutorPropertyInfo(const ExecutorProperty result) USERAUTH_HILOGE(MODULE_INNERKIT, "UserAuthAsyncStub onExecutorPropertyInfo callback_ is Null"); return ; } - USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAsyncStub result:%{public}d", result.result); - USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAsyncStub authSubType:%{public}llu", result.authSubType); - USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAsyncStub remainTimes:%{public}d", result.freezingTime); - USERAUTH_HILOGD(MODULE_INNERKIT, "userauthAsyncStub freezingTime:%{public}d", result.freezingTime); + + USERAUTH_HILOGD(MODULE_INNERKIT, + "userauthAsyncStub result:%{public}d, SubTyp:%{public}llu, reTimes:%{public}d, freTime:%{public}d", + result.result, result.authSubType, result.freezingTime, result.freezingTime); callback_->onExecutorPropertyInfo(result); } diff --git a/interfaces/innerkits/native/BUILD.gn b/interfaces/innerkits/native/BUILD.gn index e07ff2c54359d07e3119e446b0f339ce76e02bef..de98274ee294ed985144eeb1f272bb807f34fd7c 100755 --- a/interfaces/innerkits/native/BUILD.gn +++ b/interfaces/innerkits/native/BUILD.gn @@ -29,6 +29,9 @@ ohos_shared_library("userauth_framework") { "../../../frameworks/kitsimpl/src/userauth.cpp", "../../../frameworks/kitsimpl/src/userauth_async_stub.cpp", "../../../frameworks/kitsimpl/src/userauth_proxy.cpp", + "../../../frameworks/kitsimpl/src/userauth_api7.cpp", + "../../../frameworks/kitsimpl/src/userauth_api7_datamgr.cpp", + "../../../frameworks/kitsimpl/src/iuserauth_api7_callback.cpp", ] configs = [ diff --git a/interfaces/innerkits/native/include/iuserauth_api7_callback.h b/interfaces/innerkits/native/include/iuserauth_api7_callback.h new file mode 100755 index 0000000000000000000000000000000000000000..6bfecf375b510f582aa2802f09599a47fac65ef7 --- /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 be7198471cd8989568e2871fb4bd689734502a9d..4c875e4468b73e8a549a0f9122c7fcfa05b8df1a 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 100755 index 0000000000000000000000000000000000000000..070fce2e869c2fa0992a1a48867f7b5c1ec23e4b --- /dev/null +++ b/interfaces/innerkits/native/include/userauth_api7.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef USERAUTH_API7_H +#define USERAUTH_API7_H + +#include + +#include "userauth_api7_info.h" +#include "userauth_api7_callback.h" + +namespace OHOS { +namespace UserIAM { +namespace API7UserAuth { +class UserAuthAPI7 { +public: + UserAuthAPI7() = default; + ~UserAuthAPI7() = default; + static UserAuthAPI7 &GetAuthenticator(); + void Execute(std::string authtype, std::string trustLevel, std::shared_ptr callback); + int32_t Cancel(); + int32_t CheckAvailability(std::string authtype, std::string trustLevel); + void On(std::string tip, std::shared_ptr tipCallback); + void Off(std::string tip, std::shared_ptr tipCallback); +}; +} // namespace API7UserAuth +} // namespace UserIAM +} // namespace OHOS +#endif // USERAUTH_API7_H \ No newline at end of file diff --git a/interfaces/innerkits/native/include/userauth_api7_callback.h b/interfaces/innerkits/native/include/userauth_api7_callback.h new file mode 100755 index 0000000000000000000000000000000000000000..c9d3dcb7abbed938356a384083fc255a45e02b6f --- /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_datamgr.h b/interfaces/innerkits/native/include/userauth_api7_datamgr.h new file mode 100755 index 0000000000000000000000000000000000000000..40d52c116665e7ae54607221f8831d4649e73ce8 --- /dev/null +++ b/interfaces/innerkits/native/include/userauth_api7_datamgr.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef USERAUTH_API7_DATAMGR_H +#define USERAUTH_API7_DATAMGR_H + +#include "userauth_api7_info.h" + +namespace OHOS { +namespace UserIAM { +namespace API7UserAuth { +class UserAuthAPI7Datamgr { +public: + static UserAuthAPI7Datamgr &GetInstance(); + void API7AddContextID(uint64_t contextID); + void API7GetContextID(uint64_t &contextID); + void API7DeleteContextID(); + void SetAPI7Tip(bool isTipOn); + bool GetAPI7Tip(); +private: + UserAuthAPI7Datamgr() = default; + ~UserAuthAPI7Datamgr() = default; + uint64_t contextID_ = CONTEXTID_INIT; + bool isTipOn_ = false; +}; +} // namespace API7UserAuth +} // namespace UserIAM +} // namespace OHOS +#endif // USERAUTH_API7_DATAMGR_H \ No newline at end of file diff --git a/interfaces/innerkits/native/include/userauth_api7_hilog_wrapper.h b/interfaces/innerkits/native/include/userauth_api7_hilog_wrapper.h new file mode 100755 index 0000000000000000000000000000000000000000..517032c9682bb4a4b1d390fb5eeb2bb82df91be7 --- /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 L_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(L_LABEL[module], formated__(__VA_ARGS__)) +#define USERAUTH_API7_HILOGD(module, ...) (void)OHOS::HiviewDFX::HiLog::Debug(L_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 100755 index 0000000000000000000000000000000000000000..445ce9bdbcd543f7c1df828052aa314ad59d6c4c --- /dev/null +++ b/interfaces/innerkits/native/include/userauth_api7_info.h @@ -0,0 +1,150 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef USERAUTH_API7_INFO_H +#define USERAUTH_API7_INFO_H + +#include + +#define CONTEXTID_INIT 0 +#define CHALLENGE_INIT 0 + +namespace OHOS { +namespace UserIAM { +namespace API7UserAuth { +// Authentication Result +enum AuthenticationResult { + /** + * The device does not support the current authentication method. + */ + NO_SUPPORT = -1, + /** + * Authentication succeeded. + */ + SUCCESS = 0, + /** + * Alignment failed. + */ + COMPARE_FAILURE = 1, + /** + * User deauthentication. + */ + CANCELED = 2, + /** + * Authentication timed out. + */ + TIMEOUT = 3, + /** + * Failed to turn on camera. + */ + CAMERA_FAIL = 4, + /** + * The authentication service is busy, please try again later. + */ + BUSY = 5, + /** + * Authentication parameters are invalid. + */ + INVALID_PARAMETERS = 6, + /** + * Too many authentication failures, locked. + */ + LOCKED = 7, + /** + * Authentication credentials not entered. + */ + NOT_ENROLLED = 78, + /** + * other errors. + */ + GENERAL_ERROR = 100, +}; +// Device Certification Capability +enum CheckAvailabilityResult { + // The device supports the specified authentication type and authentication security level + SUPPORTED = 0, + // The device does not support the specified authentication type + AUTH_TYPE_NOT_SUPPORT = 1, + // The device does not support the specified authentication security level + SECURE_LEVEL_NOT_SUPPORT = 2, + // The device does not support distributed authentication + DISTRIBUTED_AUTH_NOT_SUPPORT = 3, + // Authentication credentials are not entered in the device + NO_ENROLLED = 4, + // wrong number of parameters + PARAM_NUM_ERROR = 5, +}; +// Prompt events during authentication +enum TipEvent { + // Entry or Certification Results + RESULT = 1, + // Entry or Certification Cancellation + CANCEL = 2, + // Prompt during entry or authentication + ACQUIRE = 3, + // Entry or authentication functions are occupied + ISBUSY = 4, + // Not enough storage + OUT_OF_MEM = 5, + // Face authentication credential index number + FACE_ID = 6, +}; +// Result +enum Result { + // SUCCESSED + SUCCESSED = 0, + // FAILED + FAILED = 1, +}; +// Prompt code during authentication +enum TipCode { + // The light is too strong, the acquired image is too bright + FACE_AUTH_TIP_TOO_BRIGHT = 1, + // The light is too dim, the acquired image is too dark + FACE_AUTH_TIP_TOO_DARK = 2, + // The face is too close to the device + FACE_AUTH_TIP_TOO_CLOSE = 3, + // The face is too far from the device + FACE_AUTH_TIP_TOO_FAR = 4, + // The device is too high, only the face obtained + FACE_AUTH_TIP_TOO_HIGH = 5, + // The device is too low, only the lower part of the face is obtained + FACE_AUTH_TIP_TOO_LOW = 6, + // The device is too far to the right, only the right part of the face is obtained + FACE_AUTH_TIP_TOO_RIGHT = 7, + // The device is too far to the left, only the left part of the face is obtained + FACE_AUTH_TIP_TOO_LEFT = 8, + // During image acquisition, the user's face moves too fast + FACE_AUTH_TIP_TOO_MUCH_MOTION = 9, + // no facing camera + FACE_AUTH_TIP_POOR_GAZE = 10, + // No face information detected + FACE_AUTH_TIP_NOT_DETECTED = 11, +}; +// Prompt information during the authentication process +struct Tip { + // Whether the prompt information was successfully obtained + int32_t errorCode; + // Authentication prompt event + int32_t tipEvent; + // Authentication prompt event + int32_t tipCode; + // Description of the event prompt code of the authentication prompt + std::string tipInfo; +}; +} // namespace API7UserAuth +} // namespace UserIam +} // namespace OHOS +#endif // USERAUTH_API7_INFO_H diff --git a/interfaces/innerkits/native/include/userauth_info.h b/interfaces/innerkits/native/include/userauth_info.h index 4284e9b699c31795a60c8b8ff3cde64e44b397e0..7a60d349d6569436667b266f68a2805578173564 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 3f2b5a61f2219ccb7fd635d3bc00b19207d81484..7ad3d7ecf78909a8f569b56334b95122a1ed66c2 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_datamgr.h b/services/include/userauth_datamgr.h index 73e60ae373fe04239e55eb02ea01fba0d49d7b75..6c6094ff9524f76c570ec206f720154b10bc3480 100755 --- a/services/include/userauth_datamgr.h +++ b/services/include/userauth_datamgr.h @@ -26,7 +26,7 @@ namespace OHOS { namespace UserIAM { namespace UserAuth { -class UserAuthDataMgr : public Singleton { +class UserAuthDataMgr { public: static UserAuthDataMgr &GetInstance(); int32_t AddContextID(uint64_t contextID); diff --git a/services/include/userauth_excallback_impl.h b/services/include/userauth_excallback_impl.h index 65a2a9c441d941324c768939d7070e38f1802d18..e20ad8d669eba36cb6133b378d59a1524fe1df3a 100644 --- a/services/include/userauth_excallback_impl.h +++ b/services/include/userauth_excallback_impl.h @@ -47,7 +47,7 @@ private: uint64_t callbackContextID_ = 0; std::string pkgName_ = ""; uint64_t callerUid_ = 0; - bool callbackResultFlag_ = false; + bool isResultDoneFlag_ = false; sptr callback_ { nullptr }; AuthType authType_; std::mutex mutex_; diff --git a/services/src/userauth_adapter.cpp b/services/src/userauth_adapter.cpp index 4dff5c26e86a491f9364355bb11c8b3935189aff..89fd00ce626f72b5ce05674ffee37ae59ef00905 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; @@ -49,10 +48,10 @@ void UserAuthAdapter::GetPropAuthInfo(uint64_t callerUID, std::string pkgName, G sptr& callback) { USERAUTH_HILOGD(MODULE_SERVICE, "UserAuth GetPropAuthInfo is start!"); - - std::shared_ptr getInfoCallback = + using namespace UserIDM; + std::shared_ptr getInfoCallback = std::make_shared(callback, requset, callerUID, pkgName); - int32_t ret = UserIDM::UserIDMClient::GetInstance().GetAuthInfo(static_cast(requset.authType), + int32_t ret = UserIDMClient::GetInstance().GetAuthInfo(static_cast(requset.authType), getInfoCallback); if (ret != SUCCESS) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuth GetPropAuthInfo ERROR!"); @@ -66,14 +65,16 @@ void UserAuthAdapter::SetPropAuthInfo(uint64_t callerUID, std::string pkgName, i sptr& callback) { USERAUTH_HILOGD(MODULE_SERVICE, "UserAuth SetPropAuthInfo is start!"); + using namespace AuthResPool; FreezInfo freezInfo; freezInfo.callerID = callerUID; freezInfo.authType = requset.authType; freezInfo.pkgName = pkgName; freezInfo.resultCode = resultCode; + std::shared_ptr setPropCallback = std::make_shared(callback, templateIds, authToken, freezInfo); - OHOS::UserIAM::AuthResPool::AuthAttributes authAttributes; + AuthAttributes authAttributes; int32_t ret = SetProPropAuthInfo(authAttributes, callerUID, pkgName, requset, templateIds, setPropCallback); if (ret != SUCCESS) { return; @@ -94,7 +95,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); @@ -139,11 +140,11 @@ void UserAuthAdapter::GetPropAuthInfoCoauth(uint64_t callerUID, std::string pkgN sptr& callback) { USERAUTH_HILOGD(MODULE_SERVICE, "UserAuth GetPropAuthInfoCoauth is start!"); - - std::shared_ptr getInfoCallback = + using namespace UserIDM; + std::shared_ptr getInfoCallback = std::make_shared(callback, callerUID, pkgName, resultCode, authToken, requset); - int32_t ret = UserIDM::UserIDMClient::GetInstance().GetAuthInfo(static_cast(requset.authType), + int32_t ret = UserIDMClient::GetInstance().GetAuthInfo(static_cast(requset.authType), getInfoCallback); if (ret != SUCCESS) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuth GetPropAuthInfoCoauth ERROR!"); @@ -156,10 +157,11 @@ void UserAuthAdapter::CoauthSetPropAuthInfo(int32_t resultCode, uint64_t callerU sptr& callback) { USERAUTH_HILOGD(MODULE_SERVICE, "UserAuth CoauthSetPropAuthInfo is start!"); - std::shared_ptr setPropCallback = + using namespace UserIDM; + std::shared_ptr setPropCallback = std::make_shared(callback, callerUID, pkgName, resultCode, authToken, requset); - int32_t ret = UserIDM::UserIDMClient::GetInstance().GetAuthInfo(static_cast(requset.authType), + int32_t ret = UserIDMClient::GetInstance().GetAuthInfo(static_cast(requset.authType), setPropCallback); if (ret != SUCCESS) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuth CoauthSetPropAuthInfo ERROR!"); @@ -170,8 +172,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; } @@ -228,10 +257,11 @@ int32_t UserAuthAdapter::GetExecutorProp(uint64_t callerUID, std::string pkgName GetPropertyRequest requset, ExecutorProperty &result) { USERAUTH_HILOGD(MODULE_SERVICE, "UserAuth GetExecutorProp is start!"); + using namespace AuthResPool; uint32_t value; - auto pAuthAttributes (std::make_shared()); - OHOS::UserIAM::AuthResPool::AuthAttributes cAuthAttributes; + auto pAuthAttributes (std::make_shared()); + AuthAttributes cAuthAttributes; int32_t ret = cAuthAttributes.SetUint32Value(AUTH_TYPE, requset.authType); if (ret != SUCCESS) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuth SetUint32Value AUTH_TYPE ERROR!"); @@ -286,8 +316,8 @@ int32_t UserAuthAdapter::GetEachExecutorProp(GetPropertyRequest &requset, Execut result.freezingTime = 0; result.remainTimes = 0; result.authSubType = UserAuth::PIN_SIX; - for (std::vector::const_iterator iter = requset.keys.begin(); iter != requset.keys.end(); ++iter) { - switch (*iter) { + for (auto const & item : requset.keys) { + switch (item) { case AUTH_SUB_TYPE: ret = pAuthAttributes->GetUint64Value(AUTH_SUBTYPE, tmpValue); if (ret != SUCCESS) { @@ -328,10 +358,15 @@ int32_t UserAuthAdapter::SetExecutorProp(uint64_t callerUID, std::string pkgName sptr& callback) { USERAUTH_HILOGD(MODULE_SERVICE, "UserAuth SetExecutorProp is start!"); + using namespace AuthResPool; + uint32_t value; std::shared_ptr setPropCallback = std::make_shared(callback); - OHOS::UserIAM::AuthResPool::AuthAttributes pAuthAttributes; - int32_t ret = pAuthAttributes.SetUint32Value(AUTH_PROPERTY_MODE, PROPERMODE_SET); + AuthAttributes pAuthAttributes; + value = requset.key == SetPropertyType::INIT_ALGORITHM ? + static_cast(AuthPropertyMode::PROPERMODE_INIT_ALGORITHM) + : static_cast(AuthPropertyMode::PROPERMODE_RELEASE_ALGORITHM); + int32_t ret = pAuthAttributes.SetUint32Value(AUTH_PROPERTY_MODE, value); if (ret != SUCCESS) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuth SetUint32Value SET_AUTH_PROPERTY_MODE ERROR!"); return ret; @@ -355,7 +390,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; @@ -386,9 +421,8 @@ int32_t UserAuthAdapter::coAuth(CoAuthInfo coAuthInfo, sptr& USERAUTH_HILOGE(MODULE_SERVICE, "UserAuth SaveCoauthCallback ERROR!"); return ret; } - for (std::vector::iterator iter = coAuthInfo.sessionIds.begin(); - iter != coAuthInfo.sessionIds.end(); ++iter) { - CoAuth::CoAuth::GetInstance().coAuth(*iter, authInfo, coAuthCallback); + for (auto const & item : coAuthInfo.sessionIds) { + CoAuth::CoAuth::GetInstance().coAuth(item, authInfo, coAuthCallback); } return SUCCESS; diff --git a/services/src/userauth_async_proxy.cpp b/services/src/userauth_async_proxy.cpp index d12862b1da120f24169130e8c58c71fca20aeade..ab5dec89e84576ccb5309ec90fede4ac8cacfc6e 100755 --- a/services/src/userauth_async_proxy.cpp +++ b/services/src/userauth_async_proxy.cpp @@ -59,7 +59,7 @@ void UserAuthAsyncProxy::onResult(const int32_t result, const AuthResult extraIn MessageParcel reply; if (!data.WriteInterfaceToken(UserAuthAsyncProxy::GetDescriptor())) { USERAUTH_HILOGI(MODULE_SERVICE, "userauth write descriptor failed!"); - return ; + return; } if (!data.WriteInt32(result)) { USERAUTH_HILOGE(MODULE_SERVICE, "failed to WriteInt32(result)."); diff --git a/services/src/userauth_excallback_impl.cpp b/services/src/userauth_excallback_impl.cpp index 695f81c01c77a590663bb676e440e1776c1d3454..125dba752e7f05386eb6c6f623e1de347622fe7f 100644 --- a/services/src/userauth_excallback_impl.cpp +++ b/services/src/userauth_excallback_impl.cpp @@ -51,6 +51,10 @@ UserAuthCallbackImplSetPropFreez::UserAuthCallbackImplSetPropFreez(const sptr templateIds, UserAuthToken authToken, FreezInfo freezInfo) { + if (impl == nullptr) { + USERAUTH_HILOGE(MODULE_SERVICE, "UserAuthCallbackImplSetPropFreez impl callback is Null"); + return ; + } callback_ = impl; templateIds_.clear(); templateIds_.assign(templateIds.begin(), templateIds.end()); @@ -112,7 +116,7 @@ UserAuthCallbackImplCoAuth::UserAuthCallbackImplCoAuth(const sptr &scheduleToken) @@ -121,12 +125,13 @@ void UserAuthCallbackImplCoAuth::OnFinish(uint32_t resultCode, std::vector scheduleToken_; scheduleToken_.assign(scheduleToken.begin(), scheduleToken.end()); - auto task = std::bind(&UserAuthCallbackImplCoAuth::OnFinishHandle, this, resultCode, scheduleToken_); - bool ret = ContextThreadPool::GetInstance().AddTask(callbackContextID_, task); + bool ret = ContextThreadPool::GetInstance().AddTask(callbackContextID_, [&](){ + OnFinishHandle(resultCode, scheduleToken_); + }); if (!ret) { USERAUTH_HILOGE(MODULE_SERVICE, "OnFinish ContextThreadPool is BUSY"); callback_->onResult(BUSY, authResult); - callbackResultFlag_ = true; + isResultDoneFlag_ = true; return; } } @@ -134,11 +139,12 @@ void UserAuthCallbackImplCoAuth::OnAcquireInfo(uint32_t acquire) { USERAUTH_HILOGD(MODULE_SERVICE, "UserAuthCallbackImplCoAuth OnAcquireInfo enter"); - auto task = std::bind(&UserAuthCallbackImplCoAuth::OnAcquireInfoHandle, this, acquire); - bool ret = ContextThreadPool::GetInstance().AddTask(callbackContextID_, task); + bool ret = ContextThreadPool::GetInstance().AddTask(callbackContextID_, [&](){ + OnAcquireInfoHandle(acquire); + }); if (!ret) { USERAUTH_HILOGE(MODULE_SERVICE, "OnAcquireInfoHandle ContextThreadPool is BUSY"); - callbackResultFlag_ = true; + isResultDoneFlag_ = true; return; } } @@ -173,22 +179,23 @@ void UserAuthCallbackImplCoAuth::OnFinishHandle(uint32_t resultCode, std::vector USERAUTH_HILOGD(MODULE_SERVICE, "OnFinishHandle scheduleTokensize:%{public}d, resultCode:%{public}d", scheduleToken.size(), resultCode); callbackNowCount_++; - if (callbackResultFlag_) { + if (isResultDoneFlag_) { return; } int32_t ret = UserAuthAdapter::GetInstance().RequestAuthResult(callbackContextID_, scheduleToken, authToken, sessionIds); + if (ret == E_RET_UNDONE) { + if (callbackNowCount_ == callbackCount_) { + USERAUTH_HILOGD(MODULE_SERVICE, "UserAuthCallbackImplCoAuth E_RET_UNDONE"); + UserAuthDataMgr::GetInstance().DeleteContextID(callbackContextID_); + UserAuthCallbackImplCoAuth::DeleteCoauthCallback(callbackContextID_); + callback_->onResult(GENERAL_ERROR, authResult); + isResultDoneFlag_ = true; + } + return; + } if (resultCode != LOCKED) { - if (ret == E_RET_UNDONE) { - if (callbackNowCount_ == callbackCount_) { - USERAUTH_HILOGD(MODULE_SERVICE, "UserAuthCallbackImplCoAuth E_RET_UNDONE"); - UserAuthDataMgr::GetInstance().DeleteContextID(callbackContextID_); - UserAuthCallbackImplCoAuth::DeleteCoauthCallback(callbackContextID_); - callback_->onResult(GENERAL_ERROR, authResult); - callbackResultFlag_ = true; - } - return ; - } else if (ret == SUCCESS) { + if (ret == SUCCESS) { OnFinishHandleExtend(setPropertyRequest, authResult, ret, authToken); } else { USERAUTH_HILOGD(MODULE_SERVICE, "RequestAuthResult NOT SUCCESS"); @@ -206,20 +213,20 @@ void UserAuthCallbackImplCoAuth::OnFinishHandle(uint32_t resultCode, std::vector callback_); } if (sessionIds.size() != 0) { - for (std::vector::iterator iter = sessionIds.begin(); iter != sessionIds.end(); iter++) { - UserAuthAdapter::GetInstance().Cancel(*iter); + for (auto const & item : sessionIds) { + UserAuthAdapter::GetInstance().Cancel(item); } } UserAuthDataMgr::GetInstance().DeleteContextID(callbackContextID_); UserAuthCallbackImplCoAuth::DeleteCoauthCallback(callbackContextID_); - callbackResultFlag_ = true; + isResultDoneFlag_ = true; } void UserAuthCallbackImplCoAuth::OnAcquireInfoHandle(uint32_t acquire) { USERAUTH_HILOGD(MODULE_SERVICE, "UserAuthCallbackImplCoAuth OnAcquireInfoHandle"); std::lock_guard lock(mutex_); - if (callbackResultFlag_) { + if (isResultDoneFlag_) { return; } int32_t module = static_cast(authType_); @@ -324,8 +331,8 @@ void UserAuthCallbackImplIDMCothGetPorpFreez::OnGetInfo(std::vector::const_iterator iter = info.begin(); iter != info.end(); ++iter) { - templateIds.push_back((*iter).templateId); + for (auto const & item : info) { + templateIds.push_back(item.templateId); } UserAuthAdapter::GetInstance().SetPropAuthInfo(callerUid_, pkgName_, resultCode_, authToken_, requset_, templateIds, callback_); diff --git a/services/src/userauth_service.cpp b/services/src/userauth_service.cpp index 4f827b15050cf69a6c1d507ea46db6469329ef17..3e93ef3c7fa551caa54fa0db6844192124279b4a 100644 --- a/services/src/userauth_service.cpp +++ b/services/src/userauth_service.cpp @@ -77,6 +77,7 @@ int32_t UserAuthService::GetAvailableStatus(const AuthType authType, const AuthT USERAUTH_HILOGE(MODULE_SERVICE, "UserAuthService GetAvailableStatus GetUserID is ERROR!"); return result; } + ret = userauthController_.GetAuthTrustLevel(userID, authType, authTurstLevelFromSys); if (ret == SUCCESS) { USERAUTH_HILOGD(MODULE_INNERKIT, "UserAuthService iAuthTurstLevel_:%{public}d", authTurstLevelFromSys); @@ -100,14 +101,14 @@ void UserAuthService::GetProperty(const GetPropertyRequest request, sptrGetCallingUid(); - callerName = std::to_string(callerID); - sptr dr = new UserAuthServiceCallbackDeathRecipient(callback); if ((!callback->AsObject()->AddDeathRecipient(dr))) { USERAUTH_HILOGE(MODULE_SERVICE, "Failed to add death recipient UserAuthServiceCallbackDeathRecipient"); } + callerID = this->GetCallingUid(); + callerName = std::to_string(callerID); + userauthController_.GetPropAuthInfo(callerName, callerID, request, callback); } @@ -122,13 +123,14 @@ void UserAuthService::SetProperty(const SetPropertyRequest request, sptrGetCallingUid(); - callerName = std::to_string(callerID); - sptr dr = new UserAuthServiceCallbackDeathRecipient(callback); if ((!callback->AsObject()->AddDeathRecipient(dr))) { USERAUTH_HILOGE(MODULE_SERVICE, "Failed to add death recipient UserAuthServiceCallbackDeathRecipient"); } + + callerID = this->GetCallingUid(); + callerName = std::to_string(callerID); + ret = userauthController_.SetExecutorProp(callerID, callerName, request, callback); if (ret != SUCCESS) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuthService SetExecutorProp getUserID is ERROR!"); @@ -151,15 +153,23 @@ uint64_t UserAuthService::Auth(const uint64_t challenge, const AuthType authType AuthSolution authSolutionParam; CoAuthInfo coAuthInfo; AuthResult extraInfo; + + sptr dr = new UserAuthServiceCallbackDeathRecipient(callback); + if ((!callback->AsObject()->AddDeathRecipient(dr))) { + USERAUTH_HILOGE(MODULE_SERVICE, "Failed to add death recipient UserAuthServiceCallbackDeathRecipient"); + } + if (GetControllerData(callback, extraInfo, authTurstLevel, callerID, callerName, contextID) == FAIL) { return ret; } + result = userauthController_.GetUserID(userID); if (result != SUCCESS) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuthService Auth GetUserID is ERROR!"); callback->onResult(FAIL, extraInfo); return ret; } + authSolutionParam.contextId = contextID; authSolutionParam.userId = userID; authSolutionParam.authTrustLevel = authTurstLevel; @@ -171,14 +181,11 @@ uint64_t UserAuthService::Auth(const uint64_t challenge, const AuthType authType callback->onResult(result, extraInfo); return ret; } + coAuthInfo.authType = authType; coAuthInfo.callerID = callerID; coAuthInfo.contextID = contextID; coAuthInfo.pkgName = callerName; - sptr dr = new UserAuthServiceCallbackDeathRecipient(callback); - if ((!callback->AsObject()->AddDeathRecipient(dr))) { - USERAUTH_HILOGE(MODULE_SERVICE, "Failed to add death recipient UserAuthServiceCallbackDeathRecipient"); - } result = userauthController_.coAuth(coAuthInfo, callback); if (result != SUCCESS) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuthService Auth coAuth is ERROR!"); @@ -201,9 +208,16 @@ uint64_t UserAuthService::AuthUser(const int32_t userId, const uint64_t challeng AuthSolution authSolutionParam; CoAuthInfo coAuthInfo; AuthResult extraInfo; + + sptr dr = new UserAuthServiceCallbackDeathRecipient(callback); + if ((!callback->AsObject()->AddDeathRecipient(dr))) { + USERAUTH_HILOGE(MODULE_SERVICE, "Failed to add death recipient UserAuthServiceCallbackDeathRecipient"); + } + if (GetControllerData(callback, extraInfo, authTurstLevel, callerID, callerName, contextID) == FAIL) { return ret; } + authSolutionParam.contextId = contextID; authSolutionParam.userId = userId; authSolutionParam.authTrustLevel = authTurstLevel; @@ -216,14 +230,11 @@ uint64_t UserAuthService::AuthUser(const int32_t userId, const uint64_t challeng callback->onResult(result, extraInfo); return ret; } + coAuthInfo.authType = authType; coAuthInfo.callerID = callerID; coAuthInfo.contextID = contextID; coAuthInfo.pkgName = callerName; - sptr dr = new UserAuthServiceCallbackDeathRecipient(callback); - if ((!callback->AsObject()->AddDeathRecipient(dr))) { - USERAUTH_HILOGE(MODULE_SERVICE, "Failed to add death recipient UserAuthServiceCallbackDeathRecipient"); - } result = userauthController_.coAuth(coAuthInfo, callback); if (result != SUCCESS) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuthService AuthUser coAuth is ERROR!"); @@ -287,8 +298,8 @@ int32_t UserAuthService::CancelAuth(const uint64_t contextId) result = userauthController_.CancelContext(contextId, sessionIds); if (result == SUCCESS) { - for (std::vector::iterator iter = sessionIds.begin(); iter != sessionIds.end(); iter++) { - result = userauthController_.Cancel(*iter); + for (auto const & item : sessionIds) { + result = userauthController_.Cancel(item); if (result != SUCCESS) { USERAUTH_HILOGE(MODULE_SERVICE, "UserAuthService CancelAuth Cancel is ERROR!"); return result; diff --git a/services/src/userauth_stub.cpp b/services/src/userauth_stub.cpp index ee2345a3a2f3e5a67ba61477606046df4e3e483d..f5f9253d21500524a0576acede984c08c7244181 100644 --- a/services/src/userauth_stub.cpp +++ b/services/src/userauth_stub.cpp @@ -103,7 +103,7 @@ int32_t UserAuthStub::GetPropertyStub(MessageParcel& data, MessageParcel& reply) sptr callback = iface_cast(obj); if (callback == nullptr) { - return E_READ_PARCEL_ERROR; + return FAIL; } GetProperty(getPropertyRequest, callback); @@ -142,7 +142,7 @@ int32_t UserAuthStub::SetPropertyStub(MessageParcel& data, MessageParcel& reply) sptr callback = iface_cast(obj); if (callback == nullptr) { - return E_READ_PARCEL_ERROR; + return FAIL; } SetProperty(setPropertyRequest, callback); @@ -177,7 +177,7 @@ int32_t UserAuthStub::AuthStub(MessageParcel& data, MessageParcel& reply) sptr callback = iface_cast(obj); if (callback == nullptr) { - return E_READ_PARCEL_ERROR; + return FAIL; } ret = Auth(challenge, static_cast(authType), static_cast(authTurstLevel), callback); @@ -221,7 +221,7 @@ int32_t UserAuthStub::AuthUserStub(MessageParcel& data, MessageParcel& reply) sptr callback = iface_cast(obj); if (callback == nullptr) { - return E_READ_PARCEL_ERROR; + return FAIL; } ret = AuthUser(userID, challenge, static_cast(authType), diff --git a/test/unittest/include/userauth_test.h b/test/unittest/include/userauth_test.h index eb0908a704951e98e7480ed07e7e0e68c4b2fee1..e020c97aa0d1e5ecf66d18d81e6fe2e0324f7af8 100644 --- a/test/unittest/include/userauth_test.h +++ b/test/unittest/include/userauth_test.h @@ -25,6 +25,11 @@ void UseriamUtTest_003(); void UseriamUtTest_004(); void UseriamUtTest_005(); void UseriamUtTest_006(); +void UseriamUtTest_007(); +void UseriamUtTest_008(); +void UseriamUtTest_009(); +void UseriamUtTest_010(); +void UseriamUtTest_011(); } } } diff --git a/test/unittest/src/userauth_test.cpp b/test/unittest/src/userauth_test.cpp index 980deb6083c1b3f24cb42bda2bdd7f6ec6b5322d..331b700988f3ff68c7e81c018bbec4107645a11c 100644 --- a/test/unittest/src/userauth_test.cpp +++ b/test/unittest/src/userauth_test.cpp @@ -17,6 +17,7 @@ #include #include #include "user_auth.h" +#include "userauth_api7.h" #include "userauth_callback.h" #include "userauth_info.h" #include "userauth_test.h" @@ -79,6 +80,25 @@ void TestUserAuthCallback::onSetExecutorProperty(const int32_t result) std::cout << "onSetExecutorProperty callback" << std::endl; } +class TestUserAuthAPI7Callback : public UserAuthAPI7Callback { + public: + TestUserAuthAPI7Callback() = default; + virtual ~TestUserAuthAPI7Callback() = default; + + void API7OnResult(const int32_t result) override; + void TipCallback(const Tip result) override; +} + +void TestUserAuthAPI7Callback::API7OnResult(const int32_t result) +{ + std::cout << "API7OnResult callback" << std::endl; +} + +void TestUserAuthAPI7Callback::TipCallback(const Tip result) +{ + std::cout << "TipCallback callback" << std::endl; +} + HWTEST_F(UseriamUtTest, UseriamUtTest_001, TestSize.Level1) { AuthType authType = FACE; @@ -131,6 +151,34 @@ HWTEST_F(UseriamUtTest, UseriamUtTest_006, TestSize.Level1) uint64_t contextId = 123; EXPECT_EQ(0, UserAuth::GetInstance().CancelAuth(contextId)); } + +HWTEST_F(UseriamUtTest, UseriamUtTest_007, TestSize.Level1) +{ + std::shared_ptr callback = std::make_shared(); + UserAuthAPI7::GetAuthenticator().Execute("FACE_ONLY", "S1", callback); +} + +HWTEST_F(UseriamUtTest, UseriamUtTest_008, TestSize.Level1) +{ + EXPECT_EQ(0, UserAuthAPI7::GetAuthenticator().Cancel()); +} + +HWTEST_F(UseriamUtTest, UseriamUtTest_009, TestSize.Level1) +{ + EXPECT_EQ(0, UserAuthAPI7::GetAuthenticator().CheckAvailability("FACE_ONLY", "S1")); +} + +HWTEST_F(UseriamUtTest, UseriamUtTest_010, TestSize.Level1) +{ + std::shared_ptr callback = std::make_shared(); + UserAuthAPI7::GetAuthenticator().On("tip", callback); +} + +HWTEST_F(UseriamUtTest, UseriamUtTest_011, TestSize.Level1) +{ + std::shared_ptr callback = std::make_shared(); + UserAuthAPI7::GetAuthenticator().Off("tip", callback); +} } } } \ No newline at end of file