• 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 "update_remote_hap_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, "UpdateRemoteHapTokenCommand"};
30 }
31 
UpdateRemoteHapTokenCommand(const std::string & srcDeviceId,const std::string & dstDeviceId,const HapTokenInfoForSync & tokenInfo)32 UpdateRemoteHapTokenCommand::UpdateRemoteHapTokenCommand(
33     const std::string &srcDeviceId, const std::string &dstDeviceId, const HapTokenInfoForSync& tokenInfo)
34     : updateTokenInfo_(tokenInfo)
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 
UpdateRemoteHapTokenCommand(const std::string & json)44 UpdateRemoteHapTokenCommand::UpdateRemoteHapTokenCommand(const std::string &json)
45 {
46     nlohmann::json jsonObject = nlohmann::json::parse(json, nullptr, false);
47     BaseRemoteCommand::FromRemoteProtocolJson(jsonObject);
48 
49     if (jsonObject.find("HapTokenInfos") != jsonObject.end()) {
50         nlohmann::json hapTokenJson = jsonObject.at("HapTokenInfos").get<nlohmann::json>();
51         BaseRemoteCommand::FromHapTokenInfoJson(hapTokenJson, updateTokenInfo_);
52     }
53 }
54 
ToJsonPayload()55 std::string UpdateRemoteHapTokenCommand::ToJsonPayload()
56 {
57     nlohmann::json j = BaseRemoteCommand::ToRemoteProtocolJson();
58     j["HapTokenInfos"] = BaseRemoteCommand::ToHapTokenInfosJson(updateTokenInfo_);
59     return j.dump();
60 }
61 
Prepare()62 void UpdateRemoteHapTokenCommand::Prepare()
63 {
64     remoteProtocol_.statusCode = Constant::SUCCESS;
65     remoteProtocol_.message = Constant::COMMAND_RESULT_SUCCESS;
66     ACCESSTOKEN_LOG_DEBUG(LABEL, "end as: UpdateRemoteHapTokenCommand");
67 }
68 
Execute()69 void UpdateRemoteHapTokenCommand::Execute()
70 {
71     ACCESSTOKEN_LOG_INFO(LABEL, "execute: start as: UpdateRemoteHapTokenCommand");
72 
73     remoteProtocol_.responseDeviceId = ConstantCommon::GetLocalDeviceId();
74     remoteProtocol_.responseVersion = Constant::DISTRIBUTED_ACCESS_TOKEN_SERVICE_VERSION;
75 
76     DeviceInfo devInfo;
77     bool result = DeviceInfoManager::GetInstance().GetDeviceInfo(remoteProtocol_.srcDeviceId,
78         DeviceIdType::UNKNOWN, devInfo);
79     if (!result) {
80         ACCESSTOKEN_LOG_INFO(LABEL, "UpdateRemoteHapTokenCommand: get remote uniqueDeviceId failed");
81         remoteProtocol_.statusCode = Constant::FAILURE_BUT_CAN_RETRY;
82         return;
83     }
84 
85     std::string uniqueDeviceId = devInfo.deviceId.uniqueDeviceId;
86     int ret = AccessTokenKit::SetRemoteHapTokenInfo(uniqueDeviceId, updateTokenInfo_);
87     if (ret != RET_SUCCESS) {
88         remoteProtocol_.statusCode = Constant::FAILURE_BUT_CAN_RETRY;
89         remoteProtocol_.message = Constant::COMMAND_RESULT_FAILED;
90     } else {
91         remoteProtocol_.statusCode = Constant::SUCCESS;
92         remoteProtocol_.message = Constant::COMMAND_RESULT_SUCCESS;
93     }
94 
95     ACCESSTOKEN_LOG_INFO(LABEL, "execute: end as: UpdateRemoteHapTokenCommand");
96 }
97 
Finish()98 void UpdateRemoteHapTokenCommand::Finish()
99 {
100     remoteProtocol_.statusCode = Constant::SUCCESS;
101     ACCESSTOKEN_LOG_INFO(LABEL, "Finish: end as: DeleteUidPermissionCommand");
102 }
103 }  // namespace AccessToken
104 }  // namespace Security
105 }  // namespace OHOS
106 
107