• 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     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 userId");
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 WidgetParamInner & widgetParam,sptr<UserAuthCallbackInterface> & callback,sptr<ModalCallbackInterface> & modalCallback)382 uint64_t UserAuthProxy::AuthWidget(int32_t apiVersion, const AuthParamInner &authParam,
383     const WidgetParamInner &widgetParam, sptr<UserAuthCallbackInterface> &callback,
384     sptr<ModalCallbackInterface> &modalCallback)
385 {
386     if (callback == nullptr) {
387         IAM_LOGE("callback is nullptr");
388         return BAD_CONTEXT_ID;
389     }
390     MessageParcel data;
391     MessageParcel reply;
392 
393     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
394         IAM_LOGE("failed to write descriptor");
395         return BAD_CONTEXT_ID;
396     }
397 
398     if (!WriteWidgetAuthParam(data, authParam)) {
399         IAM_LOGE("failed to write widget auth param");
400         return BAD_CONTEXT_ID;
401     }
402 
403     if (!WriteWidgetParam(data, widgetParam)) {
404         IAM_LOGE("failed to write widget param");
405         return BAD_CONTEXT_ID;
406     }
407 
408     if (!data.WriteRemoteObject(callback->AsObject())) {
409         IAM_LOGE("failed to write callback");
410         return BAD_CONTEXT_ID;
411     }
412 
413     if (!data.WriteRemoteObject(modalCallback->AsObject())) {
414         IAM_LOGE("failed to write modal callback");
415         return BAD_CONTEXT_ID;
416     }
417 
418     if (!data.WriteInt32(apiVersion)) {
419         IAM_LOGE("failed to write apiVersion");
420         return BAD_CONTEXT_ID;
421     }
422 
423     bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_AUTH_WIDGET, data, reply);
424     if (!ret) {
425         IAM_LOGE("failed to send auth widget IPC request");
426         return BAD_CONTEXT_ID;
427     }
428     uint64_t result = BAD_CONTEXT_ID;
429     if (!reply.ReadUint64(result)) {
430         IAM_LOGE("failed to read result");
431     }
432     return result;
433 }
434 
WriteWidgetAuthParam(MessageParcel & data,const AuthParamInner & authParam)435 bool UserAuthProxy::WriteWidgetAuthParam(MessageParcel &data, const AuthParamInner &authParam)
436 {
437     if (!data.WriteInt32(authParam.userId)) {
438         IAM_LOGE("failed to write userId");
439         return false;
440     }
441     if (!data.WriteBool(authParam.isUserIdSpecified)) {
442         IAM_LOGE("failed to write isUserIdSpecified");
443         return false;
444     }
445     if (!data.WriteUInt8Vector(authParam.challenge)) {
446         IAM_LOGE("failed to write challenge");
447         return false;
448     }
449     std::vector<int32_t> atList;
450     for (auto at : authParam.authTypes) {
451         atList.push_back(static_cast<int32_t>(at));
452     }
453     if (!data.WriteInt32Vector(atList)) {
454         IAM_LOGE("failed to write authTypeList");
455         return false;
456     }
457     if (!data.WriteUint32(authParam.authTrustLevel)) {
458         IAM_LOGE("failed to write authTrustLevel");
459         return false;
460     }
461     if (!data.WriteBool(authParam.reuseUnlockResult.isReuse)) {
462         IAM_LOGE("failed to write isReuse unlock result");
463         return false;
464     }
465     if (authParam.reuseUnlockResult.isReuse) {
466         if (!data.WriteUint32(authParam.reuseUnlockResult.reuseMode)) {
467             IAM_LOGE("failed to write reuseMode.");
468             return false;
469         }
470         if (!data.WriteUint64(authParam.reuseUnlockResult.reuseDuration)) {
471             IAM_LOGE("failed to write reuseDuration.");
472             return false;
473         }
474     }
475     return true;
476 }
477 
WriteWidgetParam(MessageParcel & data,const WidgetParamInner & widgetParam)478 bool UserAuthProxy::WriteWidgetParam(MessageParcel &data, const WidgetParamInner &widgetParam)
479 {
480     if (!data.WriteString(widgetParam.title)) {
481         IAM_LOGE("failed to write title");
482         return false;
483     }
484     if (!data.WriteString(widgetParam.navigationButtonText)) {
485         IAM_LOGE("failed to write navigation button text");
486         return false;
487     }
488     if (!data.WriteInt32(static_cast<int32_t>(widgetParam.windowMode))) {
489         IAM_LOGE("failed to write window mode");
490         return false;
491     }
492     if (!data.WriteBool(widgetParam.hasContext)) {
493         IAM_LOGE("failed to write hasContext");
494         return false;
495     }
496     return true;
497 }
498 
AuthUser(AuthParamInner & param,std::optional<RemoteAuthParam> & remoteAuthParam,sptr<UserAuthCallbackInterface> & callback)499 uint64_t UserAuthProxy::AuthUser(AuthParamInner &param, std::optional<RemoteAuthParam> &remoteAuthParam,
500     sptr<UserAuthCallbackInterface> &callback)
501 {
502     if (callback == nullptr) {
503         IAM_LOGE("callback is nullptr");
504         return BAD_CONTEXT_ID;
505     }
506     MessageParcel data;
507     MessageParcel reply;
508 
509     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
510         IAM_LOGE("failed to write descriptor");
511         return BAD_CONTEXT_ID;
512     }
513 
514     if (!WriteAuthParam(data, param)) {
515         IAM_LOGE("failed to write auth param");
516         return BAD_CONTEXT_ID;
517     }
518 
519     if (!WriteRemoteAuthParam(data, remoteAuthParam)) {
520         IAM_LOGE("failed to write remote auth param");
521         return BAD_CONTEXT_ID;
522     }
523 
524     if (!data.WriteRemoteObject(callback->AsObject())) {
525         IAM_LOGE("failed to write callback");
526         return BAD_CONTEXT_ID;
527     }
528 
529     bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_AUTH_USER, data, reply);
530     if (!ret) {
531         IAM_LOGE("failed to send auth user IPC request");
532         return BAD_CONTEXT_ID;
533     }
534     uint64_t result = BAD_CONTEXT_ID;
535     if (!reply.ReadUint64(result)) {
536         IAM_LOGE("failed to read result");
537     }
538     return result;
539 }
540 
Identify(const std::vector<uint8_t> & challenge,AuthType authType,sptr<UserAuthCallbackInterface> & callback)541 uint64_t UserAuthProxy::Identify(const std::vector<uint8_t> &challenge, AuthType authType,
542     sptr<UserAuthCallbackInterface> &callback)
543 {
544     if (callback == nullptr) {
545         IAM_LOGE("callback is nullptr");
546         return BAD_CONTEXT_ID;
547     }
548     MessageParcel data;
549     MessageParcel reply;
550 
551     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
552         IAM_LOGE("failed to write descriptor");
553         return BAD_CONTEXT_ID;
554     }
555     if (!data.WriteUInt8Vector(challenge)) {
556         IAM_LOGE("failed to write challenge");
557         return BAD_CONTEXT_ID;
558     }
559     if (!data.WriteInt32(authType)) {
560         IAM_LOGE("failed to write authType");
561         return BAD_CONTEXT_ID;
562     }
563     if (!data.WriteRemoteObject(callback->AsObject())) {
564         IAM_LOGE("failed to write callback");
565         return BAD_CONTEXT_ID;
566     }
567 
568     bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_IDENTIFY, data, reply);
569     if (!ret) {
570         IAM_LOGE("failed to send auth identify IPC request");
571         return BAD_CONTEXT_ID;
572     }
573     uint64_t result = BAD_CONTEXT_ID;
574     if (!reply.ReadUint64(result)) {
575         IAM_LOGE("failed to read result");
576     }
577     return result;
578 }
579 
CancelAuthOrIdentify(uint64_t contextId,int32_t cancelReason)580 int32_t UserAuthProxy::CancelAuthOrIdentify(uint64_t contextId, int32_t cancelReason)
581 {
582     MessageParcel data;
583     MessageParcel reply;
584 
585     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
586         IAM_LOGE("failed to write descriptor");
587         return GENERAL_ERROR;
588     }
589     if (!data.WriteUint64(contextId)) {
590         IAM_LOGE("failed to write contextId");
591         return GENERAL_ERROR;
592     }
593     if (!data.WriteInt32(cancelReason)) {
594         IAM_LOGE("failed to write cancelReason");
595         return GENERAL_ERROR;
596     }
597 
598     bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_CANCEL_AUTH, data, reply);
599     if (!ret) {
600         IAM_LOGE("failed to send cancel auth IPC request");
601         return GENERAL_ERROR;
602     }
603     int32_t result = GENERAL_ERROR;
604     if (!reply.ReadInt32(result)) {
605         IAM_LOGE("failed to read result");
606     }
607     return result;
608 }
609 
GetVersion(int32_t & version)610 int32_t UserAuthProxy::GetVersion(int32_t &version)
611 {
612     version = 0;
613     MessageParcel data;
614     MessageParcel reply;
615 
616     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
617         IAM_LOGE("failed to write descriptor");
618         return GENERAL_ERROR;
619     }
620 
621     bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_GET_VERSION, data, reply);
622     if (!ret) {
623         IAM_LOGE("failed to send get version IPC request");
624         return GENERAL_ERROR;
625     }
626     if (!reply.ReadInt32(version)) {
627         IAM_LOGE("failed to read version");
628         return GENERAL_ERROR;
629     }
630     int32_t result = GENERAL_ERROR;
631     if (!reply.ReadInt32(result)) {
632         IAM_LOGE("failed to read result");
633     }
634     return result;
635 }
636 
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)637 bool UserAuthProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply)
638 {
639     IAM_LOGI("code = %{public}u", code);
640     sptr<IRemoteObject> remote = Remote();
641     if (remote == nullptr) {
642         IAM_LOGE("failed to get remote");
643         return false;
644     }
645     MessageOption option(MessageOption::TF_SYNC);
646     int32_t result = remote->SendRequest(code, data, reply, option);
647     if (result != OHOS::NO_ERROR) {
648         IAM_LOGE("failed to send request, result = %{public}d", result);
649         return false;
650     }
651     return true;
652 }
653 
Notice(NoticeType noticeType,const std::string & eventData)654 int32_t UserAuthProxy::Notice(NoticeType noticeType, const std::string &eventData)
655 {
656     MessageParcel data;
657     MessageParcel reply;
658 
659     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
660         IAM_LOGE("failed to write descriptor");
661         return ResultCode::WRITE_PARCEL_ERROR;
662     }
663 
664     int32_t type = static_cast<int32_t>(noticeType);
665     if (!data.WriteInt32(type)) {
666         IAM_LOGE("failed to write noticeType");
667         return ResultCode::WRITE_PARCEL_ERROR;
668     }
669     if (!data.WriteString(eventData)) {
670         IAM_LOGE("failed to write eventData");
671         return ResultCode::WRITE_PARCEL_ERROR;
672     }
673 
674     bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_NOTICE, data, reply);
675     if (!ret) {
676         IAM_LOGE("failed to send user notice IPC request");
677         return ResultCode::GENERAL_ERROR;
678     }
679     int32_t result = ResultCode::GENERAL_ERROR;
680     if (!reply.ReadInt32(result)) {
681         IAM_LOGE("failed to read result");
682         return ResultCode::READ_PARCEL_ERROR;
683     }
684     return result;
685 }
686 
RegisterWidgetCallback(int32_t version,sptr<WidgetCallbackInterface> & callback)687 int32_t UserAuthProxy::RegisterWidgetCallback(int32_t version, sptr<WidgetCallbackInterface> &callback)
688 {
689     if (callback == nullptr) {
690         IAM_LOGE("callback is nullptr");
691         return GENERAL_ERROR;
692     }
693     MessageParcel data;
694     MessageParcel reply;
695 
696     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
697         IAM_LOGE("failed to write descriptor");
698         return GENERAL_ERROR;
699     }
700 
701     if (!data.WriteInt32(version)) {
702         IAM_LOGE("failed to write version");
703         return WRITE_PARCEL_ERROR;
704     }
705 
706     if (!data.WriteRemoteObject(callback->AsObject())) {
707         IAM_LOGE("failed to write callback");
708         return GENERAL_ERROR;
709     }
710 
711     bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_REG_WIDGET_CB, data, reply);
712     if (!ret) {
713         IAM_LOGE("failed to send register widget callback IPC request");
714         return GENERAL_ERROR;
715     }
716     int32_t result = GENERAL_ERROR;
717     if (!reply.ReadInt32(result)) {
718         IAM_LOGE("failed to read result");
719         return READ_PARCEL_ERROR;
720     }
721     return result;
722 }
723 
GetEnrolledState(int32_t apiVersion,AuthType authType,EnrolledState & enrolledState)724 int32_t UserAuthProxy::GetEnrolledState(int32_t apiVersion, AuthType authType, EnrolledState &enrolledState)
725 {
726     MessageParcel data;
727     MessageParcel reply;
728 
729     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
730         IAM_LOGE("failed to write descriptor");
731         return WRITE_PARCEL_ERROR;
732     }
733 
734     if (!data.WriteInt32(apiVersion)) {
735         IAM_LOGE("failed to write apiVersion");
736         return WRITE_PARCEL_ERROR;
737     }
738     if (!data.WriteInt32(authType)) {
739         IAM_LOGE("failed to write authType");
740         return WRITE_PARCEL_ERROR;
741     }
742 
743     bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_GET_ENROLLED_STATE, data, reply);
744     if (!ret) {
745         IAM_LOGE("get enrolled state failed to send request");
746         return GENERAL_ERROR;
747     }
748 
749     int32_t result = GENERAL_ERROR;
750     if (!reply.ReadInt32(result)) {
751         IAM_LOGE("failed to read result");
752         return READ_PARCEL_ERROR;
753     }
754     if (result != SUCCESS) {
755         IAM_LOGE("failed to get enrolled state");
756         return result;
757     }
758     uint64_t credentialDigest;
759     if (!reply.ReadUint64(credentialDigest)) {
760         IAM_LOGE("failed to read result");
761         return READ_PARCEL_ERROR;
762     }
763     uint16_t credentialCount;
764     if (!reply.ReadUint16(credentialCount)) {
765         IAM_LOGE("failed to read result");
766         return READ_PARCEL_ERROR;
767     }
768     enrolledState.credentialDigest = credentialDigest;
769     enrolledState.credentialCount = credentialCount;
770     return SUCCESS;
771 }
772 
RegistUserAuthSuccessEventListener(const std::vector<AuthType> & authType,const sptr<AuthEventListenerInterface> & listener)773 int32_t UserAuthProxy::RegistUserAuthSuccessEventListener(const std::vector<AuthType> &authType,
774     const sptr<AuthEventListenerInterface> &listener)
775 {
776     if (listener == nullptr) {
777         IAM_LOGE("listener is nullptr");
778         return GENERAL_ERROR;
779     }
780 
781     MessageParcel data;
782     MessageParcel reply;
783     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
784         IAM_LOGE("failed to write descriptor");
785         return WRITE_PARCEL_ERROR;
786     }
787 
788     std::vector<int32_t> authTypeList;
789     for (auto &iter : authType) {
790         authTypeList.emplace_back(static_cast<int32_t>(iter));
791     }
792 
793     if (!data.WriteInt32Vector(authTypeList)) {
794         IAM_LOGE("failed to write authType");
795         return WRITE_PARCEL_ERROR;
796     }
797 
798     if (!data.WriteRemoteObject(listener->AsObject())) {
799         IAM_LOGE("failed to write listener");
800         return WRITE_PARCEL_ERROR;
801     }
802 
803     bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_REG_EVENT_LISTENER, data, reply);
804     if (!ret) {
805         IAM_LOGE("failed to send register event listener callback IPC request");
806         return GENERAL_ERROR;
807     }
808 
809     int32_t result = GENERAL_ERROR;
810     if (!reply.ReadInt32(result)) {
811         IAM_LOGE("failed to read result");
812         return READ_PARCEL_ERROR;
813     }
814     return result;
815 }
816 
UnRegistUserAuthSuccessEventListener(const sptr<AuthEventListenerInterface> & listener)817 int32_t UserAuthProxy::UnRegistUserAuthSuccessEventListener(const sptr<AuthEventListenerInterface> &listener)
818 {
819     if (listener == nullptr) {
820         IAM_LOGE("listener is nullptr");
821         return GENERAL_ERROR;
822     }
823 
824     MessageParcel data;
825     MessageParcel reply;
826     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
827         IAM_LOGE("failed to write descriptor");
828         return WRITE_PARCEL_ERROR;
829     }
830 
831     if (!data.WriteRemoteObject(listener->AsObject())) {
832         IAM_LOGE("failed to write listener");
833         return WRITE_PARCEL_ERROR;
834     }
835 
836     bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_UNREG_EVENT_LISTENER, data, reply);
837     if (!ret) {
838         IAM_LOGE("failed to send register event listener callback IPC request");
839         return GENERAL_ERROR;
840     }
841 
842     int32_t result = GENERAL_ERROR;
843     if (!reply.ReadInt32(result)) {
844         IAM_LOGE("failed to read result");
845         return READ_PARCEL_ERROR;
846     }
847     return result;
848 }
849 
WriteGlobalConfigValue(MessageParcel & data,const GlobalConfigParam & param)850 ResultCode UserAuthProxy::WriteGlobalConfigValue(MessageParcel &data, const GlobalConfigParam &param)
851 {
852     switch (param.type) {
853         case GlobalConfigType::PIN_EXPIRED_PERIOD:
854             if (!data.WriteInt64(param.value.pinExpiredPeriod)) {
855                 IAM_LOGE("failed to write GlobalConfigParam pinExpiredPeriod");
856                 return WRITE_PARCEL_ERROR;
857             }
858             break;
859         case GlobalConfigType::ENABLE_STATUS :
860             if (!data.WriteBool(param.value.enableStatus)) {
861                 IAM_LOGE("failed to write GlobalConfigParam enableStatus");
862                 return WRITE_PARCEL_ERROR;
863             }
864             break;
865         default:
866             IAM_LOGE("GlobalConfigType not support.");
867             return INVALID_PARAMETERS;
868     }
869     return SUCCESS;
870 }
871 
872 
SetGlobalConfigParam(const GlobalConfigParam & param)873 int32_t UserAuthProxy::SetGlobalConfigParam(const GlobalConfigParam &param)
874 {
875     MessageParcel data;
876     MessageParcel reply;
877     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
878         IAM_LOGE("failed to write descriptor");
879         return WRITE_PARCEL_ERROR;
880     }
881     if (!data.WriteInt32(param.type)) {
882         IAM_LOGE("failed to write GlobalConfigParam type");
883         return WRITE_PARCEL_ERROR;
884     }
885     int32_t result = WriteGlobalConfigValue(data, param);
886     if (result != SUCCESS) {
887         IAM_LOGE("failed to WriteGlobalConfigValue");
888         return result;
889     }
890     if (!data.WriteInt32Vector(param.userIds)) {
891         IAM_LOGE("failed to write userIds");
892         return WRITE_PARCEL_ERROR;
893     }
894     std::vector<int32_t> authTypeList;
895     for (const auto &authType : param.authTypes) {
896         authTypeList.push_back(static_cast<int32_t>(authType));
897     }
898     if (!data.WriteInt32Vector(authTypeList)) {
899         IAM_LOGE("failed to write authTypeList");
900         return WRITE_PARCEL_ERROR;
901     }
902     bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_SET_CLOBAL_CONFIG_PARAM, data, reply);
903     if (!ret) {
904         IAM_LOGE("failed to set global config param IPC request");
905         return GENERAL_ERROR;
906     }
907     result = GENERAL_ERROR;
908     if (!reply.ReadInt32(result)) {
909         IAM_LOGE("failed to read result");
910         return READ_PARCEL_ERROR;
911     }
912     return result;
913 }
914 
PrepareRemoteAuth(const std::string & networkId,sptr<UserAuthCallbackInterface> & callback)915 int32_t UserAuthProxy::PrepareRemoteAuth(const std::string &networkId, sptr<UserAuthCallbackInterface> &callback)
916 {
917     if (callback == nullptr) {
918         IAM_LOGE("callback is nullptr");
919         return GENERAL_ERROR;
920     }
921     MessageParcel data;
922     MessageParcel reply;
923 
924     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
925         IAM_LOGE("failed to write descriptor");
926         return GENERAL_ERROR;
927     }
928     if (!data.WriteString(networkId)) {
929         IAM_LOGE("failed to write networkId");
930         return GENERAL_ERROR;
931     }
932     if (!data.WriteRemoteObject(callback->AsObject())) {
933         IAM_LOGE("failed to write callback");
934         return GENERAL_ERROR;
935     }
936 
937     bool ret = SendRequest(UserAuthInterfaceCode::USER_AUTH_PREPARE_REMOTE_AUTH, data, reply);
938     if (!ret) {
939         IAM_LOGE("failed to send prepare remote auth IPC request");
940         return GENERAL_ERROR;
941     }
942     int32_t result = GENERAL_ERROR;
943     if (!reply.ReadInt32(result)) {
944         IAM_LOGE("failed to read result");
945         return READ_PARCEL_ERROR;
946     }
947     return result;
948 }
949 
VerifyAuthToken(const std::vector<uint8_t> & tokenIn,uint64_t allowableDuration,const sptr<VerifyTokenCallbackInterface> & callback)950 void UserAuthProxy::VerifyAuthToken(const std::vector<uint8_t> &tokenIn, uint64_t allowableDuration,
951     const sptr<VerifyTokenCallbackInterface> &callback)
952 {
953     if (callback == nullptr) {
954         IAM_LOGE("callback is nullptr");
955         return;
956     }
957     MessageParcel data;
958     MessageParcel reply;
959     Attributes extraInfo;
960 
961     if (!data.WriteInterfaceToken(UserAuthProxy::GetDescriptor())) {
962         IAM_LOGE("failed to write descriptor");
963         callback->OnVerifyTokenResult(WRITE_PARCEL_ERROR, extraInfo);
964         return;
965     }
966     if (!data.WriteUInt8Vector(tokenIn)) {
967         IAM_LOGE("failed to write tokenIn");
968         callback->OnVerifyTokenResult(WRITE_PARCEL_ERROR, extraInfo);
969         return;
970     }
971     if (!data.WriteUint64(allowableDuration)) {
972         IAM_LOGE("failed to write allowableDuration");
973         callback->OnVerifyTokenResult(WRITE_PARCEL_ERROR, extraInfo);
974         return;
975     }
976     if (!data.WriteRemoteObject(callback->AsObject())) {
977         IAM_LOGE("failed to write callback");
978         callback->OnVerifyTokenResult(WRITE_PARCEL_ERROR, extraInfo);
979         return;
980     }
981 
982     bool ret = SendRequest(UserAuthInterfaceCode::USER_ACCESS_CTRL_VERIFY_AUTH_TOKEN, data, reply);
983     if (!ret) {
984         IAM_LOGE("failed to send verify token IPC request");
985         callback->OnVerifyTokenResult(GENERAL_ERROR, extraInfo);
986     }
987 }
988 } // namespace UserAuth
989 } // namespace UserIam
990 } // namespace OHOS