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