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