• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "account_log_wrapper.h"
17 #include "nlohmann/json.hpp"
18 
19 #include "app_account_info.h"
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 uint32_t APP_INDEX = 0;
37 
38 const std::string HYPHEN = "#";
39 }  // namespace
40 
AppAccountInfo()41 AppAccountInfo::AppAccountInfo()
42 {
43     owner_ = "";
44     name_ = "";
45     extraInfo_ = "";
46     authorizedApps_.clear();
47     syncEnable_ = false;
48     associatedData_ = "";
49     accountCredential_ = "";
50     oauthTokens_.clear();
51 }
52 
AppAccountInfo(const std::string & name,const std::string & owner)53 AppAccountInfo::AppAccountInfo(const std::string &name, const std::string &owner)
54 {
55     name_ = name;
56     owner_ = owner;
57 
58     extraInfo_ = "";
59     authorizedApps_.clear();
60     syncEnable_ = false;
61     associatedData_ = "";
62     accountCredential_ = "";
63     oauthTokens_.clear();
64 }
65 
GetOwner(std::string & owner)66 ErrCode AppAccountInfo::GetOwner(std::string &owner)
67 {
68     owner = owner_;
69     return ERR_OK;
70 }
71 
SetOwner(const std::string & owner)72 ErrCode AppAccountInfo::SetOwner(const std::string &owner)
73 {
74     owner_ = owner;
75     return ERR_OK;
76 }
77 
GetName(std::string & name) const78 ErrCode AppAccountInfo::GetName(std::string &name) const
79 {
80     name = name_;
81     return ERR_OK;
82 }
83 
SetName(const std::string & name)84 ErrCode AppAccountInfo::SetName(const std::string &name)
85 {
86     name_ = name;
87     return ERR_OK;
88 }
89 
GetExtraInfo(std::string & extraInfo) const90 ErrCode AppAccountInfo::GetExtraInfo(std::string &extraInfo) const
91 {
92     extraInfo = extraInfo_;
93     return ERR_OK;
94 }
95 
SetExtraInfo(const std::string & extraInfo)96 ErrCode AppAccountInfo::SetExtraInfo(const std::string &extraInfo)
97 {
98     extraInfo_ = extraInfo;
99     return ERR_OK;
100 }
101 
EnableAppAccess(const std::string & authorizedApp)102 ErrCode AppAccountInfo::EnableAppAccess(const std::string &authorizedApp)
103 {
104     auto it = authorizedApps_.emplace(authorizedApp);
105     if (!it.second) {
106         return ERR_APPACCOUNT_SERVICE_ENABLE_APP_ACCESS_ALREADY_EXISTS;
107     }
108     return ERR_OK;
109 }
110 
DisableAppAccess(const std::string & authorizedApp)111 ErrCode AppAccountInfo::DisableAppAccess(const std::string &authorizedApp)
112 {
113     auto result = authorizedApps_.erase(authorizedApp);
114     if (result == 0) {
115         return ERR_APPACCOUNT_SERVICE_DISABLE_APP_ACCESS_NOT_EXISTED;
116     }
117     return ERR_OK;
118 }
119 
GetAuthorizedApps(std::set<std::string> & apps) const120 ErrCode AppAccountInfo::GetAuthorizedApps(std::set<std::string> &apps) const
121 {
122     apps = authorizedApps_;
123     return ERR_OK;
124 }
125 
SetAuthorizedApps(const std::set<std::string> & apps)126 ErrCode AppAccountInfo::SetAuthorizedApps(const std::set<std::string> &apps)
127 {
128     authorizedApps_ = apps;
129     return ERR_OK;
130 }
131 
GetSyncEnable(bool & syncEnable) const132 ErrCode AppAccountInfo::GetSyncEnable(bool &syncEnable) const
133 {
134     syncEnable = syncEnable_;
135     return ERR_OK;
136 }
137 
SetSyncEnable(const bool & syncEnable)138 ErrCode AppAccountInfo::SetSyncEnable(const bool &syncEnable)
139 {
140     syncEnable_ = syncEnable;
141     return ERR_OK;
142 }
143 
GetAssociatedData(const std::string & key,std::string & value) const144 ErrCode AppAccountInfo::GetAssociatedData(const std::string &key, std::string &value) const
145 {
146     auto jsonObject = Json::parse(associatedData_, nullptr, false);
147     if (jsonObject.is_discarded()) {
148         ACCOUNT_LOGI("jsonObject is_discarded");
149         jsonObject = Json::object();
150     }
151 
152     if (jsonObject.find(key) == jsonObject.end()) {
153         ACCOUNT_LOGE("failed to find value, key = %{public}s", key.c_str());
154         return ERR_APPACCOUNT_SERVICE_GET_ASSOCIATED_DATA;
155     }
156 
157     value = jsonObject.at(key);
158     return ERR_OK;
159 }
160 
SetAssociatedData(const std::string & key,const std::string & value)161 ErrCode AppAccountInfo::SetAssociatedData(const std::string &key, const std::string &value)
162 {
163     auto jsonObject = Json::parse(associatedData_, nullptr, false);
164     if (jsonObject.is_discarded() || (!jsonObject.is_object())) {
165         ACCOUNT_LOGI("jsonObject is discarded");
166         jsonObject = Json::object();
167     }
168 
169     auto it = jsonObject.find(key);
170     if (it == jsonObject.end()) {
171         jsonObject.emplace(key, value);
172     } else {
173         jsonObject[key] = value;
174     }
175 
176     try {
177         associatedData_ = jsonObject.dump();
178     } catch (Json::type_error& err) {
179         ACCOUNT_LOGE("failed to dump json object, reason: %{public}s", err.what());
180         return ERR_APPACCOUNT_SERVICE_DUMP_JSON;
181     }
182     return ERR_OK;
183 }
184 
GetAccountCredential(const std::string & credentialType,std::string & credential) const185 ErrCode AppAccountInfo::GetAccountCredential(const std::string &credentialType, std::string &credential) const
186 {
187     auto jsonObject = Json::parse(accountCredential_, nullptr, false);
188     if (jsonObject.is_discarded()) {
189         jsonObject = Json::object();
190     }
191 
192     if (jsonObject.find(credentialType) == jsonObject.end()) {
193         ACCOUNT_LOGE("failed to find value, credentialType = %{public}s", credentialType.c_str());
194         return ERR_APPACCOUNT_SERVICE_GET_ACCOUNT_CREDENTIAL;
195     }
196 
197     credential = jsonObject.at(credentialType);
198     return ERR_OK;
199 }
200 
SetAccountCredential(const std::string & credentialType,const std::string & credential)201 ErrCode AppAccountInfo::SetAccountCredential(const std::string &credentialType, const std::string &credential)
202 {
203     auto jsonObject = Json::parse(accountCredential_, nullptr, false);
204     if (jsonObject.is_discarded() || (!jsonObject.is_object())) {
205         ACCOUNT_LOGI("jsonObject is discarded");
206         jsonObject = Json::object();
207     }
208 
209     auto it = jsonObject.find(credentialType);
210     if (it == jsonObject.end()) {
211         jsonObject.emplace(credentialType, credential);
212     } else {
213         jsonObject[credentialType] = credential;
214     }
215 
216     try {
217         accountCredential_ = jsonObject.dump();
218     } catch (Json::type_error& err) {
219         ACCOUNT_LOGE("failed to dump json object, reason: %{public}s", err.what());
220         return ERR_APPACCOUNT_SERVICE_DUMP_JSON;
221     }
222     return ERR_OK;
223 }
224 
GetOAuthToken(const std::string & authType,std::string & token) const225 ErrCode AppAccountInfo::GetOAuthToken(const std::string &authType, std::string &token) const
226 {
227     token = "";
228     auto it = oauthTokens_.find(authType);
229     if ((it == oauthTokens_.end()) || (it->second.token.empty())) {
230         ACCOUNT_LOGI("oauth token not exist");
231         return ERR_APPACCOUNT_SERVICE_OAUTH_TOKEN_NOT_EXIST;
232     }
233     token = it->second.token;
234     return ERR_OK;
235 }
236 
SetOAuthToken(const std::string & authType,const std::string & token)237 ErrCode AppAccountInfo::SetOAuthToken(const std::string &authType, const std::string &token)
238 {
239     auto it = oauthTokens_.find(authType);
240     if (it != oauthTokens_.end()) {
241         it->second.token = token;
242         return ERR_OK;
243     }
244     if (oauthTokens_.size() >= MAX_TOKEN_NUMBER) {
245         ACCOUNT_LOGE("too many types of oauth token, capacity for each account is %{public}d", MAX_TOKEN_NUMBER);
246         return ERR_APPACCOUNT_SERVICE_OAUTH_TOKEN_MAX_SIZE;
247     }
248     OAuthTokenInfo tokenInfo;
249     tokenInfo.token = token;
250     oauthTokens_.emplace(authType, tokenInfo);
251     return ERR_OK;
252 }
253 
DeleteOAuthToken(const std::string & authType,const std::string & token)254 ErrCode AppAccountInfo::DeleteOAuthToken(const std::string &authType, const std::string &token)
255 {
256     auto it = oauthTokens_.find(authType);
257     if ((it != oauthTokens_.end()) && (it->second.token == token)) {
258         it->second.token = "";
259         return ERR_OK;
260     }
261     return ERR_APPACCOUNT_SERVICE_OAUTH_TOKEN_NOT_EXIST;
262 }
263 
SetOAuthTokenVisibility(const std::string & authType,const std::string & bundleName,bool isVisible)264 ErrCode AppAccountInfo::SetOAuthTokenVisibility(
265     const std::string &authType, const std::string &bundleName, bool isVisible)
266 {
267     if (bundleName == owner_) {
268         return ERR_OK;
269     }
270     auto it = oauthTokens_.find(authType);
271     if (it == oauthTokens_.end()) {
272         if (!isVisible) {
273             return ERR_OK;
274         }
275         if (oauthTokens_.size() >= MAX_TOKEN_NUMBER) {
276             ACCOUNT_LOGE("too many types of oauth token, capacity for each account is %{public}d", MAX_TOKEN_NUMBER);
277             return ERR_APPACCOUNT_SERVICE_OAUTH_TOKEN_MAX_SIZE;
278         }
279         OAuthTokenInfo tokenInfo;
280         tokenInfo.authList.emplace(bundleName);
281         oauthTokens_.emplace(authType, tokenInfo);
282         return ERR_OK;
283     }
284     if (!isVisible) {
285         it->second.authList.erase(bundleName);
286         return ERR_OK;
287     }
288     it->second.authList.emplace(bundleName);
289     if (it->second.authList.size() > MAX_OAUTH_LIST_SIZE) {
290         ACCOUNT_LOGE("the authorization list is too large, whose capacity for each authType is %{public}d",
291             MAX_OAUTH_LIST_SIZE);
292         it->second.authList.erase(bundleName);
293         return ERR_APPACCOUNT_SERVICE_OAUTH_LIST_MAX_SIZE;
294     }
295     return ERR_OK;
296 }
297 
CheckOAuthTokenVisibility(const std::string & authType,const std::string & bundleName,bool & isVisible) const298 ErrCode AppAccountInfo::CheckOAuthTokenVisibility(
299     const std::string &authType, const std::string &bundleName, bool &isVisible) const
300 {
301     isVisible = false;
302     if (bundleName == owner_) {
303         isVisible = true;
304         return ERR_OK;
305     }
306     auto tokenInfoIt = oauthTokens_.find(authType);
307     if (tokenInfoIt == oauthTokens_.end()) {
308         return ERR_OK;
309     }
310     std::set<std::string> authList = tokenInfoIt->second.authList;
311     auto it = authList.find(bundleName);
312     if (it != authList.end()) {
313         isVisible = true;
314     }
315     return ERR_OK;
316 }
317 
GetAllOAuthTokens(std::vector<OAuthTokenInfo> & tokenInfos) const318 ErrCode AppAccountInfo::GetAllOAuthTokens(std::vector<OAuthTokenInfo> &tokenInfos) const
319 {
320     tokenInfos.clear();
321     for (auto it = oauthTokens_.begin(); it != oauthTokens_.end(); ++it) {
322         tokenInfos.push_back(it->second);
323     }
324     return ERR_OK;
325 }
326 
GetOAuthList(const std::string & authType,std::set<std::string> & oauthList) const327 ErrCode AppAccountInfo::GetOAuthList(const std::string &authType, std::set<std::string> &oauthList) const
328 {
329     oauthList.clear();
330     auto it = oauthTokens_.find(authType);
331     if (it == oauthTokens_.end()) {
332         return ERR_OK;
333     }
334     oauthList = it->second.authList;
335     return ERR_OK;
336 }
337 
Marshalling(Parcel & parcel) const338 bool AppAccountInfo::Marshalling(Parcel &parcel) const
339 {
340     if (!parcel.WriteString(owner_)) {
341         ACCOUNT_LOGE("failed to write string for owner_");
342         return false;
343     }
344 
345     if (!parcel.WriteString(name_)) {
346         ACCOUNT_LOGE("failed to write string for name_");
347         return false;
348     }
349 
350     if (!parcel.WriteString(extraInfo_)) {
351         ACCOUNT_LOGE("failed to write string for extraInfo_");
352         return false;
353     }
354 
355     if (!WriteStringSet(authorizedApps_, parcel)) {
356         ACCOUNT_LOGE("failed to write string set for authorizedApps_");
357         return false;
358     }
359 
360     if (!parcel.WriteBool(syncEnable_)) {
361         ACCOUNT_LOGE("failed to write bool for syncEnable_");
362         return false;
363     }
364 
365     if (!parcel.WriteString(associatedData_)) {
366         ACCOUNT_LOGE("failed to write string for associatedData_");
367         return false;
368     }
369 
370     if (!parcel.WriteString(accountCredential_)) {
371         ACCOUNT_LOGE("failed to write string for accountCredential_");
372         return false;
373     }
374 
375     if (!WriteTokenInfos(oauthTokens_, parcel)) {
376         ACCOUNT_LOGE("failed to write string map for oauthTokens_");
377         return false;
378     }
379     return true;
380 }
381 
Unmarshalling(Parcel & parcel)382 AppAccountInfo *AppAccountInfo::Unmarshalling(Parcel &parcel)
383 {
384     AppAccountInfo *appAccountInfo = new (std::nothrow) AppAccountInfo();
385 
386     if (appAccountInfo && !appAccountInfo->ReadFromParcel(parcel)) {
387         ACCOUNT_LOGE("failed to read from pacel");
388         delete appAccountInfo;
389         appAccountInfo = nullptr;
390     }
391 
392     return appAccountInfo;
393 }
394 
ToJson() const395 Json AppAccountInfo::ToJson() const
396 {
397     auto tokenArray = Json::array();
398     for (auto it = oauthTokens_.begin(); it != oauthTokens_.end(); ++it) {
399         if ((it->second.token.empty()) && (it->second.authList.size() == 0)) {
400             continue;
401         }
402         auto tokenObject = Json {
403             {OAUTH_TYPE, it->first},
404             {OAUTH_TOKEN, it->second.token},
405             {OAUTH_AUTH_LIST, it->second.authList}
406         };
407         tokenArray.push_back(tokenObject);
408     }
409     auto jsonObject = Json {
410         {OWNER, owner_},
411         {NAME, name_},
412         {EXTRA_INFO, extraInfo_},
413         {AUTHORIZED_APPS, authorizedApps_},
414         {SYNC_ENABLE, syncEnable_},
415         {ASSOCIATED_DATA, associatedData_},
416         {ACCOUNT_CREDENTIAL, accountCredential_},
417         {OAUTH_TOKEN_INFOS, tokenArray},
418     };
419 
420     return jsonObject;
421 }
422 
ParseTokenInfosFromJson(const Json & jsonObject)423 void AppAccountInfo::ParseTokenInfosFromJson(const Json &jsonObject)
424 {
425     oauthTokens_.clear();
426     for (auto it = jsonObject.begin(); it != jsonObject.end(); ++it) {
427         OAuthTokenInfo tokenInfo;
428         if (it->find(OAUTH_TOKEN) != it->end()) {
429             it->at(OAUTH_TOKEN).get_to(tokenInfo.token);
430         }
431         if (it->find(OAUTH_TYPE) != it->end()) {
432             it->at(OAUTH_TYPE).get_to(tokenInfo.authType);
433         }
434         if (it->find(OAUTH_AUTH_LIST) != it->end()) {
435             it->at(OAUTH_AUTH_LIST).get_to(tokenInfo.authList);
436         }
437         oauthTokens_.emplace(tokenInfo.authType, tokenInfo);
438     }
439 }
440 
FromJson(const Json & jsonObject)441 void AppAccountInfo::FromJson(const Json &jsonObject)
442 {
443     const auto &jsonObjectEnd = jsonObject.end();
444 
445     OHOS::AccountSA::GetDataByType<std::string>(
446         jsonObject, jsonObjectEnd, OWNER, owner_, OHOS::AccountSA::JsonType::STRING);
447     OHOS::AccountSA::GetDataByType<std::string>(
448         jsonObject, jsonObjectEnd, NAME, name_, OHOS::AccountSA::JsonType::STRING);
449     OHOS::AccountSA::GetDataByType<std::string>(
450         jsonObject, jsonObjectEnd, EXTRA_INFO, extraInfo_, OHOS::AccountSA::JsonType::STRING);
451     OHOS::AccountSA::GetDataByType<bool>(
452         jsonObject, jsonObjectEnd, SYNC_ENABLE, syncEnable_, OHOS::AccountSA::JsonType::BOOLEAN);
453     OHOS::AccountSA::GetDataByType<std::set<std::string>>(
454         jsonObject, jsonObjectEnd, AUTHORIZED_APPS, authorizedApps_, OHOS::AccountSA::JsonType::ARRAY);
455     OHOS::AccountSA::GetDataByType<std::string>(
456         jsonObject, jsonObjectEnd, ASSOCIATED_DATA, associatedData_, OHOS::AccountSA::JsonType::STRING);
457     OHOS::AccountSA::GetDataByType<std::string>(
458         jsonObject, jsonObjectEnd, ACCOUNT_CREDENTIAL, accountCredential_, OHOS::AccountSA::JsonType::STRING);
459     if (jsonObject.find(OAUTH_TOKEN_INFOS) != jsonObjectEnd) {
460         ParseTokenInfosFromJson(jsonObject.at(OAUTH_TOKEN_INFOS));
461     }
462 }
463 
ToString() const464 std::string AppAccountInfo::ToString() const
465 {
466     auto jsonObject = ToJson();
467     try {
468         return jsonObject.dump();
469     } catch (Json::type_error& err) {
470         ACCOUNT_LOGE("failed to dump json object, reason: %{public}s", err.what());
471         return "";
472     }
473 }
474 
GetPrimeKey() const475 std::string AppAccountInfo::GetPrimeKey() const
476 {
477     return (owner_ + HYPHEN + name_ + HYPHEN + std::to_string(APP_INDEX));
478 }
479 
ReadFromParcel(Parcel & parcel)480 bool AppAccountInfo::ReadFromParcel(Parcel &parcel)
481 {
482     if (!parcel.ReadString(owner_)) {
483         ACCOUNT_LOGE("failed to read string for owner_");
484         return false;
485     }
486 
487     if (!parcel.ReadString(name_)) {
488         ACCOUNT_LOGE("failed to read string for name_");
489         return false;
490     }
491 
492     if (!parcel.ReadString(extraInfo_)) {
493         ACCOUNT_LOGE("failed to read string for extraInfo_");
494         return false;
495     }
496 
497     if (!ReadStringSet(authorizedApps_, parcel)) {
498         ACCOUNT_LOGE("failed to read string set for authorizedApps_");
499         return false;
500     }
501 
502     if (!parcel.ReadBool(syncEnable_)) {
503         ACCOUNT_LOGE("failed to read string for syncEnable_");
504         return false;
505     }
506 
507     if (!parcel.ReadString(associatedData_)) {
508         ACCOUNT_LOGE("failed to read string for associatedData_");
509         return false;
510     }
511 
512     if (!parcel.ReadString(accountCredential_)) {
513         ACCOUNT_LOGE("failed to read string for accountCredential_");
514         return false;
515     }
516 
517     if (!ReadTokenInfos(oauthTokens_, parcel)) {
518         ACCOUNT_LOGE("failed to read string map for oauthTokens_");
519         return false;
520     }
521     return true;
522 }
523 
WriteStringSet(const std::set<std::string> & stringSet,Parcel & data) const524 bool AppAccountInfo::WriteStringSet(const std::set<std::string> &stringSet, Parcel &data) const
525 {
526     if (!data.WriteUint32(stringSet.size())) {
527         ACCOUNT_LOGE("failed to WriteInt32 for stringSet.size()");
528         return false;
529     }
530 
531     for (auto it : stringSet) {
532         if (!data.WriteString(it)) {
533             ACCOUNT_LOGE("failed to WriteString for it");
534             return false;
535         }
536     }
537 
538     return true;
539 }
540 
ReadStringSet(std::set<std::string> & stringSet,Parcel & data)541 bool AppAccountInfo::ReadStringSet(std::set<std::string> &stringSet, Parcel &data)
542 {
543     uint32_t size = 0;
544     if (!data.ReadUint32(size)) {
545         ACCOUNT_LOGE("failed to ReadInt32 for size");
546         return false;
547     }
548 
549     stringSet.clear();
550     for (uint32_t index = 0; index < size; index += 1) {
551         std::string it = data.ReadString();
552         if (it.size() == 0) {
553             ACCOUNT_LOGE("failed to ReadString for it");
554             return false;
555         }
556         stringSet.emplace(it);
557     }
558 
559     return true;
560 }
561 
WriteStringMap(const std::map<std::string,std::string> & stringMap,Parcel & data) const562 bool AppAccountInfo::WriteStringMap(const std::map<std::string, std::string> &stringMap, Parcel &data) const
563 {
564     if (!data.WriteInt32(stringMap.size())) {
565         ACCOUNT_LOGE("failed to WriteInt32 for stringSet.size()");
566         return false;
567     }
568 
569     for (auto& it : stringMap) {
570         if (!data.WriteString(it.first)) {
571             ACCOUNT_LOGE("failed to WriteString for authType");
572             return false;
573         }
574         if (!data.WriteString(it.second)) {
575             ACCOUNT_LOGE("failed to WriteString for token");
576             return false;
577         }
578     }
579 
580     return true;
581 }
582 
WriteTokenInfos(const std::map<std::string,OAuthTokenInfo> & tokenInfos,Parcel & data) const583 bool AppAccountInfo::WriteTokenInfos(const std::map<std::string, OAuthTokenInfo> &tokenInfos, Parcel &data) const
584 {
585     if (!data.WriteInt32(tokenInfos.size())) {
586         ACCOUNT_LOGE("failed to WriteInt32 for stringSet.size()");
587         return false;
588     }
589     for (auto& it : tokenInfos) {
590         if (!data.WriteString(it.first)) {
591             ACCOUNT_LOGE("failed to WriteString for authType");
592             return false;
593         }
594         if (!data.WriteString(it.second.token)) {
595             ACCOUNT_LOGE("failed to WriteString for token");
596             return false;
597         }
598         if (!WriteStringSet(it.second.authList, data)) {
599             ACCOUNT_LOGE("failed to WriteString for authList");
600             return false;
601         }
602     }
603     return true;
604 }
605 
ReadStringMap(std::map<std::string,std::string> & stringMap,Parcel & data)606 bool AppAccountInfo::ReadStringMap(std::map<std::string, std::string> &stringMap, Parcel &data)
607 {
608     int32_t size = 0;
609     if (!data.ReadInt32(size)) {
610         ACCOUNT_LOGE("failed to ReadInt32 for size");
611         return false;
612     }
613 
614     stringMap.clear();
615     for (int32_t index = 0; index < size; ++index) {
616         std::string key;
617         std::string value;
618         if (!data.ReadString(key)) {
619             ACCOUNT_LOGE("failed to ReadString for key");
620             return false;
621         }
622         if (!data.ReadString(value)) {
623             ACCOUNT_LOGE("failed to ReadString for value");
624             return false;
625         }
626         stringMap.emplace(key, value);
627     }
628 
629     return true;
630 }
631 
ReadTokenInfos(std::map<std::string,OAuthTokenInfo> & tokenInfos,Parcel & data)632 bool AppAccountInfo::ReadTokenInfos(std::map<std::string, OAuthTokenInfo> &tokenInfos, Parcel &data)
633 {
634     int32_t size = 0;
635     if (!data.ReadInt32(size)) {
636         ACCOUNT_LOGE("failed to ReadInt32 for size");
637         return false;
638     }
639     tokenInfos.clear();
640     for (int32_t index = 0; index < size; ++index) {
641         OAuthTokenInfo tokenInfo;
642         if (!data.ReadString(tokenInfo.authType)) {
643             ACCOUNT_LOGE("failed to ReadString for authType");
644             return false;
645         }
646         if (!data.ReadString(tokenInfo.token)) {
647             ACCOUNT_LOGE("failed to ReadString for token");
648             return false;
649         }
650         if (!ReadStringSet(tokenInfo.authList, data)) {
651             ACCOUNT_LOGE("failed to ReadString for authList");
652             return false;
653         }
654         tokenInfos.emplace(tokenInfo.authType, tokenInfo);
655     }
656     return true;
657 }
658 }  // namespace AccountSA
659 }  // namespace OHOS
660