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_constant.h"
17
18 #include "account_log_wrapper.h"
19 #include "napi_account_iam_common.h"
20
21 namespace OHOS {
22 namespace AccountJsKit {
23 using namespace OHOS::AccountSA;
24
AuthTypeConstructor(napi_env env)25 napi_value AuthTypeConstructor(napi_env env)
26 {
27 napi_value authType = nullptr;
28 napi_value pin = nullptr;
29 napi_value face = nullptr;
30 napi_value fingerPrint = nullptr;
31 napi_value domain = nullptr;
32 NAPI_CALL(env, napi_create_object(env, &authType));
33 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(AuthType::PIN), &pin));
34 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(AuthType::FACE), &face));
35 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(AuthType::FINGERPRINT), &fingerPrint));
36 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(IAMAuthType::DOMAIN), &domain));
37 NAPI_CALL(env, napi_set_named_property(env, authType, "PIN", pin));
38 NAPI_CALL(env, napi_set_named_property(env, authType, "FACE", face));
39 NAPI_CALL(env, napi_set_named_property(env, authType, "FINGERPRINT", fingerPrint));
40 NAPI_CALL(env, napi_set_named_property(env, authType, "DOMAIN", domain));
41 return authType;
42 }
43
AuthSubTypeConstructor(napi_env env)44 napi_value AuthSubTypeConstructor(napi_env env)
45 {
46 napi_value authSubType = nullptr;
47 napi_value pinSix = nullptr;
48 napi_value pinNumber = nullptr;
49 napi_value pinMixed = nullptr;
50 napi_value face2d = nullptr;
51 napi_value face3d = nullptr;
52 napi_value domainMixed = nullptr;
53 NAPI_CALL(env, napi_create_object(env, &authSubType));
54 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(PinSubType::PIN_SIX), &pinSix));
55 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(PinSubType::PIN_NUMBER), &pinNumber));
56 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(PinSubType::PIN_MIXED), &pinMixed));
57 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(FACE_2D), &face2d));
58 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(FACE_3D), &face3d));
59 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(IAMAuthSubType::DOMAIN_MIXED), &domainMixed));
60 NAPI_CALL(env, napi_set_named_property(env, authSubType, "PIN_SIX", pinSix));
61 NAPI_CALL(env, napi_set_named_property(env, authSubType, "PIN_NUMBER", pinNumber));
62 NAPI_CALL(env, napi_set_named_property(env, authSubType, "PIN_MIXED", pinMixed));
63 NAPI_CALL(env, napi_set_named_property(env, authSubType, "FACE_2D", face2d));
64 NAPI_CALL(env, napi_set_named_property(env, authSubType, "FACE_3D", face3d));
65 NAPI_CALL(env, napi_set_named_property(env, authSubType, "DOMAIN_MIXED", domainMixed));
66 return authSubType;
67 }
68
AuthTrustLevelConstructor(napi_env env)69 napi_value AuthTrustLevelConstructor(napi_env env)
70 {
71 napi_value authTrustLevel = nullptr;
72 napi_value atl1 = nullptr;
73 napi_value atl2 = nullptr;
74 napi_value atl3 = nullptr;
75 napi_value atl4 = nullptr;
76 NAPI_CALL(env, napi_create_object(env, &authTrustLevel));
77 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(AuthTrustLevel::ATL1), &atl1));
78 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(AuthTrustLevel::ATL2), &atl2));
79 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(AuthTrustLevel::ATL3), &atl3));
80 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(AuthTrustLevel::ATL4), &atl4));
81 NAPI_CALL(env, napi_set_named_property(env, authTrustLevel, "ATL1", atl1));
82 NAPI_CALL(env, napi_set_named_property(env, authTrustLevel, "ATL2", atl2));
83 NAPI_CALL(env, napi_set_named_property(env, authTrustLevel, "ATL3", atl3));
84 NAPI_CALL(env, napi_set_named_property(env, authTrustLevel, "ATL4", atl4));
85 return authTrustLevel;
86 }
87
GetPropertyTypeConstructor(napi_env env)88 napi_value GetPropertyTypeConstructor(napi_env env)
89 {
90 napi_value getPropertyType = nullptr;
91 napi_value authSubType = nullptr;
92 napi_value remainTimes = nullptr;
93 napi_value freezingTime = nullptr;
94 NAPI_CALL(env, napi_create_object(env, &getPropertyType));
95 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(GetPropertyType::AUTH_SUB_TYPE), &authSubType));
96 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(GetPropertyType::REMAIN_TIMES), &remainTimes));
97 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(GetPropertyType::FREEZING_TIME), &freezingTime));
98 NAPI_CALL(env, napi_set_named_property(env, getPropertyType, "AUTH_SUB_TYPE", authSubType));
99 NAPI_CALL(env, napi_set_named_property(env, getPropertyType, "REMAIN_TIMES", remainTimes));
100 NAPI_CALL(env, napi_set_named_property(env, getPropertyType, "FREEZING_TIME", freezingTime));
101 return getPropertyType;
102 }
103
SetPropertyTypeConstructor(napi_env env)104 napi_value SetPropertyTypeConstructor(napi_env env)
105 {
106 napi_value setPropertyType = nullptr;
107 napi_value initAlgorithm = nullptr;
108 NAPI_CALL(env, napi_create_object(env, &setPropertyType));
109 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(SetPropertyType::INIT_ALGORITHM), &initAlgorithm));
110 NAPI_CALL(env, napi_set_named_property(env, setPropertyType, "INIT_ALGORITHM", initAlgorithm));
111 return setPropertyType;
112 }
113
AuthMethodConstructor(napi_env env)114 napi_value AuthMethodConstructor(napi_env env)
115 {
116 napi_value authMethod = nullptr;
117 napi_value pinOnly = nullptr;
118 napi_value faceOnly = nullptr;
119 NAPI_CALL(env, napi_create_object(env, &authMethod));
120 NAPI_CALL(env, napi_create_int32(env, AuthMethod::PIN_ONLY, &pinOnly));
121 NAPI_CALL(env, napi_create_int32(env, AuthMethod::FACE_ONLY, &faceOnly));
122 NAPI_CALL(env, napi_set_named_property(env, authMethod, "PIN_ONLY", pinOnly));
123 NAPI_CALL(env, napi_set_named_property(env, authMethod, "FACE_ONLY", faceOnly));
124 return authMethod;
125 }
126
ModuleConstructor(napi_env env)127 napi_value ModuleConstructor(napi_env env)
128 {
129 napi_value module = nullptr;
130 napi_value faceAuth = nullptr;
131 NAPI_CALL(env, napi_create_object(env, &module));
132 NAPI_CALL(env, napi_create_int32(env, Module::FACE_AUTH, &faceAuth));
133 NAPI_CALL(env, napi_set_named_property(env, module, "FACE_AUTH", faceAuth));
134 return module;
135 }
136
ResultCodeConstructor(napi_env env)137 napi_value ResultCodeConstructor(napi_env env)
138 {
139 napi_value resultCode = nullptr;
140 napi_value success = nullptr;
141 napi_value fail = nullptr;
142 napi_value generalError = nullptr;
143 napi_value canceled = nullptr;
144 napi_value timeout = nullptr;
145 napi_value typeNotSupport = nullptr;
146 napi_value trustLevelNotSupport = nullptr;
147 napi_value busy = nullptr;
148 napi_value invalidParameters = nullptr;
149 napi_value locked = nullptr;
150 napi_value notEnrolled = nullptr;
151 NAPI_CALL(env, napi_create_object(env, &resultCode));
152 NAPI_CALL(env, napi_create_int32(env, ResultCode::SUCCESS, &success));
153 NAPI_CALL(env, napi_create_int32(env, ResultCode::FAIL, &fail));
154 NAPI_CALL(env, napi_create_int32(env, ResultCode::GENERAL_ERROR, &generalError));
155 NAPI_CALL(env, napi_create_int32(env, ResultCode::CANCELED, &canceled));
156 NAPI_CALL(env, napi_create_int32(env, ResultCode::TIMEOUT, &timeout));
157 NAPI_CALL(env, napi_create_int32(env, ResultCode::TYPE_NOT_SUPPORT, &typeNotSupport));
158 NAPI_CALL(env, napi_create_int32(env, ResultCode::TRUST_LEVEL_NOT_SUPPORT, &trustLevelNotSupport));
159 NAPI_CALL(env, napi_create_int32(env, ResultCode::BUSY, &busy));
160 NAPI_CALL(env, napi_create_int32(env, ResultCode::INVALID_PARAMETERS, &invalidParameters));
161 NAPI_CALL(env, napi_create_int32(env, ResultCode::LOCKED, &locked));
162 NAPI_CALL(env, napi_create_int32(env, ResultCode::NOT_ENROLLED, ¬Enrolled));
163 NAPI_CALL(env, napi_set_named_property(env, resultCode, "SUCCESS", success));
164 NAPI_CALL(env, napi_set_named_property(env, resultCode, "FAIL", fail));
165 NAPI_CALL(env, napi_set_named_property(env, resultCode, "GENERAL_ERROR", generalError));
166 NAPI_CALL(env, napi_set_named_property(env, resultCode, "CANCELED", canceled));
167 NAPI_CALL(env, napi_set_named_property(env, resultCode, "TIMEOUT", timeout));
168 NAPI_CALL(env, napi_set_named_property(env, resultCode, "TYPE_NOT_SUPPORT", typeNotSupport));
169 NAPI_CALL(env, napi_set_named_property(env, resultCode, "TRUST_LEVEL_NOT_SUPPORT", trustLevelNotSupport));
170 NAPI_CALL(env, napi_set_named_property(env, resultCode, "BUSY", busy));
171 NAPI_CALL(env, napi_set_named_property(env, resultCode, "INVALID_PARAMETERS", invalidParameters));
172 NAPI_CALL(env, napi_set_named_property(env, resultCode, "LOCKED", locked));
173 NAPI_CALL(env, napi_set_named_property(env, resultCode, "NOT_ENROLLED", notEnrolled));
174 return resultCode;
175 }
176
FaceTipsCodeConstructor(napi_env env)177 napi_value FaceTipsCodeConstructor(napi_env env)
178 {
179 napi_value faceTipsCode = nullptr;
180 napi_value faceAuthTipTooBright = nullptr;
181 napi_value faceAuthTipTooDark = nullptr;
182 napi_value faceAuthTipTooClose = nullptr;
183 napi_value faceAuthTipTooFar = nullptr;
184 napi_value faceAuthTipTooHigh = nullptr;
185 napi_value faceAuthTipTooLow = nullptr;
186 napi_value faceAuthTipTooRight = nullptr;
187 napi_value faceAuthTipTooLeft = nullptr;
188 napi_value faceAuthTipTooMuchMotion = nullptr;
189 napi_value faceAuthTipPoorGaze = nullptr;
190 napi_value faceAuthTipNotDetected = nullptr;
191 NAPI_CALL(env, napi_create_object(env, &faceTipsCode));
192 NAPI_CALL(env, napi_create_int32(env, FaceTipsCode::FACE_AUTH_TIP_TOO_BRIGHT, &faceAuthTipTooBright));
193 NAPI_CALL(env, napi_create_int32(env, FaceTipsCode::FACE_AUTH_TIP_TOO_DARK, &faceAuthTipTooDark));
194 NAPI_CALL(env, napi_create_int32(env, FaceTipsCode::FACE_AUTH_TIP_TOO_CLOSE, &faceAuthTipTooClose));
195 NAPI_CALL(env, napi_create_int32(env, FaceTipsCode::FACE_AUTH_TIP_TOO_FAR, &faceAuthTipTooFar));
196 NAPI_CALL(env, napi_create_int32(env, FaceTipsCode::FACE_AUTH_TIP_TOO_HIGH, &faceAuthTipTooHigh));
197 NAPI_CALL(env, napi_create_int32(env, FaceTipsCode::FACE_AUTH_TIP_TOO_LOW, &faceAuthTipTooLow));
198 NAPI_CALL(env, napi_create_int32(env, FaceTipsCode::FACE_AUTH_TIP_TOO_RIGHT, &faceAuthTipTooRight));
199 NAPI_CALL(env, napi_create_int32(env, FaceTipsCode::FACE_AUTH_TIP_TOO_LEFT, &faceAuthTipTooLeft));
200 NAPI_CALL(env, napi_create_int32(env, FaceTipsCode::FACE_AUTH_TIP_TOO_MUCH_MOTION, &faceAuthTipTooMuchMotion));
201 NAPI_CALL(env, napi_create_int32(env, FaceTipsCode::FACE_AUTH_TIP_POOR_GAZE, &faceAuthTipPoorGaze));
202 NAPI_CALL(env, napi_create_int32(env, FaceTipsCode::FACE_AUTH_TIP_NOT_DETECTED, &faceAuthTipNotDetected));
203 NAPI_CALL(env, napi_set_named_property(env, faceTipsCode, "FACE_AUTH_TIP_TOO_BRIGHT", faceAuthTipTooBright));
204 NAPI_CALL(env, napi_set_named_property(env, faceTipsCode, "FACE_AUTH_TIP_TOO_DARK", faceAuthTipTooDark));
205 NAPI_CALL(env, napi_set_named_property(env, faceTipsCode, "FACE_AUTH_TIP_TOO_CLOSE", faceAuthTipTooClose));
206 NAPI_CALL(env, napi_set_named_property(env, faceTipsCode, "FACE_AUTH_TIP_TOO_FAR", faceAuthTipTooFar));
207 NAPI_CALL(env, napi_set_named_property(env, faceTipsCode, "FACE_AUTH_TIP_TOO_HIGH", faceAuthTipTooHigh));
208 NAPI_CALL(env, napi_set_named_property(env, faceTipsCode, "FACE_AUTH_TIP_TOO_LOW", faceAuthTipTooLow));
209 NAPI_CALL(env, napi_set_named_property(env, faceTipsCode, "FACE_AUTH_TIP_TOO_RIGHT", faceAuthTipTooRight));
210 NAPI_CALL(env, napi_set_named_property(env, faceTipsCode, "FACE_AUTH_TIP_TOO_LEFT", faceAuthTipTooLeft));
211 NAPI_CALL(env, napi_set_named_property(env, faceTipsCode,
212 "FACE_AUTH_TIP_TOO_MUCH_MOTION", faceAuthTipTooMuchMotion));
213 NAPI_CALL(env, napi_set_named_property(env, faceTipsCode, "FACE_AUTH_TIP_POOR_GAZE", faceAuthTipPoorGaze));
214 NAPI_CALL(env, napi_set_named_property(env, faceTipsCode, "FACE_AUTH_TIP_NOT_DETECTED", faceAuthTipNotDetected));
215 return faceTipsCode;
216 }
217
FingerprintTipsConstructorForInnerkits(napi_env env)218 napi_value FingerprintTipsConstructorForInnerkits(napi_env env)
219 {
220 napi_value fingerprintTips = nullptr;
221 napi_value fingerprintTipGood = nullptr;
222 napi_value fingerprintTipImagerDirty = nullptr;
223 napi_value fingerprintTipInsufficient = nullptr;
224 napi_value fingerprintTipPartial = nullptr;
225 napi_value fingerprintTipTooFast = nullptr;
226 napi_value fingerprintTipTooSlow = nullptr;
227 NAPI_CALL(env, napi_create_object(env, &fingerprintTips));
228 NAPI_CALL(env, napi_create_int32(env, FingerprintTips::FINGERPRINT_AUTH_TIP_GOOD, &fingerprintTipGood));
229 NAPI_CALL(env, napi_create_int32(env, FingerprintTips::FINGERPRINT_AUTH_TIP_IMAGER_DIRTY,
230 &fingerprintTipImagerDirty));
231 NAPI_CALL(env, napi_create_int32(env, FingerprintTips::FINGERPRINT_AUTH_TIP_INSUFFICIENT,
232 &fingerprintTipInsufficient));
233 NAPI_CALL(env, napi_create_int32(env, FingerprintTips::FINGERPRINT_AUTH_TIP_PARTIAL, &fingerprintTipPartial));
234 NAPI_CALL(env, napi_create_int32(env, FingerprintTips::FINGERPRINT_AUTH_TIP_TOO_FAST, &fingerprintTipTooFast));
235 NAPI_CALL(env, napi_create_int32(env, FingerprintTips::FINGERPRINT_AUTH_TIP_TOO_SLOW, &fingerprintTipTooSlow));
236 NAPI_CALL(env, napi_set_named_property(env, fingerprintTips, "FINGERPRINT_TIP_GOOD", fingerprintTipGood));
237 NAPI_CALL(env, napi_set_named_property(env, fingerprintTips,
238 "FINGERPRINT_TIP_IMAGER_DIRTY", fingerprintTipImagerDirty));
239 NAPI_CALL(env, napi_set_named_property(env, fingerprintTips,
240 "FINGERPRINT_TIP_INSUFFICIENT", fingerprintTipInsufficient));
241 NAPI_CALL(env, napi_set_named_property(env, fingerprintTips,
242 "FINGERPRINT_TIP_PARTIAL", fingerprintTipPartial));
243 NAPI_CALL(env, napi_set_named_property(env, fingerprintTips,
244 "FINGERPRINT_TIP_TOO_FAST", fingerprintTipTooFast));
245 NAPI_CALL(env, napi_set_named_property(env, fingerprintTips,
246 "FINGERPRINT_TIP_TOO_SLOW", fingerprintTipTooSlow));
247 return fingerprintTips;
248 }
249
Init(napi_env env,napi_value exports)250 napi_value NapiAccountIAMConstant::Init(napi_env env, napi_value exports)
251 {
252 napi_property_descriptor descriptors[] = {
253 DECLARE_NAPI_PROPERTY("AuthType", AuthTypeConstructor(env)),
254 DECLARE_NAPI_PROPERTY("AuthSubType", AuthSubTypeConstructor(env)),
255 DECLARE_NAPI_PROPERTY("AuthTrustLevel", AuthTrustLevelConstructor(env)),
256 DECLARE_NAPI_PROPERTY("GetPropertyType", GetPropertyTypeConstructor(env)),
257 DECLARE_NAPI_PROPERTY("SetPropertyType", SetPropertyTypeConstructor(env)),
258 DECLARE_NAPI_PROPERTY("AuthMethod", AuthMethodConstructor(env)),
259 DECLARE_NAPI_PROPERTY("Module", ModuleConstructor(env)),
260 DECLARE_NAPI_PROPERTY("ResultCode", ResultCodeConstructor(env)),
261 DECLARE_NAPI_PROPERTY("FaceTipsCode", FaceTipsCodeConstructor(env)),
262 DECLARE_NAPI_PROPERTY("FingerprintTips", FingerprintTipsConstructorForInnerkits(env)),
263 };
264 napi_define_properties(env, exports, sizeof(descriptors) / sizeof(napi_property_descriptor), descriptors);
265 return exports;
266 }
267 } // namespace AccountJsKit
268 } // namespace OHOS
269