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 "sync_remote_native_token_command.h"
17
18 #include "accesstoken_kit.h"
19 #include "accesstoken_log.h"
20 #include "access_token_error.h"
21 #include "base_remote_command.h"
22 #include "constant_common.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 = {LOG_CORE,
30 SECURITY_DOMAIN_ACCESSTOKEN, "SyncRemoteNativeTokenCommand"};
31 }
32
SyncRemoteNativeTokenCommand(const std::string & srcDeviceId,const std::string & dstDeviceId)33 SyncRemoteNativeTokenCommand::SyncRemoteNativeTokenCommand(
34 const std::string &srcDeviceId, const std::string &dstDeviceId)
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
SyncRemoteNativeTokenCommand(const std::string & json)44 SyncRemoteNativeTokenCommand::SyncRemoteNativeTokenCommand(const std::string &json)
45 {
46 nlohmann::json jsonObject = nlohmann::json::parse(json, nullptr, false);
47 BaseRemoteCommand::FromRemoteProtocolJson(jsonObject);
48
49 if (jsonObject.find("NativeTokenInfos") != jsonObject.end() && jsonObject.at("NativeTokenInfos").is_array()) {
50 nlohmann::json nativeTokenListJson = jsonObject.at("NativeTokenInfos");
51 for (const auto& tokenJson : nativeTokenListJson) {
52 NativeTokenInfoForSync token;
53 BaseRemoteCommand::FromNativeTokenInfoJson(tokenJson, token);
54 nativeTokenInfo_.emplace_back(token);
55 }
56 }
57 }
58
ToJsonPayload()59 std::string SyncRemoteNativeTokenCommand::ToJsonPayload()
60 {
61 nlohmann::json j = BaseRemoteCommand::ToRemoteProtocolJson();
62 nlohmann::json nativeTokensJson;
63 for (const auto& token : nativeTokenInfo_) {
64 nlohmann::json tokenJson = BaseRemoteCommand::ToNativeTokenInfoJson(token);
65 nativeTokensJson.emplace_back(tokenJson);
66 }
67 j["NativeTokenInfos"] = nativeTokensJson;
68 return j.dump();
69 }
70
Prepare()71 void SyncRemoteNativeTokenCommand::Prepare()
72 {
73 remoteProtocol_.statusCode = Constant::SUCCESS;
74 remoteProtocol_.message = Constant::COMMAND_RESULT_SUCCESS;
75 ACCESSTOKEN_LOG_DEBUG(LABEL, "end as: SyncRemoteNativeTokenCommand");
76 }
77
Execute()78 void SyncRemoteNativeTokenCommand::Execute()
79 {
80 ACCESSTOKEN_LOG_INFO(LABEL, "execute: start as: SyncRemoteNativeTokenCommand");
81 remoteProtocol_.responseDeviceId = ConstantCommon::GetLocalDeviceId();
82 remoteProtocol_.responseVersion = Constant::DISTRIBUTED_ACCESS_TOKEN_SERVICE_VERSION;
83
84 int ret = AccessTokenKit::GetAllNativeTokenInfo(nativeTokenInfo_);
85 if (ret != RET_SUCCESS) {
86 remoteProtocol_.statusCode = Constant::FAILURE_BUT_CAN_RETRY;
87 remoteProtocol_.message = Constant::COMMAND_RESULT_FAILED;
88 } else {
89 remoteProtocol_.statusCode = Constant::SUCCESS;
90 remoteProtocol_.message = Constant::COMMAND_RESULT_SUCCESS;
91 }
92
93 ACCESSTOKEN_LOG_INFO(LABEL, "execute: end as: SyncRemoteNativeTokenCommand");
94 }
95
Finish()96 void SyncRemoteNativeTokenCommand::Finish()
97 {
98 if (remoteProtocol_.statusCode != Constant::SUCCESS) {
99 ACCESSTOKEN_LOG_ERROR(LABEL, "Finish: end as: SyncRemoteHapTokenCommand get remote result error.");
100 return;
101 }
102
103 DeviceInfo devInfo;
104 bool result = DeviceInfoManager::GetInstance().GetDeviceInfo(remoteProtocol_.dstDeviceId,
105 DeviceIdType::UNKNOWN, devInfo);
106 if (!result) {
107 ACCESSTOKEN_LOG_ERROR(LABEL, "SyncRemoteNativeTokenCommand: get remote uniqueDeviceId failed");
108 remoteProtocol_.statusCode = Constant::FAILURE_BUT_CAN_RETRY;
109 return;
110 }
111 int ret = AccessTokenKit::SetRemoteNativeTokenInfo(devInfo.deviceId.uniqueDeviceId, nativeTokenInfo_);
112 if (ret == RET_SUCCESS) {
113 remoteProtocol_.statusCode = Constant::SUCCESS;
114 } else {
115 remoteProtocol_.statusCode = Constant::FAILURE_BUT_CAN_RETRY;
116 }
117 ACCESSTOKEN_LOG_INFO(LABEL, "Finish: end as: SyncRemoteNativeTokenCommand ret %{public}d", ret);
118 }
119 } // namespace AccessToken
120 } // namespace Security
121 } // namespace OHOS
122
123