• 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 "framework_types.h"
19 #include "iam_check.h"
20 #include "iam_common_defines.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     std::vector<uint8_t> extraInfo;
48     bool getTemplateIdListRet =
49         attributes_->GetUint64ArrayValue(Attributes::ATTR_TEMPLATE_ID_LIST, templateIdList);
50     IF_FALSE_LOGE_AND_RETURN_VAL(getTemplateIdListRet == true, ResultCode::GENERAL_ERROR);
51     uint32_t tokenId = 0;
52     bool getTokenIdRet = attributes_->GetUint32Value(Attributes::ATTR_ACCESS_TOKEN_ID, tokenId);
53     IF_FALSE_LOGE_AND_RETURN_VAL(getTokenIdRet == true, ResultCode::GENERAL_ERROR);
54 
55     IamHitraceHelper traceHelper("hdi Authenticate");
56     ResultCode ret = hdi->Authenticate(scheduleId_, tokenId, templateIdList, extraInfo, shared_from_this());
57     IAM_LOGI("%{public}s authenticate result %{public}d", GetDescription(), ret);
58     return ret;
59 }
60 
OnResultInner(ResultCode result,const std::vector<uint8_t> & extraInfo)61 void AuthCommand::OnResultInner(ResultCode result, const std::vector<uint8_t> &extraInfo)
62 {
63     IAM_LOGI("%{public}s on result start", GetDescription());
64 
65     std::vector<uint8_t> nonConstExtraInfo(extraInfo.begin(), extraInfo.end());
66     auto authAttributes = Common::MakeShared<Attributes>();
67     IF_FALSE_LOGE_AND_RETURN(authAttributes != nullptr);
68     bool setResultCodeRet = authAttributes->SetUint32Value(Attributes::ATTR_RESULT_CODE, result);
69     IF_FALSE_LOGE_AND_RETURN(setResultCodeRet == true);
70     bool setAuthResultRet =
71         authAttributes->SetUint8ArrayValue(Attributes::ATTR_RESULT, nonConstExtraInfo);
72     IF_FALSE_LOGE_AND_RETURN(setAuthResultRet == true);
73     iamHitraceHelper_ = nullptr;
74     int32_t ret = MessengerFinish(scheduleId_, ALL_IN_ONE, result, authAttributes);
75     if (ret != USERAUTH_SUCCESS) {
76         IAM_LOGE("%{public}s call finish fail", GetDescription());
77         return;
78     }
79     IAM_LOGI("%{public}s call finish success result %{public}d", GetDescription(), result);
80 }
81 
OnAcquireInfoInner(int32_t acquire,const std::vector<uint8_t> & extraInfo)82 void AuthCommand::OnAcquireInfoInner(int32_t acquire, const std::vector<uint8_t> &extraInfo)
83 {
84     IAM_LOGI("%{public}s on acquire info start", GetDescription());
85 
86     std::vector<uint8_t> nonConstExtraInfo(extraInfo.begin(), extraInfo.end());
87     auto msg = AuthMessage::As(nonConstExtraInfo);
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