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 USER_AUTH_NAPI_HELPER
17 #define USER_AUTH_NAPI_HELPER
18 
19 #include <string>
20 #include <vector>
21 
22 #include "napi/native_api.h"
23 
24 #include "nocopyable.h"
25 
26 #include "auth_common.h"
27 #include "user_auth_client_impl.h"
28 
29 namespace OHOS {
30 namespace UserIam {
31 namespace UserAuth {
32 class UserAuthNapiHelper {
33 public:
34     static bool CheckAuthType(int32_t authType);
35     static bool CheckUserAuthType(int32_t authType);
36     static bool CheckAuthTrustLevel(uint32_t authTrustLevel);
37     static napi_value GenerateBusinessErrorV9(napi_env env, UserAuthResultCode result);
38     static napi_value GenerateErrorMsg(napi_env env, UserAuthResultCode result, std::string errorMsg);
39     static UserAuthResultCode ThrowErrorMsg(napi_env env, UserAuthResultCode errorCode, std::string errorMsg);
40     static bool CheckReuseUnlockResult(ReuseUnlockResult reuseUnlockResult);
41     static napi_status CheckNapiType(napi_env env, napi_value value, napi_valuetype type);
42     static napi_status GetInt32Value(napi_env env, napi_value value, int32_t &out);
43     static napi_status GetUint32Value(napi_env env, napi_value value, uint32_t &out);
44     static napi_status GetStrValue(napi_env env, napi_value value, char *out, size_t &len);
45     static napi_status GetFunctionRef(napi_env env, napi_value value, napi_ref &ref);
46     static napi_status GetUint8ArrayValue(napi_env env, napi_value value,
47         size_t limitLen, std::vector<uint8_t> &array);
48     static napi_value Uint64ToNapiUint8Array(napi_env env, uint64_t value);
49     static napi_status SetInt32Property(napi_env env, napi_value obj, const char *name, int32_t value);
50     static napi_status SetUint32Property(napi_env env, napi_value obj, const char *name, uint32_t value);
51     static napi_status SetUint8ArrayProperty(napi_env env,
52         napi_value obj, const char *name, const std::vector<uint8_t> &value);
53     static napi_status SetEnrolledStateProperty(napi_env env, napi_value obj, const char *name, EnrolledState &value);
54     static napi_status CallVoidNapiFunc(napi_env env, napi_ref funcRef, size_t argc, const napi_value *argv);
55     static napi_value GetNamedProperty(napi_env env, napi_value value, const std::string &propertyName);
56     static std::string GetStringFromValueUtf8(napi_env env, napi_value value);
57     static bool HasNamedProperty(napi_env env, napi_value object, const std::string &propertyName);
58     static std::string GetStringPropertyUtf8(napi_env env, napi_value object, const std::string &propertyName);
59     static int32_t GetResultCodeV8(int32_t result);
60     static int32_t GetResultCodeV9(int32_t result);
61     static int32_t GetResultCodeV10(int32_t result);
62     static bool SetStringPropertyUtf8(
63         napi_env env, napi_value object, const std::string &name, const std::string &value);
64     static bool GetInt32Array(napi_env env, napi_value obj, std::vector<uint32_t> vec);
65 private:
66     UserAuthNapiHelper() = default;
67     ~UserAuthNapiHelper() = default;
68 };
69 
70 class JsRefHolder : public NoCopyable {
71 public:
72     JsRefHolder(napi_env env, napi_value value);
73     ~JsRefHolder() override;
74     bool IsValid() const;
75     napi_ref Get() const;
76 
77 private:
78     napi_env env_ {nullptr};
79     napi_ref ref_ {nullptr};
80 };
81 } // namespace UserAuth
82 } // namespace UserIam
83 } // namespace OHOS
84 
85 #endif // USER_AUTH_NAPI_HELPER