• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "update_remote_hap_token_command.h"
17 
18 #include "accesstoken_kit.h"
19 #include "accesstoken_log.h"
20 #include "access_token_error.h"
21 #include "base_remote_command.h"
22 #include "constant_common.h"
23 #include "device_info_manager.h"
24 
25 namespace OHOS {
26 namespace Security {
27 namespace AccessToken {
28 namespace {
29 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
30     LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "UpdateRemoteHapTokenCommand"};
31 }
32 
UpdateRemoteHapTokenCommand(const std::string & srcDeviceId,const std::string & dstDeviceId,const HapTokenInfoForSync & tokenInfo)33 UpdateRemoteHapTokenCommand::UpdateRemoteHapTokenCommand(
34     const std::string &srcDeviceId, const std::string &dstDeviceId, const HapTokenInfoForSync& tokenInfo)
35     : updateTokenInfo_(tokenInfo)
36 {
37     remoteProtocol_.commandName = COMMAND_NAME;
38     remoteProtocol_.uniqueId = COMMAND_NAME;
39     remoteProtocol_.srcDeviceId = srcDeviceId;
40     remoteProtocol_.dstDeviceId = dstDeviceId;
41     remoteProtocol_.responseVersion = Constant::DISTRIBUTED_ACCESS_TOKEN_SERVICE_VERSION;
42     remoteProtocol_.requestVersion = Constant::DISTRIBUTED_ACCESS_TOKEN_SERVICE_VERSION;
43 }
44 
UpdateRemoteHapTokenCommand(const std::string & json)45 UpdateRemoteHapTokenCommand::UpdateRemoteHapTokenCommand(const std::string &json)
46 {
47     nlohmann::json jsonObject = nlohmann::json::parse(json, nullptr, false);
48     BaseRemoteCommand::FromRemoteProtocolJson(jsonObject);
49 
50     if ((jsonObject.find("HapTokenInfos") != jsonObject.end()) && (jsonObject.at("HapTokenInfos").is_object())) {
51         nlohmann::json hapTokenJson = jsonObject.at("HapTokenInfos").get<nlohmann::json>();
52         BaseRemoteCommand::FromHapTokenInfoJson(hapTokenJson, updateTokenInfo_);
53     }
54 }
55 
ToJsonPayload()56 std::string UpdateRemoteHapTokenCommand::ToJsonPayload()
57 {
58     nlohmann::json j = BaseRemoteCommand::ToRemoteProtocolJson();
59     j["HapTokenInfos"] = BaseRemoteCommand::ToHapTokenInfosJson(updateTokenInfo_);
60     return j.dump();
61 }
62 
Prepare()63 void UpdateRemoteHapTokenCommand::Prepare()
64 {
65     remoteProtocol_.statusCode = Constant::SUCCESS;
66     remoteProtocol_.message = Constant::COMMAND_RESULT_SUCCESS;
67     ACCESSTOKEN_LOG_DEBUG(LABEL, "end as: UpdateRemoteHapTokenCommand");
68 }
69 
Execute()70 void UpdateRemoteHapTokenCommand::Execute()
71 {
72     ACCESSTOKEN_LOG_INFO(LABEL, "execute: start as: UpdateRemoteHapTokenCommand");
73 
74     remoteProtocol_.responseDeviceId = ConstantCommon::GetLocalDeviceId();
75     remoteProtocol_.responseVersion = Constant::DISTRIBUTED_ACCESS_TOKEN_SERVICE_VERSION;
76 
77     DeviceInfo devInfo;
78     bool result = DeviceInfoManager::GetInstance().GetDeviceInfo(remoteProtocol_.srcDeviceId,
79         DeviceIdType::UNKNOWN, devInfo);
80     if (!result) {
81         ACCESSTOKEN_LOG_INFO(LABEL, "UpdateRemoteHapTokenCommand: get remote uniqueDeviceId failed");
82         remoteProtocol_.statusCode = Constant::FAILURE_BUT_CAN_RETRY;
83         return;
84     }
85 
86     std::string uniqueDeviceId = devInfo.deviceId.uniqueDeviceId;
87     int ret = AccessTokenKit::SetRemoteHapTokenInfo(uniqueDeviceId, updateTokenInfo_);
88     if (ret != RET_SUCCESS) {
89         remoteProtocol_.statusCode = Constant::FAILURE_BUT_CAN_RETRY;
90         remoteProtocol_.message = Constant::COMMAND_RESULT_FAILED;
91     } else {
92         remoteProtocol_.statusCode = Constant::SUCCESS;
93         remoteProtocol_.message = Constant::COMMAND_RESULT_SUCCESS;
94     }
95 
96     ACCESSTOKEN_LOG_INFO(LABEL, "execute: end as: UpdateRemoteHapTokenCommand");
97 }
98 
Finish()99 void UpdateRemoteHapTokenCommand::Finish()
100 {
101     remoteProtocol_.statusCode = Constant::SUCCESS;
102     ACCESSTOKEN_LOG_INFO(LABEL, "Finish: end as: DeleteUidPermissionCommand");
103 }
104 }  // namespace AccessToken
105 }  // namespace Security
106 }  // namespace OHOS
107 
108