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 "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_LABEL UserIam::Common::LABEL_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 IamHitraceHelper traceHelper("hdi Enroll");
54 ResultCode ret = hdi->Enroll(scheduleId_, tokenId, extraInfo, shared_from_this());
55 IAM_LOGI("%{public}s enroll result %{public}d", GetDescription(), ret);
56 return ret;
57 }
58
OnResultInner(ResultCode result,const std::vector<uint8_t> & extraInfo)59 void EnrollCommand::OnResultInner(ResultCode result, const std::vector<uint8_t> &extraInfo)
60 {
61 IAM_LOGI("%{public}s on result start", GetDescription());
62 UserIam::UserAuth::ReportTemplateChange(GetAuthType(),
63 TRACE_ADD_CREDENTIAL, "User Operation");
64 std::vector<uint8_t> nonConstExtraInfo(extraInfo.begin(), extraInfo.end());
65 auto authAttributes = Common::MakeShared<Attributes>();
66 IF_FALSE_LOGE_AND_RETURN(authAttributes != nullptr);
67 bool setResultCodeRet = authAttributes->SetUint32Value(Attributes::ATTR_RESULT_CODE, result);
68 IF_FALSE_LOGE_AND_RETURN(setResultCodeRet == true);
69 bool setAuthResultRet =
70 authAttributes->SetUint8ArrayValue(Attributes::ATTR_RESULT, nonConstExtraInfo);
71 IF_FALSE_LOGE_AND_RETURN(setAuthResultRet == true);
72 iamHitraceHelper_ = nullptr;
73 int32_t ret = MessengerFinish(scheduleId_, ALL_IN_ONE, result, authAttributes);
74 if (ret != USERAUTH_SUCCESS) {
75 IAM_LOGE("%{public}s call finish fail", GetDescription());
76 return;
77 }
78 IAM_LOGI("%{public}s call finish success result %{public}d", GetDescription(), result);
79 }
80
OnAcquireInfoInner(int32_t acquire,const std::vector<uint8_t> & extraInfo)81 void EnrollCommand::OnAcquireInfoInner(int32_t acquire, const std::vector<uint8_t> &extraInfo)
82 {
83 IAM_LOGI("%{public}s on acquire info start", GetDescription());
84
85 std::vector<uint8_t> nonConstExtraInfo(extraInfo.begin(), extraInfo.end());
86 auto msg = AuthMessage::As(nonConstExtraInfo);
87
88 IF_FALSE_LOGE_AND_RETURN(msg != nullptr);
89 int32_t ret = MessengerSendData(scheduleId_, transNum_, ALL_IN_ONE, SCHEDULER, msg);
90 ++transNum_;
91 if (ret != USERAUTH_SUCCESS) {
92 IAM_LOGE("%{public}s call SendData fail", GetDescription());
93 return;
94 }
95 IAM_LOGI("%{public}s call SendData success acquire %{public}d", GetDescription(), acquire);
96 }
97 } // namespace UserAuth
98 } // namespace UserIam
99 } // namespace OHOS
100