• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 using namespace OHOS::DistributedData;
27 constexpr const char USER_PARAM[] = "user";
28 constexpr const char TOKEN_ID_PARAM[] = "srcToken";
29 constexpr const char DST_BUNDLE_NAME_PARAM[] = "dstBundleName";
operator ()(std::shared_ptr<Context> context)30 bool LoadConfigCommonStrategy::operator()(std::shared_ptr<Context> context)
31 {
32     if (context->callerTokenId == 0) {
33         context->callerTokenId = IPCSkeleton::GetCallingTokenID();
34     }
35     context->currentUserId = AccountDelegate::GetInstance()->GetUserByToken(context->callerTokenId);
36     if (!URIUtils::GetAppIndexFromProxyURI(context->uri, context->appIndex)) {
37         return false;
38     }
39     // sa, userId is in uri, caller token id is from first caller tokenId
40     if (context->currentUserId == 0) {
41         GetInfoFromProxyURI(
42             context->uri, context->currentUserId, context->callerTokenId, context->calledBundleName);
43         URIUtils::FormatUri(context->uri);
44     }
45     if (context->needAutoLoadCallerBundleName && context->callerBundleName.empty()) {
46         Security::AccessToken::HapTokenInfo tokenInfo;
47         auto result = Security::AccessToken::AccessTokenKit::GetHapTokenInfo(context->callerTokenId, tokenInfo);
48         if (result != Security::AccessToken::RET_SUCCESS) {
49             ZLOGE("token:0x%{public}x, result:%{public}d", context->callerTokenId, result);
50             return false;
51         }
52         context->callerBundleName = tokenInfo.bundleName;
53     }
54     return true;
55 }
56 
GetInfoFromProxyURI(const std::string & uri,int32_t & user,uint32_t & callerTokenId,std::string & calledBundleName)57 bool LoadConfigCommonStrategy::GetInfoFromProxyURI(
58     const std::string &uri, int32_t &user, uint32_t &callerTokenId, std::string &calledBundleName)
59 {
60     auto queryParams = URIUtils::GetQueryParams(uri);
61     if (!queryParams[USER_PARAM].empty()) {
62         auto [success, data] = URIUtils::Strtoul(queryParams[USER_PARAM]);
63         if (!success) {
64             return false;
65         }
66         user = static_cast<int32_t>(std::move(data));
67     }
68     if (!queryParams[TOKEN_ID_PARAM].empty()) {
69         auto [success, data] = URIUtils::Strtoul(queryParams[TOKEN_ID_PARAM]);
70         if (!success) {
71             return false;
72         }
73         callerTokenId = std::move(data);
74     }
75     if (!queryParams[DST_BUNDLE_NAME_PARAM].empty()) {
76         calledBundleName = queryParams[DST_BUNDLE_NAME_PARAM];
77     }
78     return true;
79 }
80 } // namespace OHOS::DataShare