1 /*
2 * Copyright (c) 2022-2024 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
16 #include "user_auth_callback_service.h"
17
18 #include "callback_manager.h"
19 #include "iam_logger.h"
20 #include "iam_ptr.h"
21
22 #define LOG_TAG "USER_AUTH_SDK"
23
24 namespace OHOS {
25 namespace UserIam {
26 namespace UserAuth {
UserAuthCallbackService(const std::shared_ptr<AuthenticationCallback> & impl)27 UserAuthCallbackService::UserAuthCallbackService(const std::shared_ptr<AuthenticationCallback> &impl)
28 : authCallback_(impl),
29 iamHitraceHelper_(Common::MakeShared<UserIam::UserAuth::IamHitraceHelper>("UserAuth InnerKit"))
30 {
31 CallbackManager::CallbackAction action = [impl]() {
32 if (impl != nullptr) {
33 IAM_LOGI("user auth service death, auth callback return default result to caller");
34 Attributes extraInfo;
35 impl->OnResult(GENERAL_ERROR, extraInfo);
36 }
37 };
38 CallbackManager::GetInstance().AddCallback(reinterpret_cast<uintptr_t>(this), action);
39 }
40
UserAuthCallbackService(const std::shared_ptr<AuthenticationCallback> & impl,const std::shared_ptr<UserAuthModalClientCallback> & modalCallback)41 UserAuthCallbackService::UserAuthCallbackService(const std::shared_ptr<AuthenticationCallback> &impl,
42 const std::shared_ptr<UserAuthModalClientCallback> &modalCallback)
43 : authCallback_(impl), modalCallback_(modalCallback),
44 iamHitraceHelper_(Common::MakeShared<UserIam::UserAuth::IamHitraceHelper>("UserAuth InnerKit"))
45 {
46 CallbackManager::CallbackAction action = [impl]() {
47 if (impl != nullptr) {
48 IAM_LOGI("user auth service death, auth callback return default result to caller");
49 Attributes extraInfo;
50 impl->OnResult(GENERAL_ERROR, extraInfo);
51 }
52 };
53 CallbackManager::GetInstance().AddCallback(reinterpret_cast<uintptr_t>(this), action);
54 }
55
UserAuthCallbackService(const std::shared_ptr<IdentificationCallback> & impl)56 UserAuthCallbackService::UserAuthCallbackService(const std::shared_ptr<IdentificationCallback> &impl)
57 : identifyCallback_(impl),
58 iamHitraceHelper_(Common::MakeShared<UserIam::UserAuth::IamHitraceHelper>("UserAuth InnerKit"))
59 {
60 CallbackManager::CallbackAction action = [impl]() {
61 if (impl != nullptr) {
62 IAM_LOGI("user auth service death, identify callback return default result to caller");
63 Attributes extraInfo;
64 impl->OnResult(GENERAL_ERROR, extraInfo);
65 }
66 };
67 CallbackManager::GetInstance().AddCallback(reinterpret_cast<uintptr_t>(this), action);
68 }
69
UserAuthCallbackService(const std::shared_ptr<PrepareRemoteAuthCallback> & impl)70 UserAuthCallbackService::UserAuthCallbackService(const std::shared_ptr<PrepareRemoteAuthCallback> &impl)
71 : prepareRemoteAuthCallback_(impl),
72 iamHitraceHelper_(Common::MakeShared<UserIam::UserAuth::IamHitraceHelper>("UserAuth InnerKit"))
73 {
74 CallbackManager::CallbackAction action = [impl]() {
75 if (impl != nullptr) {
76 IAM_LOGI("user auth service death, prepare remote auth callback return default result to caller");
77 impl->OnResult(GENERAL_ERROR);
78 }
79 };
80 CallbackManager::GetInstance().AddCallback(reinterpret_cast<uintptr_t>(this), action);
81 }
82
~UserAuthCallbackService()83 UserAuthCallbackService::~UserAuthCallbackService()
84 {
85 IAM_LOGD("start");
86 iamHitraceHelper_= nullptr;
87 CallbackManager::GetInstance().RemoveCallback(reinterpret_cast<uintptr_t>(this));
88 }
89
OnResult(int32_t resultCode,const std::vector<uint8_t> & extraInfo)90 int32_t UserAuthCallbackService::OnResult(int32_t resultCode, const std::vector<uint8_t> &extraInfo)
91 {
92 IAM_LOGD("start, result:%{public}d", resultCode);
93 Attributes attribute(extraInfo);
94 if (authCallback_ != nullptr) {
95 if (modalCallback_ != nullptr) {
96 IAM_LOGI("IsModalInit :%{public}d, IsModalDestroy :%{public}d", modalCallback_->IsModalInit(),
97 modalCallback_->IsModalDestroy());
98 if (modalCallback_->IsModalInit() && !modalCallback_->IsModalDestroy()) {
99 const uint32_t sleepTime = 100000;
100 usleep(sleepTime);
101 IAM_LOGI("process result continue");
102 }
103 }
104 authCallback_->OnResult(resultCode, attribute);
105 } else if (identifyCallback_ != nullptr) {
106 identifyCallback_->OnResult(resultCode, attribute);
107 } else if (prepareRemoteAuthCallback_ != nullptr) {
108 prepareRemoteAuthCallback_->OnResult(resultCode);
109 } else {
110 IAM_LOGE("all callback is nullptr");
111 return GENERAL_ERROR;
112 }
113 iamHitraceHelper_= nullptr;
114 return SUCCESS;
115 }
116
OnAcquireInfo(int32_t module,int32_t acquireInfo,const std::vector<uint8_t> & extraInfo)117 int32_t UserAuthCallbackService::OnAcquireInfo(int32_t module, int32_t acquireInfo,
118 const std::vector<uint8_t> &extraInfo)
119 {
120 IAM_LOGD("start, module:%{public}d acquireInfo:%{public}d", module, acquireInfo);
121 Attributes attribute(extraInfo);
122 if (authCallback_ != nullptr) {
123 authCallback_->OnAcquireInfo(module, acquireInfo, attribute);
124 } else if (identifyCallback_ != nullptr) {
125 identifyCallback_->OnAcquireInfo(module, acquireInfo, attribute);
126 } else if (prepareRemoteAuthCallback_ != nullptr) {
127 IAM_LOGE("prepare remote auth callback not support acquire info");
128 } else {
129 IAM_LOGE("all callback is nullptr");
130 return GENERAL_ERROR;
131 }
132 return SUCCESS;
133 }
134
CallbackEnter(uint32_t code)135 int32_t UserAuthCallbackService::CallbackEnter([[maybe_unused]] uint32_t code)
136 {
137 IAM_LOGI("start, code:%{public}u", code);
138 return SUCCESS;
139 }
140
CallbackExit(uint32_t code,int32_t result)141 int32_t UserAuthCallbackService::CallbackExit([[maybe_unused]] uint32_t code, [[maybe_unused]] int32_t result)
142 {
143 IAM_LOGI("leave, code:%{public}u, result:%{public}d", code, result);
144 return SUCCESS;
145 }
146
GetExecutorPropertyCallbackService(const std::shared_ptr<GetPropCallback> & impl)147 GetExecutorPropertyCallbackService::GetExecutorPropertyCallbackService(const std::shared_ptr<GetPropCallback> &impl)
148 : getPropCallback_(impl)
149 {
150 CallbackManager::CallbackAction action = [impl]() {
151 if (impl != nullptr) {
152 IAM_LOGI("user auth service death, get prop callback return default result to caller");
153 Attributes extraInfo;
154 impl->OnResult(GENERAL_ERROR, extraInfo);
155 }
156 };
157 CallbackManager::GetInstance().AddCallback(reinterpret_cast<uintptr_t>(this), action);
158 }
159
~GetExecutorPropertyCallbackService()160 GetExecutorPropertyCallbackService::~GetExecutorPropertyCallbackService()
161 {
162 CallbackManager::GetInstance().RemoveCallback(reinterpret_cast<uintptr_t>(this));
163 }
164
OnGetExecutorPropertyResult(int32_t resultCode,const std::vector<uint8_t> & attributes)165 int32_t GetExecutorPropertyCallbackService::OnGetExecutorPropertyResult(int32_t resultCode,
166 const std::vector<uint8_t> &attributes)
167 {
168 IAM_LOGD("start, result:%{public}d", resultCode);
169 if (getPropCallback_ == nullptr) {
170 IAM_LOGE("get prop callback is nullptr");
171 return GENERAL_ERROR;
172 }
173 Attributes attribute(attributes);
174 getPropCallback_->OnResult(resultCode, attribute);
175 return SUCCESS;
176 }
177
CallbackEnter(uint32_t code)178 int32_t GetExecutorPropertyCallbackService::CallbackEnter([[maybe_unused]] uint32_t code)
179 {
180 IAM_LOGI("start, code:%{public}u", code);
181 return SUCCESS;
182 }
183
CallbackExit(uint32_t code,int32_t result)184 int32_t GetExecutorPropertyCallbackService::CallbackExit([[maybe_unused]] uint32_t code,
185 [[maybe_unused]] int32_t result)
186 {
187 IAM_LOGI("leave, code:%{public}u, result:%{public}d", code, result);
188 return SUCCESS;
189 }
190
SetExecutorPropertyCallbackService(const std::shared_ptr<SetPropCallback> & impl)191 SetExecutorPropertyCallbackService::SetExecutorPropertyCallbackService(const std::shared_ptr<SetPropCallback> &impl)
192 : setPropCallback_(impl)
193 {
194 CallbackManager::CallbackAction action = [impl]() {
195 if (impl != nullptr) {
196 IAM_LOGI("user auth service death, set prop callback return default result to caller");
197 Attributes extraInfo;
198 impl->OnResult(GENERAL_ERROR, extraInfo);
199 }
200 };
201 CallbackManager::GetInstance().AddCallback(reinterpret_cast<uintptr_t>(this), action);
202 }
203
~SetExecutorPropertyCallbackService()204 SetExecutorPropertyCallbackService::~SetExecutorPropertyCallbackService()
205 {
206 CallbackManager::GetInstance().RemoveCallback(reinterpret_cast<uintptr_t>(this));
207 }
208
OnSetExecutorPropertyResult(int32_t resultCode)209 int32_t SetExecutorPropertyCallbackService::OnSetExecutorPropertyResult(int32_t resultCode)
210 {
211 IAM_LOGD("start, result:%{public}d", resultCode);
212 if (setPropCallback_ == nullptr) {
213 IAM_LOGE("set prop callback is nullptr");
214 return GENERAL_ERROR;
215 }
216 Attributes attr;
217 setPropCallback_->OnResult(resultCode, attr);
218 return SUCCESS;
219 }
220
CallbackEnter(uint32_t code)221 int32_t SetExecutorPropertyCallbackService::CallbackEnter([[maybe_unused]] uint32_t code)
222 {
223 IAM_LOGI("start, code:%{public}u", code);
224 return SUCCESS;
225 }
226
CallbackExit(uint32_t code,int32_t result)227 int32_t SetExecutorPropertyCallbackService::CallbackExit([[maybe_unused]] uint32_t code,
228 [[maybe_unused]] int32_t result)
229 {
230 IAM_LOGI("leave, code:%{public}u, result:%{public}d", code, result);
231 return SUCCESS;
232 }
233 } // namespace UserAuth
234 } // namespace UserIam
235 } // namespace OHOS