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_proxy.h"
17 
18 #include <algorithm>
19 #include <cinttypes>
20 
21 #include "iam_logger.h"
22 
23 #define LOG_TAG "USER_AUTH_SDK"
24 
25 namespace OHOS {
26 namespace UserIam {
27 namespace UserAuth {
28 namespace {
29     const uint32_t MAX_ATTR_COUNT = 512;
30 } // namespace
31 
UserAuthProxy(const sptr<IRemoteObject> & object)32 UserAuthProxy::UserAuthProxy(const sptr<IRemoteObject> &object) : IRemoteProxy<UserAuthInterface>(object)
33 {
34 }
35 
GetAvailableStatus(int32_t apiVersion,int32_t userId,AuthType authType,AuthTrustLevel authTrustLevel)36 int32_t UserAuthProxy::GetAvailableStatus(int32_t apiVersion, int32_t userId, AuthType authType,
37     AuthTrustLevel authTrustLevel)
38 {
39     MessageParcel data;
40     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
41         IAM_LOGE("failed to write descriptor");
42         return WRITE_PARCEL_ERROR;
43     }
44     bool isSpecificUserId = true;
45     if (!data.WriteBool(isSpecificUserId)) {
46         IAM_LOGE("failed to write isSpecificUserId");
47         return WRITE_PARCEL_ERROR;
48     }
49     if (!data.WriteInt32(userId)) {
50         IAM_LOGE("failed to write userId");
51         return WRITE_PARCEL_ERROR;
52     }
53     return GetAvailableStatusInner(apiVersion, authType, authTrustLevel, data);
54 }
55 
GetAvailableStatus(int32_t apiVersion,AuthType authType,AuthTrustLevel authTrustLevel)56 int32_t UserAuthProxy::GetAvailableStatus(int32_t apiVersion, AuthType authType, AuthTrustLevel authTrustLevel)
57 {
58     MessageParcel data;
59     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
60         IAM_LOGE("failed to write descriptor");
61         return WRITE_PARCEL_ERROR;
62     }
63     bool isSpecificUserId = false;
64     if (!data.WriteBool(isSpecificUserId)) {
65         IAM_LOGE("failed to write isSpecificUserId");
66         return WRITE_PARCEL_ERROR;
67     }
68     return GetAvailableStatusInner(apiVersion, authType, authTrustLevel, data);
69 }
70 
GetAvailableStatusInner(int32_t apiVersion,AuthType authType,AuthTrustLevel authTrustLevel,MessageParcel & data)71 int32_t UserAuthProxy::GetAvailableStatusInner(int32_t apiVersion, AuthType authType, AuthTrustLevel authTrustLevel,
72     MessageParcel &data)
73 {
74     if (!data.WriteInt32(authType)) {
75         IAM_LOGE("failed to write authType");
76         return WRITE_PARCEL_ERROR;
77     }
78     if (!data.WriteUint32(authTrustLevel)) {
79         IAM_LOGE("failed to write authTrustLevel");
80         return WRITE_PARCEL_ERROR;
81     }
82     if (!data.WriteInt32(apiVersion)) {
83         IAM_LOGE("failed to write apiVersion");
84         return WRITE_PARCEL_ERROR;
85     }
86 
87     MessageParcel reply;
88     bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_GET_AVAILABLE_STATUS, data, reply);
89     if (!ret) {
90         IAM_LOGE("failed to send get available status IPC request");
91         return GENERAL_ERROR;
92     }
93     int32_t result = SUCCESS;
94     if (!reply.ReadInt32(result)) {
95         IAM_LOGE("failed to read result");
96         return READ_PARCEL_ERROR;
97     }
98     return result;
99 }
100 
GetProperty(int32_t userId,AuthType authType,const std::vector<Attributes::AttributeKey> & keys,sptr<GetExecutorPropertyCallbackInterface> & callback)101 void UserAuthProxy::GetProperty(int32_t userId, AuthType authType,
102     const std::vector<Attributes::AttributeKey> &keys, sptr<GetExecutorPropertyCallbackInterface> &callback)
103 {
104     if (callback == nullptr) {
105         IAM_LOGE("callback is nullptr");
106         return;
107     }
108     MessageParcel data;
109     MessageParcel reply;
110 
111     if (keys.empty() || keys.size() > MAX_ATTR_COUNT) {
112         IAM_LOGE("the attribute key vector is bad param, key size:%{public}zu", keys.size());
113         Attributes attr;
114         callback->OnGetExecutorPropertyResult(INVALID_PARAMETERS, attr);
115         return;
116     }
117 
118     std::vector<uint32_t> attrKeys;
119     attrKeys.resize(keys.size());
120     std::transform(keys.begin(), keys.end(), attrKeys.begin(), [](Attributes::AttributeKey key) {
121         return static_cast<uint32_t>(key);
122     });
123 
124     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
125         IAM_LOGE("failed to write descriptor");
126         return;
127     }
128     if (!data.WriteInt32(userId)) {
129         IAM_LOGE("failed to write userId");
130         return;
131     }
132     if (!data.WriteInt32(authType)) {
133         IAM_LOGE("failed to write authType");
134         return;
135     }
136     if (!data.WriteUInt32Vector(attrKeys)) {
137         IAM_LOGE("failed to write keys");
138         return;
139     }
140     if (!data.WriteRemoteObject(callback->AsObject())) {
141         IAM_LOGE("failed to write callback");
142         return;
143     }
144 
145     bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_GET_PROPERTY, data, reply);
146     if (!ret) {
147         IAM_LOGE("failed to send get property IPC request");
148         Attributes attr;
149         callback->OnGetExecutorPropertyResult(GENERAL_ERROR, attr);
150     }
151 }
152 
GetPropertyById(uint64_t credentialId,const std::vector<Attributes::AttributeKey> & keys,sptr<GetExecutorPropertyCallbackInterface> & callback)153 void UserAuthProxy::GetPropertyById(uint64_t credentialId, const std::vector<Attributes::AttributeKey> &keys,
154     sptr<GetExecutorPropertyCallbackInterface> &callback)
155 {
156     if (callback == nullptr) {
157         IAM_LOGE("callback is nullptr");
158         return;
159     }
160     MessageParcel data;
161     MessageParcel reply;
162 
163     if (keys.empty() || keys.size() > MAX_ATTR_COUNT) {
164         IAM_LOGE("the attribute key vector is bad param, key size:%{public}zu", keys.size());
165         Attributes attr;
166         callback->OnGetExecutorPropertyResult(INVALID_PARAMETERS, attr);
167         return;
168     }
169 
170     std::vector<uint32_t> attrKeys;
171     attrKeys.resize(keys.size());
172     std::transform(keys.begin(), keys.end(), attrKeys.begin(), [](Attributes::AttributeKey key) {
173         return static_cast<uint32_t>(key);
174     });
175 
176     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
177         IAM_LOGE("failed to write descriptor");
178         return;
179     }
180     if (!data.WriteUint64(credentialId)) {
181         IAM_LOGE("failed to write credentialId");
182         return;
183     }
184     if (!data.WriteUInt32Vector(attrKeys)) {
185         IAM_LOGE("failed to write keys");
186         return;
187     }
188     if (!data.WriteRemoteObject(callback->AsObject())) {
189         IAM_LOGE("failed to write callback");
190         return;
191     }
192 
193     bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_GET_PROPERTY_BY_ID, data, reply);
194     if (!ret) {
195         IAM_LOGE("failed to send get propertyById IPC request");
196         Attributes attr;
197         callback->OnGetExecutorPropertyResult(GENERAL_ERROR, attr);
198     }
199 }
200 
SetProperty(int32_t userId,AuthType authType,const Attributes & attributes,sptr<SetExecutorPropertyCallbackInterface> & callback)201 void UserAuthProxy::SetProperty(int32_t userId, AuthType authType, const Attributes &attributes,
202     sptr<SetExecutorPropertyCallbackInterface> &callback)
203 {
204     if (callback == nullptr) {
205         IAM_LOGE("callback is nullptr");
206         return;
207     }
208     MessageParcel data;
209     MessageParcel reply;
210 
211     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
212         IAM_LOGE("failed to write descriptor");
213         return;
214     }
215     if (!data.WriteInt32(userId)) {
216         IAM_LOGE("failed to write userId");
217         return;
218     }
219     if (!data.WriteInt32(authType)) {
220         IAM_LOGE("failed to write authType");
221         return;
222     }
223     auto buffer = attributes.Serialize();
224     if (!data.WriteUInt8Vector(buffer)) {
225         IAM_LOGE("failed to write attributes");
226         return;
227     }
228     if (!data.WriteRemoteObject(callback->AsObject())) {
229         IAM_LOGE("failed to write callback");
230         return;
231     }
232 
233     bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_SET_PROPERTY, data, reply);
234     if (!ret) {
235         IAM_LOGE("failed to send set property IPC request");
236         callback->OnSetExecutorPropertyResult(GENERAL_ERROR);
237     }
238 }
239 
WriteAuthParam(MessageParcel & data,const AuthParamInner & authParam)240 bool UserAuthProxy::WriteAuthParam(MessageParcel &data, const AuthParamInner &authParam)
241 {
242     if (!data.WriteInt32(authParam.userId)) {
243         IAM_LOGE("failed to write authType");
244         return false;
245     }
246     if (!data.WriteUInt8Vector(authParam.challenge)) {
247         IAM_LOGE("failed to write challenge");
248         return false;
249     }
250     if (!data.WriteInt32(authParam.authType)) {
251         IAM_LOGE("failed to write authType");
252         return false;
253     }
254     std::vector<int32_t> atList;
255     for (auto at : authParam.authTypes) {
256         atList.push_back(static_cast<int32_t>(at));
257     }
258     if (!data.WriteInt32Vector(atList)) {
259         IAM_LOGE("failed to write authTypeList");
260         return false;
261     }
262     if (!data.WriteUint32(authParam.authTrustLevel)) {
263         IAM_LOGE("failed to write authTrustLevel");
264         return false;
265     }
266     if (!data.WriteUint32(authParam.authIntent)) {
267         IAM_LOGE("failed to write authIntent");
268         return false;
269     }
270 
271     return true;
272 }
273 
WriteRemoteAuthParam(MessageParcel & data,const std::optional<RemoteAuthParam> & remoteAuthParam)274 bool UserAuthProxy::WriteRemoteAuthParam(MessageParcel &data, const std::optional<RemoteAuthParam> &remoteAuthParam)
275 {
276     if (!data.WriteBool(remoteAuthParam.has_value())) {
277         IAM_LOGE("failed to write remoteAuthParam has value");
278         return false;
279     }
280 
281     if (!remoteAuthParam.has_value()) {
282         return true;
283     }
284 
285     if (!WriteOptionalString(data, remoteAuthParam.value().verifierNetworkId)) {
286         IAM_LOGE("failed to write verifierNetworkId");
287         return false;
288     }
289 
290     if (!WriteOptionalString(data, remoteAuthParam.value().collectorNetworkId)) {
291         IAM_LOGE("failed to write collectorNetworkId");
292         return false;
293     }
294 
295     if (!WriteOptionalUint32(data, remoteAuthParam.value().collectorTokenId)) {
296         IAM_LOGE("failed to write collectorTokenId");
297         return false;
298     }
299 
300     return true;
301 }
302 
WriteOptionalString(MessageParcel & data,const std::optional<std::string> & str)303 bool UserAuthProxy::WriteOptionalString(MessageParcel &data, const std::optional<std::string> &str)
304 {
305     if (!data.WriteBool(str.has_value())) {
306         IAM_LOGE("failed to write str has value");
307         return false;
308     }
309 
310     if (str.has_value()) {
311         if (!data.WriteString(str.value())) {
312             IAM_LOGE("failed to write str");
313             return false;
314         }
315     }
316     return true;
317 }
318 
WriteOptionalUint32(MessageParcel & data,const std::optional<uint32_t> & val)319 bool UserAuthProxy::WriteOptionalUint32(MessageParcel &data, const std::optional<uint32_t> &val)
320 {
321     if (!data.WriteBool(val.has_value())) {
322         IAM_LOGE("failed to write val has value");
323         return false;
324     }
325 
326     if (val.has_value()) {
327         if (!data.WriteUint32(val.value())) {
328             IAM_LOGE("failed to write val");
329             return false;
330         }
331     }
332 
333     return true;
334 }
335 
Auth(int32_t apiVersion,const std::vector<uint8_t> & challenge,AuthType authType,AuthTrustLevel authTrustLevel,sptr<UserAuthCallbackInterface> & callback)336 uint64_t UserAuthProxy::Auth(int32_t apiVersion, const std::vector<uint8_t> &challenge, AuthType authType,
337     AuthTrustLevel authTrustLevel, sptr<UserAuthCallbackInterface> &callback)
338 {
339     if (callback == nullptr) {
340         IAM_LOGE("callback is nullptr");
341         return BAD_CONTEXT_ID;
342     }
343     MessageParcel data;
344     MessageParcel reply;
345 
346     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
347         IAM_LOGE("failed to write descriptor");
348         return BAD_CONTEXT_ID;
349     }
350     if (!data.WriteInt32(apiVersion)) {
351         IAM_LOGE("failed to write apiVersion");
352         return BAD_CONTEXT_ID;
353     }
354     AuthParamInner authParam = {
355         .userId = 0,
356         .challenge = challenge,
357         .authType = authType,
358         .authTrustLevel = authTrustLevel,
359     };
360     if (!WriteAuthParam(data, authParam)) {
361         IAM_LOGE("failed to write auth param");
362         return BAD_CONTEXT_ID;
363     }
364 
365     if (!data.WriteRemoteObject(callback->AsObject())) {
366         IAM_LOGE("failed to write callback");
367         return BAD_CONTEXT_ID;
368     }
369 
370     bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_AUTH, data, reply);
371     if (!ret) {
372         IAM_LOGE("failed to send user auth IPC request");
373         return BAD_CONTEXT_ID;
374     }
375     uint64_t result = BAD_CONTEXT_ID;
376     if (!reply.ReadUint64(result)) {
377         IAM_LOGE("failed to read result");
378     }
379     return result;
380 }
381 
AuthWidget(int32_t apiVersion,const AuthParamInner & authParam,const WidgetParam & widgetParam,sptr<UserAuthCallbackInterface> & callback)382 uint64_t UserAuthProxy::AuthWidget(int32_t apiVersion, const AuthParamInner &authParam,
383     const WidgetParam &widgetParam, sptr<UserAuthCallbackInterface> &callback)
384 {
385     if (callback == nullptr) {
386         IAM_LOGE("callback is nullptr");
387         return BAD_CONTEXT_ID;
388     }
389     MessageParcel data;
390     MessageParcel reply;
391 
392     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
393         IAM_LOGE("failed to write descriptor");
394         return BAD_CONTEXT_ID;
395     }
396 
397     if (!WriteWidgetAuthParam(data, authParam)) {
398         IAM_LOGE("failed to write widget auth param");
399         return BAD_CONTEXT_ID;
400     }
401 
402     if (!WriteWidgetParam(data, widgetParam)) {
403         IAM_LOGE("failed to write widget param");
404         return BAD_CONTEXT_ID;
405     }
406 
407     if (!data.WriteRemoteObject(callback->AsObject())) {
408         IAM_LOGE("failed to write callback");
409         return BAD_CONTEXT_ID;
410     }
411 
412     if (!data.WriteInt32(apiVersion)) {
413         IAM_LOGE("failed to write apiVersion");
414         return BAD_CONTEXT_ID;
415     }
416 
417     bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_AUTH_WIDGET, data, reply);
418     if (!ret) {
419         IAM_LOGE("failed to send auth widget IPC request");
420         return BAD_CONTEXT_ID;
421     }
422     uint64_t result = BAD_CONTEXT_ID;
423     if (!reply.ReadUint64(result)) {
424         IAM_LOGE("failed to read result");
425     }
426     return result;
427 }
428 
WriteWidgetAuthParam(MessageParcel & data,const AuthParamInner & authParam)429 bool UserAuthProxy::WriteWidgetAuthParam(MessageParcel &data, const AuthParamInner &authParam)
430 {
431     if (!data.WriteInt32(authParam.userId)) {
432         IAM_LOGE("failed to write userId");
433         return false;
434     }
435     if (!data.WriteBool(authParam.isUserIdSpecified)) {
436         IAM_LOGE("failed to write isUserIdSpecified");
437         return false;
438     }
439     if (!data.WriteUInt8Vector(authParam.challenge)) {
440         IAM_LOGE("failed to write challenge");
441         return false;
442     }
443     std::vector<int32_t> atList;
444     for (auto at : authParam.authTypes) {
445         atList.push_back(static_cast<int32_t>(at));
446     }
447     if (!data.WriteInt32Vector(atList)) {
448         IAM_LOGE("failed to write authTypeList");
449         return false;
450     }
451     if (!data.WriteUint32(authParam.authTrustLevel)) {
452         IAM_LOGE("failed to write authTrustLevel");
453         return false;
454     }
455     if (!data.WriteBool(authParam.reuseUnlockResult.isReuse)) {
456         IAM_LOGE("failed to write isReuse unlock result");
457         return false;
458     }
459     if (authParam.reuseUnlockResult.isReuse) {
460         if (!data.WriteUint32(authParam.reuseUnlockResult.reuseMode)) {
461             IAM_LOGE("failed to write reuseMode.");
462             return false;
463         }
464         if (!data.WriteUint64(authParam.reuseUnlockResult.reuseDuration)) {
465             IAM_LOGE("failed to write reuseDuration.");
466             return false;
467         }
468     }
469     return true;
470 }
471 
WriteWidgetParam(MessageParcel & data,const WidgetParam & widgetParam)472 bool UserAuthProxy::WriteWidgetParam(MessageParcel &data, const WidgetParam &widgetParam)
473 {
474     if (!data.WriteString(widgetParam.title)) {
475         IAM_LOGE("failed to write title");
476         return false;
477     }
478     if (!data.WriteString(widgetParam.navigationButtonText)) {
479         IAM_LOGE("failed to write navigation button text");
480         return false;
481     }
482     if (!data.WriteInt32(static_cast<int32_t>(widgetParam.windowMode))) {
483         IAM_LOGE("failed to write window mode");
484         return false;
485     }
486     return true;
487 }
488 
AuthUser(AuthParamInner & param,std::optional<RemoteAuthParam> & remoteAuthParam,sptr<UserAuthCallbackInterface> & callback)489 uint64_t UserAuthProxy::AuthUser(AuthParamInner &param, std::optional<RemoteAuthParam> &remoteAuthParam,
490     sptr<UserAuthCallbackInterface> &callback)
491 {
492     if (callback == nullptr) {
493         IAM_LOGE("callback is nullptr");
494         return BAD_CONTEXT_ID;
495     }
496     MessageParcel data;
497     MessageParcel reply;
498 
499     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
500         IAM_LOGE("failed to write descriptor");
501         return BAD_CONTEXT_ID;
502     }
503 
504     if (!WriteAuthParam(data, param)) {
505         IAM_LOGE("failed to write auth param");
506         return BAD_CONTEXT_ID;
507     }
508 
509     if (!WriteRemoteAuthParam(data, remoteAuthParam)) {
510         IAM_LOGE("failed to write remote auth param");
511         return BAD_CONTEXT_ID;
512     }
513 
514     if (!data.WriteRemoteObject(callback->AsObject())) {
515         IAM_LOGE("failed to write callback");
516         return BAD_CONTEXT_ID;
517     }
518 
519     bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_AUTH_USER, data, reply);
520     if (!ret) {
521         IAM_LOGE("failed to send auth user IPC request");
522         return BAD_CONTEXT_ID;
523     }
524     uint64_t result = BAD_CONTEXT_ID;
525     if (!reply.ReadUint64(result)) {
526         IAM_LOGE("failed to read result");
527     }
528     return result;
529 }
530 
Identify(const std::vector<uint8_t> & challenge,AuthType authType,sptr<UserAuthCallbackInterface> & callback)531 uint64_t UserAuthProxy::Identify(const std::vector<uint8_t> &challenge, AuthType authType,
532     sptr<UserAuthCallbackInterface> &callback)
533 {
534     if (callback == nullptr) {
535         IAM_LOGE("callback is nullptr");
536         return BAD_CONTEXT_ID;
537     }
538     MessageParcel data;
539     MessageParcel reply;
540 
541     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
542         IAM_LOGE("failed to write descriptor");
543         return BAD_CONTEXT_ID;
544     }
545     if (!data.WriteUInt8Vector(challenge)) {
546         IAM_LOGE("failed to write challenge");
547         return BAD_CONTEXT_ID;
548     }
549     if (!data.WriteInt32(authType)) {
550         IAM_LOGE("failed to write authType");
551         return BAD_CONTEXT_ID;
552     }
553     if (!data.WriteRemoteObject(callback->AsObject())) {
554         IAM_LOGE("failed to write callback");
555         return BAD_CONTEXT_ID;
556     }
557 
558     bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_IDENTIFY, data, reply);
559     if (!ret) {
560         IAM_LOGE("failed to send auth identify IPC request");
561         return BAD_CONTEXT_ID;
562     }
563     uint64_t result = BAD_CONTEXT_ID;
564     if (!reply.ReadUint64(result)) {
565         IAM_LOGE("failed to read result");
566     }
567     return result;
568 }
569 
CancelAuthOrIdentify(uint64_t contextId)570 int32_t UserAuthProxy::CancelAuthOrIdentify(uint64_t contextId)
571 {
572     MessageParcel data;
573     MessageParcel reply;
574 
575     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
576         IAM_LOGE("failed to write descriptor");
577         return GENERAL_ERROR;
578     }
579     if (!data.WriteUint64(contextId)) {
580         IAM_LOGE("failed to write contextId");
581         return GENERAL_ERROR;
582     }
583 
584     bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_CANCEL_AUTH, data, reply);
585     if (!ret) {
586         IAM_LOGE("failed to send cancel auth IPC request");
587         return GENERAL_ERROR;
588     }
589     int32_t result = GENERAL_ERROR;
590     if (!reply.ReadInt32(result)) {
591         IAM_LOGE("failed to read result");
592     }
593     return result;
594 }
595 
GetVersion(int32_t & version)596 int32_t UserAuthProxy::GetVersion(int32_t &version)
597 {
598     version = 0;
599     MessageParcel data;
600     MessageParcel reply;
601 
602     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
603         IAM_LOGE("failed to write descriptor");
604         return GENERAL_ERROR;
605     }
606 
607     bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_GET_VERSION, data, reply);
608     if (!ret) {
609         IAM_LOGE("failed to send get version IPC request");
610         return GENERAL_ERROR;
611     }
612     if (!reply.ReadInt32(version)) {
613         IAM_LOGE("failed to read version");
614         return GENERAL_ERROR;
615     }
616     int32_t result = GENERAL_ERROR;
617     if (!reply.ReadInt32(result)) {
618         IAM_LOGE("failed to read result");
619     }
620     return result;
621 }
622 
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)623 bool UserAuthProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply)
624 {
625     IAM_LOGI("code = %{public}u", code);
626     sptr<IRemoteObject> remote = Remote();
627     if (remote == nullptr) {
628         IAM_LOGE("failed to get remote");
629         return false;
630     }
631     MessageOption option(MessageOption::TF_SYNC);
632     int32_t result = remote->SendRequest(code, data, reply, option);
633     if (result != OHOS::NO_ERROR) {
634         IAM_LOGE("failed to send request, result = %{public}d", result);
635         return false;
636     }
637     return true;
638 }
639 
Notice(NoticeType noticeType,const std::string & eventData)640 int32_t UserAuthProxy::Notice(NoticeType noticeType, const std::string &eventData)
641 {
642     MessageParcel data;
643     MessageParcel reply;
644 
645     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
646         IAM_LOGE("failed to write descriptor");
647         return ResultCode::WRITE_PARCEL_ERROR;
648     }
649 
650     int32_t type = static_cast<int32_t>(noticeType);
651     if (!data.WriteInt32(type)) {
652         IAM_LOGE("failed to write noticeType");
653         return ResultCode::WRITE_PARCEL_ERROR;
654     }
655     if (!data.WriteString(eventData)) {
656         IAM_LOGE("failed to write eventData");
657         return ResultCode::WRITE_PARCEL_ERROR;
658     }
659 
660     bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_NOTICE, data, reply);
661     if (!ret) {
662         IAM_LOGE("failed to send user notice IPC request");
663         return ResultCode::GENERAL_ERROR;
664     }
665     int32_t result = ResultCode::GENERAL_ERROR;
666     if (!reply.ReadInt32(result)) {
667         IAM_LOGE("failed to read result");
668         return ResultCode::READ_PARCEL_ERROR;
669     }
670     return result;
671 }
672 
RegisterWidgetCallback(int32_t version,sptr<WidgetCallbackInterface> & callback)673 int32_t UserAuthProxy::RegisterWidgetCallback(int32_t version, sptr<WidgetCallbackInterface> &callback)
674 {
675     if (callback == nullptr) {
676         IAM_LOGE("callback is nullptr");
677         return GENERAL_ERROR;
678     }
679     MessageParcel data;
680     MessageParcel reply;
681 
682     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
683         IAM_LOGE("failed to write descriptor");
684         return GENERAL_ERROR;
685     }
686 
687     if (!data.WriteInt32(version)) {
688         IAM_LOGE("failed to write version");
689         return WRITE_PARCEL_ERROR;
690     }
691 
692     if (!data.WriteRemoteObject(callback->AsObject())) {
693         IAM_LOGE("failed to write callback");
694         return GENERAL_ERROR;
695     }
696 
697     bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_REG_WIDGET_CB, data, reply);
698     if (!ret) {
699         IAM_LOGE("failed to send register widget callback IPC request");
700         return GENERAL_ERROR;
701     }
702     int32_t result = GENERAL_ERROR;
703     if (!reply.ReadInt32(result)) {
704         IAM_LOGE("failed to read result");
705         return READ_PARCEL_ERROR;
706     }
707     return result;
708 }
709 
GetEnrolledState(int32_t apiVersion,AuthType authType,EnrolledState & enrolledState)710 int32_t UserAuthProxy::GetEnrolledState(int32_t apiVersion, AuthType authType, EnrolledState &enrolledState)
711 {
712     MessageParcel data;
713     MessageParcel reply;
714 
715     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
716         IAM_LOGE("failed to write descriptor");
717         return WRITE_PARCEL_ERROR;
718     }
719 
720     if (!data.WriteInt32(apiVersion)) {
721         IAM_LOGE("failed to write apiVersion");
722         return WRITE_PARCEL_ERROR;
723     }
724     if (!data.WriteInt32(authType)) {
725         IAM_LOGE("failed to write authType");
726         return WRITE_PARCEL_ERROR;
727     }
728 
729     bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_GET_ENROLLED_STATE, data, reply);
730     if (!ret) {
731         IAM_LOGE("get enrolled state failed to send request");
732         return GENERAL_ERROR;
733     }
734 
735     int32_t result = GENERAL_ERROR;
736     if (!reply.ReadInt32(result)) {
737         IAM_LOGE("failed to read result");
738         return READ_PARCEL_ERROR;
739     }
740     if (result != SUCCESS) {
741         IAM_LOGE("failed to get enrolled state");
742         return result;
743     }
744     uint64_t credentialDigest;
745     if (!reply.ReadUint64(credentialDigest)) {
746         IAM_LOGE("failed to read result");
747         return READ_PARCEL_ERROR;
748     }
749     uint16_t credentialCount;
750     if (!reply.ReadUint16(credentialCount)) {
751         IAM_LOGE("failed to read result");
752         return READ_PARCEL_ERROR;
753     }
754     enrolledState.credentialDigest = credentialDigest;
755     enrolledState.credentialCount = credentialCount;
756     return SUCCESS;
757 }
758 
RegistUserAuthSuccessEventListener(const std::vector<AuthType> & authType,const sptr<AuthEventListenerInterface> & listener)759 int32_t UserAuthProxy::RegistUserAuthSuccessEventListener(const std::vector<AuthType> &authType,
760     const sptr<AuthEventListenerInterface> &listener)
761 {
762     if (listener == nullptr) {
763         IAM_LOGE("listener is nullptr");
764         return GENERAL_ERROR;
765     }
766 
767     MessageParcel data;
768     MessageParcel reply;
769     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
770         IAM_LOGE("failed to write descriptor");
771         return WRITE_PARCEL_ERROR;
772     }
773 
774     std::vector<int32_t> authTypeList;
775     for (auto &iter : authType) {
776         authTypeList.emplace_back(static_cast<int32_t>(iter));
777     }
778 
779     if (!data.WriteInt32Vector(authTypeList)) {
780         IAM_LOGE("failed to write authType");
781         return WRITE_PARCEL_ERROR;
782     }
783 
784     if (!data.WriteRemoteObject(listener->AsObject())) {
785         IAM_LOGE("failed to write listener");
786         return WRITE_PARCEL_ERROR;
787     }
788 
789     bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_REG_EVENT_LISTENER, data, reply);
790     if (!ret) {
791         IAM_LOGE("failed to send register event listener callback IPC request");
792         return GENERAL_ERROR;
793     }
794 
795     int32_t result = GENERAL_ERROR;
796     if (!reply.ReadInt32(result)) {
797         IAM_LOGE("failed to read result");
798         return READ_PARCEL_ERROR;
799     }
800     return result;
801 }
802 
UnRegistUserAuthSuccessEventListener(const sptr<AuthEventListenerInterface> & listener)803 int32_t UserAuthProxy::UnRegistUserAuthSuccessEventListener(const sptr<AuthEventListenerInterface> &listener)
804 {
805     if (listener == nullptr) {
806         IAM_LOGE("listener is nullptr");
807         return GENERAL_ERROR;
808     }
809 
810     MessageParcel data;
811     MessageParcel reply;
812     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
813         IAM_LOGE("failed to write descriptor");
814         return WRITE_PARCEL_ERROR;
815     }
816 
817     if (!data.WriteRemoteObject(listener->AsObject())) {
818         IAM_LOGE("failed to write listener");
819         return WRITE_PARCEL_ERROR;
820     }
821 
822     bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_UNREG_EVENT_LISTENER, data, reply);
823     if (!ret) {
824         IAM_LOGE("failed to send register event listener callback IPC request");
825         return GENERAL_ERROR;
826     }
827 
828     int32_t result = GENERAL_ERROR;
829     if (!reply.ReadInt32(result)) {
830         IAM_LOGE("failed to read result");
831         return READ_PARCEL_ERROR;
832     }
833     return result;
834 }
835 
SetGlobalConfigParam(const GlobalConfigParam & param)836 int32_t UserAuthProxy::SetGlobalConfigParam(const GlobalConfigParam &param)
837 {
838     MessageParcel data;
839     MessageParcel reply;
840     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
841         IAM_LOGE("failed to write descriptor");
842         return WRITE_PARCEL_ERROR;
843     }
844     if (!data.WriteInt32(param.type)) {
845         IAM_LOGE("failed to write GlobalConfigParam type");
846         return WRITE_PARCEL_ERROR;
847     }
848     if (param.type == GlobalConfigType::PIN_EXPIRED_PERIOD) {
849         IAM_LOGI("GlobalConfigType is pin expired period");
850         if (!data.WriteInt64(param.value.pinExpiredPeriod)) {
851             IAM_LOGE("failed to write GlobalConfigParam pinExpiredPeriod");
852             return WRITE_PARCEL_ERROR;
853         }
854     }
855 
856     bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_SET_CLOBAL_CONFIG_PARAM, data, reply);
857     if (!ret) {
858         IAM_LOGE("failed to set global config param IPC request");
859         return GENERAL_ERROR;
860     }
861     int32_t result = GENERAL_ERROR;
862     if (!reply.ReadInt32(result)) {
863         IAM_LOGE("failed to read result");
864         return READ_PARCEL_ERROR;
865     }
866     return result;
867 }
868 
PrepareRemoteAuth(const std::string & networkId,sptr<UserAuthCallbackInterface> & callback)869 int32_t UserAuthProxy::PrepareRemoteAuth(const std::string &networkId, sptr<UserAuthCallbackInterface> &callback)
870 {
871     if (callback == nullptr) {
872         IAM_LOGE("callback is nullptr");
873         return GENERAL_ERROR;
874     }
875     MessageParcel data;
876     MessageParcel reply;
877 
878     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
879         IAM_LOGE("failed to write descriptor");
880         return GENERAL_ERROR;
881     }
882     if (!data.WriteString(networkId)) {
883         IAM_LOGE("failed to write networkId");
884         return GENERAL_ERROR;
885     }
886     if (!data.WriteRemoteObject(callback->AsObject())) {
887         IAM_LOGE("failed to write callback");
888         return GENERAL_ERROR;
889     }
890 
891     bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_PREPARE_REMOTE_AUTH, data, reply);
892     if (!ret) {
893         IAM_LOGE("failed to send prepare remote auth IPC request");
894         return GENERAL_ERROR;
895     }
896     int32_t result = GENERAL_ERROR;
897     if (!reply.ReadInt32(result)) {
898         IAM_LOGE("failed to read result");
899         return READ_PARCEL_ERROR;
900     }
901     return result;
902 }
903 } // namespace UserAuth
904 } // namespace UserIam
905 } // namespace OHOS