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) {
41         cJSON_Delete(root);
42         return false;
43     }
44     policy.complexityReg = cJSON_GetStringValue(complexityReg);
45     policy.validityPeriod = cJSON_GetNumberValue(validityPeriod);
46     policy.additionalDescription = cJSON_GetStringValue(additionalDescription);
47     cJSON_Delete(root);
48     EDMLOGI("PasswordSerializer::Deserialize %{public}s", jsonString.c_str());
49     return true;
50 }
51 
Serialize(const PasswordPolicy & policy,std::string & jsonString)52 bool PasswordSerializer::Serialize(const PasswordPolicy &policy, std::string &jsonString)
53 {
54     cJSON* root = nullptr;
55     CJSON_CREATE_OBJECT_AND_CHECK(root, false);
56     cJSON_AddStringToObject(root, COMPLEXITY_REG.c_str(), policy.complexityReg.c_str());
57     cJSON_AddNumberToObject(root, VALIDITY_PERIOD.c_str(), policy.validityPeriod);
58     cJSON_AddStringToObject(root, ADDITIONAL_DESCRIPTION.c_str(), policy.additionalDescription.c_str());
59     char *cJsonStr = cJSON_Print(root);
60     if (cJsonStr != nullptr) {
61         jsonString = std::string(cJsonStr);
62         cJSON_free(cJsonStr);
63     }
64     cJSON_Delete(root);
65     EDMLOGI("PasswordSerializer::Deserialize %{public}s", jsonString.c_str());
66     return true;
67 }
68 
GetPolicy(MessageParcel & data,PasswordPolicy & result)69 bool PasswordSerializer::GetPolicy(MessageParcel &data, PasswordPolicy &result)
70 {
71     result.complexityReg = data.ReadString();
72     result.validityPeriod = data.ReadInt64();
73     result.additionalDescription = data.ReadString();
74     return true;
75 }
76 
WritePolicy(MessageParcel & reply,PasswordPolicy & result)77 bool PasswordSerializer::WritePolicy(MessageParcel &reply, PasswordPolicy &result)
78 {
79     return true;
80 }
81 
MergePolicy(std::vector<PasswordPolicy> & data,PasswordPolicy & result)82 bool PasswordSerializer::MergePolicy(std::vector<PasswordPolicy> &data, PasswordPolicy &result)
83 {
84     if (data.empty()) {
85         return false;
86     }
87     result = data.back();
88     return true;
89 }
90 } // namespace EDM
91 } // namespace OHOS