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     if (jsonObject.is_discarded()) {
49         ACCESSTOKEN_LOG_ERROR(LABEL, "JsonObject is invalid.");
50         return;
51     }
52     BaseRemoteCommand::FromRemoteProtocolJson(jsonObject);
53 
54     if ((jsonObject.find("HapTokenInfos") != jsonObject.end()) && (jsonObject.at("HapTokenInfos").is_object())) {
55         nlohmann::json hapTokenJson = jsonObject.at("HapTokenInfos").get<nlohmann::json>();
56         BaseRemoteCommand::FromHapTokenInfoJson(hapTokenJson, updateTokenInfo_);
57     }
58 }
59 
ToJsonPayload()60 std::string UpdateRemoteHapTokenCommand::ToJsonPayload()
61 {
62     nlohmann::json j = BaseRemoteCommand::ToRemoteProtocolJson();
63     j["HapTokenInfos"] = BaseRemoteCommand::ToHapTokenInfosJson(updateTokenInfo_);
64     return j.dump();
65 }
66 
Prepare()67 void UpdateRemoteHapTokenCommand::Prepare()
68 {
69     remoteProtocol_.statusCode = Constant::SUCCESS;
70     remoteProtocol_.message = Constant::COMMAND_RESULT_SUCCESS;
71     ACCESSTOKEN_LOG_DEBUG(LABEL, "End as: UpdateRemoteHapTokenCommand");
72 }
73 
Execute()74 void UpdateRemoteHapTokenCommand::Execute()
75 {
76     ACCESSTOKEN_LOG_INFO(LABEL, "Execute: start as: UpdateRemoteHapTokenCommand");
77 
78     remoteProtocol_.responseDeviceId = ConstantCommon::GetLocalDeviceId();
79     remoteProtocol_.responseVersion = Constant::DISTRIBUTED_ACCESS_TOKEN_SERVICE_VERSION;
80 
81     DeviceInfo devInfo;
82     bool result = DeviceInfoManager::GetInstance().GetDeviceInfo(remoteProtocol_.srcDeviceId,
83         DeviceIdType::UNKNOWN, devInfo);
84     if (!result) {
85         ACCESSTOKEN_LOG_INFO(LABEL, "UpdateRemoteHapTokenCommand: get remote uniqueDeviceId failed");
86         remoteProtocol_.statusCode = Constant::FAILURE_BUT_CAN_RETRY;
87         return;
88     }
89 
90     std::string uniqueDeviceId = devInfo.deviceId.uniqueDeviceId;
91     int ret = AccessTokenKit::SetRemoteHapTokenInfo(uniqueDeviceId, updateTokenInfo_);
92     if (ret != RET_SUCCESS) {
93         remoteProtocol_.statusCode = Constant::FAILURE_BUT_CAN_RETRY;
94         remoteProtocol_.message = Constant::COMMAND_RESULT_FAILED;
95     } else {
96         remoteProtocol_.statusCode = Constant::SUCCESS;
97         remoteProtocol_.message = Constant::COMMAND_RESULT_SUCCESS;
98     }
99 
100     ACCESSTOKEN_LOG_INFO(LABEL, "Execute: end as: UpdateRemoteHapTokenCommand");
101 }
102 
Finish()103 void UpdateRemoteHapTokenCommand::Finish()
104 {
105     remoteProtocol_.statusCode = Constant::SUCCESS;
106     ACCESSTOKEN_LOG_INFO(LABEL, "Finish: end as: DeleteUidPermissionCommand");
107 }
108 }  // namespace AccessToken
109 }  // namespace Security
110 }  // namespace OHOS
111 
112