• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
27 namespace OHOS::DistributedData {
28 class AuthHandlerStub : public AuthHandler {
29 public:
30     // override for mock auth in current version, need remove in the future
31     bool CheckAccess(
32         int localUserId, int peerUserId, const std::string &peerDeviceId, const std::string &appId) override;
33 
34 private:
35     bool IsUserActive(const std::vector<UserStatus> &users, int32_t userId);
36     static constexpr pid_t UID_CAPACITY = 10000;
37     static constexpr int SYSTEM_USER = 0;
38 };
39 
CheckAccess(int localUserId,int peerUserId,const std::string & peerDeviceId,const std::string & appId)40 bool AuthHandler::CheckAccess(
41     int localUserId, int peerUserId, const std::string &peerDeviceId, const std::string &appId)
42 {
43     auto group = GetGroupInfo(localUserId, appId, peerDeviceId);
44     if (group.groupType < GroupType::ALL_GROUP) {
45         ZLOGE("failed to parse group %{public}s)", group.groupId.c_str());
46         return false;
47     }
48     auto groupManager = GetGmInstance();
49     if (groupManager == nullptr || groupManager->checkAccessToGroup == nullptr) {
50         ZLOGE("failed to get group manager");
51         return false;
52     }
53     auto ret = groupManager->checkAccessToGroup(localUserId, appId.c_str(), group.groupId.c_str());
54     ZLOGD("check access to group ret:%{public}d", ret);
55     return ret == HC_SUCCESS;
56 }
57 
GetGroupType(int localUserId,int peerUserId,const std::string & peerDeviceId,const std::string & appId)58 int32_t AuthHandler::GetGroupType(
59     int localUserId, int peerUserId, const std::string &peerDeviceId, const std::string &appId)
60 {
61     auto group = GetGroupInfo(localUserId, appId, peerDeviceId);
62     if (group.groupType < GroupType::ALL_GROUP) {
63         ZLOGE("failed to parse group json(%{public}d)", group.groupType);
64     }
65     return group.groupType;
66 }
67 
GetGroupInfo(int32_t localUserId,const std::string & appId,const std::string & peerDeviceId)68 AuthHandler::RelatedGroup AuthHandler::GetGroupInfo(
69     int32_t localUserId, const std::string &appId, const std::string &peerDeviceId)
70 {
71     auto groupManager = GetGmInstance();
72     if (groupManager == nullptr || groupManager->getRelatedGroups == nullptr || groupManager->destroyInfo == nullptr) {
73         ZLOGE("failed to get group manager");
74         return {};
75     }
76     char *groupInfo = nullptr;
77     uint32_t groupNum = 0;
78     ZLOGI("get related groups, user:%{public}d, app:%{public}s", localUserId, appId.c_str());
79     auto ret = groupManager->getRelatedGroups(localUserId, appId.c_str(), peerDeviceId.c_str(), &groupInfo, &groupNum);
80     if (groupInfo == nullptr) {
81         ZLOGE("failed to get related groups, ret:%{public}d", ret);
82         return {};
83     }
84     ZLOGI("get related group json :%{public}s", groupInfo);
85     std::vector<RelatedGroup> groups;
86     RelatedGroup::Unmarshall(groupInfo, groups);
87     groupManager->destroyInfo(&groupInfo);
88 
89     // same account has priority
90     std::sort(groups.begin(), groups.end(),
91         [](const RelatedGroup &group1, const RelatedGroup &group2) { return group1.groupType < group2.groupType; });
92     if (!groups.empty()) {
93         ZLOGI("get group type:%{public}d", groups.front().groupType);
94         return groups.front();
95     }
96     ZLOGD("there is no group to access to peer device:%{public}s", Anonymous::Change(peerDeviceId).c_str());
97     return {};
98 }
99 
GetTrustedDevicesByType(AUTH_GROUP_TYPE type,int32_t localUserId,const std::string & appId)100 std::vector<std::string> AuthHandler::GetTrustedDevicesByType(
101     AUTH_GROUP_TYPE type, int32_t localUserId, const std::string &appId)
102 {
103     auto groupManager = GetGmInstance();
104     if (groupManager == nullptr || groupManager->getRelatedGroups == nullptr
105         || groupManager->getTrustedDevices == nullptr || groupManager->destroyInfo == nullptr) {
106         ZLOGE("failed to get group manager");
107         return {};
108     }
109 
110     char *groupsJson = nullptr;
111     uint32_t groupNum = 0;
112     ZLOGI("get joined groups, user:%{public}d, app:%{public}s, type:%{public}d", localUserId, appId.c_str(), type);
113     auto ret = groupManager->getJoinedGroups(localUserId, appId.c_str(), type, &groupsJson, &groupNum);
114     if (groupsJson == nullptr) {
115         ZLOGE("failed to get joined groups, ret:%{public}d", ret);
116         return {};
117     }
118     ZLOGI("get joined group json :%{public}s", groupsJson);
119     std::vector<RelatedGroup> groups;
120     RelatedGroup::Unmarshall(groupsJson, groups);
121     groupManager->destroyInfo(&groupsJson);
122 
123     std::vector<std::string> trustedDevices;
124     for (const auto &group : groups) {
125         if (group.groupType != type) {
126             continue;
127         }
128         char *devicesJson = nullptr;
129         uint32_t devNum = 0;
130         ret = groupManager->getTrustedDevices(localUserId, appId.c_str(), group.groupId.c_str(), &devicesJson, &devNum);
131         if (devicesJson == nullptr) {
132             ZLOGE("failed to get trusted devicesJson, ret:%{public}d", ret);
133             return {};
134         }
135         ZLOGI("get trusted device json:%{public}s", devicesJson);
136         std::vector<TrustDevice> devices;
137         TrustDevice::Unmarshall(devicesJson, devices);
138         groupManager->destroyInfo(&devicesJson);
139         for (const auto &item : devices) {
140             auto &dmAdapter = DeviceManagerAdapter::GetInstance();
141             auto networkId = dmAdapter.ToNetworkID(item.authId);
142             auto uuid = dmAdapter.GetUuidByNetworkId(networkId);
143             trustedDevices.push_back(uuid);
144         }
145     }
146 
147     return trustedDevices;
148 }
149 
CheckAccess(int localUserId,int peerUserId,const std::string & peerDeviceId,const std::string & appId)150 bool AuthHandlerStub::CheckAccess(
151     int localUserId, int peerUserId, const std::string &peerDeviceId, const std::string &appId)
152 {
153     if (localUserId == SYSTEM_USER) {
154         return peerUserId == SYSTEM_USER;
155     }
156 
157     auto localUsers = UserDelegate::GetInstance().GetLocalUserStatus();
158     auto peerUsers = UserDelegate::GetInstance().GetRemoteUserStatus(peerDeviceId);
159     return peerUserId != SYSTEM_USER && IsUserActive(localUsers, localUserId) && IsUserActive(peerUsers, peerUserId);
160 }
161 
IsUserActive(const std::vector<UserStatus> & users,int32_t userId)162 bool AuthHandlerStub::IsUserActive(const std::vector<UserStatus> &users, int32_t userId)
163 {
164     for (const auto &user : users) {
165         if (user.id == userId && user.isActive) {
166             return true;
167         }
168     }
169     return false;
170 }
171 
GetInstance()172 AuthHandler *AuthDelegate::GetInstance()
173 {
174     // change auth way in the future
175     static AuthHandlerStub instance;
176     return &instance;
177 }
178 } // namespace OHOS::DistributedData