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 #include "simple_auth_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 {
SimpleAuthContext(uint64_t contextId,std::shared_ptr<Authentication> auth,std::shared_ptr<ContextCallback> callback)27 SimpleAuthContext::SimpleAuthContext(uint64_t contextId, std::shared_ptr<Authentication> auth,
28 std::shared_ptr<ContextCallback> callback)
29 : BaseContext("SimpleAuth", contextId, callback),
30 auth_(auth)
31 {
32 }
33
GetContextType() const34 ContextType SimpleAuthContext::GetContextType() const
35 {
36 return CONTEXT_SIMPLE_AUTH;
37 }
38
OnStart()39 bool SimpleAuthContext::OnStart()
40 {
41 IAM_LOGI("%{public}s start", GetDescription());
42 IF_FALSE_LOGE_AND_RETURN_VAL(auth_ != nullptr, false);
43 bool startRet = auth_->Start(scheduleList_, shared_from_this());
44 if (!startRet) {
45 IAM_LOGE("%{public}s auth start fail", GetDescription());
46 SetLatestError(auth_->GetLatestError());
47 return startRet;
48 }
49 IF_FALSE_LOGE_AND_RETURN_VAL(scheduleList_.size() == 1, false);
50 IF_FALSE_LOGE_AND_RETURN_VAL(scheduleList_[0] != nullptr, false);
51 bool startScheduleRet = scheduleList_[0]->StartSchedule();
52 IF_FALSE_LOGE_AND_RETURN_VAL(startScheduleRet, false);
53 IAM_LOGI("%{public}s success", GetDescription());
54 return true;
55 }
56
OnResult(int32_t resultCode,const std::shared_ptr<Attributes> & scheduleResultAttr)57 void SimpleAuthContext::OnResult(int32_t resultCode, const std::shared_ptr<Attributes> &scheduleResultAttr)
58 {
59 IAM_LOGI("%{public}s receive result code %{public}d", GetDescription(), resultCode);
60 Authentication::AuthResultInfo resultInfo = {};
61 bool updateRet = UpdateScheduleResult(scheduleResultAttr, resultInfo);
62 if (!updateRet) {
63 IAM_LOGE("%{public}s UpdateScheduleResult fail", GetDescription());
64 if (resultCode == SUCCESS) {
65 resultCode = GetLatestError();
66 }
67 resultInfo.result = resultCode;
68 }
69 InvokeResultCallback(resultInfo);
70 IAM_LOGI("%{public}s on result %{public}d finish", GetDescription(), resultCode);
71 }
72
OnStop()73 bool SimpleAuthContext::OnStop()
74 {
75 IAM_LOGI("%{public}s start", GetDescription());
76 if (scheduleList_.size() == 1 && scheduleList_[0] != nullptr) {
77 scheduleList_[0]->StopSchedule();
78 }
79
80 IF_FALSE_LOGE_AND_RETURN_VAL(auth_ != nullptr, false);
81 bool cancelRet = auth_->Cancel();
82 if (!cancelRet) {
83 IAM_LOGE("%{public}s auth stop fail", GetDescription());
84 SetLatestError(auth_->GetLatestError());
85 return cancelRet;
86 }
87 return true;
88 }
89
UpdateScheduleResult(const std::shared_ptr<Attributes> & scheduleResultAttr,Authentication::AuthResultInfo & resultInfo)90 bool SimpleAuthContext::UpdateScheduleResult(const std::shared_ptr<Attributes> &scheduleResultAttr,
91 Authentication::AuthResultInfo &resultInfo)
92 {
93 IF_FALSE_LOGE_AND_RETURN_VAL(auth_ != nullptr, false);
94 IF_FALSE_LOGE_AND_RETURN_VAL(scheduleResultAttr != nullptr, false);
95 std::vector<uint8_t> scheduleResult;
96 bool getResultCodeRet = scheduleResultAttr->GetUint8ArrayValue(Attributes::ATTR_RESULT, scheduleResult);
97 IF_FALSE_LOGE_AND_RETURN_VAL(getResultCodeRet == true, false);
98 bool updateRet = auth_->Update(scheduleResult, resultInfo);
99 if (!updateRet) {
100 IAM_LOGE("%{public}s auth update fail", GetDescription());
101 SetLatestError(auth_->GetLatestError());
102 return updateRet;
103 }
104 return true;
105 }
106
InvokeResultCallback(const Authentication::AuthResultInfo & resultInfo) const107 void SimpleAuthContext::InvokeResultCallback(const Authentication::AuthResultInfo &resultInfo) const
108 {
109 IAM_LOGI("%{public}s start", GetDescription());
110 IF_FALSE_LOGE_AND_RETURN(callback_ != nullptr);
111 Attributes finalResult;
112 bool setResultCodeRet = finalResult.SetInt32Value(Attributes::ATTR_RESULT_CODE, resultInfo.result);
113 IF_FALSE_LOGE_AND_RETURN(setResultCodeRet == true);
114 bool setFreezingTimeRet = finalResult.SetInt32Value(Attributes::ATTR_FREEZING_TIME, resultInfo.freezingTime);
115 IF_FALSE_LOGE_AND_RETURN(setFreezingTimeRet == true);
116 bool setUserIdRet = finalResult.SetInt32Value(Attributes::ATTR_REMAIN_TIMES, resultInfo.remainTimes);
117 IF_FALSE_LOGE_AND_RETURN(setUserIdRet == true);
118 if (resultInfo.token.size() != 0) {
119 bool setSignatureResult = finalResult.SetUint8ArrayValue(Attributes::ATTR_SIGNATURE, resultInfo.token);
120 IF_FALSE_LOGE_AND_RETURN(setSignatureResult == true);
121 }
122 if (resultInfo.rootSecret.size() != 0) {
123 bool setRootSecret = finalResult.SetUint8ArrayValue(Attributes::ATTR_ROOT_SECRET, resultInfo.rootSecret);
124 IF_FALSE_LOGE_AND_RETURN(setRootSecret == true);
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