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
16 #include "useridm_client.h"
17 #include <if_system_ability_manager.h>
18 #include <iservice_registry.h>
19 #include <system_ability_definition.h>
20 #include "useridm_callback_stub.h"
21 #include "useridm_getinfo_callback_stub.h"
22 #include "useridm_getsecinfo_callback_stub.h"
23 #include "useridm_hilog_wrapper.h"
24
25 namespace OHOS {
26 namespace UserIAM {
27 namespace UserIDM {
28 UserIDMClient::UserIDMClient() = default;
29 UserIDMClient::~UserIDMClient() = default;
30
GetUserIDMProxy()31 sptr<IUserIDM> UserIDMClient::GetUserIDMProxy()
32 {
33 USERIDM_HILOGD(MODULE_CLIENT, "GetUserIDMProxy start");
34
35 std::lock_guard<std::mutex> lock(mutex_);
36 if (proxy_ != nullptr) {
37 return proxy_;
38 }
39
40 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
41 if (!sam) {
42 USERIDM_HILOGE(MODULE_CLIENT, "failed to get systemAbilityManager");
43 return nullptr;
44 }
45
46 sptr<IRemoteObject> obj = sam->CheckSystemAbility(SUBSYS_USERIAM_SYS_ABILITY_USERIDM);
47 if (!obj) {
48 USERIDM_HILOGE(MODULE_CLIENT, "failed to get remoteObject");
49 return nullptr;
50 }
51
52 sptr<IRemoteObject::DeathRecipient> dr = new UserIDMDeathRecipient();
53 if ((obj->IsProxyObject()) && (!obj->AddDeathRecipient(dr))) {
54 USERIDM_HILOGE(MODULE_CLIENT, "Failed to add death recipient");
55 return nullptr;
56 }
57
58 proxy_ = iface_cast<IUserIDM>(obj);
59 deathRecipient_ = dr;
60
61 USERIDM_HILOGD(MODULE_CLIENT, "Succeed to connect manager service");
62 return proxy_;
63 }
64
ResetUserIDMProxy(const wptr<IRemoteObject> & remote)65 void UserIDMClient::ResetUserIDMProxy(const wptr<IRemoteObject>& remote)
66 {
67 USERIDM_HILOGD(MODULE_CLIENT, "ResetUserIDMProxy start");
68
69 std::lock_guard<std::mutex> lock(mutex_);
70 if (!proxy_) {
71 USERIDM_HILOGE(MODULE_CLIENT, "ResetUserIDMProxy proxy is nullptr");
72 return;
73 }
74
75 auto serviceRemote = proxy_->AsObject();
76 if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
77 serviceRemote->RemoveDeathRecipient(deathRecipient_);
78 proxy_ = nullptr;
79 }
80 }
81
OnRemoteDied(const wptr<IRemoteObject> & remote)82 void UserIDMClient::UserIDMDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
83 {
84 USERIDM_HILOGD(MODULE_CLIENT, "OnRemoteDied start");
85
86 if (remote == nullptr) {
87 USERIDM_HILOGE(MODULE_CLIENT, "OnRemoteDied failed, remote is nullptr");
88 return;
89 }
90
91 UserIDMClient::GetInstance().ResetUserIDMProxy(remote);
92 USERIDM_HILOGE(MODULE_CLIENT, "UserIDMDeathRecipient::Recv death notice");
93 }
94
OpenSession()95 uint64_t UserIDMClient::OpenSession()
96 {
97 USERIDM_HILOGD(MODULE_CLIENT, "OpenSession start");
98
99 auto proxy = GetUserIDMProxy();
100 if (proxy == nullptr) {
101 USERIDM_HILOGE(MODULE_CLIENT, "OpenSession proxy is nullptr");
102 return FAIL;
103 }
104 return proxy->OpenSession();
105 }
106
CloseSession()107 void UserIDMClient::CloseSession()
108 {
109 USERIDM_HILOGD(MODULE_CLIENT, "CloseSession start");
110
111 auto proxy = GetUserIDMProxy();
112 if (proxy == nullptr) {
113 USERIDM_HILOGE(MODULE_CLIENT, "CloseSession proxy is nullptr");
114 return;
115 }
116
117 proxy->CloseSession();
118 }
119
GetAuthInfo(int32_t userId,AuthType authType,const std::shared_ptr<GetInfoCallback> & callback)120 int32_t UserIDMClient::GetAuthInfo(int32_t userId, AuthType authType, const std::shared_ptr<GetInfoCallback>& callback)
121 {
122 USERIDM_HILOGD(MODULE_CLIENT, "GetAuthInfoById start");
123
124 if (callback == nullptr) {
125 USERIDM_HILOGE(MODULE_CLIENT, "callback is nullptr");
126 return INVALID_PARAMETERS;
127 }
128
129 auto proxy = GetUserIDMProxy();
130 if (proxy == nullptr) {
131 USERIDM_HILOGE(MODULE_CLIENT, "GetAuthInfo proxy is nullptr");
132 return FAIL;
133 }
134
135 sptr<IGetInfoCallback> callbackStub = new UserIDMGetInfoCallbackStub(callback);
136 return proxy->GetAuthInfo(userId, authType, callbackStub);
137 }
138
GetAuthInfo(AuthType authType,const std::shared_ptr<GetInfoCallback> & napiCallback)139 int32_t UserIDMClient::GetAuthInfo(AuthType authType, const std::shared_ptr<GetInfoCallback>& napiCallback)
140 {
141 USERIDM_HILOGD(MODULE_CLIENT, "GetAuthInfo start");
142
143 if (napiCallback == nullptr) {
144 USERIDM_HILOGE(MODULE_CLIENT, "callback is nullptr");
145 return INVALID_PARAMETERS;
146 }
147
148 auto proxy = GetUserIDMProxy();
149 if (proxy == nullptr) {
150 USERIDM_HILOGE(MODULE_CLIENT, "GetAuthInfo proxy is nullptr");
151 return FAIL;
152 }
153
154 sptr<IGetInfoCallback> callbackStub = new UserIDMGetInfoCallbackStub(napiCallback);
155 return proxy->GetAuthInfo(authType, callbackStub);
156 }
157
GetSecInfo(int32_t userId,const std::shared_ptr<GetSecInfoCallback> & napiCallback)158 int32_t UserIDMClient::GetSecInfo(int32_t userId, const std::shared_ptr<GetSecInfoCallback>& napiCallback)
159 {
160 USERIDM_HILOGD(MODULE_CLIENT, "GetSecInfo start");
161
162 if (napiCallback == nullptr) {
163 USERIDM_HILOGE(MODULE_CLIENT, "callback is nullptr");
164 return INVALID_PARAMETERS;
165 }
166
167 auto proxy = GetUserIDMProxy();
168 if (proxy == nullptr) {
169 USERIDM_HILOGE(MODULE_CLIENT, "GetSecInfo proxy is nullptr");
170 return FAIL;
171 }
172
173 sptr<IGetSecInfoCallback> callbackStub = new UserIDMGetSecInfoCallbackStub(napiCallback);
174 return proxy->GetSecInfo(userId, callbackStub);
175 }
176
AddCredential(AddCredInfo & credInfo,const std::shared_ptr<IDMCallback> & napiCallback)177 void UserIDMClient::AddCredential(AddCredInfo& credInfo, const std::shared_ptr<IDMCallback>& napiCallback)
178 {
179 USERIDM_HILOGD(MODULE_CLIENT, "AddCredential start");
180
181 if (napiCallback == nullptr) {
182 USERIDM_HILOGE(MODULE_CLIENT, "callback is nullptr");
183 return;
184 }
185
186 auto proxy = GetUserIDMProxy();
187 if (proxy == nullptr) {
188 USERIDM_HILOGE(MODULE_CLIENT, "AddCredential proxy is nullptr");
189 return;
190 }
191
192 sptr<IIDMCallback> callbackStub = new UserIDMCallbackStub(napiCallback);
193 proxy->AddCredential(credInfo, callbackStub);
194 }
195
UpdateCredential(AddCredInfo & credInfo,const std::shared_ptr<IDMCallback> & napiCallback)196 void UserIDMClient::UpdateCredential(AddCredInfo& credInfo, const std::shared_ptr<IDMCallback>& napiCallback)
197 {
198 USERIDM_HILOGD(MODULE_CLIENT, "UpdateCredential start");
199
200 if (napiCallback == nullptr) {
201 USERIDM_HILOGE(MODULE_CLIENT, " callback is nullptr");
202 return;
203 }
204
205 auto proxy = GetUserIDMProxy();
206 if (proxy == nullptr) {
207 USERIDM_HILOGE(MODULE_CLIENT, "UpdateCredential proxy is nullptr");
208 return;
209 }
210
211 sptr<IIDMCallback> callbackStub = new UserIDMCallbackStub(napiCallback);
212 proxy->UpdateCredential(credInfo, callbackStub);
213 }
214
Cancel(uint64_t challenge)215 int32_t UserIDMClient::Cancel(uint64_t challenge)
216 {
217 USERIDM_HILOGD(MODULE_CLIENT, "Cancel start");
218
219 auto proxy = GetUserIDMProxy();
220 if (proxy == nullptr) {
221 USERIDM_HILOGE(MODULE_CLIENT, "Cancel proxy is nullptr");
222 return FAIL;
223 }
224
225 return proxy->Cancel(challenge);
226 }
227
EnforceDelUser(int32_t userId,const std::shared_ptr<IDMCallback> & napiCallback)228 int32_t UserIDMClient::EnforceDelUser(int32_t userId, const std::shared_ptr<IDMCallback>& napiCallback)
229 {
230 USERIDM_HILOGD(MODULE_CLIENT, "EnforceDelUser start");
231
232 if (napiCallback == nullptr) {
233 USERIDM_HILOGE(MODULE_CLIENT, " callback is nullptr");
234 return INVALID_PARAMETERS;
235 }
236
237 auto proxy = GetUserIDMProxy();
238 if (proxy == nullptr) {
239 USERIDM_HILOGE(MODULE_CLIENT, "EnforceDelUser proxy is nullptr");
240 return FAIL;
241 }
242
243 sptr<IIDMCallback> callbackStub = new UserIDMCallbackStub(napiCallback);
244 return proxy->EnforceDelUser(userId, callbackStub);
245 }
246
DelUser(std::vector<uint8_t> authToken,const std::shared_ptr<IDMCallback> & napiCallback)247 void UserIDMClient::DelUser(std::vector<uint8_t> authToken, const std::shared_ptr<IDMCallback>& napiCallback)
248 {
249 USERIDM_HILOGD(MODULE_CLIENT, "DelUser start");
250
251 if (napiCallback == nullptr) {
252 USERIDM_HILOGE(MODULE_CLIENT, "callback is nullptr");
253 return;
254 }
255
256 auto proxy = GetUserIDMProxy();
257 if (proxy == nullptr) {
258 USERIDM_HILOGE(MODULE_CLIENT, "DelUser proxy is nullptr");
259 return;
260 }
261
262 sptr<IIDMCallback> callbackStub = new UserIDMCallbackStub(napiCallback);
263 proxy->DelUser(authToken, callbackStub);
264 }
265
DelCred(uint64_t credentialId,std::vector<uint8_t> authToken,const std::shared_ptr<IDMCallback> & napiCallback)266 void UserIDMClient::DelCred(uint64_t credentialId, std::vector<uint8_t> authToken,
267 const std::shared_ptr<IDMCallback>& napiCallback)
268 {
269 USERIDM_HILOGD(MODULE_CLIENT, "DelCred start");
270
271 if (napiCallback == nullptr) {
272 USERIDM_HILOGE(MODULE_CLIENT, "callback is nullptr");
273 return;
274 }
275
276 auto proxy = GetUserIDMProxy();
277 if (proxy == nullptr) {
278 USERIDM_HILOGE(MODULE_CLIENT, "DelCred proxy is nullptr");
279 return;
280 }
281
282 sptr<IIDMCallback> callbackStub = new UserIDMCallbackStub(napiCallback);
283 proxy->DelCred(credentialId, authToken, callbackStub);
284 }
285 } // namespace UserIDM
286 } // namespace UserIAM
287 } // namespace OHOS
288