• 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 #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 napi_value GenerateBusinessErrorV9(napi_env env, UserAuthResultCode result);
36     static napi_value GenerateErrorMsg(napi_env env, UserAuthResultCode result, std::string errorMsg);
37     static UserAuthResultCode ThrowErrorMsg(napi_env env, UserAuthResultCode errorCode, std::string errorMsg);
38     static napi_status CheckNapiType(napi_env env, napi_value value, napi_valuetype type);
39     static napi_status GetBoolValue(napi_env env, napi_value value, bool &out);
40     static napi_status GetInt32Value(napi_env env, napi_value value, int32_t &out);
41     static napi_status GetUint32Value(napi_env env, napi_value value, uint32_t &out);
42     static napi_status GetStrValue(napi_env env, napi_value value, char *out, size_t &len);
43     static napi_status GetFunctionRef(napi_env env, napi_value value, napi_ref &ref);
44     static napi_status GetUint8ArrayValue(napi_env env, napi_value value,
45         size_t limitLen, std::vector<uint8_t> &array);
46     static napi_value Uint64ToNapiUint8Array(napi_env env, uint64_t value);
47     static napi_value Uint8VectorToNapiUint8Array(napi_env env,  std::vector<uint8_t> &value);
48     static napi_status SetInt32Property(napi_env env, napi_value obj, const char *name, int32_t value);
49     static napi_status SetUint32Property(napi_env env, napi_value obj, const char *name, uint32_t value);
50     static napi_status SetUint8ArrayProperty(napi_env env,
51         napi_value obj, const char *name, const std::vector<uint8_t> &value);
52     static napi_status SetEnrolledStateProperty(napi_env env, napi_value obj, const char *name, EnrolledState &value);
53     static napi_status CallVoidNapiFunc(napi_env env, napi_ref funcRef, size_t argc, const napi_value *argv);
54     static napi_value GetNamedProperty(napi_env env, napi_value value, const std::string &propertyName);
55     static std::string GetStringFromValueUtf8(napi_env env, napi_value value);
56     static bool HasNamedProperty(napi_env env, napi_value object, const std::string &propertyName);
57     static std::string GetStringPropertyUtf8(napi_env env, napi_value object, const std::string &propertyName);
58     static int32_t GetResultCodeV8(int32_t result);
59     static int32_t GetResultCodeV9(int32_t result);
60     static bool SetStringPropertyUtf8(
61         napi_env env, napi_value object, const std::string &name, const std::string &value);
62     static bool GetInt32Array(napi_env env, napi_value obj, std::vector<uint32_t> vec);
63 private:
64     UserAuthNapiHelper() = default;
65     ~UserAuthNapiHelper() = default;
66 };
67 
68 class JsRefHolder : public NoCopyable {
69 public:
70     JsRefHolder(napi_env env, napi_value value);
71     ~JsRefHolder() override;
72     bool IsValid() const;
73     napi_ref Get() const;
74 
75 private:
76     napi_env env_ {nullptr};
77     napi_ref ref_ {nullptr};
78 };
79 } // namespace UserAuth
80 } // namespace UserIam
81 } // namespace OHOS
82 
83 #endif // USER_AUTH_NAPI_HELPER