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