From 1437d2c1b5be0cb7dce1f08e174636fea8c4846b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9F=A9=E9=9C=87?= Date: Sun, 16 Jan 2022 15:07:05 +0800 Subject: [PATCH] CoAuth Code Update MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 韩震 --- LICENSE | 177 ++++++ OAT.xml | 25 + coauth.gni | 29 + .../kitsimpl/include/coauth_callback_proxy.h | 43 ++ .../kitsimpl/include/coauth_callback_stub.h | 45 ++ frameworks/kitsimpl/include/coauth_proxy.h | 54 ++ .../include/executor_callback_proxy.h | 47 ++ .../kitsimpl/include/executor_callback_stub.h | 48 ++ .../include/executor_messenger_proxy.h | 45 ++ .../include/executor_messenger_stub.h | 39 ++ frameworks/kitsimpl/include/i_coauth.h | 61 ++ .../kitsimpl/include/icoauth_callback.h | 43 ++ .../kitsimpl/include/iexecutor_callback.h | 50 ++ .../kitsimpl/include/iexecutor_messenger.h | 47 ++ frameworks/kitsimpl/include/iquery_callback.h | 39 ++ .../kitsimpl/include/iset_prop_callback.h | 39 ++ .../kitsimpl/include/query_callback_proxy.h | 42 ++ .../kitsimpl/include/query_callback_stub.h | 42 ++ .../include/set_prop_callback_proxy.h | 41 ++ .../kitsimpl/include/set_prop_callback_stub.h | 44 ++ .../kitsimpl/src/auth_executor_registry.cpp | 112 ++++ frameworks/kitsimpl/src/auth_info.cpp | 47 ++ frameworks/kitsimpl/src/coauth.cpp | 144 +++++ .../kitsimpl/src/coauth_callback_proxy.cpp | 74 +++ .../kitsimpl/src/coauth_callback_stub.cpp | 78 +++ frameworks/kitsimpl/src/coauth_proxy.cpp | 285 +++++++++ .../kitsimpl/src/executor_callback_proxy.cpp | 179 ++++++ .../kitsimpl/src/executor_callback_stub.cpp | 185 ++++++ .../kitsimpl/src/executor_messenger_proxy.cpp | 110 ++++ .../kitsimpl/src/executor_messenger_stub.cpp | 82 +++ .../kitsimpl/src/query_callback_proxy.cpp | 53 ++ .../kitsimpl/src/query_callback_stub.cpp | 57 ++ .../kitsimpl/src/set_prop_callback_proxy.cpp | 59 ++ .../kitsimpl/src/set_prop_callback_stub.cpp | 59 ++ interfaces/innerkits/BUILD.gn | 74 +++ .../innerkits/include/auth_attributes.h | 97 ++++ interfaces/innerkits/include/auth_executor.h | 61 ++ .../include/auth_executor_registry.h | 54 ++ interfaces/innerkits/include/auth_info.h | 39 ++ interfaces/innerkits/include/auth_message.h | 41 ++ interfaces/innerkits/include/co_auth.h | 60 ++ .../innerkits/include/coauth_callback.h | 36 ++ .../innerkits/include/coauth_info_define.h | 164 ++++++ .../innerkits/include/executor_callback.h | 38 ++ .../innerkits/include/executor_messenger.h | 40 ++ interfaces/innerkits/include/query_callback.h | 30 + .../innerkits/include/set_prop_callback.h | 33 ++ interfaces/innerkits/src/auth_attributes.cpp | 429 ++++++++++++++ interfaces/innerkits/src/auth_executor.cpp | 102 ++++ interfaces/innerkits/src/auth_message.cpp | 35 ++ ohos.build | 23 + sa_profile/5203.xml | 24 + sa_profile/BUILD.gn | 30 + sa_profile/useriam.cfg | 16 + sa_profile/useriam.rc | 5 + services/BUILD.gn | 65 +++ services/include/auth_res_manager.h | 56 ++ services/include/auth_res_pool.h | 67 +++ services/include/coauth_manager.h | 56 ++ services/include/coauth_service.h | 59 ++ services/include/coauth_stub.h | 46 ++ services/include/coauth_thread_pool.h | 39 ++ services/mock/include/coauth_interface.h | 63 ++ services/mock/include/common_defines.h | 22 + services/mock/include/useriam_common.h | 44 ++ services/mock/src/schedule_ca_mock.cpp | 72 +++ services/src/auth_res_manager.cpp | 151 +++++ services/src/auth_res_pool.cpp | 175 ++++++ services/src/coauth_manager.cpp | 216 +++++++ services/src/coauth_service.cpp | 177 ++++++ services/src/coauth_stub.cpp | 196 +++++++ services/src/coauth_thread_pool.cpp | 45 ++ services/src/executor_messenger.cpp | 96 ++++ test/BUILD.gn | 21 + test/unittest/BUILD.gn | 50 ++ test/unittest/include/coauth_test.h | 46 ++ test/unittest/src/coauth_test.cpp | 543 ++++++++++++++++++ utils/BUILD.gn | 21 + utils/native/include/coauth_errors.h | 35 ++ utils/native/include/coauth_hilog_wrapper.h | 94 +++ 80 files changed, 6410 insertions(+) create mode 100755 LICENSE create mode 100755 OAT.xml create mode 100755 coauth.gni create mode 100644 frameworks/kitsimpl/include/coauth_callback_proxy.h create mode 100644 frameworks/kitsimpl/include/coauth_callback_stub.h create mode 100755 frameworks/kitsimpl/include/coauth_proxy.h create mode 100644 frameworks/kitsimpl/include/executor_callback_proxy.h create mode 100644 frameworks/kitsimpl/include/executor_callback_stub.h create mode 100644 frameworks/kitsimpl/include/executor_messenger_proxy.h create mode 100644 frameworks/kitsimpl/include/executor_messenger_stub.h create mode 100755 frameworks/kitsimpl/include/i_coauth.h create mode 100644 frameworks/kitsimpl/include/icoauth_callback.h create mode 100755 frameworks/kitsimpl/include/iexecutor_callback.h create mode 100644 frameworks/kitsimpl/include/iexecutor_messenger.h create mode 100644 frameworks/kitsimpl/include/iquery_callback.h create mode 100644 frameworks/kitsimpl/include/iset_prop_callback.h create mode 100644 frameworks/kitsimpl/include/query_callback_proxy.h create mode 100644 frameworks/kitsimpl/include/query_callback_stub.h create mode 100644 frameworks/kitsimpl/include/set_prop_callback_proxy.h create mode 100644 frameworks/kitsimpl/include/set_prop_callback_stub.h create mode 100644 frameworks/kitsimpl/src/auth_executor_registry.cpp create mode 100644 frameworks/kitsimpl/src/auth_info.cpp create mode 100755 frameworks/kitsimpl/src/coauth.cpp create mode 100644 frameworks/kitsimpl/src/coauth_callback_proxy.cpp create mode 100644 frameworks/kitsimpl/src/coauth_callback_stub.cpp create mode 100755 frameworks/kitsimpl/src/coauth_proxy.cpp create mode 100644 frameworks/kitsimpl/src/executor_callback_proxy.cpp create mode 100644 frameworks/kitsimpl/src/executor_callback_stub.cpp create mode 100644 frameworks/kitsimpl/src/executor_messenger_proxy.cpp create mode 100644 frameworks/kitsimpl/src/executor_messenger_stub.cpp create mode 100644 frameworks/kitsimpl/src/query_callback_proxy.cpp create mode 100644 frameworks/kitsimpl/src/query_callback_stub.cpp create mode 100644 frameworks/kitsimpl/src/set_prop_callback_proxy.cpp create mode 100644 frameworks/kitsimpl/src/set_prop_callback_stub.cpp create mode 100755 interfaces/innerkits/BUILD.gn create mode 100644 interfaces/innerkits/include/auth_attributes.h create mode 100644 interfaces/innerkits/include/auth_executor.h create mode 100644 interfaces/innerkits/include/auth_executor_registry.h create mode 100644 interfaces/innerkits/include/auth_info.h create mode 100644 interfaces/innerkits/include/auth_message.h create mode 100755 interfaces/innerkits/include/co_auth.h create mode 100644 interfaces/innerkits/include/coauth_callback.h create mode 100644 interfaces/innerkits/include/coauth_info_define.h create mode 100644 interfaces/innerkits/include/executor_callback.h create mode 100644 interfaces/innerkits/include/executor_messenger.h create mode 100644 interfaces/innerkits/include/query_callback.h create mode 100644 interfaces/innerkits/include/set_prop_callback.h create mode 100644 interfaces/innerkits/src/auth_attributes.cpp create mode 100644 interfaces/innerkits/src/auth_executor.cpp create mode 100644 interfaces/innerkits/src/auth_message.cpp create mode 100755 ohos.build create mode 100755 sa_profile/5203.xml create mode 100755 sa_profile/BUILD.gn create mode 100644 sa_profile/useriam.cfg create mode 100644 sa_profile/useriam.rc create mode 100755 services/BUILD.gn create mode 100644 services/include/auth_res_manager.h create mode 100644 services/include/auth_res_pool.h create mode 100644 services/include/coauth_manager.h create mode 100755 services/include/coauth_service.h create mode 100755 services/include/coauth_stub.h create mode 100644 services/include/coauth_thread_pool.h create mode 100644 services/mock/include/coauth_interface.h create mode 100644 services/mock/include/common_defines.h create mode 100644 services/mock/include/useriam_common.h create mode 100644 services/mock/src/schedule_ca_mock.cpp create mode 100644 services/src/auth_res_manager.cpp create mode 100644 services/src/auth_res_pool.cpp create mode 100644 services/src/coauth_manager.cpp create mode 100755 services/src/coauth_service.cpp create mode 100755 services/src/coauth_stub.cpp create mode 100644 services/src/coauth_thread_pool.cpp create mode 100644 services/src/executor_messenger.cpp create mode 100755 test/BUILD.gn create mode 100755 test/unittest/BUILD.gn create mode 100644 test/unittest/include/coauth_test.h create mode 100644 test/unittest/src/coauth_test.cpp create mode 100755 utils/BUILD.gn create mode 100755 utils/native/include/coauth_errors.h create mode 100755 utils/native/include/coauth_hilog_wrapper.h diff --git a/LICENSE b/LICENSE new file mode 100755 index 0000000..4947287 --- /dev/null +++ b/LICENSE @@ -0,0 +1,177 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS \ No newline at end of file diff --git a/OAT.xml b/OAT.xml new file mode 100755 index 0000000..131151b --- /dev/null +++ b/OAT.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + diff --git a/coauth.gni b/coauth.gni new file mode 100755 index 0000000..c3fbfa7 --- /dev/null +++ b/coauth.gni @@ -0,0 +1,29 @@ +# 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("//build/ohos.gni") + +coauth_native_part_name = "coauthmgr" + +coauth_root_path = "//base/useriam/coauth/" + +coauth_service_path = "${coauth_root_path}/services" + +coauth_frameworks_path = "${coauth_root_path}/frameworks" + +coauth_interfaces_path = "${coauth_root_path}/interfaces" + +coauth_innerkits_path = "${coauth_interfaces_path}/innerkits" + +coauth_utils_path = "${coauth_root_path}/utils" + diff --git a/frameworks/kitsimpl/include/coauth_callback_proxy.h b/frameworks/kitsimpl/include/coauth_callback_proxy.h new file mode 100644 index 0000000..128b7e9 --- /dev/null +++ b/frameworks/kitsimpl/include/coauth_callback_proxy.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 COAUTH_CALLBACK_PROXY_H +#define COAUTH_CALLBACK_PROXY_H + +#include +#include +#include "icoauth_callback.h" + +namespace OHOS { +namespace UserIAM { +namespace CoAuth { +class CoAuthCallbackProxy : public IRemoteProxy { +public: + explicit CoAuthCallbackProxy(const sptr& impl) + : IRemoteProxy(impl) {} + ~CoAuthCallbackProxy() override = default; + + virtual void OnFinish(uint32_t resultCode, std::vector &scheduleToken) override; + virtual void OnAcquireInfo(uint32_t acquire) override; +private: + bool SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply); + +private: + static inline BrokerDelegator delegator_; +}; +} // namespace CoAuth +} // namespace UserIAM +} // namespace OHOS +#endif // COAUTH_CALLBACK_PROXY_H \ No newline at end of file diff --git a/frameworks/kitsimpl/include/coauth_callback_stub.h b/frameworks/kitsimpl/include/coauth_callback_stub.h new file mode 100644 index 0000000..aae0218 --- /dev/null +++ b/frameworks/kitsimpl/include/coauth_callback_stub.h @@ -0,0 +1,45 @@ +/* + * 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 COAUTH_CALLBACK_STUB_H +#define COAUTH_CALLBACK_STUB_H + +#include +#include "icoauth_callback.h" +#include "coauth_callback.h" + +namespace OHOS { +namespace UserIAM { +namespace CoAuth { +class CoAuthCallbackStub : public IRemoteStub { +public: + explicit CoAuthCallbackStub(const std::shared_ptr& impl); + ~CoAuthCallbackStub() override = default; + + virtual void OnFinish(uint32_t resultCode, std::vector &scheduleToken) override; + virtual void OnAcquireInfo(uint32_t acquire) override; + + int32_t OnRemoteRequest( + uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; +private: + int32_t OnFinishStub(MessageParcel &data, MessageParcel &reply); + int32_t OnAcquireInfoStub(MessageParcel &data, MessageParcel &reply); + + std::shared_ptr callback_; +}; +} // namespace CoAuth +} // namespace UserIAM +} // namespace OHOS +#endif // COAUTH_CALLBACK_STUB_H \ No newline at end of file diff --git a/frameworks/kitsimpl/include/coauth_proxy.h b/frameworks/kitsimpl/include/coauth_proxy.h new file mode 100755 index 0000000..ad0bc59 --- /dev/null +++ b/frameworks/kitsimpl/include/coauth_proxy.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef COAUTH_PROXY_H +#define COAUTH_PROXY_H + +#include +#include +#include "i_coauth.h" +#include "auth_attributes.h" + +namespace OHOS { +namespace UserIAM { +namespace CoAuth { +class CoAuthProxy : public IRemoteProxy { +public: + explicit CoAuthProxy(const sptr& impl) + : IRemoteProxy(impl) {} + ~CoAuthProxy() override = default; + + virtual uint64_t Register(std::shared_ptr executorInfo, + const sptr &callback) override; + virtual void QueryStatus(AuthResPool::AuthExecutor &executorInfo, + const sptr &callback) override; + virtual void coAuth(uint64_t scheduleId, AuthInfo &authInfo, const sptr &callback) override; + virtual int32_t Cancel(uint64_t scheduleId) override; + virtual int32_t GetExecutorProp(AuthResPool::AuthAttributes &conditions, + std::shared_ptr values) override; + virtual void SetExecutorProp(AuthResPool::AuthAttributes &conditions, + const sptr &callback) override; + +private: + bool SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, bool isSync = true); + uint32_t WriteAuthExecutor(AuthResPool::AuthExecutor &executorInfo, MessageParcel &data); + +private: + static inline BrokerDelegator delegator_; +}; +} // namespace CoAuth +} // namespace UserIAM +} // namespace OHOS +#endif // COAUTH_PROXY_H \ No newline at end of file diff --git a/frameworks/kitsimpl/include/executor_callback_proxy.h b/frameworks/kitsimpl/include/executor_callback_proxy.h new file mode 100644 index 0000000..c9e64ef --- /dev/null +++ b/frameworks/kitsimpl/include/executor_callback_proxy.h @@ -0,0 +1,47 @@ +/* + * 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 EXECUTOR_CALLBACK_PROXY_H +#define EXECUTOR_CALLBACK_PROXY_H + +#include +#include "nocopyable.h" +#include "iexecutor_callback.h" + +namespace OHOS { +namespace UserIAM { +namespace AuthResPool { +class ExecutorCallbackProxy : public IRemoteProxy { +public: + explicit ExecutorCallbackProxy(const sptr& impl) + : IRemoteProxy(impl) {} + + ~ExecutorCallbackProxy() override = default; + virtual void OnMessengerReady(const sptr &messenger) override; + virtual int32_t OnBeginExecute(uint64_t scheduleId, std::vector &publicKey, + std::shared_ptr commandAttrs) override; + virtual int32_t OnEndExecute(uint64_t scheduleId, std::shared_ptr consumerAttr) override; + virtual int32_t OnSetProperty(std::shared_ptr properties) override; + virtual int32_t OnGetProperty(std::shared_ptr conditions, + std::shared_ptr values) override; +private: + bool SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply); + static inline BrokerDelegator delegator_; +}; +} // namespace AuthResPool +} // namespace UserIAM +} // namespace OHOS + +#endif // EXECUTOR_CALLBACK_PROXY_H \ No newline at end of file diff --git a/frameworks/kitsimpl/include/executor_callback_stub.h b/frameworks/kitsimpl/include/executor_callback_stub.h new file mode 100644 index 0000000..59bbf4e --- /dev/null +++ b/frameworks/kitsimpl/include/executor_callback_stub.h @@ -0,0 +1,48 @@ +/* + * 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 EXECUTOR_CALLBACK_STUB_H +#define EXECUTOR_CALLBACK_STUB_H + +#include "iexecutor_callback.h" +#include "executor_callback.h" +#include "iremote_stub.h" +namespace OHOS { +namespace UserIAM { +namespace AuthResPool { +class ExecutorCallbackStub : public IRemoteStub { +public: + ExecutorCallbackStub(const std::shared_ptr& impl); + ~ExecutorCallbackStub() override = default; + virtual void OnMessengerReady(const sptr &messenger) override; + virtual int32_t OnBeginExecute(uint64_t scheduleId, std::vector &publicKey, + std::shared_ptr commandAttrs) override; + virtual int32_t OnEndExecute(uint64_t scheduleId, std::shared_ptr consumerAttr) override; + virtual int32_t OnSetProperty(std::shared_ptr properties) override; + virtual int32_t OnGetProperty(std::shared_ptr conditions, + std::shared_ptr values) override; + int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; +private: + int32_t OnMessengerReadyStub(MessageParcel& data, MessageParcel& reply); + int32_t OnBeginExecuteStub(MessageParcel& data, MessageParcel& reply); + int32_t OnEndExecuteStub(MessageParcel& data, MessageParcel& reply); + int32_t OnGetPropertyStub(MessageParcel& data, MessageParcel& reply); + int32_t OnSetPropertyStub(MessageParcel& data, MessageParcel& reply); + std::shared_ptr callback_; +}; +} // namespace AuthResPool +} // namespace UserIAM +} // namespace OHOS +#endif // EXECUTOR_CALLBACK_STUB_H \ No newline at end of file diff --git a/frameworks/kitsimpl/include/executor_messenger_proxy.h b/frameworks/kitsimpl/include/executor_messenger_proxy.h new file mode 100644 index 0000000..c5d3d76 --- /dev/null +++ b/frameworks/kitsimpl/include/executor_messenger_proxy.h @@ -0,0 +1,45 @@ +/* + * 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 EXECUTOR_MESSENGER_PROXY_H +#define EXECUTOR_MESSENGER_PROXY_H + +#include +#include "nocopyable.h" +#include "iexecutor_messenger.h" + +namespace OHOS { +namespace UserIAM { +namespace AuthResPool { +class ExecutorMessengerProxy : public IRemoteProxy { +public: + explicit ExecutorMessengerProxy(const sptr& impl) + : IRemoteProxy(impl) {} + + ~ExecutorMessengerProxy() override = default; + virtual int32_t SendData(uint64_t scheduleId, uint64_t transNum, int32_t srcType, + int32_t dstType, std::shared_ptr msg) override; + virtual int32_t Finish(uint64_t scheduleId, int32_t srcType, int32_t resultCode, + std::shared_ptr finalResult) override; + +private: + bool SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply); + static inline BrokerDelegator delegator_; +}; +} // namespace AuthResPool +} // namespace UserIAM +} // namespace OHOS + +#endif // EXECUTOR_MESSENGER_PROXY_H \ No newline at end of file diff --git a/frameworks/kitsimpl/include/executor_messenger_stub.h b/frameworks/kitsimpl/include/executor_messenger_stub.h new file mode 100644 index 0000000..58939f3 --- /dev/null +++ b/frameworks/kitsimpl/include/executor_messenger_stub.h @@ -0,0 +1,39 @@ +/* + * 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 EXECUTOR_MESSENGER_STUB_H +#define EXECUTOR_MESSENGER_STUB_H + +#include "nocopyable.h" +#include "iexecutor_messenger.h" +#include "iremote_stub.h" + +namespace OHOS { +namespace UserIAM { +namespace AuthResPool { +class ExecutorMessengerStub : public IRemoteStub { +public: + ExecutorMessengerStub() = default; + ~ExecutorMessengerStub() override = default; + + int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; +private: + int32_t SendDataStub(MessageParcel& data, MessageParcel& reply); + int32_t FinishStub(MessageParcel& data, MessageParcel& reply); +}; +} // namespace AuthResPool +} // namespace UserIAM +} // namespace OHOS +#endif // EXECUTOR_MESSENGER_STUB_H \ No newline at end of file diff --git a/frameworks/kitsimpl/include/i_coauth.h b/frameworks/kitsimpl/include/i_coauth.h new file mode 100755 index 0000000..fb9d039 --- /dev/null +++ b/frameworks/kitsimpl/include/i_coauth.h @@ -0,0 +1,61 @@ +/* + * 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 I_COAUTH_H +#define I_COAUTH_H + +#include +#include +#include "auth_info.h" +#include "icoauth_callback.h" +#include "iset_prop_callback.h" +#include "auth_attributes.h" +#include "iexecutor_callback.h" +#include "iquery_callback.h" +#include "auth_executor.h" + +namespace OHOS { +namespace UserIAM { +namespace CoAuth { +class ICoAuth : public IRemoteBroker { +public: + + /* Message ID */ + enum { + COAUTH_EXECUTOR_REGIST = 0, + COAUTH_QUERY_STATUS, + COAUTH_SCHEDULE_REQUEST, + COAUTH_SCHEDULE_CANCEL, + COAUTH_GET_PROPERTY, + COAUTH_SET_PROPERTY + }; + + /* Business function */ + virtual uint64_t Register(std::shared_ptr executorInfo, + const sptr &callback) = 0; + virtual void QueryStatus(AuthResPool::AuthExecutor &executorInfo, + const sptr &callback) = 0; + virtual void coAuth(uint64_t scheduleId, AuthInfo &authInfo, const sptr &callback) = 0; + virtual int32_t Cancel(uint64_t scheduleId) = 0; + virtual int32_t GetExecutorProp(AuthResPool::AuthAttributes &conditions, + std::shared_ptr values) = 0; + virtual void SetExecutorProp(AuthResPool::AuthAttributes &conditions, const sptr &callback) = 0; + + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.CoAuth.ICoAuth"); +}; +} // namespace CoAuth +} // namespace UserIAM +} // namespace OHOS +#endif // I_COAUTH_H \ No newline at end of file diff --git a/frameworks/kitsimpl/include/icoauth_callback.h b/frameworks/kitsimpl/include/icoauth_callback.h new file mode 100644 index 0000000..cc7eb67 --- /dev/null +++ b/frameworks/kitsimpl/include/icoauth_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 ICOAUTH_CALLBACK_H +#define ICOAUTH_CALLBACK_H + +#include +#include +#include +#include + +namespace OHOS { +namespace UserIAM { +namespace CoAuth { +class ICoAuthCallback : public IRemoteBroker { +public: + + enum { + ONFINISH = 0, + ONACQUIREINFO + }; + + virtual void OnFinish(uint32_t resultCode, std::vector &scheduleToken) = 0; + virtual void OnAcquireInfo(uint32_t acquire) = 0; + + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.CoAuth.ICoAuthCallback"); +}; +} // namespace CoAuth +} // namespace UserIAM +} // namespace OHOS +#endif // ICOAUTH_CALLBACK_H diff --git a/frameworks/kitsimpl/include/iexecutor_callback.h b/frameworks/kitsimpl/include/iexecutor_callback.h new file mode 100755 index 0000000..32507eb --- /dev/null +++ b/frameworks/kitsimpl/include/iexecutor_callback.h @@ -0,0 +1,50 @@ +/* + * 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 IEXECUTOR_CALLBACK_H +#define IEXECUTOR_CALLBACK_H + +#include +#include +#include "iexecutor_messenger.h" + +namespace OHOS { +namespace UserIAM { +namespace AuthResPool { +class IExecutorCallback : public IRemoteBroker { +public: + virtual void OnMessengerReady(const sptr &messenger) = 0; + virtual int32_t OnBeginExecute(uint64_t scheduleId, std::vector &publicKey, + std::shared_ptr commandAttrs) = 0; + virtual int32_t OnEndExecute(uint64_t scheduleId, std::shared_ptr consumerAttr) = 0; + virtual int32_t OnSetProperty(std::shared_ptr properties) = 0; + virtual int32_t OnGetProperty(std::shared_ptr conditions, + std::shared_ptr values) = 0; + + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.UserIAM.AuthResPool.ExecutorCallback"); + + enum Message { + ON_MESSENGER_READY = 1, + ON_BEGIN_EXECUTE, + ON_END_EXECUTE, + ON_SET_PROPERTY, + ON_GET_PROPERTY + }; +}; +} // namespace AuthResPool +} // namespace UserIAM +} // namespace OHOS + +#endif // IEXECUTOR_CALLBACK_H \ No newline at end of file diff --git a/frameworks/kitsimpl/include/iexecutor_messenger.h b/frameworks/kitsimpl/include/iexecutor_messenger.h new file mode 100644 index 0000000..f013584 --- /dev/null +++ b/frameworks/kitsimpl/include/iexecutor_messenger.h @@ -0,0 +1,47 @@ +/* + * 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 IEXECUTOR_MESSENGER_H +#define IEXECUTOR_MESSENGER_H + +#include + +#include "coauth_info_define.h" +#include "auth_attributes.h" +#include "auth_message.h" +#include "coauth_hilog_wrapper.h" + +namespace OHOS { +namespace UserIAM { +namespace AuthResPool { +class IExecutorMessenger : public IRemoteBroker { +public: + /* Message ID */ + enum { + COAUTH_SEND_DATA = 0, + COAUTH_FINISH + }; + /* Business function */ + virtual int32_t SendData(uint64_t scheduleId, uint64_t transNum, int32_t srcType, + int32_t dstType, std::shared_ptr msg) = 0; + virtual int32_t Finish(uint64_t scheduleId, int32_t srcType, int32_t resultCode, + std::shared_ptr finalResult) = 0; + + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.UserIAM.AuthResPool.IExecutor_Messenger"); +}; +} // namespace IExecutor_Messenger +} // namespace UserIAM +} // namespace OHOS +#endif // IEXECUTOR_MESSENGER_H \ No newline at end of file diff --git a/frameworks/kitsimpl/include/iquery_callback.h b/frameworks/kitsimpl/include/iquery_callback.h new file mode 100644 index 0000000..660be30 --- /dev/null +++ b/frameworks/kitsimpl/include/iquery_callback.h @@ -0,0 +1,39 @@ +/* + * 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 IQUERY_CALLBACK_H +#define IQUERY_CALLBACK_H + +#include +#include +#include "query_callback.h" + +namespace OHOS { +namespace UserIAM { +namespace AuthResPool { +class IQueryCallback : public IRemoteBroker { +public: + enum { + COAUTH_QUERY_RESULT = 0 + }; + + virtual void OnResult(uint32_t resultCode) = 0; + + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.CoAuth.IQueryCallback"); +}; +} // namespace AuthResPool +} // namespace UserIAM +} // namespace OHOS +#endif // IQUERY_CALLBACK_H diff --git a/frameworks/kitsimpl/include/iset_prop_callback.h b/frameworks/kitsimpl/include/iset_prop_callback.h new file mode 100644 index 0000000..cf833e0 --- /dev/null +++ b/frameworks/kitsimpl/include/iset_prop_callback.h @@ -0,0 +1,39 @@ +/* + * 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 ISET_PROP_CALLBACK_H +#define ISET_PROP_CALLBACK_H + +#include +#include + +namespace OHOS { +namespace UserIAM { +namespace CoAuth { +class ISetPropCallback : public IRemoteBroker { +public: + + enum { + ONRESULT = 0, + }; + + virtual void OnResult(uint32_t result, std::vector &extraInfo) = 0; + + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.CoAuth.ISetPropCallback"); +}; +} // namespace CoAuth +} // namespace UserIAM +} // namespace OHOS +#endif // ISET_PROP_CALLBACK_H diff --git a/frameworks/kitsimpl/include/query_callback_proxy.h b/frameworks/kitsimpl/include/query_callback_proxy.h new file mode 100644 index 0000000..91654f4 --- /dev/null +++ b/frameworks/kitsimpl/include/query_callback_proxy.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 QUERY_CALLBACK_PROXY_H +#define QUERY_CALLBACK_PROXY_H + +#include +#include "iquery_callback.h" + +namespace OHOS { +namespace UserIAM { +namespace AuthResPool { +class QueryCallbackProxy : public IRemoteProxy { +public: + explicit QueryCallbackProxy(const sptr& impl) + : IRemoteProxy(impl) {} + ~QueryCallbackProxy() override = default; + + virtual void OnResult(uint32_t resultCode) override; + +private: + bool SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply); + +private: + static inline BrokerDelegator delegator_; +}; +} // namespace AuthResPool +} // namespace UserIAM +} // namespace OHOS +#endif // QUERY_CALLBACK_PROXY_H \ No newline at end of file diff --git a/frameworks/kitsimpl/include/query_callback_stub.h b/frameworks/kitsimpl/include/query_callback_stub.h new file mode 100644 index 0000000..133d342 --- /dev/null +++ b/frameworks/kitsimpl/include/query_callback_stub.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 QUERY_CALLBACK_STUB_H +#define QUERY_CALLBACK_STUB_H + +#include +#include "iquery_callback.h" + +namespace OHOS { +namespace UserIAM { +namespace AuthResPool { +class QueryCallbackStub : public IRemoteStub { +public: + explicit QueryCallbackStub(const std::shared_ptr& impl); + ~QueryCallbackStub() override = default; + + virtual void OnResult(uint32_t resultCode) override; + + int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; + +private: + int32_t OnResultStub(MessageParcel& data, MessageParcel& reply); + + std::shared_ptr callback_; +}; +} // namespace AuthResPool +} // namespace UserIAM +} // namespace OHOS +#endif // QUERY_CALLBACK_STUB_H \ No newline at end of file diff --git a/frameworks/kitsimpl/include/set_prop_callback_proxy.h b/frameworks/kitsimpl/include/set_prop_callback_proxy.h new file mode 100644 index 0000000..6f3acb6 --- /dev/null +++ b/frameworks/kitsimpl/include/set_prop_callback_proxy.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 SET_PROP_CALLBACK_PROXY_H +#define SET_PROP_CALLBACK_PROXY_H + +#include +#include "iset_prop_callback.h" + +namespace OHOS { +namespace UserIAM { +namespace CoAuth { +class SetPropCallbackProxy : public IRemoteProxy { +public: + explicit SetPropCallbackProxy(const sptr& impl) + : IRemoteProxy(impl) {} + ~SetPropCallbackProxy() override = default; + virtual void OnResult(uint32_t result, std::vector &extraInfo) override; + +private: + bool SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply); + +private: + static inline BrokerDelegator delegator_; +}; +} // namespace CoAuth +} // namespace UserIAM +} // namespace OHOS +#endif // SET_PROP_CALLBACK_PROXY_H \ No newline at end of file diff --git a/frameworks/kitsimpl/include/set_prop_callback_stub.h b/frameworks/kitsimpl/include/set_prop_callback_stub.h new file mode 100644 index 0000000..88aafe4 --- /dev/null +++ b/frameworks/kitsimpl/include/set_prop_callback_stub.h @@ -0,0 +1,44 @@ +/* + * 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 SET_PROP_CALLBACK_STUB_H +#define SET_PROP_CALLBACK_STUB_H + +#include +#include "iset_prop_callback.h" +#include "set_prop_callback.h" +#include "coauth_info_define.h" + +namespace OHOS { +namespace UserIAM { +namespace CoAuth { +class SetPropCallbackStub : public IRemoteStub { +public: + explicit SetPropCallbackStub(const std::shared_ptr& impl); + ~SetPropCallbackStub() override = default; + + virtual void OnResult(uint32_t result, std::vector &extraInfo) override; + + int32_t OnRemoteRequest( + uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; +private: + int32_t OnResultStub(MessageParcel &data, MessageParcel &reply); + + std::shared_ptr callback_; +}; +} // namespace CoAuth +} // namespace UserIAM +} // namespace OHOS +#endif // SET_PROP_CALLBACK_STUB_H \ No newline at end of file diff --git a/frameworks/kitsimpl/src/auth_executor_registry.cpp b/frameworks/kitsimpl/src/auth_executor_registry.cpp new file mode 100644 index 0000000..ec1d48c --- /dev/null +++ b/frameworks/kitsimpl/src/auth_executor_registry.cpp @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include "query_callback_stub.h" +#include "executor_callback_stub.h" +#include "auth_executor_registry.h" + +namespace OHOS { +namespace UserIAM { +namespace AuthResPool { +AuthExecutorRegistry::AuthExecutorRegistry() = default; +AuthExecutorRegistry::~AuthExecutorRegistry() = default; + +sptr AuthExecutorRegistry::GetProxy() +{ + if (proxy_ != nullptr) { + return proxy_; + } + + sptr sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (sam == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "Failed to get system ability manager"); + return nullptr; + } + sptr obj = sam->CheckSystemAbility(COOPERATION_AUTHENTICATION_SERVICE_SA_ID); + if (obj == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "Failed to get coauth service"); + return nullptr; + } + sptr dr = new AuthExecutorRegistryDeathRecipient(); + if ((obj->IsProxyObject()) && (!obj->AddDeathRecipient(dr))) { + COAUTH_HILOGE(MODULE_INNERKIT, "Failed to add death recipient"); + return nullptr; + } + + proxy_ = iface_cast(obj); + deathRecipient_ = dr; + COAUTH_HILOGI(MODULE_INNERKIT, "Succeed to connect coauth service"); + return proxy_; +} + +void AuthExecutorRegistry::ResetProxy(const wptr& remote) +{ + std::lock_guard lock(mutex_); + auto serviceRemote = proxy_->AsObject(); + if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) { + serviceRemote->RemoveDeathRecipient(deathRecipient_); + proxy_ = nullptr; + } +} + +uint64_t AuthExecutorRegistry::Register(std::shared_ptr executorInfo, + std::shared_ptr callback) +{ + COAUTH_HILOGD(MODULE_INNERKIT, "Register enter"); + if (executorInfo == nullptr || callback == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "Register failed,executorInfo or callback is nullptr"); + return FAIL; + } + auto proxy = GetProxy(); + if (proxy == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "GetProxy is nullptr"); + return FAIL; + } + sptr iExecutorCallback = new ExecutorCallbackStub(callback); + return proxy->Register(executorInfo, iExecutorCallback); +} + +void AuthExecutorRegistry::QueryStatus(AuthExecutor &executorInfo, std::shared_ptr callback) +{ + COAUTH_HILOGD(MODULE_INNERKIT, "QueryStatus enter"); + if (callback == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "QueryStatus failed, callback is nullptr"); + return; + } + auto proxy = GetProxy(); + if (proxy == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "GetProxy is nullptr"); + return; + } + sptr iQueryCallback = new QueryCallbackStub(callback); + return proxy->QueryStatus(executorInfo, iQueryCallback); +} + +void AuthExecutorRegistry::AuthExecutorRegistryDeathRecipient::OnRemoteDied(const wptr& remote) +{ + if (remote == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "OnRemoteDied failed, remote is nullptr"); + return; + } + + AuthExecutorRegistry::GetInstance().ResetProxy(remote); + COAUTH_HILOGE(MODULE_INNERKIT, "CoAuthDeathRecipient::Recv death notice."); +} +} // namespace AuthResPool +} // namespace UserIAM +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/kitsimpl/src/auth_info.cpp b/frameworks/kitsimpl/src/auth_info.cpp new file mode 100644 index 0000000..72066fb --- /dev/null +++ b/frameworks/kitsimpl/src/auth_info.cpp @@ -0,0 +1,47 @@ +/* + * 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 "coauth_info_define.h" +#include "auth_info.h" + +namespace OHOS { +namespace UserIAM { +namespace CoAuth { +int32_t AuthInfo::GetPkgName(std::string &value) +{ + value = pkgName_; + return SUCCESS; +} + +int32_t AuthInfo::GetCallerUid(uint64_t &value) +{ + value = callerUid_; + return SUCCESS; +} + +int32_t AuthInfo::SetPkgName(std::string value) +{ + pkgName_ = value; + return SUCCESS; +} + +int32_t AuthInfo::SetCallerUid(uint64_t value) +{ + callerUid_= value; + return SUCCESS; +} +} // namespace CoAuth +} // namespace UserIAM +} // namespace OHOS diff --git a/frameworks/kitsimpl/src/coauth.cpp b/frameworks/kitsimpl/src/coauth.cpp new file mode 100755 index 0000000..d095f4f --- /dev/null +++ b/frameworks/kitsimpl/src/coauth.cpp @@ -0,0 +1,144 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include "coauth_hilog_wrapper.h" +#include "coauth_callback_stub.h" +#include "set_prop_callback_stub.h" +#include "co_auth.h" + +namespace OHOS { +namespace UserIAM { +namespace CoAuth { +CoAuth::CoAuth() = default; +CoAuth::~CoAuth() = default; + +sptr CoAuth::GetProxy() +{ + if (proxy_ != nullptr) { + return proxy_; + } + + sptr sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (sam == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "Failed to get system ability manager"); + return nullptr; + } + sptr obj = sam->CheckSystemAbility(COOPERATION_AUTHENTICATION_SERVICE_SA_ID); + if (obj == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "Failed to get coauth manager service"); + return nullptr; + } + sptr dr = new CoAuthDeathRecipient(); + if ((obj->IsProxyObject()) && (!obj->AddDeathRecipient(dr))) { + COAUTH_HILOGE(MODULE_INNERKIT, "Failed to add death recipient"); + return nullptr; + } + + proxy_ = iface_cast(obj); + deathRecipient_ = dr; + COAUTH_HILOGE(MODULE_INNERKIT, "Succeed to connect coauth manager service"); + return proxy_; +} + +void CoAuth::ResetProxy(const wptr& remote) +{ + std::lock_guard lock(mutex_); + if (proxy_ == nullptr) { + return; + } + + auto serviceRemote = proxy_->AsObject(); + if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) { + serviceRemote->RemoveDeathRecipient(deathRecipient_); + proxy_ = nullptr; + } +} + +void CoAuth::coAuth(uint64_t scheduleId, AuthInfo &authInfo, std::shared_ptr callback) +{ + COAUTH_HILOGD(MODULE_INNERKIT, "coAuth enter"); + if (callback == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "coAuth failed, callback is nullptr"); + return ; + } + auto proxy = GetProxy(); + if (proxy == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "coAuth failed, remote is nullptr"); + return; + } + + sptr icoAuthCallback = new CoAuthCallbackStub(callback); + if (icoAuthCallback == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "coAuth failed, icoAuthCallback is nullptr"); + return ; + } + return proxy->coAuth(scheduleId, authInfo, icoAuthCallback); +} + +int32_t CoAuth::Cancel(uint64_t scheduleId) +{ + COAUTH_HILOGD(MODULE_INNERKIT, "CoAuth Cancel enter"); + auto proxy = GetProxy(); + if (proxy == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "Cancel failed, proxy is nullptr"); + return FAIL; + } + + return proxy->Cancel(scheduleId); +} + +int32_t CoAuth::GetExecutorProp(AuthResPool::AuthAttributes &conditions, + std::shared_ptr values) +{ + COAUTH_HILOGD(MODULE_INNERKIT, "CoAuth: GetExecutorProp enter"); + auto proxy = GetProxy(); + if (proxy == nullptr || values == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "GetExecutorProp failed, proxy or values is nullptr"); + return FAIL; + } + + return proxy->GetExecutorProp(conditions, values); +} + +void CoAuth::SetExecutorProp(AuthResPool::AuthAttributes &conditions, std::shared_ptr callback) +{ + COAUTH_HILOGD(MODULE_INNERKIT, "CoAuth: SetExecutorProp enter"); + auto proxy = GetProxy(); + if (proxy == nullptr || callback == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "SetExecutorProp failed, proxy or callback is nullptr"); + return; + } + + sptr iSetExecutorCallback = new SetPropCallbackStub(callback); + return proxy->SetExecutorProp(conditions, iSetExecutorCallback); +} + + +void CoAuth::CoAuthDeathRecipient::OnRemoteDied(const wptr& remote) +{ + if (remote == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "OnRemoteDied failed, remote is nullptr"); + return; + } + + CoAuth::GetInstance().ResetProxy(remote); + COAUTH_HILOGE(MODULE_INNERKIT, "CoAuthDeathRecipient::Recv death notice."); +} +} // namespace CoAuth +} // namespace UserIAM +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/kitsimpl/src/coauth_callback_proxy.cpp b/frameworks/kitsimpl/src/coauth_callback_proxy.cpp new file mode 100644 index 0000000..781795a --- /dev/null +++ b/frameworks/kitsimpl/src/coauth_callback_proxy.cpp @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include "coauth_hilog_wrapper.h" +#include "icoauth_callback.h" +#include "coauth_callback_proxy.h" + +namespace OHOS { +namespace UserIAM { +namespace CoAuth { +void CoAuthCallbackProxy::OnFinish(uint32_t resultCode, std::vector &scheduleToken) +{ + MessageParcel data; + MessageParcel reply; + + if (!data.WriteUint32(resultCode)) { + COAUTH_HILOGE(MODULE_INNERKIT, "failed to WriteUint32(resultCode)."); + } + + if (!data.WriteUInt8Vector(scheduleToken)) { + COAUTH_HILOGE(MODULE_INNERKIT, "failed to WriteUInt8Vector(scheduleToken)."); + } + + bool ret = SendRequest(static_cast(ICoAuthCallback::ONFINISH), data, reply); + if (ret) { + COAUTH_HILOGI(MODULE_INNERKIT, "result = %{public}d", ret); + } +} + +void CoAuthCallbackProxy::OnAcquireInfo(uint32_t acquire) +{ + MessageParcel data; + if (!data.WriteUint32(acquire)) { + COAUTH_HILOGE(MODULE_INNERKIT, "failed to WriteUint32(acquire)."); + } + + MessageParcel reply; + bool ret = SendRequest(static_cast(ICoAuthCallback::ONACQUIREINFO), data, reply); + if (ret) { + COAUTH_HILOGI(MODULE_INNERKIT, "result = %{public}d", ret); + } +} + +bool CoAuthCallbackProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply) +{ + sptr remote = Remote(); + if (remote == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "failed to get remote."); + return false; + } + MessageOption option(MessageOption::TF_SYNC); + int32_t result = remote->SendRequest(code, data, reply, option); + if (result != OHOS::NO_ERROR) { + COAUTH_HILOGE(MODULE_INNERKIT, "failed to SendRequest.result = %{public}d", result); + return false; + } + return true; +} +} // namespace CoAuth +} // namespace UserIAM +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/kitsimpl/src/coauth_callback_stub.cpp b/frameworks/kitsimpl/src/coauth_callback_stub.cpp new file mode 100644 index 0000000..787c1af --- /dev/null +++ b/frameworks/kitsimpl/src/coauth_callback_stub.cpp @@ -0,0 +1,78 @@ +/* + * 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 "coauth_hilog_wrapper.h" +#include "coauth_info_define.h" +#include "coauth_callback_stub.h" + +namespace OHOS { +namespace UserIAM { +namespace CoAuth { +CoAuthCallbackStub::CoAuthCallbackStub(const std::shared_ptr& impl) +{ + callback_ = impl; +} + +int32_t CoAuthCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, + MessageParcel &reply, MessageOption &option) +{ + switch (code) { + case static_cast(ICoAuthCallback::ONFINISH): + return OnFinishStub(data, reply); + case static_cast(ICoAuthCallback::ONACQUIREINFO): + return OnAcquireInfoStub(data, reply); + default: + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } +} + +int32_t CoAuthCallbackStub::OnFinishStub(MessageParcel &data, MessageParcel &reply) +{ + uint32_t resultCode = data.ReadUint32(); + std::vector scheduleToken; + data.ReadUInt8Vector(&scheduleToken); + + OnFinish(resultCode, scheduleToken); + return SUCCESS; +} + +int32_t CoAuthCallbackStub::OnAcquireInfoStub(MessageParcel &data, MessageParcel &reply) +{ + uint32_t acquire = data.ReadUint32(); + + OnAcquireInfo(acquire); + return SUCCESS; +} + +void CoAuthCallbackStub::OnFinish(uint32_t resultCode, std::vector &scheduleToken) +{ + if (callback_ == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "CoAuthCallback is null."); + } else { + callback_->OnFinish(resultCode, scheduleToken); + } +} + +void CoAuthCallbackStub::OnAcquireInfo(uint32_t acquire) +{ + if (callback_ == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "CoAuthCallback is null."); + } else { + callback_->OnAcquireInfo(acquire); + } +} +} // namespace CoAuth +} // namespace UserIAM +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/kitsimpl/src/coauth_proxy.cpp b/frameworks/kitsimpl/src/coauth_proxy.cpp new file mode 100755 index 0000000..b835baa --- /dev/null +++ b/frameworks/kitsimpl/src/coauth_proxy.cpp @@ -0,0 +1,285 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include "coauth_hilog_wrapper.h" +#include "coauth_proxy.h" + +namespace OHOS { +namespace UserIAM { +namespace CoAuth { +uint32_t CoAuthProxy::WriteAuthExecutor(AuthResPool::AuthExecutor &executorInfo, MessageParcel &data) +{ + AuthType authType; + executorInfo.GetAuthType(authType); + if (!data.WriteInt32(authType)) { + return FAIL; + } + COAUTH_HILOGD(MODULE_INNERKIT, "WriteInt32,authType:%{public}d", authType); + + uint64_t authAbility; + executorInfo.GetAuthAbility(authAbility); + if (!data.WriteUint64(authAbility)) { + return FAIL; + } + COAUTH_HILOGD(MODULE_INNERKIT, "WriteUint64,authAbility:%{public}llu", authAbility); + + ExecutorSecureLevel executorSecLevel; + executorInfo.GetExecutorSecLevel(executorSecLevel); + if (!data.WriteInt32(executorSecLevel)) { + return FAIL; + } + COAUTH_HILOGD(MODULE_INNERKIT, "WriteInt32,executorSecLevel:%{public}d", executorSecLevel); + + ExecutorType executorType; + executorInfo.GetExecutorType(executorType); + if (!data.WriteInt32(executorType)) { + return FAIL; + } + COAUTH_HILOGD(MODULE_INNERKIT, "WriteInt32,executorType:%{public}d", executorType); + + std::vector publicKey; + executorInfo.GetPublicKey(publicKey); + if (!data.WriteUInt8Vector(publicKey)) { + return FAIL; + } + + std::vector deviceId; + executorInfo.GetDeviceId(deviceId); + if (!data.WriteUInt8Vector(deviceId)) { + return FAIL; + } + return SUCCESS; +} + +uint64_t CoAuthProxy::Register(std::shared_ptr executorInfo, + const sptr &callback) +{ + COAUTH_HILOGD(MODULE_INNERKIT, "Register enter"); + if (executorInfo == nullptr || callback == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "Register failed,executorInfo or callback is nullptr"); + return FAIL; + } + MessageParcel data; + MessageParcel reply; + uint64_t result = 0; + + if (!data.WriteInterfaceToken(CoAuthProxy::GetDescriptor())) { + COAUTH_HILOGE(MODULE_INNERKIT, "write descriptor failed!"); + return FAIL; + } + + if (WriteAuthExecutor(*executorInfo, data) == FAIL) { + COAUTH_HILOGE(MODULE_INNERKIT, "write executorInfo failed!"); + return FAIL; + } + if (!data.WriteRemoteObject(callback->AsObject())) { + COAUTH_HILOGD(MODULE_INNERKIT, "failed to WriteRemoteObject(callback)."); + return FAIL; + } + + bool ret = SendRequest(static_cast(ICoAuth::COAUTH_EXECUTOR_REGIST), data, reply); + if (ret) { + result = reply.ReadUint64(); + COAUTH_HILOGD(MODULE_INNERKIT, "result = %{public}llu", result); + } + return result; +} + +void CoAuthProxy::QueryStatus(AuthResPool::AuthExecutor &executorInfo, + const sptr &callback) +{ + MessageParcel data; + MessageParcel reply; + + if (!data.WriteInterfaceToken(CoAuthProxy::GetDescriptor())) { + COAUTH_HILOGE(MODULE_INNERKIT, "write descriptor failed!"); + return; + } + if (WriteAuthExecutor(executorInfo, data) == FAIL) { + COAUTH_HILOGE(MODULE_INNERKIT, "write executorInfo failed!"); + return ; + } + + if (!data.WriteRemoteObject(callback->AsObject())) { + COAUTH_HILOGE(MODULE_INNERKIT, "failed to WriteRemoteObject(callback)."); + return; + } + + bool ret = SendRequest(static_cast(ICoAuth::COAUTH_QUERY_STATUS), data, reply, false); + if (ret) { + COAUTH_HILOGD(MODULE_INNERKIT, "ret = %{public}d", ret); + } + return; +} + +void CoAuthProxy::coAuth(uint64_t scheduleId, AuthInfo &authInfo, const sptr &callback) +{ + MessageParcel data; + MessageParcel reply; + + if (!data.WriteInterfaceToken(CoAuthProxy::GetDescriptor())) { + COAUTH_HILOGE(MODULE_INNERKIT, "write descriptor failed!"); + } + + std::string GetPkgName; + authInfo.GetPkgName(GetPkgName); + if (!data.WriteString(GetPkgName)) { + COAUTH_HILOGD(MODULE_INNERKIT, "WriteString,GetPkgName failed"); + } + uint64_t GetCallerUid; + authInfo.GetCallerUid(GetCallerUid); + data.WriteUint64(GetCallerUid); + COAUTH_HILOGD(MODULE_INNERKIT, "WriteUint64,GetCallerUid:%{public}llu", GetCallerUid); + + if (!data.WriteUint64(scheduleId)) { + COAUTH_HILOGE(MODULE_INNERKIT, "failed to WriteUint64(scheduleId)."); + } else { + COAUTH_HILOGD(MODULE_INNERKIT, "WriteUint64,scheduleId:%{public}llu", scheduleId); + } + + if (!data.WriteRemoteObject(callback->AsObject())) { + COAUTH_HILOGD(MODULE_INNERKIT, "failed to WriteRemoteObject(callback)."); + } + + bool ret = SendRequest(static_cast(ICoAuth::COAUTH_SCHEDULE_REQUEST), data, reply, false); + if (ret) { + COAUTH_HILOGD(MODULE_INNERKIT, "ret = %{public}d", ret); + } else { + COAUTH_HILOGE(MODULE_INNERKIT, "failed to SendRequest."); + } +} + +int32_t CoAuthProxy::Cancel(uint64_t scheduleId) +{ + MessageParcel data; + COAUTH_HILOGD(MODULE_INNERKIT, "CoauthProxy: Cancel enter"); + if (!data.WriteInterfaceToken(CoAuthProxy::GetDescriptor())) { + COAUTH_HILOGE(MODULE_INNERKIT, "write descriptor failed!"); + return FAIL; + } + if (!data.WriteUint64(scheduleId)) { + COAUTH_HILOGE(MODULE_INNERKIT, "failed to WriteUint64(scheduleId)."); + return FAIL; + } + + MessageParcel reply; + int32_t result = 0; + bool ret = SendRequest(static_cast(ICoAuth::COAUTH_SCHEDULE_CANCEL), data, reply); + if (ret) { + result = reply.ReadInt32(); + COAUTH_HILOGD(MODULE_INNERKIT, "result = %{public}d", result); + } + return SUCCESS; +} + +int32_t CoAuthProxy::GetExecutorProp(AuthResPool::AuthAttributes &conditions, + std::shared_ptr values) +{ + COAUTH_HILOGD(MODULE_INNERKIT, "CoauthProxy: GetExecutorProp enter"); + int32_t result = SUCCESS; + MessageParcel data; + MessageParcel reply; + + if (!data.WriteInterfaceToken(CoAuthProxy::GetDescriptor())) { + COAUTH_HILOGE(MODULE_INNERKIT, "write descriptor failed!"); + return FAIL; + } + + if (values == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "failed to get valuesptr."); + return FAIL; + } + std::vector buffer; + if (conditions.Pack(buffer)) { + COAUTH_HILOGE(MODULE_INNERKIT, "conditions pack buffer failed!"); + return FAIL; + } + if (!data.WriteUInt8Vector(buffer)) { + COAUTH_HILOGE(MODULE_INNERKIT, "data WriteUInt8Vector buffer failed!"); + return FAIL; + } + if (values->Pack(buffer)) { + COAUTH_HILOGE(MODULE_INNERKIT, "values pack buffer failed!"); + return FAIL; + } + if (!data.WriteUInt8Vector(buffer)) { + COAUTH_HILOGE(MODULE_INNERKIT, "data WriteUInt8Vector buffer failed!"); + return FAIL; + } + + bool ret = SendRequest(static_cast(ICoAuth::COAUTH_GET_PROPERTY), data, reply); + if (!ret) { + COAUTH_HILOGE(MODULE_INNERKIT, "SendRequest is failed, error code: %d", ret); + return FAIL; + } + if (!reply.ReadInt32(result)) { + COAUTH_HILOGE(MODULE_INNERKIT, "Readback fail!"); + return FAIL; + } + return result; +} + +void CoAuthProxy::SetExecutorProp(AuthResPool::AuthAttributes &conditions, + const sptr &callback) +{ + COAUTH_HILOGD(MODULE_INNERKIT, "CoauthProxy: SetExecutorProp enter"); + int result = false; + MessageParcel data; + MessageParcel reply; + + if (!data.WriteInterfaceToken(CoAuthProxy::GetDescriptor())) { + COAUTH_HILOGE(MODULE_INNERKIT, "write descriptor failed!"); + return ; + } + std::vector buffer; + if (conditions.Pack(buffer)) { + COAUTH_HILOGE(MODULE_INNERKIT, "conditions pack buffer failed!"); + return; + } + if (!data.WriteUInt8Vector(buffer)) { + COAUTH_HILOGE(MODULE_INNERKIT, "data WriteUInt8Vector buffer failed!"); + return; + } + if (!data.WriteRemoteObject(callback->AsObject())) { + COAUTH_HILOGE(MODULE_INNERKIT, "failed to WriteRemoteObject(callback)."); + } + bool ret = SendRequest(static_cast(ICoAuth::COAUTH_SET_PROPERTY), data, reply, false); + if (ret) { + COAUTH_HILOGD(MODULE_INNERKIT, "ret = %{public}d", result); + } else { + COAUTH_HILOGE(MODULE_INNERKIT, "failed to SendRequest."); + } +} + +bool CoAuthProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, bool isSync) +{ + COAUTH_HILOGD(MODULE_INNERKIT, "CoauthProxy: SendRequest enter"); + sptr remote = Remote(); + if (remote == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "failed to get remote."); + return false; + } + MessageOption option(isSync ? MessageOption::TF_SYNC : MessageOption::TF_ASYNC); + int32_t result = remote->SendRequest(code, data, reply, option); + if (result != OHOS::NO_ERROR) { + COAUTH_HILOGE(MODULE_INNERKIT, "failed to SendRequest.result = %{public}d", result); + return false; + } + return true; +} +} // namespace CoAuth +} // namespace UserIAM +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/kitsimpl/src/executor_callback_proxy.cpp b/frameworks/kitsimpl/src/executor_callback_proxy.cpp new file mode 100644 index 0000000..16f0043 --- /dev/null +++ b/frameworks/kitsimpl/src/executor_callback_proxy.cpp @@ -0,0 +1,179 @@ +/* + * 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 "executor_callback_proxy.h" +#include "coauth_hilog_wrapper.h" +#include "message_parcel.h" + +namespace OHOS { +namespace UserIAM { +namespace AuthResPool { +void ExecutorCallbackProxy::OnMessengerReady(const sptr &messenger) +{ + COAUTH_HILOGD(MODULE_INNERKIT, "ExecutorCallbackProxy OnMessengerReady!"); + MessageParcel data; + MessageParcel reply; + if (!data.WriteInterfaceToken(ExecutorCallbackProxy::GetDescriptor())) { + COAUTH_HILOGE(MODULE_INNERKIT, "write descriptor failed!"); + return ; + } + + if (!data.WriteRemoteObject(messenger.GetRefPtr()->AsObject())) { + COAUTH_HILOGE(MODULE_INNERKIT, "write RemoteObject failed!"); + return ; + } + bool ret = SendRequest(static_cast(IExecutorCallback::ON_MESSENGER_READY), data, reply); + if (ret) { + COAUTH_HILOGI(MODULE_INNERKIT, "ret = %{public}d", ret); + } + return ; +} + +int32_t ExecutorCallbackProxy::OnBeginExecute(uint64_t scheduleId, std::vector &publicKey, + std::shared_ptr commandAttrs) +{ + MessageParcel data; + MessageParcel reply; + int32_t result = 0; + if (!data.WriteInterfaceToken(ExecutorCallbackProxy::GetDescriptor())) { + COAUTH_HILOGE(MODULE_INNERKIT, "write descriptor failed!"); + return FAIL; + } + + if (!data.WriteUint64(scheduleId)) { + return FAIL; + } + if (!data.WriteUInt8Vector(publicKey)) { + return FAIL; + } + std::vector buffer; + if (commandAttrs->Pack(buffer)) { + return FAIL; + } + if (!data.WriteUInt8Vector(buffer)) { + return FAIL; + } + + bool ret = SendRequest(static_cast(IExecutorCallback::ON_BEGIN_EXECUTE), data, reply); + if (ret) { + result = reply.ReadInt32(); + COAUTH_HILOGI(MODULE_INNERKIT, "result = %{public}d", result); + } + return SUCCESS; +} +int32_t ExecutorCallbackProxy::OnEndExecute(uint64_t scheduleId, std::shared_ptr consumerAttr) +{ + MessageParcel data; + MessageParcel reply; + int32_t result = 0; + if (!data.WriteInterfaceToken(ExecutorCallbackProxy::GetDescriptor())) { + COAUTH_HILOGE(MODULE_INNERKIT, "write descriptor failed!"); + return FAIL; + } + + if (!data.WriteUint64(scheduleId)) { + return FAIL; + } + + std::vector buffer; + if (consumerAttr->Pack(buffer)) { + return FAIL; + } + if (!data.WriteUInt8Vector(buffer)) { + return FAIL; + } + + bool ret = SendRequest(static_cast(IExecutorCallback::ON_END_EXECUTE), data, reply); + if (ret) { + result = reply.ReadInt32(); + COAUTH_HILOGI(MODULE_INNERKIT, "result = %{public}d", result); + } + return SUCCESS; +} + +int32_t ExecutorCallbackProxy::OnSetProperty(std::shared_ptr properties) +{ + MessageParcel data; + MessageParcel reply; + int32_t result = 0; + if (!data.WriteInterfaceToken(ExecutorCallbackProxy::GetDescriptor())) { + COAUTH_HILOGE(MODULE_INNERKIT, "write descriptor failed!"); + return FAIL; + } + std::vector buffer; + if (properties->Pack(buffer)) { + return FAIL; + } + if (!data.WriteUInt8Vector(buffer)) { + return FAIL; + } + bool ret = SendRequest(static_cast(IExecutorCallback::ON_SET_PROPERTY), data, reply); + if (ret) { + result = reply.ReadInt32(); + COAUTH_HILOGI(MODULE_INNERKIT, "result = %{public}d", result); + } + return SUCCESS; +} + +int32_t ExecutorCallbackProxy::OnGetProperty(std::shared_ptr conditions, + std::shared_ptr values) +{ + MessageParcel data; + MessageParcel reply; + int32_t result = 0; + if (!data.WriteInterfaceToken(ExecutorCallbackProxy::GetDescriptor())) { + COAUTH_HILOGE(MODULE_INNERKIT, "write descriptor failed!"); + return FAIL; + } + + std::vector buffer; + if (conditions->Pack(buffer)) { + return FAIL; + } + if (!data.WriteUInt8Vector(buffer)) { + return FAIL; + } + if (values->Pack(buffer)) { + return FAIL; + } + if (!data.WriteUInt8Vector(buffer)) { + return FAIL; + } + bool ret = SendRequest(static_cast(IExecutorCallback::ON_GET_PROPERTY), data, reply); + if (ret) { + result = reply.ReadInt32(); + COAUTH_HILOGI(MODULE_INNERKIT, "result = %{public}d", result); + } + return result; +} + + +bool ExecutorCallbackProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply) +{ + sptr remote = Remote(); + if (remote == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "failed to get remote."); + return false; + } + MessageOption option(MessageOption::TF_SYNC); + int32_t result = remote->SendRequest(code, data, reply, option); + if (result != OHOS::NO_ERROR) { + COAUTH_HILOGE(MODULE_INNERKIT, "failed to SendRequest.result = %{public}d", result); + return false; + } + return true; +} +} +} +} \ No newline at end of file diff --git a/frameworks/kitsimpl/src/executor_callback_stub.cpp b/frameworks/kitsimpl/src/executor_callback_stub.cpp new file mode 100644 index 0000000..6731eb3 --- /dev/null +++ b/frameworks/kitsimpl/src/executor_callback_stub.cpp @@ -0,0 +1,185 @@ +/* + * 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 "executor_callback_stub.h" +#include "message_parcel.h" + +namespace OHOS { +namespace UserIAM { +namespace AuthResPool { +const std::string PERMISSION_AUTH_RESPOOL = "ohos.permission.ACCESS_AUTH_RESPOOL"; +const std::string PERMISSION_ACCESS_COAUTH = "ohos.permission.ACCESS_COAUTH"; + +ExecutorCallbackStub::ExecutorCallbackStub(const std::shared_ptr& impl) +{ + callback_ = impl; +} + +int32_t ExecutorCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, + MessageParcel &reply, MessageOption &option) +{ + COAUTH_HILOGD(MODULE_INNERKIT, "ExecutorCallbackStub::OnRemoteRequest!"); + std::u16string descripter = ExecutorCallbackStub::GetDescriptor(); + std::u16string remoteDescripter = data.ReadInterfaceToken(); + if (descripter != remoteDescripter) { + COAUTH_HILOGD(MODULE_INNERKIT, "CoAuthStub::OnRemoteRequest failed, descriptor is not matched!"); + return FAIL; + } + + switch (code) { + case static_cast(IExecutorCallback::ON_MESSENGER_READY): + return OnMessengerReadyStub(data, reply); + case static_cast(IExecutorCallback::ON_BEGIN_EXECUTE): + return OnBeginExecuteStub(data, reply); + case static_cast(IExecutorCallback::ON_END_EXECUTE): + return OnEndExecuteStub(data, reply); + case static_cast(IExecutorCallback::ON_SET_PROPERTY): + return OnSetPropertyStub(data, reply); + case static_cast(IExecutorCallback::ON_GET_PROPERTY): + return OnGetPropertyStub(data, reply); + default: + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } +} + +int32_t ExecutorCallbackStub::OnMessengerReadyStub(MessageParcel& data, MessageParcel& reply) +{ + COAUTH_HILOGD(MODULE_INNERKIT, "ExecutorCallbackStub::OnMessengerReadyStub"); + sptr messenger = iface_cast(data.ReadRemoteObject()); + if (messenger == nullptr) { + COAUTH_HILOGD(MODULE_INNERKIT, "messenger is nullptr"); + return FAIL; + } + COAUTH_HILOGD(MODULE_INNERKIT, "iface_cast right"); + OnMessengerReady(messenger); + COAUTH_HILOGD(MODULE_INNERKIT, "OnMessengerReady GetRefPtr"); + return SUCCESS; +} +int32_t ExecutorCallbackStub::OnBeginExecuteStub(MessageParcel& data, MessageParcel& reply) +{ + uint64_t scheduleId = data.ReadUint64(); + std::vector publicKey, buffer; + std::shared_ptr commandAttrs = std::make_shared(); + data.ReadUInt8Vector(&publicKey); + data.ReadUInt8Vector(&buffer); + commandAttrs->Unpack(buffer); + int32_t ret = OnBeginExecute(scheduleId, publicKey, commandAttrs); + if (!reply.WriteInt32(ret)) { + COAUTH_HILOGE(MODULE_INNERKIT, "failed to WriteInt32(ret)"); + return FAIL; + } + return SUCCESS; +} + +int32_t ExecutorCallbackStub::OnEndExecuteStub(MessageParcel& data, MessageParcel& reply) +{ + uint64_t scheduleId = data.ReadUint64(); + std::vector buffer; + std::shared_ptr consumerAttr = std::make_shared(); + data.ReadUInt8Vector(&buffer); + consumerAttr->Unpack(buffer); + int32_t ret = OnEndExecute(scheduleId, consumerAttr); + if (!reply.WriteInt32(ret)) { + COAUTH_HILOGE(MODULE_INNERKIT, "failed to WriteInt32(ret)"); + return FAIL; + } + return SUCCESS; +} + +int32_t ExecutorCallbackStub::OnGetPropertyStub(MessageParcel& data, MessageParcel& reply) +{ + std::vector buffer; + std::shared_ptr conditions = std::make_shared(); + data.ReadUInt8Vector(&buffer); + conditions->Unpack(buffer); + buffer.clear(); + std::shared_ptr values = std::make_shared(); + data.ReadUInt8Vector(&buffer); + values->Unpack(buffer); + int32_t ret = OnGetProperty(conditions, values); + if (!reply.WriteInt32(ret)) { + COAUTH_HILOGE(MODULE_INNERKIT, "failed to WriteInt32(ret)"); + return FAIL; + } + return SUCCESS; +} +int32_t ExecutorCallbackStub::OnSetPropertyStub(MessageParcel& data, MessageParcel& reply) +{ + std::vector buffer; + std::shared_ptr properties = std::make_shared(); + data.ReadUInt8Vector(&buffer); + properties->Unpack(buffer); + + int32_t ret = OnSetProperty(properties); + if (!reply.WriteInt32(ret)) { + COAUTH_HILOGE(MODULE_INNERKIT, "failed to WriteInt32(ret)"); + return FAIL; + } + return SUCCESS; +} + +void ExecutorCallbackStub::OnMessengerReady(const sptr &messenger) +{ + if (callback_ == nullptr) { + return ; + } else { + callback_->OnMessengerReady(messenger); + } +} +int32_t ExecutorCallbackStub::OnBeginExecute(uint64_t scheduleId, std::vector &publicKey, + std::shared_ptr commandAttrs) +{ + int32_t ret = FAIL; + if (callback_ == nullptr) { + return FAIL; + } else { + ret = callback_->OnBeginExecute(scheduleId, publicKey, commandAttrs); + } + return ret; +} +int32_t ExecutorCallbackStub::OnEndExecute(uint64_t scheduleId, std::shared_ptr consumerAttr) +{ + int32_t ret = FAIL; + if (callback_ == nullptr) { + return FAIL; + } else { + ret = callback_->OnEndExecute(scheduleId, consumerAttr); + } + return ret; +} +int32_t ExecutorCallbackStub::OnSetProperty(std::shared_ptr properties) +{ + int32_t ret = FAIL; + if (callback_ == nullptr) { + return FAIL; + } else { + ret = callback_->OnSetProperty(properties); + } + return ret; +} +int32_t ExecutorCallbackStub::OnGetProperty(std::shared_ptr conditions, + std::shared_ptr values) +{ + int32_t ret = FAIL; + if (callback_ == nullptr) { + return FAIL; + } else { + ret = callback_->OnGetProperty(conditions, values); + } + return ret; +} +} // namespace AuthResPool +} // namespace UserIAM +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/kitsimpl/src/executor_messenger_proxy.cpp b/frameworks/kitsimpl/src/executor_messenger_proxy.cpp new file mode 100644 index 0000000..4cc0149 --- /dev/null +++ b/frameworks/kitsimpl/src/executor_messenger_proxy.cpp @@ -0,0 +1,110 @@ +/* + * 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 "executor_messenger_proxy.h" +#include "coauth_hilog_wrapper.h" +#include "message_parcel.h" + +namespace OHOS { +namespace UserIAM { +namespace AuthResPool { +int32_t ExecutorMessengerProxy::SendData(uint64_t scheduleId, uint64_t transNum, + int32_t srcType, int32_t dstType, std::shared_ptr msg) +{ + MessageParcel data; + MessageParcel reply; + int32_t result = 0; + if (!data.WriteInterfaceToken(ExecutorMessengerProxy::GetDescriptor())) { + COAUTH_HILOGE(MODULE_INNERKIT, "write descriptor failed!"); + return FAIL; + } + if (!data.WriteUint64(scheduleId)) { + return FAIL; + } + if (!data.WriteUint64(transNum)) { + return FAIL; + } + if (!data.WriteInt32(srcType)) { + return FAIL; + } + if (!data.WriteInt32(dstType)) { + return FAIL; + } + + std::vector buffer; + msg->FromUint8Array(buffer); + if (!data.WriteUInt8Vector(buffer)) { + return FAIL; + } + bool ret = SendRequest(static_cast(IExecutorMessenger::COAUTH_SEND_DATA), data, reply); + if (ret) { + result = reply.ReadInt32(); + COAUTH_HILOGI(MODULE_INNERKIT, "result = %{public}d", result); + } + return result; +} + + +int32_t ExecutorMessengerProxy::Finish(uint64_t scheduleId, int32_t srcType, int32_t resultCode, + std::shared_ptr finalResult) +{ + MessageParcel data; + MessageParcel reply; + int32_t result = 0; + if (!data.WriteInterfaceToken(ExecutorMessengerProxy::GetDescriptor())) { + COAUTH_HILOGE(MODULE_INNERKIT, "write descriptor failed!"); + return result; + } + + if (!data.WriteUint64(scheduleId)) { + return FAIL; + } + if (!data.WriteInt32(srcType)) { + return FAIL; + } + if (!data.WriteInt32(resultCode)) { + return FAIL; + } + std::vector buffer; + if (finalResult->Pack(buffer)) { + return FAIL; + } + if (!data.WriteUInt8Vector(buffer)) { + return FAIL; + } + bool ret = SendRequest(static_cast(IExecutorMessenger::COAUTH_FINISH), data, reply); + if (ret) { + result = reply.ReadInt32(); + COAUTH_HILOGI(MODULE_INNERKIT, "result = %{public}d", result); + } + return result; +} +bool ExecutorMessengerProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply) +{ + sptr remote = Remote(); + if (remote == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "failed to get remote."); + return false; + } + MessageOption option(MessageOption::TF_SYNC); + int32_t result = remote->SendRequest(code, data, reply, option); + if (result != OHOS::NO_ERROR) { + COAUTH_HILOGE(MODULE_INNERKIT, "failed to SendRequest.result = %{public}d", result); + return false; + } + return true; +} +} +} +} \ No newline at end of file diff --git a/frameworks/kitsimpl/src/executor_messenger_stub.cpp b/frameworks/kitsimpl/src/executor_messenger_stub.cpp new file mode 100644 index 0000000..b6157e4 --- /dev/null +++ b/frameworks/kitsimpl/src/executor_messenger_stub.cpp @@ -0,0 +1,82 @@ +/* + * 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 "executor_messenger_stub.h" + +namespace OHOS { +namespace UserIAM { +namespace AuthResPool { +const std::string PERMISSION_AUTH_RESPOOL = "ohos.permission.ACCESS_AUTH_RESPOOL"; +const std::string PERMISSION_ACCESS_COAUTH = "ohos.permission.ACCESS_COAUTH"; + +int32_t ExecutorMessengerStub::OnRemoteRequest(uint32_t code, MessageParcel &data, + MessageParcel &reply, MessageOption &option) +{ + COAUTH_HILOGD(MODULE_SERVICE, "CoAuthStub::OnRemoteRequest, cmd = %d, flags= %d", + code, option.GetFlags()); + std::u16string descripter = ExecutorMessengerStub::GetDescriptor(); + std::u16string remoteDescripter = data.ReadInterfaceToken(); + if (descripter != remoteDescripter) { + COAUTH_HILOGE(MODULE_SERVICE, "CoAuthStub::OnRemoteRequest failed, descriptor is not matched!"); + return FAIL; + } + + switch (code) { + case static_cast(IExecutorMessenger::COAUTH_SEND_DATA): + return SendDataStub(data, reply); // call Stub + case static_cast(IExecutorMessenger::COAUTH_FINISH): + return FinishStub(data, reply); + default: + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } +} +int32_t ExecutorMessengerStub::SendDataStub(MessageParcel& data, MessageParcel& reply) +{ + uint64_t scheduleId = data.ReadUint64(); + uint64_t transNum = data.ReadUint64(); + int32_t srcType = data.ReadInt32(); + int32_t dstType = data.ReadInt32(); + std::vector buffer; + data.ReadUInt8Vector(&buffer); + std::shared_ptr msg = std::make_shared(buffer); + int32_t ret = SendData(scheduleId, transNum, srcType, dstType, msg); // Call business function + if (!reply.WriteInt32(ret)) { + COAUTH_HILOGE(MODULE_INNERKIT, "failed to WriteInt32(ret)"); + return FAIL; + } + return SUCCESS; +} + +int32_t ExecutorMessengerStub::FinishStub(MessageParcel& data, MessageParcel& reply) +{ + uint64_t scheduleId = data.ReadUint64(); + int32_t srcType = data.ReadInt32(); + int32_t resultCode = data.ReadInt32(); + + std::vector buffer; + std::shared_ptr finalResult = std::make_shared(); + data.ReadUInt8Vector(&buffer); + finalResult->Unpack(buffer); + + int32_t ret = Finish(scheduleId, srcType, resultCode, finalResult); + if (!reply.WriteInt32(ret)) { + COAUTH_HILOGE(MODULE_INNERKIT, "failed to WriteInt32(ret)"); + return FAIL; + } + return SUCCESS; +} +} // namespace AuthResPool +} // namespace UserIAM +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/kitsimpl/src/query_callback_proxy.cpp b/frameworks/kitsimpl/src/query_callback_proxy.cpp new file mode 100644 index 0000000..6b9ce47 --- /dev/null +++ b/frameworks/kitsimpl/src/query_callback_proxy.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include "coauth_hilog_wrapper.h" +#include "query_callback_proxy.h" + +namespace OHOS { +namespace UserIAM { +namespace AuthResPool { +void QueryCallbackProxy::OnResult(uint32_t resultCode) +{ + MessageParcel data; + if (!data.WriteUint32(resultCode)) { + COAUTH_HILOGE(MODULE_INNERKIT, "failed to WriteUint64(scheduleId)."); + } + MessageParcel reply; + bool ret = SendRequest(static_cast(IQueryCallback::COAUTH_QUERY_RESULT), data, reply); + if (ret) { + COAUTH_HILOGI(MODULE_INNERKIT, "ret = %{public}d", ret); + } +} + +bool QueryCallbackProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply) +{ + sptr remote = Remote(); + if (remote == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "failed to get remote."); + return false; + } + MessageOption option(MessageOption::TF_SYNC); + int32_t result = remote->SendRequest(code, data, reply, option); + if (result != OHOS::NO_ERROR) { + COAUTH_HILOGE(MODULE_INNERKIT, "failed to SendRequest.result = %{public}d", result); + return false; + } + return true; +} +} // namespace AuthResPool +} // namespace UserIAM +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/kitsimpl/src/query_callback_stub.cpp b/frameworks/kitsimpl/src/query_callback_stub.cpp new file mode 100644 index 0000000..fa8bb3d --- /dev/null +++ b/frameworks/kitsimpl/src/query_callback_stub.cpp @@ -0,0 +1,57 @@ +/* + * 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 "coauth_hilog_wrapper.h" +#include "coauth_info_define.h" +#include "query_callback_stub.h" + +namespace OHOS { +namespace UserIAM { +namespace AuthResPool { +QueryCallbackStub::QueryCallbackStub(const std::shared_ptr& impl) +{ + callback_ = impl; +} + +int32_t QueryCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, + MessageParcel &reply, MessageOption &option) +{ + switch (code) { + case static_cast(IQueryCallback::COAUTH_QUERY_RESULT): + return OnResultStub(data, reply); // call Stub + default: + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } +} + +int32_t QueryCallbackStub::OnResultStub(MessageParcel& data, MessageParcel& reply) +{ + uint32_t resultCode = data.ReadUint32(); + OnResult(resultCode); + + return SUCCESS; +} + +void QueryCallbackStub::OnResult(uint32_t resultCode) +{ + if (callback_ == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "callback_ is null"); + } else { + callback_->OnResult(resultCode); + } +} +} // namespace AuthResPool +} // namespace UserIAM +} // namespace OHOS diff --git a/frameworks/kitsimpl/src/set_prop_callback_proxy.cpp b/frameworks/kitsimpl/src/set_prop_callback_proxy.cpp new file mode 100644 index 0000000..45ca9d7 --- /dev/null +++ b/frameworks/kitsimpl/src/set_prop_callback_proxy.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include "coauth_hilog_wrapper.h" +#include "set_prop_callback_proxy.h" + +namespace OHOS { +namespace UserIAM { +namespace CoAuth { +void SetPropCallbackProxy::OnResult(uint32_t result, std::vector &extraInfo) +{ + MessageParcel data; + MessageParcel reply; + + if (!data.WriteUint32(result)) { + COAUTH_HILOGE(MODULE_INNERKIT, "failed to WriteUint32(result)."); + } + + if (!data.WriteUInt8Vector(extraInfo)) { + COAUTH_HILOGE(MODULE_INNERKIT, "fail to wirte WriteUInt8Vector extraInfo"); + } + + bool ret = SendRequest(static_cast(ISetPropCallback::ONRESULT), data, reply); + if (!ret) { + COAUTH_HILOGE(MODULE_INNERKIT, "SendRequest is failed, error code: %d", ret); + } +} + +bool SetPropCallbackProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply) +{ + sptr remote = Remote(); + if (remote == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "failed to get remote."); + return false; + } + MessageOption option(MessageOption::TF_SYNC); + int32_t result = remote->SendRequest(code, data, reply, option); + if (result != OHOS::NO_ERROR) { + COAUTH_HILOGE(MODULE_INNERKIT, "failed to SendRequest.result = %{public}d", result); + return false; + } + return true; +} +} // namespace CoAuth +} // namespace UserIAM +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/kitsimpl/src/set_prop_callback_stub.cpp b/frameworks/kitsimpl/src/set_prop_callback_stub.cpp new file mode 100644 index 0000000..f614dd8 --- /dev/null +++ b/frameworks/kitsimpl/src/set_prop_callback_stub.cpp @@ -0,0 +1,59 @@ +/* + * 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 "coauth_hilog_wrapper.h" +#include "set_prop_callback_stub.h" + +namespace OHOS { +namespace UserIAM { +namespace CoAuth { +SetPropCallbackStub::SetPropCallbackStub(const std::shared_ptr& impl) +{ + callback_ = impl; +} + +int32_t SetPropCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, + MessageParcel &reply, MessageOption &option) +{ + switch (code) { + case static_cast(ISetPropCallback::ONRESULT): + return OnResultStub(data, reply); + default: + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } +} + +int32_t SetPropCallbackStub::OnResultStub(MessageParcel &data, MessageParcel &reply) +{ + uint32_t result = data.ReadUint32(); + std::vector extraInfo; + data.ReadUInt8Vector(&extraInfo); + + OnResult(result, extraInfo); + + return SUCCESS; +} + +void SetPropCallbackStub::OnResult(uint32_t result, std::vector &extraInfo) +{ + if (callback_ == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "callback is null"); + } else { + callback_->OnResult(result, extraInfo); + } +} +} // namespace CoAuth +} // namespace UserIAM +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/innerkits/BUILD.gn b/interfaces/innerkits/BUILD.gn new file mode 100755 index 0000000..ac64308 --- /dev/null +++ b/interfaces/innerkits/BUILD.gn @@ -0,0 +1,74 @@ +# 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("//base/useriam/coauth/coauth.gni") + +config("coauth_private_config") { + include_dirs = [ + "${coauth_service_path}/include", + "//utils/system/safwk/napi/include", + "${coauth_frameworks_path}/kitsimpl/include", + "//foundation/distributeddatamgr/distributeddatamgr/frameworks/innerkitsimpl/distributeddatafwk/include", + ] +} + +config("coauth_public_config") { + include_dirs = [ "include", + "${coauth_frameworks_path}/kitsimpl/include", + ] +} + +ohos_shared_library("coauth_framework") { + sources = [ + "../../frameworks/kitsimpl/src/coauth_proxy.cpp", + "../../frameworks/kitsimpl/src/coauth.cpp", + "../../frameworks/kitsimpl/src/query_callback_proxy.cpp", + "../../frameworks/kitsimpl/src/query_callback_stub.cpp", + "../../frameworks/kitsimpl/src/executor_callback_stub.cpp", + "../../frameworks/kitsimpl/src/executor_callback_proxy.cpp", + "../../interfaces/innerkits/src/auth_attributes.cpp", + "../../interfaces/innerkits/src/auth_message.cpp", + "../../frameworks/kitsimpl/src/executor_messenger_proxy.cpp", + "../../frameworks/kitsimpl/src/executor_messenger_stub.cpp", + "../../frameworks/kitsimpl/src/auth_executor_registry.cpp", + "../../interfaces/innerkits/src/auth_executor.cpp", + "../../frameworks/kitsimpl/src/auth_info.cpp", + "../../frameworks/kitsimpl/src/set_prop_callback_proxy.cpp", + "../../frameworks/kitsimpl/src/set_prop_callback_stub.cpp", + "../../frameworks/kitsimpl/src/coauth_callback_proxy.cpp", + "../../frameworks/kitsimpl/src/coauth_callback_stub.cpp", + ] + + configs = [ + "${coauth_utils_path}:utils_config", + ":coauth_private_config" + ] + + public_configs = [ ":coauth_public_config", + "${coauth_utils_path}:utils_config" ] + + deps = [ + "//utils/native/base:utils", + "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/distributeddata:distributeddata_inner", + "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter:distributeddata_adapter", + ] + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_standard:samgr_proxy", + ] + + part_name = "${coauth_native_part_name}" +} diff --git a/interfaces/innerkits/include/auth_attributes.h b/interfaces/innerkits/include/auth_attributes.h new file mode 100644 index 0000000..87bf8ba --- /dev/null +++ b/interfaces/innerkits/include/auth_attributes.h @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef AUTH_ATTRIBUTES_H +#define AUTH_ATTRIBUTES_H + +#include +#include +#include +#include +#include + +#include "coauth_info_define.h" +#include "iremote_object.h" +#include "parcel.h" +#include "iremote_broker.h" +#include "coauth_hilog_wrapper.h" + +namespace OHOS { +namespace UserIAM { +namespace AuthResPool { +class AuthAttributes { +public: + AuthAttributes(); + ~AuthAttributes() {}; + void clear(); + int32_t GetBoolValue(AuthAttributeType attrType, bool &value); + int32_t GetUint32Value(AuthAttributeType attrType, uint32_t &value); + int32_t GetUint64Value(AuthAttributeType attrType, uint64_t &value); + int32_t GetUint64ArrayValue(AuthAttributeType attrType, std::vector &value); + int32_t GetUint32ArrayValue(AuthAttributeType attrType, std::vector &value); + int32_t GetUint8ArrayValue(AuthAttributeType attrType, std::vector &value); + int32_t Pack(std::vector &buffer); + + int32_t SetBoolValue(AuthAttributeType attrType, bool value); + int32_t SetUint32Value(AuthAttributeType attrType, uint32_t value); + int32_t SetUint64Value(AuthAttributeType attrType, uint64_t value); + int32_t SetUint32ArrayValue(AuthAttributeType attrType, std::vector &value); + int32_t SetUint64ArrayValue(AuthAttributeType attrType, std::vector &value); + int32_t SetUint8ArrayValue(AuthAttributeType attrType, std::vector &value); + AuthAttributes* Unpack(std::vector &buffer); + enum ValueType { + BOOLTYPE = 1, + UINT32TYPE = 2, + UINT64TYPE = 3, + UINT32ARRAYTYPE = 4, + UINT8ARRAYTYPE = 5, + UINT64ARRAYTYPE = 6 + }; + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.UserIAM.AuthResPool.AuthAttributes"); + +private: + std::map boolValueMap_; + std::map uint32ValueMap_; + std::map uint64ValueMap_; + std::map > uint32ArraylValueMap_; + std::map > uint64ArraylValueMap_; + std::map > uint8ArrayValueMap_; + std::map authAttributesPosition_; + std::vector existAttributes_; + AuthAttributeType GetUint32FromUint8(std::vector &data, uint32_t begin); + bool GetBoolFromUint8(std::vector &data, uint32_t begin); + uint64_t GetUint64FromUint8(std::vector &data, uint32_t begin); + std::vector GetUint64ArrayFromUint8(std::vector &data, uint32_t begin, uint32_t len); + std::vector GetUint32ArrayFromUint8(std::vector &data, uint32_t begin, uint32_t len); + void PackToBuffer(std::map::iterator iter, + uint32_t dataLength, uint8_t *writePointer, + std::vector &buffer); + void WriteDataLength(std::vector &buffer, uint8_t *writePointer, uint32_t dataLength); + void UnpackTag(AuthAttributeType &tag, std::vector &buffer, + uint32_t &authDataLength, uint32_t &dataLength); + void Write32Array(std::vector &uint32ArraylValue, uint8_t *writePointer, + std::vector &buffer); + void Write64Array(std::vector &uint64ArraylValue, uint8_t *writePointer, + std::vector &buffer); + void InsertMap(AuthAttributeType a, ValueType b) + { + authAttributesPosition_.insert(std::map::value_type(a, b)); + } +}; +} // namespace AuthResPool +} // namespace UserIAM +} // namespace OHOS + +#endif // AUTH_ATTRIBUTES_H \ No newline at end of file diff --git a/interfaces/innerkits/include/auth_executor.h b/interfaces/innerkits/include/auth_executor.h new file mode 100644 index 0000000..b5c248e --- /dev/null +++ b/interfaces/innerkits/include/auth_executor.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef AUTH_EXECUTOR_H +#define AUTH_EXECUTOR_H + +#include +#include +#include +#include +#include "coauth_info_define.h" + +namespace OHOS { +namespace UserIAM { +namespace AuthResPool { +class AuthExecutor { +public: + AuthExecutor(); + ~AuthExecutor(); + + /* Actuator information acquisition */ + int32_t GetAuthType(AuthType &value); // Credential type + int32_t GetAuthAbility(uint64_t &value); // Actuator capability + int32_t GetExecutorSecLevel(ExecutorSecureLevel &value); // Safety level + int32_t GetExecutorType(ExecutorType &value); // Actuator type + int32_t GetPublicKey(std::vector &value); // Public key + int32_t GetDeviceId(std::vector &value); // Device ID + + /* Actuator information setting */ + int32_t SetAuthType(AuthType value); // Credential type + int32_t SetAuthAbility(uint64_t value); // Actuator capability + int32_t SetExecutorSecLevel(ExecutorSecureLevel value); // Safety level + int32_t SetExecutorType(ExecutorType value); // Actuator type + int32_t SetPublicKey(std::vector &value); // Public key + int32_t SetDeviceId(std::vector &value); // Device ID + +private: + AuthType authTypeValue_; + uint64_t authAbilityValue_; + ExecutorSecureLevel executorSecLevelValue_; + ExecutorType executorTypeValue_; + std::vector publicKeyValue_; + std::vector deviceIdValue_; +}; +} // namespace AuthResPool +} // namespace userIAM +} // namespace ohos + +#endif // AUTH_EXECUTOR_H \ No newline at end of file diff --git a/interfaces/innerkits/include/auth_executor_registry.h b/interfaces/innerkits/include/auth_executor_registry.h new file mode 100644 index 0000000..4bf1675 --- /dev/null +++ b/interfaces/innerkits/include/auth_executor_registry.h @@ -0,0 +1,54 @@ +/* +* Copyright (c) 2021 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef AUTH_EXECUTOR_REGISTRY_H +#define AUTH_EXECUTOR_REGISTRY_H + +#include "i_coauth.h" +#include "query_callback.h" +#include "executor_callback.h" + +namespace OHOS { +namespace UserIAM { +namespace AuthResPool { +class AuthExecutorRegistry : public DelayedRefSingleton { + DECLARE_DELAYED_REF_SINGLETON(AuthExecutorRegistry); + +public: +DISALLOW_COPY_AND_MOVE(AuthExecutorRegistry); +/* InnerKit */ +uint64_t Register(std::shared_ptr executorInfo, std::shared_ptr callback); +void QueryStatus(AuthExecutor &executorInfo, std::shared_ptr callback); + +private: + class AuthExecutorRegistryDeathRecipient : public IRemoteObject::DeathRecipient { + public: + AuthExecutorRegistryDeathRecipient() = default; + ~AuthExecutorRegistryDeathRecipient() = default; + void OnRemoteDied(const wptr& remote) override; + + private: + DISALLOW_COPY_AND_MOVE(AuthExecutorRegistryDeathRecipient); + }; + void ResetProxy(const wptr& remote); + sptr GetProxy(); + std::mutex mutex_; + sptr proxy_ {nullptr}; + sptr deathRecipient_ {nullptr}; +}; +} // namespace AuthResPool +} // namespace UserIAM +} // namespace OHOS +#endif // AUTH_EXECUTOR_REGISTRY_H \ No newline at end of file diff --git a/interfaces/innerkits/include/auth_info.h b/interfaces/innerkits/include/auth_info.h new file mode 100644 index 0000000..c1f0d85 --- /dev/null +++ b/interfaces/innerkits/include/auth_info.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef AUTH_INFO_H +#define AUTH_INFO_H +#include + +namespace OHOS { +namespace UserIAM { +namespace CoAuth { +class AuthInfo { +public: + int32_t GetPkgName(std::string &value); + int32_t GetCallerUid(uint64_t &value); + + int32_t SetPkgName(std::string value); + int32_t SetCallerUid(uint64_t value); + +private: + std::string pkgName_; + int32_t callerUid_; +}; +} // namespace CoAuth +} // namespace UserIAM +} // namespace OHOS + +#endif // AUTH_INFO_H diff --git a/interfaces/innerkits/include/auth_message.h b/interfaces/innerkits/include/auth_message.h new file mode 100644 index 0000000..5678ea1 --- /dev/null +++ b/interfaces/innerkits/include/auth_message.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 AUTH_MESSAGE_H +#define AUTH_MESSAGE_H + +#include +#include +#include "iremote_object.h" +#include "parcel.h" +#include "iremote_broker.h" + +namespace OHOS { +namespace UserIAM { +namespace AuthResPool { +class AuthMessage { +public: + AuthMessage(std::vector &msg); + ~AuthMessage(); + AuthMessage* FromUint8Array(std::vector &msg); + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.UserIAM.AuthResPool.AuthMessage"); +private: + std::vector authMessage_; +}; +} // namespace AuthResPool +} // namespace UserIAM +} // namespace OHOS + +#endif // AUTH_MESSAGE_H \ No newline at end of file diff --git a/interfaces/innerkits/include/co_auth.h b/interfaces/innerkits/include/co_auth.h new file mode 100755 index 0000000..dd9a9be --- /dev/null +++ b/interfaces/innerkits/include/co_auth.h @@ -0,0 +1,60 @@ +/* + * 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 CO_AUTH_H +#define CO_AUTH_H + +#include +#include +#include "coauth_callback.h" +#include "set_prop_callback.h" +#include "coauth_info_define.h" +#include "i_coauth.h" +#include "auth_attributes.h" + +namespace OHOS { +namespace UserIAM { +namespace CoAuth { +class CoAuth : public DelayedRefSingleton { + DECLARE_DELAYED_REF_SINGLETON(CoAuth); + +public: + DISALLOW_COPY_AND_MOVE(CoAuth); + void coAuth(uint64_t scheduleId, AuthInfo &authInfo, std::shared_ptr callback); + int32_t Cancel(uint64_t scheduleId); + int32_t GetExecutorProp(AuthResPool::AuthAttributes &conditions, + std::shared_ptr values); + void SetExecutorProp(AuthResPool::AuthAttributes &conditions, std::shared_ptr callback); + +private: + class CoAuthDeathRecipient : public IRemoteObject::DeathRecipient { + public: + CoAuthDeathRecipient() = default; + ~CoAuthDeathRecipient() = default; + void OnRemoteDied(const wptr& remote) override; + + private: + DISALLOW_COPY_AND_MOVE(CoAuthDeathRecipient); + }; + void ResetProxy(const wptr& remote); + sptr GetProxy(); + std::mutex mutex_; + sptr proxy_ {nullptr}; + sptr deathRecipient_ {nullptr}; +}; +} // namespace CoAuth +} // namespace UserIAM +} // namespace OHOS +#endif // CO_AUTH_H \ No newline at end of file diff --git a/interfaces/innerkits/include/coauth_callback.h b/interfaces/innerkits/include/coauth_callback.h new file mode 100644 index 0000000..6fc128d --- /dev/null +++ b/interfaces/innerkits/include/coauth_callback.h @@ -0,0 +1,36 @@ +/* + * 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 COAUTH_CALLBACK_H +#define COAUTH_CALLBACK_H + +#include +#include +#include +#include + +namespace OHOS { +namespace UserIAM { +namespace CoAuth { +class CoAuthCallback { +public: + virtual void OnFinish(uint32_t resultCode, std::vector &scheduleToken) = 0; + virtual void OnAcquireInfo(uint32_t acquire) = 0; +}; +} // namespace CoAuth +} // namespace UserIAM +} // namespace OHOS + +#endif // COAUTH_CALLBACK_H \ No newline at end of file diff --git a/interfaces/innerkits/include/coauth_info_define.h b/interfaces/innerkits/include/coauth_info_define.h new file mode 100644 index 0000000..d16b83c --- /dev/null +++ b/interfaces/innerkits/include/coauth_info_define.h @@ -0,0 +1,164 @@ +/* + * 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 COAUTH_INFO_DEFINE_H +#define COAUTH_INFO_DEFINE_H + +#include "parcel.h" + +namespace OHOS { +namespace UserIAM { +/* enums define */ +enum AuthAttributeType { + /* Root tag */ + AUTH_ROOT = 100000, + /* Result code */ + AUTH_RESULT_CODE = 100001, + /* Tag of signature data in TLV */ + AUTH_SIGNATURE = 100004, + /* Identify mode */ + AUTH_IDENTIFY_MODE = 100005, + /* Tag of templateId data in TLV */ + AUTH_TEMPLATE_ID = 100006, + /* Tag of templateId list data in TLV */ + AUTH_TEMPLATE_ID_LIST = 100007, + /* Expected attribute, tag of remain count in TLV */ + AUTH_REMAIN_COUNT = 100009, + /* Remain time */ + AUTH_REMAIN_TIME = 100010, + /* Session id, required when decode in C */ + AUTH_SESSION_ID = 100014, + /* Package name */ + AUTH_CALLER_NAME = 100015, + /* Schedule version */ + AUTH_SCHEDULE_VERSION = 100016, + /* Tag of lock out template in TLV */ + AUTH_LOCK_OUT_TEMPLATE = 100018, + /* Tag of unlock template in TLV */ + AUTH_UNLOCK_TEMPLATE = 100019, + /* Tag of data */ + AUTH_DATA = 100020, + /* Tag of auth subType */ + AUTH_SUBTYPE = 100021, + /* Tag of auth schedule mode */ + AUTH_SCHEDULE_MODE = 100022, + /* Tag of property */ + AUTH_PROPERTY_MODE = 100023, + /* Tag of auth type */ + AUTH_TYPE = 100024, + /* Tag of cred id */ + AUTH_CREDENTIAL_ID = 100025, + /* Controller */ + AUTH_CONTROLLER = 100026, + /* calleruid */ + AUTH_CALLER_UID = 100027, + /* result */ + AUTH_RESULT = 100028 +}; + +enum AuthType { + /* Authentication type pin */ + PIN = 1, + /* Authentication type face */ + FACE = 2 +}; + +enum AuthAbility { + /* Executor authentication ability six number pin */ + PIN_SIX = 1, + /* Executor authentication ability self defined number pin */ + PIN_NUMBER = 2, + /* Executor authentication ability mixed pin */ + PIN_MIXED = 4, + /* Executor authentication ability 2D face */ + FACE_2D = 1, + /* Executor authentication ability 3D face */ + FACE_3D = 2 +}; + +/* Safety level of actuator */ +enum ExecutorSecureLevel { + /* Executor without access control */ + ESL0 = 0, + /* Executor with access control */ + ESL1 = 1, + /* Executor in secure hardware */ + ESL2 = 2, + /* Executor in high secure hardware */ + ESL3 = 3 +}; + +enum ExecutorType { + /* Type of coauth */ + TYPE_CO_AUTH = 0, + /* Type of executor collector */ + TYPE_COLLECTOR = 1, + /* Type of executor verifier */ + TYPE_VERIFIER = 2, + /* Type of executor all in one */ + TYPE_ALL_IN_ONE = 3 +}; + +enum ResultCode { + /** + * Indicates that authentication is success or ability is supported. + */ + SUCCESS = 0, + /** + * Indicates the authenticator fails to identify user. + */ + FAIL = 1, + /** + * Indicates other errors. + */ + GENERAL_ERROR = 2, + /** + * Indicates that authentication has been canceled. + */ + CANCELED = 3, + /** + * Indicates that authentication has timed out. + */ + TIMEOUT = 4, + /** + * Indicates that this authentication type is not supported. + */ + TYPE_NOT_SUPPORT = 5, + /** + * Indicates that the authentication trust level is not supported. + */ + TRUST_LEVEL_NOT_SUPPORT = 6, + /** + * Indicates that the authentication task is busy. Wait for a few seconds and try again. + */ + BUSY = 7, + /** + * Indicates incorrect parameters. + */ + INVALID_PARAMETERS = 8, + /** + * Indicates that the authenticator is locked. + */ + LOCKED = 9, + /** + * Indicates that the user has not enrolled the authenticator. + */ + NOT_ENROLLED = 10 +}; + +const uint64_t INVALID_EXECUTOR_ID = 0; +} // namespace UserIAM +} // namespace OHOS +#endif // COAUTH_INFO_DEFINE_H diff --git a/interfaces/innerkits/include/executor_callback.h b/interfaces/innerkits/include/executor_callback.h new file mode 100644 index 0000000..ef6af44 --- /dev/null +++ b/interfaces/innerkits/include/executor_callback.h @@ -0,0 +1,38 @@ +/* + * 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 EXECUTOR_CALLBACK_H +#define EXECUTOR_CALLBACK_H + +#include "iexecutor_messenger.h" + +namespace OHOS { +namespace UserIAM { +namespace AuthResPool { +class ExecutorCallback { +public: + virtual void OnMessengerReady(const sptr &messenger) = 0; + virtual int32_t OnBeginExecute(uint64_t scheduleId, std::vector &publicKey, + std::shared_ptr commandAttrs) = 0; + virtual int32_t OnEndExecute(uint64_t scheduleId, std::shared_ptr consumerAttr) = 0; + virtual int32_t OnSetProperty(std::shared_ptr properties) = 0; + virtual int32_t OnGetProperty(std::shared_ptr conditions, + std::shared_ptr values) = 0; +}; +} // namespace AuthResPool +} // namespace UserIAM +} // namespace OHOS + +#endif // EXECUTOR_CALLBACK_H \ No newline at end of file diff --git a/interfaces/innerkits/include/executor_messenger.h b/interfaces/innerkits/include/executor_messenger.h new file mode 100644 index 0000000..858ef65 --- /dev/null +++ b/interfaces/innerkits/include/executor_messenger.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 EXECUTOR_MESSENGER_H +#define EXECUTOR_MESSENGER_H + +#include "coauth_info_define.h" +#include "executor_messenger_stub.h" +#include "auth_res_pool.h" + +namespace OHOS { +namespace UserIAM { +namespace AuthResPool { +class ExecutorMessenger : public ExecutorMessengerStub { +public: + ExecutorMessenger(UserIAM::CoAuth::AuthResPool*); + ~ExecutorMessenger() override = default; + virtual int32_t SendData(uint64_t scheduleId, uint64_t transNum, int32_t srcType, + int32_t dstType, std::shared_ptr msg) override; + virtual int32_t Finish(uint64_t scheduleId, int32_t srcType, int32_t resultCode, + std::shared_ptr finalResult) override; +private: + UserIAM::CoAuth::AuthResPool* ScheResPool_; +}; +} // namespace AuthResPool +} // namespace UserIAM +} // namespace OHOS +#endif // EXECUTOR_MESSENGER_H \ No newline at end of file diff --git a/interfaces/innerkits/include/query_callback.h b/interfaces/innerkits/include/query_callback.h new file mode 100644 index 0000000..a7a8b01 --- /dev/null +++ b/interfaces/innerkits/include/query_callback.h @@ -0,0 +1,30 @@ +/* + * 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 QUERY_CALLBACK_H +#define QUERY_CALLBACK_H + + +namespace OHOS { +namespace UserIAM { +namespace AuthResPool { +class QueryCallback { +public: + virtual void OnResult(uint32_t resultCode) = 0; +}; +} // namespace AuthResPool +} // namespace UserIAM +} // namespace OHOS +#endif // QUERY_CALLBACK_H diff --git a/interfaces/innerkits/include/set_prop_callback.h b/interfaces/innerkits/include/set_prop_callback.h new file mode 100644 index 0000000..f36c04b --- /dev/null +++ b/interfaces/innerkits/include/set_prop_callback.h @@ -0,0 +1,33 @@ +/* + * 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 SET_PROP_CALLBACK_H +#define SET_PROP_CALLBACK_H + +#include +#include + +namespace OHOS { +namespace UserIAM { +namespace CoAuth { +class SetPropCallback { +public: + virtual void OnResult(uint32_t result, std::vector &extraInfo) = 0; +}; +} // namespace CoAuth +} // namespace UserIAM +} // namespace OHOS + +#endif // SET_PROP_CALLBACK_H \ No newline at end of file diff --git a/interfaces/innerkits/src/auth_attributes.cpp b/interfaces/innerkits/src/auth_attributes.cpp new file mode 100644 index 0000000..4181400 --- /dev/null +++ b/interfaces/innerkits/src/auth_attributes.cpp @@ -0,0 +1,429 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "auth_attributes.h" + +namespace OHOS { +namespace UserIAM { +namespace AuthResPool { +AuthAttributes::AuthAttributes() +{ + InsertMap(AUTH_RESULT_CODE, UINT32TYPE); + InsertMap(AUTH_SIGNATURE, UINT8ARRAYTYPE); + InsertMap(AUTH_IDENTIFY_MODE, UINT32TYPE); + InsertMap(AUTH_TEMPLATE_ID, UINT64TYPE); + InsertMap(AUTH_TEMPLATE_ID_LIST, UINT64ARRAYTYPE); + InsertMap(AUTH_REMAIN_COUNT, UINT64TYPE); + InsertMap(AUTH_REMAIN_TIME, UINT64TYPE); + InsertMap(AUTH_SESSION_ID, UINT64TYPE); + InsertMap(AUTH_CALLER_NAME, UINT8ARRAYTYPE); + InsertMap(AUTH_SCHEDULE_VERSION, UINT32TYPE); + InsertMap(AUTH_LOCK_OUT_TEMPLATE, UINT64TYPE); + InsertMap(AUTH_UNLOCK_TEMPLATE, UINT64TYPE); + InsertMap(AUTH_SUBTYPE, UINT64TYPE); + InsertMap(AUTH_SCHEDULE_MODE, UINT32TYPE); + InsertMap(AUTH_PROPERTY_MODE, UINT32TYPE); + InsertMap(AUTH_TYPE, UINT32TYPE); + InsertMap(AUTH_CREDENTIAL_ID, UINT64TYPE); + InsertMap(AUTH_CONTROLLER, BOOLTYPE); + InsertMap(AUTH_CALLER_UID, UINT64TYPE); + InsertMap(AUTH_RESULT, UINT8ARRAYTYPE); +} + +void AuthAttributes::clear() +{ + boolValueMap_.clear(); + uint32ValueMap_.clear(); + uint64ValueMap_.clear(); + uint32ArraylValueMap_.clear(); + uint64ArraylValueMap_.clear(); + uint8ArrayValueMap_.clear(); +} + +int32_t AuthAttributes::GetBoolValue(AuthAttributeType attrType, bool &value) +{ + int32_t ret = SUCCESS; + std::map::iterator iter = boolValueMap_.find(attrType); + if (iter != boolValueMap_.end()) { + value = iter->second; + } else { + ret = FAIL; + } + return ret; +} + +int32_t AuthAttributes::GetUint32Value(AuthAttributeType attrType, uint32_t &value) +{ + int32_t ret = SUCCESS; + std::map::iterator iter = uint32ValueMap_.find(attrType); + if (iter != uint32ValueMap_.end()) { + value = iter->second; + } else { + ret = FAIL; + } + return ret; +} + +int32_t AuthAttributes::GetUint64Value(AuthAttributeType attrType, uint64_t &value) +{ + int32_t ret = SUCCESS; + std::map::iterator iter = uint64ValueMap_.find(attrType); + if (iter != uint64ValueMap_.end()) { + value = iter->second; + } else { + ret = FAIL; + } + return ret; +} + +int32_t AuthAttributes::GetUint32ArrayValue(AuthAttributeType attrType, std::vector &value) +{ + int32_t ret = SUCCESS; + std::map >::iterator iter = uint32ArraylValueMap_.find(attrType); + if (iter != uint32ArraylValueMap_.end()) { + value = iter->second; + } else { + ret = FAIL; + } + return ret; +} + +int32_t AuthAttributes::GetUint64ArrayValue(AuthAttributeType attrType, std::vector &value) +{ + int32_t ret = SUCCESS; + std::map >::iterator iter = uint64ArraylValueMap_.find(attrType); + if (iter != uint64ArraylValueMap_.end()) { + value = iter->second; + } else { + ret = FAIL; + } + return ret; +} + +int32_t AuthAttributes::GetUint8ArrayValue(AuthAttributeType attrType, std::vector &value) +{ + int32_t ret = SUCCESS; + std::map >::iterator iter = uint8ArrayValueMap_.find(attrType); + if (iter != uint8ArrayValueMap_.end()) { + value = iter->second; + } else { + ret = FAIL; + } + return ret; +} + +int32_t AuthAttributes::SetBoolValue(AuthAttributeType attrType, bool value) +{ + int32_t ret = SUCCESS; + if (authAttributesPosition_[attrType] != BOOLTYPE) { + return FAIL; + } + boolValueMap_[attrType] = value; + existAttributes_.push_back(attrType); + return ret; +} + +int32_t AuthAttributes::SetUint32Value(AuthAttributeType attrType, uint32_t value) +{ + int32_t ret = SUCCESS; + if (authAttributesPosition_[attrType] != UINT32TYPE) { + return FAIL; + } + uint32ValueMap_[attrType] = value; + existAttributes_.push_back(attrType); + COAUTH_HILOGD(MODULE_INNERKIT, "SetUint32Value : %{public}d.", value); + return ret; +} + +int32_t AuthAttributes::SetUint64Value(AuthAttributeType attrType, uint64_t value) +{ + int32_t ret = SUCCESS; + if (authAttributesPosition_[attrType] != UINT64TYPE) { + return FAIL; + } + uint64ValueMap_[attrType] = value; + existAttributes_.push_back(attrType); + return ret; +} + +int32_t AuthAttributes::SetUint32ArrayValue(AuthAttributeType attrType, std::vector &value) +{ + int32_t ret = SUCCESS; + if (authAttributesPosition_[attrType] != UINT32ARRAYTYPE) { + return FAIL; + } + uint32ArraylValueMap_[attrType] = value; + existAttributes_.push_back(attrType); + return ret; +} + +int32_t AuthAttributes::SetUint64ArrayValue(AuthAttributeType attrType, std::vector &value) +{ + int32_t ret = SUCCESS; + if (authAttributesPosition_[attrType] != UINT64ARRAYTYPE) { + return FAIL; + } + uint64ArraylValueMap_[attrType] = value; + existAttributes_.push_back(attrType); + return ret; +} + +int32_t AuthAttributes::SetUint8ArrayValue(AuthAttributeType attrType, std::vector &value) +{ + int32_t ret = SUCCESS; + if (authAttributesPosition_[attrType] != UINT8ARRAYTYPE) { + return FAIL; + } + uint8ArrayValueMap_[attrType] = value; + existAttributes_.push_back(attrType); + return ret; +} + +void AuthAttributes::UnpackTag(AuthAttributeType &tag, std::vector &buffer, + uint32_t &authDataLength, uint32_t &dataLength) +{ + tag = GetUint32FromUint8(buffer, authDataLength); + COAUTH_HILOGD(MODULE_INNERKIT, "buffer read tag : %{public}d.", tag); + authDataLength += sizeof(uint32_t); + dataLength = static_cast(GetUint32FromUint8(buffer, authDataLength)); + COAUTH_HILOGD(MODULE_INNERKIT, "buffer read dataLength : %{public}d.", dataLength); + authDataLength += sizeof(uint32_t); +} + +AuthAttributes* AuthAttributes::Unpack(std::vector &buffer) +{ + if (buffer.size() == 0) { + return nullptr; + } + uint32_t dataLength, authDataLength = 0; + AuthAttributeType tag; + std::vector uint32ArraylValue; + std::vector uint64ArraylValue; + std::vector uint8ArrayValue; + UnpackTag(tag, buffer, authDataLength, dataLength); + UnpackTag(tag, buffer, authDataLength, dataLength); + while (authDataLength < buffer.size()) { + UnpackTag(tag, buffer, authDataLength, dataLength); + std::map::iterator iter = authAttributesPosition_.find(tag); + switch (iter->second) { + case BOOLTYPE: + SetBoolValue(tag, GetBoolFromUint8(buffer, authDataLength)); + authDataLength += sizeof(bool); + break; + case UINT32TYPE: + SetUint32Value(tag, static_cast(GetUint32FromUint8(buffer, authDataLength))); + authDataLength += sizeof(uint32_t); + break; + case UINT64TYPE: + SetUint64Value(tag, GetUint64FromUint8(buffer, authDataLength)); + authDataLength += sizeof(uint64_t); + break; + case UINT32ARRAYTYPE: + uint32ArraylValue = GetUint32ArrayFromUint8(buffer, authDataLength, dataLength); + SetUint32ArrayValue(tag, uint32ArraylValue); + authDataLength += dataLength; + break; + case UINT64ARRAYTYPE: + uint64ArraylValue = GetUint64ArrayFromUint8(buffer, authDataLength, dataLength); + SetUint64ArrayValue(tag, uint64ArraylValue); + authDataLength += dataLength; + break; + case UINT8ARRAYTYPE: + if (dataLength != 0) { + uint8ArrayValue.insert(uint8ArrayValue.begin(), buffer.begin() + authDataLength, + buffer.begin() + authDataLength + dataLength); + SetUint8ArrayValue(tag, uint8ArrayValue); + authDataLength += dataLength; + } + break; + default: + break; + } + } + return this; +} + +AuthAttributeType AuthAttributes::GetUint32FromUint8(std::vector &data, uint32_t begin) +{ + uint8_t tmp[sizeof(uint32_t)]; + for (uint32_t i = 0; i < sizeof(uint32_t); i++) { + tmp[i] = data[begin + i]; + } + AuthAttributeType *re = static_cast(static_cast(tmp)); + return *re; +} + +bool AuthAttributes::GetBoolFromUint8(std::vector &data, uint32_t begin) +{ + uint8_t tmp = data[begin]; + bool *re = static_cast(static_cast(&tmp)); + return *re; +} + +uint64_t AuthAttributes::GetUint64FromUint8(std::vector &data, uint32_t begin) +{ + uint8_t tmp[sizeof(uint64_t)]; + for (uint32_t i = 0; i < sizeof(uint64_t); i++) { + tmp[i] = data[begin + i]; + } + uint64_t *re = static_cast(static_cast(tmp)); + return *re; +} + +std::vector AuthAttributes::GetUint32ArrayFromUint8(std::vector &data, uint32_t begin, uint32_t len) +{ + std::vector tmp; + for (uint32_t i = 0; i < len / sizeof(uint32_t); i++) { + uint32_t uint32data = GetUint32FromUint8(data, begin + i * sizeof(uint32_t)); + tmp.push_back(uint32data); + COAUTH_HILOGD(MODULE_INNERKIT, "buffer read uint32ArrayValue : %{public}d.", uint32data); + } + return tmp; +} + +std::vector AuthAttributes::GetUint64ArrayFromUint8(std::vector &data, uint32_t begin, uint32_t len) +{ + std::vector tmp; + for (uint32_t i = 0; i < len / sizeof(uint64_t); i++) { + uint64_t uint64data = GetUint64FromUint8(data, begin + i * sizeof(uint64_t)); + tmp.push_back(uint64data); + COAUTH_HILOGD(MODULE_INNERKIT, "buffer read uint64ArrayValue : %{public}llu.", uint64data); + } + return tmp; +} + +int32_t AuthAttributes::Pack(std::vector &buffer) +{ + uint8_t *writePointer; + uint32_t dataLength = 0, tag, authDataLength = 0; + + buffer.clear(); + + sort(existAttributes_.begin(), existAttributes_.end()); + for (int32_t i = 0; i != existAttributes_.size(); i++) { + if (existAttributes_[i] == AUTH_ROOT || + existAttributes_[i] == AUTH_DATA || + existAttributes_[i] == AUTH_SIGNATURE) { + continue; + } + tag = authAttributesPosition_.find(existAttributes_[i])->first; + writePointer = static_cast(static_cast(&tag)); + buffer.insert(buffer.end(), writePointer, writePointer + sizeof(AuthAttributeType)); + COAUTH_HILOGE(MODULE_INNERKIT, "data Write tag : %{public}d.", tag); + PackToBuffer(authAttributesPosition_.find(existAttributes_[i]), dataLength, writePointer, buffer); + } + + authDataLength = buffer.size(); + writePointer = static_cast(static_cast(&authDataLength)); + buffer.insert(buffer.begin(), writePointer, writePointer + sizeof(uint32_t)); + tag = AUTH_DATA; + writePointer = static_cast(static_cast(&tag)); + buffer.insert(buffer.begin(), writePointer, writePointer + sizeof(AuthAttributeType)); + + tag = AUTH_SIGNATURE; + writePointer = static_cast(static_cast(&tag)); + buffer.insert(buffer.end(), writePointer, writePointer + sizeof(AuthAttributeType)); + + PackToBuffer(authAttributesPosition_.find(AUTH_SIGNATURE), dataLength, writePointer, buffer); + + authDataLength = buffer.size(); + writePointer = static_cast(static_cast(&authDataLength)); + buffer.insert(buffer.begin(), writePointer, writePointer + sizeof(uint32_t)); + tag = AUTH_ROOT; + writePointer = static_cast(static_cast(&tag)); + buffer.insert(buffer.begin(), writePointer, writePointer + sizeof(AuthAttributeType)); + return SUCCESS; +} + + +void AuthAttributes::Write32Array(std::vector &uint32ArraylValue, uint8_t *writePointer, + std::vector &buffer) +{ + for (std::size_t num = 0; num < uint32ArraylValue.size(); num++) { + writePointer = static_cast(static_cast(&uint32ArraylValue[num])); + buffer.insert(buffer.end(), writePointer, writePointer + sizeof(uint32_t)); + } +} +void AuthAttributes::Write64Array(std::vector &uint64ArraylValue, uint8_t *writePointer, + std::vector &buffer) +{ + for (std::size_t num = 0; num < uint64ArraylValue.size(); num++) { + writePointer = static_cast(static_cast(&uint64ArraylValue[num])); + buffer.insert(buffer.end(), writePointer, writePointer + sizeof(uint64_t)); + } +} + +void AuthAttributes::PackToBuffer(std::map::iterator iter, + uint32_t dataLength, uint8_t *writePointer, + std::vector &buffer) +{ + bool boolValue; + uint32_t uint32Value; + uint64_t uint64Value; + std::vector uint32ArraylValue; + std::vector uint64ArraylValue; + std::vector uint8ArrayValue; + switch (iter->second) { + case BOOLTYPE: + GetBoolValue(iter->first, boolValue); + WriteDataLength(buffer, writePointer, sizeof(bool)); + writePointer = static_cast(static_cast(&boolValue)); + buffer.insert(buffer.end(), writePointer, writePointer + sizeof(bool)); + break; + case UINT32TYPE: + GetUint32Value(iter->first, uint32Value); + WriteDataLength(buffer, writePointer, sizeof(uint32_t)); + writePointer = static_cast(static_cast(&uint32Value)); + buffer.insert(buffer.end(), writePointer, writePointer + sizeof(uint32_t)); + COAUTH_HILOGE(MODULE_INNERKIT, "uint32Value : %{public}d.", uint32Value); + break; + case UINT64TYPE: + GetUint64Value(iter->first, uint64Value); + WriteDataLength(buffer, writePointer, sizeof(uint64_t)); + writePointer = static_cast(static_cast(&uint64Value)); + buffer.insert(buffer.end(), writePointer, writePointer + sizeof(uint64_t)); + break; + case UINT32ARRAYTYPE: + GetUint32ArrayValue(iter->first, uint32ArraylValue); + WriteDataLength(buffer, writePointer, sizeof(uint32_t) * uint32ArraylValue.size()); + Write32Array(uint32ArraylValue, writePointer, buffer); + break; + case UINT64ARRAYTYPE: + GetUint64ArrayValue(iter->first, uint64ArraylValue); + WriteDataLength(buffer, writePointer, sizeof(uint64_t) * uint64ArraylValue.size()); + Write64Array(uint64ArraylValue, writePointer, buffer); + break; + case UINT8ARRAYTYPE: + if (GetUint8ArrayValue(iter->first, uint8ArrayValue)) { + WriteDataLength(buffer, writePointer, 0); + break; + } + dataLength = sizeof(uint8_t) * uint8ArrayValue.size(); + WriteDataLength(buffer, writePointer, dataLength); + buffer.insert(buffer.end(), uint8ArrayValue.begin(), uint8ArrayValue.begin() + dataLength); + break; + default: + break; + } +} + +void AuthAttributes::WriteDataLength(std::vector &buffer, uint8_t *writePointer, uint32_t dataLength) +{ + writePointer = static_cast(static_cast(&dataLength)); + buffer.insert(buffer.end(), writePointer, writePointer + sizeof(uint32_t)); +} +} +} +} \ No newline at end of file diff --git a/interfaces/innerkits/src/auth_executor.cpp b/interfaces/innerkits/src/auth_executor.cpp new file mode 100644 index 0000000..2e50d2c --- /dev/null +++ b/interfaces/innerkits/src/auth_executor.cpp @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "auth_executor.h" + +namespace OHOS { +namespace UserIAM { +namespace AuthResPool { + AuthExecutor::AuthExecutor() + : authTypeValue_(PIN), + authAbilityValue_(0), + executorSecLevelValue_(ESL0), + executorTypeValue_(TYPE_CO_AUTH), + publicKeyValue_(0), + deviceIdValue_(0) + { + } + + AuthExecutor::~AuthExecutor() + { + } + + int32_t AuthExecutor::GetAuthType(AuthType &value) + { + value = authTypeValue_; + return 0; + } + int32_t AuthExecutor::SetAuthType(AuthType value) + { + authTypeValue_ = value; + return 0; + } + + int32_t AuthExecutor::GetAuthAbility(uint64_t &value) + { + value = authAbilityValue_; + return 0; + } + int32_t AuthExecutor::SetAuthAbility(uint64_t value) + { + authAbilityValue_ = value; + return 0; + } + + int32_t AuthExecutor::GetExecutorSecLevel(ExecutorSecureLevel &value) + { + value = executorSecLevelValue_; + return 0; + } + int32_t AuthExecutor::SetExecutorSecLevel(ExecutorSecureLevel value) + { + executorSecLevelValue_ = value; + return 0; + } + + int32_t AuthExecutor::GetExecutorType(ExecutorType &value) + { + value = executorTypeValue_; + return 0; + } + int32_t AuthExecutor::SetExecutorType(ExecutorType value) + { + executorTypeValue_ = value; + return 0; + } + + int32_t AuthExecutor::GetPublicKey(std::vector &value) + { + value = publicKeyValue_; + return 0; + } + int32_t AuthExecutor::SetPublicKey(std::vector &value) + { + publicKeyValue_ = value; + return 0; + } + + int32_t AuthExecutor::GetDeviceId(std::vector &value) + { + value = deviceIdValue_; + return 0; + } + int32_t AuthExecutor::SetDeviceId(std::vector &value) + { + deviceIdValue_ = value; + return 0; + } +} // namespace ohos +} // namespace userIAM +} // namespace authResPool \ No newline at end of file diff --git a/interfaces/innerkits/src/auth_message.cpp b/interfaces/innerkits/src/auth_message.cpp new file mode 100644 index 0000000..097aa37 --- /dev/null +++ b/interfaces/innerkits/src/auth_message.cpp @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "auth_message.h" + +namespace OHOS { +namespace UserIAM { +namespace AuthResPool { +AuthMessage* AuthMessage::FromUint8Array(std::vector &msg) +{ + msg = authMessage_; + return this; +} + +AuthMessage::AuthMessage(std::vector &msg) +{ + authMessage_ = msg; +} + +AuthMessage::~AuthMessage() = default; +} // namespace ohos +} // namespace userIAM +} // namespace authResPool \ No newline at end of file diff --git a/ohos.build b/ohos.build new file mode 100755 index 0000000..618f791 --- /dev/null +++ b/ohos.build @@ -0,0 +1,23 @@ +{ + "subsystem": "coauth", + "parts": { + "coauthmgr": { + "variants": [ + "wearable", + "phone" + ], + "module_list": [ + "//base/useriam/coauth/sa_profile:coauth_sa_profile", + "//base/useriam/coauth/services:coauthservice", + "//base/useriam/coauth/interfaces/innerkits:coauth_framework", + "//base/useriam/coauth/sa_profile:useriam.init" + ], + "inner_kits": [ + + ], + "test_list": [ + "//base/useriam/coauth/test:coauth_unittest_test" + ] + } + } +} diff --git a/sa_profile/5203.xml b/sa_profile/5203.xml new file mode 100755 index 0000000..d3050af --- /dev/null +++ b/sa_profile/5203.xml @@ -0,0 +1,24 @@ + + + + useriam + + 5203 + libcoauthservice.z.so + true + false + 1 + + diff --git a/sa_profile/BUILD.gn b/sa_profile/BUILD.gn new file mode 100755 index 0000000..860cb12 --- /dev/null +++ b/sa_profile/BUILD.gn @@ -0,0 +1,30 @@ +# 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("//base/useriam/coauth/coauth.gni") +import("//build/ohos/sa_profile/sa_profile.gni") + +ohos_sa_profile("coauth_sa_profile") { + sources = [ "5203.xml" ] + part_name = "${coauth_native_part_name}" +} +ohos_prebuilt_etc("useriam.init") { + if (use_musl) { + source = "useriam.cfg" + } else { + source = "useriam.rc" + } + relative_install_dir = "init" + part_name = "coauthmgr" + subsystem_name = "coauth" +} \ No newline at end of file diff --git a/sa_profile/useriam.cfg b/sa_profile/useriam.cfg new file mode 100644 index 0000000..6f0834f --- /dev/null +++ b/sa_profile/useriam.cfg @@ -0,0 +1,16 @@ +{ + "jobs":[{ + "name" : "boot", + "cmds" : [ + "start useriam" + ] + } + ], + "services" : [{ + "name" : "useriam", + "path" : ["/system/bin/sa_main", "/system/profile/useriam.xml"], + "uid" : "system", + "gid" : ["system", "shell"] + } + ] +} \ No newline at end of file diff --git a/sa_profile/useriam.rc b/sa_profile/useriam.rc new file mode 100644 index 0000000..6410e2e --- /dev/null +++ b/sa_profile/useriam.rc @@ -0,0 +1,5 @@ +service useriam /system/bin/sa_main /system/profile/useriam.xml + class z_core + user system + group system shell + seclabel u:r:useriam:s0 \ No newline at end of file diff --git a/services/BUILD.gn b/services/BUILD.gn new file mode 100755 index 0000000..f9157ef --- /dev/null +++ b/services/BUILD.gn @@ -0,0 +1,65 @@ +# 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("//base/useriam/coauth/coauth.gni") + +config("coauth_private_config") { + include_dirs = [ + "//utils/system/safwk/native/include", + "//base/useriam/coauth/frameworks/innerkitsimpl/distributeddatafwk/include", + "${coauth_frameworks_path}/kitsimpl/include", + "//base/useriam/coauth/services/mock/include", + ] +} + +config("coauth_public_config") { + include_dirs = [ + "include", + ] +} + +ohos_shared_library("coauthservice") { + sources = [ + "src/coauth_service.cpp", + "src/coauth_stub.cpp", + "src/auth_res_pool.cpp", + "src/coauth_thread_pool.cpp", + "src/auth_res_manager.cpp", + "src/coauth_manager.cpp", + "src/executor_messenger.cpp", + "mock/src/schedule_ca_mock.cpp", + ] + + configs = [ + "${coauth_utils_path}:utils_config", + ":coauth_private_config", + ] + + public_configs = [ ":coauth_public_config" ] + + deps = [ + "${coauth_innerkits_path}:coauth_framework", + "//drivers/peripheral/display/hal:hdi_display_device", + "//utils/native/base:utils", + "//base/startup/syspara_lite/interfaces/innerkits/native/syspara:syspara", + ] + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_standard:samgr_proxy", + ] + + part_name = "${coauth_native_part_name}" +} diff --git a/services/include/auth_res_manager.h b/services/include/auth_res_manager.h new file mode 100644 index 0000000..eb07d47 --- /dev/null +++ b/services/include/auth_res_manager.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef AUTH_RES_MANAGER_H +#define AUTH_RES_MANAGER_H + +#include +#include "auth_res_pool.h" +#include "auth_executor.h" +#include "iexecutor_callback.h" +#include "iquery_callback.h" +#include "coauth_interface.h" + +namespace OHOS { +namespace UserIAM { +namespace CoAuth { +class AuthResManager { +public: + uint64_t Register(std::shared_ptr executorInfo, sptr callback); + void QueryStatus(ResAuthExecutor &executorInfo, sptr callback); + int32_t FindExecutorCallback(uint64_t executorID, sptr &callback); + int32_t FindExecutorCallback(uint32_t authType, sptr &callback); + int32_t DeleteExecutorCallback(uint64_t executorID); + int32_t SaveScheduleCallback(uint64_t scheduleId, uint64_t executorNum, sptr callback); + int32_t FindScheduleCallback(uint64_t scheduleId, sptr &callback); + int32_t DeleteScheduleCallback(uint64_t scheduleId); +private: + class ResIExecutorCallbackDeathRecipient : public IRemoteObject::DeathRecipient { + public: + ResIExecutorCallbackDeathRecipient(uint64_t executorID, AuthResManager* parent); + ~ResIExecutorCallbackDeathRecipient() = default; + void OnRemoteDied(const wptr& remote) override; + + private: + uint64_t executorID_; + AuthResManager* parent_; + DISALLOW_COPY_AND_MOVE(ResIExecutorCallbackDeathRecipient); + }; + AuthResPool coAuthResPool_; +}; +} // namespace CoAuth +} // namespace UserIAM +} // namespace OHOS +#endif // AUTH_RES_MANAGER_H diff --git a/services/include/auth_res_pool.h b/services/include/auth_res_pool.h new file mode 100644 index 0000000..b765bdc --- /dev/null +++ b/services/include/auth_res_pool.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef AUTH_RES_POOL_H +#define AUTH_RES_POOL_H + +#include +#include +#include +#include +#include "coauth_stub.h" +#include "coauth_errors.h" +#include "coauth_hilog_wrapper.h" +#include "iexecutor_callback.h" + +namespace OHOS { +namespace UserIAM { +namespace CoAuth { + using ResAuthExecutor = UserIAM::AuthResPool::AuthExecutor; + using ResIQueryCallback = UserIAM::AuthResPool::IQueryCallback; + using ResAuthAttributes = UserIAM::AuthResPool::AuthAttributes; + using ResIExecutorCallback = UserIAM::AuthResPool::IExecutorCallback; + +class AuthResPool { +public: + typedef struct { + std::shared_ptr executorInfo; + sptr callback; + } ExecutorRegister; + + typedef struct { + uint64_t executorNum; + sptr callback; + } ScheduleRegister; + int32_t Insert(uint64_t executorID, std::shared_ptr executorInfo, + sptr callback); + int32_t Insert(uint64_t scheduleId, uint64_t executorNum, sptr callback); + int32_t FindExecutorCallback(uint64_t executorID, sptr &callback); + int32_t FindExecutorCallback(uint32_t authType, sptr &callback); + int32_t DeleteExecutorCallback(uint64_t executorID); + int32_t FindScheduleCallback(uint64_t scheduleId, sptr &callback); + int32_t ScheduleCountMinus(uint64_t scheduleId); + int32_t GetScheduleCount(uint64_t scheduleId, uint64_t &scheduleCount); + int32_t DeleteScheduleCallback(uint64_t scheduleId); + +private: + std::mutex authMutex_; + std::mutex scheMutex_; + std::map> authResPool_; + std::map> scheResPool_; +}; +} // namespace CoAuth +} // namespace UserIAM +} // namespace OHOS +#endif // AUTH_RES_POOL_H diff --git a/services/include/coauth_manager.h b/services/include/coauth_manager.h new file mode 100644 index 0000000..bb7811f --- /dev/null +++ b/services/include/coauth_manager.h @@ -0,0 +1,56 @@ +/* + * 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 COAUTH_MANAGER_H +#define COAUTH_MANAGER_H + +#include "auth_res_pool.h" +#include "auth_executor.h" +#include "iexecutor_callback.h" +#include "iquery_callback.h" +#include "auth_res_manager.h" + +namespace OHOS { +namespace UserIAM { +namespace CoAuth { +class CoAuthManager { +public: + void coAuth(uint64_t scheduleId, AuthInfo &authInfo, sptr callback); + int32_t Cancel(uint64_t scheduleId); + int32_t GetExecutorProp(ResAuthAttributes &conditions, std::shared_ptr values); + void SetExecutorProp(ResAuthAttributes &conditions, sptr callback); + void RegistResourceManager(AuthResManager* resMgr); + + void CoAuthHandle(uint64_t scheduleId, AuthInfo &authInfo, sptr callback); +private: + void SetAuthAttributes(std::shared_ptr commandAttrs, + ScheduleInfo &scheduleInfo, AuthInfo &authInfo); + class ResICoAuthCallbackDeathRecipient : public IRemoteObject::DeathRecipient { + public: + ResICoAuthCallbackDeathRecipient(uint64_t scheduleId, CoAuthManager* parent); + ~ResICoAuthCallbackDeathRecipient() = default; + void OnRemoteDied(const wptr& remote) override; + + private: + uint64_t scheduleId; + CoAuthManager* parent_; + DISALLOW_COPY_AND_MOVE(ResICoAuthCallbackDeathRecipient); + }; + AuthResManager* coAuthResMgrPtr_; +}; +} // namespace CoAuth +} // namespace UserIAM +} // namespace OHOS +#endif // COAUTH_MANAGER_H diff --git a/services/include/coauth_service.h b/services/include/coauth_service.h new file mode 100755 index 0000000..93874c6 --- /dev/null +++ b/services/include/coauth_service.h @@ -0,0 +1,59 @@ +/* + * 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 COAUTH_SERVICE_H +#define COAUTH_SERVICE_H + +#include "coauth_stub.h" +#include "iset_prop_callback.h" +#include "auth_attributes.h" +#include "coauth_info_define.h" +#include "auth_res_manager.h" +#include "coauth_manager.h" +#include "auth_info.h" +#include "coauth_hilog_wrapper.h" +#include "coauth_errors.h" + +namespace OHOS { +namespace UserIAM { +namespace CoAuth { +const int CHECK_TIMES = 3; +const int SLEEP_TIME = 3; +enum class CoAuthRunningState { STATE_STOPPED, STATE_RUNNING }; + +class CoAuthService : public SystemAbility, public CoAuthStub { +public: + DECLEAR_SYSTEM_ABILITY(CoAuthService); + explicit CoAuthService(int32_t systemAbilityId, bool runOnCreate = false); + virtual ~CoAuthService() override; + void OnStart() override; + void OnStop() override; + virtual uint64_t Register(std::shared_ptr executorInfo, + const sptr &callback) override; + virtual void QueryStatus(ResAuthExecutor &executorInfo, const sptr &callback) override; + virtual void coAuth(uint64_t scheduleId, AuthInfo &authInfo, const sptr &callback) override; + virtual int32_t Cancel(uint64_t scheduleId) override; + virtual int32_t GetExecutorProp(ResAuthAttributes &conditions, std::shared_ptr values) override; + virtual void SetExecutorProp(ResAuthAttributes &conditions, const sptr &callback) override; + +private: + CoAuthRunningState state_ = CoAuthRunningState::STATE_STOPPED; + AuthResManager authResMgr_; + CoAuthManager coAuthMgr_; +}; +} // namespace CoAuth +} // namespace UserIAM +} // namespace OHOS +#endif // COAUTH_SERVICE_H diff --git a/services/include/coauth_stub.h b/services/include/coauth_stub.h new file mode 100755 index 0000000..a985834 --- /dev/null +++ b/services/include/coauth_stub.h @@ -0,0 +1,46 @@ +/* + * 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 COAUTH_STUB_H +#define COAUTH_STUB_H + +#include +#include "co_auth.h" +#include "auth_attributes.h" + +namespace OHOS { +namespace UserIAM { +namespace CoAuth { +const std::string PERMISSION_AUTH_RESPOOL = "ohos.permission.ACCESS_AUTH_RESPOOL"; +const std::string PERMISSION_ACCESS_COAUTH = "ohos.permission.ACCESS_COAUTH"; + +class CoAuthStub : public IRemoteStub { +public: + virtual int OnRemoteRequest( + uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; + +private: + int32_t RegisterStub(MessageParcel& data, MessageParcel& reply); + int32_t QueryStatusStub(MessageParcel& data, MessageParcel& reply); + int32_t coAuthStub(MessageParcel &data, MessageParcel &reply); + int32_t CancelStub(MessageParcel &data, MessageParcel &reply); + int32_t GetExecutorPropStub(MessageParcel &data, MessageParcel &reply); + int32_t SetExecutorPropStub(MessageParcel &data, MessageParcel &reply); + void ReadAuthExecutor(AuthResPool::AuthExecutor &executorInfo, MessageParcel& data); +}; +} // namespace CoAuth +} // namespace UserIAM +} // namespace OHOS +#endif // COAUTH_STUB_H \ No newline at end of file diff --git a/services/include/coauth_thread_pool.h b/services/include/coauth_thread_pool.h new file mode 100644 index 0000000..f27d367 --- /dev/null +++ b/services/include/coauth_thread_pool.h @@ -0,0 +1,39 @@ +/* + * 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 COAUTH_SERVICES_INCLUDE_COAUTH_THREAD_POOL_H +#define COAUTH_SERVICES_INCLUDE_COAUTH_THREAD_POOL_H +#include +#include "nocopyable.h" +#include "thread_pool.h" + +namespace OHOS { +namespace UserIAM { +namespace CoAuth { +class CoAuthThreadPool : public ThreadPool { +public: + CoAuthThreadPool(); + virtual ~CoAuthThreadPool(); + static std::shared_ptr GetInstance(); + +private: + static std::mutex mutex_; + static std::shared_ptr instance_; +}; +} // namespace CoAuth +} // namespace UserIAM +} // namespace OHOS + +#endif // COAUTH_SERVICES_INCLUDE_COAUTH_THREAD_POOL_H diff --git a/services/mock/include/coauth_interface.h b/services/mock/include/coauth_interface.h new file mode 100644 index 0000000..3d70931 --- /dev/null +++ b/services/mock/include/coauth_interface.h @@ -0,0 +1,63 @@ +/* + * 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 USER_IAM_COAUTH_INTERFACE +#define USER_IAM_COAUTH_INTERFACE + +#include "vector" +#include "common_defines.h" + +namespace OHOS { +namespace UserIAM { +namespace CoAuth { +typedef struct { + uint32_t scheduleResult; + uint64_t scheduleId; + uint32_t authType; + uint64_t authSubType; + uint64_t templateId; + uint32_t scheduleMode; + uint32_t version; + uint64_t time; + uint8_t sign[SIGN_LEN]; +} ScheduleToken; + +typedef struct { + uint32_t authType; + uint64_t authAbility; + uint32_t esl; + uint32_t executorType; + uint8_t publicKey[PUBLIC_KEY_LEN]; +} ExecutorInfo; + +typedef struct { + std::vector executors; + uint64_t templateId; + uint64_t authSubType; + uint32_t scheduleMode; +} ScheduleInfo; + +int32_t GetScheduleInfo(uint64_t scheduleId, ScheduleInfo &scheduleInfo); +int32_t DeleteScheduleInfo(uint64_t scheduleId, ScheduleInfo &scheduleInfo); +int32_t GetScheduleToken(std::vector executorFinishMsg, ScheduleToken &scheduleToken); + +int32_t ExecutorRegister(ExecutorInfo executorInfo, uint64_t &executorId); +int32_t ExecutorUnRegister(uint64_t executorId); +bool IsExecutorExist(uint32_t authType); +} +} +} + +#endif // USER_IAM_COAUTH_INTERFACE \ No newline at end of file diff --git a/services/mock/include/common_defines.h b/services/mock/include/common_defines.h new file mode 100644 index 0000000..6dccc7e --- /dev/null +++ b/services/mock/include/common_defines.h @@ -0,0 +1,22 @@ +/* + * 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 USER_IAM_INTERFACE_COMMON_DEFINES +#define USER_IAM_INTERFACE_COMMON_DEFINES + +#define SIGN_LEN 32 +#define PUBLIC_KEY_LEN 32 + +#endif // USER_IAM_INTERFACE_COMMON_DEFINES \ No newline at end of file diff --git a/services/mock/include/useriam_common.h b/services/mock/include/useriam_common.h new file mode 100644 index 0000000..ac2e25f --- /dev/null +++ b/services/mock/include/useriam_common.h @@ -0,0 +1,44 @@ +/* + * 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 USER_IAM_COMMON_INTERFACE +#define USER_IAM_COMMON_INTERFACE + +#include "vector" +#include "stdint.h" +#include "coauth_info_define.h" + +namespace OHOS { +namespace UserIAM { +namespace Common { +int32_t Init() +{ + return SUCCESS; +} + +int32_t Close() +{ + return SUCCESS; +} + +bool IsIAMInited() +{ + return false; +} +} +} +} + +#endif // USER_IAM_COMMON_INTERFACE \ No newline at end of file diff --git a/services/mock/src/schedule_ca_mock.cpp b/services/mock/src/schedule_ca_mock.cpp new file mode 100644 index 0000000..bb92dc1 --- /dev/null +++ b/services/mock/src/schedule_ca_mock.cpp @@ -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. + */ + +#include "coauth_interface.h" +namespace OHOS { +namespace UserIAM { +namespace CoAuth { + static uint64_t id = 10000; + static ExecutorInfo exeInfo; + const uint32_t templateId = 10000; + const uint32_t addScheduleMode = 0; + const uint32_t deleteScheduleMode = 2; + +int32_t GetScheduleInfo(uint64_t scheduleId, ScheduleInfo &scheduleInfo) +{ + exeInfo.authType = 1; + exeInfo.authAbility = 1; + exeInfo.esl = 1; + exeInfo.executorType = 1; + exeInfo.publicKey[1] = 'a'; + scheduleInfo.templateId = templateId; + scheduleInfo.authSubType = 1; + scheduleInfo.scheduleMode = addScheduleMode; + scheduleInfo.executors.push_back(exeInfo); + return 0; +} +int32_t DeleteScheduleInfo(uint64_t scheduleId, ScheduleInfo &scheduleInfo) +{ + exeInfo.authType = 1; + exeInfo.authAbility = 1; + exeInfo.esl = 1; + exeInfo.executorType = 1; + exeInfo.publicKey[0] = 'a'; + scheduleInfo.templateId = templateId; + scheduleInfo.authSubType = 1; + scheduleInfo.scheduleMode = deleteScheduleMode; + scheduleInfo.executors.push_back(exeInfo); + return 0; +} +int32_t GetScheduleToken(std::vector executorFinishMsg, ScheduleToken &scheduleToken) +{ + return 0; +} +int32_t ExecutorRegister(ExecutorInfo executorInfo, uint64_t &executorId) +{ + id += 1; + executorId = id; + return 0; +} +int32_t ExecutorUnRegister(uint64_t executorId) +{ + return 0; +} +bool IsExecutorExist(uint32_t authType) +{ + return false; +} +} // namespace CoAuth +} // namespace UserIAM +} // namespace OHOS \ No newline at end of file diff --git a/services/src/auth_res_manager.cpp b/services/src/auth_res_manager.cpp new file mode 100644 index 0000000..aabb777 --- /dev/null +++ b/services/src/auth_res_manager.cpp @@ -0,0 +1,151 @@ +/* + * 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 "executor_messenger.h" +#include "auth_res_manager.h" + +namespace OHOS { +namespace UserIAM { +namespace CoAuth { +/* Register the executor, pass in the executor information and the callback returns the executor ID. */ +uint64_t AuthResManager::Register(std::shared_ptr executorInfo, sptr callback) +{ + int32_t result = SUCCESS; + uint64_t executorId = INVALID_EXECUTOR_ID; + if (executorInfo == nullptr || callback == nullptr) { + COAUTH_HILOGE(MODULE_SERVICE, "executorInfo or callback is nullptr"); + return executorId; + } + ExecutorInfo info; + std::vector publicKey; + AuthType authType; + ExecutorSecureLevel esl; + ExecutorType exeType; + executorInfo->GetAuthType(authType); + executorInfo->GetAuthAbility(info.authAbility); + executorInfo->GetExecutorSecLevel(esl); + executorInfo->GetExecutorType(exeType); + executorInfo->GetPublicKey(publicKey); + info.authType = authType; + info.esl = esl; + info.executorType = exeType; + if (publicKey.size() > PUBLIC_KEY_LEN) { + COAUTH_HILOGE(MODULE_SERVICE, "publicKey is wrong!"); + return executorId; + } else { + for (std::size_t i = 0; i < publicKey.size(); i++) { + info.publicKey[i] = publicKey[i]; + } + } + result = ExecutorRegister(info, executorId); // call TA + if (result == SUCCESS) { + sptr dr = new ResIExecutorCallbackDeathRecipient(executorId, this); + if (!callback->AsObject()->AddDeathRecipient(dr)) { + COAUTH_HILOGE(MODULE_INNERKIT, "Failed to add death recipient ResIExecutorCallbackDeathRecipient"); + return INVALID_EXECUTOR_ID; + } + coAuthResPool_.Insert(executorId, executorInfo, callback); // Cache executorId + COAUTH_HILOGI(MODULE_SERVICE, "register is sucessfull!"); + // Assign messenger + sptr messenger = + new UserIAM::AuthResPool::ExecutorMessenger(&coAuthResPool_); + callback->OnMessengerReady(messenger); + COAUTH_HILOGD(MODULE_SERVICE, "register is sucessfull,exeID is XXXX%{public}04llx!!!", executorId); + return executorId; // executorId returned after successful registration + } + if (result == FAIL) { + COAUTH_HILOGE(MODULE_SERVICE, "register is failure!"); + return INVALID_EXECUTOR_ID; // If the registration fails, an invalid id0 is returned + } + return executorId; +} + +/* Query whether the executor is registered */ +void AuthResManager::QueryStatus(ResAuthExecutor &executorInfo, sptr callback) +{ + bool isExist = false; + int32_t result = SUCCESS; + AuthType authType; + if (callback == nullptr) { + COAUTH_HILOGE(MODULE_SERVICE, "callback is nullptr"); + return; + } + result = executorInfo.GetAuthType(authType); + if (result == SUCCESS) { + COAUTH_HILOGI(MODULE_SERVICE, "GetAuthType is SUCESS"); + isExist = IsExecutorExist(authType); // call TA + } else { + COAUTH_HILOGE(MODULE_SERVICE, "GetAuthType is failure"); + } + if (isExist == false) { + COAUTH_HILOGE(MODULE_SERVICE, "queryustatus executor register is not exist!"); + } else { + COAUTH_HILOGI(MODULE_SERVICE, "queryustatus executor register is exist!"); + } + callback->OnResult(isExist ? SUCCESS : FAIL); +} + +int32_t AuthResManager::FindExecutorCallback(uint64_t executorID, + sptr &callback) +{ + return coAuthResPool_.FindExecutorCallback(executorID, callback); +} + +int32_t AuthResManager::FindExecutorCallback(uint32_t authType, + sptr &callback) +{ + return coAuthResPool_.FindExecutorCallback(authType, callback); +} + +int32_t AuthResManager::DeleteExecutorCallback(uint64_t executorID) +{ + return coAuthResPool_.DeleteExecutorCallback(executorID); +} + +int32_t AuthResManager::SaveScheduleCallback(uint64_t scheduleId, uint64_t executorNum, sptr callback) +{ + return coAuthResPool_.Insert(scheduleId, executorNum, callback); +} + +int32_t AuthResManager::FindScheduleCallback(uint64_t scheduleId, sptr &callback) +{ + return coAuthResPool_.FindScheduleCallback(scheduleId, callback); +} + +int32_t AuthResManager::DeleteScheduleCallback(uint64_t scheduleId) +{ + return coAuthResPool_.DeleteScheduleCallback(scheduleId); +} + +AuthResManager::ResIExecutorCallbackDeathRecipient::ResIExecutorCallbackDeathRecipient( + uint64_t executorID, AuthResManager* parent) : executorID_(executorID), parent_(parent) +{ +} + +void AuthResManager::ResIExecutorCallbackDeathRecipient::OnRemoteDied(const wptr& remote) +{ + if (remote == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "ExecutorCallback OnRemoteDied failed, remote is nullptr"); + return; + } + + if (parent_ != nullptr) { + parent_->DeleteExecutorCallback(executorID_); + } + COAUTH_HILOGE(MODULE_INNERKIT, "ResIExecutorCallbackDeathRecipient::Recv death notice."); +} +} // namespace CoAuth +} // namespace UserIAM +} // namespace OHOS \ No newline at end of file diff --git a/services/src/auth_res_pool.cpp b/services/src/auth_res_pool.cpp new file mode 100644 index 0000000..a1a8eb1 --- /dev/null +++ b/services/src/auth_res_pool.cpp @@ -0,0 +1,175 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "auth_res_pool.h" +#include "coauth_info_define.h" + +namespace OHOS { +namespace UserIAM { +namespace CoAuth { +int32_t AuthResPool::Insert(uint64_t executorID, std::shared_ptr executorInfo, + sptr callback) +{ + int32_t resultCode = SUCCESS; + std::lock_guard lock(authMutex_); + auto executorRegister = std::make_shared(); + executorRegister->executorInfo = executorInfo; + executorRegister->callback = callback; + authResPool_.insert(std::make_pair(executorID, executorRegister)); + if (authResPool_.begin() != authResPool_.end()) { + resultCode = SUCCESS; + COAUTH_HILOGI(MODULE_SERVICE, "authResPool_ insert success"); + } else { + resultCode = FAIL; + COAUTH_HILOGE(MODULE_SERVICE, "authResPool_ is null"); + } + return resultCode; +} + +int32_t AuthResPool::Insert(uint64_t scheduleId, uint64_t executorNum, sptr callback) +{ + int32_t resultCode = 0; + std::lock_guard lock(scheMutex_); + auto scheduleRegister = std::make_shared(); + scheduleRegister->executorNum = executorNum; + scheduleRegister->callback = callback; + scheResPool_.insert(std::make_pair(scheduleId, scheduleRegister)); + if (scheResPool_.begin() != scheResPool_.end()) { + resultCode = SUCCESS; + COAUTH_HILOGE(MODULE_SERVICE, "scheResPool_ is not null"); + } else { + resultCode = FAIL; + COAUTH_HILOGE(MODULE_SERVICE, "scheResPool_ is null"); + } + return resultCode; +} + +int32_t AuthResPool::FindExecutorCallback(uint64_t executorID, sptr &callback) +{ + int32_t resultCode = 0; + std::lock_guard lock(authMutex_); + std::map> ::iterator iter = authResPool_.find(executorID); + if (iter != authResPool_.end()) { + resultCode = SUCCESS; + callback = iter->second->callback; + COAUTH_HILOGI(MODULE_SERVICE, "callback is found"); + } else { + resultCode = FAIL; + COAUTH_HILOGE(MODULE_SERVICE, "callback is not found, size is %{public}d", authResPool_.size()); + } + return resultCode; +} + +int32_t AuthResPool::FindExecutorCallback(uint32_t authType, sptr &callback) +{ + int32_t resultCode = SUCCESS; + AuthType getAuthType; + std::lock_guard lock(authMutex_); + std::map> ::iterator iter; + for (iter = authResPool_.begin(); iter != authResPool_.end(); iter++) { + iter->second->executorInfo->GetAuthType(getAuthType); + if (getAuthType == (int32_t)authType) { + callback = iter->second->callback; + COAUTH_HILOGI(MODULE_SERVICE, "Executor callback is found"); + return resultCode; + } + } + COAUTH_HILOGE(MODULE_SERVICE, "Executor callback is not found, size is %{public}d", authResPool_.size()); + callback = nullptr; + resultCode = FAIL; + return resultCode; +} + +int32_t AuthResPool::DeleteExecutorCallback(uint64_t executorID) +{ + int32_t resultCode = SUCCESS; + std::lock_guard lock(authMutex_); + std::map> ::iterator iter = authResPool_.find(executorID); + if (iter != authResPool_.end()) { + authResPool_.erase(iter); + resultCode = SUCCESS; + COAUTH_HILOGI(MODULE_SERVICE, "executor callback XXXX%{public}04llx is deleted", executorID); + } else { + resultCode = FAIL; + COAUTH_HILOGE(MODULE_SERVICE, "executorID is not found and do not delete callback"); + } + return resultCode; +} + +int32_t AuthResPool::FindScheduleCallback(uint64_t scheduleId, sptr &callback) +{ + int32_t resultCode = SUCCESS; + std::lock_guard lock(scheMutex_); + std::map> ::iterator iter = scheResPool_.find(scheduleId); + if (iter != scheResPool_.end()) { + resultCode = SUCCESS; + callback = iter->second->callback; + COAUTH_HILOGI(MODULE_SERVICE, "Schedule callback is found"); + } else { + resultCode = FAIL; + COAUTH_HILOGE(MODULE_SERVICE, "Schedule callback is not found"); + } + return resultCode; +} + +int32_t AuthResPool::ScheduleCountMinus(uint64_t scheduleId) +{ + int32_t resultCode = SUCCESS; + std::lock_guard lock(scheMutex_); + std::map> ::iterator iter = scheResPool_.find(scheduleId); + if (iter != scheResPool_.end()) { + iter->second->executorNum--; + resultCode = SUCCESS; + COAUTH_HILOGD(MODULE_SERVICE, "Schedule callback is found"); + } else { + resultCode = FAIL; + COAUTH_HILOGE(MODULE_SERVICE, "Schedule callback is not found"); + } + return resultCode; +} + +int32_t AuthResPool::GetScheduleCount(uint64_t scheduleId, uint64_t &scheduleCount) +{ + int32_t resultCode = SUCCESS; + std::lock_guard lock(scheMutex_); + std::map> ::iterator iter = scheResPool_.find(scheduleId); + if (iter != scheResPool_.end()) { + scheduleCount = iter->second->executorNum; + resultCode = SUCCESS; + COAUTH_HILOGD(MODULE_SERVICE, "Schedule callback is found"); + } else { + resultCode = FAIL; + COAUTH_HILOGE(MODULE_SERVICE, "Schedule callback is not found"); + } + return resultCode; +} + +int32_t AuthResPool::DeleteScheduleCallback(uint64_t scheduleId) +{ + int32_t resultCode = SUCCESS; + std::lock_guard lock(scheMutex_); + std::map> ::iterator iter = scheResPool_.find(scheduleId); + if (iter != scheResPool_.end()) { + scheResPool_.erase(iter); + resultCode = SUCCESS; + COAUTH_HILOGD(MODULE_SERVICE, "Schedule callback is found"); + } else { + resultCode = FAIL; + COAUTH_HILOGE(MODULE_SERVICE, "scheduleId is not found and do not delete callback"); + } + return resultCode; +} +} // namespace CoAuth +} // namespace UserIAM +} // namespace OHOS \ No newline at end of file diff --git a/services/src/coauth_manager.cpp b/services/src/coauth_manager.cpp new file mode 100644 index 0000000..6ee8dff --- /dev/null +++ b/services/src/coauth_manager.cpp @@ -0,0 +1,216 @@ +/* + * 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 "coauth_manager.h" +#include "coauth_thread_pool.h" + +namespace OHOS { +namespace UserIAM { +namespace CoAuth { +/* Apply for collaborative scheduling */ +void CoAuthManager::coAuth(uint64_t scheduleId, AuthInfo &authInfo, sptr callback) +{ + CoAuthHandle(scheduleId, authInfo, callback); +} + +void CoAuthManager::CoAuthHandle(uint64_t scheduleId, AuthInfo &authInfo, sptr callback) +{ + if (callback == nullptr) { + COAUTH_HILOGI(MODULE_SERVICE, "Schedule callback is null."); + return; + } + int32_t executeRet = SUCCESS; + ScheduleInfo scheduleInfo; + std::vector scheduleToken; + int32_t ret = GetScheduleInfo(scheduleId, scheduleInfo); + if (ret != SUCCESS) { + COAUTH_HILOGI(MODULE_SERVICE, "Schedule faild."); + return callback->OnFinish(ret, scheduleToken); + } + std::size_t executorNum = scheduleInfo.executors.size(); + if (executorNum == 0) { + COAUTH_HILOGE(MODULE_SERVICE, "executorId does not exist."); + return callback->OnFinish(FAIL, scheduleToken); + } + sptr dr = new ResICoAuthCallbackDeathRecipient(scheduleId, this); + if ((!callback->AsObject()->AddDeathRecipient(dr))) { + COAUTH_HILOGE(MODULE_INNERKIT, "Failed to add death recipient ResICoAuthCallbackDeathRecipient"); + } + int32_t saveRet = coAuthResMgrPtr_->SaveScheduleCallback(scheduleId, executorNum, callback); + if (saveRet != SUCCESS) { + COAUTH_HILOGW(MODULE_SERVICE, "Save schedule callback error."); + return callback->OnFinish(saveRet, scheduleToken); + } + for (std::size_t i = 0; i < executorNum; i++) { + uint32_t authType = scheduleInfo.executors[i].authType; + COAUTH_HILOGD(MODULE_SERVICE, "get authType = XXXX%{public}04d", authType); + sptr executorCallback; + std::vector publicKey(scheduleInfo.executors[i].publicKey, + scheduleInfo.executors[i].publicKey + PUBLIC_KEY_LEN); + int32_t findRet = coAuthResMgrPtr_->FindExecutorCallback(authType, executorCallback); + if ((findRet != 0) || (executorCallback == nullptr)) { + COAUTH_HILOGE(MODULE_SERVICE, "executor callback not found."); + continue; + } + auto commandAttrs = std::make_shared(); + SetAuthAttributes(commandAttrs, scheduleInfo, authInfo); + executeRet |= executorCallback->OnBeginExecute(scheduleId, publicKey, commandAttrs); + } + // set timeout + if (executeRet != SUCCESS) { + COAUTH_HILOGW(MODULE_SERVICE, "There are one or more failures in execution."); + return callback->OnFinish(executeRet, scheduleToken); + } +} + +void CoAuthManager::SetAuthAttributes(std::shared_ptr commandAttrs, + ScheduleInfo &scheduleInfo, AuthInfo &authInfo) +{ + std::string callerNameString; + authInfo.GetPkgName(callerNameString); + std::vector callerName; + callerName.assign(callerNameString.begin(), callerNameString.end()); + uint64_t value; + authInfo.GetCallerUid(value); + commandAttrs->SetUint32Value(AUTH_SCHEDULE_MODE, scheduleInfo.scheduleMode); + commandAttrs->SetUint64Value(AUTH_SUBTYPE, scheduleInfo.authSubType); + commandAttrs->SetUint64Value(AUTH_TEMPLATE_ID, scheduleInfo.templateId); + commandAttrs->SetUint64Value(AUTH_CALLER_UID, value); + commandAttrs->SetUint8ArrayValue(AUTH_CALLER_NAME, callerName); +} + +/* Cancel collaborative schedule */ +int32_t CoAuthManager::Cancel(uint64_t scheduleId) +{ + int32_t cancelRet = SUCCESS; + int32_t executeRet = SUCCESS; + ScheduleInfo scheduleInfo; + sptr callback = nullptr; + cancelRet = DeleteScheduleInfo(scheduleId, scheduleInfo); // call TA + if (cancelRet != SUCCESS) { + COAUTH_HILOGE(MODULE_SERVICE, "cancel is failure"); + return FAIL; + } + COAUTH_HILOGI(MODULE_SERVICE, "cancel is sucessfull"); + std::size_t executorNum = scheduleInfo.executors.size(); + if (executorNum == 0) { + COAUTH_HILOGE(MODULE_SERVICE, "executorId does not exist."); + return FAIL; + } + for (std::size_t i = 0; i < executorNum; i++) { + uint32_t authType = scheduleInfo.executors[i].authType; + sptr executorCallback; + COAUTH_HILOGD(MODULE_SERVICE, "get exeID = XXXX%{public}04d", authType); + int32_t findRet = coAuthResMgrPtr_->FindExecutorCallback(authType, executorCallback); + if ((findRet != 0) || (executorCallback == nullptr)) { + COAUTH_HILOGE(MODULE_SERVICE, "executor callback not found."); + continue; + } + auto commandAttrs = std::make_shared(); + commandAttrs->SetUint32Value(AUTH_SCHEDULE_MODE, scheduleInfo.scheduleMode); + commandAttrs->SetUint64Value(AUTH_SUBTYPE, scheduleInfo.authSubType); + commandAttrs->SetUint64Value(AUTH_TEMPLATE_ID, scheduleInfo.templateId); + executeRet |= executorCallback->OnEndExecute(scheduleId, commandAttrs); + } + if (executeRet != SUCCESS) { + COAUTH_HILOGW(MODULE_SERVICE, "There are one or more failures when canceling."); + } + return executeRet; +} + +/* Set executor properties */ +void CoAuthManager::SetExecutorProp(ResAuthAttributes &conditions, sptr callback) +{ + /* + * To delete user credential information, the caller must be userauth, + * The first caller who locks and unlocks the template must be useridm + */ + + if (callback == nullptr) { + COAUTH_HILOGE(MODULE_SERVICE, "callback is nullptr"); + return; + } + + uint32_t result = FAIL; + sptr execallback = nullptr; + std::vector extraInfo; + uint32_t authType; + conditions.GetUint32Value(AUTH_TYPE, authType); + COAUTH_HILOGD(MODULE_SERVICE, "get authType = XXXX%{public}d", authType); + coAuthResMgrPtr_->FindExecutorCallback(authType, execallback); + if (execallback == nullptr) { + COAUTH_HILOGE(MODULE_SERVICE, "executor callback not found."); + return callback->OnResult(result, extraInfo); + } + std::vector buffer; + std::shared_ptr properties = std::make_shared(); + conditions.Pack(buffer); + properties->Unpack(buffer); + result = execallback->OnSetProperty(properties); + if (result != SUCCESS) { + COAUTH_HILOGE(MODULE_SERVICE, "set properties failure"); + } + COAUTH_HILOGI(MODULE_SERVICE, "set properties sucessfull"); + callback->OnResult(result, extraInfo); +} + +int32_t CoAuthManager::GetExecutorProp(ResAuthAttributes &conditions, std::shared_ptr values) +{ + int32_t retCode = SUCCESS; + uint32_t authType; + sptr execallback = nullptr; + conditions.GetUint32Value(AUTH_TYPE, authType); + COAUTH_HILOGD(MODULE_SERVICE, "authType is %{public}d", authType); + coAuthResMgrPtr_->FindExecutorCallback(authType, execallback); + if (execallback == nullptr) { + COAUTH_HILOGE(MODULE_SERVICE, "executor callback not found."); + return FAIL; + } + std::vector buffer; + std::shared_ptr properties = std::make_shared(); + conditions.Pack(buffer); + properties->Unpack(buffer); + retCode = execallback->OnGetProperty(properties, values); + if (retCode != SUCCESS) { + COAUTH_HILOGE(MODULE_SERVICE, "get properties failure"); + } + COAUTH_HILOGI(MODULE_SERVICE, "get properties end"); + return retCode; +} + +void CoAuthManager::RegistResourceManager(AuthResManager* resMgr) +{ + coAuthResMgrPtr_ = resMgr; +} +CoAuthManager::ResICoAuthCallbackDeathRecipient::ResICoAuthCallbackDeathRecipient( + uint64_t scheduleId, CoAuthManager* parent) : scheduleId(scheduleId), parent_(parent) +{ +} + +void CoAuthManager::ResICoAuthCallbackDeathRecipient::OnRemoteDied(const wptr& remote) +{ + if (remote == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "CoAuthCallback OnRemoteDied failed, remote is nullptr"); + return; + } + + if (parent_ != nullptr) { + parent_->coAuthResMgrPtr_->DeleteScheduleCallback(scheduleId); + } + COAUTH_HILOGE(MODULE_INNERKIT, "ResICoAuthCallbackDeathRecipient::Recv death notice."); +} +} // namespace CoAuth +} // namespace UserIAM +} // namespace OHOS \ No newline at end of file diff --git a/services/src/coauth_service.cpp b/services/src/coauth_service.cpp new file mode 100755 index 0000000..db81b4f --- /dev/null +++ b/services/src/coauth_service.cpp @@ -0,0 +1,177 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "useriam_common.h" +#include "coauth_service.h" + +namespace OHOS { +namespace UserIAM { +namespace CoAuth { +void CheckSystemAbility() +{ + sptr sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (sam == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "Failed to get system ability manager"); + return; + } + for (int i = 0; i < CHECK_TIMES; i++) { + bool isExist = false; + sam->CheckSystemAbility(PIN_AUTH_SERVICE_SA_ID, isExist); + if (!isExist) { + COAUTH_HILOGI(MODULE_INNERKIT, "PIN_AUTH_SERVICE is not exist, start pin auth ability failed, to do next"); + } else { + COAUTH_HILOGI(MODULE_INNERKIT, "PIN_AUTH_SERVICE is exist, start pin auth ability success"); + return; + } + if (i < CHECK_TIMES - 1) { + sleep(SLEEP_TIME); + } + } + COAUTH_HILOGI(MODULE_INNERKIT, "start pin auth ability all failed"); +} + +REGISTER_SYSTEM_ABILITY_BY_ID(CoAuthService, COOPERATION_AUTHENTICATION_SERVICE_SA_ID, true); +CoAuthService::CoAuthService(int32_t systemAbilityId, bool runOnCreate) + : SystemAbility(systemAbilityId, runOnCreate) +{ + coAuthMgr_.RegistResourceManager(&authResMgr_); +} + +CoAuthService::~CoAuthService() +{ +} + +void CoAuthService::OnStart() +{ + if (state_ == CoAuthRunningState::STATE_RUNNING) { + COAUTH_HILOGW(MODULE_SERVICE, "CoAuthService has already started"); + return; + } + COAUTH_HILOGI(MODULE_SERVICE, "Start service"); + if (!Publish(this)) { + COAUTH_HILOGE(MODULE_SERVICE, "Failed to publish service"); + return; + } + state_ = CoAuthRunningState::STATE_RUNNING; + + if (!Common::IsIAMInited()) { + if (Common::Init() != SUCCESS) { + COAUTH_HILOGI(MODULE_SERVICE, " IAM CA init failed"); + } + COAUTH_HILOGI(MODULE_SERVICE, " IAM CA init success"); + } else { + COAUTH_HILOGI(MODULE_SERVICE, " IAM CA is inited"); + } + + // Send registration broadcast + // Start other sevice + std::thread checkThread(OHOS::UserIAM::CoAuth::CheckSystemAbility); + checkThread.join(); +} + +void CoAuthService::OnStop() +{ + if (state_ == CoAuthRunningState::STATE_STOPPED) { + COAUTH_HILOGW(MODULE_SERVICE, "CoAuthService already stopped"); + return; + } + state_ = CoAuthRunningState::STATE_STOPPED; + + if (Common::IsIAMInited()) { + if (Common::Close() != SUCCESS) { + COAUTH_HILOGI(MODULE_SERVICE, " IAM CA Close failed"); + } + COAUTH_HILOGI(MODULE_SERVICE, " IAM CA close success"); + } else { + COAUTH_HILOGI(MODULE_SERVICE, " IAM CA is closed"); + } + COAUTH_HILOGI(MODULE_SERVICE, "Stop service"); +} + +/* Register the executor, pass in the executor information and the callback returns the executor ID. */ +uint64_t CoAuthService::Register(std::shared_ptr executorInfo, + const sptr &callback) +{ + if (executorInfo == nullptr) { + COAUTH_HILOGE(MODULE_SERVICE, "executorInfo is nullptr"); + return FAIL; + } + + if (callback == nullptr) { + COAUTH_HILOGE(MODULE_SERVICE, "callback is nullptr"); + return FAIL; + } + + uint64_t exeID = authResMgr_.Register(executorInfo, callback); + COAUTH_HILOGE(MODULE_SERVICE, "exeID is XXXX%{public}04llx", exeID); + return exeID; +} + +/* Query whether the executor is registered */ +void CoAuthService::QueryStatus(ResAuthExecutor &executorInfo, const sptr &callback) +{ + if (callback == nullptr) { + COAUTH_HILOGE(MODULE_SERVICE, "callback is nullptr"); + return; + } + return authResMgr_.QueryStatus(executorInfo, callback); +} + +/* Apply for collaborative scheduling */ +void CoAuthService::coAuth(uint64_t scheduleId, AuthInfo &authInfo, const sptr &callback) +{ + if (callback == nullptr) { + COAUTH_HILOGE(MODULE_SERVICE, "callback is nullptr"); + return; + } + return coAuthMgr_.coAuth(scheduleId, authInfo, callback); +} + +/* Cancel collaborative schedule */ +int32_t CoAuthService::Cancel(uint64_t scheduleId) +{ + return coAuthMgr_.Cancel(scheduleId); +} + +/* Set executor properties */ +void CoAuthService::SetExecutorProp(ResAuthAttributes &conditions, const sptr &callback) +{ + if (callback == nullptr) { + COAUTH_HILOGE(MODULE_SERVICE, "callback is nullptr"); + return; + } + return coAuthMgr_.SetExecutorProp(conditions, callback); +} + +/* Get executor properties */ +int32_t CoAuthService::GetExecutorProp(ResAuthAttributes &conditions, std::shared_ptr values) +{ + if (values == nullptr) { + COAUTH_HILOGE(MODULE_SERVICE, "values pointer is nullptr"); + return FAIL; + } + return coAuthMgr_.GetExecutorProp(conditions, values); +} +} // namespace CoAu +} // namespace UserIAM +} // namespace OHOS \ No newline at end of file diff --git a/services/src/coauth_stub.cpp b/services/src/coauth_stub.cpp new file mode 100755 index 0000000..41e472a --- /dev/null +++ b/services/src/coauth_stub.cpp @@ -0,0 +1,196 @@ +/* + * 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 "coauth_hilog_wrapper.h" +#include "coauth_errors.h" +#include "coauth_stub.h" + +namespace OHOS { +namespace UserIAM { +namespace CoAuth { +int32_t CoAuthStub::OnRemoteRequest(uint32_t code, MessageParcel &data, + MessageParcel &reply, MessageOption &option) +{ + COAUTH_HILOGD(MODULE_SERVICE, "CoAuthStub::OnRemoteRequest, cmd = %d, flags= %d", + code, option.GetFlags()); + std::u16string descripter = CoAuthStub::GetDescriptor(); + std::u16string remoteDescripter = data.ReadInterfaceToken(); + if (descripter != remoteDescripter) { + COAUTH_HILOGD(MODULE_SERVICE, "CoAuthStub::OnRemoteRequest failed, descriptor is not matched!"); + return E_GET_POWER_SERVICE_FAILED; + } + + switch (code) { + case static_cast(ICoAuth::COAUTH_EXECUTOR_REGIST): + return RegisterStub(data, reply); + case static_cast(ICoAuth::COAUTH_QUERY_STATUS): + return QueryStatusStub(data, reply); + case static_cast(ICoAuth::COAUTH_SCHEDULE_REQUEST): + return coAuthStub(data, reply); + case static_cast(ICoAuth::COAUTH_SCHEDULE_CANCEL): + return CancelStub(data, reply); + case static_cast(ICoAuth::COAUTH_GET_PROPERTY): + return GetExecutorPropStub(data, reply); + case static_cast(ICoAuth::COAUTH_SET_PROPERTY): + return SetExecutorPropStub(data, reply); + default: + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } +} + +void CoAuthStub::ReadAuthExecutor(AuthResPool::AuthExecutor &executorInfo, MessageParcel& data) +{ + int32_t authType = data.ReadInt32(); + executorInfo.SetAuthType(static_cast(authType)); + COAUTH_HILOGD(MODULE_INNERKIT, "ReadInt32,authType:%{public}d", authType); + + uint64_t authAbility = data.ReadUint64(); + executorInfo.SetAuthAbility(authAbility); + COAUTH_HILOGD(MODULE_INNERKIT, "ReadInt64,authAbility:%{public}llu", authAbility); + + int32_t executorSecLevel = data.ReadInt32(); + executorInfo.SetExecutorSecLevel(static_cast(executorSecLevel)); + COAUTH_HILOGD(MODULE_INNERKIT, "ReadInt32,executorSecLevel:%{public}d", executorSecLevel); + + int32_t executorType = data.ReadInt32(); + executorInfo.SetExecutorType(static_cast(executorType)); + COAUTH_HILOGD(MODULE_INNERKIT, "ReadInt32,executorSecLevel:%{public}d", executorSecLevel); + + std::vector publicKey; + data.ReadUInt8Vector(&publicKey); + executorInfo.SetPublicKey(publicKey); + + std::vector deviceId; + data.ReadUInt8Vector(&deviceId); + executorInfo.SetDeviceId(deviceId); +} + +int32_t CoAuthStub::RegisterStub(MessageParcel& data, MessageParcel& reply) +{ + std::shared_ptr executorInfo = std::make_shared(); + ReadAuthExecutor(*executorInfo, data); + sptr callback = iface_cast(data.ReadRemoteObject()); + if (callback == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "read IExecutorCallback is nullptr"); + return FAIL; + } + uint64_t ret = Register(executorInfo, callback); + if (!reply.WriteUint64(ret)) { + COAUTH_HILOGE(MODULE_INNERKIT, "failed to WriteInt32(ret)"); + return FAIL; + } + return SUCCESS; +} + +int32_t CoAuthStub::QueryStatusStub(MessageParcel& data, MessageParcel& reply) +{ + AuthResPool::AuthExecutor executorInfo; + ReadAuthExecutor(executorInfo, data); + sptr callback = iface_cast(data.ReadRemoteObject()); + if (callback == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "read IQueryCallback is nullptr"); + return FAIL; + } + + QueryStatus(executorInfo, callback); + return SUCCESS; +} + +int32_t CoAuthStub::coAuthStub(MessageParcel& data, MessageParcel& reply) +{ + AuthInfo authInfo; + + std::string GetPkgName = data.ReadString(); + authInfo.SetPkgName(GetPkgName); + uint64_t GetCallerUid = data.ReadUint64(); + authInfo.SetCallerUid(GetCallerUid); + COAUTH_HILOGD(MODULE_INNERKIT, "ReadUint64,GetCallerUid:%{public}llu", GetCallerUid); + + uint64_t scheduleId = data.ReadUint64(); + COAUTH_HILOGD(MODULE_INNERKIT, "ReadUint64,scheduleId:%{public}llu", scheduleId); + + sptr callback = iface_cast(data.ReadRemoteObject()); + if (callback == nullptr) { + COAUTH_HILOGE(MODULE_INNERKIT, "read ICoAuthCallback is nullptr"); + return FAIL; + } + + coAuth(scheduleId, authInfo, callback); + + return SUCCESS; +} + +int32_t CoAuthStub::CancelStub(MessageParcel& data, MessageParcel& reply) +{ + COAUTH_HILOGD(MODULE_SERVICE, "CoAuthStub: CancelStub enter"); + + uint64_t scheduleId = data.ReadUint64(); + COAUTH_HILOGD(MODULE_INNERKIT, "ReadUint64 scheduleId:%{public}llu", scheduleId); + + int ret = Cancel(scheduleId); + if (!reply.WriteInt32(ret)) { + COAUTH_HILOGE(MODULE_SERVICE, "failed to WriteInt32(ret)"); + return FAIL; + } + return SUCCESS; +} + +int32_t CoAuthStub::GetExecutorPropStub(MessageParcel& data, MessageParcel& reply) +{ + COAUTH_HILOGD(MODULE_SERVICE, "CoAuthStub: GetExecutorPropStub enter"); + std::vector buffer; + AuthResPool::AuthAttributes conditions; + data.ReadUInt8Vector(&buffer); + conditions.Unpack(buffer); + std::shared_ptr values = std::make_shared(); + if (values == nullptr) { + COAUTH_HILOGE(MODULE_SERVICE, "GetExecutorPropStub failed, values is nullptr"); + return FAIL; + } + buffer.clear(); + data.ReadUInt8Vector(&buffer); + values->Unpack(buffer); + + int32_t ret = GetExecutorProp(conditions, values); + if (!reply.WriteInt32(ret)) { + COAUTH_HILOGE(MODULE_SERVICE, "failed to WriteInt32(ret)"); + return FAIL; + } + + return SUCCESS; +} + +int32_t CoAuthStub::SetExecutorPropStub(MessageParcel& data, MessageParcel& reply) +{ + COAUTH_HILOGD(MODULE_SERVICE, "CoAuthStub: SetExecutorPropStub enter"); + std::vector buffer; + std::shared_ptr conditions = std::make_shared(); + + data.ReadUInt8Vector(&buffer); + conditions->Unpack(buffer); + + sptr callback = iface_cast(data.ReadRemoteObject()); + if (callback == nullptr) { + COAUTH_HILOGE(MODULE_SERVICE, "SetExecutorPropStub failed, callback is nullptr"); + return FAIL; + } + + SetExecutorProp(*conditions, callback); + + return SUCCESS; +} +} // namespace CoAuth +} // namespace UserIAM +} // namespace OHOS diff --git a/services/src/coauth_thread_pool.cpp b/services/src/coauth_thread_pool.cpp new file mode 100644 index 0000000..f2c3f27 --- /dev/null +++ b/services/src/coauth_thread_pool.cpp @@ -0,0 +1,45 @@ +/* + * 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 "coauth_thread_pool.h" + +namespace OHOS { +namespace UserIAM { +namespace CoAuth { +const int32_t COAUTH_THREAD_NUM = 20; +std::mutex CoAuthThreadPool::mutex_; +std::shared_ptr CoAuthThreadPool::instance_ = nullptr; +CoAuthThreadPool::CoAuthThreadPool() +{ + Start(COAUTH_THREAD_NUM); +} + +CoAuthThreadPool::~CoAuthThreadPool() +{ + Stop(); +} + +std::shared_ptr CoAuthThreadPool::GetInstance() +{ + if (instance_ == nullptr) { + std::lock_guard lock_l(mutex_); + if (instance_ == nullptr) { + instance_ = std::make_shared(); + } + } + return instance_; +} +} // namespace CoAuth +} // namespace UserIAM +} // namespace OHOS \ No newline at end of file diff --git a/services/src/executor_messenger.cpp b/services/src/executor_messenger.cpp new file mode 100644 index 0000000..3ad6894 --- /dev/null +++ b/services/src/executor_messenger.cpp @@ -0,0 +1,96 @@ +/* + * 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 "securec.h" +#include "coauth_interface.h" +#include "executor_messenger.h" + +namespace OHOS { +namespace UserIAM { +namespace AuthResPool { +ExecutorMessenger::ExecutorMessenger(UserIAM::CoAuth::AuthResPool* scheduleRes) +{ + ScheResPool_ = scheduleRes; +} + +int32_t ExecutorMessenger::SendData(uint64_t scheduleId, uint64_t transNum, int32_t srcType, + int32_t dstType, std::shared_ptr msg) +{ + if (ScheResPool_ == nullptr) { + COAUTH_HILOGE(MODULE_SERVICE, "ScheResPool_ is nullptr"); + return FAIL; + } else { + sptr callback; + int32_t findRet = ScheResPool_->FindScheduleCallback(scheduleId, callback); + if (findRet == SUCCESS && callback != nullptr) { + uint32_t acquire = 0; + // trans to acquireCode + callback->OnAcquireInfo(acquire); + COAUTH_HILOGD(MODULE_SERVICE, "feedback acquire info"); + } else { + COAUTH_HILOGE(MODULE_SERVICE, "ScheduleCallback not find"); + } + } + return SUCCESS; +} +int32_t ExecutorMessenger::Finish(uint64_t scheduleId, int32_t srcType, int32_t resultCode, + std::shared_ptr finalResult) +{ + COAUTH_HILOGD(MODULE_SERVICE, "ExecutorMessenger::Finish"); + if (finalResult == nullptr) { + COAUTH_HILOGE(MODULE_SERVICE, "finalResult is nullptr"); + return FAIL; + } + + if (ScheResPool_ == nullptr) { + COAUTH_HILOGE(MODULE_SERVICE, "ScheResPool_ is nullptr"); + return FAIL; + } else { + sptr callback; + uint64_t scheCount; + ScheResPool_->GetScheduleCount(scheduleId, scheCount); + if (scheCount > 1) { // The last one will sign the token + ScheResPool_->ScheduleCountMinus(scheduleId); + return SUCCESS; + } + int32_t findRet = ScheResPool_->FindScheduleCallback(scheduleId, callback); + if (findRet == SUCCESS && callback != nullptr) { + UserIAM::CoAuth::ScheduleToken signScheduleToken; + std::vector executorFinishMsg; + std::vector scheduleToken; + finalResult->GetUint8ArrayValue(AUTH_RESULT, executorFinishMsg); + int32_t signRet = UserIAM::CoAuth::GetScheduleToken(executorFinishMsg, signScheduleToken); + if (signRet != SUCCESS) { + return signRet; + } + + scheduleToken.resize(sizeof(UserIAM::CoAuth::ScheduleToken)); + if (memcpy_s(&scheduleToken[0], scheduleToken.size(), &signScheduleToken, + sizeof(UserIAM::CoAuth::ScheduleToken)) != EOK) { + COAUTH_HILOGE(MODULE_SERVICE, "copy scheduleToken failed"); + return FAIL; + } + callback->OnFinish(resultCode, scheduleToken); + COAUTH_HILOGD(MODULE_SERVICE, "feedback finish info"); + ScheResPool_->DeleteScheduleCallback(scheduleId); + } else { + COAUTH_HILOGE(MODULE_SERVICE, "ScheduleCallback not find"); + } + } + return SUCCESS; +} +} // namespace CoAuth +} // namespace UserIAM +} // namespace OHOS \ No newline at end of file diff --git a/test/BUILD.gn b/test/BUILD.gn new file mode 100755 index 0000000..ea1d917 --- /dev/null +++ b/test/BUILD.gn @@ -0,0 +1,21 @@ +# 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("//build/ohos.gni") +import("//base/useriam/coauth/coauth.gni") +#import("//foundation/distributeddatamgr/distributedgallery/distributedgallery.gni") +import("//build/test.gni") + +group("coauth_unittest_test") { + testonly = true + deps = [ "unittest:coauth_UT_test" ] +} diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn new file mode 100755 index 0000000..e1b1bf2 --- /dev/null +++ b/test/unittest/BUILD.gn @@ -0,0 +1,50 @@ +# 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("//build/ohos.gni") +import("//build/test.gni") +import("//base/useriam/coauth/coauth.gni") + +module_output_path = "coauthmgr/coauth_UT_test" + +ohos_unittest("coauth_UT_test") { + + module_out_path = module_output_path + + sources = [ + "//base/useriam/coauth/test/unittest/src/coauth_test.cpp", + ] + + include_dirs = [ + "include", + "//base/useriam/coauth/interfaces/innerkits/include", + "${coauth_service_path}/include", + "//utils/system/safwk/napi/include", + "${coauth_frameworks_path}/kitsimpl/include", + "${coauth_utils_path}/native/include", + "//foundation/distributeddatamgr/distributeddatamgr/frameworks/innerkitsimpl/distributeddatafwk/include", + ] + deps = [ + "${coauth_innerkits_path}:coauth_framework", + "//utils/native/base:utils", + "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/distributeddata:distributeddata_inner", + "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter:distributeddata_adapter", + ] + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_standard:samgr_proxy", + ] +} \ No newline at end of file diff --git a/test/unittest/include/coauth_test.h b/test/unittest/include/coauth_test.h new file mode 100644 index 0000000..e303f5b --- /dev/null +++ b/test/unittest/include/coauth_test.h @@ -0,0 +1,46 @@ +/* + * 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 COAUTH_TEST_H +#define COAUTH_TEST_H +#include "auth_executor.h" +#include "iexecutor_callback.h" +#include "executor_callback.h" +#include "coauth_proxy.h" +#include "auth_attributes.h" +#include "co_auth.h" +#include "auth_executor_registry.h" +#include "query_callback.h" +#include "coauth_hilog_wrapper.h" + +void UseriamUtTest001(void); +void UseriamUtTest002(void); +void UseriamUtTest003(void); +void UseriamUtTest004(void); +void UseriamUtTest005(void); +void UseriamUtTest006(void); +void UseriamUtTest007(void); +void UseriamUtTest008(void); +void UseriamUtTest009(void); +void UseriamUtTest010(void); +void UseriamUtTest011(void); +void UseriamUtTest012(void); +void UseriamUtTest013(void); +void UseriamUtTest014(void); +void UseriamUtTest015(void); +void UseriamUtTest016(void); +void UseriamUtTest017(void); +void UseriamUtTest018(void); + +#endif \ No newline at end of file diff --git a/test/unittest/src/coauth_test.cpp b/test/unittest/src/coauth_test.cpp new file mode 100644 index 0000000..07663e0 --- /dev/null +++ b/test/unittest/src/coauth_test.cpp @@ -0,0 +1,543 @@ +/* + * 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 "coauth_test.h" + + +using namespace testing::ext; +namespace OHOS { +namespace UserIAM { +namespace CoAuth { +class CoAuthTest : public testing::Test { +public: + static void SetUpTestCase(void); + + static void TearDownTestCase(void); + + void SetUp(); + + void TearDown(); +}; + +void CoAuthTest::SetUpTestCase(void) +{ +} + +void CoAuthTest::TearDownTestCase(void) +{ +} + +void CoAuthTest::SetUp() +{ +} + +void CoAuthTest::TearDown() +{ +} + +/** + * @tc.name: UseriamUtTest001 + * @tc.desc: Test AuthType(PIN). + * @tc.type: FUNC + */ +HWTEST_F(CoAuthTest, UseriamUtTest001, TestSize.Level0) +{ + COAUTH_HILOGD(MODULE_INNERKIT, "UseriamUtTest001 start"); + std::shared_ptr executorInfo = std::make_shared(); + executorInfo->SetAuthType(PIN); + executorInfo->SetAuthAbility(1); + executorInfo->SetExecutorSecLevel(ESL0); + executorInfo->SetExecutorType(TYPE_CO_AUTH); + + std::vector publicKey(32, '1'); + executorInfo->SetPublicKey(publicKey); + + std::vector deviceId; + deviceId.push_back('2'); + executorInfo->SetDeviceId(deviceId); + class MyExecutorCallback : public AuthResPool::ExecutorCallback { + public: + virtual ~MyExecutorCallback() {}; + void OnMessengerReady(const sptr &messenger)override { + COAUTH_HILOGE(MODULE_SERVICE, "MyExecutorCallback OnMessengerReady."); + return; + } + int32_t OnBeginExecute(uint64_t scheduleId, std::vector &publicKey, + std::shared_ptr commandAttrs)override { + COAUTH_HILOGE(MODULE_SERVICE, "MyExecutorCallback OnBeginExecute."); + return SUCCESS; + } + int32_t OnEndExecute(uint64_t scheduleId, std::shared_ptr consumerAttr)override { + COAUTH_HILOGE(MODULE_SERVICE, "MyExecutorCallback OnEndExecute."); + return SUCCESS; + } + int32_t OnSetProperty(std::shared_ptr properties)override { + COAUTH_HILOGE(MODULE_SERVICE, "MyExecutorCallback OnSetProperty."); + return SUCCESS; + } + int32_t OnGetProperty(std::shared_ptr conditions, + std::shared_ptr values)override { + COAUTH_HILOGE(MODULE_SERVICE, "MyExecutorCallback OnGetProperty."); + return SUCCESS; + } + }; + std::shared_ptr callback = std::make_shared(); + uint64_t ret = AuthResPool::AuthExecutorRegistry::GetInstance().Register(executorInfo, callback); + EXPECT_LE(10000, ret); +} + + +/** + * @tc.name: UseriamUtTest002 + * @tc.desc: Test empty callback. + * @tc.type: FUNC + */ +HWTEST_F(CoAuthTest, UseriamUtTest002, TestSize.Level0) +{ + COAUTH_HILOGD(MODULE_INNERKIT, "UseriamUtTest002 start"); + std::shared_ptr executorInfo = std::make_shared(); + executorInfo->SetAuthType(PIN); + executorInfo->SetAuthAbility(1); + executorInfo->SetExecutorSecLevel(ESL0); + executorInfo->SetExecutorType(TYPE_CO_AUTH); + + std::vector publicKey(32, '1'); + executorInfo->SetPublicKey(publicKey); + + std::vector deviceId; + deviceId.push_back('2'); + executorInfo->SetDeviceId(deviceId); + std::shared_ptr callback = nullptr; + uint64_t ret = AuthResPool::AuthExecutorRegistry::GetInstance().Register(executorInfo, callback); + EXPECT_LE(FAIL, ret); +} +/** + * @tc.name: UseriamUtTest003 + * @tc.desc: Test AuthType(FACE). + * @tc.type: FUNC + */ +HWTEST_F(CoAuthTest, UseriamUtTest003, TestSize.Level0) +{ + COAUTH_HILOGD(MODULE_INNERKIT, "UseriamUtTest003 start"); + std::shared_ptr executorInfo = std::make_shared(); + executorInfo->SetAuthType(FACE); + executorInfo->SetAuthAbility(1); + executorInfo->SetExecutorSecLevel(ESL0); + executorInfo->SetExecutorType(TYPE_CO_AUTH); + + std::vector publicKey(32, '1'); + executorInfo->SetPublicKey(publicKey); + + std::vector deviceId; + deviceId.push_back('2'); + executorInfo->SetDeviceId(deviceId); + class MyExecutorCallback : public AuthResPool::ExecutorCallback { + public: + virtual ~MyExecutorCallback() {}; + void OnMessengerReady(const sptr &messenger)override { + COAUTH_HILOGE(MODULE_SERVICE, "MyExecutorCallback OnMessengerReady."); + return; + } + int32_t OnBeginExecute(uint64_t scheduleId, std::vector &publicKey, + std::shared_ptr commandAttrs)override { + COAUTH_HILOGE(MODULE_SERVICE, "MyExecutorCallback OnBeginExecute."); + return SUCCESS; + } + int32_t OnEndExecute(uint64_t scheduleId, std::shared_ptr consumerAttr)override { + COAUTH_HILOGE(MODULE_SERVICE, "MyExecutorCallback OnEndExecute."); + return SUCCESS; + } + int32_t OnSetProperty(std::shared_ptr properties)override { + COAUTH_HILOGE(MODULE_SERVICE, "MyExecutorCallback OnSetProperty."); + return SUCCESS; + } + int32_t OnGetProperty(std::shared_ptr conditions, + std::shared_ptr values)override { + COAUTH_HILOGE(MODULE_SERVICE, "MyExecutorCallback OnGetProperty."); + return SUCCESS; + } + }; + std::shared_ptr callback = std::make_shared(); + uint64_t ret = AuthResPool::AuthExecutorRegistry::GetInstance().Register(executorInfo, callback); + EXPECT_LE(10000, ret); +} + +/** + * @tc.name: UseriamUtTest004 + * @tc.desc: Test publicKey error length. + * @tc.type: FUNC + */ +HWTEST_F(CoAuthTest, UseriamUtTest004, TestSize.Level0) +{ + COAUTH_HILOGD(MODULE_INNERKIT, "UseriamUtTest004 start"); + std::shared_ptr executorInfo = std::make_shared(); + executorInfo->SetAuthType(FACE); + executorInfo->SetAuthAbility(1); + executorInfo->SetExecutorSecLevel(ESL0); + executorInfo->SetExecutorType(TYPE_CO_AUTH); + + std::vector publicKey(10, '1'); + executorInfo->SetPublicKey(publicKey); + + std::vector deviceId; + deviceId.push_back('2'); + executorInfo->SetDeviceId(deviceId); + class MyExecutorCallback : public AuthResPool::ExecutorCallback { + public: + virtual ~MyExecutorCallback() {}; + void OnMessengerReady(const sptr &messenger)override { + COAUTH_HILOGE(MODULE_SERVICE, "MyExecutorCallback OnMessengerReady."); + return; + } + int32_t OnBeginExecute(uint64_t scheduleId, std::vector &publicKey, + std::shared_ptr commandAttrs)override { + COAUTH_HILOGE(MODULE_SERVICE, "MyExecutorCallback OnBeginExecute."); + return SUCCESS; + } + int32_t OnEndExecute(uint64_t scheduleId, std::shared_ptr consumerAttr)override { + COAUTH_HILOGE(MODULE_SERVICE, "MyExecutorCallback OnEndExecute."); + return SUCCESS; + } + int32_t OnSetProperty(std::shared_ptr properties)override { + COAUTH_HILOGE(MODULE_SERVICE, "MyExecutorCallback OnSetProperty."); + return SUCCESS; + } + int32_t OnGetProperty(std::shared_ptr conditions, + std::shared_ptr values)override { + COAUTH_HILOGE(MODULE_SERVICE, "MyExecutorCallback OnGetProperty."); + return SUCCESS; + } + }; + std::shared_ptr callback = std::make_shared(); + uint64_t ret = AuthResPool::AuthExecutorRegistry::GetInstance().Register(executorInfo, callback); + EXPECT_LE(0, ret); +} + +/** + * @tc.name: UseriamUtTest005 + * @tc.desc: Test empty executorInfo and empty callback. + * @tc.type: FUNC + */ +HWTEST_F(CoAuthTest, UseriamUtTest005, TestSize.Level0) +{ + COAUTH_HILOGD(MODULE_INNERKIT, "UseriamUtTest005 start"); + std::shared_ptr executorInfo = nullptr; + std::shared_ptr callback = nullptr; + uint64_t ret = AuthResPool::AuthExecutorRegistry::GetInstance().Register(executorInfo, callback); + EXPECT_EQ(FAIL, ret); +} + +/** + * @tc.name: UseriamUtTest006 + * @tc.desc: Test AuthType(PIN). + * @tc.type: FUNC + */ +HWTEST_F(CoAuthTest, UseriamUtTest006, TestSize.Level0) +{ + COAUTH_HILOGD(MODULE_INNERKIT, "UseriamUtTest006 start"); + AuthResPool::AuthExecutor executorInfo; + executorInfo.SetAuthType(PIN); + executorInfo.SetAuthAbility(1); + executorInfo.SetExecutorSecLevel(ESL0); + executorInfo.SetExecutorType(TYPE_CO_AUTH); + + std::vector publicKey(32, '1'); + executorInfo.SetPublicKey(publicKey); + + std::vector deviceId; + deviceId.push_back('2'); + executorInfo.SetDeviceId(deviceId); + + class MyQueryCallback : public AuthResPool::QueryCallback { + public: + virtual ~MyQueryCallback() {}; + virtual void OnResult(uint32_t resultCode) override { + COAUTH_HILOGE(MODULE_SERVICE, "MyQueryCallback OnResult."); + return; + } + }; + std::shared_ptr callback = std::make_shared(); + AuthResPool::AuthExecutorRegistry::GetInstance().QueryStatus(executorInfo, callback); + SUCCEED(); +} + +/** + * @tc.name: UseriamUtTest007 + * @tc.desc: Test empty executorInfo and empty callback. + * @tc.type: FUNC + */ +HWTEST_F(CoAuthTest, UseriamUtTest007, TestSize.Level0) +{ + COAUTH_HILOGD(MODULE_INNERKIT, "UseriamUtTest007 start"); + AuthResPool::AuthExecutor executorInfo; + std::shared_ptr callback = nullptr; + AuthResPool::AuthExecutorRegistry::GetInstance().QueryStatus(executorInfo, callback); + SUCCEED(); +} + +/** + * @tc.name: UseriamUtTest008 + * @tc.desc: Test AuthType(FACE). + * @tc.type: FUNC + */ +HWTEST_F(CoAuthTest, UseriamUtTest008, TestSize.Level0) +{ + COAUTH_HILOGD(MODULE_INNERKIT, "UseriamUtTest008 start"); + AuthResPool::AuthExecutor executorInfo; + executorInfo.SetAuthType(FACE); + executorInfo.SetAuthAbility(1); + executorInfo.SetExecutorSecLevel(ESL0); + executorInfo.SetExecutorType(TYPE_CO_AUTH); + + std::vector publicKey(32, '1'); + executorInfo.SetPublicKey(publicKey); + + std::vector deviceId; + deviceId.push_back('2'); + executorInfo.SetDeviceId(deviceId); + + class MyQueryCallback : public AuthResPool::QueryCallback { + public: + virtual ~MyQueryCallback() {}; + virtual void OnResult(uint32_t resultCode) override { + COAUTH_HILOGE(MODULE_SERVICE, "MyQueryCallback OnResult."); + return; + } + }; + std::shared_ptr callback = std::make_shared(); + AuthResPool::AuthExecutorRegistry::GetInstance().QueryStatus(executorInfo, callback); + SUCCEED(); +} +/** + * @tc.name: UseriamUtTest009 + * @tc.desc: Test empty callback. + * @tc.type: FUNC + */ +HWTEST_F(CoAuthTest, UseriamUtTest009, TestSize.Level0) +{ + COAUTH_HILOGD(MODULE_INNERKIT, "UseriamUtTest009 start"); + AuthResPool::AuthExecutor executorInfo; + executorInfo.SetAuthType(FACE); + executorInfo.SetAuthAbility(1); + executorInfo.SetExecutorSecLevel(ESL0); + executorInfo.SetExecutorType(TYPE_CO_AUTH); + + std::vector publicKey(32, '1'); + executorInfo.SetPublicKey(publicKey); + + std::vector deviceId; + deviceId.push_back('2'); + executorInfo.SetDeviceId(deviceId); + + std::shared_ptr callback = nullptr; + AuthResPool::AuthExecutorRegistry::GetInstance().QueryStatus(executorInfo, callback); + SUCCEED(); +} +/** + * @tc.name: UseriamUtTest010 + * @tc.desc: Test empty authInfo and empty callback. + * @tc.type: FUNC + */ +HWTEST_F(CoAuthTest, UseriamUtTest010, TestSize.Level0) +{ + COAUTH_HILOGD(MODULE_INNERKIT, "UseriamUtTest010 start"); + AuthInfo authInfo; + std::shared_ptr callback = nullptr; + CoAuth::GetInstance().coAuth(1, authInfo, callback); + SUCCEED(); +} + +/** + * @tc.name: UseriamUtTest011 + * @tc.desc: Test normal value. + * @tc.type: FUNC + */ +HWTEST_F(CoAuthTest, UseriamUtTest011, TestSize.Level0) +{ + COAUTH_HILOGD(MODULE_INNERKIT, "UseriamUtTest011 start"); + AuthInfo authInfo; + std::string value = "packagedemo"; + authInfo.SetPkgName(value); + authInfo.SetCallerUid(10000); + class MyCoAuthCallback : public CoAuthCallback { + public: + virtual ~MyCoAuthCallback() {}; + virtual void OnFinish(uint32_t resultCode, std::vector &scheduleToken) override { + COAUTH_HILOGE(MODULE_SERVICE, "MyCoAuthCallback OnFinish."); + return; + } + virtual void OnAcquireInfo(uint32_t acquire) override { + COAUTH_HILOGE(MODULE_SERVICE, "MyCoAuthCallback OnAcquireInfo."); + return; + } + }; + std::shared_ptr callback = std::make_shared(); + CoAuth::GetInstance().coAuth(1, authInfo, callback); + SUCCEED(); +} +/** + * @tc.name: UseriamUtTest012 + * @tc.desc: Test empty callback. + * @tc.type: FUNC + */ +HWTEST_F(CoAuthTest, UseriamUtTest012, TestSize.Level0) +{ + COAUTH_HILOGD(MODULE_INNERKIT, "UseriamUtTest012 start"); + AuthInfo authInfo; + std::string value = "packagedemo"; + authInfo.SetPkgName(value); + authInfo.SetCallerUid(10000); + std::shared_ptr callback = nullptr; + CoAuth::GetInstance().coAuth(1, authInfo, callback); + SUCCEED(); +} + +HWTEST_F(CoAuthTest, UseriamUtTest013, TestSize.Level0) +{ + COAUTH_HILOGE(MODULE_SERVICE, "UseriamUtTest013 enter."); + uint64_t scheduleId = 0; + EXPECT_EQ(0, CoAuth::GetInstance().Cancel(scheduleId)); + + scheduleId = 1; + EXPECT_EQ(0, CoAuth::GetInstance().Cancel(scheduleId)); +} + +HWTEST_F(CoAuthTest, UseriamUtTest014, TestSize.Level0) +{ + COAUTH_HILOGE(MODULE_SERVICE, "UseriamUtTest014 enter."); + AuthResPool::AuthAttributes conditions; + conditions.SetUint32Value(AUTH_TYPE, FACE); + conditions.SetBoolValue(AUTH_CONTROLLER, 0); + conditions.SetUint32Value(AUTH_SCHEDULE_MODE, 1); + conditions.SetUint64Value(AUTH_SESSION_ID, 1); + + std::vector val1; + val1.push_back(1); + conditions.SetUint64ArrayValue(AUTH_TEMPLATE_ID_LIST, val1); + + std::vector val2; + val2.push_back('5'); + conditions.SetUint8ArrayValue(AUTH_CALLER_NAME, val2); + + class MySetPropCallback : public SetPropCallback { + public: + + virtual ~MySetPropCallback() {}; + void OnResult(uint32_t result, std::vector &extraInfo)override + { + COAUTH_HILOGE(MODULE_SERVICE, "MySetPropCallback OnResult."); + return; + } + + }; + std::shared_ptr callback = std::make_shared(); + CoAuth::GetInstance().SetExecutorProp(conditions, callback); + sleep(5); + SUCCEED(); +} + +HWTEST_F(CoAuthTest, UseriamUtTest015, TestSize.Level0) +{ + COAUTH_HILOGE(MODULE_SERVICE, "UseriamUtTest015 enter."); + AuthResPool::AuthAttributes conditions; + conditions.SetUint32Value(AUTH_TYPE, FACE); + conditions.SetBoolValue(AUTH_CONTROLLER, 0); + conditions.SetUint32Value(AUTH_SCHEDULE_MODE, 1); + conditions.SetUint64Value(AUTH_SESSION_ID, 1); + + std::vector val1; + val1.push_back(1); + conditions.SetUint64ArrayValue(AUTH_TEMPLATE_ID_LIST, val1); + + std::vector val2; + val2.push_back('5'); + conditions.SetUint8ArrayValue(AUTH_CALLER_NAME, val2); + std::shared_ptr callback = nullptr; + CoAuth::GetInstance().SetExecutorProp(conditions, callback); + sleep(5); + SUCCEED(); +} + +HWTEST_F(CoAuthTest, UseriamUtTest016, TestSize.Level0) +{ + COAUTH_HILOGE(MODULE_SERVICE, "UseriamUtTest016 enter."); + AuthResPool::AuthAttributes conditions; + conditions.SetUint32Value(AUTH_TYPE, FACE); + conditions.SetBoolValue(AUTH_CONTROLLER, 0); + conditions.SetUint32Value(AUTH_SCHEDULE_MODE, 1); + conditions.SetUint64Value(AUTH_SESSION_ID, 1); + + std::vector val1; + val1.push_back(1); + conditions.SetUint64ArrayValue(AUTH_TEMPLATE_ID_LIST, val1); + + std::vector val2; + val2.push_back('5'); + conditions.SetUint8ArrayValue(AUTH_CALLER_NAME, val2); + + std::shared_ptr values = std::make_shared(); + int32_t ret = CoAuth::GetInstance().GetExecutorProp(conditions, values); + sleep(5); + EXPECT_EQ(0, ret); +} + +HWTEST_F(CoAuthTest, UseriamUtTest017, TestSize.Level0) +{ + COAUTH_HILOGE(MODULE_SERVICE, "UseriamUtTest017 enter."); + AuthResPool::AuthAttributes conditions; + conditions.SetBoolValue(AUTH_CONTROLLER, 0); + conditions.SetUint32Value(AUTH_SCHEDULE_MODE, 1); + conditions.SetUint64Value(AUTH_SESSION_ID, 1); + + std::vector val1; + val1.push_back(1); + conditions.SetUint64ArrayValue(AUTH_TEMPLATE_ID_LIST, val1); + + std::vector val2; + val2.push_back('5'); + conditions.SetUint8ArrayValue(AUTH_CALLER_NAME, val2); + + std::shared_ptr values = nullptr; + int32_t ret = CoAuth::GetInstance().GetExecutorProp(conditions, values); + sleep(5); + EXPECT_EQ(1, ret); +} + +HWTEST_F(CoAuthTest, UseriamUtTest018, TestSize.Level0) +{ + COAUTH_HILOGE(MODULE_SERVICE, "UseriamUtTest018 enter."); + AuthResPool::AuthAttributes conditions; + conditions.SetUint32Value(AUTH_TYPE, PIN); + conditions.SetBoolValue(AUTH_CONTROLLER, 0); + conditions.SetUint32Value(AUTH_SCHEDULE_MODE, 1); + conditions.SetUint64Value(AUTH_SESSION_ID, 1); + + std::vector val1; + val1.push_back(1); + conditions.SetUint64ArrayValue(AUTH_TEMPLATE_ID_LIST, val1); + + std::vector val2; + val2.push_back('5'); + conditions.SetUint8ArrayValue(AUTH_CALLER_NAME, val2); + + std::shared_ptr values = std::make_shared(); + int32_t ret = CoAuth::GetInstance().GetExecutorProp(conditions, values); + sleep(5); + EXPECT_EQ(0, ret); +} +} +} +} \ No newline at end of file diff --git a/utils/BUILD.gn b/utils/BUILD.gn new file mode 100755 index 0000000..fc7fd4c --- /dev/null +++ b/utils/BUILD.gn @@ -0,0 +1,21 @@ +# 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("//base/useriam/coauth/coauth.gni") + +config("utils_config") { + include_dirs = [ + "native/include", + "//utils/native/include", + ] +} diff --git a/utils/native/include/coauth_errors.h b/utils/native/include/coauth_errors.h new file mode 100755 index 0000000..5d232fd --- /dev/null +++ b/utils/native/include/coauth_errors.h @@ -0,0 +1,35 @@ +/* + * 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 COAUTH_ERRORS_H +#define COAUTH_ERRORS_H + +#include + +namespace OHOS { +namespace UserIAM { +enum { + E_WRITE_PARCEL_ERROR = 0, + E_READ_PARCEL_ERROR, + E_GET_SYSTEM_ABILITY_MANAGER_FAILED, + E_GET_POWER_SERVICE_FAILED, + E_ADD_DEATH_RECIPIENT_FAILED, + E_INNER_ERR, + E_SPR_NULL_ERROR +}; +} // namespace UserIAM +} // namespace OHOS + +#endif // COAUTH_ERRORS_H diff --git a/utils/native/include/coauth_hilog_wrapper.h b/utils/native/include/coauth_hilog_wrapper.h new file mode 100755 index 0000000..09a2305 --- /dev/null +++ b/utils/native/include/coauth_hilog_wrapper.h @@ -0,0 +1,94 @@ +/* + * 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 COAUTH_HILOG_WRAPPER_H +#define COAUTH_HILOG_WRAPPER_H + +#define CONFIG_HILOG +#ifdef CONFIG_HILOG +#include "hilog/log.h" +namespace OHOS { +namespace UserIAM { +#define FILENAME (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__) +#define FORMATED(fmt, ...) "[%{public}s] %{public}s# " fmt, FILENAME, __FUNCTION__, ##__VA_ARGS__ + +#ifdef COAUTH_HILOGF +#undef COAUTH_HILOGF +#endif + +#ifdef COAUTH_HILOGE +#undef COAUTH_HILOGE +#endif + +#ifdef COAUTH_HILOGW +#undef COAUTH_HILOGW +#endif + +#ifdef COAUTH_HILOGI +#undef COAUTH_HILOGI +#endif + +#ifdef COAUTH_HILOGD +#undef COAUTH_HILOGD +#endif + +// param of log interface, such as COAUTH_HILOGF. +enum CoauthSubModule { + MODULE_INNERKIT = 0, + MODULE_SERVICE, + MODULE_COMMON, + MODULE_JS_NAPI, + COAUTH_MODULE_BUTT, +}; + +// 0xD002900: subsystem:distributeddatamgr module:distributedgallery, 8 bits reserved. +static constexpr unsigned int BASE_COAUTH_DOMAIN_ID = 0xD002910; + +enum CoauthDomainId { + COAUTH_INNERKIT_DOMAIN = BASE_COAUTH_DOMAIN_ID + MODULE_INNERKIT, + COAUTH_SERVICE_DOMAIN, + COMMON_DOMAIN, + COAUTH_JS_NAPI, + COAUTH_BUTT, +}; + +static constexpr OHOS::HiviewDFX::HiLogLabel COAUTH_LABEL[COAUTH_MODULE_BUTT] = { + {LOG_CORE, COAUTH_INNERKIT_DOMAIN, "CoAuth"}, + {LOG_CORE, COAUTH_SERVICE_DOMAIN, "CoAuthService"}, + {LOG_CORE, COMMON_DOMAIN, "CoAuthCommon"}, + {LOG_CORE, COAUTH_JS_NAPI, "CoAuthJSNAPI"}, +}; + +// In order to improve performance, do not check the module range. +// Besides, make sure module is less than COAUTH_MODULE_BUTT. +#define COAUTH_HILOGF(module, ...) (void)OHOS::HiviewDFX::HiLog::Fatal(COAUTH_LABEL[module], FORMATED(__VA_ARGS__)) +#define COAUTH_HILOGE(module, ...) (void)OHOS::HiviewDFX::HiLog::Error(COAUTH_LABEL[module], FORMATED(__VA_ARGS__)) +#define COAUTH_HILOGW(module, ...) (void)OHOS::HiviewDFX::HiLog::Warn(COAUTH_LABEL[module], FORMATED(__VA_ARGS__)) +#define COAUTH_HILOGI(module, ...) (void)OHOS::HiviewDFX::HiLog::Info(COAUTH_LABEL[module], FORMATED(__VA_ARGS__)) +#define COAUTH_HILOGD(module, ...) (void)OHOS::HiviewDFX::HiLog::Debug(COAUTH_LABEL[module], FORMATED(__VA_ARGS__)) +} // namespace UserIAM +} // namespace OHOS + +#else + +#define COAUTH_HILOGF(...) +#define COAUTH_HILOGE(...) +#define COAUTH_HILOGW(...) +#define COAUTH_HILOGI(...) +#define COAUTH_HILOGD(...) + +#endif // CONFIG_HILOG + +#endif // COAUTH_HILOG_WRAPPER_H -- Gitee