• 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_user_auth.h"
17 
18 #include "account_iam_client.h"
19 #include "account_log_wrapper.h"
20 #include "napi_account_iam_common.h"
21 #include "napi_account_common.h"
22 #include "napi_account_error.h"
23 
24 namespace OHOS {
25 namespace AccountJsKit {
26 using namespace OHOS::AccountSA;
27 
Init(napi_env env,napi_value exports)28 napi_value NapiAccountIAMUserAuth::Init(napi_env env, napi_value exports)
29 {
30     napi_value cons;
31     napi_property_descriptor clzDes[] = {
32         DECLARE_NAPI_FUNCTION("getVersion", GetVersion),
33         DECLARE_NAPI_FUNCTION("getAvailableStatus", GetAvailableStatus),
34         DECLARE_NAPI_FUNCTION("getProperty", GetProperty),
35         DECLARE_NAPI_FUNCTION("setProperty", SetProperty),
36         DECLARE_NAPI_FUNCTION("auth", Auth),
37         DECLARE_NAPI_FUNCTION("authUser", AuthUser),
38         DECLARE_NAPI_FUNCTION("cancelAuth", CancelAuth),
39     };
40     NAPI_CALL(env, napi_define_class(env, "UserAuth", NAPI_AUTO_LENGTH, JsConstructor,
41         nullptr, sizeof(clzDes) / sizeof(napi_property_descriptor), clzDes, &cons));
42     NAPI_CALL(env, napi_set_named_property(env, exports, "UserAuth", cons));
43     return exports;
44 }
45 
JsConstructor(napi_env env,napi_callback_info info)46 napi_value NapiAccountIAMUserAuth::JsConstructor(napi_env env, napi_callback_info info)
47 {
48     napi_value thisVar = nullptr;
49     NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
50     return thisVar;
51 }
52 
GetVersion(napi_env env,napi_callback_info info)53 napi_value NapiAccountIAMUserAuth::GetVersion(napi_env env, napi_callback_info info)
54 {
55     int32_t result = 0;
56     napi_value version = 0;
57     NAPI_CALL(env, napi_create_int32(env, result, &version));
58     return version;
59 }
60 
GetAvailableStatus(napi_env env,napi_callback_info info)61 napi_value NapiAccountIAMUserAuth::GetAvailableStatus(napi_env env, napi_callback_info info)
62 {
63     size_t argc = ARG_SIZE_TWO;
64     napi_value argv[ARG_SIZE_TWO] = {0};
65     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
66     if (argc != ARG_SIZE_TWO) {
67         ACCOUNT_LOGE("expect 2 parameters, but got %{public}zu", argc);
68         std::string errMsg = "The arg number must be 2 characters";
69         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
70         return nullptr;
71     }
72     napi_valuetype valType = napi_undefined;
73     napi_typeof(env, argv[PARAM_ZERO], &valType);
74     if (valType != napi_number) {
75         std::string errMsg = "The type of arg 1 must be number";
76         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
77         return nullptr;
78     }
79     int32_t authType = -1;
80     napi_get_value_int32(env, argv[PARAM_ZERO], &authType);
81     napi_typeof(env, argv[PARAM_ONE], &valType);
82     if (valType != napi_number) {
83         std::string errMsg = "The type of arg 2 must be number";
84         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
85         return nullptr;
86     }
87     int32_t authSubType = -1;
88     napi_get_value_int32(env, argv[PARAM_ONE], &authSubType);
89     int32_t status;
90     napi_value result = nullptr;
91     int32_t errCode = AccountIAMClient::GetInstance().GetAvailableStatus(
92         static_cast<AuthType>(authType), static_cast<AuthTrustLevel>(authSubType), status);
93     if (errCode == ERR_OK) {
94         if ((status != ERR_IAM_SUCCESS) && (status != ERR_IAM_TYPE_NOT_SUPPORT) &&
95             (status != ERR_IAM_TRUST_LEVEL_NOT_SUPPORT) && (status != ERR_IAM_NOT_ENROLLED)) {
96             AccountIAMNapiThrow(env, AccountIAMConvertToJSErrCode(status), true);
97         } else {
98             napi_create_int32(env, status, &result);
99         }
100     } else {
101         AccountIAMNapiThrow(env, AccountIAMConvertToJSErrCode(errCode), true);
102     }
103     return result;
104 }
105 
ParseContextForGetSetProperty(napi_env env,napi_callback_info info,CommonAsyncContext * context,napi_value * result,bool isGet=true)106 static napi_status ParseContextForGetSetProperty(
107     napi_env env, napi_callback_info info, CommonAsyncContext *context, napi_value *result, bool isGet = true)
108 {
109     size_t argc = ARG_SIZE_TWO;
110     napi_value argv[ARG_SIZE_TWO] = {0};
111     NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), napi_generic_failure);
112     if (argc < ARG_SIZE_ONE) {
113         ACCOUNT_LOGE("expect at least 1 parameter, but got zero");
114         std::string errMsg = "The arg number must be at least 1 character";
115         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
116         return napi_generic_failure;
117     }
118     napi_valuetype valueType = napi_undefined;
119     if (argc == ARG_SIZE_TWO) {
120         NAPI_CALL_BASE(env, napi_typeof(env, argv[PARAM_ONE], &valueType), napi_generic_failure);
121     }
122     if (valueType == napi_function) {
123         NAPI_CALL_BASE(env, napi_create_reference(env, argv[PARAM_ONE], 1, &context->callbackRef),
124             napi_generic_failure);
125     } else {
126         NAPI_CALL_BASE(env, napi_create_promise(env, &context->deferred, result), napi_generic_failure);
127     }
128     if (isGet) {
129         if (ParseGetPropRequest(env, argv[PARAM_ZERO], reinterpret_cast<GetPropertyContext *>(context)->request) !=
130             napi_ok) {
131             std::string errMsg = "Arg 1 must be a valid GetPropertyRequest";
132             AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
133             return napi_generic_failure;
134         }
135     } else {
136         if (ParseSetPropRequest(env, argv[PARAM_ZERO], reinterpret_cast<SetPropertyContext *>(context)->request) !=
137             napi_ok) {
138             std::string errMsg = "Arg 1 must be a valid SetPropertyRequest";
139             AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
140             return napi_generic_failure;
141         }
142     }
143     return napi_ok;
144 }
145 
GetProperty(napi_env env,napi_callback_info info)146 napi_value NapiAccountIAMUserAuth::GetProperty(napi_env env, napi_callback_info info)
147 {
148     napi_value result = nullptr;
149     GetPropertyContext *context = new (std::nothrow) GetPropertyContext(env);
150     if (context == nullptr) {
151         ACCOUNT_LOGE("failed to create GetPropertyContext");
152         return result;
153     }
154     std::unique_ptr<GetPropertyContext> contextPtr(context);
155     if (ParseContextForGetSetProperty(env, info, context, &result) != napi_ok) {
156         return nullptr;
157     }
158     napi_value resourceName = nullptr;
159     NAPI_CALL(env, napi_create_string_utf8(env, "GetProperty", NAPI_AUTO_LENGTH, &resourceName));
160     NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName,
161         [](napi_env env, void *data) {
162             GetPropertyContext *context = reinterpret_cast<GetPropertyContext *>(data);
163             auto getPropCallback = std::make_shared<NapiGetPropCallback>(
164                 context->env, context->callbackRef, context->deferred);
165             AccountIAMClient::GetInstance().GetProperty(0, context->request, getPropCallback);
166             context->callbackRef = nullptr;
167         },
168         [](napi_env env, napi_status status, void *data) {
169             delete reinterpret_cast<GetPropertyContext *>(data);
170         },
171         reinterpret_cast<void *>(context), &context->work));
172     NAPI_CALL(env, napi_queue_async_work(env, context->work));
173     contextPtr.release();
174     return result;
175 }
176 
SetProperty(napi_env env,napi_callback_info info)177 napi_value NapiAccountIAMUserAuth::SetProperty(napi_env env, napi_callback_info info)
178 {
179     napi_value result = nullptr;
180     SetPropertyContext *context = new (std::nothrow) SetPropertyContext(env);
181     if (context == nullptr) {
182         ACCOUNT_LOGE("failed to create SetPropertyContext");
183         return result;
184     }
185     std::unique_ptr<SetPropertyContext> contextPtr(context);
186     if (ParseContextForGetSetProperty(env, info, context, &result, false) != napi_ok) {
187         return nullptr;
188     }
189     napi_value resourceName = nullptr;
190     NAPI_CALL(env, napi_create_string_utf8(env, "SetProperty", NAPI_AUTO_LENGTH, &resourceName));
191     NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName,
192         [](napi_env env, void *data) {
193             SetPropertyContext *context = reinterpret_cast<SetPropertyContext *>(data);
194             auto setPropCallback = std::make_shared<NapiSetPropCallback>(
195                 context->env, context->callbackRef, context->deferred);
196             AccountIAMClient::GetInstance().SetProperty(0, context->request, setPropCallback);
197             context->callbackRef = nullptr;
198         },
199         [](napi_env env, napi_status status, void *data) {
200             delete reinterpret_cast<SetPropertyContext *>(data);
201         },
202         reinterpret_cast<void *>(context), &context->work));
203     NAPI_CALL(env, napi_queue_async_work(env, context->work));
204     contextPtr.release();
205     return result;
206 }
207 
ParseContextForAuth(napi_env env,napi_callback_info info,AuthContext & context,bool needUser=false)208 static napi_status ParseContextForAuth(
209     napi_env env, napi_callback_info info, AuthContext &context, bool needUser = false)
210 {
211     size_t expectedSize = needUser ? ARG_SIZE_FIVE : ARG_SIZE_FOUR;
212     size_t argc = ARG_SIZE_FIVE;
213     napi_value argv[ARG_SIZE_FIVE] = {0};
214     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
215     if (argc != expectedSize) {
216         ACCOUNT_LOGE("failed to parse parameters, expect %{public}zu parameters, but got %{public}zu",
217             expectedSize, argc);
218         std::string errMsg = "The arg number must be at least " + std::to_string(expectedSize) + " characters";
219         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, context.throwErr);
220         return napi_invalid_arg;
221     }
222     int32_t index = 0;
223     if (needUser && !GetIntProperty(env, argv[index++], context.userId)) {
224         ACCOUNT_LOGE("Get userId failed");
225         std::string errMsg = "The type of arg " + std::to_string(index) + " must be number";
226         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, context.throwErr);
227         return napi_invalid_arg;
228     }
229     if (ParseUint8TypedArrayToVector(env, argv[index++], context.challenge)!= napi_ok) {
230         ACCOUNT_LOGE("Get challenge failed");
231         std::string errMsg = "The type of arg " + std::to_string(index) + " must be valid int array";
232         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, context.throwErr);
233         return napi_invalid_arg;
234     }
235     if (!GetIntProperty(env, argv[index++], context.authType)) {
236         ACCOUNT_LOGE("Get authType failed");
237         std::string errMsg = "The type of arg " + std::to_string(index) + " must be number";
238         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, context.throwErr);
239         return napi_invalid_arg;
240     }
241     if (!GetIntProperty(env, argv[index++], context.trustLevel)) {
242         ACCOUNT_LOGE("Get trustLevel failed");
243         std::string errMsg = "The type of arg " + std::to_string(index) + " must be number";
244         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, context.throwErr);
245         return napi_invalid_arg;
246     }
247     JsIAMCallback jsCallback;
248     if (ParseIAMCallback(env, argv[index++], jsCallback) != napi_ok) {
249         ACCOUNT_LOGE("Get callback failed");
250         std::string errMsg = "The type of arg " + std::to_string(index) + " must be function";
251         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, context.throwErr);
252         return napi_invalid_arg;
253     }
254     NapiUserAuthCallback *object = new (std::nothrow) NapiUserAuthCallback(env, jsCallback);
255     if (object == nullptr) {
256         ACCOUNT_LOGE("failed to create NapiUserAuthCallback");
257         return napi_generic_failure;
258     }
259     context.callback.reset(object);
260     return napi_ok;
261 }
262 
Auth(napi_env env,napi_callback_info info)263 napi_value NapiAccountIAMUserAuth::Auth(napi_env env, napi_callback_info info)
264 {
265     AuthContext context;
266     NAPI_CALL(env, ParseContextForAuth(env, info, context));
267     uint64_t contextId = AccountIAMClient::GetInstance().Auth(context.challenge,
268         static_cast<AuthType>(context.authType), static_cast<AuthTrustLevel>(context.trustLevel), context.callback);
269     return CreateUint8Array(env, reinterpret_cast<uint8_t *>(&contextId), sizeof(uint64_t));
270 }
271 
AuthUser(napi_env env,napi_callback_info info)272 napi_value NapiAccountIAMUserAuth::AuthUser(napi_env env, napi_callback_info info)
273 {
274     AuthContext context;
275     NAPI_CALL(env, ParseContextForAuth(env, info, context, true));
276     uint64_t contextId;
277     AccountIAMClient::GetInstance().AuthUser(context.userId, context.challenge,
278         static_cast<AuthType>(context.authType), static_cast<AuthTrustLevel>(context.trustLevel), context.callback);
279     return CreateUint8Array(env, reinterpret_cast<uint8_t *>(&contextId), sizeof(uint64_t));
280 }
281 
CancelAuth(napi_env env,napi_callback_info info)282 napi_value NapiAccountIAMUserAuth::CancelAuth(napi_env env, napi_callback_info info)
283 {
284     size_t argc = ARG_SIZE_ONE;
285     napi_value argv[ARG_SIZE_ONE] = {0};
286     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
287     if (argc != ARG_SIZE_ONE) {
288         ACCOUNT_LOGE("failed to parse parameters, expect at least one parameter, but got %zu", argc);
289         std::string errMsg = "The arg number must be at least " + std::to_string(ARG_SIZE_ONE) + " characters";
290         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
291         return nullptr;
292     }
293     uint64_t contextId = 0;
294     if (ParseUint8TypedArrayToUint64(env, argv[0], contextId) != napi_ok) {
295         ACCOUNT_LOGE("failed to parse contextId");
296         std::string errMsg = "The type of arg 1 must be number";
297         AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
298         return nullptr;
299     }
300     int32_t result = AccountIAMClient::GetInstance().CancelAuth(contextId);
301     napi_value napiResult = nullptr;
302     if (result == ERR_OK) {
303         NAPI_CALL(env, napi_create_int32(env, result, &napiResult));
304         return napiResult;
305     }
306     AccountIAMNapiThrow(env, AccountIAMConvertToJSErrCode(result), true);
307     return nullptr;
308 }
309 }  // namespace AccountJsKit
310 }  // namespace OHOS
311