• 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 "schedule_node_builder.h"
16 
17 #include <mutex>
18 
19 #include "nocopyable.h"
20 
21 #include "iam_common_defines.h"
22 #include "iam_logger.h"
23 #include "iam_ptr.h"
24 
25 #define LOG_LABEL UserIam::Common::LABEL_USER_AUTH_SA
26 
27 namespace OHOS {
28 namespace UserIam {
29 namespace UserAuth {
30 using Builder = ScheduleNode::Builder;
31 
ScheduleNodeBuilder(const std::shared_ptr<ResourceNode> & collector,const std::shared_ptr<ResourceNode> & verifier)32 ScheduleNodeBuilder::ScheduleNodeBuilder(const std::shared_ptr<ResourceNode> &collector,
33     const std::shared_ptr<ResourceNode> &verifier)
34     : collector_(collector),
35       verifier_(verifier)
36 {
37     if (collector) {
38         info_.authType = collector->GetAuthType();
39     }
40     info_.endAfterFirstFail = false;
41 }
42 
SetScheduleId(uint64_t scheduleId)43 std::shared_ptr<Builder> ScheduleNodeBuilder::SetScheduleId(uint64_t scheduleId)
44 {
45     info_.scheduleId = scheduleId;
46     return shared_from_this();
47 }
48 
SetAccessTokenId(uint32_t tokenId)49 std::shared_ptr<Builder> ScheduleNodeBuilder::SetAccessTokenId(uint32_t tokenId)
50 {
51     info_.tokenId = tokenId;
52     return shared_from_this();
53 }
54 
SetPinSubType(PinSubType pinSubType)55 std::shared_ptr<Builder> ScheduleNodeBuilder::SetPinSubType(PinSubType pinSubType)
56 {
57     info_.pinSubType = pinSubType;
58     return shared_from_this();
59 }
60 
SetTemplateIdList(const std::vector<uint64_t> & templateIdList)61 std::shared_ptr<Builder> ScheduleNodeBuilder::SetTemplateIdList(const std::vector<uint64_t> &templateIdList)
62 {
63     info_.templateIdList = templateIdList;
64     return shared_from_this();
65 }
66 
SetAuthType(AuthType authType)67 std::shared_ptr<Builder> ScheduleNodeBuilder::SetAuthType(AuthType authType)
68 {
69     info_.authType = authType;
70     return shared_from_this();
71 }
72 
SetExecutorMatcher(uint32_t executorMatcher)73 std::shared_ptr<Builder> ScheduleNodeBuilder::SetExecutorMatcher(uint32_t executorMatcher)
74 {
75     info_.executorMatcher = executorMatcher;
76     return shared_from_this();
77 }
78 
SetScheduleMode(ScheduleMode scheduleMode)79 std::shared_ptr<Builder> ScheduleNodeBuilder::SetScheduleMode(ScheduleMode scheduleMode)
80 {
81     info_.scheduleMode = scheduleMode;
82     return shared_from_this();
83 }
84 
SetScheduleCallback(const std::shared_ptr<ScheduleNodeCallback> & callback)85 std::shared_ptr<Builder> ScheduleNodeBuilder::SetScheduleCallback(const std::shared_ptr<ScheduleNodeCallback> &callback)
86 {
87     info_.callback = callback;
88     return shared_from_this();
89 }
90 
SetExpiredTime(uint32_t ms)91 std::shared_ptr<Builder> ScheduleNodeBuilder::SetExpiredTime(uint32_t ms)
92 {
93     info_.expiredTime = ms;
94     return shared_from_this();
95 }
96 
SetParametersAttributes(const std::shared_ptr<Attributes> & parameters)97 std::shared_ptr<Builder> ScheduleNodeBuilder::SetParametersAttributes(const std::shared_ptr<Attributes> &parameters)
98 {
99     info_.parameters = parameters;
100     return shared_from_this();
101 }
102 
SetThreadHandler(const std::shared_ptr<ThreadHandler> & threadHandler)103 std::shared_ptr<Builder> ScheduleNodeBuilder::SetThreadHandler(const std::shared_ptr<ThreadHandler> &threadHandler)
104 {
105     info_.threadHandler = threadHandler;
106     return shared_from_this();
107 }
108 
SetEndAfterFirstFail(const bool endAfterFirstFail)109 std::shared_ptr<Builder> ScheduleNodeBuilder::SetEndAfterFirstFail(const bool endAfterFirstFail)
110 {
111     info_.endAfterFirstFail = endAfterFirstFail;
112     return shared_from_this();
113 }
114 
SetExtraInfo(const std::vector<uint8_t> & extraInfo)115 std::shared_ptr<Builder> ScheduleNodeBuilder::SetExtraInfo(const std::vector<uint8_t> &extraInfo)
116 {
117     info_.extraInfo = extraInfo;
118     return shared_from_this();
119 }
120 
Build()121 std::shared_ptr<ScheduleNode> ScheduleNodeBuilder::Build()
122 {
123     if (!CheckParameters()) {
124         IAM_LOGE("checkParameters error");
125         return nullptr;
126     }
127     IAM_LOGI("scheduleNode builder start to build");
128     info_.collector = collector_;
129     info_.verifier = verifier_;
130     return Common::MakeShared<ScheduleNodeImpl>(info_);
131 }
132 
CheckParameters() const133 bool ScheduleNodeBuilder::CheckParameters() const
134 {
135     if (collector_ && collector_->GetAuthType() != info_.authType) {
136         IAM_LOGE("authType mismatch");
137         return false;
138     }
139     return true;
140 }
141 
CheckExecutors(const std::shared_ptr<ResourceNode> & collector,const std::shared_ptr<ResourceNode> & verifier)142 bool ScheduleNodeBuilder::CheckExecutors(const std::shared_ptr<ResourceNode> &collector,
143     const std::shared_ptr<ResourceNode> &verifier)
144 {
145     if (!collector) {
146         IAM_LOGE("collector not set");
147         return false;
148     }
149 
150     if (!verifier) {
151         IAM_LOGE("verifier not set");
152         return false;
153     }
154 
155     if (collector->GetAuthType() != verifier->GetAuthType()) {
156         IAM_LOGE("collector_ && verifier authtype mismatch");
157         return false;
158     }
159 
160     if (collector->GetExecutorMatcher() != verifier->GetExecutorMatcher()) {
161         IAM_LOGE("executorType mismatch");
162         return false;
163     }
164 
165     // all in one
166     if (collector == verifier && collector->GetExecutorRole() == ALL_IN_ONE) {
167         return true;
168     }
169 
170     if (collector->GetExecutorRole() == COLLECTOR && verifier->GetExecutorRole() == VERIFIER) {
171         return true;
172     }
173 
174     IAM_LOGE("executor role type mismatch");
175     return false;
176 }
177 
New(const std::shared_ptr<ResourceNode> & collector,const std::shared_ptr<ResourceNode> & verifier)178 std::shared_ptr<Builder> Builder::New(const std::shared_ptr<ResourceNode> &collector,
179     const std::shared_ptr<ResourceNode> &verifier)
180 {
181     auto result = ScheduleNodeBuilder::CheckExecutors(collector, verifier);
182     if (!result) {
183         IAM_LOGE("checkExecutors failed");
184         return nullptr;
185     }
186 
187     return Common::MakeShared<ScheduleNodeBuilder>(collector, verifier);
188 }
189 } // namespace UserAuth
190 } // namespace UserIam
191 } // namespace OHOS
192