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 "schedule_holder_context.h"
26 #include "widget_context.h"
27 #include "remote_msg_util.h"
28
29 #define LOG_TAG "USER_AUTH_SA"
30
31 namespace OHOS {
32 namespace UserIam {
33 namespace UserAuth {
CreateSimpleAuthContext(const Authentication::AuthenticationPara & para,const std::shared_ptr<ContextCallback> & callback)34 std::shared_ptr<Context> ContextFactory::CreateSimpleAuthContext(const Authentication::AuthenticationPara ¶,
35 const std::shared_ptr<ContextCallback> &callback)
36 {
37 IF_FALSE_LOGE_AND_RETURN_VAL(callback != nullptr, nullptr);
38 uint64_t newContextId = ContextPool::GetNewContextId();
39 auto auth = Common::MakeShared<AuthenticationImpl>(newContextId, para);
40 IF_FALSE_LOGE_AND_RETURN_VAL(auth != nullptr, nullptr);
41 auth->SetChallenge(para.challenge);
42 auth->SetAccessTokenId(para.tokenId);
43 auth->SetEndAfterFirstFail(para.endAfterFirstFail);
44 return Common::MakeShared<SimpleAuthContext>(newContextId, auth, callback);
45 }
46
CreateIdentifyContext(const Identification::IdentificationPara & para,const std::shared_ptr<ContextCallback> & callback)47 std::shared_ptr<Context> ContextFactory::CreateIdentifyContext(const Identification::IdentificationPara ¶,
48 const std::shared_ptr<ContextCallback> &callback)
49 {
50 IF_FALSE_LOGE_AND_RETURN_VAL(callback != nullptr, nullptr);
51 uint64_t newContextId = ContextPool::GetNewContextId();
52 auto identify = Common::MakeShared<IdentificationImpl>(newContextId, para.authType);
53 IF_FALSE_LOGE_AND_RETURN_VAL(identify != nullptr, nullptr);
54 identify->SetChallenge(para.challenge);
55 identify->SetAccessTokenId(para.tokenId);
56 return Common::MakeShared<IdentifyContext>(newContextId, identify, callback);
57 }
58
CreateEnrollContext(const Enrollment::EnrollmentPara & para,const std::shared_ptr<ContextCallback> & callback)59 std::shared_ptr<Context> ContextFactory::CreateEnrollContext(const Enrollment::EnrollmentPara ¶,
60 const std::shared_ptr<ContextCallback> &callback)
61 {
62 IF_FALSE_LOGE_AND_RETURN_VAL(callback != nullptr, nullptr);
63 uint64_t newContextId = ContextPool::GetNewContextId();
64 auto enroll = Common::MakeShared<EnrollmentImpl>(para);
65 IF_FALSE_LOGE_AND_RETURN_VAL(enroll != nullptr, nullptr);
66 enroll->SetAuthToken(para.token);
67 enroll->SetAccessTokenId(para.tokenId);
68 enroll->SetPinSubType(para.pinType);
69 enroll->SetIsUpdate(para.isUpdate);
70 return Common::MakeShared<EnrollContext>(newContextId, enroll, callback);
71 }
72
CreateWidgetAuthContext(std::shared_ptr<ContextCallback> callback)73 std::shared_ptr<Context> ContextFactory::CreateWidgetAuthContext(std::shared_ptr<ContextCallback> callback)
74 {
75 return nullptr;
76 }
77
CreateWidgetContext(const AuthWidgetContextPara & para,std::shared_ptr<ContextCallback> callback)78 std::shared_ptr<Context> ContextFactory::CreateWidgetContext(const AuthWidgetContextPara ¶,
79 std::shared_ptr<ContextCallback> callback)
80 {
81 IF_FALSE_LOGE_AND_RETURN_VAL(callback != nullptr, nullptr);
82 uint64_t newContextId = ContextPool::GetNewContextId();
83 return Common::MakeShared<WidgetContext>(newContextId, para, callback);
84 }
85
CreateRemoteAuthContext(const Authentication::AuthenticationPara & para,RemoteAuthContextParam & remoteAuthContextParam,const std::shared_ptr<ContextCallback> & callback)86 std::shared_ptr<Context> ContextFactory::CreateRemoteAuthContext(const Authentication::AuthenticationPara ¶,
87 RemoteAuthContextParam &remoteAuthContextParam, const std::shared_ptr<ContextCallback> &callback)
88 {
89 IF_FALSE_LOGE_AND_RETURN_VAL(callback != nullptr, nullptr);
90
91 uint64_t newContextId = ContextPool::GetNewContextId();
92 auto auth = Common::MakeShared<AuthenticationImpl>(newContextId, para);
93 IF_FALSE_LOGE_AND_RETURN_VAL(auth != nullptr, nullptr);
94 auth->SetChallenge(para.challenge);
95 auth->SetAccessTokenId(para.tokenId);
96
97 if (remoteAuthContextParam.connectionName == "") {
98 bool getConnectionNameRet = RemoteMsgUtil::GetConnectionName(newContextId,
99 remoteAuthContextParam.connectionName);
100 IF_FALSE_LOGE_AND_RETURN_VAL(getConnectionNameRet, nullptr);
101 }
102
103 return Common::MakeShared<RemoteAuthContext>(newContextId, auth, remoteAuthContextParam, callback);
104 }
105
CreateRemoteAuthInvokerContext(AuthParamInner authParam,RemoteAuthInvokerContextParam param,std::shared_ptr<ContextCallback> callback)106 std::shared_ptr<Context> ContextFactory::CreateRemoteAuthInvokerContext(AuthParamInner authParam,
107 RemoteAuthInvokerContextParam param, std::shared_ptr<ContextCallback> callback)
108 {
109 IF_FALSE_LOGE_AND_RETURN_VAL(callback != nullptr, nullptr);
110
111 uint64_t newContextId = ContextPool::GetNewContextId();
112 bool getConnectionNameRet = RemoteMsgUtil::GetConnectionName(newContextId, param.connectionName);
113 IF_FALSE_LOGE_AND_RETURN_VAL(getConnectionNameRet, nullptr);
114
115 return Common::MakeShared<RemoteAuthInvokerContext>(newContextId, authParam, param, callback);
116 }
117
CreateScheduleHolderContext(std::shared_ptr<ScheduleNode> scheduleNode)118 std::shared_ptr<Context> ContextFactory::CreateScheduleHolderContext(std::shared_ptr<ScheduleNode> scheduleNode)
119 {
120 uint64_t newContextId = ContextPool::GetNewContextId();
121 return Common::MakeShared<ScheduleHolderContext>(newContextId, scheduleNode);
122 }
123 } // namespace UserAuth
124 } // namespace UserIam
125 } // namespace OHOS
126