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