1 /*
2 * Copyright (c) 2023 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 <map>
17 #include "iam_common_defines.h"
18 #include "iam_logger.h"
19 #include "user_auth_common_defines.h"
20 #include "widget_json.h"
21
22 #define LOG_TAG "USER_AUTH_SA"
23
24 namespace OHOS {
25 namespace UserIam {
26 namespace UserAuth {
27
28 const std::string AUTH_TYPE_PIN = "pin";
29 const std::string AUTH_TYPE_FACE = "face";
30 const std::string AUTH_TYPE_FINGER_PRINT = "fingerprint";
31 const std::string AUTH_TYPE_ALL = "all";
32 const std::string AUTH_TYPE_PRIVATE_PIN = "privatePin";
33
34 const std::string WINDOW_MODE_DIALOG = "DIALOG_BOX";
35 const std::string WINDOW_MODE_FULLSCREEN = "FULLSCREEN";
36 const std::string WINDOW_MODE_NONE_INTERRUPTION_DIALOG_BOX = "NONE_INTERRUPTION_DIALOG_BOX";
37
38 const std::string PIN_SUB_TYPE_SIX = "PIN_SIX";
39 const std::string PIN_SUB_TYPE_NUM = "PIN_NUMBER";
40 const std::string PIN_SUB_TYPE_MIX = "PIN_MIXED";
41 const std::string PIN_SUB_TYPE_FOUR = "PIN_FOUR";
42 const std::string PIN_SUB_TYPE_PATTERN = "PIN_PATTERN";
43 const std::string PIN_SUB_TYPE_MAX = "PIN_MAX";
44
45 const std::string JSON_AUTH_TYPE = "type";
46 const std::string JSON_WIDGET_CTX_ID = "widgetContextId";
47 const std::string JSON_WIDGET_CTX_ID_STR = "widgetContextIdStr";
48 const std::string JSON_AUTH_EVENT = "event";
49 const std::string JSON_AUTH_VERSION = "version";
50 const std::string JSON_AUTH_PAYLOAD = "payload";
51 const std::string JSON_AUTH_END_AFTER_FIRST_FAIL = "endAfterFirstFail";
52 const std::string JSON_AUTH_INTENT = "authIntent";
53 const std::string JSON_ORIENTATION = "orientation";
54 const std::string JSON_NEED_ROTATE = "needRotate";
55 const std::string JSON_ALREADY_LOAD = "alreadyLoad";
56 const std::string JSON_LOCKOUT_DURATION = "lockoutDuration";
57 const std::string JSON_REMAIN_ATTEMPTS = "remainAttempts";
58 const std::string JSON_AUTH_RESULT = "result";
59 const std::string JSON_SENSOR_INFO = "sensorInfo";
60 const std::string JSON_AUTH_TIP_TYPE = "tipType";
61 const std::string JSON_AUTH_TIP_INFO = "tipInfo";
62 const std::string JSON_AUTH_TITLE = "title";
63 const std::string JSON_AUTH_CMD = "cmd";
64 const std::string JSON_AUTH_PIN_SUB_TYPE = "pinSubType";
65 const std::string JSON_AUTH_WINDOW_MODE = "windowModeType";
66 const std::string JSON_AUTH_NAVI_BTN_TEXT = "navigationButtonText";
67 const std::string JSON_WIDGET_IS_RELOAD = "isReload";
68 const std::string JSON_WIDGET_ROTATE_AUTH_TYPE = "rotateAuthType";
69 const std::string JSON_WIDGET_CALLING_PROCESS_NAME = "callingProcessName";
70 const std::string JSON_WIDGET_CALLING_APP_ID = "callingAppID";
71 const std::string JSON_WIDGET_USER_ID = "userId";
72
73 const std::string JSON_UI_EXTENSION_TYPE = "ability.want.params.uiExtensionType";
74 const std::string JSON_UI_EXT_NODE_ANGLE = "ability.want.params.uiExtNodeAngle";
75 const std::string JSON_USER_IAM_CMD_DATA = "useriamCmdData";
76 const std::string JSON_SYS_DIALOG_ZORDER = "sysDialogZOrder";
77 const std::string JSON_IS_ENABLE_ROTATE = "isEnableRotate";
78
79 const std::string JSON_CHALLENGE = "challenge";
80 const std::string JSON_CALLER_BUNDLE_NAME = "callingBundleName";
81 const std::string JSON_CMD_EXTRA_INFO = "extraInfo";
82
83 const uint32_t DISABLE_ROTATE = 0;
84
85 namespace {
GetJsonPayload(nlohmann::json & jsonPayload,const WidgetCommand::Cmd & cmd)86 void GetJsonPayload(nlohmann::json &jsonPayload, const WidgetCommand::Cmd &cmd)
87 {
88 jsonPayload[JSON_AUTH_TYPE] = cmd.type;
89 if (cmd.lockoutDuration != -1) {
90 jsonPayload[JSON_LOCKOUT_DURATION] = cmd.lockoutDuration;
91 }
92 if (cmd.remainAttempts != -1) {
93 jsonPayload[JSON_REMAIN_ATTEMPTS] = cmd.remainAttempts;
94 }
95 if (cmd.event == CMD_NOTIFY_AUTH_RESULT || cmd.result == PIN_EXPIRED) {
96 jsonPayload[JSON_AUTH_RESULT] = cmd.result;
97 }
98 if (cmd.event == CMD_NOTIFY_AUTH_TIP) {
99 jsonPayload[JSON_AUTH_TIP_TYPE] = cmd.tipType;
100 jsonPayload[JSON_AUTH_TIP_INFO] = cmd.tipInfo;
101 }
102 if (!cmd.sensorInfo.empty()) {
103 jsonPayload[JSON_SENSOR_INFO] = cmd.sensorInfo;
104 }
105 auto jsonCmdExtraInfo = nlohmann::json({{JSON_CHALLENGE, cmd.extraInfo.challenge},
106 {JSON_CALLER_BUNDLE_NAME, cmd.extraInfo.callingBundleName}});
107 jsonPayload[JSON_CMD_EXTRA_INFO] = jsonCmdExtraInfo;
108 }
109
GetJsonCmd(nlohmann::json & jsonCommand,const WidgetCommand & command)110 void GetJsonCmd(nlohmann::json &jsonCommand, const WidgetCommand &command)
111 {
112 std::vector<nlohmann::json> jsonCmdList;
113 for (auto &cmd : command.cmdList) {
114 auto jsonCmd = nlohmann::json({{JSON_AUTH_EVENT, cmd.event},
115 {JSON_AUTH_VERSION, cmd.version}
116 });
117 nlohmann::json jsonPayload;
118 GetJsonPayload(jsonPayload, cmd);
119 jsonCmd[JSON_AUTH_PAYLOAD] = jsonPayload;
120 jsonCmdList.push_back(jsonCmd);
121 }
122
123 jsonCommand = nlohmann::json({{JSON_WIDGET_CTX_ID, command.widgetContextId},
124 {JSON_AUTH_TYPE, command.typeList},
125 {JSON_AUTH_TITLE, command.title},
126 {JSON_AUTH_CMD, jsonCmdList},
127 {JSON_WIDGET_CTX_ID_STR, command.widgetContextIdStr}
128 });
129 if (command.pinSubType != "") {
130 jsonCommand[JSON_AUTH_PIN_SUB_TYPE] = command.pinSubType;
131 }
132 if (command.windowModeType != "") {
133 jsonCommand[JSON_AUTH_WINDOW_MODE] = command.windowModeType;
134 }
135 if (command.navigationButtonText != "") {
136 jsonCommand[JSON_AUTH_NAVI_BTN_TEXT] = command.navigationButtonText;
137 }
138 jsonCommand[JSON_WIDGET_IS_RELOAD] = command.isReload;
139 jsonCommand[JSON_WIDGET_ROTATE_AUTH_TYPE] = command.rotateAuthType;
140 jsonCommand[JSON_WIDGET_CALLING_APP_ID] = command.callingAppID;
141 jsonCommand[JSON_WIDGET_CALLING_PROCESS_NAME] = command.callingProcessName;
142 jsonCommand[JSON_WIDGET_USER_ID] = command.userId;
143 }
144 }
145
146 // utils
Str2AuthType(const std::string & strAuthType)147 AuthType Str2AuthType(const std::string &strAuthType)
148 {
149 AuthType authType = AuthType::ALL;
150 if (strAuthType.compare(AUTH_TYPE_ALL) == 0) {
151 authType = AuthType::ALL;
152 } else if (strAuthType.compare(AUTH_TYPE_PIN) == 0) {
153 authType = AuthType::PIN;
154 } else if (strAuthType.compare(AUTH_TYPE_FACE) == 0) {
155 authType = AuthType::FACE;
156 } else if (strAuthType.compare(AUTH_TYPE_FINGER_PRINT) == 0) {
157 authType = AuthType::FINGERPRINT;
158 } else if (strAuthType.compare(AUTH_TYPE_PRIVATE_PIN) == 0) {
159 authType = AuthType::PRIVATE_PIN;
160 } else {
161 IAM_LOGE("strAuthType: %{public}s", strAuthType.c_str());
162 }
163 return authType;
164 }
165
AuthType2Str(const AuthType & authType)166 std::string AuthType2Str(const AuthType &authType)
167 {
168 std::string strAuthType = "";
169 switch (authType) {
170 case AuthType::ALL: {
171 strAuthType = AUTH_TYPE_ALL;
172 break;
173 }
174 case AuthType::PIN: {
175 strAuthType = AUTH_TYPE_PIN;
176 break;
177 }
178 case AuthType::FACE: {
179 strAuthType = AUTH_TYPE_FACE;
180 break;
181 }
182 case AuthType::FINGERPRINT: {
183 strAuthType = AUTH_TYPE_FINGER_PRINT;
184 break;
185 }
186 case AuthType::PRIVATE_PIN: {
187 strAuthType = AUTH_TYPE_PRIVATE_PIN;
188 break;
189 }
190 default: {
191 IAM_LOGE("authType: %{public}u", authType);
192 }
193 }
194 return strAuthType;
195 }
196
WinModeType2Str(const WindowModeType & winModeType)197 std::string WinModeType2Str(const WindowModeType &winModeType)
198 {
199 std::string strWinModeType = "";
200 switch (winModeType) {
201 case WindowModeType::DIALOG_BOX: {
202 strWinModeType = WINDOW_MODE_DIALOG;
203 break;
204 }
205 case WindowModeType::FULLSCREEN: {
206 strWinModeType = WINDOW_MODE_FULLSCREEN;
207 break;
208 }
209 case WindowModeType::NONE_INTERRUPTION_DIALOG_BOX: {
210 strWinModeType = WINDOW_MODE_NONE_INTERRUPTION_DIALOG_BOX;
211 break;
212 }
213 default: {
214 IAM_LOGE("winModeType: %{public}u", winModeType);
215 }
216 }
217 return strWinModeType;
218 }
219
AuthTypeList() const220 std::vector<AuthType> WidgetNotice::AuthTypeList() const
221 {
222 std::vector<AuthType> authTypeList;
223 for (const auto &type : typeList) {
224 authTypeList.emplace_back(Str2AuthType(type));
225 }
226 return authTypeList;
227 }
228
PinSubType2Str(const PinSubType & subType)229 std::string PinSubType2Str(const PinSubType &subType)
230 {
231 std::string strPinSubType = "";
232 switch (subType) {
233 case PinSubType::PIN_SIX: {
234 strPinSubType = PIN_SUB_TYPE_SIX;
235 break;
236 }
237 case PinSubType::PIN_NUMBER: {
238 strPinSubType = PIN_SUB_TYPE_NUM;
239 break;
240 }
241 case PinSubType::PIN_MIXED: {
242 strPinSubType = PIN_SUB_TYPE_MIX;
243 break;
244 }
245 case PinSubType::PIN_FOUR: {
246 strPinSubType = PIN_SUB_TYPE_FOUR;
247 break;
248 }
249 case PinSubType::PIN_PATTERN: {
250 strPinSubType = PIN_SUB_TYPE_PATTERN;
251 break;
252 }
253 case PinSubType::PIN_MAX: {
254 strPinSubType = PIN_SUB_TYPE_MAX;
255 break;
256 }
257 default: {
258 IAM_LOGE("subType: %{public}u", subType);
259 }
260 }
261 return strPinSubType;
262 }
263
to_json(nlohmann::json & jsonNotice,const WidgetNotice & notice)264 void to_json(nlohmann::json &jsonNotice, const WidgetNotice ¬ice)
265 {
266 auto type = nlohmann::json({{JSON_AUTH_TYPE, notice.typeList},
267 {JSON_AUTH_END_AFTER_FIRST_FAIL, notice.endAfterFirstFail},
268 {JSON_AUTH_INTENT, notice.authIntent}});
269 jsonNotice = nlohmann::json({{JSON_WIDGET_CTX_ID, notice.widgetContextId},
270 {JSON_AUTH_EVENT, notice.event},
271 {JSON_ORIENTATION, notice.orientation},
272 {JSON_NEED_ROTATE, notice.needRotate},
273 {JSON_ALREADY_LOAD, notice.alreadyLoad},
274 {JSON_AUTH_VERSION, notice.version},
275 {JSON_AUTH_PAYLOAD, type}});
276 }
277
isNumberItem(const nlohmann::json & jsonNotice,const std::string & item)278 bool isNumberItem(const nlohmann::json &jsonNotice, const std::string &item)
279 {
280 if (jsonNotice.find(item) != jsonNotice.end() && jsonNotice[item].is_number()) {
281 return true;
282 }
283 return false;
284 }
285
from_json(const nlohmann::json & jsonNotice,WidgetNotice & notice)286 void from_json(const nlohmann::json &jsonNotice, WidgetNotice ¬ice)
287 {
288 if (isNumberItem(jsonNotice, JSON_WIDGET_CTX_ID)) {
289 jsonNotice.at(JSON_WIDGET_CTX_ID).get_to(notice.widgetContextId);
290 }
291 if (jsonNotice.find(JSON_AUTH_EVENT) != jsonNotice.end() && jsonNotice[JSON_AUTH_EVENT].is_string()) {
292 jsonNotice.at(JSON_AUTH_EVENT).get_to(notice.event);
293 }
294 if (isNumberItem(jsonNotice, JSON_ORIENTATION)) {
295 jsonNotice.at(JSON_ORIENTATION).get_to(notice.orientation);
296 }
297 if (isNumberItem(jsonNotice, JSON_NEED_ROTATE)) {
298 jsonNotice.at(JSON_NEED_ROTATE).get_to(notice.needRotate);
299 }
300 if (isNumberItem(jsonNotice, JSON_ALREADY_LOAD)) {
301 jsonNotice.at(JSON_ALREADY_LOAD).get_to(notice.alreadyLoad);
302 }
303 if (jsonNotice.find(JSON_AUTH_VERSION) != jsonNotice.end() && jsonNotice[JSON_AUTH_VERSION].is_string()) {
304 jsonNotice.at(JSON_AUTH_VERSION).get_to(notice.version);
305 }
306 if (jsonNotice.find(JSON_AUTH_PAYLOAD) == jsonNotice.end()) {
307 return;
308 }
309 if (jsonNotice[JSON_AUTH_PAYLOAD].find(JSON_AUTH_TYPE) != jsonNotice[JSON_AUTH_PAYLOAD].end() &&
310 jsonNotice[JSON_AUTH_PAYLOAD][JSON_AUTH_TYPE].is_array()) {
311 for (size_t index = 0; index < jsonNotice[JSON_AUTH_PAYLOAD][JSON_AUTH_TYPE].size(); index++) {
312 if (!jsonNotice[JSON_AUTH_PAYLOAD][JSON_AUTH_TYPE].at(index).is_string()) {
313 notice.typeList.clear();
314 break;
315 }
316 notice.typeList.emplace_back(jsonNotice[JSON_AUTH_PAYLOAD][JSON_AUTH_TYPE].at(index).get<std::string>());
317 }
318 }
319 if ((jsonNotice[JSON_AUTH_PAYLOAD].find(JSON_AUTH_END_AFTER_FIRST_FAIL) !=
320 jsonNotice[JSON_AUTH_PAYLOAD].end()) &&
321 jsonNotice[JSON_AUTH_PAYLOAD][JSON_AUTH_END_AFTER_FIRST_FAIL].is_boolean()) {
322 jsonNotice[JSON_AUTH_PAYLOAD].at(JSON_AUTH_END_AFTER_FIRST_FAIL).get_to(notice.endAfterFirstFail);
323 }
324 if (isNumberItem(jsonNotice[JSON_AUTH_PAYLOAD], JSON_AUTH_INTENT)) {
325 jsonNotice[JSON_AUTH_PAYLOAD].at(JSON_AUTH_INTENT).get_to(notice.authIntent);
326 }
327 }
328
to_json(nlohmann::json & jsonCommand,const WidgetCommand & command)329 void to_json(nlohmann::json &jsonCommand, const WidgetCommand &command)
330 {
331 GetJsonCmd(jsonCommand, command);
332 }
333
334 // WidgetCmdParameters
to_json(nlohmann::json & jsWidgetCmdParam,const WidgetCmdParameters & widgetCmdParameters)335 void to_json(nlohmann::json &jsWidgetCmdParam, const WidgetCmdParameters &widgetCmdParameters)
336 {
337 nlohmann::json jsonCommand;
338 GetJsonCmd(jsonCommand, widgetCmdParameters.useriamCmdData);
339
340 jsWidgetCmdParam = nlohmann::json({{JSON_UI_EXTENSION_TYPE, widgetCmdParameters.uiExtensionType},
341 {JSON_SYS_DIALOG_ZORDER, widgetCmdParameters.sysDialogZOrder},
342 {JSON_UI_EXT_NODE_ANGLE, widgetCmdParameters.uiExtNodeAngle},
343 {JSON_IS_ENABLE_ROTATE, DISABLE_ROTATE},
344 {JSON_USER_IAM_CMD_DATA, jsonCommand}
345 });
346 }
347 } // namespace UserAuth
348 } // namespace UserIam
349 } // namespace OHOS