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 "delete_remote_token_command.h"
17
18 #include "access_token_error.h"
19 #include "accesstoken_kit.h"
20 #include "accesstoken_log.h"
21 #include "base_remote_command.h"
22 #include "constant_common.h"
23 #include "device_info.h"
24 #include "device_info_manager.h"
25
26 namespace OHOS {
27 namespace Security {
28 namespace AccessToken {
29 namespace {
30 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
31 LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "DeleteRemoteTokenCommand"};
32 }
33
DeleteRemoteTokenCommand(const std::string & srcDeviceId,const std::string & dstDeviceId,AccessTokenID deleteID)34 DeleteRemoteTokenCommand::DeleteRemoteTokenCommand(
35 const std::string &srcDeviceId, const std::string &dstDeviceId, AccessTokenID deleteID)
36 : deleteTokenId_(deleteID)
37 {
38 remoteProtocol_.commandName = COMMAND_NAME;
39 remoteProtocol_.uniqueId = COMMAND_NAME;
40 remoteProtocol_.srcDeviceId = srcDeviceId;
41 remoteProtocol_.dstDeviceId = dstDeviceId;
42 remoteProtocol_.responseVersion = Constant::DISTRIBUTED_ACCESS_TOKEN_SERVICE_VERSION;
43 remoteProtocol_.requestVersion = Constant::DISTRIBUTED_ACCESS_TOKEN_SERVICE_VERSION;
44 }
45
DeleteRemoteTokenCommand(const std::string & json)46 DeleteRemoteTokenCommand::DeleteRemoteTokenCommand(const std::string& json)
47 {
48 deleteTokenId_ = 0;
49 nlohmann::json jsonObject = nlohmann::json::parse(json, nullptr, false);
50 BaseRemoteCommand::FromRemoteProtocolJson(jsonObject);
51
52 if (jsonObject.find("tokenId") != jsonObject.end() && jsonObject.at("tokenId").is_number()) {
53 deleteTokenId_ = (AccessTokenID)jsonObject.at("tokenId").get<int>();
54 }
55 }
56
ToJsonPayload()57 std::string DeleteRemoteTokenCommand::ToJsonPayload()
58 {
59 nlohmann::json j = BaseRemoteCommand::ToRemoteProtocolJson();
60 if (j.is_discarded()) {
61 ACCESSTOKEN_LOG_ERROR(LABEL, "j is invalid.");
62 return "";
63 }
64 j["tokenId"] = deleteTokenId_;
65 return j.dump();
66 }
67
Prepare()68 void DeleteRemoteTokenCommand::Prepare()
69 {
70 remoteProtocol_.statusCode = Constant::SUCCESS;
71 remoteProtocol_.message = Constant::COMMAND_RESULT_SUCCESS;
72 ACCESSTOKEN_LOG_INFO(LABEL, "end as: DeleteRemoteTokenCommand");
73 }
74
Execute()75 void DeleteRemoteTokenCommand::Execute()
76 {
77 ACCESSTOKEN_LOG_INFO(LABEL, "execute: start as: DeleteRemoteTokenCommand");
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, "error: 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::DeleteRemoteToken(uniqueDeviceId, deleteTokenId_);
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: DeleteRemoteTokenCommand");
101 }
102
Finish()103 void DeleteRemoteTokenCommand::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