• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "napi_account_iam_pin_auth.h"
17 
18 #include "account_iam_client.h"
19 #include "account_log_wrapper.h"
20 #include "napi_account_common.h"
21 #include "napi_account_error.h"
22 #include "napi_account_iam_common.h"
23 
24 namespace OHOS {
25 namespace AccountJsKit {
26 using namespace OHOS::AccountSA;
27 
Init(napi_env env,napi_value exports)28 napi_value NapiAccountIAMPINAuth::Init(napi_env env, napi_value exports)
29 {
30     napi_value cons;
31     napi_property_descriptor clzDes[] = {
32         DECLARE_NAPI_FUNCTION("registerInputer", RegisterInputer),
33         DECLARE_NAPI_FUNCTION("unregisterInputer", UnregisterInputer)
34     };
35     NAPI_CALL(env, napi_define_class(env, "PINAuth", NAPI_AUTO_LENGTH, JsConstructor,
36         nullptr, sizeof(clzDes) / sizeof(napi_property_descriptor), clzDes, &cons));
37     NAPI_CALL(env, napi_set_named_property(env, exports, "PINAuth", cons));
38     return exports;
39 }
40 
JsConstructor(napi_env env,napi_callback_info info)41 napi_value NapiAccountIAMPINAuth::JsConstructor(napi_env env, napi_callback_info info)
42 {
43     if (!IsSystemApp(env)) {
44         return nullptr;
45     }
46     napi_value thisVar = nullptr;
47     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
48     return thisVar;
49 }
50 
ParseContextForRegisterInputer(napi_env env,napi_callback_info info,napi_ref * callback)51 static bool ParseContextForRegisterInputer(napi_env env, napi_callback_info info, napi_ref *callback)
52 {
53     size_t argc = ARG_SIZE_ONE;
54     napi_value argv[ARG_SIZE_ONE] = {nullptr};
55     NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), false);
56     if (argc != ARG_SIZE_ONE) {
57         ACCOUNT_LOGE("expect at least one parameter, but got %zu", argc);
58         std::string errMsg = "Parameter error. The number of parameters should be at least 1";
59         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
60         return false;
61     }
62     napi_valuetype valuetype = napi_undefined;
63     napi_typeof(env, argv[PARAM_ZERO], &valuetype);
64     napi_value onGetData = nullptr;
65     if (valuetype == napi_object) {
66         napi_get_named_property(env, argv[PARAM_ZERO], "onGetData", &onGetData);
67     } else if (valuetype == napi_function) {
68         onGetData = argv[PARAM_ZERO];
69     } else {
70         ACCOUNT_LOGE("inputer param type error");
71         std::string errMsg = "Parameter error. The type of \"inputer\" must be function or object";
72         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
73         return false;
74     }
75     return napi_create_reference(env, onGetData, 1, callback) == napi_ok ? true : false;
76 }
77 
RegisterInputer(napi_env env,napi_callback_info info)78 napi_value NapiAccountIAMPINAuth::RegisterInputer(napi_env env, napi_callback_info info)
79 {
80     napi_value result;
81     NAPI_CALL(env, napi_get_boolean(env, false, &result));
82     napi_ref callback = nullptr;
83     if (!ParseContextForRegisterInputer(env, info, &callback)) {
84         return result;
85     }
86     auto napiCallback = std::make_shared<NapiCallbackRef>(env, callback);
87     auto inputer = std::make_shared<NapiGetDataCallback>(env, napiCallback);
88     int32_t errCode = AccountIAMClient::GetInstance().RegisterPINInputer(inputer);
89     napi_value napiResult = nullptr;
90     if (errCode == ERR_OK) {
91         NAPI_CALL(env, napi_get_boolean(env, true, &napiResult));
92         return napiResult;
93     }
94     ACCOUNT_LOGE("Failed to register inputer, errCode=%{public}d", errCode);
95     AccountIAMNapiThrow(env, AccountIAMConvertToJSErrCode(errCode), true);
96     return nullptr;
97 }
98 
UnregisterInputer(napi_env env,napi_callback_info info)99 napi_value NapiAccountIAMPINAuth::UnregisterInputer(napi_env env, napi_callback_info info)
100 {
101     ErrCode errCode = AccountIAMClient::GetInstance().UnregisterPINInputer();
102     if (errCode != ERR_OK) {
103         AccountIAMNapiThrow(env, AccountIAMConvertToJSErrCode(errCode), true);
104     }
105     return nullptr;
106 }
107 }  // namespace AccountJsKit
108 }  // namespace OHOS
109