1 /*
2 * Copyright (c) 2024 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 "password_policy_serializer.h"
17
18 #include "cJSON.h"
19 #include "cjson_check.h"
20
21 namespace OHOS {
22 namespace EDM {
23 const std::string COMPLEXITY_REG = "complexityReg";
24 const std::string VALIDITY_PERIOD = "validityPeriod";
25 const std::string ADDITIONAL_DESCRIPTION = "additionalDescription";
26
Deserialize(const std::string & jsonString,PasswordPolicy & policy)27 bool PasswordSerializer::Deserialize(const std::string &jsonString, PasswordPolicy &policy)
28 {
29 if (jsonString.empty()) {
30 return true;
31 }
32 cJSON* root = cJSON_Parse(jsonString.c_str());
33 if (root == nullptr) {
34 return false;
35 }
36 cJSON* complexityReg = cJSON_GetObjectItem(root, COMPLEXITY_REG.c_str());
37 cJSON* validityPeriod = cJSON_GetObjectItem(root, VALIDITY_PERIOD.c_str());
38 cJSON* additionalDescription = cJSON_GetObjectItem(root, ADDITIONAL_DESCRIPTION.c_str());
39 if (complexityReg == nullptr || validityPeriod == nullptr
40 || additionalDescription == nullptr || !cJSON_IsString(complexityReg)
41 || !cJSON_IsNumber(validityPeriod) || !cJSON_IsString(additionalDescription)) {
42 cJSON_Delete(root);
43 return false;
44 }
45 policy.complexityReg = cJSON_GetStringValue(complexityReg);
46 policy.validityPeriod = cJSON_GetNumberValue(validityPeriod);
47 policy.additionalDescription = cJSON_GetStringValue(additionalDescription);
48 cJSON_Delete(root);
49 EDMLOGI("PasswordSerializer::Deserialize %{public}s", jsonString.c_str());
50 return true;
51 }
52
Serialize(const PasswordPolicy & policy,std::string & jsonString)53 bool PasswordSerializer::Serialize(const PasswordPolicy &policy, std::string &jsonString)
54 {
55 cJSON* root = nullptr;
56 CJSON_CREATE_OBJECT_AND_CHECK(root, false);
57 cJSON_AddStringToObject(root, COMPLEXITY_REG.c_str(), policy.complexityReg.c_str());
58 cJSON_AddNumberToObject(root, VALIDITY_PERIOD.c_str(), policy.validityPeriod);
59 cJSON_AddStringToObject(root, ADDITIONAL_DESCRIPTION.c_str(), policy.additionalDescription.c_str());
60 char *cJsonStr = cJSON_Print(root);
61 if (cJsonStr != nullptr) {
62 jsonString = std::string(cJsonStr);
63 cJSON_free(cJsonStr);
64 }
65 cJSON_Delete(root);
66 EDMLOGI("PasswordSerializer::Serialize %{public}s", jsonString.c_str());
67 return true;
68 }
69
GetPolicy(MessageParcel & data,PasswordPolicy & result)70 bool PasswordSerializer::GetPolicy(MessageParcel &data, PasswordPolicy &result)
71 {
72 result.complexityReg = data.ReadString();
73 result.validityPeriod = data.ReadInt64();
74 result.additionalDescription = data.ReadString();
75 return true;
76 }
77
WritePolicy(MessageParcel & reply,PasswordPolicy & result)78 bool PasswordSerializer::WritePolicy(MessageParcel &reply, PasswordPolicy &result)
79 {
80 return true;
81 }
82
MergePolicy(std::vector<PasswordPolicy> & data,PasswordPolicy & result)83 bool PasswordSerializer::MergePolicy(std::vector<PasswordPolicy> &data, PasswordPolicy &result)
84 {
85 for (auto policy : data) {
86 if (!policy.complexityReg.empty()) {
87 result.complexityReg = policy.complexityReg;
88 }
89 if (policy.validityPeriod != 0) {
90 result.validityPeriod = policy.validityPeriod;
91 }
92 if (!policy.additionalDescription.empty()) {
93 result.additionalDescription = policy.additionalDescription;
94 }
95 }
96 return true;
97 }
98 } // namespace EDM
99 } // namespace OHOS