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 "delete_context.h"
20 #include "enroll_context.h"
21 #include "iam_check.h"
22 #include "iam_logger.h"
23 #include "iam_ptr.h"
24 #include "identify_context.h"
25 #include "simple_auth_context.h"
26 #include "schedule_holder_context.h"
27 #include "widget_context.h"
28 #include "remote_msg_util.h"
29
30 #define LOG_TAG "USER_AUTH_SA"
31
32 namespace OHOS {
33 namespace UserIam {
34 namespace UserAuth {
CreateSimpleAuthContext(const Authentication::AuthenticationPara & para,const std::shared_ptr<ContextCallback> & callback,bool needSubscribeAppState)35 std::shared_ptr<Context> ContextFactory::CreateSimpleAuthContext(const Authentication::AuthenticationPara ¶,
36 const std::shared_ptr<ContextCallback> &callback, bool needSubscribeAppState)
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);
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, needSubscribeAppState);
46 }
47
CreateIdentifyContext(const Identification::IdentificationPara & para,const std::shared_ptr<ContextCallback> & callback)48 std::shared_ptr<Context> ContextFactory::CreateIdentifyContext(const Identification::IdentificationPara ¶,
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 Enrollment::EnrollmentPara & para,const std::shared_ptr<ContextCallback> & callback,bool needSubscribeAppState)60 std::shared_ptr<Context> ContextFactory::CreateEnrollContext(const Enrollment::EnrollmentPara ¶,
61 const std::shared_ptr<ContextCallback> &callback, bool needSubscribeAppState)
62 {
63 IF_FALSE_LOGE_AND_RETURN_VAL(callback != nullptr, nullptr);
64 uint64_t newContextId = ContextPool::GetNewContextId();
65 auto enroll = Common::MakeShared<EnrollmentImpl>(para);
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, needSubscribeAppState);
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,const sptr<IModalCallback> & modalCallback)79 std::shared_ptr<Context> ContextFactory::CreateWidgetContext(const AuthWidgetContextPara ¶,
80 std::shared_ptr<ContextCallback> callback, const sptr<IModalCallback> &modalCallback)
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, modalCallback);
85 }
86
CreateRemoteAuthContext(const Authentication::AuthenticationPara & para,RemoteAuthContextParam & remoteAuthContextParam,const std::shared_ptr<ContextCallback> & callback)87 std::shared_ptr<Context> ContextFactory::CreateRemoteAuthContext(const Authentication::AuthenticationPara ¶,
88 RemoteAuthContextParam &remoteAuthContextParam, const std::shared_ptr<ContextCallback> &callback)
89 {
90 IF_FALSE_LOGE_AND_RETURN_VAL(callback != nullptr, nullptr);
91
92 uint64_t newContextId = ContextPool::GetNewContextId();
93 auto auth = Common::MakeShared<AuthenticationImpl>(newContextId, para);
94 IF_FALSE_LOGE_AND_RETURN_VAL(auth != nullptr, nullptr);
95 auth->SetChallenge(para.challenge);
96 auth->SetAccessTokenId(para.tokenId);
97
98 if (remoteAuthContextParam.connectionName == "") {
99 bool getConnectionNameRet = RemoteMsgUtil::GetConnectionName(newContextId,
100 remoteAuthContextParam.connectionName);
101 IF_FALSE_LOGE_AND_RETURN_VAL(getConnectionNameRet, nullptr);
102 }
103
104 return Common::MakeShared<RemoteAuthContext>(newContextId, auth, remoteAuthContextParam, callback);
105 }
106
CreateRemoteAuthInvokerContext(AuthParamInner authParam,RemoteAuthInvokerContextParam param,std::shared_ptr<ContextCallback> callback)107 std::shared_ptr<Context> ContextFactory::CreateRemoteAuthInvokerContext(AuthParamInner authParam,
108 RemoteAuthInvokerContextParam param, std::shared_ptr<ContextCallback> callback)
109 {
110 IF_FALSE_LOGE_AND_RETURN_VAL(callback != nullptr, nullptr);
111
112 uint64_t newContextId = ContextPool::GetNewContextId();
113 bool getConnectionNameRet = RemoteMsgUtil::GetConnectionName(newContextId, param.connectionName);
114 IF_FALSE_LOGE_AND_RETURN_VAL(getConnectionNameRet, nullptr);
115
116 return Common::MakeShared<RemoteAuthInvokerContext>(newContextId, authParam, param, callback);
117 }
118
CreateLocalRemoteAuthContext(const Authentication::AuthenticationPara & para,LocalRemoteAuthContextParam & localRemoteAuthContextParam,const std::shared_ptr<ContextCallback> & callback)119 std::shared_ptr<Context> ContextFactory::CreateLocalRemoteAuthContext(const Authentication::AuthenticationPara ¶,
120 LocalRemoteAuthContextParam &localRemoteAuthContextParam, const std::shared_ptr<ContextCallback> &callback)
121 {
122 IF_FALSE_LOGE_AND_RETURN_VAL(callback != nullptr, nullptr);
123
124 uint64_t newContextId = ContextPool::GetNewContextId();
125 auto auth = Common::MakeShared<AuthenticationImpl>(newContextId, para);
126 IF_FALSE_LOGE_AND_RETURN_VAL(auth != nullptr, nullptr);
127 auth->SetChallenge(para.challenge);
128 auth->SetAccessTokenId(para.tokenId);
129 return Common::MakeShared<LocalRemoteAuthContext>(newContextId, auth, localRemoteAuthContextParam, callback);
130 }
131
CreateScheduleHolderContext(std::shared_ptr<ScheduleNode> scheduleNode)132 std::shared_ptr<Context> ContextFactory::CreateScheduleHolderContext(std::shared_ptr<ScheduleNode> scheduleNode)
133 {
134 uint64_t newContextId = ContextPool::GetNewContextId();
135 return Common::MakeShared<ScheduleHolderContext>(newContextId, scheduleNode);
136 }
137
CreateDeleteContext(const Deletion::DeleteParam & para,const std::shared_ptr<ContextCallback> & callback)138 std::shared_ptr<Context> ContextFactory::CreateDeleteContext(const Deletion::DeleteParam ¶,
139 const std::shared_ptr<ContextCallback> &callback)
140 {
141 IF_FALSE_LOGE_AND_RETURN_VAL(callback != nullptr, nullptr);
142 uint64_t newContextId = ContextPool::GetNewContextId();
143 auto deleteImpl = Common::MakeShared<DeleteImpl>(para);
144 IF_FALSE_LOGE_AND_RETURN_VAL(deleteImpl != nullptr, nullptr);
145 deleteImpl->SetAccessTokenId(para.tokenId);
146 return Common::MakeShared<DeleteContext>(newContextId, deleteImpl, callback);
147 }
148 } // namespace UserAuth
149 } // namespace UserIam
150 } // namespace OHOS
151