• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2024-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 "cj_lambda.h"
17 #include "user_auth_client_impl.h"
18 
19 #include "user_auth_ffi.h"
20 
21 using namespace OHOS::UserIam::UserAuth;
22 
FfiUserAuthGetAvailableStatus(const uint32_t authType,const uint32_t authTrustLevel)23 int32_t FfiUserAuthGetAvailableStatus(const uint32_t authType, const uint32_t authTrustLevel)
24 {
25     constexpr int32_t API_VERSION_9 = 9;
26     return UserAuthClientImpl::Instance().GetNorthAvailableStatus(API_VERSION_9, AuthType(authType),
27         AuthTrustLevel(authTrustLevel));
28 }
29 
FfiUserAuthGetEnrolledState(const uint32_t authType,EnrolledState * enrolledState)30 int32_t FfiUserAuthGetEnrolledState(const uint32_t authType, EnrolledState *enrolledState)
31 {
32     constexpr int32_t API_VERSION_12 = 12;
33     return UserAuthClientImpl::Instance().GetEnrolledState(API_VERSION_12, AuthType(authType), *enrolledState);
34 }
35 
FfiUserAuthNewCb(void (* const callback)(CjUserAuthResult))36 CjUserAuthCallback *FfiUserAuthNewCb(void (*const callback)(CjUserAuthResult))
37 {
38     return new CjUserAuthCallback(CJLambda::Create(callback));
39 }
40 
FfiUserAuthDeleteCb(const CjUserAuthCallback * callbackPtr)41 void FfiUserAuthDeleteCb(const CjUserAuthCallback *callbackPtr)
42 {
43     delete callbackPtr;
44 }
45 
FfiUserAuthStart(const CjAuthParam & authParam,const CjWidgetParam & widgetParam,CjUserAuthCallback * callbackPtr)46 uint64_t FfiUserAuthStart(const CjAuthParam &authParam, const CjWidgetParam &widgetParam,
47     CjUserAuthCallback *callbackPtr)
48 {
49     constexpr int32_t API_VERSION_10 = 10;
50     std::vector<AuthType> authTypes;
51     for (int i = 0; i < authParam.authTypesLen; ++i) {
52         authTypes.push_back(AuthType(authParam.authTypes[i]));
53     }
54     WidgetAuthParam authParamInner{
55         .userId = INVALID_USER_ID,
56         .challenge = std::vector<uint8_t>(authParam.challenge, authParam.challenge + authParam.challengeLen),
57         .authTypes = authTypes,
58         .authTrustLevel = AuthTrustLevel(authParam.authTrustLevel),
59     };
60     if (authParam.isReuse) {
61         authParamInner.reuseUnlockResult = {
62             .isReuse = true,
63             .reuseMode = ReuseMode(authParam.reuseMode),
64             .reuseDuration = authParam.reuseDuration,
65         };
66     }
67     WidgetParam widgetInner = {
68         .title = widgetParam.title,
69         .navigationButtonText = widgetParam.navigationButtonText ? widgetParam.navigationButtonText : nullptr,
70         .windowMode = WindowModeType::UNKNOWN_WINDOW_MODE,
71     };
72     if (callbackPtr == nullptr) {
73         return UserAuthClientImpl::Instance().BeginWidgetAuth(API_VERSION_10, authParamInner, widgetInner,
74                                                               std::make_shared<CjUserAuthCallback>());
75     }
76     const auto callback = std::shared_ptr<CjUserAuthCallback>(
77         callbackPtr, [](CjUserAuthCallback *) {
78              // don't free, resource will be freed in FfiUserAuthDeleteCb
79         });
80     return UserAuthClientImpl::Instance().BeginWidgetAuth(API_VERSION_10, authParamInner, widgetInner, callback);
81 }
82 
FfiUserAuthCancel(const uint64_t contextId)83 int32_t FfiUserAuthCancel(const uint64_t contextId)
84 {
85     return UserAuthClientImpl::GetInstance().CancelAuthentication(contextId);
86 }