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 "session_manager.h"
17 
18 #define LOG_TAG "SessionManager"
19 
20 #include <algorithm>
21 
22 #include "auth_delegate.h"
23 #include "checker/checker_manager.h"
24 #include "log_print.h"
25 #include "metadata/meta_data_manager.h"
26 #include "metadata/store_meta_data.h"
27 #include "user_delegate.h"
28 #include "utils/anonymous.h"
29 #include "utils/converter.h"
30 #include "types.h"
31 namespace OHOS::DistributedData {
32 using namespace OHOS::DistributedKv;
GetInstance()33 SessionManager &SessionManager::GetInstance()
34 {
35     static SessionManager instance;
36     return instance;
37 }
38 
GetSession(const SessionPoint & from,const std::string & targetDeviceId) const39 Session SessionManager::GetSession(const SessionPoint &from, const std::string &targetDeviceId) const
40 {
41     ZLOGD("begin. peer device:%{public}s", Anonymous::Change(targetDeviceId).c_str());
42     Session session;
43     session.appId = from.appId;
44     session.sourceUserId = from.userId;
45     session.sourceDeviceId = from.deviceId;
46     session.targetDeviceId = targetDeviceId;
47     auto users = UserDelegate::GetInstance().GetRemoteUserStatus(targetDeviceId);
48     // system service
49     if (from.userId == UserDelegate::SYSTEM_USER) {
50         StoreMetaData metaData;
51         metaData.deviceId = from.deviceId;
52         metaData.user = std::to_string(from.userId);
53         metaData.bundleName = from.appId;
54         metaData.storeId = from.storeId;
55         if (MetaDataManager::GetInstance().LoadMeta(metaData.GetKey(), metaData) &&
56             CheckerManager::GetInstance().GetAppId(Converter::ConvertToStoreInfo(metaData)) == from.appId) {
57             session.targetUserIds.push_back(UserDelegate::SYSTEM_USER);
58         }
59     }
60 
61     std::string bundleName = "";
62     int32_t authType = static_cast<int32_t>(AuthType::DEFAULT);
63     if (!GetAuthParams(from, bundleName, authType)) {
64         ZLOGE("GetAuthParams failed");
65         return session;
66     }
67 
68     for (const auto &user : users) {
69         bool isPermitted = AuthDelegate::GetInstance()->CheckAccess(from.userId, user.id,
70             targetDeviceId, authType);
71         ZLOGD("access to peer user %{public}d is %{public}d", user.id, isPermitted);
72         if (isPermitted) {
73             auto it = std::find(session.targetUserIds.begin(), session.targetUserIds.end(), user.id);
74             if (it == session.targetUserIds.end()) {
75                 session.targetUserIds.push_back(user.id);
76             }
77         }
78     }
79     ZLOGD("end");
80     return session;
81 }
82 
GetAuthParams(const SessionPoint & from,std::string & bundleName,int32_t & auth) const83 bool SessionManager::GetAuthParams(const SessionPoint &from, std::string &bundleName, int32_t &auth) const
84 {
85     std::vector<StoreMetaData> metaData;
86     if (!MetaDataManager::GetInstance().LoadMeta(StoreMetaData::GetPrefix({ from.deviceId }), metaData)) {
87         ZLOGW("load meta failed, deviceId:%{public}s", Anonymous::Change(from.deviceId).c_str());
88         return false;
89     }
90     for (const auto &storeMeta : metaData) {
91         if (storeMeta.appId == from.appId) {
92             bundleName = storeMeta.bundleName;
93             auth = storeMeta.authType;
94             break;
95         }
96     }
97     if (bundleName.empty()) {
98         ZLOGE("not find bundleName");
99         return false;
100     }
101     return true;
102 }
103 
CheckSession(const SessionPoint & from,const SessionPoint & to) const104 bool SessionManager::CheckSession(const SessionPoint &from, const SessionPoint &to) const
105 {
106     std::string bundleName = "";
107     int32_t authType = static_cast<int32_t>(AuthType::DEFAULT);
108     if (!GetAuthParams(from, bundleName, authType)) {
109         ZLOGE("GetAuthParams failed");
110         return false;
111     }
112     return AuthDelegate::GetInstance()->CheckAccess(from.userId, to.userId, to.deviceId, authType, false);
113 }
114 
Marshal(json & node) const115 bool Session::Marshal(json &node) const
116 {
117     bool ret = true;
118     ret = SetValue(node[GET_NAME(sourceDeviceId)], sourceDeviceId) && ret;
119     ret = SetValue(node[GET_NAME(targetDeviceId)], targetDeviceId) && ret;
120     ret = SetValue(node[GET_NAME(sourceUserId)], sourceUserId) && ret;
121     ret = SetValue(node[GET_NAME(targetUserIds)], targetUserIds) && ret;
122     ret = SetValue(node[GET_NAME(appId)], appId) && ret;
123     return ret;
124 }
125 
Unmarshal(const json & node)126 bool Session::Unmarshal(const json &node)
127 {
128     bool ret = true;
129     ret = GetValue(node, GET_NAME(sourceDeviceId), sourceDeviceId) && ret;
130     ret = GetValue(node, GET_NAME(targetDeviceId), targetDeviceId) && ret;
131     ret = GetValue(node, GET_NAME(sourceUserId), sourceUserId) && ret;
132     ret = GetValue(node, GET_NAME(targetUserIds), targetUserIds) && ret;
133     ret = GetValue(node, GET_NAME(appId), appId) && ret;
134     return ret;
135 }
136 } // namespace OHOS::DistributedData
137