1 /*
2 * Copyright (c) 2021-2022 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 "ohos_account_data_deal.h"
17 #include <fstream>
18 #include <iostream>
19 #include <vector>
20 #include "account_error_no.h"
21 #include "account_info.h"
22 #include "account_log_wrapper.h"
23 #include "directory_ex.h"
24 #include "file_ex.h"
25
26 namespace OHOS {
27 namespace AccountSA {
28 namespace {
29 const std::string ACCOUNT_CFG_FILE_NAME = "/account.json";
30 const std::string DATADEAL_JSON_KEY_ACCOUNT_NAME = "account_name";
31 const std::string DATADEAL_JSON_KEY_OPENID = "open_id";
32 const std::string DATADEAL_JSON_KEY_USERID = "user_id";
33 const std::string DATADEAL_JSON_KEY_BIND_TIME = "bind_time";
34 const std::string DATADEAL_JSON_KEY_STATUS = "bind_status";
35 } // namespace
36
OhosAccountDataDeal(const std::string & configFileDir)37 OhosAccountDataDeal::OhosAccountDataDeal(const std::string &configFileDir) : configFileDir_(configFileDir)
38 {
39 initOk_ = false;
40 }
41
Init(std::int32_t userId)42 ErrCode OhosAccountDataDeal::Init(std::int32_t userId)
43 {
44 std::string configFile = configFileDir_ + std::to_string(userId) + ACCOUNT_CFG_FILE_NAME;
45 if (!FileExists(configFile)) {
46 ACCOUNT_LOGI("file %{public}s not exist, create!", configFile.c_str());
47 BuildJsonFileFromScratch(userId);
48 }
49
50 std::ifstream fin(configFile);
51 if (!fin) {
52 ACCOUNT_LOGE("Failed to open config file %{public}s", configFile.c_str());
53 return ERR_ACCOUNT_DATADEAL_INPUT_FILE_ERROR;
54 }
55
56 // NOT-allow exceptions when parse json file
57 std::lock_guard<std::mutex> lock(mutex_);
58 jsonData_ = json::parse(fin, nullptr, false);
59 fin.close();
60 if (!jsonData_.is_structured()) {
61 ACCOUNT_LOGE("Invalid json file, remove");
62 if (RemoveFile(configFile)) {
63 ACCOUNT_LOGE("Remove invalid json file failed");
64 }
65 return ERR_ACCOUNT_DATADEAL_JSON_FILE_CORRUPTION;
66 }
67
68 initOk_ = true;
69 return ERR_OK;
70 }
71
AccountInfoFromJson(AccountInfo & accountInfo,const std::int32_t userId)72 ErrCode OhosAccountDataDeal::AccountInfoFromJson(AccountInfo &accountInfo, const std::int32_t userId)
73 {
74 if (!initOk_) {
75 ACCOUNT_LOGE("not init yet!");
76 return ERR_ACCOUNT_DATADEAL_NOT_READY;
77 }
78
79 std::string configFile = configFileDir_ + std::to_string(userId) + ACCOUNT_CFG_FILE_NAME;
80 if (!FileExists(configFile)) {
81 ACCOUNT_LOGI("file %{public}s not exist, create!", configFile.c_str());
82 BuildJsonFileFromScratch(userId); // create default config file for first login
83 }
84 std::ifstream fin(configFile);
85 if (!fin) {
86 ACCOUNT_LOGE("Failed to open config file %{public}s", configFile.c_str());
87 return ERR_ACCOUNT_DATADEAL_INPUT_FILE_ERROR;
88 }
89
90 // NOT-allow exceptions when parse json file
91 std::lock_guard<std::mutex> lock(mutex_);
92 jsonData_ = json::parse(fin, nullptr, false);
93 fin.close();
94 if (!jsonData_.is_structured()) {
95 ACCOUNT_LOGE("Invalid json file, %{public}s, remove", configFile.c_str());
96 return ERR_ACCOUNT_DATADEAL_JSON_FILE_CORRUPTION;
97 }
98
99 const auto &jsonObjectEnd = jsonData_.end();
100 if (jsonData_.find(DATADEAL_JSON_KEY_ACCOUNT_NAME) != jsonObjectEnd) {
101 accountInfo.ohosAccountName_ = jsonData_.at(DATADEAL_JSON_KEY_ACCOUNT_NAME).get<std::string>();
102 }
103
104 if (jsonData_.find(DATADEAL_JSON_KEY_OPENID) != jsonObjectEnd) {
105 accountInfo.ohosAccountUid_ = jsonData_.at(DATADEAL_JSON_KEY_OPENID).get<std::string>();
106 }
107
108 accountInfo.userId_ = userId;
109
110 if (jsonData_.find(DATADEAL_JSON_KEY_BIND_TIME) != jsonObjectEnd) {
111 accountInfo.bindTime_ = jsonData_.at(DATADEAL_JSON_KEY_BIND_TIME).get<std::time_t>();
112 }
113
114 if (jsonData_.find(DATADEAL_JSON_KEY_STATUS) != jsonObjectEnd) {
115 accountInfo.ohosAccountStatus_ = jsonData_.at(DATADEAL_JSON_KEY_STATUS).get<std::int32_t>();
116 }
117
118 ACCOUNT_LOGI("AccountInfo, ohos account %{public}s status: %{public}d",
119 accountInfo.ohosAccountName_.c_str(), accountInfo.ohosAccountStatus_);
120 return ERR_OK;
121 }
122
AccountInfoToJson(const AccountInfo & accountInfo) const123 ErrCode OhosAccountDataDeal::AccountInfoToJson(const AccountInfo &accountInfo) const
124 {
125 if (!initOk_) {
126 ACCOUNT_LOGE("Not init ok");
127 return ERR_ACCOUNT_DATADEAL_NOT_READY;
128 }
129
130 SaveAccountInfo(accountInfo);
131 return ERR_OK;
132 }
133
SaveAccountInfo(const AccountInfo & accountInfo) const134 void OhosAccountDataDeal::SaveAccountInfo(const AccountInfo &accountInfo) const
135 {
136 nlohmann::json jsonData = json {
137 {DATADEAL_JSON_KEY_BIND_TIME, accountInfo.bindTime_},
138 {DATADEAL_JSON_KEY_USERID, accountInfo.userId_},
139 {DATADEAL_JSON_KEY_OPENID, accountInfo.ohosAccountUid_},
140 {DATADEAL_JSON_KEY_ACCOUNT_NAME, accountInfo.ohosAccountName_},
141 {DATADEAL_JSON_KEY_STATUS, accountInfo.ohosAccountStatus_}
142 };
143 std::string configFile = configFileDir_ + std::to_string(accountInfo.userId_) + ACCOUNT_CFG_FILE_NAME;
144 std::ofstream out(configFile);
145 if (!out) {
146 ACCOUNT_LOGE("Failed to open file %{public}s", configFile.c_str());
147 return;
148 }
149 out << jsonData;
150 out.close();
151 }
152
BuildJsonFileFromScratch(std::int32_t userId) const153 void OhosAccountDataDeal::BuildJsonFileFromScratch(std::int32_t userId) const
154 {
155 AccountInfo accountInfo;
156 accountInfo.userId_ = userId;
157 accountInfo.bindTime_ = 0;
158 accountInfo.ohosAccountUid_ = DEFAULT_OHOS_ACCOUNT_UID;
159 accountInfo.ohosAccountName_ = DEFAULT_OHOS_ACCOUNT_NAME;
160 accountInfo.ohosAccountStatus_ = ACCOUNT_STATE_UNBOUND;
161 accountInfo.digest_ = "";
162 SaveAccountInfo(accountInfo);
163 }
164 } // namespace AccountSA
165 } // namespace OHOS