• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025-2025 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 "user_auth_param_utils.h"
17 
18 #include <algorithm>
19 #include <cinttypes>
20 #include <string>
21 
22 #include "napi_base_context.h"
23 #include "ui_content.h"
24 #include "ui_extension_context.h"
25 #include "ui_holder_extension_context.h"
26 
27 #include "iam_logger.h"
28 #include "iam_ptr.h"
29 
30 #include "user_auth_common_defines.h"
31 #include "user_auth_helper.h"
32 #include "user_auth_napi_helper.h"
33 
34 #define LOG_TAG "USER_AUTH_NAPI"
35 
36 namespace OHOS {
37 namespace UserIam {
38 namespace UserAuth {
39 namespace {
40     const std::string AUTH_PARAM_CHALLENGE = "challenge";
41     const std::string AUTH_PARAM_AUTHTYPE = "authType";
42     const std::string AUTH_PARAM_AUTHTRUSTLEVEL = "authTrustLevel";
43     const std::string AUTH_PARAM_REUSEUNLOCKRESULT = "reuseUnlockResult";
44     const std::string AUTH_PARAM_USER_ID = "userId";
45     const std::string AUTH_PARAM_SKIP_LOCKED_BIOMETRIC_AUTH = "skipLockedBiometricAuth";
46     const std::string WIDGET_PARAM_TITLE = "title";
47     const std::string WIDGET_PARAM_NAVIBTNTEXT = "navigationButtonText";
48     const std::string WIDGET_PARAM_WINDOWMODE = "windowMode";
49     const std::string WIDGET_PARAM_CONTEXT = "uiContext";
50     const std::string NOTICETYPE = "noticeType";
51     const std::string REUSEMODE = "reuseMode";
52     const std::string REUSEDURATION = "reuseDuration";
53 }
54 
55 namespace WidgetType {
56     constexpr int32_t TITLE_MAX = 500;
57     constexpr int32_t BUTTON_MAX = 60;
58 }
59 
InitChallenge(napi_env env,napi_value value,AuthParamInner & authParam)60 UserAuthResultCode UserAuthParamUtils::InitChallenge(napi_env env, napi_value value, AuthParamInner &authParam)
61 {
62     authParam.challenge.clear();
63     napi_status ret = UserAuthNapiHelper::CheckNapiType(env, value, napi_null);
64     if (ret == napi_ok) {
65         IAM_LOGI("challenge is null");
66         std::string msgStr = "Parameter error. The type of \"challenge\" must be Uint8Array.";
67         return UserAuthNapiHelper::ThrowErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr);
68     }
69     ret = UserAuthNapiHelper::GetUint8ArrayValue(env, value, MAX_CHALLENG_LEN, authParam.challenge);
70     if (ret != napi_ok) {
71         IAM_LOGE("GetUint8ArrayValue fail:%{public}d", ret);
72         std::string msgStr = "Parameter error. The length of \"challenge\" connot exceed 32.";
73         return UserAuthNapiHelper::ThrowErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr);
74     }
75     IAM_LOGI("challenge size:%{public}zu", authParam.challenge.size());
76     return UserAuthResultCode::SUCCESS;
77 }
78 
InitAuthType(napi_env env,napi_value value,AuthParamInner & authParam)79 UserAuthResultCode UserAuthParamUtils::InitAuthType(napi_env env, napi_value value, AuthParamInner &authParam)
80 {
81     bool isArray = false;
82     napi_is_array(env, value, &isArray);
83     if (!isArray) {
84         IAM_LOGI("authType is not array");
85         std::string msgStr = "Parameter error. The type of \"authType\" must be array.";
86         return UserAuthNapiHelper::ThrowErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr);
87     }
88     uint32_t length = 0;
89     napi_get_array_length(env, value, &length);
90     for (uint32_t i = 0; i < length; ++i) {
91         napi_value jsValue = nullptr;
92         napi_handle_scope scope = nullptr;
93         napi_open_handle_scope(env, &scope);
94         napi_get_element(env, value, i, &jsValue);
95         if (jsValue == nullptr) {
96             napi_close_handle_scope(env, scope);
97             continue;
98         }
99         int32_t value = 0;
100         napi_status ret = UserAuthNapiHelper::GetInt32Value(env, jsValue, value);
101         napi_close_handle_scope(env, scope);
102         if (ret != napi_ok) {
103             IAM_LOGE("napi authType GetUint32Value fail:%{public}d", ret);
104             std::string msgStr = "Parameter error. The type of \"authType\" must be number.";
105             return UserAuthNapiHelper::ThrowErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr);
106         }
107         IAM_LOGI("napi get authType:%{public}d", value);
108         if (!UserAuthHelper::CheckUserAuthType(value)) {
109             IAM_LOGE("authType is illegal, %{public}d", value);
110             return UserAuthResultCode::TYPE_NOT_SUPPORT;
111         }
112         auto iter = std::find(authParam.authTypes.begin(), authParam.authTypes.end(), static_cast<AuthType>(value));
113         if (iter != authParam.authTypes.end()) {
114             IAM_LOGE("napi authType:%{public}d exist", value);
115             std::string msgStr = "Parameter error. The type of \"authType\" must be AuthType.";
116             return UserAuthNapiHelper::ThrowErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr);
117         }
118         authParam.authTypes.push_back(static_cast<AuthType>(value));
119     }
120 
121     IAM_LOGI("authType size:%{public}zu", authParam.authTypes.size());
122     return UserAuthResultCode::SUCCESS;
123 }
124 
InitAuthTrustLevel(napi_env env,napi_value value,AuthParamInner & authParam)125 UserAuthResultCode UserAuthParamUtils::InitAuthTrustLevel(napi_env env, napi_value value, AuthParamInner &authParam)
126 {
127     napi_status ret = UserAuthNapiHelper::CheckNapiType(env, value, napi_null);
128     if (ret == napi_ok) {
129         IAM_LOGI("authTrustLevel is null");
130         std::string msgStr = "Parameter error. The type of \"authTrustLevel\" must be number.";
131         return UserAuthNapiHelper::ThrowErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr);
132     }
133     uint32_t authTrustLevel;
134     ret = UserAuthNapiHelper::GetUint32Value(env, value, authTrustLevel);
135     if (ret != napi_ok) {
136         IAM_LOGE("GetUint32Value fail:%{public}d", ret);
137         std::string msgStr = "Parameter error. The type of \"authTrustLevel\" must be number.";
138         return UserAuthNapiHelper::ThrowErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr);
139     }
140     if (!UserAuthHelper::CheckAuthTrustLevel(authTrustLevel)) {
141         IAM_LOGE("AuthTrustLevel fail:%{public}u", authTrustLevel);
142         return UserAuthResultCode::TRUST_LEVEL_NOT_SUPPORT;
143     }
144     authParam.authTrustLevel = AuthTrustLevel(authTrustLevel);
145     IAM_LOGI("authTrustLevel:%{public}u", authParam.authTrustLevel);
146     return UserAuthResultCode::SUCCESS;
147 }
148 
InitReuseUnlockResult(napi_env env,napi_value value,AuthParamInner & authParam)149 UserAuthResultCode UserAuthParamUtils::InitReuseUnlockResult(napi_env env, napi_value value, AuthParamInner &authParam)
150 {
151     uint32_t reuseMode;
152     uint32_t reuseDuration;
153     if (!UserAuthNapiHelper::HasNamedProperty(env, value, REUSEMODE)) {
154         IAM_LOGE("propertyName: %{public}s not exists.", REUSEMODE.c_str());
155         std::string msgStr = "Parameter error. \"reuseMode\" is a mandatory parameter and is left unspecified.";
156         return UserAuthNapiHelper::ThrowErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr);
157     }
158     napi_value napi_reuseMode = UserAuthNapiHelper::GetNamedProperty(env, value, REUSEMODE);
159     napi_status ret = UserAuthNapiHelper::GetUint32Value(env, napi_reuseMode, reuseMode);
160     if (ret != napi_ok) {
161         IAM_LOGE("GetUint32Value fail:%{public}d", ret);
162         std::string msgStr = "Parameter error. The type of \"reuseMode\" must be number.";
163         return UserAuthNapiHelper::ThrowErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr);
164     }
165     authParam.reuseUnlockResult.reuseMode = ReuseMode(reuseMode);
166     if (!UserAuthNapiHelper::HasNamedProperty(env, value, REUSEDURATION)) {
167         IAM_LOGE("propertyName: %{public}s not exists.", REUSEDURATION.c_str());
168         std::string msgStr = "Parameter error. \"reuseDuration\" is a mandatory parameter and is left unspecified.";
169         return UserAuthNapiHelper::ThrowErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr);
170     }
171     napi_value napi_reuseDuration = UserAuthNapiHelper::GetNamedProperty(env, value, REUSEDURATION);
172     ret = UserAuthNapiHelper::GetUint32Value(env, napi_reuseDuration, reuseDuration);
173     if (ret != napi_ok) {
174         IAM_LOGE("GetUint32Value fail:%{public}d", ret);
175         std::string msgStr = "Parameter error. The type of \"reuseDuration\" must be number.";
176         return UserAuthNapiHelper::ThrowErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr);
177     }
178     authParam.reuseUnlockResult.reuseDuration = reuseDuration;
179     if (!UserAuthHelper::CheckReuseUnlockResult(authParam.reuseUnlockResult)) {
180         IAM_LOGE("ReuseUnlockResult fail");
181         std::string msgStr = "Parameter error. The type of \"reuseUnlockResult\" must be ReuseUnlockResult.";
182         return UserAuthNapiHelper::ThrowErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr);
183     }
184     authParam.reuseUnlockResult.isReuse = true;
185     IAM_LOGI("reuseMode: %{public}u, reuseDuration: %{public}" PRIu64, authParam.reuseUnlockResult.reuseMode,
186         authParam.reuseUnlockResult.reuseDuration);
187     return UserAuthResultCode::SUCCESS;
188 }
189 
InitUserId(napi_env env,napi_value value,AuthParamInner & authParam)190 UserAuthResultCode UserAuthParamUtils::InitUserId(napi_env env, napi_value value, AuthParamInner &authParam)
191 {
192     napi_status ret = UserAuthNapiHelper::GetInt32Value(env, value, authParam.userId);
193     if (ret != napi_ok) {
194         IAM_LOGE("GetUint32Value fail:%{public}d", ret);
195         std::string msgStr = "Parameter error. The type of \"userId\" must be number.";
196         return UserAuthNapiHelper::ThrowErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr);
197     }
198     if (authParam.userId < 0) {
199         IAM_LOGE("GetInt32Value fail:%{public}d", ret);
200         std::string msgStr = "Parameter error. The \"userId\" must be greater than or equal to 0";
201         return UserAuthNapiHelper::ThrowErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr);
202     }
203     IAM_LOGI("InitUserId userId: %{public}d", authParam.userId);
204     return UserAuthResultCode::SUCCESS;
205 }
206 
ProcessAuthTrustLevelAndUserId(napi_env env,napi_value value,AuthParamInner & authParam)207 UserAuthResultCode UserAuthParamUtils::ProcessAuthTrustLevelAndUserId(napi_env env, napi_value value,
208     AuthParamInner &authParam)
209 {
210     if (!UserAuthNapiHelper::HasNamedProperty(env, value, AUTH_PARAM_AUTHTRUSTLEVEL)) {
211         IAM_LOGE("propertyName: %{public}s not exists.", AUTH_PARAM_AUTHTRUSTLEVEL.c_str());
212         std::string msgStr = "Parameter error. \"authTrustLevel\" is a mandatory parameter and is left unspecified.";
213         return UserAuthNapiHelper::ThrowErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr);
214     }
215     napi_value napi_authTrustLevel = UserAuthNapiHelper::GetNamedProperty(env, value, AUTH_PARAM_AUTHTRUSTLEVEL);
216     UserAuthResultCode errorCode = InitAuthTrustLevel(env, napi_authTrustLevel, authParam);
217     if (errorCode != UserAuthResultCode::SUCCESS) {
218         IAM_LOGE("InitAuthTrustLevel fail:%{public}d", errorCode);
219         return errorCode;
220     }
221 
222     if (UserAuthNapiHelper::HasNamedProperty(env, value, AUTH_PARAM_USER_ID)) {
223         napi_value napi_userId = UserAuthNapiHelper::GetNamedProperty(env, value, AUTH_PARAM_USER_ID);
224         errorCode = InitUserId(env, napi_userId, authParam);
225         if (errorCode != UserAuthResultCode::SUCCESS) {
226             IAM_LOGE("InitUserId fail:%{public}d", errorCode);
227             return errorCode;
228         }
229     }
230     return UserAuthResultCode::SUCCESS;
231 }
232 
InitAuthParam(napi_env env,napi_value value,AuthParamInner & authParam)233 UserAuthResultCode UserAuthParamUtils::InitAuthParam(napi_env env, napi_value value, AuthParamInner &authParam)
234 {
235     napi_status ret = UserAuthNapiHelper::CheckNapiType(env, value, napi_null);
236     if (ret == napi_ok) {
237         IAM_LOGI("authParam is null");
238         std::string msgStr = "Parameter error. \"authParam\" is a mandatory parameter and is left unspecified.";
239         return UserAuthNapiHelper::ThrowErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr);
240     }
241 
242     if (!UserAuthNapiHelper::HasNamedProperty(env, value, AUTH_PARAM_CHALLENGE)) {
243         IAM_LOGE("propertyName: %{public}s not exists.", AUTH_PARAM_CHALLENGE.c_str());
244         std::string msgStr = "Parameter error. \"challenge\" is a mandatory parameter and is left unspecified.";
245         return UserAuthNapiHelper::ThrowErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr);
246     }
247     napi_value napi_challenge = UserAuthNapiHelper::GetNamedProperty(env, value, AUTH_PARAM_CHALLENGE);
248     UserAuthResultCode errorCode = InitChallenge(env, napi_challenge, authParam);
249     if (errorCode != UserAuthResultCode::SUCCESS) {
250         IAM_LOGE("InitChallenge fail:%{public}d", errorCode);
251         return UserAuthResultCode::OHOS_INVALID_PARAM;
252     }
253 
254     if (!UserAuthNapiHelper::HasNamedProperty(env, value, AUTH_PARAM_AUTHTYPE)) {
255         IAM_LOGE("propertyName: %{public}s not exists.", AUTH_PARAM_AUTHTYPE.c_str());
256         std::string msgStr = "Parameter error. \"authType\" is a mandatory parameter and is left unspecified.";
257         return UserAuthNapiHelper::ThrowErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr);
258     }
259     napi_value napi_authType = UserAuthNapiHelper::GetNamedProperty(env, value, AUTH_PARAM_AUTHTYPE);
260     errorCode = InitAuthType(env, napi_authType, authParam);
261     if (errorCode != UserAuthResultCode::SUCCESS) {
262         IAM_LOGE("InitAuthType fail:%{public}d", errorCode);
263         return errorCode;
264     }
265 
266     errorCode = ProcessReuseUnlockResult(env, value, authParam);
267     if (errorCode != UserAuthResultCode::SUCCESS) {
268         return errorCode;
269     }
270     errorCode = ProcessAuthTrustLevelAndUserId(env, value, authParam);
271     if (errorCode != UserAuthResultCode::SUCCESS) {
272         IAM_LOGE("ProcessAuthTrustLevelAndUserId fail:%{public}d", errorCode);
273         return errorCode;
274     }
275     errorCode = ProcessSkipLockedBiometricAuth(env, value, authParam);
276     if (errorCode != UserAuthResultCode::SUCCESS) {
277         IAM_LOGE("ProcessSkipLockedBiometricAuth fail:%{public}d", errorCode);
278         return errorCode;
279     }
280     return UserAuthResultCode::SUCCESS;
281 }
282 
ProcessReuseUnlockResult(napi_env env,napi_value value,AuthParamInner & authParam)283 UserAuthResultCode UserAuthParamUtils::ProcessReuseUnlockResult(napi_env env, napi_value value,
284     AuthParamInner &authParam)
285 {
286     if (UserAuthNapiHelper::HasNamedProperty(env, value, AUTH_PARAM_REUSEUNLOCKRESULT)) {
287         napi_value napi_reuseUnlockResult = UserAuthNapiHelper::GetNamedProperty(env, value,
288             AUTH_PARAM_REUSEUNLOCKRESULT);
289         UserAuthResultCode errorCode = InitReuseUnlockResult(env, napi_reuseUnlockResult, authParam);
290         if (errorCode != UserAuthResultCode::SUCCESS) {
291             IAM_LOGE("InitReuseUnlockResult fail:%{public}d", errorCode);
292             return errorCode;
293         }
294     } else {
295         authParam.reuseUnlockResult.isReuse = false;
296     }
297     return UserAuthResultCode::SUCCESS;
298 }
299 
InitWidgetParam(napi_env env,napi_value value,UserAuthNapiClientImpl::WidgetParamNapi & widgetParam,std::shared_ptr<AbilityRuntime::Context> & abilityContext)300 UserAuthResultCode UserAuthParamUtils::InitWidgetParam(napi_env env, napi_value value,
301     UserAuthNapiClientImpl::WidgetParamNapi &widgetParam, std::shared_ptr<AbilityRuntime::Context> &abilityContext)
302 {
303     napi_status ret = UserAuthNapiHelper::CheckNapiType(env, value, napi_null);
304     if (ret == napi_ok) {
305         IAM_LOGE("widgetParam is null");
306         std::string msgStr = "Parameter error. \"widgetParam\" is a mandatory parameter and is left unspecified.";
307         return UserAuthNapiHelper::ThrowErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr);
308     }
309 
310     if (!UserAuthNapiHelper::HasNamedProperty(env, value, WIDGET_PARAM_TITLE)) {
311         IAM_LOGE("propertyName: %{public}s not exists.", WIDGET_PARAM_TITLE.c_str());
312         std::string msgStr = "Parameter error. \"title\" is a mandatory parameter and is left unspecified.";
313         return UserAuthNapiHelper::ThrowErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr);
314     }
315     std::string title = UserAuthNapiHelper::GetStringPropertyUtf8(env, value, WIDGET_PARAM_TITLE);
316     if (title == "" || title.size() > WidgetType::TITLE_MAX) {
317         IAM_LOGE("title is invalid. size: %{public}zu", title.size());
318         std::string msgStr = "Parameter error. The length of \"title\" connot exceed 500.";
319         return UserAuthNapiHelper::ThrowErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr);
320     }
321     widgetParam.title = title;
322 
323     if (UserAuthNapiHelper::HasNamedProperty(env, value, WIDGET_PARAM_NAVIBTNTEXT)) {
324         std::string naviBtnTxt = UserAuthNapiHelper::GetStringPropertyUtf8(env, value, WIDGET_PARAM_NAVIBTNTEXT);
325         if (naviBtnTxt == "" || naviBtnTxt.size() > WidgetType::BUTTON_MAX) {
326             IAM_LOGE("navigation button text is invalid, size: %{public}zu", naviBtnTxt.size());
327             std::string msgStr = "Parameter error. The length of \"navigationButtonText\" connot exceed 60.";
328             return UserAuthNapiHelper::ThrowErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr);
329         }
330         widgetParam.navigationButtonText = naviBtnTxt;
331     }
332 
333     UserAuthResultCode errorCode = ProcessWindowMode(env, value, widgetParam);
334     if (errorCode != UserAuthResultCode::SUCCESS) {
335         return errorCode;
336     }
337     errorCode = ProcessContext(env, value, widgetParam, abilityContext);
338     if (errorCode != UserAuthResultCode::SUCCESS) {
339         return errorCode;
340     }
341     return UserAuthResultCode::SUCCESS;
342 }
343 
ProcessContext(napi_env env,napi_value value,UserAuthNapiClientImpl::WidgetParamNapi & widgetParam,std::shared_ptr<AbilityRuntime::Context> & abilityContext)344 UserAuthResultCode UserAuthParamUtils::ProcessContext(napi_env env, napi_value value,
345     UserAuthNapiClientImpl::WidgetParamNapi &widgetParam, std::shared_ptr<AbilityRuntime::Context> &abilityContext)
346 {
347     IAM_LOGI("process uiContext");
348     if (UserAuthNapiHelper::HasNamedProperty(env, value, WIDGET_PARAM_CONTEXT)) {
349         IAM_LOGI("widgetParam has uiContext");
350         napi_value napi_uiContext = UserAuthNapiHelper::GetNamedProperty(env, value, WIDGET_PARAM_CONTEXT);
351         napi_status ret = UserAuthNapiHelper::CheckNapiType(env, napi_uiContext, napi_object);
352         if (ret != napi_ok) {
353             IAM_LOGE("get uiContext fail: %{public}d", ret);
354             std::string msgStr = "Parameter error. The type of \"uiContext\" must be context.";
355             return UserAuthNapiHelper::ThrowErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr);
356         }
357         bool stageMode = false;
358         ret = OHOS::AbilityRuntime::IsStageContext(env, napi_uiContext, stageMode);
359         if (ret != napi_ok) {
360             IAM_LOGE("uiContext must be stage mode: %{public}d", ret);
361             std::string msgStr = "Parameter error. The type of \"uiContext\" must be stage mode.";
362             return UserAuthNapiHelper::ThrowErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr);
363         }
364         auto context = OHOS::AbilityRuntime::GetStageModeContext(env, napi_uiContext);
365         if (CheckUIContext(context)) {
366             abilityContext = context;
367             widgetParam.hasContext = true;
368             IAM_LOGI("widgetParam has valid uiContext");
369         } else {
370             // Default as modal system
371             IAM_LOGI("widgetParam has invalid uiContext, not base on valid AbilityContext or UIExtensionContext.");
372         }
373     }
374     return UserAuthResultCode::SUCCESS;
375 }
376 
ProcessWindowMode(napi_env env,napi_value value,UserAuthNapiClientImpl::WidgetParamNapi & widgetParam)377 UserAuthResultCode UserAuthParamUtils::ProcessWindowMode(napi_env env, napi_value value,
378     UserAuthNapiClientImpl::WidgetParamNapi &widgetParam)
379 {
380     if (UserAuthNapiHelper::HasNamedProperty(env, value, WIDGET_PARAM_WINDOWMODE)) {
381         napi_value napi_windowModeType = UserAuthNapiHelper::GetNamedProperty(env, value, WIDGET_PARAM_WINDOWMODE);
382         uint32_t windowMode;
383         napi_status ret = UserAuthNapiHelper::GetUint32Value(env, napi_windowModeType, windowMode);
384         if (ret != napi_ok) {
385             IAM_LOGE("napi authType GetUint32Value fail:%{public}d", ret);
386             std::string msgStr = "Parameter error. The type of \"windowMode\" must be number.";
387             return UserAuthNapiHelper::ThrowErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr);
388         }
389         switch (windowMode) {
390             case WindowModeType::DIALOG_BOX:
391             case WindowModeType::FULLSCREEN:
392             case WindowModeType::NONE_INTERRUPTION_DIALOG_BOX:
393                 widgetParam.windowMode = static_cast<WindowModeType>(windowMode);
394                 break;
395             default:
396                 IAM_LOGE("windowMode type not support.");
397                 std::string msgStr = "Parameter error. The type of \"windowMode\" must be WindowModeType.";
398                 return UserAuthNapiHelper::ThrowErrorMsg(env, UserAuthResultCode::OHOS_INVALID_PARAM, msgStr);
399         }
400     }
401 
402     IAM_LOGI("widgetParam title:%{public}s, navBtnText:%{public}s, winMode:%{public}u",
403         widgetParam.title.c_str(), widgetParam.navigationButtonText.c_str(),
404         static_cast<uint32_t>(widgetParam.windowMode));
405     return UserAuthResultCode::SUCCESS;
406 }
407 
CheckUIContext(const std::shared_ptr<AbilityRuntime::Context> context)408 bool UserAuthParamUtils::CheckUIContext(const std::shared_ptr<AbilityRuntime::Context> context)
409 {
410     if (context == nullptr) {
411         IAM_LOGE("get context failed");
412         return false;
413     }
414 
415     auto abilityContext = AbilityRuntime::Context::ConvertTo<AbilityRuntime::AbilityContext>(context);
416     if (abilityContext == nullptr) {
417         IAM_LOGE("abilityContext is null");
418         auto holderContext = AbilityRuntime::Context::ConvertTo<AbilityRuntime::UIHolderExtensionContext>(context);
419         if (holderContext == nullptr) {
420             IAM_LOGE("uiExtensionContext is null");
421             return false;
422         }
423         if (holderContext->GetUIContent() == nullptr) {
424             IAM_LOGE("uiContent is null");
425             return false;
426         }
427     } else {
428         if (abilityContext->GetUIContent() == nullptr) {
429             IAM_LOGE("uiContent is null");
430             return false;
431         }
432     }
433     return true;
434 }
435 
ProcessSkipLockedBiometricAuth(napi_env env,napi_value value,AuthParamInner & authParam)436 UserAuthResultCode UserAuthParamUtils::ProcessSkipLockedBiometricAuth(napi_env env, napi_value value,
437     AuthParamInner &authParam)
438 {
439     if (UserAuthNapiHelper::HasNamedProperty(env, value, AUTH_PARAM_SKIP_LOCKED_BIOMETRIC_AUTH)) {
440         napi_value skipLockedBiometricAuth = UserAuthNapiHelper::GetNamedProperty(env, value,
441             AUTH_PARAM_SKIP_LOCKED_BIOMETRIC_AUTH);
442         napi_status ret = UserAuthNapiHelper::GetBoolValue(env, skipLockedBiometricAuth,
443             authParam.skipLockedBiometricAuth);
444         if (ret != napi_ok) {
445             IAM_LOGE("GetBoolValue fail:%{public}d", ret);
446             return UserAuthResultCode::OHOS_INVALID_PARAM;
447         }
448         IAM_LOGI("Init skipLockedBiometricAuth: %{public}d", authParam.skipLockedBiometricAuth);
449     }
450     return UserAuthResultCode::SUCCESS;
451 }
452 } // namespace UserAuth
453 } // namespace UserIam
454 } // namespace OHOS
455