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