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 "enroll_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 {
EnrollCommand(std::weak_ptr<Executor> executor,uint64_t scheduleId,const Attributes & attributes,std::shared_ptr<ExecutorMessenger> executorMessenger)32 EnrollCommand::EnrollCommand(std::weak_ptr<Executor> executor, uint64_t scheduleId,
33 const Attributes &attributes, std::shared_ptr<ExecutorMessenger> executorMessenger)
34 : AsyncCommandBase("ENROLL", scheduleId, executor, executorMessenger),
35 attributes_(Common::MakeShared<Attributes>(attributes.Serialize())),
36 iamHitraceHelper_(Common::MakeShared<IamHitraceHelper>("EnrollCommand"))
37 {
38 }
39
SendRequest()40 ResultCode EnrollCommand::SendRequest()
41 {
42 IAM_LOGI("%{public}s send request start", GetDescription());
43 IF_FALSE_LOGE_AND_RETURN_VAL(attributes_ != nullptr, ResultCode::GENERAL_ERROR);
44
45 auto hdi = GetExecutorHdi();
46 IF_FALSE_LOGE_AND_RETURN_VAL(hdi != nullptr, ResultCode::GENERAL_ERROR);
47
48 uint32_t tokenId = 0;
49 bool getTokenIdRet = attributes_->GetUint32Value(Attributes::ATTR_ACCESS_TOKEN_ID, tokenId);
50 IF_FALSE_LOGE_AND_RETURN_VAL(getTokenIdRet == true, ResultCode::GENERAL_ERROR);
51
52 std::vector<uint8_t> extraInfo;
53 bool getExtraInfoRet = attributes_->GetUint8ArrayValue(Attributes::ATTR_EXTRA_INFO, extraInfo);
54 IF_FALSE_LOGE_AND_RETURN_VAL(getExtraInfoRet == true, ResultCode::GENERAL_ERROR);
55
56 int32_t userId;
57 bool getUserId = attributes_->GetInt32Value(Attributes::ATTR_USER_ID, userId);
58 IF_FALSE_LOGE_AND_RETURN_VAL(getUserId == true, ResultCode::GENERAL_ERROR);
59
60 IamHitraceHelper traceHelper("hdi Enroll");
61 ResultCode ret = hdi->Enroll(scheduleId_, (EnrollParam) { tokenId, extraInfo, userId}, shared_from_this());
62 IAM_LOGI("%{public}s enroll result %{public}d", GetDescription(), ret);
63 return ret;
64 }
65
OnResultInner(ResultCode result,const std::vector<uint8_t> & extraInfo)66 void EnrollCommand::OnResultInner(ResultCode result, const std::vector<uint8_t> &extraInfo)
67 {
68 IAM_LOGI("%{public}s on result start", GetDescription());
69 TemplateChangeTrace info = {};
70 info.scheduleId = scheduleId_;
71 info.executorType = GetAuthType();
72 info.changeType = TRACE_ADD_CREDENTIAL;
73 info.reason = "AddTemplate";
74 UserIam::UserAuth::ReportSecurityTemplateChange(info);
75 std::vector<uint8_t> nonConstExtraInfo(extraInfo.begin(), extraInfo.end());
76 auto authAttributes = Common::MakeShared<Attributes>();
77 IF_FALSE_LOGE_AND_RETURN(authAttributes != nullptr);
78 bool setResultCodeRet = authAttributes->SetUint32Value(Attributes::ATTR_RESULT_CODE, result);
79 IF_FALSE_LOGE_AND_RETURN(setResultCodeRet == true);
80 bool setAuthResultRet =
81 authAttributes->SetUint8ArrayValue(Attributes::ATTR_RESULT, nonConstExtraInfo);
82 IF_FALSE_LOGE_AND_RETURN(setAuthResultRet == true);
83 iamHitraceHelper_ = nullptr;
84 int32_t ret = MessengerFinish(scheduleId_, result, authAttributes);
85 if (ret != USERAUTH_SUCCESS) {
86 IAM_LOGE("%{public}s call finish fail", GetDescription());
87 return;
88 }
89 IAM_LOGI("%{public}s call finish success result %{public}d", GetDescription(), result);
90 }
91 } // namespace UserAuth
92 } // namespace UserIam
93 } // namespace OHOS
94