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