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 #include "napi_account_iam_identity_manager.h"
17
18 #include <memory>
19 #include "account_log_wrapper.h"
20 #include "account_iam_client.h"
21 #include "napi_account_iam_common.h"
22 #include "napi_account_common.h"
23 #include "napi_account_error.h"
24
25 namespace OHOS {
26 namespace AccountJsKit {
27 using namespace OHOS::AccountSA;
28
Init(napi_env env,napi_value exports)29 napi_value NapiAccountIAMIdentityManager::Init(napi_env env, napi_value exports)
30 {
31 napi_value cons;
32 napi_property_descriptor clzDes[] = {
33 DECLARE_NAPI_FUNCTION("openSession", OpenSession),
34 DECLARE_NAPI_FUNCTION("addCredential", AddCredential),
35 DECLARE_NAPI_FUNCTION("updateCredential", UpdateCredential),
36 DECLARE_NAPI_FUNCTION("closeSession", CloseSession),
37 DECLARE_NAPI_FUNCTION("cancel", Cancel),
38 DECLARE_NAPI_FUNCTION("delUser", DelUser),
39 DECLARE_NAPI_FUNCTION("delCred", DelCred),
40 DECLARE_NAPI_FUNCTION("getAuthInfo", GetAuthInfo),
41 DECLARE_NAPI_FUNCTION("getEnrolledId", GetEnrolledId),
42 };
43 NAPI_CALL(env, napi_define_class(env, "UserIdentityManager", NAPI_AUTO_LENGTH, JsConstructor,
44 nullptr, sizeof(clzDes) / sizeof(napi_property_descriptor), clzDes, &cons));
45 NAPI_CALL(env, napi_set_named_property(env, exports, "UserIdentityManager", cons));
46 return exports;
47 }
48
JsConstructor(napi_env env,napi_callback_info info)49 napi_value NapiAccountIAMIdentityManager::JsConstructor(napi_env env, napi_callback_info info)
50 {
51 if (!IsSystemApp(env)) {
52 return nullptr;
53 }
54 napi_value thisVar = nullptr;
55 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
56 return thisVar;
57 }
58
ParseContextForOpenSession(napi_env env,napi_callback_info info,IDMContext * context)59 static bool ParseContextForOpenSession(
60 napi_env env, napi_callback_info info, IDMContext *context)
61 {
62 size_t argc = ARG_SIZE_ONE;
63 napi_value argv[ARG_SIZE_ONE] = {0};
64 NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), false);
65
66 if (argc > 0) {
67 if ((!GetCallbackProperty(env, argv[0], context->callbackRef, 1)) &&
68 (!GetOptionIntProperty(env, argv[0], context->accountId, context->parseHasAccountId))) {
69 std::string errMsg = "Parameter error. The type of arg 1 must be function or number";
70 AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
71 return false;
72 }
73 }
74 return true;
75 }
76
ParseContextForCloseSession(napi_env env,napi_callback_info info,IDMContext * context)77 static bool ParseContextForCloseSession(
78 napi_env env, napi_callback_info info, IDMContext *context)
79 {
80 size_t argc = ARG_SIZE_ONE;
81 napi_value argv[ARG_SIZE_ONE] = {0};
82 NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), false);
83 if (argc > 0) {
84 if (!GetOptionIntProperty(env, argv[0], context->accountId, context->parseHasAccountId)) {
85 std::string errMsg = "Parameter error. The type of \"accountId\" must be number";
86 AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
87 return false;
88 }
89 }
90 return true;
91 }
92
OpenSession(napi_env env,napi_callback_info info)93 napi_value NapiAccountIAMIdentityManager::OpenSession(napi_env env, napi_callback_info info)
94 {
95 auto context = std::make_unique<IDMContext>(env);
96 if (!ParseContextForOpenSession(env, info, context.get())) {
97 return nullptr;
98 }
99
100 napi_value result = nullptr;
101 if (context->callbackRef == nullptr) {
102 napi_create_promise(env, &context->deferred, &result);
103 } else {
104 napi_get_undefined(env, &result);
105 }
106
107 napi_value resourceName = nullptr;
108 NAPI_CALL(env, napi_create_string_utf8(env, "OpenSession", NAPI_AUTO_LENGTH, &resourceName));
109 NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName,
110 [](napi_env env, void *data) {
111 IDMContext *context = reinterpret_cast<IDMContext *>(data);
112 if ((context->parseHasAccountId) && (!IsAccountIdValid(context->accountId))) {
113 context->errCode = ERR_JS_ACCOUNT_NOT_FOUND;
114 return;
115 }
116 context->errCode = AccountIAMClient::GetInstance().OpenSession(context->accountId, context->challenge);
117 },
118 [](napi_env env, napi_status status, void *data) {
119 IDMContext *context = reinterpret_cast<IDMContext *>(data);
120 napi_value errJs = nullptr;
121 napi_value dataJs = nullptr;
122 if (context->errCode != 0) {
123 int32_t jsErrCode = AccountIAMConvertToJSErrCode(context->errCode);
124 errJs = GenerateBusinessError(env, jsErrCode, ConvertToJsErrMsg(jsErrCode));
125 napi_get_null(env, &dataJs);
126 } else {
127 napi_get_null(env, &errJs);
128 dataJs = CreateUint8Array(env, context->challenge.data(), context->challenge.size());
129 }
130 CallbackAsyncOrPromise(env, context, errJs, dataJs);
131 delete context;
132 },
133 reinterpret_cast<void *>(context.get()), &context->work));
134 NAPI_CALL(env, napi_queue_async_work_with_qos(env, context->work, napi_qos_user_initiated));
135 context.release();
136 return result;
137 }
138
ParseContextForUpdateCredential(napi_env env,napi_callback_info info,IDMContext * context)139 static bool ParseContextForUpdateCredential(napi_env env, napi_callback_info info, IDMContext *context)
140 {
141 size_t argc = ARG_SIZE_TWO;
142 napi_value argv[ARG_SIZE_TWO] = {0};
143 NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), false);
144 if (argc < ARG_SIZE_TWO) {
145 std::string errMsg = "Parameter error. The number of parameters should be 2";
146 AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
147 return false;
148 }
149 if (ParseAddCredInfo(env, argv[PARAM_ZERO], *context) != napi_ok) {
150 std::string errMsg = "Parameter error. The type of \"credentialInfo\" must be CredentialInfo";
151 AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
152 return false;
153 }
154 context->callback = std::make_shared<JsIAMCallback>(env);
155 if (ParseIAMCallback(env, argv[PARAM_ONE], context->callback) != napi_ok) {
156 std::string errMsg = "Parameter error. The type of \"callback\" must be function";
157 AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
158 return false;
159 }
160 return true;
161 }
162
AddCredential(napi_env env,napi_callback_info info)163 napi_value NapiAccountIAMIdentityManager::AddCredential(napi_env env, napi_callback_info info)
164 {
165 auto context = std::make_unique<IDMContext>(env);
166 if (!ParseContextForUpdateCredential(env, info, context.get())) {
167 return nullptr;
168 }
169
170 napi_value resourceName = nullptr;
171 NAPI_CALL(env, napi_create_string_utf8(env, "AddCredential", NAPI_AUTO_LENGTH, &resourceName));
172 NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName,
173 [](napi_env env, void *data) {
174 IDMContext *context = reinterpret_cast<IDMContext *>(data);
175 auto idmCallback = std::make_shared<NapiIDMCallback>(context->env, context->callback);
176 if ((context->parseHasAccountId) && (!IsAccountIdValid(context->accountId))) {
177 Attributes emptyResult;
178 idmCallback->OnResult(ERR_JS_ACCOUNT_NOT_FOUND, emptyResult);
179 return;
180 }
181 AccountIAMClient::GetInstance().AddCredential(context->accountId, context->addCredInfo, idmCallback);
182 },
183 [](napi_env env, napi_status status, void *data) {
184 delete reinterpret_cast<IDMContext *>(data);
185 },
186 reinterpret_cast<void *>(context.get()), &context->work));
187 NAPI_CALL(env, napi_queue_async_work_with_qos(env, context->work, napi_qos_user_initiated));
188 context.release();
189 return nullptr;
190 }
191
UpdateCredential(napi_env env,napi_callback_info info)192 napi_value NapiAccountIAMIdentityManager::UpdateCredential(napi_env env, napi_callback_info info)
193 {
194 auto context = std::make_unique<IDMContext>(env);
195 if (!ParseContextForUpdateCredential(env, info, context.get())) {
196 return nullptr;
197 }
198
199 napi_value resourceName = nullptr;
200 NAPI_CALL(env, napi_create_string_utf8(env, "UpdateCredential", NAPI_AUTO_LENGTH, &resourceName));
201 NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName,
202 [](napi_env env, void *data) {
203 IDMContext *context = reinterpret_cast<IDMContext *>(data);
204 auto idmCallback = std::make_shared<NapiIDMCallback>(context->env, context->callback);
205 if ((context->parseHasAccountId) && (!IsAccountIdValid(context->accountId))) {
206 Attributes emptyResult;
207 idmCallback->OnResult(ERR_JS_ACCOUNT_NOT_FOUND, emptyResult);
208 return;
209 }
210 AccountIAMClient::GetInstance().UpdateCredential(context->accountId, context->addCredInfo, idmCallback);
211 },
212 [](napi_env env, napi_status status, void *data) {
213 delete reinterpret_cast<IDMContext *>(data);
214 },
215 reinterpret_cast<void *>(context.get()), &context->work));
216 NAPI_CALL(env, napi_queue_async_work_with_qos(env, context->work, napi_qos_user_initiated));
217 context.release();
218 return nullptr;
219 }
220
CloseSession(napi_env env,napi_callback_info info)221 napi_value NapiAccountIAMIdentityManager::CloseSession(napi_env env, napi_callback_info info)
222 {
223 auto context = std::make_unique<IDMContext>(env);
224 if (!ParseContextForCloseSession(env, info, context.get())) {
225 return nullptr;
226 }
227 if ((context->parseHasAccountId) && (!IsAccountIdValid(context->accountId))) {
228 AccountIAMNapiThrow(env, AccountIAMConvertToJSErrCode(ERR_JS_ACCOUNT_NOT_FOUND), true);
229 return nullptr;
230 }
231 ErrCode errCode = AccountIAMClient::GetInstance().CloseSession(context->accountId);
232 if (errCode != ERR_OK) {
233 AccountIAMNapiThrow(env, AccountIAMConvertToJSErrCode(errCode), true);
234 }
235 return nullptr;
236 }
237
Cancel(napi_env env,napi_callback_info info)238 napi_value NapiAccountIAMIdentityManager::Cancel(napi_env env, napi_callback_info info)
239 {
240 size_t argc = ARG_SIZE_ONE;
241 napi_value argv[ARG_SIZE_ONE] = {0};
242 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
243 if (argc < ARG_SIZE_ONE) {
244 ACCOUNT_LOGE("failed to parse parameters, expect one parameter, but got %{public}zu", argc);
245 std::string errMsg = "Parameter error. The number of parameters should be 1";
246 AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
247 return nullptr;
248 }
249 uint8_t *data = nullptr;
250 size_t length = 0;
251 napi_status status = ParseUint8TypedArray(env, argv[0], &data, &length);
252 if ((status != napi_ok) || (length < sizeof(uint64_t))) {
253 std::string errMsg = "Parameter error. The type of \"challenge\" must be Uint8Array";
254 AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
255 return nullptr;
256 }
257 int32_t ret = AccountIAMClient::GetInstance().Cancel(-1); // -1 indicates the current user
258 napi_value napiResult = nullptr;
259 if (ret == ERR_OK) {
260 napi_create_int32(env, ret, &napiResult);
261 return napiResult;
262 }
263 ACCOUNT_LOGE("Failed to cancel account, ret = %{public}d", ret);
264 AccountIAMNapiThrow(env, AccountIAMConvertToJSErrCode(ret), true);
265 return nullptr;
266 }
267
ParseContextForDelUser(napi_env env,napi_callback_info info,IDMContext * context)268 static napi_status ParseContextForDelUser(napi_env env, napi_callback_info info, IDMContext *context)
269 {
270 size_t argc = ARG_SIZE_TWO;
271 napi_value argv[ARG_SIZE_TWO] = {0};
272 NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), napi_generic_failure);
273 if (argc != ARG_SIZE_TWO) {
274 ACCOUNT_LOGE("failed to parse parameters, expect two parameters, but got one");
275 std::string errMsg = "Parameter error. The number of parameters should be 2";
276 AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
277 return napi_invalid_arg;
278 }
279 if (ParseUint8TypedArrayToVector(env, argv[0], context->token) != napi_ok) {
280 std::string errMsg = "Parameter error. The type of \"token\" must be Uint8Array";
281 AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
282 return napi_invalid_arg;
283 }
284 context->callback = std::make_shared<JsIAMCallback>(env);
285 if (ParseIAMCallback(env, argv[PARAM_ONE], context->callback) != napi_ok) {
286 std::string errMsg = "Parameter error. The type of \"callback\" must be function";
287 AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
288 return napi_invalid_arg;
289 }
290 return napi_ok;
291 }
292
DelUser(napi_env env,napi_callback_info info)293 napi_value NapiAccountIAMIdentityManager::DelUser(napi_env env, napi_callback_info info)
294 {
295 auto context = std::make_unique<IDMContext>(env);
296 NAPI_CALL(env, ParseContextForDelUser(env, info, context.get()));
297 napi_value resourceName = nullptr;
298 NAPI_CALL(env, napi_create_string_utf8(env, "DelUser", NAPI_AUTO_LENGTH, &resourceName));
299 NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName,
300 [](napi_env env, void *data) {
301 IDMContext *context = reinterpret_cast<IDMContext *>(data);
302 auto idmCallback = std::make_shared<NapiIDMCallback>(context->env, context->callback);
303 AccountIAMClient::GetInstance().DelUser(context->accountId, context->token, idmCallback);
304 },
305 [](napi_env env, napi_status status, void *data) {
306 delete reinterpret_cast<IDMContext *>(data);
307 },
308 reinterpret_cast<void *>(context.get()), &context->work));
309 NAPI_CALL(env, napi_queue_async_work_with_qos(env, context->work, napi_qos_user_initiated));
310 context.release();
311 return nullptr;
312 }
313
ParseContextForDelCred(napi_env env,napi_callback_info info,IDMContext * context)314 static napi_status ParseContextForDelCred(napi_env env, napi_callback_info info, IDMContext *context)
315 {
316 size_t argc = ARG_SIZE_THREE;
317 napi_value argv[ARG_SIZE_THREE] = {0};
318 NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), napi_generic_failure);
319 if (argc < ARG_SIZE_THREE) {
320 ACCOUNT_LOGE("failed to parse parameters, expect three parameters, but got %zu", argc);
321 std::string errMsg = "Parameter error. The number of parameters should be 3";
322 AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
323 return napi_invalid_arg;
324 }
325 if (ParseUint8TypedArrayToUint64(env, argv[0], context->credentialId) != napi_ok) {
326 std::string errMsg = "Parameter error. The type of \"credentialId\" must be Uint8Array";
327 AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
328 return napi_invalid_arg;
329 }
330 if (ParseUint8TypedArrayToVector(env, argv[PARAM_ONE], context->token) != napi_ok) {
331 std::string errMsg = "Parameter error. The type of \"token\" must be Uint8Array";
332 AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
333 return napi_invalid_arg;
334 }
335 context->callback = std::make_shared<JsIAMCallback>(env);
336 if (ParseIAMCallback(env, argv[PARAM_TWO], context->callback) != napi_ok) {
337 std::string errMsg = "Parameter error. The type of \"callback\" must be function";
338 AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
339 return napi_invalid_arg;
340 }
341 return napi_ok;
342 }
343
DelCred(napi_env env,napi_callback_info info)344 napi_value NapiAccountIAMIdentityManager::DelCred(napi_env env, napi_callback_info info)
345 {
346 auto context = std::make_unique<IDMContext>(env);
347 NAPI_CALL(env, ParseContextForDelCred(env, info, context.get()));
348 napi_value resourceName = nullptr;
349 NAPI_CALL(env, napi_create_string_utf8(env, "DelCred", NAPI_AUTO_LENGTH, &resourceName));
350 NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName,
351 [](napi_env env, void *data) {
352 IDMContext *context = reinterpret_cast<IDMContext *>(data);
353 auto idmCallback = std::make_shared<NapiIDMCallback>(context->env, context->callback);
354 AccountIAMClient::GetInstance().DelCred(
355 context->accountId, context->credentialId, context->token, idmCallback);
356 },
357 [](napi_env env, napi_status status, void *data) {
358 delete reinterpret_cast<IDMContext *>(data);
359 },
360 reinterpret_cast<void *>(context.get()), &context->work));
361 NAPI_CALL(env, napi_queue_async_work_with_qos(env, context->work, napi_qos_user_initiated));
362 context.release();
363 return nullptr;
364 }
365
ParseGetAuthInfoOptions(napi_env env,napi_value jsOptions,GetAuthInfoContext * context,int32_t & authType)366 static bool ParseGetAuthInfoOptions(napi_env env, napi_value jsOptions, GetAuthInfoContext *context, int32_t &authType)
367 {
368 bool hasAuthType;
369 if (!GetOptionalNumberPropertyByKey(env, jsOptions, "authType", authType, hasAuthType)) {
370 ACCOUNT_LOGE("Get authOptions's authType failed");
371 return false;
372 }
373 if (!GetOptionalNumberPropertyByKey(env, jsOptions, "accountId", context->accountId, context->parseHasAccountId)) {
374 ACCOUNT_LOGE("Get authOptions's accountId failed");
375 return false;
376 }
377 return true;
378 }
379
ParseOneParamForGetAuthInfo(napi_env env,GetAuthInfoContext * context,napi_value * result,napi_value argv,int32_t & authType)380 static napi_status ParseOneParamForGetAuthInfo(napi_env env, GetAuthInfoContext *context,
381 napi_value *result, napi_value argv, int32_t &authType)
382 {
383 napi_valuetype valueType = napi_undefined;
384 NAPI_CALL_BASE(env, napi_typeof(env, argv, &valueType), napi_generic_failure);
385 if (valueType == napi_function) {
386 if (!GetCallbackProperty(env, argv, context->callbackRef, 1)) {
387 ACCOUNT_LOGE("Get callbackRef failed");
388 std::string errMsg = "Parameter error. The type of \"callback\" must be function";
389 AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
390 return napi_invalid_arg;
391 }
392 } else if (valueType == napi_number) {
393 if (!GetIntProperty(env, argv, authType)) {
394 ACCOUNT_LOGE("Get authType failed");
395 std::string errMsg = "Parameter error. The type of \"authType\" must be AuthType";
396 AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
397 return napi_invalid_arg;
398 }
399 NAPI_CALL_BASE(env, napi_create_promise(env, &context->deferred, result), napi_generic_failure);
400 } else if (valueType == napi_object) {
401 if (!ParseGetAuthInfoOptions(env, argv, context, authType)) {
402 ACCOUNT_LOGE("Parse GetAuthInfoOptions failed");
403 std::string errMsg = "Parameter error. The type of \"options\" must be object";
404 AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
405 return napi_invalid_arg;
406 }
407 NAPI_CALL_BASE(env, napi_create_promise(env, &context->deferred, result), napi_generic_failure);
408 } else if ((valueType == napi_undefined) || (valueType == napi_null)) {
409 ACCOUNT_LOGI("the arg 1 is undefined or null");
410 NAPI_CALL_BASE(env, napi_create_promise(env, &context->deferred, result), napi_generic_failure);
411 } else {
412 ACCOUNT_LOGE("Get arg 1 failed");
413 std::string errMsg = "Parameter error. The type of arg 1 must be AuthType, function or object";
414 AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
415 return napi_invalid_arg;
416 }
417 return napi_ok;
418 }
419
ParseContextForGetAuthInfo(napi_env env,napi_callback_info info,GetAuthInfoContext * context,napi_value * result)420 static napi_status ParseContextForGetAuthInfo(
421 napi_env env, napi_callback_info info, GetAuthInfoContext *context, napi_value *result)
422 {
423 size_t argc = ARG_SIZE_TWO;
424 napi_value argv[ARG_SIZE_TWO] = {0};
425 NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), napi_generic_failure);
426 int32_t authType = 0;
427 if (argc == 0) {
428 NAPI_CALL_BASE(env, napi_create_promise(env, &context->deferred, result), napi_generic_failure);
429 return napi_ok;
430 }
431 if (argc == ARG_SIZE_ONE) {
432 if (ParseOneParamForGetAuthInfo(env, context, result, argv[PARAM_ZERO], authType) != napi_ok) {
433 return napi_invalid_arg;
434 }
435 }
436 if (argc == ARG_SIZE_TWO) {
437 if (!GetCallbackProperty(env, argv[PARAM_ONE], context->callbackRef, 1)) {
438 ACCOUNT_LOGE("Get callbackRef failed");
439 std::string errMsg = "Parameter error. The type of \"callback\" must be function";
440 AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
441 return napi_invalid_arg;
442 }
443 napi_valuetype valueType = napi_undefined;
444 napi_typeof(env, argv[PARAM_ZERO], &valueType);
445 if ((valueType == napi_undefined) || (valueType == napi_null)) {
446 ACCOUNT_LOGI("the authType is undefined or null");
447 } else {
448 if (!GetIntProperty(env, argv[PARAM_ZERO], authType)) {
449 ACCOUNT_LOGE("Get authType failed");
450 std::string errMsg = "Parameter error. The type of \"authType\" must be AuthType";
451 AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
452 return napi_invalid_arg;
453 }
454 }
455 }
456
457 context->authType = static_cast<AuthType>(authType);
458 return napi_ok;
459 }
460
GetAuthInfo(napi_env env,napi_callback_info info)461 napi_value NapiAccountIAMIdentityManager::GetAuthInfo(napi_env env, napi_callback_info info)
462 {
463 napi_value result = nullptr;
464 auto context = std::make_unique<GetAuthInfoContext>(env);
465 NAPI_CALL(env, ParseContextForGetAuthInfo(env, info, context.get(), &result));
466 napi_value resourceName = nullptr;
467 NAPI_CALL(env, napi_create_string_utf8(env, "GetAuthInfo", NAPI_AUTO_LENGTH, &resourceName));
468 NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName,
469 [](napi_env env, void *data) {
470 GetAuthInfoContext *context = reinterpret_cast<GetAuthInfoContext *>(data);
471 auto idmCallback = std::make_shared<NapiGetInfoCallback>(
472 context->env, context->callbackRef, context->deferred);
473 if (idmCallback == nullptr) {
474 ACCOUNT_LOGE("Failed for nullptr");
475 return;
476 }
477 context->callbackRef = nullptr;
478 if ((context->parseHasAccountId) && (!IsAccountIdValid(context->accountId))) {
479 std::vector<AccountSA::CredentialInfo> emptyInfoList;
480 idmCallback->OnCredentialInfo(ERR_JS_ACCOUNT_NOT_FOUND, emptyInfoList);
481 return;
482 }
483 AccountIAMClient::GetInstance().GetCredentialInfo(context->accountId, context->authType, idmCallback);
484 },
485 [](napi_env env, napi_status status, void *data) {
486 delete reinterpret_cast<GetAuthInfoContext *>(data);
487 },
488 reinterpret_cast<void *>(context.get()), &context->work));
489 NAPI_CALL(env, napi_queue_async_work_with_qos(env, context->work, napi_qos_user_initiated));
490 context.release();
491 return result;
492 }
493
ParseContextForGetEnrolledId(napi_env env,napi_callback_info info,GetEnrolledIdContext * context,std::string & errMsg)494 static bool ParseContextForGetEnrolledId(
495 napi_env env, napi_callback_info info, GetEnrolledIdContext *context, std::string &errMsg)
496 {
497 size_t argc = ARG_SIZE_TWO;
498 napi_value argv[ARG_SIZE_TWO] = {0};
499
500 NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), false);
501 if (argc < ARG_SIZE_ONE) {
502 errMsg = "Parameter error. the parameter of number should be at least one";
503 return false;
504 }
505 if (argc == ARG_SIZE_TWO) {
506 if (!GetOptionIntProperty(env, argv[argc - 1], context->accountId, context->parseHasAccountId)) {
507 errMsg = "Parameter error. the type of \"accountId\" must be number";
508 return false;
509 }
510 }
511 int32_t authType = 0;
512 if (!GetIntProperty(env, argv[0], authType)) {
513 errMsg = "Parameter error. the type of \"authType\" must be AuthType";
514 return false;
515 }
516 context->authType = static_cast<AuthType>(authType);
517 return true;
518 }
519
GetEnrolledId(napi_env env,napi_callback_info info)520 napi_value NapiAccountIAMIdentityManager::GetEnrolledId(napi_env env, napi_callback_info info)
521 {
522 auto context = std::make_unique<GetEnrolledIdContext>(env);
523 std::string errMsg;
524 if (!ParseContextForGetEnrolledId(env, info, context.get(), errMsg)) {
525 ACCOUNT_LOGE("Parse context failed, %{public}s", errMsg.c_str());
526 AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, true);
527 return nullptr;
528 }
529 napi_value result = nullptr;
530 NAPI_CALL(env, napi_create_promise(env, &context->deferred, &result));
531 napi_value resourceName = nullptr;
532 NAPI_CALL(env, napi_create_string_utf8(env, "GetEnrolledId", NAPI_AUTO_LENGTH, &resourceName));
533 NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName,
534 [](napi_env env, void *data) {
535 if (data == nullptr) {
536 ACCOUNT_LOGE("Data is nullptr");
537 return;
538 }
539 GetEnrolledIdContext *context = reinterpret_cast<GetEnrolledIdContext *>(data);
540 auto getEnrolledIdCallback = std::make_shared<NapiGetEnrolledIdCallback>(
541 context->env, context->deferred);
542 if ((context->parseHasAccountId) && (!IsAccountIdValid(context->accountId))) {
543 uint64_t enrolledId = 0;
544 getEnrolledIdCallback->OnEnrolledId(ERR_JS_ACCOUNT_NOT_FOUND, enrolledId);
545 return;
546 }
547 AccountIAMClient::GetInstance().GetEnrolledId(context->accountId, context->authType, getEnrolledIdCallback);
548 },
549 [](napi_env env, napi_status status, void *data) {
550 delete reinterpret_cast<GetEnrolledIdContext *>(data);
551 },
552 reinterpret_cast<void *>(context.get()), &context->work));
553 NAPI_CALL(env, napi_queue_async_work_with_qos(env, context->work, napi_qos_user_initiated));
554 context.release();
555 return result;
556 }
557 } // namespace AccountJsKit
558 } // namespace OHOS
559