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 "context_factory.h"
16
17 #include "context_callback_impl.h"
18 #include "context_pool.h"
19 #include "enroll_context.h"
20 #include "iam_check.h"
21 #include "iam_logger.h"
22 #include "iam_ptr.h"
23 #include "identify_context.h"
24 #include "simple_auth_context.h"
25 #include "widget_context.h"
26
27 #define LOG_LABEL UserIam::Common::LABEL_USER_AUTH_SA
28
29 namespace OHOS {
30 namespace UserIam {
31 namespace UserAuth {
CreateSimpleAuthContext(const Authentication::AuthenticationPara & para,const std::shared_ptr<ContextCallback> & callback)32 std::shared_ptr<Context> ContextFactory::CreateSimpleAuthContext(const Authentication::AuthenticationPara ¶,
33 const std::shared_ptr<ContextCallback> &callback)
34 {
35 IF_FALSE_LOGE_AND_RETURN_VAL(callback != nullptr, nullptr);
36 uint64_t newContextId = ContextPool::GetNewContextId();
37 auto auth = Common::MakeShared<AuthenticationImpl>(newContextId, para);
38 IF_FALSE_LOGE_AND_RETURN_VAL(auth != nullptr, nullptr);
39 auth->SetChallenge(para.challenge);
40 auth->SetAccessTokenId(para.tokenId);
41 auth->SetEndAfterFirstFail(para.endAfterFirstFail);
42 return Common::MakeShared<SimpleAuthContext>(newContextId, auth, callback);
43 }
44
CreateIdentifyContext(const Identification::IdentificationPara & para,const std::shared_ptr<ContextCallback> & callback)45 std::shared_ptr<Context> ContextFactory::CreateIdentifyContext(const Identification::IdentificationPara ¶,
46 const std::shared_ptr<ContextCallback> &callback)
47 {
48 IF_FALSE_LOGE_AND_RETURN_VAL(callback != nullptr, nullptr);
49 uint64_t newContextId = ContextPool::GetNewContextId();
50 auto identify = Common::MakeShared<IdentificationImpl>(newContextId, para.authType);
51 IF_FALSE_LOGE_AND_RETURN_VAL(identify != nullptr, nullptr);
52 identify->SetChallenge(para.challenge);
53 identify->SetAccessTokenId(para.tokenId);
54 return Common::MakeShared<IdentifyContext>(newContextId, identify, callback);
55 }
56
CreateEnrollContext(const Enrollment::EnrollmentPara & para,const std::shared_ptr<ContextCallback> & callback)57 std::shared_ptr<Context> ContextFactory::CreateEnrollContext(const Enrollment::EnrollmentPara ¶,
58 const std::shared_ptr<ContextCallback> &callback)
59 {
60 IF_FALSE_LOGE_AND_RETURN_VAL(callback != nullptr, nullptr);
61 uint64_t newContextId = ContextPool::GetNewContextId();
62 auto enroll = Common::MakeShared<EnrollmentImpl>(para);
63 IF_FALSE_LOGE_AND_RETURN_VAL(enroll != nullptr, nullptr);
64 enroll->SetAuthToken(para.token);
65 enroll->SetAccessTokenId(para.tokenId);
66 enroll->SetPinSubType(para.pinType);
67 enroll->SetIsUpdate(para.isUpdate);
68 return Common::MakeShared<EnrollContext>(newContextId, enroll, callback);
69 }
70
CreateWidgetAuthContext(std::shared_ptr<ContextCallback> callback)71 std::shared_ptr<Context> ContextFactory::CreateWidgetAuthContext(std::shared_ptr<ContextCallback> callback)
72 {
73 return nullptr;
74 }
75
CreateWidgetContext(const AuthWidgetContextPara & para,std::shared_ptr<ContextCallback> callback)76 std::shared_ptr<Context> ContextFactory::CreateWidgetContext(const AuthWidgetContextPara ¶,
77 std::shared_ptr<ContextCallback> callback)
78 {
79 IF_FALSE_LOGE_AND_RETURN_VAL(callback != nullptr, nullptr);
80 uint64_t newContextId = ContextPool::GetNewContextId();
81 return Common::MakeShared<WidgetContext>(newContextId, para, callback);
82 }
83 } // namespace UserAuth
84 } // namespace UserIam
85 } // namespace OHOS
86