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_MAX = "PIN_MAX";
37
38 const std::string JSON_AUTH_TYPE = "type";
39 const std::string JSON_WIDGET_CTX_ID = "widgetContextId";
40 const std::string JSON_AUTH_EVENT = "event";
41 const std::string JSON_AUTH_VERSION = "version";
42 const std::string JSON_AUTH_PAYLOAD = "payload";
43 const std::string JSON_LOCKOUT_DURATION = "lockoutDuration";
44 const std::string JSON_REMAIN_ATTEMPTS = "remainAttempts";
45 const std::string JSON_AUTH_RESULT = "result";
46 const std::string JSON_SENSOR_INFO = "sensorInfo";
47 const std::string JSON_AUTH_TIP = "tip";
48 const std::string JSON_AUTH_TITLE = "title";
49 const std::string JSON_AUTH_CMD = "cmd";
50 const std::string JSON_AUTH_PIN_SUB_TYPE = "pinSubType";
51 const std::string JSON_AUTH_WINDOW_MODE = "windowModeType";
52 const std::string JSON_AUTH_NAVI_BTN_TEXT = "navigationButtonText";
53
54 const std::string JSON_UI_EXTENSION_TYPE = "ability.want.params.uiExtensionType";
55 const std::string JSON_USER_IAM_CMD_DATA = "useriamCmdData";
56
57 const std::string JSON_CHALLENGE = "challenge";
58 const std::string JSON_CALLER_BUNDLE_NAME = "callingBundleName";
59 const std::string JSON_CMD_EXTRA_INFO = "extraInfo";
60
61 // utils
Str2AuthType(const std::string & strAuthType)62 AuthType Str2AuthType(const std::string &strAuthType)
63 {
64 std::map<std::string, AuthType> authTypeMap;
65 authTypeMap.emplace(std::make_pair(AUTH_TYPE_ALL, AuthType::ALL));
66 authTypeMap.emplace(std::make_pair(AUTH_TYPE_PIN, AuthType::PIN));
67 authTypeMap.emplace(std::make_pair(AUTH_TYPE_FACE, AuthType::FACE));
68 authTypeMap.emplace(std::make_pair(AUTH_TYPE_FINGER_PRINT, AuthType::FINGERPRINT));
69 auto result = AuthType::ALL;
70 if (authTypeMap.find(strAuthType) != authTypeMap.end()) {
71 result = authTypeMap[strAuthType];
72 }
73 return result;
74 }
75
AuthType2Str(const AuthType & authType)76 std::string AuthType2Str(const AuthType &authType)
77 {
78 std::map<int32_t, std::string> authTypeMap;
79 authTypeMap.emplace(std::make_pair(AuthType::ALL, AUTH_TYPE_ALL));
80 authTypeMap.emplace(std::make_pair(AuthType::PIN, AUTH_TYPE_PIN));
81 authTypeMap.emplace(std::make_pair(AuthType::FACE, AUTH_TYPE_FACE));
82 authTypeMap.emplace(std::make_pair(AuthType::FINGERPRINT, AUTH_TYPE_FINGER_PRINT));
83 std::string result = "";
84 if (authTypeMap.find(authType) != authTypeMap.end()) {
85 result = authTypeMap[authType];
86 }
87 return result;
88 }
89
WinModeType2Str(const WindowModeType & winModeType)90 std::string WinModeType2Str(const WindowModeType &winModeType)
91 {
92 std::map<int32_t, std::string> winModeTypeMap;
93 winModeTypeMap.emplace(std::make_pair(WindowModeType::DIALOG_BOX, WINDOW_MODE_DIALOG));
94 winModeTypeMap.emplace(std::make_pair(WindowModeType::FULLSCREEN, WINDOW_MODE_FULLSCREEN));
95 std::string result = "";
96 if (winModeTypeMap.find(winModeType) != winModeTypeMap.end()) {
97 result = winModeTypeMap[winModeType];
98 }
99 return result;
100 }
101
AuthTypeList() const102 std::vector<AuthType> WidgetNotice::AuthTypeList() const
103 {
104 std::vector<AuthType> authTypeList;
105 for (const auto &type : typeList) {
106 authTypeList.emplace_back(Str2AuthType(type));
107 }
108 return authTypeList;
109 }
110
PinSubType2Str(const PinSubType & subType)111 std::string PinSubType2Str(const PinSubType &subType)
112 {
113 std::map<PinSubType, std::string> pinSubTypeMap;
114 pinSubTypeMap.emplace(std::make_pair(PinSubType::PIN_SIX, PIN_SUB_TYPE_SIX));
115 pinSubTypeMap.emplace(std::make_pair(PinSubType::PIN_NUMBER, PIN_SUB_TYPE_NUM));
116 pinSubTypeMap.emplace(std::make_pair(PinSubType::PIN_MIXED, PIN_SUB_TYPE_MIX));
117 pinSubTypeMap.emplace(std::make_pair(PinSubType::PIN_MAX, PIN_SUB_TYPE_MAX));
118
119 std::string result = "";
120 if (pinSubTypeMap.find(subType) != pinSubTypeMap.end()) {
121 result = pinSubTypeMap[subType];
122 }
123 return result;
124 }
125
to_json(nlohmann::json & jsonNotice,const WidgetNotice & notice)126 void to_json(nlohmann::json &jsonNotice, const WidgetNotice ¬ice)
127 {
128 auto type = nlohmann::json({{JSON_AUTH_TYPE, notice.typeList}});
129 jsonNotice = nlohmann::json({{JSON_WIDGET_CTX_ID, notice.widgetContextId},
130 {JSON_AUTH_EVENT, notice.event},
131 {JSON_AUTH_VERSION, notice.version},
132 {JSON_AUTH_PAYLOAD, type}});
133 }
134
from_json(const nlohmann::json & jsonNotice,WidgetNotice & notice)135 void from_json(const nlohmann::json &jsonNotice, WidgetNotice ¬ice)
136 {
137 if (jsonNotice.find(JSON_WIDGET_CTX_ID) != jsonNotice.end() && jsonNotice[JSON_WIDGET_CTX_ID].is_number()) {
138 jsonNotice.at(JSON_WIDGET_CTX_ID).get_to(notice.widgetContextId);
139 }
140 if (jsonNotice.find(JSON_AUTH_EVENT) != jsonNotice.end() && jsonNotice[JSON_AUTH_EVENT].is_string()) {
141 jsonNotice.at(JSON_AUTH_EVENT).get_to(notice.event);
142 }
143 if (jsonNotice.find(JSON_AUTH_VERSION) != jsonNotice.end() && jsonNotice[JSON_AUTH_VERSION].is_string()) {
144 jsonNotice.at(JSON_AUTH_VERSION).get_to(notice.version);
145 }
146 if (jsonNotice.find(JSON_AUTH_PAYLOAD) != jsonNotice.end() &&
147 jsonNotice[JSON_AUTH_PAYLOAD].find(JSON_AUTH_TYPE) != jsonNotice[JSON_AUTH_PAYLOAD].end() &&
148 jsonNotice[JSON_AUTH_PAYLOAD][JSON_AUTH_TYPE].is_array()) {
149 for (size_t index = 0; index < jsonNotice[JSON_AUTH_PAYLOAD][JSON_AUTH_TYPE].size(); index++) {
150 if (!jsonNotice[JSON_AUTH_PAYLOAD][JSON_AUTH_TYPE].at(index).is_string()) {
151 notice.typeList.clear();
152 break;
153 }
154 notice.typeList.emplace_back(jsonNotice[JSON_AUTH_PAYLOAD][JSON_AUTH_TYPE].at(index).get<std::string>());
155 }
156 }
157 }
158
to_json(nlohmann::json & jsonCommand,const WidgetCommand & command)159 void to_json(nlohmann::json &jsonCommand, const WidgetCommand &command)
160 {
161 std::vector<nlohmann::json> jsonCmdList;
162 for (auto &cmd : command.cmdList) {
163 auto jsonCmd = nlohmann::json({{JSON_AUTH_EVENT, cmd.event},
164 {JSON_AUTH_VERSION, cmd.version}
165 });
166 auto jsonPayload = nlohmann::json({{JSON_AUTH_TYPE, cmd.type}});
167 if (cmd.lockoutDuration != -1) {
168 jsonPayload[JSON_LOCKOUT_DURATION] = cmd.lockoutDuration;
169 }
170 if (cmd.remainAttempts != -1) {
171 jsonPayload[JSON_REMAIN_ATTEMPTS] = cmd.remainAttempts;
172 }
173 if (cmd.event == "CMD_NOTIFY_AUTH_RESULT") {
174 jsonPayload[JSON_AUTH_RESULT] = cmd.result;
175 }
176 if (cmd.sensorInfo != "") {
177 jsonPayload[JSON_SENSOR_INFO] = cmd.sensorInfo;
178 }
179 if (cmd.tip != "") {
180 jsonPayload[JSON_AUTH_TIP] = cmd.tip;
181 }
182 auto jsonCmdExtraInfo = nlohmann::json({{JSON_CHALLENGE, cmd.extraInfo.challenge},
183 {JSON_CALLER_BUNDLE_NAME, cmd.extraInfo.callingBundleName}});
184 jsonPayload[JSON_CMD_EXTRA_INFO] = jsonCmdExtraInfo;
185
186 jsonCmd[JSON_AUTH_PAYLOAD] = jsonPayload;
187 jsonCmdList.push_back(jsonCmd);
188 }
189
190 jsonCommand = nlohmann::json({{JSON_WIDGET_CTX_ID, command.widgetContextId},
191 {JSON_AUTH_TYPE, command.typeList},
192 {JSON_AUTH_TITLE, command.title},
193 {JSON_AUTH_CMD, jsonCmdList}
194 });
195 if (command.pinSubType != "") {
196 jsonCommand[JSON_AUTH_PIN_SUB_TYPE] = command.pinSubType;
197 }
198 if (command.windowModeType != "") {
199 jsonCommand[JSON_AUTH_WINDOW_MODE] = command.windowModeType;
200 }
201 if (command.navigationButtonText != "") {
202 jsonCommand[JSON_AUTH_NAVI_BTN_TEXT] = command.navigationButtonText;
203 }
204 }
205
206 // WidgetCmdParameters
to_json(nlohmann::json & jsWidgetCmdParam,const WidgetCmdParameters & widgetCmdParameters)207 void to_json(nlohmann::json &jsWidgetCmdParam, const WidgetCmdParameters &widgetCmdParameters)
208 {
209 std::vector<nlohmann::json> jsonCmdList;
210 for (auto &cmd : widgetCmdParameters.useriamCmdData.cmdList) {
211 auto jsonCmd = nlohmann::json({{JSON_AUTH_EVENT, cmd.event},
212 {JSON_AUTH_VERSION, cmd.version}
213 });
214 auto jsonPayload = nlohmann::json({{JSON_AUTH_TYPE, cmd.type}});
215 if (cmd.lockoutDuration != -1) {
216 jsonPayload[JSON_LOCKOUT_DURATION] = cmd.lockoutDuration;
217 }
218 if (cmd.remainAttempts != -1) {
219 jsonPayload[JSON_REMAIN_ATTEMPTS] = cmd.remainAttempts;
220 }
221 if (cmd.event == "CMD_NOTIFY_AUTH_RESULT") {
222 jsonPayload[JSON_AUTH_RESULT] = cmd.result;
223 }
224 if (cmd.sensorInfo != "") {
225 jsonPayload[JSON_SENSOR_INFO] = cmd.sensorInfo;
226 }
227 if (cmd.tip != "") {
228 jsonPayload[JSON_AUTH_TIP] = cmd.tip;
229 }
230 jsonCmd[JSON_AUTH_PAYLOAD] = jsonPayload;
231 jsonCmdList.push_back(jsonCmd);
232 }
233
234 auto jsCommand = nlohmann::json({{JSON_WIDGET_CTX_ID, widgetCmdParameters.useriamCmdData.widgetContextId},
235 {JSON_AUTH_TYPE, widgetCmdParameters.useriamCmdData.typeList},
236 {JSON_AUTH_TITLE, widgetCmdParameters.useriamCmdData.title},
237 {JSON_AUTH_CMD, jsonCmdList}
238 });
239
240 if (widgetCmdParameters.useriamCmdData.pinSubType != "") {
241 jsCommand[JSON_AUTH_PIN_SUB_TYPE] = widgetCmdParameters.useriamCmdData.pinSubType;
242 }
243 if (widgetCmdParameters.useriamCmdData.windowModeType != "") {
244 jsCommand[JSON_AUTH_WINDOW_MODE] = widgetCmdParameters.useriamCmdData.windowModeType;
245 }
246 if (widgetCmdParameters.useriamCmdData.navigationButtonText != "") {
247 jsCommand[JSON_AUTH_NAVI_BTN_TEXT] = widgetCmdParameters.useriamCmdData.navigationButtonText;
248 }
249
250 jsWidgetCmdParam = nlohmann::json({{JSON_UI_EXTENSION_TYPE, widgetCmdParameters.uiExtensionType},
251 {JSON_USER_IAM_CMD_DATA, jsCommand}
252 });
253 }
254 } // namespace UserAuth
255 } // namespace UserIam
256 } // namespace OHOS