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_common_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
DeleteRemoteTokenCommand(const std::string & srcDeviceId,const std::string & dstDeviceId,AccessTokenID deleteID)30 DeleteRemoteTokenCommand::DeleteRemoteTokenCommand(
31 const std::string &srcDeviceId, const std::string &dstDeviceId, AccessTokenID deleteID)
32 : deleteTokenId_(deleteID)
33 {
34 remoteProtocol_.commandName = COMMAND_NAME;
35 remoteProtocol_.uniqueId = COMMAND_NAME;
36 remoteProtocol_.srcDeviceId = srcDeviceId;
37 remoteProtocol_.dstDeviceId = dstDeviceId;
38 remoteProtocol_.responseVersion = Constant::DISTRIBUTED_ACCESS_TOKEN_SERVICE_VERSION;
39 remoteProtocol_.requestVersion = Constant::DISTRIBUTED_ACCESS_TOKEN_SERVICE_VERSION;
40 }
41
DeleteRemoteTokenCommand(const std::string & json)42 DeleteRemoteTokenCommand::DeleteRemoteTokenCommand(const std::string& json)
43 {
44 deleteTokenId_ = 0;
45 CJsonUnique jsonObject = CreateJsonFromString(json);
46 if (jsonObject == nullptr) {
47 LOGE(ATM_DOMAIN, ATM_TAG, "JsonObject is invalid.");
48 return;
49 }
50 BaseRemoteCommand::FromRemoteProtocolJson(jsonObject.get());
51 uint32_t tokenId;
52 if (GetUnsignedIntFromJson(jsonObject, "tokenId", tokenId)) {
53 deleteTokenId_ = (AccessTokenID)tokenId;
54 }
55 }
56
ToJsonPayload()57 std::string DeleteRemoteTokenCommand::ToJsonPayload()
58 {
59 CJsonUnique j = BaseRemoteCommand::ToRemoteProtocolJson();
60 if (j == nullptr) {
61 LOGE(ATM_DOMAIN, ATM_TAG, "J is invalid.");
62 return "";
63 }
64 AddUnsignedIntToJson(j, "tokenId", deleteTokenId_);
65 return PackJsonToString(j);
66 }
67
Prepare()68 void DeleteRemoteTokenCommand::Prepare()
69 {
70 remoteProtocol_.statusCode = Constant::SUCCESS;
71 remoteProtocol_.message = Constant::COMMAND_RESULT_SUCCESS;
72 LOGI(ATM_DOMAIN, ATM_TAG, "End as: DeleteRemoteTokenCommand");
73 }
74
Execute()75 void DeleteRemoteTokenCommand::Execute()
76 {
77 LOGI(ATM_DOMAIN, ATM_TAG, "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 LOGI(ATM_DOMAIN, ATM_TAG, "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 LOGI(ATM_DOMAIN, ATM_TAG, "Execute: end as: DeleteRemoteTokenCommand");
101 }
102
Finish()103 void DeleteRemoteTokenCommand::Finish()
104 {
105 remoteProtocol_.statusCode = Constant::SUCCESS;
106 LOGI(ATM_DOMAIN, ATM_TAG, "Finish: end as: DeleteUidPermissionCommand");
107 }
108 } // namespace AccessToken
109 } // namespace Security
110 } // namespace OHOS
111
112