• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 #include "identify_context.h"
16 
17 #include "iam_check.h"
18 #include "iam_logger.h"
19 #include "iam_ptr.h"
20 #include "schedule_node.h"
21 #include "schedule_node_callback.h"
22 
23 #define LOG_LABEL UserIam::Common::LABEL_USER_AUTH_SA
24 namespace OHOS {
25 namespace UserIam {
26 namespace UserAuth {
IdentifyContext(uint64_t contextId,std::shared_ptr<Identification> identify,std::shared_ptr<ContextCallback> callback)27 IdentifyContext::IdentifyContext(uint64_t contextId, std::shared_ptr<Identification> identify,
28     std::shared_ptr<ContextCallback> callback)
29     : BaseContext("Identify", contextId, callback),
30       identify_(identify)
31 {
32 }
33 
GetContextType() const34 ContextType IdentifyContext::GetContextType() const
35 {
36     return CONTEXT_IDENTIFY;
37 }
38 
GetTokenId() const39 uint32_t IdentifyContext::GetTokenId() const
40 {
41     return identify_->GetAccessTokenId();
42 }
43 
OnStart()44 bool IdentifyContext::OnStart()
45 {
46     IAM_LOGI("%{public}s start", GetDescription());
47     IF_FALSE_LOGE_AND_RETURN_VAL(identify_ != nullptr, false);
48     bool startRet = identify_->Start(scheduleList_, shared_from_this());
49     if (!startRet) {
50         IAM_LOGE("%{public}s identify start fail", GetDescription());
51         SetLatestError(identify_->GetLatestError());
52         return startRet;
53     }
54     IF_FALSE_LOGE_AND_RETURN_VAL(scheduleList_.size() == 1, false);
55     IF_FALSE_LOGE_AND_RETURN_VAL(scheduleList_[0] != nullptr, false);
56     bool startScheduleRet = scheduleList_[0]->StartSchedule();
57     IF_FALSE_LOGE_AND_RETURN_VAL(startScheduleRet, false);
58     IAM_LOGI("%{public}s success", GetDescription());
59     return true;
60 }
61 
OnResult(int32_t resultCode,const std::shared_ptr<Attributes> & scheduleResultAttr)62 void IdentifyContext::OnResult(int32_t resultCode, const std::shared_ptr<Attributes> &scheduleResultAttr)
63 {
64     IAM_LOGI("%{public}s receive result code %{public}d", GetDescription(), resultCode);
65     Identification::IdentifyResultInfo resultInfo = {};
66     bool updateRet = UpdateScheduleResult(scheduleResultAttr, resultInfo);
67     if (!updateRet) {
68         IAM_LOGE("%{public}s UpdateScheduleResult fail", GetDescription());
69         if (resultCode == SUCCESS) {
70             resultCode = GetLatestError();
71         }
72         resultInfo.result = resultCode;
73     }
74     InvokeResultCallback(resultInfo);
75     IAM_LOGI("%{public}s on result %{public}d finish", GetDescription(), resultCode);
76 }
77 
OnStop()78 bool IdentifyContext::OnStop()
79 {
80     IAM_LOGI("%{public}s start", GetDescription());
81     if (scheduleList_.size() == 1 && scheduleList_[0] != nullptr) {
82         scheduleList_[0]->StopSchedule();
83     }
84 
85     IF_FALSE_LOGE_AND_RETURN_VAL(identify_ != nullptr, false);
86     bool cancelRet = identify_->Cancel();
87     if (!cancelRet) {
88         IAM_LOGE("%{public}s identify cancel fail", GetDescription());
89         SetLatestError(identify_->GetLatestError());
90         return cancelRet;
91     }
92     return true;
93 }
94 
UpdateScheduleResult(const std::shared_ptr<Attributes> & scheduleResultAttr,Identification::IdentifyResultInfo & resultInfo)95 bool IdentifyContext::UpdateScheduleResult(const std::shared_ptr<Attributes> &scheduleResultAttr,
96     Identification::IdentifyResultInfo &resultInfo)
97 {
98     IF_FALSE_LOGE_AND_RETURN_VAL(identify_ != nullptr, false);
99     IF_FALSE_LOGE_AND_RETURN_VAL(scheduleResultAttr != nullptr, false);
100     std::vector<uint8_t> scheduleResult;
101     bool getResultCodeRet = scheduleResultAttr->GetUint8ArrayValue(Attributes::ATTR_RESULT, scheduleResult);
102     IF_FALSE_LOGE_AND_RETURN_VAL(getResultCodeRet == true, false);
103     bool updateRet = identify_->Update(scheduleResult, resultInfo);
104     if (!updateRet) {
105         IAM_LOGE("%{public}s identify update fail", GetDescription());
106         SetLatestError(identify_->GetLatestError());
107         return updateRet;
108     }
109     return true;
110 }
111 
InvokeResultCallback(const Identification::IdentifyResultInfo & resultInfo) const112 void IdentifyContext::InvokeResultCallback(const Identification::IdentifyResultInfo &resultInfo) const
113 {
114     IAM_LOGI("%{public}s start", GetDescription());
115     IF_FALSE_LOGE_AND_RETURN(callback_ != nullptr);
116     Attributes finalResult;
117     bool setResultCodeRet = finalResult.SetInt32Value(Attributes::ATTR_RESULT_CODE, resultInfo.result);
118     IF_FALSE_LOGE_AND_RETURN(setResultCodeRet == true);
119     bool setUserIdRet = finalResult.SetInt32Value(Attributes::ATTR_USER_ID, resultInfo.userId);
120     IF_FALSE_LOGE_AND_RETURN(setUserIdRet == true);
121     if (resultInfo.token.size() != 0) {
122         bool setSignatureResult = finalResult.SetUint8ArrayValue(Attributes::ATTR_SIGNATURE, resultInfo.token);
123         IF_FALSE_LOGE_AND_RETURN(setSignatureResult == true);
124     }
125 
126     callback_->OnResult(resultInfo.result, finalResult);
127     IAM_LOGI("%{public}s invoke result callback success", GetDescription());
128 }
129 } // namespace UserAuth
130 } // namespace UserIam
131 } // namespace OHOS
132