1 /*
2 * Copyright (c) 2023 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 #define LOG_TAG "LoadConfigCommonStrategy"
16 #include "load_config_common_strategy.h"
17
18 #include "accesstoken_kit.h"
19 #include "account/account_delegate.h"
20 #include "hap_token_info.h"
21 #include "ipc_skeleton.h"
22 #include "log_print.h"
23 #include "uri_utils.h"
24
25 namespace OHOS::DataShare {
26 constexpr const char USER_PARAM[] = "user";
27 constexpr const char TOKEN_ID_PARAM[] = "srcToken";
28 constexpr const char DST_BUNDLE_NAME_PARAM[] = "dstBundleName";
operator ()(std::shared_ptr<Context> context)29 bool LoadConfigCommonStrategy::operator()(std::shared_ptr<Context> context)
30 {
31 if (context->callerTokenId == 0) {
32 context->callerTokenId = IPCSkeleton::GetCallingTokenID();
33 }
34 context->currentUserId = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(context->callerTokenId);
35 if (!URIUtils::GetAppIndexFromProxyURI(context->uri, context->appIndex)) {
36 return false;
37 }
38 // sa, userId is in uri, caller token id is from first caller tokenId
39 if (context->currentUserId == 0) {
40 GetInfoFromProxyURI(
41 context->uri, context->currentUserId, context->callerTokenId, context->calledBundleName);
42 URIUtils::FormatUri(context->uri);
43 }
44 if (context->needAutoLoadCallerBundleName && context->callerBundleName.empty()) {
45 Security::AccessToken::HapTokenInfo tokenInfo;
46 auto result = Security::AccessToken::AccessTokenKit::GetHapTokenInfo(context->callerTokenId, tokenInfo);
47 if (result != Security::AccessToken::RET_SUCCESS) {
48 ZLOGE("token:0x%{public}x, result:%{public}d", context->callerTokenId, result);
49 return false;
50 }
51 context->callerBundleName = tokenInfo.bundleName;
52 }
53 return true;
54 }
55
GetInfoFromProxyURI(const std::string & uri,int32_t & user,uint32_t & callerTokenId,std::string & calledBundleName)56 bool LoadConfigCommonStrategy::GetInfoFromProxyURI(
57 const std::string &uri, int32_t &user, uint32_t &callerTokenId, std::string &calledBundleName)
58 {
59 auto queryParams = URIUtils::GetQueryParams(uri);
60 if (!queryParams[USER_PARAM].empty()) {
61 auto [success, data] = URIUtils::Strtoul(queryParams[USER_PARAM]);
62 if (!success) {
63 return false;
64 }
65 user = static_cast<int32_t>(std::move(data));
66 }
67 if (!queryParams[TOKEN_ID_PARAM].empty()) {
68 auto [success, data] = URIUtils::Strtoul(queryParams[TOKEN_ID_PARAM]);
69 if (!success) {
70 return false;
71 }
72 callerTokenId = std::move(data);
73 }
74 if (!queryParams[DST_BUNDLE_NAME_PARAM].empty()) {
75 calledBundleName = queryParams[DST_BUNDLE_NAME_PARAM];
76 }
77 return true;
78 }
79 } // namespace OHOS::DataShare