• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "delete_context.h"
16 
17 #include "hisysevent_adapter.h"
18 #include "iam_check.h"
19 #include "iam_logger.h"
20 #include "iam_para2str.h"
21 #include "resource_node_utils.h"
22 #include "schedule_node.h"
23 #include "schedule_node_callback.h"
24 
25 #define LOG_TAG "USER_AUTH_SA"
26 
27 namespace OHOS {
28 namespace UserIam {
29 namespace UserAuth {
DeleteContext(uint64_t contextId,std::shared_ptr<Deletion> deletion,std::shared_ptr<ContextCallback> callback)30 DeleteContext::DeleteContext(uint64_t contextId, std::shared_ptr<Deletion> deletion,
31     std::shared_ptr<ContextCallback> callback)
32     : BaseContext("Delete", contextId, callback, false), deletion_(deletion)
33 {
34 }
35 
GetContextType() const36 ContextType DeleteContext::GetContextType() const
37 {
38     return CONTEXT_DELETE;
39 }
40 
GetTokenId() const41 uint32_t DeleteContext::GetTokenId() const
42 {
43     IF_FALSE_LOGE_AND_RETURN_VAL(deletion_ != nullptr, 0);
44     return deletion_->GetAccessTokenId();
45 }
46 
GetUserId() const47 int32_t DeleteContext::GetUserId() const
48 {
49     IF_FALSE_LOGE_AND_RETURN_VAL(deletion_ != nullptr, INVALID_USER_ID);
50     return deletion_->GetUserId();
51 }
52 
OnStart()53 bool DeleteContext::OnStart()
54 {
55     IAM_LOGI("%{public}s start", GetDescription());
56     IF_FALSE_LOGE_AND_RETURN_VAL(deletion_ != nullptr, false);
57     bool isCredentilaDelete = false;
58     bool startRet = deletion_->Start(scheduleList_, shared_from_this(), isCredentilaDelete);
59     if (!startRet) {
60         IAM_LOGE("%{public}s delete start fail", GetDescription());
61         SetLatestError(deletion_->GetLatestError());
62         return startRet;
63     }
64 
65     if (isCredentilaDelete) {
66         InvokeResultCallback(ResultCode::SUCCESS);
67     } else {
68         IF_FALSE_LOGE_AND_RETURN_VAL(scheduleList_.size() == 1, false);
69         IF_FALSE_LOGE_AND_RETURN_VAL(scheduleList_[0] != nullptr, false);
70         bool startScheduleRet = scheduleList_[0]->StartSchedule();
71         IF_FALSE_LOGE_AND_RETURN_VAL(startScheduleRet, false);
72         IAM_LOGI("%{public}s Schedule:%{public}s Type:%{public}d success", GetDescription(),
73             GET_MASKED_STRING(scheduleList_[0]->GetScheduleId()).c_str(), scheduleList_[0]->GetAuthType());
74     }
75     return true;
76 }
77 
OnResult(int32_t resultCode,const std::shared_ptr<Attributes> & scheduleResultAttr)78 void DeleteContext::OnResult(int32_t resultCode, const std::shared_ptr<Attributes> &scheduleResultAttr)
79 {
80     IAM_LOGI("%{public}s receive result code %{public}d", GetDescription(), resultCode);
81     bool updateRet = UpdateScheduleResult(scheduleResultAttr);
82     if (!updateRet) {
83         IAM_LOGE("%{public}s UpdateScheduleResult fail", GetDescription());
84         if (resultCode == SUCCESS) {
85             resultCode = GetLatestError();
86         }
87     }
88     InvokeResultCallback(resultCode);
89     IAM_LOGI("%{public}s on result %{public}d finish", GetDescription(), resultCode);
90 }
91 
OnStop()92 bool DeleteContext::OnStop()
93 {
94     IAM_LOGI("%{public}s start", GetDescription());
95     if (scheduleList_.size() == 1 && scheduleList_[0] != nullptr) {
96         scheduleList_[0]->StopSchedule();
97     }
98 
99     IF_FALSE_LOGE_AND_RETURN_VAL(deletion_ != nullptr, false);
100     bool cancelRet = deletion_->Cancel();
101     if (!cancelRet) {
102         IAM_LOGE("%{public}s delete stop fail", GetDescription());
103         SetLatestError(deletion_->GetLatestError());
104         return cancelRet;
105     }
106     return true;
107 }
108 
UpdateScheduleResult(const std::shared_ptr<Attributes> & scheduleResultAttr)109 bool DeleteContext::UpdateScheduleResult(const std::shared_ptr<Attributes> &scheduleResultAttr)
110 {
111     IAM_LOGI("%{public}s start", GetDescription());
112     IF_FALSE_LOGE_AND_RETURN_VAL(deletion_ != nullptr, false);
113     IF_FALSE_LOGE_AND_RETURN_VAL(scheduleResultAttr != nullptr, false);
114     std::vector<uint8_t> scheduleResult;
115     bool getResultCodeRet = scheduleResultAttr->GetUint8ArrayValue(Attributes::ATTR_RESULT, scheduleResult);
116     IF_FALSE_LOGE_AND_RETURN_VAL(getResultCodeRet, false);
117     std::shared_ptr<CredentialInfoInterface> infoToDel;
118     bool updateRet = deletion_->Update(scheduleResult, infoToDel);
119     if (!updateRet) {
120         IAM_LOGE("%{public}s delete update fail", GetDescription());
121         SetLatestError(deletion_->GetLatestError());
122         return updateRet;
123     }
124     if (infoToDel == nullptr) {
125         IAM_LOGI("no credential to delete");
126     } else {
127         std::vector<std::shared_ptr<CredentialInfoInterface>> credInfos = {infoToDel};
128         int32_t ret = ResourceNodeUtils::NotifyExecutorToDeleteTemplates(credInfos, "DeleteForUpdate");
129         if (ret != SUCCESS) {
130             IAM_LOGE("failed to notify executor delete template, error code : %{public}d", ret);
131         }
132     }
133 
134     return true;
135 }
136 
InvokeResultCallback(int32_t resultCode) const137 void DeleteContext::InvokeResultCallback(int32_t resultCode) const
138 {
139     IAM_LOGI("%{public}s start", GetDescription());
140     IF_FALSE_LOGE_AND_RETURN(callback_ != nullptr);
141     Attributes finalResult;
142     callback_->OnResult(resultCode, finalResult);
143     IAM_LOGI("%{public}s invoke result callback success", GetDescription());
144 }
145 } // namespace UserAuth
146 } // namespace UserIam
147 } // namespace OHOS
148