• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "auth_command.h"
17 
18 #include "iam_check.h"
19 #include "iam_common_defines.h"
20 #include "iam_executor_framework_types.h"
21 #include "iam_logger.h"
22 #include "iam_para2str.h"
23 #include "iam_ptr.h"
24 
25 #define LOG_LABEL UserIam::Common::LABEL_USER_AUTH_EXECUTOR
26 
27 namespace OHOS {
28 namespace UserIam {
29 namespace UserAuth {
AuthCommand(std::weak_ptr<Executor> executor,uint64_t scheduleId,const Attributes & attributes,std::shared_ptr<ExecutorMessenger> executorMessenger)30 AuthCommand::AuthCommand(std::weak_ptr<Executor> executor, uint64_t scheduleId,
31     const Attributes &attributes, std::shared_ptr<ExecutorMessenger> executorMessenger)
32     : AsyncCommandBase("AUTH", scheduleId, executor, executorMessenger),
33       attributes_(Common::MakeShared<Attributes>(attributes.Serialize())),
34       iamHitraceHelper_(Common::MakeShared<IamHitraceHelper>("AuthCommand"))
35 {
36 }
37 
SendRequest()38 ResultCode AuthCommand::SendRequest()
39 {
40     IAM_LOGI("%{public}s send request start", GetDescription());
41     IF_FALSE_LOGE_AND_RETURN_VAL(attributes_ != nullptr, ResultCode::GENERAL_ERROR);
42 
43     auto hdi = GetExecutorHdi();
44     IF_FALSE_LOGE_AND_RETURN_VAL(hdi != nullptr, ResultCode::GENERAL_ERROR);
45 
46     std::vector<uint64_t> templateIdList;
47     bool getTemplateIdListRet = attributes_->GetUint64ArrayValue(Attributes::ATTR_TEMPLATE_ID_LIST, templateIdList);
48     IF_FALSE_LOGE_AND_RETURN_VAL(getTemplateIdListRet == true, ResultCode::GENERAL_ERROR);
49     uint32_t tokenId = 0;
50     bool getTokenIdRet = attributes_->GetUint32Value(Attributes::ATTR_ACCESS_TOKEN_ID, tokenId);
51     IF_FALSE_LOGE_AND_RETURN_VAL(getTokenIdRet == true, ResultCode::GENERAL_ERROR);
52     bool endAfterFirstFail;
53     bool getEndAfterFirstFailRet = attributes_->GetBoolValue(Attributes::ATTR_END_AFTER_FIRST_FAIL, endAfterFirstFail);
54     IF_FALSE_LOGE_AND_RETURN_VAL(getEndAfterFirstFailRet == true, ResultCode::GENERAL_ERROR);
55     std::vector<uint8_t> extraInfo;
56     bool getExtraInfoRet = attributes_->GetUint8ArrayValue(Attributes::ATTR_EXTRA_INFO, extraInfo);
57     IF_FALSE_LOGE_AND_RETURN_VAL(getExtraInfoRet == true, ResultCode::GENERAL_ERROR);
58 
59     IamHitraceHelper traceHelper("hdi Authenticate");
60     ResultCode ret = hdi->Authenticate(scheduleId_,
61         (AuthenticateParam) { tokenId, templateIdList, extraInfo, endAfterFirstFail }, shared_from_this());
62     IAM_LOGI("%{public}s authenticate result %{public}d", GetDescription(), ret);
63     return ret;
64 }
65 
OnResultInner(ResultCode result,const std::vector<uint8_t> & extraInfo)66 void AuthCommand::OnResultInner(ResultCode result, const std::vector<uint8_t> &extraInfo)
67 {
68     IAM_LOGI("%{public}s on result start", GetDescription());
69 
70     std::vector<uint8_t> nonConstExtraInfo(extraInfo.begin(), extraInfo.end());
71     auto authAttributes = Common::MakeShared<Attributes>();
72     IF_FALSE_LOGE_AND_RETURN(authAttributes != nullptr);
73     bool setResultCodeRet = authAttributes->SetUint32Value(Attributes::ATTR_RESULT_CODE, result);
74     IF_FALSE_LOGE_AND_RETURN(setResultCodeRet == true);
75     bool setAuthResultRet =
76         authAttributes->SetUint8ArrayValue(Attributes::ATTR_RESULT, nonConstExtraInfo);
77     IF_FALSE_LOGE_AND_RETURN(setAuthResultRet == true);
78     iamHitraceHelper_ = nullptr;
79     int32_t ret = MessengerFinish(scheduleId_, ALL_IN_ONE, result, authAttributes);
80     if (ret != USERAUTH_SUCCESS) {
81         IAM_LOGE("%{public}s call finish fail", GetDescription());
82         return;
83     }
84     IAM_LOGI("%{public}s call finish success result %{public}d", GetDescription(), result);
85 }
86 
OnAcquireInfoInner(int32_t acquire,const std::vector<uint8_t> & extraInfo)87 void AuthCommand::OnAcquireInfoInner(int32_t acquire, const std::vector<uint8_t> &extraInfo)
88 {
89     IAM_LOGI("%{public}s on acquire info start", GetDescription());
90 
91     Attributes attr;
92     bool setAcquireRet = attr.SetInt32Value(Attributes::ATTR_TIP_INFO, acquire);
93     IF_FALSE_LOGE_AND_RETURN(setAcquireRet);
94     bool setExtraInfoRet = attr.SetUint8ArrayValue(Attributes::ATTR_EXTRA_INFO, extraInfo);
95     IF_FALSE_LOGE_AND_RETURN(setExtraInfoRet);
96 
97     auto msg = AuthMessage::As(attr.Serialize());
98     IF_FALSE_LOGE_AND_RETURN(msg != nullptr);
99     int32_t ret = MessengerSendData(scheduleId_, transNum_, ALL_IN_ONE, SCHEDULER, msg);
100     ++transNum_;
101     if (ret != USERAUTH_SUCCESS) {
102         IAM_LOGE("%{public}s call SendData fail", GetDescription());
103         return;
104     }
105     IAM_LOGI("%{public}s call SendData success acquire %{public}d", GetDescription(), acquire);
106 }
107 } // namespace UserAuth
108 } // namespace UserIam
109 } // namespace OHOS
110