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_TAG "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 IAM_LOGI("%{public}s auth message len %{public}zu", GetDescription(), extraInfo.size());
59
60 int32_t authIntent;
61 bool getAuthIntent = attributes_->GetInt32Value(Attributes::ATTR_AUTH_INTENTION, authIntent);
62 IF_FALSE_LOGE_AND_RETURN_VAL(getAuthIntent == true, ResultCode::GENERAL_ERROR);
63
64 int32_t userId;
65 bool getUserId = attributes_->GetInt32Value(Attributes::ATTR_USER_ID, userId);
66 IF_FALSE_LOGE_AND_RETURN_VAL(getUserId == true, ResultCode::GENERAL_ERROR);
67
68 IamHitraceHelper traceHelper("hdi Authenticate");
69 ResultCode ret = hdi->Authenticate(scheduleId_,
70 (AuthenticateParam) { tokenId, templateIdList, extraInfo, endAfterFirstFail, authIntent, userId},
71 shared_from_this());
72 IAM_LOGI("%{public}s authenticate result %{public}d", GetDescription(), ret);
73 return ret;
74 }
75
OnResultInner(ResultCode result,const std::vector<uint8_t> & extraInfo)76 void AuthCommand::OnResultInner(ResultCode result, const std::vector<uint8_t> &extraInfo)
77 {
78 IAM_LOGI("%{public}s on result start", GetDescription());
79
80 std::vector<uint8_t> nonConstExtraInfo(extraInfo.begin(), extraInfo.end());
81 auto authAttributes = Common::MakeShared<Attributes>();
82 IF_FALSE_LOGE_AND_RETURN(authAttributes != nullptr);
83 bool setResultCodeRet = authAttributes->SetUint32Value(Attributes::ATTR_RESULT_CODE, result);
84 IF_FALSE_LOGE_AND_RETURN(setResultCodeRet == true);
85 bool setAuthResultRet =
86 authAttributes->SetUint8ArrayValue(Attributes::ATTR_RESULT, nonConstExtraInfo);
87 IF_FALSE_LOGE_AND_RETURN(setAuthResultRet == true);
88 iamHitraceHelper_ = nullptr;
89 int32_t ret = MessengerFinish(scheduleId_, result, authAttributes);
90 if (ret != USERAUTH_SUCCESS) {
91 IAM_LOGE("%{public}s call finish fail", GetDescription());
92 return;
93 }
94 IAM_LOGI("%{public}s call finish success result %{public}d", GetDescription(), result);
95 }
96 } // namespace UserAuth
97 } // namespace UserIam
98 } // namespace OHOS
99