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_APP_ID = "callingAppID";
70
71 const std::string JSON_UI_EXTENSION_TYPE = "ability.want.params.uiExtensionType";
72 const std::string JSON_UI_EXT_NODE_ANGLE = "ability.want.params.uiExtNodeAngle";
73 const std::string JSON_USER_IAM_CMD_DATA = "useriamCmdData";
74 const std::string JSON_SYS_DIALOG_ZORDER = "sysDialogZOrder";
75
76 const std::string JSON_CHALLENGE = "challenge";
77 const std::string JSON_CALLER_BUNDLE_NAME = "callingBundleName";
78 const std::string JSON_CMD_EXTRA_INFO = "extraInfo";
79
80 namespace {
GetJsonPayload(nlohmann::json & jsonPayload,const WidgetCommand::Cmd & cmd,bool setExtraInfo)81 void GetJsonPayload(nlohmann::json &jsonPayload, const WidgetCommand::Cmd &cmd, bool setExtraInfo)
82 {
83 jsonPayload[JSON_AUTH_TYPE] = cmd.type;
84 if (cmd.lockoutDuration != -1) {
85 jsonPayload[JSON_LOCKOUT_DURATION] = cmd.lockoutDuration;
86 }
87 if (cmd.remainAttempts != -1) {
88 jsonPayload[JSON_REMAIN_ATTEMPTS] = cmd.remainAttempts;
89 }
90 if (cmd.event == CMD_NOTIFY_AUTH_RESULT || cmd.result == PIN_EXPIRED) {
91 jsonPayload[JSON_AUTH_RESULT] = cmd.result;
92 }
93 if (cmd.event == CMD_NOTIFY_AUTH_TIP) {
94 jsonPayload[JSON_AUTH_TIP_TYPE] = cmd.tipType;
95 jsonPayload[JSON_AUTH_TIP_INFO] = cmd.tipInfo;
96 }
97 if (!cmd.sensorInfo.empty()) {
98 jsonPayload[JSON_SENSOR_INFO] = cmd.sensorInfo;
99 }
100 if (setExtraInfo) {
101 auto jsonCmdExtraInfo = nlohmann::json({{JSON_CHALLENGE, cmd.extraInfo.challenge},
102 {JSON_CALLER_BUNDLE_NAME, cmd.extraInfo.callingBundleName}});
103 jsonPayload[JSON_CMD_EXTRA_INFO] = jsonCmdExtraInfo;
104 }
105 }
106
GetJsonCmd(nlohmann::json & jsonCommand,const WidgetCommand & command,bool setExtraInfo)107 void GetJsonCmd(nlohmann::json &jsonCommand, const WidgetCommand &command, bool setExtraInfo)
108 {
109 std::vector<nlohmann::json> jsonCmdList;
110 for (auto &cmd : command.cmdList) {
111 auto jsonCmd = nlohmann::json({{JSON_AUTH_EVENT, cmd.event},
112 {JSON_AUTH_VERSION, cmd.version}
113 });
114 nlohmann::json jsonPayload;
115 GetJsonPayload(jsonPayload, cmd, setExtraInfo);
116 jsonCmd[JSON_AUTH_PAYLOAD] = jsonPayload;
117 jsonCmdList.push_back(jsonCmd);
118 }
119
120 jsonCommand = nlohmann::json({{JSON_WIDGET_CTX_ID, command.widgetContextId},
121 {JSON_AUTH_TYPE, command.typeList},
122 {JSON_AUTH_TITLE, command.title},
123 {JSON_AUTH_CMD, jsonCmdList},
124 {JSON_WIDGET_CTX_ID_STR, command.widgetContextIdStr}
125 });
126 if (command.pinSubType != "") {
127 jsonCommand[JSON_AUTH_PIN_SUB_TYPE] = command.pinSubType;
128 }
129 if (command.windowModeType != "") {
130 jsonCommand[JSON_AUTH_WINDOW_MODE] = command.windowModeType;
131 }
132 if (command.navigationButtonText != "") {
133 jsonCommand[JSON_AUTH_NAVI_BTN_TEXT] = command.navigationButtonText;
134 }
135 jsonCommand[JSON_WIDGET_IS_RELOAD] = command.isReload;
136 jsonCommand[JSON_WIDGET_ROTATE_AUTH_TYPE] = command.rotateAuthType;
137 jsonCommand[JSON_WIDGET_CALLING_APP_ID] = command.callingAppID;
138 }
139 }
140
141 // utils
Str2AuthType(const std::string & strAuthType)142 AuthType Str2AuthType(const std::string &strAuthType)
143 {
144 AuthType authType = AuthType::ALL;
145 if (strAuthType.compare(AUTH_TYPE_ALL) == 0) {
146 authType = AuthType::ALL;
147 } else if (strAuthType.compare(AUTH_TYPE_PIN) == 0) {
148 authType = AuthType::PIN;
149 } else if (strAuthType.compare(AUTH_TYPE_FACE) == 0) {
150 authType = AuthType::FACE;
151 } else if (strAuthType.compare(AUTH_TYPE_FINGER_PRINT) == 0) {
152 authType = AuthType::FINGERPRINT;
153 } else if (strAuthType.compare(AUTH_TYPE_PRIVATE_PIN) == 0) {
154 authType = AuthType::PRIVATE_PIN;
155 } else {
156 IAM_LOGE("strAuthType: %{public}s", strAuthType.c_str());
157 }
158 return authType;
159 }
160
AuthType2Str(const AuthType & authType)161 std::string AuthType2Str(const AuthType &authType)
162 {
163 std::string strAuthType = "";
164 switch (authType) {
165 case AuthType::ALL: {
166 strAuthType = AUTH_TYPE_ALL;
167 break;
168 }
169 case AuthType::PIN: {
170 strAuthType = AUTH_TYPE_PIN;
171 break;
172 }
173 case AuthType::FACE: {
174 strAuthType = AUTH_TYPE_FACE;
175 break;
176 }
177 case AuthType::FINGERPRINT: {
178 strAuthType = AUTH_TYPE_FINGER_PRINT;
179 break;
180 }
181 case AuthType::PRIVATE_PIN: {
182 strAuthType = AUTH_TYPE_PRIVATE_PIN;
183 break;
184 }
185 default: {
186 IAM_LOGE("authType: %{public}u", authType);
187 }
188 }
189 return strAuthType;
190 }
191
WinModeType2Str(const WindowModeType & winModeType)192 std::string WinModeType2Str(const WindowModeType &winModeType)
193 {
194 std::map<int32_t, std::string> winModeTypeMap;
195 winModeTypeMap.emplace(std::make_pair(WindowModeType::DIALOG_BOX, WINDOW_MODE_DIALOG));
196 winModeTypeMap.emplace(std::make_pair(WindowModeType::FULLSCREEN, WINDOW_MODE_FULLSCREEN));
197 winModeTypeMap.emplace(std::make_pair(WindowModeType::NONE_INTERRUPTION_DIALOG_BOX,
198 WINDOW_MODE_NONE_INTERRUPTION_DIALOG_BOX));
199 std::string result = "";
200 if (winModeTypeMap.find(winModeType) != winModeTypeMap.end()) {
201 result = winModeTypeMap[winModeType];
202 }
203 return result;
204 }
205
AuthTypeList() const206 std::vector<AuthType> WidgetNotice::AuthTypeList() const
207 {
208 std::vector<AuthType> authTypeList;
209 for (const auto &type : typeList) {
210 authTypeList.emplace_back(Str2AuthType(type));
211 }
212 return authTypeList;
213 }
214
PinSubType2Str(const PinSubType & subType)215 std::string PinSubType2Str(const PinSubType &subType)
216 {
217 std::map<PinSubType, std::string> pinSubTypeMap;
218 pinSubTypeMap.emplace(std::make_pair(PinSubType::PIN_SIX, PIN_SUB_TYPE_SIX));
219 pinSubTypeMap.emplace(std::make_pair(PinSubType::PIN_NUMBER, PIN_SUB_TYPE_NUM));
220 pinSubTypeMap.emplace(std::make_pair(PinSubType::PIN_MIXED, PIN_SUB_TYPE_MIX));
221 pinSubTypeMap.emplace(std::make_pair(PinSubType::PIN_FOUR, PIN_SUB_TYPE_FOUR));
222 pinSubTypeMap.emplace(std::make_pair(PinSubType::PIN_PATTERN, PIN_SUB_TYPE_PATTERN));
223 pinSubTypeMap.emplace(std::make_pair(PinSubType::PIN_MAX, PIN_SUB_TYPE_MAX));
224
225 std::string result = "";
226 if (pinSubTypeMap.find(subType) != pinSubTypeMap.end()) {
227 result = pinSubTypeMap[subType];
228 }
229 return result;
230 }
231
to_json(nlohmann::json & jsonNotice,const WidgetNotice & notice)232 void to_json(nlohmann::json &jsonNotice, const WidgetNotice ¬ice)
233 {
234 auto type = nlohmann::json({{JSON_AUTH_TYPE, notice.typeList},
235 {JSON_AUTH_END_AFTER_FIRST_FAIL, notice.endAfterFirstFail},
236 {JSON_AUTH_INTENT, notice.authIntent}});
237 jsonNotice = nlohmann::json({{JSON_WIDGET_CTX_ID, notice.widgetContextId},
238 {JSON_AUTH_EVENT, notice.event},
239 {JSON_ORIENTATION, notice.orientation},
240 {JSON_NEED_ROTATE, notice.needRotate},
241 {JSON_ALREADY_LOAD, notice.alreadyLoad},
242 {JSON_AUTH_VERSION, notice.version},
243 {JSON_AUTH_PAYLOAD, type}});
244 }
245
isNumberItem(const nlohmann::json & jsonNotice,const std::string item)246 bool isNumberItem(const nlohmann::json &jsonNotice, const std::string item)
247 {
248 if (jsonNotice.find(item) != jsonNotice.end() && jsonNotice[item].is_number()) {
249 return true;
250 }
251 return false;
252 }
253
from_json(const nlohmann::json & jsonNotice,WidgetNotice & notice)254 void from_json(const nlohmann::json &jsonNotice, WidgetNotice ¬ice)
255 {
256 if (isNumberItem(jsonNotice, JSON_WIDGET_CTX_ID)) {
257 jsonNotice.at(JSON_WIDGET_CTX_ID).get_to(notice.widgetContextId);
258 }
259 if (jsonNotice.find(JSON_AUTH_EVENT) != jsonNotice.end() && jsonNotice[JSON_AUTH_EVENT].is_string()) {
260 jsonNotice.at(JSON_AUTH_EVENT).get_to(notice.event);
261 }
262 if (isNumberItem(jsonNotice, JSON_ORIENTATION)) {
263 jsonNotice.at(JSON_ORIENTATION).get_to(notice.orientation);
264 }
265 if (isNumberItem(jsonNotice, JSON_NEED_ROTATE)) {
266 jsonNotice.at(JSON_NEED_ROTATE).get_to(notice.needRotate);
267 }
268 if (isNumberItem(jsonNotice, JSON_ALREADY_LOAD)) {
269 jsonNotice.at(JSON_ALREADY_LOAD).get_to(notice.alreadyLoad);
270 }
271 if (jsonNotice.find(JSON_AUTH_VERSION) != jsonNotice.end() && jsonNotice[JSON_AUTH_VERSION].is_string()) {
272 jsonNotice.at(JSON_AUTH_VERSION).get_to(notice.version);
273 }
274 if (jsonNotice.find(JSON_AUTH_PAYLOAD) == jsonNotice.end()) {
275 return;
276 }
277 if (jsonNotice[JSON_AUTH_PAYLOAD].find(JSON_AUTH_TYPE) != jsonNotice[JSON_AUTH_PAYLOAD].end() &&
278 jsonNotice[JSON_AUTH_PAYLOAD][JSON_AUTH_TYPE].is_array()) {
279 for (size_t index = 0; index < jsonNotice[JSON_AUTH_PAYLOAD][JSON_AUTH_TYPE].size(); index++) {
280 if (!jsonNotice[JSON_AUTH_PAYLOAD][JSON_AUTH_TYPE].at(index).is_string()) {
281 notice.typeList.clear();
282 break;
283 }
284 notice.typeList.emplace_back(jsonNotice[JSON_AUTH_PAYLOAD][JSON_AUTH_TYPE].at(index).get<std::string>());
285 }
286 }
287 if ((jsonNotice[JSON_AUTH_PAYLOAD].find(JSON_AUTH_END_AFTER_FIRST_FAIL) !=
288 jsonNotice[JSON_AUTH_PAYLOAD].end()) &&
289 jsonNotice[JSON_AUTH_PAYLOAD][JSON_AUTH_END_AFTER_FIRST_FAIL].is_boolean()) {
290 jsonNotice[JSON_AUTH_PAYLOAD].at(JSON_AUTH_END_AFTER_FIRST_FAIL).get_to(notice.endAfterFirstFail);
291 }
292 if (isNumberItem(jsonNotice[JSON_AUTH_PAYLOAD], JSON_AUTH_INTENT)) {
293 jsonNotice[JSON_AUTH_PAYLOAD].at(JSON_AUTH_INTENT).get_to(notice.authIntent);
294 }
295 }
296
to_json(nlohmann::json & jsonCommand,const WidgetCommand & command)297 void to_json(nlohmann::json &jsonCommand, const WidgetCommand &command)
298 {
299 GetJsonCmd(jsonCommand, command, true);
300 }
301
302 // WidgetCmdParameters
to_json(nlohmann::json & jsWidgetCmdParam,const WidgetCmdParameters & widgetCmdParameters)303 void to_json(nlohmann::json &jsWidgetCmdParam, const WidgetCmdParameters &widgetCmdParameters)
304 {
305 nlohmann::json jsonCommand;
306 GetJsonCmd(jsonCommand, widgetCmdParameters.useriamCmdData, false);
307
308 jsWidgetCmdParam = nlohmann::json({{JSON_UI_EXTENSION_TYPE, widgetCmdParameters.uiExtensionType},
309 {JSON_SYS_DIALOG_ZORDER, widgetCmdParameters.sysDialogZOrder},
310 {JSON_UI_EXT_NODE_ANGLE, widgetCmdParameters.uiExtNodeAngle},
311 {JSON_USER_IAM_CMD_DATA, jsonCommand}
312 });
313 }
314 } // namespace UserAuth
315 } // namespace UserIam
316 } // namespace OHOS