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