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 "identify_command.h"
17 
18 #include "iam_check.h"
19 #include "iam_logger.h"
20 #include "iam_para2str.h"
21 #include "iam_ptr.h"
22 
23 #define LOG_TAG "USER_AUTH_EXECUTOR"
24 
25 namespace OHOS {
26 namespace UserIam {
27 namespace UserAuth {
IdentifyCommand(std::weak_ptr<Executor> executor,uint64_t scheduleId,const Attributes & attributes,std::shared_ptr<ExecutorMessenger> executorMessenger)28 IdentifyCommand::IdentifyCommand(std::weak_ptr<Executor> executor, uint64_t scheduleId,
29     const Attributes &attributes, std::shared_ptr<ExecutorMessenger> executorMessenger)
30     : AsyncCommandBase("IDENTIFY", scheduleId, executor, executorMessenger),
31       attributes_(Common::MakeShared<Attributes>(attributes.Serialize())),
32       iamHitraceHelper_(Common::MakeShared<IamHitraceHelper>("IdentifyCommand"))
33 {
34 }
35 
SendRequest()36 ResultCode IdentifyCommand::SendRequest()
37 {
38     IAM_LOGI("%{public}s send request start", GetDescription());
39     IF_FALSE_LOGE_AND_RETURN_VAL(attributes_ != nullptr, ResultCode::GENERAL_ERROR);
40 
41     auto hdi = GetExecutorHdi();
42     IF_FALSE_LOGE_AND_RETURN_VAL(hdi != nullptr, ResultCode::GENERAL_ERROR);
43 
44     uint32_t tokenId;
45     bool getTokenIdRet = attributes_->GetUint32Value(Attributes::ATTR_ACCESS_TOKEN_ID, tokenId);
46     IF_FALSE_LOGE_AND_RETURN_VAL(getTokenIdRet == true, ResultCode::GENERAL_ERROR);
47 
48     std::vector<uint8_t> extraInfo;
49     bool getExtraInfoRet = attributes_->GetUint8ArrayValue(Attributes::ATTR_EXTRA_INFO, extraInfo);
50     IF_FALSE_LOGE_AND_RETURN_VAL(getExtraInfoRet == true, ResultCode::GENERAL_ERROR);
51 
52     IamHitraceHelper traceHelper("hdi Identify");
53     ResultCode ret = hdi->Identify(scheduleId_, (IdentifyParam) { tokenId, extraInfo }, shared_from_this());
54     IAM_LOGI("%{public}s identify result %{public}d", GetDescription(), ret);
55     return ret;
56 }
57 
OnResultInner(ResultCode result,const std::vector<uint8_t> & extraInfo)58 void IdentifyCommand::OnResultInner(ResultCode result, const std::vector<uint8_t> &extraInfo)
59 {
60     IAM_LOGI("%{public}s on result start", GetDescription());
61 
62     std::vector<uint8_t> nonConstExtraInfo(extraInfo.begin(), extraInfo.end());
63     auto authAttributes = Common::MakeShared<Attributes>();
64     IF_FALSE_LOGE_AND_RETURN(authAttributes != nullptr);
65     bool setResultCodeRet = authAttributes->SetUint32Value(Attributes::ATTR_RESULT_CODE, result);
66     IF_FALSE_LOGE_AND_RETURN(setResultCodeRet == true);
67     bool setAuthResultRet =
68         authAttributes->SetUint8ArrayValue(Attributes::ATTR_RESULT, nonConstExtraInfo);
69     IF_FALSE_LOGE_AND_RETURN(setAuthResultRet == true);
70     iamHitraceHelper_ = nullptr;
71     int32_t ret = MessengerFinish(scheduleId_, result, authAttributes);
72     if (ret != USERAUTH_SUCCESS) {
73         IAM_LOGE("%{public}s call finish fail", GetDescription());
74         return;
75     }
76     IAM_LOGI("%{public}s call finish success result %{public}d", GetDescription(), result);
77 }
78 } // namespace UserAuth
79 } // namespace UserIam
80 } // namespace OHOS
81