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 "account_iam_mgr_proxy.h"
17
18 #include "account_error_no.h"
19 #include "account_log_wrapper.h"
20
21 namespace OHOS {
22 namespace AccountSA {
AccountIAMMgrProxy(const sptr<IRemoteObject> & object)23 AccountIAMMgrProxy::AccountIAMMgrProxy(const sptr<IRemoteObject> &object) : IRemoteProxy<IAccountIAM>(object)
24 {}
25
~AccountIAMMgrProxy()26 AccountIAMMgrProxy::~AccountIAMMgrProxy()
27 {}
28
SendRequest(IAccountIAM::Message code,MessageParcel & data,MessageParcel & reply)29 ErrCode AccountIAMMgrProxy::SendRequest(IAccountIAM::Message code, MessageParcel &data, MessageParcel &reply)
30 {
31 sptr<IRemoteObject> remote = Remote();
32 if (remote == nullptr) {
33 ACCOUNT_LOGE("remote is nullptr, code = %{public}d", code);
34 return ERR_ACCOUNT_COMMON_NULL_PTR_ERROR;
35 }
36 MessageOption option(MessageOption::TF_SYNC);
37 int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
38 if (result != ERR_OK) {
39 ACCOUNT_LOGE("failed to SendRequest, code = %{public}d, result = %{public}d", code, result);
40 }
41 return result;
42 }
43
WriteCommonData(MessageParcel & data,int32_t userId)44 bool AccountIAMMgrProxy::WriteCommonData(MessageParcel &data, int32_t userId)
45 {
46 if (!data.WriteInterfaceToken(GetDescriptor())) {
47 ACCOUNT_LOGE("failed to write descriptor!");
48 return false;
49 }
50 if (!data.WriteInt32(userId)) {
51 ACCOUNT_LOGE("failed to write userId!");
52 return false;
53 }
54 return true;
55 }
56
OpenSession(int32_t userId,std::vector<uint8_t> & challenge)57 int32_t AccountIAMMgrProxy::OpenSession(int32_t userId, std::vector<uint8_t> &challenge)
58 {
59 challenge.clear();
60 MessageParcel data;
61 if (!WriteCommonData(data, userId)) {
62 return ERR_ACCOUNT_IAM_KIT_WRITE_PARCEL_FAIL;
63 }
64 MessageParcel reply;
65 int32_t result = SendRequest(IAccountIAM::Message::OPEN_SESSION, data, reply);
66 if (result != ERR_OK) {
67 return result;
68 }
69 if (!reply.ReadInt32(result)) {
70 ACCOUNT_LOGE("failed to read result");
71 return ERR_ACCOUNT_IAM_KIT_READ_PARCEL_FAIL;
72 }
73 if (result != ERR_OK) {
74 ACCOUNT_LOGE("failed to open session, result: %{public}d", result);
75 return result;
76 }
77 if (!reply.ReadUInt8Vector(&challenge)) {
78 ACCOUNT_LOGE("failed to read challenge!");
79 return ERR_ACCOUNT_IAM_KIT_READ_PARCEL_FAIL;
80 }
81 return ERR_OK;
82 }
83
CloseSession(int32_t userId)84 int32_t AccountIAMMgrProxy::CloseSession(int32_t userId)
85 {
86 MessageParcel data;
87 if (!WriteCommonData(data, userId)) {
88 return ERR_ACCOUNT_IAM_KIT_WRITE_PARCEL_FAIL;
89 }
90 MessageParcel reply;
91 int32_t result = SendRequest(IAccountIAM::Message::CLOSE_SESSION, data, reply);
92 if (result != ERR_OK) {
93 return result;
94 }
95 if (!reply.ReadInt32(result)) {
96 ACCOUNT_LOGE("failed to read result");
97 return ERR_ACCOUNT_IAM_KIT_READ_PARCEL_FAIL;
98 }
99 return result;
100 }
101
AddOrUpdateCredential(int32_t userId,const CredentialParameters & credInfo,const sptr<IIDMCallback> & callback,bool isAdd)102 void AccountIAMMgrProxy::AddOrUpdateCredential(
103 int32_t userId, const CredentialParameters &credInfo, const sptr<IIDMCallback> &callback, bool isAdd)
104 {
105 if (callback == nullptr) {
106 ACCOUNT_LOGE("callback is nullptr");
107 return;
108 }
109 Attributes emptyResult;
110 MessageParcel data;
111 if (!WriteCommonData(data, userId)) {
112 callback->OnResult(ERR_ACCOUNT_IAM_KIT_WRITE_PARCEL_FAIL, emptyResult);
113 return;
114 }
115 if (!data.WriteInt32(credInfo.authType)) {
116 ACCOUNT_LOGE("failed to write authType");
117 callback->OnResult(ERR_ACCOUNT_IAM_KIT_WRITE_PARCEL_FAIL, emptyResult);
118 return;
119 }
120 PinSubType pinType = credInfo.pinType.value_or(PinSubType::PIN_MAX);
121 if (!data.WriteInt32(pinType)) {
122 ACCOUNT_LOGE("failed to write pinType");
123 callback->OnResult(ERR_ACCOUNT_IAM_KIT_WRITE_PARCEL_FAIL, emptyResult);
124 return;
125 }
126 if (!data.WriteUInt8Vector(credInfo.token)) {
127 ACCOUNT_LOGE("failed to write token");
128 callback->OnResult(ERR_ACCOUNT_IAM_KIT_WRITE_PARCEL_FAIL, emptyResult);
129 return;
130 }
131 if (!data.WriteRemoteObject(callback->AsObject())) {
132 ACCOUNT_LOGE("failed to write callback");
133 callback->OnResult(ERR_ACCOUNT_IAM_KIT_WRITE_PARCEL_FAIL, emptyResult);
134 return;
135 }
136 MessageParcel reply;
137 int32_t result;
138 if (isAdd) {
139 result = SendRequest(IAccountIAM::Message::ADD_CREDENTIAL, data, reply);
140 } else {
141 result = SendRequest(IAccountIAM::Message::UPDATE_CREDENTIAL, data, reply);
142 }
143 if (result != ERR_OK) {
144 callback->OnResult(result, emptyResult);
145 }
146 }
147
AddCredential(int32_t userId,const CredentialParameters & credInfo,const sptr<IIDMCallback> & callback)148 void AccountIAMMgrProxy::AddCredential(
149 int32_t userId, const CredentialParameters &credInfo, const sptr<IIDMCallback> &callback)
150 {
151 AddOrUpdateCredential(userId, credInfo, callback, true);
152 }
153
UpdateCredential(int32_t userId,const CredentialParameters & credInfo,const sptr<IIDMCallback> & callback)154 void AccountIAMMgrProxy::UpdateCredential(
155 int32_t userId, const CredentialParameters &credInfo, const sptr<IIDMCallback> &callback)
156 {
157 AddOrUpdateCredential(userId, credInfo, callback, false);
158 }
159
Cancel(int32_t userId)160 int32_t AccountIAMMgrProxy::Cancel(int32_t userId)
161 {
162 MessageParcel data;
163 if (!WriteCommonData(data, userId)) {
164 return ERR_ACCOUNT_IAM_KIT_WRITE_PARCEL_FAIL;
165 }
166 MessageParcel reply;
167 int32_t result = SendRequest(IAccountIAM::Message::CANCEL, data, reply);
168 if (result != ERR_OK) {
169 return result;
170 }
171 if (!reply.ReadInt32(result)) {
172 ACCOUNT_LOGE("failed to read result");
173 return ERR_ACCOUNT_IAM_KIT_READ_PARCEL_FAIL;
174 }
175 return result;
176 }
177
DelCred(int32_t userId,uint64_t credentialId,const std::vector<uint8_t> & authToken,const sptr<IIDMCallback> & callback)178 void AccountIAMMgrProxy::DelCred(
179 int32_t userId, uint64_t credentialId, const std::vector<uint8_t> &authToken, const sptr<IIDMCallback> &callback)
180 {
181 if (callback == nullptr) {
182 ACCOUNT_LOGE("callback is nullptr");
183 return;
184 }
185 Attributes emptyResult;
186 MessageParcel data;
187 if (!WriteCommonData(data, userId)) {
188 callback->OnResult(ERR_ACCOUNT_IAM_KIT_WRITE_PARCEL_FAIL, emptyResult);
189 return;
190 }
191 if (!data.WriteUint64(credentialId)) {
192 ACCOUNT_LOGE("failed to write userId");
193 callback->OnResult(ERR_ACCOUNT_IAM_KIT_WRITE_PARCEL_FAIL, emptyResult);
194 return;
195 }
196 if (!data.WriteUInt8Vector(authToken)) {
197 ACCOUNT_LOGE("failed to write token for DelCred");
198 callback->OnResult(ERR_ACCOUNT_IAM_KIT_WRITE_PARCEL_FAIL, emptyResult);
199 return;
200 }
201 if (!data.WriteRemoteObject(callback->AsObject())) {
202 ACCOUNT_LOGE("failed to write callback for DelCred");
203 callback->OnResult(ERR_ACCOUNT_IAM_KIT_WRITE_PARCEL_FAIL, emptyResult);
204 return;
205 }
206 MessageParcel reply;
207 int32_t result = SendRequest(IAccountIAM::Message::DEL_CRED, data, reply);
208 if (result != ERR_OK) {
209 callback->OnResult(result, emptyResult);
210 }
211 }
212
DelUser(int32_t userId,const std::vector<uint8_t> & authToken,const sptr<IIDMCallback> & callback)213 void AccountIAMMgrProxy::DelUser(
214 int32_t userId, const std::vector<uint8_t> &authToken, const sptr<IIDMCallback> &callback)
215 {
216 if (callback == nullptr) {
217 ACCOUNT_LOGE("callback is nullptr");
218 return;
219 }
220 Attributes emptyResult;
221 MessageParcel data;
222 if (!WriteCommonData(data, userId)) {
223 callback->OnResult(ERR_ACCOUNT_IAM_KIT_WRITE_PARCEL_FAIL, emptyResult);
224 return;
225 }
226 if (!data.WriteUInt8Vector(authToken)) {
227 ACCOUNT_LOGE("failed to write token for DelUser");
228 callback->OnResult(ERR_ACCOUNT_IAM_KIT_WRITE_PARCEL_FAIL, emptyResult);
229 return;
230 }
231 if (!data.WriteRemoteObject(callback->AsObject())) {
232 ACCOUNT_LOGE("failed to write callback for DelUser");
233 callback->OnResult(ERR_ACCOUNT_IAM_KIT_WRITE_PARCEL_FAIL, emptyResult);
234 return;
235 }
236 MessageParcel reply;
237 int32_t result = SendRequest(IAccountIAM::Message::DEL_USER, data, reply);
238 if (result != ERR_OK) {
239 callback->OnResult(result, emptyResult);
240 }
241 }
242
GetCredentialInfo(int32_t userId,AuthType authType,const sptr<IGetCredInfoCallback> & callback)243 int32_t AccountIAMMgrProxy::GetCredentialInfo(
244 int32_t userId, AuthType authType, const sptr<IGetCredInfoCallback> &callback)
245 {
246 if (callback == nullptr) {
247 ACCOUNT_LOGE("callback is nullptr");
248 return ERR_APPACCOUNT_KIT_INVALID_PARAMETER;
249 }
250 MessageParcel data;
251 if (!WriteCommonData(data, userId)) {
252 return ERR_ACCOUNT_IAM_KIT_WRITE_PARCEL_FAIL;
253 }
254 if (!data.WriteInt32(authType)) {
255 ACCOUNT_LOGE("failed to write authType");
256 return ERR_ACCOUNT_IAM_KIT_WRITE_PARCEL_FAIL;
257 }
258 if (!data.WriteRemoteObject(callback->AsObject())) {
259 ACCOUNT_LOGE("failed to write callback");
260 return ERR_ACCOUNT_IAM_KIT_WRITE_PARCEL_FAIL;
261 }
262 MessageParcel reply;
263 int32_t result = SendRequest(IAccountIAM::Message::GET_CREDENTIAL_INFO, data, reply);
264 if (result != ERR_OK) {
265 return result;
266 }
267 if (!reply.ReadInt32(result)) {
268 ACCOUNT_LOGE("failed to read result");
269 return ERR_ACCOUNT_IAM_KIT_READ_PARCEL_FAIL;
270 }
271 return result;
272 }
273
AuthUser(int32_t userId,const std::vector<uint8_t> & challenge,AuthType authType,AuthTrustLevel authTrustLevel,const sptr<IIDMCallback> & callback)274 uint64_t AccountIAMMgrProxy::AuthUser(int32_t userId, const std::vector<uint8_t> &challenge, AuthType authType,
275 AuthTrustLevel authTrustLevel, const sptr<IIDMCallback> &callback)
276 {
277 uint64_t contextId = 0;
278 if (callback == nullptr) {
279 ACCOUNT_LOGE("callback is nullptr");
280 return contextId;
281 }
282 MessageParcel data;
283 if (!WriteCommonData(data, userId)) {
284 return contextId;
285 }
286 if (!data.WriteUInt8Vector(challenge)) {
287 ACCOUNT_LOGE("failed to write challenge");
288 return contextId;
289 }
290 if (!data.WriteInt32(authType)) {
291 ACCOUNT_LOGE("failed to write authType");
292 return contextId;
293 }
294 if (!data.WriteUint32(authTrustLevel)) {
295 ACCOUNT_LOGE("failed to write authTrustLevel");
296 return contextId;
297 }
298 if (!data.WriteRemoteObject(callback->AsObject())) {
299 ACCOUNT_LOGE("failed to write callback");
300 return contextId;
301 }
302 MessageParcel reply;
303 if (SendRequest(IAccountIAM::Message::AUTH_USER, data, reply) != ERR_OK) {
304 return contextId;
305 }
306 if (!reply.ReadUint64(contextId)) {
307 ACCOUNT_LOGE("failed to read contextId");
308 }
309 return contextId;
310 }
311
CancelAuth(uint64_t contextId)312 int32_t AccountIAMMgrProxy::CancelAuth(uint64_t contextId)
313 {
314 MessageParcel data;
315 if (!data.WriteInterfaceToken(GetDescriptor())) {
316 ACCOUNT_LOGE("failed to write descriptor");
317 return ERR_ACCOUNT_IAM_KIT_WRITE_PARCEL_FAIL;
318 }
319 if (!data.WriteUint64(contextId)) {
320 ACCOUNT_LOGE("failed to write contextId");
321 return ERR_ACCOUNT_IAM_KIT_WRITE_PARCEL_FAIL;
322 }
323 MessageParcel reply;
324 int32_t result = SendRequest(IAccountIAM::Message::CANCEL_AUTH, data, reply);
325 if (result != ERR_OK) {
326 return result;
327 }
328 if (!reply.ReadInt32(result)) {
329 ACCOUNT_LOGE("failed to read result");
330 return ERR_ACCOUNT_IAM_KIT_READ_PARCEL_FAIL;
331 }
332 return result;
333 }
334
GetAvailableStatus(const AuthType authType,const AuthTrustLevel authTrustLevel,int32_t & status)335 int32_t AccountIAMMgrProxy::GetAvailableStatus(const AuthType authType, const AuthTrustLevel authTrustLevel,
336 int32_t &status)
337 {
338 MessageParcel data;
339 if (!data.WriteInterfaceToken(GetDescriptor())) {
340 ACCOUNT_LOGE("failed to write descriptor");
341 return ERR_ACCOUNT_IAM_KIT_WRITE_PARCEL_FAIL;
342 }
343 if (!data.WriteInt32(authType)) {
344 ACCOUNT_LOGE("failed to write authType");
345 return ERR_ACCOUNT_IAM_KIT_WRITE_PARCEL_FAIL;
346 }
347 if (!data.WriteUint32(authTrustLevel)) {
348 ACCOUNT_LOGE("failed to write authTrustLevel");
349 return ERR_ACCOUNT_IAM_KIT_WRITE_PARCEL_FAIL;
350 }
351 MessageParcel reply;
352 int32_t res = SendRequest(IAccountIAM::Message::GET_AVAILABLE_STATUS, data, reply);
353 if (res != ERR_OK) {
354 return res;
355 }
356 if (!reply.ReadInt32(res)) {
357 ACCOUNT_LOGE("failed to read result");
358 return ERR_ACCOUNT_IAM_KIT_READ_PARCEL_FAIL;
359 }
360 if (res != ERR_OK) {
361 ACCOUNT_LOGE("failed to get available status, result: %{public}d", res);
362 return res;
363 }
364 if (!reply.ReadInt32(status)) {
365 ACCOUNT_LOGE("failed to read status");
366 return ERR_ACCOUNT_IAM_KIT_READ_PARCEL_FAIL;
367 }
368 return ERR_OK;
369 }
370
GetProperty(int32_t userId,const GetPropertyRequest & request,const sptr<IGetSetPropCallback> & callback)371 void AccountIAMMgrProxy::GetProperty(
372 int32_t userId, const GetPropertyRequest &request, const sptr<IGetSetPropCallback> &callback)
373 {
374 if (callback == nullptr) {
375 ACCOUNT_LOGE("get property callback is nullptr");
376 return;
377 }
378 Attributes emptyResult;
379 MessageParcel data;
380 if (!WriteCommonData(data, userId)) {
381 callback->OnResult(ERR_ACCOUNT_IAM_KIT_WRITE_PARCEL_FAIL, emptyResult);
382 return;
383 }
384 if (!data.WriteInt32(request.authType)) {
385 ACCOUNT_LOGE("failed to write authType for GetProperty");
386 callback->OnResult(ERR_ACCOUNT_IAM_KIT_WRITE_PARCEL_FAIL, emptyResult);
387 return;
388 }
389 std::vector<uint32_t> attrKeys;
390 std::transform(request.keys.begin(), request.keys.end(), std::back_inserter(attrKeys),
391 [](const auto &key) { return static_cast<uint32_t>(key); });
392
393 if (!data.WriteUInt32Vector(attrKeys)) {
394 ACCOUNT_LOGE("failed to write keys");
395 callback->OnResult(ERR_ACCOUNT_IAM_KIT_WRITE_PARCEL_FAIL, emptyResult);
396 return;
397 }
398 if (!data.WriteRemoteObject(callback->AsObject())) {
399 ACCOUNT_LOGE("failed to write callback");
400 callback->OnResult(ERR_ACCOUNT_IAM_KIT_WRITE_PARCEL_FAIL, emptyResult);
401 return;
402 }
403 MessageParcel reply;
404 int32_t result = SendRequest(IAccountIAM::Message::GET_PROPERTY, data, reply);
405 if (result != ERR_OK) {
406 callback->OnResult(result, emptyResult);
407 }
408 }
409
SetProperty(int32_t userId,const SetPropertyRequest & request,const sptr<IGetSetPropCallback> & callback)410 void AccountIAMMgrProxy::SetProperty(
411 int32_t userId, const SetPropertyRequest &request, const sptr<IGetSetPropCallback> &callback)
412 {
413 if (callback == nullptr) {
414 ACCOUNT_LOGE("set property callback is nullptr");
415 return;
416 }
417 Attributes emptyResult;
418 MessageParcel data;
419 if (!WriteCommonData(data, userId)) {
420 callback->OnResult(ERR_ACCOUNT_IAM_KIT_WRITE_PARCEL_FAIL, emptyResult);
421 return;
422 }
423 if (!data.WriteInt32(request.authType)) {
424 ACCOUNT_LOGE("failed to write authType for SetProperty");
425 callback->OnResult(ERR_ACCOUNT_IAM_KIT_WRITE_PARCEL_FAIL, emptyResult);
426 return;
427 }
428 auto buffer = request.attrs.Serialize();
429 if (!data.WriteUInt8Vector(buffer)) {
430 ACCOUNT_LOGE("failed to write attributes");
431 callback->OnResult(ERR_ACCOUNT_IAM_KIT_WRITE_PARCEL_FAIL, emptyResult);
432 return;
433 }
434 if (!data.WriteRemoteObject(callback->AsObject())) {
435 ACCOUNT_LOGE("failed to write callback");
436 callback->OnResult(ERR_ACCOUNT_IAM_KIT_WRITE_PARCEL_FAIL, emptyResult);
437 return;
438 }
439 MessageParcel reply;
440 int32_t result = SendRequest(IAccountIAM::Message::SET_PROPERTY, data, reply);
441 if (result != ERR_OK) {
442 callback->OnResult(result, emptyResult);
443 }
444 }
445
GetAccountState(int32_t userId)446 IAMState AccountIAMMgrProxy::GetAccountState(int32_t userId)
447 {
448 IAMState defaultState = IDLE;
449 MessageParcel data;
450 if (!WriteCommonData(data, userId)) {
451 return defaultState;
452 }
453 MessageParcel reply;
454 SendRequest(IAccountIAM::Message::GET_ACCOUNT_STATE, data, reply);
455 int32_t state = defaultState;
456 if (!reply.ReadInt32(state)) {
457 ACCOUNT_LOGE("failed to read state");
458 }
459 return static_cast<IAMState>(state);
460 }
461 } // namespace AccountSA
462 } // namespace OHOS
463