• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "constant_common.h"
22 #include "device_info.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, "DeleteRemoteTokenCommand"};
31 }
32 
DeleteRemoteTokenCommand(const std::string & srcDeviceId,const std::string & dstDeviceId,AccessTokenID deleteID)33 DeleteRemoteTokenCommand::DeleteRemoteTokenCommand(
34     const std::string &srcDeviceId, const std::string &dstDeviceId, AccessTokenID deleteID)
35     : deleteTokenId_(deleteID)
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 
DeleteRemoteTokenCommand(const std::string & json)45 DeleteRemoteTokenCommand::DeleteRemoteTokenCommand(const std::string& json)
46 {
47     deleteTokenId_ = 0;
48     nlohmann::json jsonObject = nlohmann::json::parse(json, nullptr, false);
49     BaseRemoteCommand::FromRemoteProtocolJson(jsonObject);
50 
51     if (jsonObject.find("tokenId") != jsonObject.end() && jsonObject.at("tokenId").is_number()) {
52         deleteTokenId_ = (AccessTokenID)jsonObject.at("tokenId").get<int>();
53     }
54 }
55 
ToJsonPayload()56 std::string DeleteRemoteTokenCommand::ToJsonPayload()
57 {
58     nlohmann::json j = BaseRemoteCommand::ToRemoteProtocolJson();
59     if (j.is_discarded()) {
60         ACCESSTOKEN_LOG_ERROR(LABEL, "j is invalid.");
61         return "";
62     }
63     j["tokenId"] = deleteTokenId_;
64     return j.dump();
65 }
66 
Prepare()67 void DeleteRemoteTokenCommand::Prepare()
68 {
69     remoteProtocol_.statusCode = Constant::SUCCESS;
70     remoteProtocol_.message = Constant::COMMAND_RESULT_SUCCESS;
71     ACCESSTOKEN_LOG_INFO(LABEL, "end as: DeleteRemoteTokenCommand");
72 }
73 
Execute()74 void DeleteRemoteTokenCommand::Execute()
75 {
76     ACCESSTOKEN_LOG_INFO(LABEL, "execute: start as: DeleteRemoteTokenCommand");
77     remoteProtocol_.responseDeviceId = ConstantCommon::GetLocalDeviceId();
78     remoteProtocol_.responseVersion = Constant::DISTRIBUTED_ACCESS_TOKEN_SERVICE_VERSION;
79 
80     DeviceInfo devInfo;
81     bool result = DeviceInfoManager::GetInstance().GetDeviceInfo(remoteProtocol_.srcDeviceId,
82         DeviceIdType::UNKNOWN, devInfo);
83     if (!result) {
84         ACCESSTOKEN_LOG_INFO(LABEL, "error: get remote uniqueDeviceId failed");
85         remoteProtocol_.statusCode = Constant::FAILURE_BUT_CAN_RETRY;
86         return;
87     }
88 
89     std::string uniqueDeviceId = devInfo.deviceId.uniqueDeviceId;
90     int ret = AccessTokenKit::DeleteRemoteToken(uniqueDeviceId, deleteTokenId_);
91     if (ret != RET_SUCCESS) {
92         remoteProtocol_.statusCode = Constant::FAILURE_BUT_CAN_RETRY;
93         remoteProtocol_.message = Constant::COMMAND_RESULT_FAILED;
94     } else {
95         remoteProtocol_.statusCode = Constant::SUCCESS;
96         remoteProtocol_.message = Constant::COMMAND_RESULT_SUCCESS;
97     }
98 
99     ACCESSTOKEN_LOG_INFO(LABEL, "execute: end as: DeleteRemoteTokenCommand");
100 }
101 
Finish()102 void DeleteRemoteTokenCommand::Finish()
103 {
104     remoteProtocol_.statusCode = Constant::SUCCESS;
105     ACCESSTOKEN_LOG_INFO(LABEL, "Finish: end as: DeleteUidPermissionCommand");
106 }
107 }  // namespace AccessToken
108 }  // namespace Security
109 }  // namespace OHOS
110 
111