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 #include "trust_group_manager.h"
17
18 #include "device_profile_errors.h"
19 #include "device_profile_log.h"
20
21 namespace OHOS {
22 namespace DeviceProfile {
23 namespace {
24 const std::string TAG = "TrustGroupManager";
25
26 constexpr int32_t VISIBILITY_PUBLIC = -1;
27 const std::string AUTH_APPID = "device_profile_auth";
28 }
29
30 IMPLEMENT_SINGLE_INSTANCE(TrustGroupManager);
31
from_json(const nlohmann::json & jsonObject,GroupInfo & groupInfo)32 void from_json(const nlohmann::json& jsonObject, GroupInfo& groupInfo)
33 {
34 jsonObject.at(FIELD_GROUP_NAME).get_to(groupInfo.groupName);
35 jsonObject.at(FIELD_GROUP_ID).get_to(groupInfo.groupId);
36 jsonObject.at(FIELD_GROUP_OWNER).get_to(groupInfo.groupOwner);
37 jsonObject.at(FIELD_GROUP_TYPE).get_to(groupInfo.groupType);
38 jsonObject.at(FIELD_GROUP_VISIBILITY).get_to(groupInfo.groupVisibility);
39 }
40
Init()41 void TrustGroupManager::Init()
42 {
43 if (InitDeviceAuthService() != ERR_OK) {
44 HILOGE("auth InitDeviceAuthService failed");
45 return;
46 }
47
48 hichainGmInstance_ = GetGmInstance();
49 if (hichainGmInstance_ == nullptr) {
50 HILOGE("auth GetGmInstance failed");
51 return;
52 }
53 HILOGI("init succeeded");
54 }
55
CheckTrustGroup(const std::string & deviceId)56 bool TrustGroupManager::CheckTrustGroup(const std::string& deviceId)
57 {
58 if (hichainGmInstance_ == nullptr) {
59 Init();
60 }
61
62 if (hichainGmInstance_ == nullptr) {
63 HILOGE("auth GetGmInstance failed");
64 return false;
65 }
66
67 uint32_t groupNum = 0;
68 char* returnGroups = nullptr;
69 int32_t ret = hichainGmInstance_->getRelatedGroups(ANY_OS_ACCOUNT, AUTH_APPID.c_str(), deviceId.c_str(),
70 &returnGroups, &groupNum);
71 if (ret != ERR_OK) {
72 HILOGE("failed, ret:%{public}d", ret);
73 return false;
74 }
75
76 if (returnGroups == nullptr || groupNum == 0) {
77 HILOGE("failed, returnGroups is nullptr");
78 return false;
79 }
80
81 nlohmann::json jsonObject = nlohmann::json::parse(returnGroups, nullptr, false);
82 if (jsonObject.is_discarded()) {
83 HILOGE("returnGroups parse error");
84 return false;
85 }
86
87 std::vector<GroupInfo> groupInfos = jsonObject.get<std::vector<GroupInfo>>();
88 if (groupInfos.empty()) {
89 HILOGE("failed, groupInfos is empty");
90 return false;
91 }
92 for (const auto& groupInfo : groupInfos) {
93 // check group visibility is whether public or not
94 if (groupInfo.groupVisibility != VISIBILITY_PUBLIC) {
95 continue;
96 }
97
98 // check group type is whether (same count or point to point) or not
99 if (groupInfo.groupType == GroupType::IDENTICAL_ACCOUNT_GROUP
100 || groupInfo.groupType == GroupType::PEER_TO_PEER_GROUP) {
101 HILOGI("check success type = %{public}d", groupInfo.groupType);
102 return true;
103 }
104 }
105 HILOGE("check failed, not in trust group");
106 return false;
107 }
108 } // namespace DeviceProfile
109 } // namespace OHOS