1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #define LOG_TAG "AuthHandler"
17 #include "auth_delegate.h"
18
19 #include "checker/checker_manager.h"
20 #include "device_auth.h"
21 #include "device_auth_defines.h"
22 #include "device_manager_adapter.h"
23 #include "log_print.h"
24 #include "user_delegate.h"
25 #include "utils/anonymous.h"
26 #include "metadata/store_meta_data.h"
27 #include "metadata/meta_data_manager.h"
28 namespace OHOS::DistributedData {
29 using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter;
30 class AuthHandlerStub : public AuthHandler {
31 public:
32 // override for mock auth in current version, need remove in the future
33 bool CheckAccess(
34 int localUserId, int peerUserId, const std::string &peerDeviceId,
35 int32_t authType, bool isSend = true) override;
36 private:
37 bool IsUserActive(const std::vector<UserStatus> &users, int32_t userId);
38 bool CheckUsers(int localUserId, int peerUserId, const std::string &peerDeviceId);
39 static constexpr pid_t UID_CAPACITY = 10000;
40 static constexpr int SYSTEM_USER = 0;
41 };
42
CheckUsers(int localUserId,int peerUserId,const std::string & peerDeviceId)43 bool AuthHandlerStub::CheckUsers(int localUserId, int peerUserId, const std::string &peerDeviceId)
44 {
45 if (localUserId == SYSTEM_USER) {
46 return peerUserId == SYSTEM_USER;
47 }
48
49 auto localUsers = UserDelegate::GetInstance().GetLocalUserStatus();
50 auto peerUsers = UserDelegate::GetInstance().GetRemoteUserStatus(peerDeviceId);
51 return peerUserId != SYSTEM_USER && IsUserActive(localUsers, localUserId) && IsUserActive(peerUsers, peerUserId);
52 }
53
CheckAccess(int localUserId,int peerUserId,const std::string & peerDeviceId,int32_t authType,bool isSend)54 bool AuthHandlerStub::CheckAccess(
55 int localUserId, int peerUserId, const std::string &peerDeviceId, int32_t authType, bool isSend)
56 {
57 if (authType == static_cast<int32_t>(DistributedKv::AuthType::IDENTICAL_ACCOUNT) &&
58 !DmAdapter::GetInstance().IsSameAccount(peerDeviceId)) {
59 ZLOGE("CheckAccess failed.");
60 return false;
61 }
62 return CheckUsers(localUserId, peerUserId, peerDeviceId);
63 }
64
IsUserActive(const std::vector<UserStatus> & users,int32_t userId)65 bool AuthHandlerStub::IsUserActive(const std::vector<UserStatus> &users, int32_t userId)
66 {
67 for (const auto &user : users) {
68 if (user.id == userId && user.isActive) {
69 return true;
70 }
71 }
72 return false;
73 }
74
GetInstance()75 AuthHandler *AuthDelegate::GetInstance()
76 {
77 // change auth way in the future
78 static AuthHandlerStub instance;
79 return &instance;
80 }
81 } // namespace OHOS::DistributedData