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 #ifndef OS_ACCOUNT_INTERFACES_KITS_NAPI_IAM_INCLUDE_NAPI_IAM_COMMON_H
17 #define OS_ACCOUNT_INTERFACES_KITS_NAPI_IAM_INCLUDE_NAPI_IAM_COMMON_H
18 
19 #include "account_error_no.h"
20 #include "account_iam_client_callback.h"
21 #include "account_iam_info.h"
22 #ifdef HAS_PIN_AUTH_PART
23 #include "i_inputer.h"
24 #endif
25 #include "napi/native_api.h"
26 #include "napi/native_node_api.h"
27 #include "napi_account_common.h"
28 #include "napi_account_iam_constant.h"
29 
30 namespace OHOS {
31 namespace AccountJsKit {
32 using namespace OHOS::AccountSA;
33 
34 constexpr size_t ARG_SIZE_ONE = 1;
35 constexpr size_t ARG_SIZE_TWO = 2;
36 constexpr size_t ARG_SIZE_THREE = 3;
37 constexpr size_t ARG_SIZE_FOUR = 4;
38 constexpr size_t ARG_SIZE_FIVE = 5;
39 
40 constexpr size_t PARAM_ZERO = 0;
41 constexpr size_t PARAM_ONE = 1;
42 constexpr size_t PARAM_TWO = 2;
43 constexpr size_t PARAM_THREE = 3;
44 constexpr size_t PARAM_FOUR = 4;
45 constexpr size_t PARAM_FIVE = 5;
46 
47 int32_t AccountIAMConvertToJSErrCode(int32_t errCode);
48 
49 struct JsIAMCallback {
JsIAMCallbackJsIAMCallback50     JsIAMCallback(napi_env env) : env(env) {}
~JsIAMCallbackJsIAMCallback51     ~JsIAMCallback()
52     {
53         ReleaseNapiRefArray(env, {onResult, onAcquireInfo});
54     }
55     bool onResultCalled = false;
56     napi_env env;
57     napi_ref onResult = nullptr;
58     napi_ref onAcquireInfo = nullptr;
59     bool hasOnAcquireInfo = false;
60 };
61 
62 struct ExecutorProperty {
63     int32_t result = 0;
64     int32_t authSubType = 0;
65     int32_t remainTimes = 0;
66     int32_t freezingTime = 0;
67     int32_t nextPhaseFreezingTime = -1;
68     std::string enrollmentProgress;
69     std::string sensorInfo;
70 };
71 
72 struct CommonCallbackInfo {
CommonCallbackInfoCommonCallbackInfo73     CommonCallbackInfo(napi_env env) : env(env) {}
74     napi_env env;
75     napi_ref callbackRef = nullptr;
76     napi_deferred deferred = nullptr;
77     napi_value errJs = nullptr;
78     napi_value dataJs = nullptr;
79     int32_t errCode = 0;
80 };
81 
82 #ifdef HAS_USER_AUTH_PART
83 struct IDMCallbackParam : public CommonAsyncContext {
IDMCallbackParamIDMCallbackParam84     explicit IDMCallbackParam(napi_env napiEnv) : CommonAsyncContext(napiEnv) {};
85     std::shared_ptr<JsIAMCallback> callback;
86     int32_t result = 0;
87     int32_t module = 0;
88     uint32_t acquire = 0;
89     uint64_t credentialId = 0;
90     std::vector<uint8_t> extraInfo;
91 };
92 
93 struct AuthCallbackParam : public CommonAsyncContext {
AuthCallbackParamAuthCallbackParam94     explicit AuthCallbackParam(napi_env napiEnv) : CommonAsyncContext(napiEnv) {};
95     int32_t module = 0;
96     uint32_t acquireInfo = 0;
97     std::vector<uint8_t> extraInfo;
98     int32_t resultCode = 0;
99     int32_t remainTimes = -1;
100     int32_t freezingTime = -1;
101     std::vector<uint8_t> token;
102     std::shared_ptr<JsIAMCallback> callback;
103     bool hasNextPhaseFreezingTime = false;
104     bool hasCredentialId = false;
105     bool hasAccountId = false;
106     bool hasPinValidityPeriod = false;
107     int32_t nextPhaseFreezingTime = -1;
108     uint64_t credentialId = 0;
109     int32_t accountId = -1;
110     int64_t pinValidityPeriod = -1;
111 };
112 
113 struct AuthContext {
114     int32_t userId = -1;
115     int32_t authType = -1;
116     int32_t trustLevel = -1;
117     bool throwErr = true;
118     bool parseHasAccountId = false;
119     std::vector<uint8_t> challenge;
120     AccountSA::AuthOptions authOptions;
121     std::shared_ptr<AccountSA::IDMCallback> callback;
122 };
123 
124 struct PrepareRemoteAuthContext : public CommonAsyncContext {
PrepareRemoteAuthContextPrepareRemoteAuthContext125     explicit PrepareRemoteAuthContext(napi_env napiEnv) : CommonAsyncContext(napiEnv) {};
126     int32_t result = 0;
127     std::string remoteNetworkId;
128 };
129 
130 class NapiPrepareRemoteAuthCallback : public AccountSA::PreRemoteAuthCallback {
131 public:
132     explicit NapiPrepareRemoteAuthCallback(napi_env env, napi_ref callbackRef, napi_deferred deferred);
133     virtual ~NapiPrepareRemoteAuthCallback();
134 
135     void OnResult(int32_t result) override;
136 
137 private:
138     napi_env env_ = nullptr;
139     napi_ref callbackRef_ = nullptr;
140     napi_deferred deferred_ = nullptr;
141     std::mutex mutex_;
142 };
143 
144 struct IDMContext : public CommonAsyncContext {
IDMContextIDMContext145     IDMContext(napi_env napiEnv) : CommonAsyncContext(napiEnv) {};
146     bool throwErr = true;
147     std::vector<uint8_t> challenge;
148     uint64_t credentialId = 0;
149     int32_t accountId = -1;
150     bool parseHasAccountId = false;
151     std::vector<uint8_t> token;
152     AccountSA::CredentialParameters addCredInfo;
153     std::shared_ptr<JsIAMCallback> callback;
154 };
155 
156 struct GetAuthInfoContext : public CommonAsyncContext {
GetAuthInfoContextGetAuthInfoContext157     explicit GetAuthInfoContext(napi_env napiEnv) : CommonAsyncContext(napiEnv) {};
158     int32_t accountId = -1;
159     bool parseHasAccountId = false;
160     AccountSA::AuthType authType {0};
161     std::vector<AccountSA::CredentialInfo> credInfo;
162     std::shared_ptr<NapiCallbackRef> callback;
163 };
164 
165 struct GetEnrolledIdContext : public CommonAsyncContext {
GetEnrolledIdContextGetEnrolledIdContext166     explicit GetEnrolledIdContext(napi_env napiEnv) : CommonAsyncContext(napiEnv) {};
167     int32_t accountId = -1;
168     bool parseHasAccountId = false;
169     AccountSA::AuthType authType {0};
170     uint64_t enrolledId = 0;
171 };
172 
173 struct GetPropertyCommonContext : public CommonAsyncContext {
GetPropertyCommonContextGetPropertyCommonContext174     explicit GetPropertyCommonContext(napi_env napiEnv) : CommonAsyncContext(napiEnv) {};
175     bool isGetById = false;
176     std::vector<Attributes::AttributeKey> keys {};
177     ExecutorProperty propertyInfo;
178     std::shared_ptr<NapiCallbackRef> callback;
179 };
180 
181 struct GetPropertyContext : public GetPropertyCommonContext {
GetPropertyContextGetPropertyContext182     explicit GetPropertyContext(napi_env napiEnv) : GetPropertyCommonContext(napiEnv) {};
183     AccountSA::GetPropertyRequest request;
184     int32_t accountId = -1;
185     bool parseHasAccountId = false;
186 };
187 
188 struct GetPropertyByIdContext : public GetPropertyCommonContext {
GetPropertyByIdContextGetPropertyByIdContext189     explicit GetPropertyByIdContext(napi_env napiEnv) : GetPropertyCommonContext(napiEnv) {};
190     uint8_t *credentialIdData = nullptr;
191     size_t credentialIdLength = 0;
192     uint64_t credentialId = 0;
193 };
194 
195 struct SetPropertyContext : public CommonAsyncContext {
SetPropertyContextSetPropertyContext196     explicit SetPropertyContext(napi_env napiEnv) : CommonAsyncContext(napiEnv) {};
197     AccountSA::SetPropertyRequest request;
198     int32_t result = 0;
199     int32_t accountId = -1;
200     std::shared_ptr<NapiCallbackRef> callback;
201 };
202 
203 class NapiIDMCallback : public AccountSA::IDMCallback {
204 public:
205     NapiIDMCallback(napi_env env, const std::shared_ptr<JsIAMCallback> &callback);
206     virtual ~NapiIDMCallback();
207 
208     void OnResult(int32_t result, const AccountSA::Attributes &extraInfo) override;
209     void OnAcquireInfo(int32_t module, uint32_t acquireInfo, const AccountSA::Attributes &extraInfo) override;
210 
211 private:
212     napi_env env_;
213     std::shared_ptr<JsIAMCallback> callback_;
214     std::mutex mutex_;
215 };
216 
217 class NapiGetInfoCallback : public AccountSA::GetCredInfoCallback {
218 public:
219     explicit NapiGetInfoCallback(napi_env env, napi_ref callbackRef, napi_deferred deferred);
220     virtual ~NapiGetInfoCallback();
221 
222     void OnCredentialInfo(int32_t result, const std::vector<AccountSA::CredentialInfo> &infoList) override;
223 
224 private:
225     napi_env env_;
226     std::shared_ptr<NapiCallbackRef> callback_;
227     napi_deferred deferred_;
228     std::mutex mutex_;
229     bool onResultCalled_ = false;
230 };
231 
232 class NapiGetEnrolledIdCallback : public AccountSA::GetEnrolledIdCallback {
233 public:
234     explicit NapiGetEnrolledIdCallback(napi_env env, napi_deferred deferred);
235     virtual ~NapiGetEnrolledIdCallback();
236 
237     void OnEnrolledId(int32_t result, uint64_t enrolledId) override;
238 private:
239     napi_env env_;
240     napi_deferred deferred_;
241 };
242 
243 class NapiUserAuthCallback : public AccountSA::IDMCallback {
244 public:
245     NapiUserAuthCallback(napi_env env, const std::shared_ptr<JsIAMCallback> &callback);
246     virtual ~NapiUserAuthCallback();
247 
248     void OnResult(int32_t result, const AccountSA::Attributes &extraInfo) override;
249     void OnAcquireInfo(int32_t module, uint32_t acquireInfo, const AccountSA::Attributes &extraInfo) override;
250 
251 private:
252     void PrepareAuthResult(int32_t result, const AccountSA::Attributes &extraInfo, AuthCallbackParam &param);
253 
254 private:
255     napi_env env_;
256     std::shared_ptr<JsIAMCallback> callback_;
257     std::mutex mutex_;
258 };
259 
260 class NapiGetPropCallback : public AccountSA::GetSetPropCallback {
261 public:
262     NapiGetPropCallback(
263         napi_env env, napi_ref callbackRef, napi_deferred deferred, const std::vector<Attributes::AttributeKey> &keys);
264     virtual ~NapiGetPropCallback();
265     void GetExecutorPropertys(const UserIam::UserAuth::Attributes &extraInfo, ExecutorProperty &propertyInfo);
266     void OnResult(int32_t result, const AccountSA::Attributes &extraInfo) override;
267 
268 public:
269     bool isGetById_ = false;
270 
271 private:
272     napi_env env_ = nullptr;
273     std::shared_ptr<NapiCallbackRef> callback_;
274     napi_deferred deferred_ = nullptr;
275     std::vector<Attributes::AttributeKey> keys_ {};
276     std::mutex mutex_;
277     bool onResultCalled_ = false;
278 };
279 
280 class NapiSetPropCallback : public AccountSA::GetSetPropCallback {
281 public:
282     explicit NapiSetPropCallback(napi_env env, napi_ref callbackRef, napi_deferred deferred);
283     virtual ~NapiSetPropCallback();
284 
285     void OnResult(int32_t result, const AccountSA::Attributes &extraInfo) override;
286 
287 private:
288     napi_env env_ = nullptr;
289     std::shared_ptr<NapiCallbackRef> callback_;
290     napi_deferred deferred_ = nullptr;
291     std::mutex mutex_;
292     bool onResultCalled_ = false;
293 };
294 #endif  // HAS_USER_AUTH_PART
295 
296 #ifdef HAS_PIN_AUTH_PART
297 struct InputerContext : public CommonAsyncContext {
298     int32_t authSubType = -1;
299     std::vector<uint8_t> challenge;
300     std::shared_ptr<AccountSA::IInputerData> inputerData = nullptr;
301     std::shared_ptr<NapiCallbackRef> callback;
302 };
303 
304 class NapiGetDataCallback : public AccountSA::IInputer {
305 public:
306     NapiGetDataCallback(napi_env env, const std::shared_ptr<NapiCallbackRef> &callback);
307     virtual ~NapiGetDataCallback();
308 
309     void OnGetData(int32_t authSubType, std::vector<uint8_t> challenge,
310         const std::shared_ptr<AccountSA::IInputerData> inputerData) override;
311 
312 private:
313     napi_env env_;
314     std::shared_ptr<NapiCallbackRef> callback_;
315     ThreadLockInfo lockInfo_;
316 };
317 #endif  // HAS_PIN_AUTH_PART
318 
319 void CallbackAsyncOrPromise(const CommonCallbackInfo &callbackInfo);
320 void CallbackAsyncOrPromise(napi_env env, CommonAsyncContext *context, napi_value errJs, napi_value dataJs);
321 napi_value CreateErrorObject(napi_env env, int32_t code);
322 napi_status ParseUInt32Array(napi_env env, napi_value value, std::vector<uint32_t> &data);
323 napi_status ParseIAMCallback(napi_env env, napi_value object, std::shared_ptr<JsIAMCallback> &callback);
324 #ifdef HAS_USER_AUTH_PART
325 napi_status ParseAddCredInfo(napi_env env, napi_value value, IDMContext &context);
326 napi_status ParseGetPropKeys(napi_env env, napi_value napiKeys, std::vector<Attributes::AttributeKey> &keys);
327 napi_status ParseGetPropRequest(napi_env env, napi_value object, GetPropertyContext &context);
328 napi_status ParseSetPropRequest(napi_env env, napi_value object, AccountSA::SetPropertyRequest &request);
329 napi_value CreateCredInfoArray(napi_env env, const std::vector<AccountSA::CredentialInfo> &info);
330 napi_value CreateAuthResult(napi_env env, const std::vector<uint8_t> &token, int32_t remainTimes, int32_t freezingTime);
331 bool IsAccountIdValid(int32_t accountId);
332 napi_status ConvertGetPropertyTypeToAttributeKey(GetPropertyType in, Attributes::AttributeKey &out);
333 #endif
334 }  // namespace AccountJsKit
335 }  // namespace OHOS
336 #endif  // OS_ACCOUNT_INTERFACES_KITS_NAPI_IAM_INCLUDE_NAPI_IAM_COMMON_H
337