diff --git a/services/distributeddataservice/adapter/account/src/account_delegate_default_impl.cpp b/services/distributeddataservice/adapter/account/src/account_delegate_default_impl.cpp index b855d79021431dda9f0f460eb244301ffe5d4c9b..0b235360423fe94d2908a1a6404bc13df44391ee 100644 --- a/services/distributeddataservice/adapter/account/src/account_delegate_default_impl.cpp +++ b/services/distributeddataservice/adapter/account/src/account_delegate_default_impl.cpp @@ -20,7 +20,7 @@ namespace OHOS { namespace DistributedKv { namespace { - const std::string DEFAULT_OHOS_ACCOUNT_UID = ""; // default UID + constexpr const char *DEFAULT_OHOS_ACCOUNT_UID = ""; // default UID } AccountDelegate::BaseInstance AccountDelegate::getInstance_ = AccountDelegateDefaultImpl::GetBaseInstance; @@ -36,9 +36,9 @@ AccountDelegate *AccountDelegateDefaultImpl::GetBaseInstance() return AccountDelegateDefaultImpl::GetInstance(); } -std::string AccountDelegateDefaultImpl::GetCurrentAccountId(const std::string &bundleName) const +std::string AccountDelegateDefaultImpl::GetCurrentAccountId() const { - ZLOGD("no account part, return default. bundlename:%s", bundleName.c_str()); + ZLOGD("no account part, return default."); return DEFAULT_OHOS_ACCOUNT_UID; } @@ -51,7 +51,7 @@ std::string AccountDelegateDefaultImpl::GetDeviceAccountIdByUID(int32_t uid) con bool AccountDelegateDefaultImpl::QueryUsers(std::vector &users) { ZLOGD("no account part."); - users.emplace_back(0); // default user + users = {0}; // default user return true; } diff --git a/services/distributeddataservice/adapter/account/src/account_delegate_default_impl.h b/services/distributeddataservice/adapter/account/src/account_delegate_default_impl.h index 50a708566fef6334e5e8c29b9bb33171cf29bedd..1d4e5454d46499323a766dc9a56bdf74a0a9e31e 100644 --- a/services/distributeddataservice/adapter/account/src/account_delegate_default_impl.h +++ b/services/distributeddataservice/adapter/account/src/account_delegate_default_impl.h @@ -23,7 +23,7 @@ class AccountDelegateDefaultImpl final : public AccountDelegateImpl { public: static AccountDelegateDefaultImpl *GetInstance(); static AccountDelegate *GetBaseInstance(); - std::string GetCurrentAccountId(const std::string &bundleName = "") const override; + std::string GetCurrentAccountId() const override; std::string GetDeviceAccountIdByUID(int32_t uid) const override; bool QueryUsers(std::vector &users) override; void SubscribeAccountEvent() override; diff --git a/services/distributeddataservice/adapter/account/src/account_delegate_impl.cpp b/services/distributeddataservice/adapter/account/src/account_delegate_impl.cpp index afe70d73dab6b43b709c3182cf6a4329eb532794..359a8bab8c17490456b1e6bed7a97efd93a9fbf4 100644 --- a/services/distributeddataservice/adapter/account/src/account_delegate_impl.cpp +++ b/services/distributeddataservice/adapter/account/src/account_delegate_impl.cpp @@ -62,7 +62,15 @@ void EventSubscriber::SetEventCallback(EventCallback callback) void AccountDelegateImpl::NotifyAccountChanged(const AccountEventInfo &accountEventInfo) { observerMap_.ForEach([&accountEventInfo] (const auto& key, const auto& val) { - val->OnAccountChanged(accountEventInfo); + if (val->GetLevel() == AccountDelegate::Observer::LevelType::HIGH) { + val->OnAccountChanged(accountEventInfo); + } + return false; + }); + observerMap_.ForEach([&accountEventInfo] (const auto& key, const auto& val) { + if (val->GetLevel() == AccountDelegate::Observer::LevelType::LOW) { + val->OnAccountChanged(accountEventInfo); + } return false; }); } diff --git a/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.cpp b/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.cpp index 6ce6c48f80ecdf7d098d54277a504426bc653eb0..948d42527e0787a0c2f8c11d8e93aa30d4e2ec59 100644 --- a/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.cpp +++ b/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.cpp @@ -16,6 +16,7 @@ #include "account_delegate_normal_impl.h" #include +#include #include #include #include @@ -43,7 +44,7 @@ AccountDelegate *AccountDelegateNormalImpl::GetBaseInstance() return AccountDelegateNormalImpl::GetInstance(); } -std::string AccountDelegateNormalImpl::GetCurrentAccountId(const std::string &bundleName) const +std::string AccountDelegateNormalImpl::GetCurrentAccountId() const { ZLOGD("start"); auto ohosAccountInfo = AccountSA::OhosAccountKits::GetInstance().QueryOhosAccountInfo(); @@ -72,7 +73,7 @@ std::string AccountDelegateNormalImpl::GetDeviceAccountIdByUID(int32_t uid) cons bool AccountDelegateNormalImpl::QueryUsers(std::vector &users) { - users.emplace_back(0); // default user + users = {0}; // default user return AccountSA::OsAccountManager::QueryActiveOsAccountIds(users) == 0; } @@ -141,8 +142,8 @@ std::string AccountDelegateNormalImpl::Sha256AccountId(const std::string &plainT plainVal = atoll(plainText.substr(plainText.size() - int64MaxLen + 1, int64MaxLen - 1).c_str()); } - int64_t pValBigEndian = htobe64(plainVal); - return Crypto::Sha256(std::to_string(pValBigEndian), true); + plainVal = htobe64(plainVal); + return Crypto::Sha256(std::to_string(plainVal), true); } } // namespace DistributedKv } // namespace OHOS \ No newline at end of file diff --git a/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.h b/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.h index bae7095d2b36c5cba3e711edf7617d0cb0eea6da..f658dc48f18a6f57d61a727ae0ba4dfc19de9235 100644 --- a/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.h +++ b/services/distributeddataservice/adapter/account/src/account_delegate_normal_impl.h @@ -27,7 +27,7 @@ class AccountDelegateNormalImpl final : public AccountDelegateImpl { public: static AccountDelegateNormalImpl *GetInstance(); static AccountDelegate *GetBaseInstance(); - std::string GetCurrentAccountId(const std::string &bundleName = "") const override; + std::string GetCurrentAccountId() const override; std::string GetDeviceAccountIdByUID(int32_t uid) const override; bool QueryUsers(std::vector &users) override; void SubscribeAccountEvent() override; diff --git a/services/distributeddataservice/adapter/account/test/account_delegate_test.cpp b/services/distributeddataservice/adapter/account/test/account_delegate_test.cpp index 14cfcf89f6c86c320dd5bc26335d0b0f74607eb1..a38c067b2fa53b1b50e8326daea290439f56ee93 100644 --- a/services/distributeddataservice/adapter/account/test/account_delegate_test.cpp +++ b/services/distributeddataservice/adapter/account/test/account_delegate_test.cpp @@ -34,6 +34,11 @@ public: { return "accountTestObserver"; } + + LevelType GetLevel() + { + return LevelType::LOW; + } }; /** * @tc.name: Test001 diff --git a/services/distributeddataservice/adapter/auth/BUILD.gn b/services/distributeddataservice/adapter/auth/BUILD.gn deleted file mode 100644 index 0b250db92e20ded86625df00647d8d49b333e19c..0000000000000000000000000000000000000000 --- a/services/distributeddataservice/adapter/auth/BUILD.gn +++ /dev/null @@ -1,43 +0,0 @@ -# Copyright (c) 2022 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") - -ohos_static_library("distributeddata_auth_static") { - sources = [ "src/auth_delegate.cpp" ] - - include_dirs = [ - "../include", - "../include/autils", - "../include/communicator", - "../include/log", - "//third_party/json/single_include", - "//utils/native/base/include", - "//foundation/distributeddatamgr/distributeddatamgr/frameworks/common", - "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/distributeddata/include", - "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/framework/include", - ] - - cflags_cc = [ "-fvisibility=hidden" ] - - deps = [ - "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/framework:distributeddatasvcfwk", - "//utils/native/base:utils", - ] - - external_deps = [ - "deviceauth_standard:deviceauth_sdk", - "hiviewdfx_hilog_native:libhilog", - ] - part_name = "distributeddatamgr" -} diff --git a/services/distributeddataservice/adapter/include/account/account_delegate.h b/services/distributeddataservice/adapter/include/account/account_delegate.h index e917a15aed005d42e8849250db21d773e0de1544..e309124149241d20290701ffe876234543b68ca9 100644 --- a/services/distributeddataservice/adapter/include/account/account_delegate.h +++ b/services/distributeddataservice/adapter/include/account/account_delegate.h @@ -41,16 +41,21 @@ class AccountDelegate { public: class Observer { public: + enum class LevelType { + HIGH, + LOW, + }; API_EXPORT virtual ~Observer() = default; API_EXPORT virtual void OnAccountChanged(const AccountEventInfo &eventInfo) = 0; // must specify unique name for observer API_EXPORT virtual std::string Name() = 0; + API_EXPORT virtual LevelType GetLevel() = 0; }; API_EXPORT virtual ~AccountDelegate() = default; API_EXPORT virtual Status Subscribe(std::shared_ptr observer) = 0; API_EXPORT virtual Status Unsubscribe(std::shared_ptr observer) = 0; - API_EXPORT virtual std::string GetCurrentAccountId(const std::string &bundleName = "") const = 0; + API_EXPORT virtual std::string GetCurrentAccountId() const = 0; API_EXPORT virtual std::string GetDeviceAccountIdByUID(int32_t uid) const = 0; API_EXPORT virtual void SubscribeAccountEvent() = 0; API_EXPORT virtual bool QueryUsers(std::vector &users) = 0; diff --git a/services/distributeddataservice/app/BUILD.gn b/services/distributeddataservice/app/BUILD.gn index 155e27ba0906993cfc39d22237757d7584fddf37..f431b1927b56da45f53daec78b93a027d72b2fc4 100644 --- a/services/distributeddataservice/app/BUILD.gn +++ b/services/distributeddataservice/app/BUILD.gn @@ -109,6 +109,7 @@ ohos_shared_library("distributeddataservice") { ] kv_sources = [ + "../service/kvdb/auth_delegate.cpp", "../service/kvdb/executor_factory.cpp", "../service/kvdb/user_delegate.cpp", ] @@ -121,7 +122,6 @@ ohos_shared_library("distributeddataservice") { "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/distributeddata:distributeddata_inner", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter:distributeddata_adapter", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/account:distributeddata_account_static", - "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/auth:distributeddata_auth_static", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/broadcaster:distributeddata_broadcaster_static", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/permission:distributeddata_permission_static", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/utils:distributeddata_utils_static", @@ -139,6 +139,7 @@ ohos_shared_library("distributeddataservice") { "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "dataclassification:data_transit_mgr", + "deviceauth_standard:deviceauth_sdk", "hiviewdfx_hilog_native:libhilog", "huks:libhukssdk", "ipc:ipc_core", diff --git a/services/distributeddataservice/app/src/backup_handler.cpp b/services/distributeddataservice/app/src/backup_handler.cpp index 8853650282533cc5390e518b31425b04fef7ce45..b7480feb6e876a54a0aba984910faf2a70fc6c0d 100644 --- a/services/distributeddataservice/app/src/backup_handler.cpp +++ b/services/distributeddataservice/app/src/backup_handler.cpp @@ -92,7 +92,7 @@ void BackupHandler::SingleKvStoreBackup(const MetaData &metaData) DistributedDB::KvStoreNbDelegate::Option dbOption; SetDBOptions(dbOption, backupPara, metaData); auto *delegateMgr = new(std::nothrow) DistributedDB::KvStoreDelegateManager(metaData.kvStoreMetaData.appId, - AccountDelegate::GetInstance()->GetCurrentAccountId(metaData.kvStoreMetaData.bundleName)); + AccountDelegate::GetInstance()->GetCurrentAccountId()); if (delegateMgr == nullptr) { return; } @@ -160,7 +160,7 @@ void BackupHandler::MultiKvStoreBackup(const MetaData &metaData) option.createDirByStoreIdOnly = true; auto *delegateMgr = new DistributedDB::KvStoreDelegateManager(metaData.kvStoreMetaData.appId, - AccountDelegate::GetInstance()->GetCurrentAccountId(metaData.kvStoreMetaData.bundleName)); + AccountDelegate::GetInstance()->GetCurrentAccountId()); std::string appDataStoragePath = KvStoreAppManager::GetDataStoragePath(metaData.kvStoreMetaData.deviceAccountId, metaData.kvStoreMetaData.bundleName, backupPara.pathType); DistributedDB::KvStoreConfig kvStoreConfig; diff --git a/services/distributeddataservice/app/src/kvstore_account_observer.h b/services/distributeddataservice/app/src/kvstore_account_observer.h index eb4de43bd7bc6fefa5dff8fd4ce8cfcd33c791a0..ddc4103df6e47cf3cf7f697947422478a7cd3278 100644 --- a/services/distributeddataservice/app/src/kvstore_account_observer.h +++ b/services/distributeddataservice/app/src/kvstore_account_observer.h @@ -16,8 +16,8 @@ #ifndef KVSTORE_ACCOUNT_OBSERVER_H #define KVSTORE_ACCOUNT_OBSERVER_H -#include "account_delegate.h" #include +#include "account_delegate.h" namespace OHOS { namespace DistributedKv { @@ -43,6 +43,11 @@ public: return "DistributedDataService"; } + LevelType GetLevel() override + { + return LevelType::LOW; + } + private: KvStoreDataService &kvStoreDataService_; }; diff --git a/services/distributeddataservice/app/src/kvstore_app_manager.cpp b/services/distributeddataservice/app/src/kvstore_app_manager.cpp index a4e64e987a590819f3167a9fdab1b94825388293..e6f86af24b07646e2bff157b392a33ec8d838b88 100644 --- a/services/distributeddataservice/app/src/kvstore_app_manager.cpp +++ b/services/distributeddataservice/app/src/kvstore_app_manager.cpp @@ -458,7 +458,7 @@ DistributedDB::KvStoreDelegateManager *KvStoreAppManager::GetDelegateManager(Pat return nullptr; } - userId_ = AccountDelegate::GetInstance()->GetCurrentAccountId(bundleName_); + userId_ = AccountDelegate::GetInstance()->GetCurrentAccountId(); ZLOGD("accountId: %{public}s bundleName: %{public}s", deviceAccountId_.c_str(), bundleName_.c_str()); delegateManagers_[type] = new (std::nothrow) DistributedDB::KvStoreDelegateManager(trueAppId_, deviceAccountId_); if (delegateManagers_[type] == nullptr) { diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index b3fe3dbdece5839d117dd0911947ea4e2381b7a3..eb5b274a4260b0412be0136996d1da6cfd479cda 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -25,7 +25,7 @@ #include #include -#include "auth/auth_delegate.h" +#include "auth_delegate.h" #include "auto_launch_export.h" #include "bootstrap.h" #include "checker/checker_manager.h" @@ -375,7 +375,7 @@ Status KvStoreDataService::UpdateMetaData(const Options &options, const KvStoreP metaData.schema = options.schema; metaData.storeId = kvParas.storeId; metaData.tokenId = IPCSkeleton::GetCallingTokenID(); - metaData.userId = AccountDelegate::GetInstance()->GetCurrentAccountId(kvParas.bundleName); + metaData.userId = AccountDelegate::GetInstance()->GetCurrentAccountId(); metaData.uid = IPCSkeleton::GetCallingUid(); metaData.version = STORE_VERSION; metaData.securityLevel = options.securityLevel; diff --git a/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp b/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp index dcdb9920b701b71b6e5b68edea53007e5c912d6b..2947a236f2cfb8516b9656bc5bf7a805620d1021 100644 --- a/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp +++ b/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp @@ -16,7 +16,7 @@ #define LOG_TAG "RouteHeadHandler" -#include "auth/auth_delegate.h" +#include "auth_delegate.h" #include "device_kvstore_impl.h" #include "kvstore_meta_manager.h" #include "log_print.h" @@ -245,8 +245,8 @@ bool RouteHeadHandlerImpl::UnPackDataBody(const uint8_t *data, uint32_t totalLen return false; } const SessionDevicePair *devicePair = reinterpret_cast(ptr); - session_.sourceDeviceId.append(devicePair->sourceDeviceId, DEVICE_ID_SIZE_MAX); - session_.targetDeviceId.append(devicePair->targetDeviceId, DEVICE_ID_SIZE_MAX); + session_.sourceDeviceId.append(devicePair->sourceDeviceId, DEVICE_ID_SIZE_MAX - 1); + session_.targetDeviceId.append(devicePair->targetDeviceId, DEVICE_ID_SIZE_MAX - 1); ptr += sizeof(SessionDevicePair); leftSize -= sizeof(SessionDevicePair); diff --git a/services/distributeddataservice/app/src/session_manager/session_manager.cpp b/services/distributeddataservice/app/src/session_manager/session_manager.cpp index 93d7a60427f5fca4c7a0684c206062c8c19adcdc..da49c05dc8d0ce74c93b2eac728c766b300a37c1 100644 --- a/services/distributeddataservice/app/src/session_manager/session_manager.cpp +++ b/services/distributeddataservice/app/src/session_manager/session_manager.cpp @@ -19,7 +19,7 @@ #include -#include "auth/auth_delegate.h" +#include "auth_delegate.h" #include "checker/checker_manager.h" #include "log/log_print.h" #include "user_delegate.h" diff --git a/services/distributeddataservice/app/src/session_manager/upgrade_manager.h b/services/distributeddataservice/app/src/session_manager/upgrade_manager.h index 7b04ec81390c6682456c2173e1abc07f710eadba..18c044b2596606554675ecd38576b80441058dba 100644 --- a/services/distributeddataservice/app/src/session_manager/upgrade_manager.h +++ b/services/distributeddataservice/app/src/session_manager/upgrade_manager.h @@ -17,7 +17,7 @@ #define DISTRIBUTEDDATAMGR_UPGRADE_MANAGER_H #include -#include "auth/auth_delegate.h" +#include "auth_delegate.h" #include "concurrent_map.h" #include "kvstore_meta_manager.h" #include "metadata/capability_meta_data.h" diff --git a/services/distributeddataservice/app/src/single_kvstore_impl.cpp b/services/distributeddataservice/app/src/single_kvstore_impl.cpp index ddd67136104b037f3a9e5958d3fc556be137a797..4f58d0c7fde4a4e1993b9ebb77ec23a427f2ec1d 100644 --- a/services/distributeddataservice/app/src/single_kvstore_impl.cpp +++ b/services/distributeddataservice/app/src/single_kvstore_impl.cpp @@ -18,12 +18,12 @@ #include "single_kvstore_impl.h" #include #include "account_delegate.h" +#include "auth_delegate.h" #include "backup_handler.h" #include "checker/checker_manager.h" #include "constant.h" #include "dds_trace.h" #include "device_kvstore_impl.h" -#include "auth/auth_delegate.h" #include "kvstore_data_service.h" #include "kvstore_utils.h" #include "ipc_skeleton.h" diff --git a/services/distributeddataservice/app/test/BUILD.gn b/services/distributeddataservice/app/test/BUILD.gn index 373e35701e2cbf980e282b0b681e4a1aff8fefac..014e2a47757a13b3a4cb1b6d83ba42b6ff650a82 100644 --- a/services/distributeddataservice/app/test/BUILD.gn +++ b/services/distributeddataservice/app/test/BUILD.gn @@ -88,6 +88,7 @@ ohos_unittest("KvStoreImplLogicalIsolationTest") { external_deps = [ "dataclassification:data_transit_mgr", + "deviceauth_standard:deviceauth_sdk", "hiviewdfx_hilog_native:libhilog", "huks:libhukssdk", "ipc:ipc_core", @@ -103,6 +104,7 @@ ohos_unittest("KvStoreImplLogicalIsolationTest") { } kv_sources = [ + "../../service/kvdb/auth_delegate.cpp", "../../service/kvdb/executor_factory.cpp", "../../service/kvdb/user_delegate.cpp", ] @@ -112,7 +114,6 @@ ohos_unittest("KvStoreImplLogicalIsolationTest") { "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/distributeddata:distributeddata_inner", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter:distributeddata_adapter", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/account:distributeddata_account_static", - "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/auth:distributeddata_auth_static", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/broadcaster:distributeddata_broadcaster_static", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/permission:distributeddata_permission_static", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/utils:distributeddata_utils_static", @@ -157,6 +158,7 @@ ohos_unittest("KvStoreImplPhysicalIsolationTest") { "unittest/kvstore_impl_physical_isolation_test.cpp", ] kv_sources = [ + "../../service/kvdb/auth_delegate.cpp", "../../service/kvdb/executor_factory.cpp", "../../service/kvdb/user_delegate.cpp", ] @@ -167,6 +169,7 @@ ohos_unittest("KvStoreImplPhysicalIsolationTest") { external_deps = [ "dataclassification:data_transit_mgr", + "deviceauth_standard:deviceauth_sdk", "hiviewdfx_hilog_native:libhilog", "huks:libhukssdk", "ipc:ipc_core", @@ -185,7 +188,6 @@ ohos_unittest("KvStoreImplPhysicalIsolationTest") { "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/distributeddata:distributeddata_inner", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter:distributeddata_adapter", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/account:distributeddata_account_static", - "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/auth:distributeddata_auth_static", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/broadcaster:distributeddata_broadcaster_static", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/permission:distributeddata_permission_static", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/utils:distributeddata_utils_static", @@ -230,6 +232,7 @@ ohos_unittest("KvStoreDataServiceTest") { "unittest/kvstore_data_service_test.cpp", ] kv_sources = [ + "../../service/kvdb/auth_delegate.cpp", "../../service/kvdb/executor_factory.cpp", "../../service/kvdb/user_delegate.cpp", ] @@ -239,6 +242,7 @@ ohos_unittest("KvStoreDataServiceTest") { external_deps = [ "dataclassification:data_transit_mgr", + "deviceauth_standard:deviceauth_sdk", "hiviewdfx_hilog_native:libhilog", "huks:libhukssdk", "ipc:ipc_core", @@ -257,7 +261,6 @@ ohos_unittest("KvStoreDataServiceTest") { "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/distributeddata:distributeddata_inner", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter:distributeddata_adapter", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/account:distributeddata_account_static", - "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/auth:distributeddata_auth_static", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/broadcaster:distributeddata_broadcaster_static", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/permission:distributeddata_permission_static", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/utils:distributeddata_utils_static", @@ -303,6 +306,7 @@ ohos_unittest("KvStoreBackupTest") { "unittest/kvstore_backup_test.cpp", ] kv_sources = [ + "../../service/kvdb/auth_delegate.cpp", "../../service/kvdb/executor_factory.cpp", "../../service/kvdb/user_delegate.cpp", ] @@ -313,6 +317,7 @@ ohos_unittest("KvStoreBackupTest") { external_deps = [ "dataclassification:data_transit_mgr", + "deviceauth_standard:deviceauth_sdk", "hiviewdfx_hilog_native:libhilog", "huks:libhukssdk", "ipc:ipc_core", @@ -331,7 +336,6 @@ ohos_unittest("KvStoreBackupTest") { "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/distributeddata:distributeddata_inner", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter:distributeddata_adapter", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/account:distributeddata_account_static", - "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/auth:distributeddata_auth_static", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/broadcaster:distributeddata_broadcaster_static", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/permission:distributeddata_permission_static", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/utils:distributeddata_utils_static", @@ -356,6 +360,7 @@ ohos_unittest("KvStoreFlowCtrlManagerTest") { external_deps = [ "dataclassification:data_transit_mgr", + "deviceauth_standard:deviceauth_sdk", "hiviewdfx_hilog_native:libhilog", "huks:libhukssdk", "ipc:ipc_core", @@ -411,6 +416,7 @@ ohos_unittest("KvStoreSyncManagerTest") { "../src/single_kvstore_impl.cpp", ] kv_sources = [ + "../../service/kvdb/auth_delegate.cpp", "../../service/kvdb/executor_factory.cpp", "../../service/kvdb/user_delegate.cpp", ] @@ -420,6 +426,7 @@ ohos_unittest("KvStoreSyncManagerTest") { external_deps = [ "dataclassification:data_transit_mgr", + "deviceauth_standard:deviceauth_sdk", "hiviewdfx_hilog_native:libhilog", "huks:libhukssdk", "ipc:ipc_core", @@ -438,7 +445,6 @@ ohos_unittest("KvStoreSyncManagerTest") { "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/distributeddata:distributeddata_inner", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter:distributeddata_adapter", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/account:distributeddata_account_static", - "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/auth:distributeddata_auth_static", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/broadcaster:distributeddata_broadcaster_static", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/permission:distributeddata_permission_static", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/utils:distributeddata_utils_static", @@ -483,6 +489,7 @@ ohos_unittest("KvStoreUninstallerTest") { "unittest/uninstaller_test.cpp", ] kv_sources = [ + "../../service/kvdb/auth_delegate.cpp", "../../service/kvdb/executor_factory.cpp", "../../service/kvdb/user_delegate.cpp", ] @@ -494,7 +501,6 @@ ohos_unittest("KvStoreUninstallerTest") { "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/distributeddata:distributeddata_inner", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter:distributeddata_adapter", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/account:distributeddata_account_static", - "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/auth:distributeddata_auth_static", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/broadcaster:distributeddata_broadcaster_static", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/permission:distributeddata_permission_static", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/utils:distributeddata_utils_static", @@ -511,6 +517,7 @@ ohos_unittest("KvStoreUninstallerTest") { "ability_base:base", "ability_base:want", "dataclassification:data_transit_mgr", + "deviceauth_standard:deviceauth_sdk", "hiviewdfx_hilog_native:libhilog", "huks:libhukssdk", "ipc:ipc_core", diff --git a/services/distributeddataservice/adapter/auth/src/auth_delegate.cpp b/services/distributeddataservice/service/kvdb/auth_delegate.cpp similarity index 85% rename from services/distributeddataservice/adapter/auth/src/auth_delegate.cpp rename to services/distributeddataservice/service/kvdb/auth_delegate.cpp index 310e73926b0487122d98769a2e9e58f33b4fcf38..cc82b2a2593daa313aff35a77690f8495a01c864 100644 --- a/services/distributeddataservice/adapter/auth/src/auth_delegate.cpp +++ b/services/distributeddataservice/service/kvdb/auth_delegate.cpp @@ -1,159 +1,184 @@ -/* - * Copyright (c) 2022 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. - */ - -#define LOG_TAG "AuthHandler" -#include "auth/auth_delegate.h" - -#include "checker/checker_manager.h" -#include "communication_provider.h" -#include "device_auth.h" -#include "device_auth_defines.h" -#include "log_print.h" -#include "utils/anonymous.h" - -namespace OHOS::DistributedData { -bool AuthHandler::CheckAccess( - int localUserId, int peerUserId, const std::string &peerDeviceId, const std::string &appId) -{ - auto group = GetGroupInfo(localUserId, appId, peerDeviceId); - if (group.groupType < GroupType::ALL_GROUP) { - ZLOGE("failed to parse group %{public}s)", group.groupId.c_str()); - return false; - } - auto groupManager = GetGmInstance(); - if (groupManager == nullptr || groupManager->checkAccessToGroup == nullptr) { - ZLOGE("failed to get group manager"); - return false; - } - auto ret = groupManager->checkAccessToGroup(localUserId, appId.c_str(), group.groupId.c_str()); - ZLOGD("check access to group ret:%{public}d", ret); - return ret == HC_SUCCESS; -} - -int32_t AuthHandler::GetGroupType( - int localUserId, int peerUserId, const std::string &peerDeviceId, const std::string &appId) -{ - auto group = GetGroupInfo(localUserId, appId, peerDeviceId); - if (group.groupType < GroupType::ALL_GROUP) { - ZLOGE("failed to parse group json(%{public}d)", group.groupType); - } - return group.groupType; -} - -AuthHandler::RelatedGroup AuthHandler::GetGroupInfo( - int32_t localUserId, const std::string &appId, const std::string &peerDeviceId) -{ - auto groupManager = GetGmInstance(); - if (groupManager == nullptr || groupManager->getRelatedGroups == nullptr || groupManager->destroyInfo == nullptr) { - ZLOGE("failed to get group manager"); - return {}; - } - char *groupInfo = nullptr; - uint32_t groupNum = 0; - ZLOGI("get related groups, user:%{public}d, app:%{public}s", localUserId, appId.c_str()); - auto ret = groupManager->getRelatedGroups(localUserId, appId.c_str(), peerDeviceId.c_str(), &groupInfo, &groupNum); - if (groupInfo == nullptr) { - ZLOGE("failed to get related groups, ret:%{public}d", ret); - return {}; - } - ZLOGI("get related group json :%{public}s", groupInfo); - std::vector groups; - RelatedGroup::Unmarshall(groupInfo, groups); - groupManager->destroyInfo(&groupInfo); - - // same account has priority - std::sort(groups.begin(), groups.end(), - [](const RelatedGroup &group1, const RelatedGroup &group2) { return group1.groupType < group2.groupType; }); - if (!groups.empty()) { - ZLOGI("get group type:%{public}d", groups.front().groupType); - return groups.front(); - } - ZLOGD("there is no group to access to peer device:%{public}s", Anonymous::Change(peerDeviceId).c_str()); - return {}; -} - -std::vector AuthHandler::GetTrustedDevicesByType( - AUTH_GROUP_TYPE type, int32_t localUserId, const std::string &appId) -{ - auto groupManager = GetGmInstance(); - if (groupManager == nullptr || groupManager->getRelatedGroups == nullptr - || groupManager->getTrustedDevices == nullptr || groupManager->destroyInfo == nullptr) { - ZLOGE("failed to get group manager"); - return {}; - } - - char *groupsJson = nullptr; - uint32_t groupNum = 0; - ZLOGI("get joined groups, user:%{public}d, app:%{public}s, type:%{public}d", localUserId, appId.c_str(), type); - auto ret = groupManager->getJoinedGroups(localUserId, appId.c_str(), type, &groupsJson, &groupNum); - if (groupsJson == nullptr) { - ZLOGE("failed to get joined groups, ret:%{public}d", ret); - return {}; - } - ZLOGI("get joined group json :%{public}s", groupsJson); - std::vector groups; - RelatedGroup::Unmarshall(groupsJson, groups); - groupManager->destroyInfo(&groupsJson); - - std::vector trustedDevices; - for (const auto &group : groups) { - if (group.groupType != type) { - continue; - } - char *devicesJson = nullptr; - uint32_t devNum = 0; - ret = groupManager->getTrustedDevices(localUserId, appId.c_str(), group.groupId.c_str(), &devicesJson, &devNum); - if (devicesJson == nullptr) { - ZLOGE("failed to get trusted devicesJson, ret:%{public}d", ret); - return {}; - } - ZLOGI("get trusted device json:%{public}s", devicesJson); - std::vector devices; - TrustDevice::Unmarshall(devicesJson, devices); - groupManager->destroyInfo(&devicesJson); - for (const auto &item : devices) { - auto &provider = AppDistributedKv::CommunicationProvider::GetInstance(); - auto networkId = provider.ToNodeId(item.authId); - auto uuid = provider.GetUuidByNodeId(networkId); - trustedDevices.push_back(uuid); - } - } - - return trustedDevices; -} - -bool AuthHandlerStub::CheckAccess( - int localUserId, int peerUserId, const std::string &peerDeviceId, const std::string &appId) -{ - auto checker = CheckerManager::GetInstance().GetChecker("SystemChecker"); - if (checker == nullptr) { - ZLOGE("get system checker failed"); - return false; - } - bool isSystemApp = checker->IsValid(UID_CAPACITY * localUserId, appId); - if (isSystemApp) { - ZLOGE("system app:%{public}s", appId.c_str()); - return peerUserId == SYSTEM_USER; - } - return peerUserId != SYSTEM_USER; -} - -AuthHandler *AuthDelegate::GetInstance() -{ - // change auth way in the future - static AuthHandlerStub instance; - return &instance; -} +/* + * Copyright (c) 2022 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. + */ + +#define LOG_TAG "AuthHandler" +#include "auth_delegate.h" + +#include "checker/checker_manager.h" +#include "communication_provider.h" +#include "device_auth.h" +#include "device_auth_defines.h" +#include "log_print.h" +#include "utils/anonymous.h" +#include "user_delegate.h" + +namespace OHOS::DistributedData { +class AuthHandlerStub : public AuthHandler { +public: + // override for mock auth in current version, need remove in the future + bool CheckAccess( + int localUserId, int peerUserId, const std::string &peerDeviceId, const std::string &appId) override; + +private: + bool IsUserActive(const std::vector &userStatus, int32_t userId); + static constexpr pid_t UID_CAPACITY = 10000; + static constexpr int SYSTEM_USER = 0; +}; + +bool AuthHandler::CheckAccess( + int localUserId, int peerUserId, const std::string &peerDeviceId, const std::string &appId) +{ + auto group = GetGroupInfo(localUserId, appId, peerDeviceId); + if (group.groupType < GroupType::ALL_GROUP) { + ZLOGE("failed to parse group %{public}s)", group.groupId.c_str()); + return false; + } + auto groupManager = GetGmInstance(); + if (groupManager == nullptr || groupManager->checkAccessToGroup == nullptr) { + ZLOGE("failed to get group manager"); + return false; + } + auto ret = groupManager->checkAccessToGroup(localUserId, appId.c_str(), group.groupId.c_str()); + ZLOGD("check access to group ret:%{public}d", ret); + return ret == HC_SUCCESS; +} + +int32_t AuthHandler::GetGroupType( + int localUserId, int peerUserId, const std::string &peerDeviceId, const std::string &appId) +{ + auto group = GetGroupInfo(localUserId, appId, peerDeviceId); + if (group.groupType < GroupType::ALL_GROUP) { + ZLOGE("failed to parse group json(%{public}d)", group.groupType); + } + return group.groupType; +} + +AuthHandler::RelatedGroup AuthHandler::GetGroupInfo( + int32_t localUserId, const std::string &appId, const std::string &peerDeviceId) +{ + auto groupManager = GetGmInstance(); + if (groupManager == nullptr || groupManager->getRelatedGroups == nullptr || groupManager->destroyInfo == nullptr) { + ZLOGE("failed to get group manager"); + return {}; + } + char *groupInfo = nullptr; + uint32_t groupNum = 0; + ZLOGI("get related groups, user:%{public}d, app:%{public}s", localUserId, appId.c_str()); + auto ret = groupManager->getRelatedGroups(localUserId, appId.c_str(), peerDeviceId.c_str(), &groupInfo, &groupNum); + if (groupInfo == nullptr) { + ZLOGE("failed to get related groups, ret:%{public}d", ret); + return {}; + } + ZLOGI("get related group json :%{public}s", groupInfo); + std::vector groups; + RelatedGroup::Unmarshall(groupInfo, groups); + groupManager->destroyInfo(&groupInfo); + + // same account has priority + std::sort(groups.begin(), groups.end(), + [](const RelatedGroup &group1, const RelatedGroup &group2) { return group1.groupType < group2.groupType; }); + if (!groups.empty()) { + ZLOGI("get group type:%{public}d", groups.front().groupType); + return groups.front(); + } + ZLOGD("there is no group to access to peer device:%{public}s", Anonymous::Change(peerDeviceId).c_str()); + return {}; +} + +std::vector AuthHandler::GetTrustedDevicesByType( + AUTH_GROUP_TYPE type, int32_t localUserId, const std::string &appId) +{ + auto groupManager = GetGmInstance(); + if (groupManager == nullptr || groupManager->getRelatedGroups == nullptr + || groupManager->getTrustedDevices == nullptr || groupManager->destroyInfo == nullptr) { + ZLOGE("failed to get group manager"); + return {}; + } + + char *groupsJson = nullptr; + uint32_t groupNum = 0; + ZLOGI("get joined groups, user:%{public}d, app:%{public}s, type:%{public}d", localUserId, appId.c_str(), type); + auto ret = groupManager->getJoinedGroups(localUserId, appId.c_str(), type, &groupsJson, &groupNum); + if (groupsJson == nullptr) { + ZLOGE("failed to get joined groups, ret:%{public}d", ret); + return {}; + } + ZLOGI("get joined group json :%{public}s", groupsJson); + std::vector groups; + RelatedGroup::Unmarshall(groupsJson, groups); + groupManager->destroyInfo(&groupsJson); + + std::vector trustedDevices; + for (const auto &group : groups) { + if (group.groupType != type) { + continue; + } + char *devicesJson = nullptr; + uint32_t devNum = 0; + ret = groupManager->getTrustedDevices(localUserId, appId.c_str(), group.groupId.c_str(), &devicesJson, &devNum); + if (devicesJson == nullptr) { + ZLOGE("failed to get trusted devicesJson, ret:%{public}d", ret); + return {}; + } + ZLOGI("get trusted device json:%{public}s", devicesJson); + std::vector devices; + TrustDevice::Unmarshall(devicesJson, devices); + groupManager->destroyInfo(&devicesJson); + for (const auto &item : devices) { + auto &provider = AppDistributedKv::CommunicationProvider::GetInstance(); + auto networkId = provider.ToNodeId(item.authId); + auto uuid = provider.GetUuidByNodeId(networkId); + trustedDevices.push_back(uuid); + } + } + + return trustedDevices; +} + +bool AuthHandlerStub::CheckAccess( + int localUserId, int peerUserId, const std::string &peerDeviceId, const std::string &appId) +{ + auto checker = CheckerManager::GetInstance().GetChecker("SystemChecker"); + if (checker == nullptr) { + ZLOGE("get system checker failed"); + return false; + } + bool isSystemApp = checker->IsValid(UID_CAPACITY * localUserId, appId); + if (isSystemApp) { + ZLOGE("system app:%{public}s", appId.c_str()); + return peerUserId == SYSTEM_USER; + } + auto localUsers = UserDelegate::GetInstance().GetLocalUserStatus(); + auto peerUsers = UserDelegate::GetInstance().GetRemoteUserStatus(peerDeviceId); + return peerUserId != SYSTEM_USER && IsUserActive(localUsers, localUserId) && IsUserActive(peerUsers, peerUserId); +} + +bool AuthHandlerStub::IsUserActive(const std::vector &users, int32_t userId) +{ + for (const auto &user : users) { + if (user.id == userId && user.isActive) { + return true; + } + } + return false; +} + +AuthHandler *AuthDelegate::GetInstance() +{ + // change auth way in the future + static AuthHandlerStub instance; + return &instance; +} } // namespace OHOS::DistributedData \ No newline at end of file diff --git a/services/distributeddataservice/adapter/include/auth/auth_delegate.h b/services/distributeddataservice/service/kvdb/auth_delegate.h similarity index 86% rename from services/distributeddataservice/adapter/include/auth/auth_delegate.h rename to services/distributeddataservice/service/kvdb/auth_delegate.h index b4af68d9e55ff1e5b7df362fcff305117ae47aea..e6335c071bf53563dd6db3c00029283a03336f31 100644 --- a/services/distributeddataservice/adapter/include/auth/auth_delegate.h +++ b/services/distributeddataservice/service/kvdb/auth_delegate.h @@ -1,103 +1,93 @@ -/* - * Copyright (c) 2022 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 DISTRIBUTEDDATAMGR_AUTH_DELEGATE_H -#define DISTRIBUTEDDATAMGR_AUTH_DELEGATE_H - -#include - -#include "serializable/serializable.h" -namespace OHOS::DistributedData { -enum AUTH_GROUP_TYPE { - ALL_GROUP = 0, - IDENTICAL_ACCOUNT_GROUP = 1, - PEER_TO_PEER_GROUP = 256, - COMPATIBLE_GROUP = 512, - ACROSS_ACCOUNT_AUTHORIZE_GROUP = 1282 -}; - -class AuthHandler { -public: - virtual bool CheckAccess( - int localUserId, int peerUserId, const std::string &peerDeviceId, const std::string &appId); - virtual int32_t GetGroupType( - int localUserId, int peerUserId, const std::string &peerDeviceId, const std::string &appId); - virtual std::vector GetTrustedDevicesByType( - AUTH_GROUP_TYPE type, int32_t localUserId, const std::string &appId); - -private: - struct RelatedGroup final : public Serializable { - int32_t groupType = -1; - std::string groupId; - RelatedGroup() - { - } - ~RelatedGroup() - { - } - RelatedGroup(const RelatedGroup &) = default; - RelatedGroup &operator=(const RelatedGroup &) = default; - bool Marshal(json &node) const override - { - SetValue(node[GET_NAME(groupType)], groupType); - SetValue(node[GET_NAME(groupId)], groupId); - return true; - } - - bool Unmarshal(const json &node) override - { - GetValue(node, GET_NAME(groupType), groupType); - GetValue(node, GET_NAME(groupId), groupId); - return true; - } - }; - - struct TrustDevice final : public Serializable { - std::string authId; // udid - TrustDevice() = default; - TrustDevice(const TrustDevice &) = default; - TrustDevice &operator=(const TrustDevice &) = default; - bool Marshal(json &node) const override - { - SetValue(node[GET_NAME(authId)], authId); - return true; - } - - bool Unmarshal(const json &node) override - { - GetValue(node, GET_NAME(authId), authId); - return true; - } - }; - static RelatedGroup GetGroupInfo(int32_t localUserId, const std::string &appId, const std::string &peerDeviceId); -}; - -class AuthHandlerStub : public AuthHandler { -public: - // override for mock auth in current version, need remove in the future - bool CheckAccess( - int localUserId, int peerUserId, const std::string &peerDeviceId, const std::string &appId) override; - -private: - static constexpr pid_t UID_CAPACITY = 10000; - static constexpr int SYSTEM_USER = 0; -}; - -class AuthDelegate { -public: - API_EXPORT static AuthHandler *GetInstance(); -}; -} // namespace OHOS::DistributedData -#endif // DISTRIBUTEDDATAMGR_AUTH_DELEGATE_H +/* + * Copyright (c) 2022 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 DISTRIBUTEDDATAMGR_AUTH_DELEGATE_H +#define DISTRIBUTEDDATAMGR_AUTH_DELEGATE_H + +#include + +#include "metadata/user_meta_data.h" +#include "serializable/serializable.h" +namespace OHOS::DistributedData { +enum AUTH_GROUP_TYPE { + ALL_GROUP = 0, + IDENTICAL_ACCOUNT_GROUP = 1, + PEER_TO_PEER_GROUP = 256, + COMPATIBLE_GROUP = 512, + ACROSS_ACCOUNT_AUTHORIZE_GROUP = 1282 +}; + +class AuthHandler { +public: + virtual bool CheckAccess( + int localUserId, int peerUserId, const std::string &peerDeviceId, const std::string &appId); + virtual int32_t GetGroupType( + int localUserId, int peerUserId, const std::string &peerDeviceId, const std::string &appId); + virtual std::vector GetTrustedDevicesByType( + AUTH_GROUP_TYPE type, int32_t localUserId, const std::string &appId); + +private: + struct RelatedGroup final : public Serializable { + int32_t groupType = -1; + std::string groupId; + RelatedGroup() + { + } + ~RelatedGroup() + { + } + RelatedGroup(const RelatedGroup &) = default; + RelatedGroup &operator=(const RelatedGroup &) = default; + bool Marshal(json &node) const override + { + SetValue(node[GET_NAME(groupType)], groupType); + SetValue(node[GET_NAME(groupId)], groupId); + return true; + } + + bool Unmarshal(const json &node) override + { + GetValue(node, GET_NAME(groupType), groupType); + GetValue(node, GET_NAME(groupId), groupId); + return true; + } + }; + + struct TrustDevice final : public Serializable { + std::string authId; // udid + TrustDevice() = default; + TrustDevice(const TrustDevice &) = default; + TrustDevice &operator=(const TrustDevice &) = default; + bool Marshal(json &node) const override + { + SetValue(node[GET_NAME(authId)], authId); + return true; + } + + bool Unmarshal(const json &node) override + { + GetValue(node, GET_NAME(authId), authId); + return true; + } + }; + static RelatedGroup GetGroupInfo(int32_t localUserId, const std::string &appId, const std::string &peerDeviceId); +}; + +class AuthDelegate { +public: + API_EXPORT static AuthHandler *GetInstance(); +}; +} // namespace OHOS::DistributedData +#endif // DISTRIBUTEDDATAMGR_AUTH_DELEGATE_H diff --git a/services/distributeddataservice/service/kvdb/user_delegate.h b/services/distributeddataservice/service/kvdb/user_delegate.h index 97627c5898c2e2cfe21e9701680c4287a1953554..8dff1b316113e171de88ea82a76cc7f7396f8e90 100644 --- a/services/distributeddataservice/service/kvdb/user_delegate.h +++ b/services/distributeddataservice/service/kvdb/user_delegate.h @@ -46,6 +46,10 @@ private: explicit LocalUserObserver(UserDelegate &userDelegate); void OnAccountChanged(const DistributedKv::AccountEventInfo &eventInfo) override; std::string Name() override; + LevelType GetLevel() override + { + return LevelType::HIGH; + } private: UserDelegate &userDelegate_; diff --git a/services/distributeddataservice/service/rdb/rdb_syncer.cpp b/services/distributeddataservice/service/rdb/rdb_syncer.cpp index 431ebc417d593382b609a774584cb24d0fc4f009..141c5820eb5abfb18b75fd873a06a471137cfeb4 100644 --- a/services/distributeddataservice/service/rdb/rdb_syncer.cpp +++ b/services/distributeddataservice/service/rdb/rdb_syncer.cpp @@ -118,7 +118,7 @@ int32_t RdbSyncer::CreateMetaData() newMeta.storeId = GetStoreId(); newMeta.uid = uid_; newMeta.user = AccountDelegate::GetInstance()->GetDeviceAccountIdByUID(uid_); - newMeta.account = AccountDelegate::GetInstance()->GetCurrentAccountId(GetBundleName()); + newMeta.account = AccountDelegate::GetInstance()->GetCurrentAccountId(); newMeta.dataDir = GetPath(); auto metaKey = StoreMetaData::GetKey({ newMeta.user, "default", newMeta.bundleName, newMeta.storeId });