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