• 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_impl.h"
16 
17 #include "credential_info_impl.h"
18 #include "event_listener_manager.h"
19 #include "hdi_wrapper.h"
20 #include "iam_hitrace_helper.h"
21 #include "iam_logger.h"
22 #include "iam_ptr.h"
23 #include "ipc_common.h"
24 #include "load_mode_handler.h"
25 #include "publish_event_adapter.h"
26 #include "resource_node_utils.h"
27 #include "schedule_node_helper.h"
28 #include "update_pin_param_impl.h"
29 #include "user_idm_database.h"
30 
31 #define LOG_TAG "USER_AUTH_SA"
32 
33 namespace OHOS {
34 namespace UserIam {
35 namespace UserAuth {
DeleteImpl(DeleteParam deletePara)36 DeleteImpl::DeleteImpl(DeleteParam deletePara) : deletePara_(deletePara)
37 {
38 }
39 
~DeleteImpl()40 DeleteImpl::~DeleteImpl()
41 {
42     Cancel();
43 }
44 
SetLatestError(int32_t error)45 void DeleteImpl::SetLatestError(int32_t error)
46 {
47     if (error != ResultCode::SUCCESS) {
48         latestError_ = error;
49     }
50 }
51 
GetLatestError() const52 int32_t DeleteImpl::GetLatestError() const
53 {
54     return latestError_;
55 }
56 
SetAccessTokenId(uint32_t tokenId)57 void DeleteImpl::SetAccessTokenId(uint32_t tokenId)
58 {
59     tokenId_ = tokenId;
60 }
61 
GetAccessTokenId() const62 uint32_t DeleteImpl::GetAccessTokenId() const
63 {
64     return tokenId_;
65 }
66 
GetUserId() const67 int32_t DeleteImpl::GetUserId() const
68 {
69     return deletePara_.userId;
70 }
71 
Start(std::vector<std::shared_ptr<ScheduleNode>> & scheduleList,std::shared_ptr<ScheduleNodeCallback> callback,bool & isCredentialDelete)72 bool DeleteImpl::Start(std::vector<std::shared_ptr<ScheduleNode>> &scheduleList,
73     std::shared_ptr<ScheduleNodeCallback> callback, bool &isCredentialDelete)
74 {
75     IAM_LOGE("UserId:%{public}d", deletePara_.userId);
76     auto hdi = HdiWrapper::GetHdiInstance();
77     if (!hdi) {
78         IAM_LOGE("bad hdi");
79         return false;
80     }
81 
82     HdiCredentialOperateResult hdiResult = {};
83     IamHitraceHelper traceHelper("hdi DeleteCredential");
84     int32_t ret = hdi->DeleteCredential(deletePara_.userId, deletePara_.credentialId, deletePara_.token,
85         hdiResult);
86     if (ret != HDF_SUCCESS) {
87         IAM_LOGE("failed to delete credential, error code : %{public}d", ret);
88         return false;
89     }
90 
91     if (hdiResult.operateType == HdiCredentialOperateType::CREDENTIAL_DELETE) {
92         return DeleteCredential(deletePara_.userId, hdiResult.credentialInfos, isCredentialDelete);
93     } else if (hdiResult.operateType == HdiCredentialOperateType::CREDENTIAL_ABANDON) {
94         return StartSchedule(deletePara_.userId, hdiResult.scheduleInfo, scheduleList, callback);
95     }
96 
97     return false;
98 }
99 
Update(const std::vector<uint8_t> & scheduleResult,std::shared_ptr<CredentialInfoInterface> & info)100 bool DeleteImpl::Update(const std::vector<uint8_t> &scheduleResult, std::shared_ptr<CredentialInfoInterface> &info)
101 {
102     auto hdi = HdiWrapper::GetHdiInstance();
103     if (!hdi) {
104         IAM_LOGE("bad hdi");
105         return false;
106     }
107 
108     std::vector<HdiCredentialInfo> credentialInfos;
109     auto result = hdi->UpdateAbandonResult(deletePara_.userId, scheduleResult, credentialInfos);
110     if (result != HDF_SUCCESS) {
111         IAM_LOGE("hdi UpdateAbandonResult failed, err is %{public}d, userId is %{public}d", result,
112             deletePara_.userId);
113         SetLatestError(result);
114         return false;
115     }
116 
117     if (!credentialInfos.empty()) {
118         info =  Common::MakeShared<CredentialInfoImpl>(deletePara_.userId, credentialInfos[0]);
119         if (info == nullptr) {
120             IAM_LOGE("bad alloc");
121             return false;
122         }
123     }
124     PublishCommonEvent(deletePara_.userId, deletePara_.credentialId, PIN);
125     return true;
126 }
127 
Cancel()128 bool DeleteImpl::Cancel()
129 {
130     return true;
131 }
132 
StartSchedule(int32_t userId,HdiScheduleInfo & info,std::vector<std::shared_ptr<ScheduleNode>> & scheduleList,std::shared_ptr<ScheduleNodeCallback> callback)133 bool DeleteImpl::StartSchedule(int32_t userId, HdiScheduleInfo &info,
134     std::vector<std::shared_ptr<ScheduleNode>> &scheduleList, std::shared_ptr<ScheduleNodeCallback> callback)
135 {
136     IAM_LOGI("start");
137     std::vector<HdiScheduleInfo> infos = {};
138     infos.emplace_back(info);
139 
140     ScheduleNodeHelper::NodeOptionalPara para;
141     para.tokenId = tokenId_;
142     para.userId = userId;
143 
144     if (!ScheduleNodeHelper::BuildFromHdi(infos, callback, scheduleList, para)) {
145         IAM_LOGE("BuildFromHdi failed");
146         return false;
147     }
148     if (scheduleList.size() == 0 || scheduleList[0] == nullptr) {
149         IAM_LOGE("Bad Parameter!");
150         return false;
151     }
152 
153     scheduleId_ = scheduleList[0]->GetScheduleId();
154     return true;
155 }
156 
DeleteCredential(int32_t userId,std::vector<HdiCredentialInfo> & credentialInfos,bool & isCredentialDelete)157 bool DeleteImpl::DeleteCredential(int32_t userId, std::vector<HdiCredentialInfo> &credentialInfos,
158     bool &isCredentialDelete)
159 {
160     IAM_LOGI("start");
161     std::vector<std::shared_ptr<CredentialInfoInterface>> list;
162     for (auto credentialInfo : credentialInfos) {
163         auto info = Common::MakeShared<CredentialInfoImpl>(userId, credentialInfo);
164         if (info == nullptr) {
165             IAM_LOGE("bad alloc");
166             return false;
167         }
168         list.push_back(info);
169     }
170 
171     int32_t ret = ResourceNodeUtils::NotifyExecutorToDeleteTemplates(list, "DeleteTemplate");
172     if (ret != SUCCESS) {
173         IAM_LOGE("NotifyExecutorToDeleteTemplates fail, ret:%{public}d", ret);
174         return false;
175     }
176     isCredentialDelete = true;
177     for (auto info : list) {
178         PublishCommonEvent(userId, info->GetCredentialId(), info->GetAuthType());
179     }
180     return true;
181 }
182 
PublishCommonEvent(int32_t userId,uint64_t credentialId,AuthType authType)183 void DeleteImpl::PublishCommonEvent(int32_t userId, uint64_t credentialId, AuthType authType)
184 {
185     std::vector<std::shared_ptr<CredentialInfoInterface>> credentialInfos;
186     int32_t ret = UserIdmDatabase::Instance().GetCredentialInfo(userId, authType, credentialInfos);
187     if (ret != SUCCESS) {
188         IAM_LOGE("get credential fail, ret:%{public}d, userId:%{public}d, authType:%{public}d", ret, userId, authType);
189         return;
190     }
191     PublishEventAdapter::GetInstance().PublishCredentialUpdatedEvent(userId, authType, credentialInfos.size());
192     PublishEventAdapter::GetInstance().PublishUpdatedEvent(userId, credentialId);
193     CredChangeEventInfo changeInfo = {deletePara_.callerName, deletePara_.callerType, 0, credentialId, false};
194     if (authType != PIN) {
195         CredChangeEventListenerManager::GetInstance().OnNotifyCredChangeEvent(
196             userId, authType, DEL_CRED, changeInfo);
197     }
198 }
199 } // namespace UserAuth
200 } // namespace UserIam
201 } // namespace OHOS