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 // sa, userId is in uri, caller token id is from first caller tokenId
36 if (context->currentUserId == 0) {
37 GetInfoFromProxyURI(
38 context->uri, context->currentUserId, context->callerTokenId, context->calledBundleName);
39 URIUtils::FormatUri(context->uri);
40 }
41 if (context->needAutoLoadCallerBundleName && context->callerBundleName.empty()) {
42 Security::AccessToken::HapTokenInfo tokenInfo;
43 auto result = Security::AccessToken::AccessTokenKit::GetHapTokenInfo(context->callerTokenId, tokenInfo);
44 if (result != Security::AccessToken::RET_SUCCESS) {
45 ZLOGE("token:0x%{public}x, result:%{public}d", context->callerTokenId, result);
46 return false;
47 }
48 context->callerBundleName = tokenInfo.bundleName;
49 }
50 return true;
51 }
52
GetInfoFromProxyURI(const std::string & uri,int32_t & user,uint32_t & callerTokenId,std::string & calledBundleName)53 bool LoadConfigCommonStrategy::GetInfoFromProxyURI(
54 const std::string &uri, int32_t &user, uint32_t &callerTokenId, std::string &calledBundleName)
55 {
56 auto queryParams = URIUtils::GetQueryParams(uri);
57 if (!queryParams[USER_PARAM].empty()) {
58 auto [success, data] = URIUtils::Strtoul(queryParams[USER_PARAM]);
59 if (!success) {
60 return false;
61 }
62 user = static_cast<int32_t>(std::move(data));
63 }
64 if (!queryParams[TOKEN_ID_PARAM].empty()) {
65 auto [success, data] = URIUtils::Strtoul(queryParams[TOKEN_ID_PARAM]);
66 if (!success) {
67 return false;
68 }
69 callerTokenId = std::move(data);
70 }
71 if (!queryParams[DST_BUNDLE_NAME_PARAM].empty()) {
72 calledBundleName = queryParams[DST_BUNDLE_NAME_PARAM];
73 }
74 return true;
75 }
76 } // namespace OHOS::DataShare