1 /*
2 * Copyright (c) 2025 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 "abandon_command.h"
17
18 #include "iam_executor_framework_types.h"
19 #include "iam_check.h"
20 #include "iam_logger.h"
21 #include "iam_mem.h"
22 #include "iam_para2str.h"
23 #include "iam_ptr.h"
24 #include "iam_defines.h"
25 #include "hisysevent_adapter.h"
26
27 #define LOG_TAG "USER_AUTH_EXECUTOR"
28
29 namespace OHOS {
30 namespace UserIam {
31 namespace UserAuth {
AbandonCommand(std::weak_ptr<Executor> executor,uint64_t scheduleId,const Attributes & attributes,std::shared_ptr<ExecutorMessenger> executorMessenger)32 AbandonCommand::AbandonCommand(std::weak_ptr<Executor> executor, uint64_t scheduleId,
33 const Attributes &attributes, std::shared_ptr<ExecutorMessenger> executorMessenger)
34 : AsyncCommandBase("Abandon", scheduleId, executor, executorMessenger),
35 attributes_(Common::MakeShared<Attributes>(attributes.Serialize()))
36 {
37 }
38
SendRequest()39 ResultCode AbandonCommand::SendRequest()
40 {
41 IAM_LOGI("%{public}s send request start", GetDescription());
42 IF_FALSE_LOGE_AND_RETURN_VAL(attributes_ != nullptr, ResultCode::GENERAL_ERROR);
43
44 auto hdi = GetExecutorHdi();
45 IF_FALSE_LOGE_AND_RETURN_VAL(hdi != nullptr, ResultCode::GENERAL_ERROR);
46
47 uint32_t tokenId = 0;
48 bool getTokenIdRet = attributes_->GetUint32Value(Attributes::ATTR_ACCESS_TOKEN_ID, tokenId);
49 IF_FALSE_LOGE_AND_RETURN_VAL(getTokenIdRet, ResultCode::GENERAL_ERROR);
50
51 std::vector<uint8_t> extraInfo;
52 bool getExtraInfoRet = attributes_->GetUint8ArrayValue(Attributes::ATTR_EXTRA_INFO, extraInfo);
53 IF_FALSE_LOGE_AND_RETURN_VAL(getExtraInfoRet, ResultCode::GENERAL_ERROR);
54
55 int32_t userId;
56 bool getUserId = attributes_->GetInt32Value(Attributes::ATTR_USER_ID, userId);
57 IF_FALSE_LOGE_AND_RETURN_VAL(getUserId, ResultCode::GENERAL_ERROR);
58
59 std::vector<uint64_t> templateIds;
60 bool getTempalteIds = attributes_->GetUint64ArrayValue(Attributes::ATTR_TEMPLATE_ID_LIST, templateIds);
61 IF_FALSE_LOGE_AND_RETURN_VAL(getTempalteIds, ResultCode::GENERAL_ERROR);
62 IF_FALSE_LOGE_AND_RETURN_VAL(templateIds.size() != 0, ResultCode::GENERAL_ERROR);
63
64 ResultCode ret = hdi->Abandon(scheduleId_, (DeleteParam) { tokenId, userId, templateIds[0], extraInfo},
65 shared_from_this());
66 IAM_LOGI("%{public}s abandon result %{public}d", GetDescription(), ret);
67 return ret;
68 }
69
OnResultInner(ResultCode result,const std::vector<uint8_t> & extraInfo)70 void AbandonCommand::OnResultInner(ResultCode result, const std::vector<uint8_t> &extraInfo)
71 {
72 IAM_LOGI("%{public}s on result start", GetDescription());
73 std::vector<uint8_t> nonConstExtraInfo(extraInfo.begin(), extraInfo.end());
74 auto authAttributes = Common::MakeShared<Attributes>();
75 IF_FALSE_LOGE_AND_RETURN(authAttributes != nullptr);
76 bool setResultCodeRet = authAttributes->SetUint32Value(Attributes::ATTR_RESULT_CODE, result);
77 IF_FALSE_LOGE_AND_RETURN(setResultCodeRet == true);
78 bool setAuthResultRet =
79 authAttributes->SetUint8ArrayValue(Attributes::ATTR_RESULT, nonConstExtraInfo);
80 IF_FALSE_LOGE_AND_RETURN(setAuthResultRet == true);
81 int32_t ret = MessengerFinish(scheduleId_, result, authAttributes);
82 if (ret != USERAUTH_SUCCESS) {
83 IAM_LOGE("%{public}s call finish fail", GetDescription());
84 return;
85 }
86 IAM_LOGI("%{public}s call finish success result %{public}d", GetDescription(), result);
87 }
88 } // namespace UserAuth
89 } // namespace UserIam
90 } // namespace OHOS
91