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