1 /*
2 * Copyright (c) 2021-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 "app_account_info.h"
17
18 #include "account_log_wrapper.h"
19 #include "nlohmann/json.hpp"
20
21 namespace OHOS {
22 namespace AccountSA {
23 namespace {
24 const std::string OWNER = "owner";
25 const std::string NAME = "name";
26 const std::string EXTRA_INFO = "extraInfo";
27 const std::string SYNC_ENABLE = "syncEnable";
28 const std::string AUTHORIZED_APPS = "authorizedApps";
29 const std::string ASSOCIATED_DATA = "associatedData";
30 const std::string ACCOUNT_CREDENTIAL = "accountCredential";
31 const std::string OAUTH_TOKEN = "oauthToken";
32 const std::string OAUTH_TOKEN_INFOS = "tokenInfos";
33 const std::string OAUTH_TYPE = "authType";
34 const std::string OAUTH_AUTH_LIST = "authList";
35 const std::string OAUTH_TOKEN_TO_TYPE = "tokenToType";
36 const std::string HYPHEN = "#";
37 constexpr uint32_t APP_INDEX = 0;
38 constexpr uint32_t MAX_TOKEN_NUMBER = 128;
39 constexpr uint32_t MAX_OAUTH_LIST_SIZE = 512;
40 constexpr uint32_t MAX_ASSOCIATED_DATA_NUMBER = 1024;
41 } // namespace
42
AppAccountInfo()43 AppAccountInfo::AppAccountInfo()
44 {
45 owner_ = "";
46 name_ = "";
47 appIndex_ = APP_INDEX;
48 extraInfo_ = "";
49 authorizedApps_.clear();
50 syncEnable_ = false;
51 associatedData_ = "";
52 accountCredential_ = "";
53 oauthTokens_.clear();
54 }
55
AppAccountInfo(const std::string & name,const std::string & owner)56 AppAccountInfo::AppAccountInfo(const std::string &name, const std::string &owner)
57 {
58 name_ = name;
59 owner_ = owner;
60 appIndex_ = APP_INDEX;
61 extraInfo_ = "";
62 authorizedApps_.clear();
63 syncEnable_ = false;
64 associatedData_ = "";
65 accountCredential_ = "";
66 oauthTokens_.clear();
67 }
68
GetOwner()69 std::string AppAccountInfo::GetOwner()
70 {
71 return owner_;
72 }
73
GetOwner(std::string & owner)74 void AppAccountInfo::GetOwner(std::string &owner)
75 {
76 owner = owner_;
77 }
78
SetOwner(const std::string & owner)79 void AppAccountInfo::SetOwner(const std::string &owner)
80 {
81 owner_ = owner;
82 }
83
GetName()84 std::string AppAccountInfo::GetName()
85 {
86 return name_;
87 }
88
GetName(std::string & name) const89 void AppAccountInfo::GetName(std::string &name) const
90 {
91 name = name_;
92 }
93
SetName(const std::string & name)94 void AppAccountInfo::SetName(const std::string &name)
95 {
96 name_ = name;
97 }
98
GetAppIndex()99 uint32_t AppAccountInfo::GetAppIndex()
100 {
101 return appIndex_;
102 }
103
SetAppIndex(const uint32_t & appIndex)104 void AppAccountInfo::SetAppIndex(const uint32_t &appIndex)
105 {
106 appIndex_ = appIndex;
107 }
108
GetExtraInfo(std::string & extraInfo) const109 void AppAccountInfo::GetExtraInfo(std::string &extraInfo) const
110 {
111 extraInfo = extraInfo_;
112 }
113
SetExtraInfo(const std::string & extraInfo)114 void AppAccountInfo::SetExtraInfo(const std::string &extraInfo)
115 {
116 extraInfo_ = extraInfo;
117 }
118
EnableAppAccess(const std::string & authorizedApp,const uint32_t apiVersion)119 ErrCode AppAccountInfo::EnableAppAccess(const std::string &authorizedApp, const uint32_t apiVersion)
120 {
121 auto it = authorizedApps_.emplace(authorizedApp);
122 if (!it.second && apiVersion < Constants::API_VERSION9) {
123 return ERR_APPACCOUNT_SERVICE_ENABLE_APP_ACCESS_ALREADY_EXISTS;
124 }
125 return ERR_OK;
126 }
127
DisableAppAccess(const std::string & authorizedApp,const uint32_t apiVersion)128 ErrCode AppAccountInfo::DisableAppAccess(const std::string &authorizedApp, const uint32_t apiVersion)
129 {
130 auto result = authorizedApps_.erase(authorizedApp);
131 if (result == 0 && apiVersion < Constants::API_VERSION9) {
132 return ERR_APPACCOUNT_SERVICE_DISABLE_APP_ACCESS_NOT_EXISTED;
133 }
134 return ERR_OK;
135 }
136
CheckAppAccess(const std::string & authorizedApp,bool & isAccessible)137 ErrCode AppAccountInfo::CheckAppAccess(const std::string &authorizedApp, bool &isAccessible)
138 {
139 isAccessible = false;
140 auto it = authorizedApps_.find(authorizedApp);
141 if (it != authorizedApps_.end()) {
142 isAccessible = true;
143 }
144 return ERR_OK;
145 }
146
GetAuthorizedApps(std::set<std::string> & apps) const147 void AppAccountInfo::GetAuthorizedApps(std::set<std::string> &apps) const
148 {
149 apps = authorizedApps_;
150 }
151
SetAuthorizedApps(const std::set<std::string> & apps)152 void AppAccountInfo::SetAuthorizedApps(const std::set<std::string> &apps)
153 {
154 authorizedApps_ = apps;
155 }
156
GetSyncEnable(bool & syncEnable) const157 void AppAccountInfo::GetSyncEnable(bool &syncEnable) const
158 {
159 syncEnable = syncEnable_;
160 }
161
SetSyncEnable(const bool & syncEnable)162 void AppAccountInfo::SetSyncEnable(const bool &syncEnable)
163 {
164 syncEnable_ = syncEnable;
165 }
166
InitCustomData(const std::map<std::string,std::string> & data)167 ErrCode AppAccountInfo::InitCustomData(const std::map<std::string, std::string> &data)
168 {
169 Json jsonObject = data;
170 try {
171 associatedData_ = jsonObject.dump();
172 } catch (Json::type_error& err) {
173 ACCOUNT_LOGE("failed to dump json object, reason: %{public}s", err.what());
174 return ERR_ACCOUNT_COMMON_DUMP_JSON_ERROR;
175 }
176 return ERR_OK;
177 }
178
GetAllAssociatedData(std::map<std::string,std::string> & data) const179 ErrCode AppAccountInfo::GetAllAssociatedData(std::map<std::string, std::string> &data) const
180 {
181 auto jsonObject = Json::parse(associatedData_, nullptr, false);
182 if (!jsonObject.is_object()) {
183 ACCOUNT_LOGE("jsonObject is_discarded");
184 return ERR_APPACCOUNT_SERVICE_GET_ASSOCIATED_DATA;
185 }
186 try {
187 data = jsonObject.get<std::map<std::string, std::string>>();
188 } catch (Json::type_error& err) {
189 ACCOUNT_LOGE("failed to convert json object to map, reason: %{public}s", err.what());
190 return ERR_APPACCOUNT_SERVICE_GET_ASSOCIATED_DATA;
191 }
192 return ERR_OK;
193 }
194
GetAssociatedData(const std::string & key,std::string & value) const195 ErrCode AppAccountInfo::GetAssociatedData(const std::string &key, std::string &value) const
196 {
197 auto jsonObject = Json::parse(associatedData_, nullptr, false);
198 if (jsonObject.is_discarded()) {
199 ACCOUNT_LOGI("jsonObject is_discarded");
200 jsonObject = Json::object();
201 }
202
203 if (jsonObject.find(key) == jsonObject.end()) {
204 ACCOUNT_LOGE("failed to find value, key = %{public}s", key.c_str());
205 return ERR_APPACCOUNT_SERVICE_ASSOCIATED_DATA_KEY_NOT_EXIST;
206 }
207
208 value = jsonObject.at(key);
209 return ERR_OK;
210 }
211
SetAssociatedData(const std::string & key,const std::string & value)212 ErrCode AppAccountInfo::SetAssociatedData(const std::string &key, const std::string &value)
213 {
214 auto jsonObject = Json::parse(associatedData_, nullptr, false);
215 if (jsonObject.is_discarded() || (!jsonObject.is_object())) {
216 ACCOUNT_LOGI("jsonObject is discarded");
217 jsonObject = Json::object();
218 }
219 auto it = jsonObject.find(key);
220 if (it == jsonObject.end()) {
221 if (jsonObject.size() >= MAX_ASSOCIATED_DATA_NUMBER) {
222 ACCOUNT_LOGW("associated data is over size, the max number is: %{public}d", MAX_ASSOCIATED_DATA_NUMBER);
223 return ERR_APPACCOUNT_SERVICE_ASSOCIATED_DATA_OVER_SIZE;
224 }
225 jsonObject.emplace(key, value);
226 } else {
227 jsonObject[key] = value;
228 }
229
230 try {
231 associatedData_ = jsonObject.dump();
232 } catch (Json::type_error& err) {
233 ACCOUNT_LOGE("failed to dump json object, reason: %{public}s", err.what());
234 return ERR_ACCOUNT_COMMON_DUMP_JSON_ERROR;
235 }
236 return ERR_OK;
237 }
238
GetAccountCredential(const std::string & credentialType,std::string & credential) const239 ErrCode AppAccountInfo::GetAccountCredential(const std::string &credentialType, std::string &credential) const
240 {
241 auto jsonObject = Json::parse(accountCredential_, nullptr, false);
242 if (jsonObject.is_discarded()) {
243 jsonObject = Json::object();
244 }
245
246 if (jsonObject.find(credentialType) == jsonObject.end()) {
247 ACCOUNT_LOGE("failed to find value, credentialType = %{public}s", credentialType.c_str());
248 return ERR_APPACCOUNT_SERVICE_ACCOUNT_CREDENTIAL_NOT_EXIST;
249 }
250
251 credential = jsonObject.at(credentialType);
252 return ERR_OK;
253 }
254
SetAccountCredential(const std::string & credentialType,const std::string & credential,bool isDelete)255 ErrCode AppAccountInfo::SetAccountCredential(
256 const std::string &credentialType, const std::string &credential, bool isDelete)
257 {
258 auto jsonObject = Json::parse(accountCredential_, nullptr, false);
259 if (jsonObject.is_discarded() || (!jsonObject.is_object())) {
260 ACCOUNT_LOGI("jsonObject is discarded");
261 jsonObject = Json::object();
262 }
263
264 if (isDelete) {
265 auto ret = jsonObject.erase(credentialType);
266 if (ret == 0) {
267 return ERR_APPACCOUNT_SERVICE_ACCOUNT_CREDENTIAL_NOT_EXIST;
268 }
269 } else {
270 jsonObject[credentialType] = credential;
271 }
272
273 try {
274 accountCredential_ = jsonObject.dump();
275 } catch (Json::type_error& err) {
276 ACCOUNT_LOGE("failed to dump json object, reason: %{public}s", err.what());
277 return ERR_ACCOUNT_COMMON_DUMP_JSON_ERROR;
278 }
279 return ERR_OK;
280 }
281
GetOAuthToken(const std::string & authType,std::string & token,const uint32_t apiVersion) const282 ErrCode AppAccountInfo::GetOAuthToken(const std::string &authType, std::string &token, const uint32_t apiVersion) const
283 {
284 token = "";
285 auto it = oauthTokens_.find(authType);
286 if (apiVersion >= Constants::API_VERSION9) {
287 if ((it == oauthTokens_.end()) || (!it->second.status)) {
288 ACCOUNT_LOGE("oauth token not exist");
289 return ERR_APPACCOUNT_SERVICE_OAUTH_TOKEN_NOT_EXIST;
290 }
291 } else {
292 if ((it == oauthTokens_.end()) || (it->second.token.empty())) {
293 ACCOUNT_LOGE("oauth token not exist");
294 return ERR_APPACCOUNT_SERVICE_OAUTH_TOKEN_NOT_EXIST;
295 }
296 }
297 token = it->second.token;
298 return ERR_OK;
299 }
300
SetOAuthToken(const std::string & authType,const std::string & token)301 ErrCode AppAccountInfo::SetOAuthToken(const std::string &authType, const std::string &token)
302 {
303 auto it = oauthTokens_.find(authType);
304 if (it != oauthTokens_.end()) {
305 it->second.token = token;
306 return ERR_OK;
307 }
308 if (oauthTokens_.size() >= MAX_TOKEN_NUMBER) {
309 ACCOUNT_LOGE("too many types of oauth token, capacity for each account is %{public}d", MAX_TOKEN_NUMBER);
310 return ERR_APPACCOUNT_SERVICE_OAUTH_TOKEN_MAX_SIZE;
311 }
312 OAuthTokenInfo tokenInfo;
313 tokenInfo.token = token;
314 oauthTokens_.emplace(authType, tokenInfo);
315 return ERR_OK;
316 }
317
DeleteOAuthToken(const std::string & authType,const std::string & token)318 ErrCode AppAccountInfo::DeleteOAuthToken(const std::string &authType, const std::string &token)
319 {
320 auto it = oauthTokens_.find(authType);
321 if ((it != oauthTokens_.end()) && (it->second.token == token)) {
322 it->second.token = "";
323 return ERR_OK;
324 }
325 return ERR_APPACCOUNT_SERVICE_OAUTH_TOKEN_NOT_EXIST;
326 }
327
DeleteAuthToken(const std::string & authType,const std::string & token,bool isOwnerSelf)328 ErrCode AppAccountInfo::DeleteAuthToken(const std::string &authType, const std::string &token, bool isOwnerSelf)
329 {
330 auto it = oauthTokens_.find(authType);
331 if (it == oauthTokens_.end()) {
332 return ERR_APPACCOUNT_SERVICE_OAUTH_TOKEN_NOT_EXIST;
333 }
334 if (it->second.token == token && isOwnerSelf) {
335 oauthTokens_.erase(it);
336 } else {
337 it->second.status = false;
338 }
339 return ERR_OK;
340 }
341
SetOAuthTokenVisibility(const std::string & authType,const std::string & bundleName,bool isVisible,const uint32_t apiVersion)342 ErrCode AppAccountInfo::SetOAuthTokenVisibility(
343 const std::string &authType, const std::string &bundleName, bool isVisible, const uint32_t apiVersion)
344 {
345 if (bundleName == owner_) {
346 return ERR_OK;
347 }
348 auto it = oauthTokens_.find(authType);
349 if (it == oauthTokens_.end()) {
350 if (apiVersion >= Constants::API_VERSION9) {
351 return ERR_APPACCOUNT_SERVICE_OAUTH_TYPE_NOT_EXIST;
352 }
353 if (!isVisible) {
354 return ERR_OK;
355 }
356 if (oauthTokens_.size() >= MAX_TOKEN_NUMBER) {
357 ACCOUNT_LOGE("too many types of oauth token, capacity for each account is %{public}d", MAX_TOKEN_NUMBER);
358 return ERR_APPACCOUNT_SERVICE_OAUTH_TOKEN_MAX_SIZE;
359 }
360 OAuthTokenInfo tokenInfo;
361 tokenInfo.authList.emplace(bundleName);
362 oauthTokens_.emplace(authType, tokenInfo);
363 return ERR_OK;
364 }
365 if (!isVisible) {
366 it->second.authList.erase(bundleName);
367 return ERR_OK;
368 }
369 it->second.authList.emplace(bundleName);
370 if (it->second.authList.size() > MAX_OAUTH_LIST_SIZE) {
371 ACCOUNT_LOGE("the authorization list is too large, whose capacity for each authType is %{public}d",
372 MAX_OAUTH_LIST_SIZE);
373 it->second.authList.erase(bundleName);
374 return ERR_APPACCOUNT_SERVICE_OAUTH_LIST_MAX_SIZE;
375 }
376 return ERR_OK;
377 }
378
CheckOAuthTokenVisibility(const std::string & authType,const std::string & bundleName,bool & isVisible,const uint32_t apiVersion) const379 ErrCode AppAccountInfo::CheckOAuthTokenVisibility(
380 const std::string &authType, const std::string &bundleName, bool &isVisible, const uint32_t apiVersion) const
381 {
382 isVisible = false;
383 if (bundleName == owner_) {
384 isVisible = true;
385 return ERR_OK;
386 }
387 auto tokenInfoIt = oauthTokens_.find(authType);
388 if (tokenInfoIt == oauthTokens_.end()) {
389 if (apiVersion >= Constants::API_VERSION9) {
390 return ERR_APPACCOUNT_SERVICE_OAUTH_TYPE_NOT_EXIST;
391 } else {
392 return ERR_OK;
393 }
394 }
395 std::set<std::string> authList = tokenInfoIt->second.authList;
396 auto it = authList.find(bundleName);
397 if (it != authList.end()) {
398 isVisible = true;
399 }
400 return ERR_OK;
401 }
402
GetAllOAuthTokens(std::vector<OAuthTokenInfo> & tokenInfos) const403 ErrCode AppAccountInfo::GetAllOAuthTokens(std::vector<OAuthTokenInfo> &tokenInfos) const
404 {
405 tokenInfos.clear();
406 for (auto it = oauthTokens_.begin(); it != oauthTokens_.end(); ++it) {
407 tokenInfos.push_back(it->second);
408 }
409 return ERR_OK;
410 }
411
GetOAuthList(const std::string & authType,std::set<std::string> & oauthList,const uint32_t apiVersion) const412 ErrCode AppAccountInfo::GetOAuthList(
413 const std::string &authType, std::set<std::string> &oauthList, const uint32_t apiVersion) const
414 {
415 oauthList.clear();
416 auto it = oauthTokens_.find(authType);
417 if (it == oauthTokens_.end()) {
418 if (apiVersion >= Constants::API_VERSION9) {
419 return ERR_APPACCOUNT_SERVICE_OAUTH_TYPE_NOT_EXIST;
420 } else {
421 return ERR_OK;
422 }
423 }
424 oauthList = it->second.authList;
425 return ERR_OK;
426 }
427
Marshalling(Parcel & parcel) const428 bool AppAccountInfo::Marshalling(Parcel &parcel) const
429 {
430 if (!parcel.WriteString(owner_)) {
431 ACCOUNT_LOGE("failed to write string for owner_");
432 return false;
433 }
434
435 if (!parcel.WriteString(name_)) {
436 ACCOUNT_LOGE("failed to write string for name_");
437 return false;
438 }
439
440 if (!parcel.WriteString(extraInfo_)) {
441 ACCOUNT_LOGE("failed to write string for extraInfo_");
442 return false;
443 }
444
445 if (!WriteStringSet(authorizedApps_, parcel)) {
446 ACCOUNT_LOGE("failed to write string set for authorizedApps_");
447 return false;
448 }
449
450 if (!parcel.WriteBool(syncEnable_)) {
451 ACCOUNT_LOGE("failed to write bool for syncEnable_");
452 return false;
453 }
454
455 if (!parcel.WriteString(associatedData_)) {
456 ACCOUNT_LOGE("failed to write string for associatedData_");
457 return false;
458 }
459
460 if (!parcel.WriteString(accountCredential_)) {
461 ACCOUNT_LOGE("failed to write string for accountCredential_");
462 return false;
463 }
464
465 if (!WriteTokenInfos(oauthTokens_, parcel)) {
466 ACCOUNT_LOGE("failed to write string map for oauthTokens_");
467 return false;
468 }
469 return true;
470 }
471
Unmarshalling(Parcel & parcel)472 AppAccountInfo *AppAccountInfo::Unmarshalling(Parcel &parcel)
473 {
474 AppAccountInfo *appAccountInfo = new (std::nothrow) AppAccountInfo();
475
476 if ((appAccountInfo != nullptr) && (!appAccountInfo->ReadFromParcel(parcel))) {
477 ACCOUNT_LOGE("failed to read from parcel");
478 delete appAccountInfo;
479 appAccountInfo = nullptr;
480 }
481
482 return appAccountInfo;
483 }
484
ToJson() const485 Json AppAccountInfo::ToJson() const
486 {
487 auto tokenArray = Json::array();
488 for (auto it = oauthTokens_.begin(); it != oauthTokens_.end(); ++it) {
489 if ((it->second.token.empty()) && (it->second.authList.size() == 0)) {
490 continue;
491 }
492 auto tokenObject = Json {
493 {OAUTH_TYPE, it->first},
494 {OAUTH_TOKEN, it->second.token},
495 {OAUTH_AUTH_LIST, it->second.authList}
496 };
497 tokenArray.push_back(tokenObject);
498 }
499 auto jsonObject = Json {
500 {OWNER, owner_},
501 {NAME, name_},
502 {EXTRA_INFO, extraInfo_},
503 {AUTHORIZED_APPS, authorizedApps_},
504 {SYNC_ENABLE, syncEnable_},
505 {ASSOCIATED_DATA, associatedData_},
506 {ACCOUNT_CREDENTIAL, accountCredential_},
507 {OAUTH_TOKEN_INFOS, tokenArray},
508 };
509
510 return jsonObject;
511 }
512
ParseTokenInfosFromJson(const Json & jsonObject)513 void AppAccountInfo::ParseTokenInfosFromJson(const Json &jsonObject)
514 {
515 oauthTokens_.clear();
516 for (auto it = jsonObject.begin(); it != jsonObject.end(); ++it) {
517 OAuthTokenInfo tokenInfo;
518 if (it->find(OAUTH_TOKEN) != it->end()) {
519 it->at(OAUTH_TOKEN).get_to(tokenInfo.token);
520 }
521 if (it->find(OAUTH_TYPE) != it->end()) {
522 it->at(OAUTH_TYPE).get_to(tokenInfo.authType);
523 }
524 if (it->find(OAUTH_AUTH_LIST) != it->end()) {
525 it->at(OAUTH_AUTH_LIST).get_to(tokenInfo.authList);
526 }
527 oauthTokens_.emplace(tokenInfo.authType, tokenInfo);
528 }
529 }
530
FromJson(const Json & jsonObject)531 void AppAccountInfo::FromJson(const Json &jsonObject)
532 {
533 const auto &jsonObjectEnd = jsonObject.end();
534
535 OHOS::AccountSA::GetDataByType<std::string>(
536 jsonObject, jsonObjectEnd, OWNER, owner_, OHOS::AccountSA::JsonType::STRING);
537 OHOS::AccountSA::GetDataByType<std::string>(
538 jsonObject, jsonObjectEnd, NAME, name_, OHOS::AccountSA::JsonType::STRING);
539 OHOS::AccountSA::GetDataByType<std::string>(
540 jsonObject, jsonObjectEnd, EXTRA_INFO, extraInfo_, OHOS::AccountSA::JsonType::STRING);
541 OHOS::AccountSA::GetDataByType<bool>(
542 jsonObject, jsonObjectEnd, SYNC_ENABLE, syncEnable_, OHOS::AccountSA::JsonType::BOOLEAN);
543 OHOS::AccountSA::GetDataByType<std::set<std::string>>(
544 jsonObject, jsonObjectEnd, AUTHORIZED_APPS, authorizedApps_, OHOS::AccountSA::JsonType::ARRAY);
545 OHOS::AccountSA::GetDataByType<std::string>(
546 jsonObject, jsonObjectEnd, ASSOCIATED_DATA, associatedData_, OHOS::AccountSA::JsonType::STRING);
547 OHOS::AccountSA::GetDataByType<std::string>(
548 jsonObject, jsonObjectEnd, ACCOUNT_CREDENTIAL, accountCredential_, OHOS::AccountSA::JsonType::STRING);
549 if (jsonObject.find(OAUTH_TOKEN_INFOS) != jsonObjectEnd) {
550 ParseTokenInfosFromJson(jsonObject.at(OAUTH_TOKEN_INFOS));
551 }
552 }
553
ToString() const554 std::string AppAccountInfo::ToString() const
555 {
556 auto jsonObject = ToJson();
557 try {
558 return jsonObject.dump();
559 } catch (Json::type_error& err) {
560 ACCOUNT_LOGE("failed to dump json object, reason: %{public}s", err.what());
561 return "";
562 }
563 }
564
GetPrimeKey() const565 std::string AppAccountInfo::GetPrimeKey() const
566 {
567 return (owner_ + HYPHEN + std::to_string(appIndex_) + HYPHEN + name_ + HYPHEN);
568 }
569
ReadFromParcel(Parcel & parcel)570 bool AppAccountInfo::ReadFromParcel(Parcel &parcel)
571 {
572 if (!parcel.ReadString(owner_)) {
573 ACCOUNT_LOGE("failed to read string for owner_");
574 return false;
575 }
576
577 if (!parcel.ReadString(name_)) {
578 ACCOUNT_LOGE("failed to read string for name_");
579 return false;
580 }
581
582 if (!parcel.ReadString(extraInfo_)) {
583 ACCOUNT_LOGE("failed to read string for extraInfo_");
584 return false;
585 }
586
587 if (!ReadStringSet(authorizedApps_, parcel)) {
588 ACCOUNT_LOGE("failed to read string set for authorizedApps_");
589 return false;
590 }
591
592 if (!parcel.ReadBool(syncEnable_)) {
593 ACCOUNT_LOGE("failed to read string for syncEnable_");
594 return false;
595 }
596
597 if (!parcel.ReadString(associatedData_)) {
598 ACCOUNT_LOGE("failed to read string for associatedData_");
599 return false;
600 }
601
602 if (!parcel.ReadString(accountCredential_)) {
603 ACCOUNT_LOGE("failed to read string for accountCredential_");
604 return false;
605 }
606
607 if (!ReadTokenInfos(oauthTokens_, parcel)) {
608 ACCOUNT_LOGE("failed to read string map for oauthTokens_");
609 return false;
610 }
611 return true;
612 }
613
WriteStringSet(const std::set<std::string> & stringSet,Parcel & data) const614 bool AppAccountInfo::WriteStringSet(const std::set<std::string> &stringSet, Parcel &data) const
615 {
616 if (!data.WriteUint32(stringSet.size())) {
617 ACCOUNT_LOGE("failed to WriteInt32 for stringSet.size()");
618 return false;
619 }
620
621 for (auto it : stringSet) {
622 if (!data.WriteString(it)) {
623 ACCOUNT_LOGE("failed to WriteString for it");
624 return false;
625 }
626 }
627
628 return true;
629 }
630
ReadStringSet(std::set<std::string> & stringSet,Parcel & data)631 bool AppAccountInfo::ReadStringSet(std::set<std::string> &stringSet, Parcel &data)
632 {
633 uint32_t size = 0;
634 if (!data.ReadUint32(size)) {
635 ACCOUNT_LOGE("failed to ReadInt32 for size");
636 return false;
637 }
638
639 stringSet.clear();
640 for (uint32_t index = 0; index < size; index += 1) {
641 std::string it = data.ReadString();
642 if (it.size() == 0) {
643 ACCOUNT_LOGE("failed to ReadString for it");
644 return false;
645 }
646 stringSet.emplace(it);
647 }
648
649 return true;
650 }
651
WriteStringMap(const std::map<std::string,std::string> & stringMap,Parcel & data) const652 bool AppAccountInfo::WriteStringMap(const std::map<std::string, std::string> &stringMap, Parcel &data) const
653 {
654 if (!data.WriteInt32(stringMap.size())) {
655 ACCOUNT_LOGE("failed to WriteInt32 for stringSet.size()");
656 return false;
657 }
658
659 for (auto& it : stringMap) {
660 if (!data.WriteString(it.first)) {
661 ACCOUNT_LOGE("failed to WriteString for authType");
662 return false;
663 }
664 if (!data.WriteString(it.second)) {
665 ACCOUNT_LOGE("failed to WriteString for token");
666 return false;
667 }
668 }
669
670 return true;
671 }
672
WriteTokenInfos(const std::map<std::string,OAuthTokenInfo> & tokenInfos,Parcel & data) const673 bool AppAccountInfo::WriteTokenInfos(const std::map<std::string, OAuthTokenInfo> &tokenInfos, Parcel &data) const
674 {
675 if (!data.WriteUint32(tokenInfos.size())) {
676 ACCOUNT_LOGE("failed to WriteInt32 for stringSet.size()");
677 return false;
678 }
679 for (auto& it : tokenInfos) {
680 if (!data.WriteString(it.first)) {
681 ACCOUNT_LOGE("failed to WriteString for authType");
682 return false;
683 }
684 if (!data.WriteString(it.second.token)) {
685 ACCOUNT_LOGE("failed to WriteString for token");
686 return false;
687 }
688 if (!WriteStringSet(it.second.authList, data)) {
689 ACCOUNT_LOGE("failed to WriteString for authList");
690 return false;
691 }
692 }
693 return true;
694 }
695
ReadStringMap(std::map<std::string,std::string> & stringMap,Parcel & data)696 bool AppAccountInfo::ReadStringMap(std::map<std::string, std::string> &stringMap, Parcel &data)
697 {
698 int32_t size = 0;
699 if (!data.ReadInt32(size)) {
700 ACCOUNT_LOGE("failed to ReadInt32 for size");
701 return false;
702 }
703
704 stringMap.clear();
705 for (int32_t index = 0; index < size; ++index) {
706 std::string key;
707 std::string value;
708 if (!data.ReadString(key)) {
709 ACCOUNT_LOGE("failed to ReadString for key");
710 return false;
711 }
712 if (!data.ReadString(value)) {
713 ACCOUNT_LOGE("failed to ReadString for value");
714 return false;
715 }
716 stringMap.emplace(key, value);
717 }
718
719 return true;
720 }
721
ReadTokenInfos(std::map<std::string,OAuthTokenInfo> & tokenInfos,Parcel & data)722 bool AppAccountInfo::ReadTokenInfos(std::map<std::string, OAuthTokenInfo> &tokenInfos, Parcel &data)
723 {
724 uint32_t size = 0;
725 if (!data.ReadUint32(size)) {
726 ACCOUNT_LOGE("failed to ReadInt32 for size");
727 return false;
728 }
729 if (size > MAX_TOKEN_NUMBER) {
730 ACCOUNT_LOGE("invalid token number");
731 return false;
732 }
733 tokenInfos.clear();
734 for (uint32_t index = 0; index < size; ++index) {
735 OAuthTokenInfo tokenInfo;
736 if (!data.ReadString(tokenInfo.authType)) {
737 ACCOUNT_LOGE("failed to ReadString for authType");
738 return false;
739 }
740 if (!data.ReadString(tokenInfo.token)) {
741 ACCOUNT_LOGE("failed to ReadString for token");
742 return false;
743 }
744 if (!ReadStringSet(tokenInfo.authList, data)) {
745 ACCOUNT_LOGE("failed to ReadString for authList");
746 return false;
747 }
748 tokenInfos.emplace(tokenInfo.authType, tokenInfo);
749 }
750 return true;
751 }
752 } // namespace AccountSA
753 } // namespace OHOS
754