• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 #include "app_account_info_json_parser.h"
18 #include "account_log_wrapper.h"
19 #ifdef HAS_ASSET_PART
20 #include <iomanip>
21 #include <sstream>
22 #include <openssl/sha.h>
23 #endif
24 
25 namespace OHOS {
26 namespace AccountSA {
27 
Marshalling(Parcel & parcel) const28 bool OAuthTokenInfo::Marshalling(Parcel &parcel) const
29 {
30     if (!parcel.WriteString(authType)) {
31         ACCOUNT_LOGE("Write authType failed, please check authType value or parcel status");
32         return false;
33     }
34     if (!parcel.WriteString(token)) {
35         ACCOUNT_LOGE("Write token failed, please check token value or parcel status");
36         return false;
37     }
38     if (!parcel.WriteUint32(authList.size())) {
39         ACCOUNT_LOGE("Write authList size failed, please check authList size or parcel status");
40         return false;
41     }
42     for (const auto &item : authList) {
43         if ((!parcel.WriteString(item))) {
44             ACCOUNT_LOGE("Write authList failed, please check authList value or parcel status");
45             return false;
46         }
47     }
48     if (!parcel.WriteBool(status)) {
49         ACCOUNT_LOGE("Write status failed, please check parcel status");
50         return false;
51     }
52     return true;
53 }
54 
Unmarshalling(Parcel & parcel)55 OAuthTokenInfo *OAuthTokenInfo::Unmarshalling(Parcel &parcel)
56 {
57     OAuthTokenInfo *info = new (std::nothrow) OAuthTokenInfo();
58     if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
59         ACCOUNT_LOGW("ReadFromParcel failed, please check paecel data");
60         delete info;
61         info = nullptr;
62     }
63     return info;
64 }
65 
ReadFromParcel(Parcel & parcel)66 bool OAuthTokenInfo::ReadFromParcel(Parcel &parcel)
67 {
68     if (!parcel.ReadString(authType)) {
69         ACCOUNT_LOGE("Read authType failed, please check authType in parcel");
70         return false;
71     }
72     if (!parcel.ReadString(token)) {
73         ACCOUNT_LOGE("Read token failed, please check token in parcel");
74         return false;
75     }
76     uint32_t size = 0;
77     if (!parcel.ReadUint32(size)) {
78         ACCOUNT_LOGE("Read size failed, please check authList size in parcel");
79         return false;
80     }
81     authList.clear();
82     std::string item;
83     for (uint32_t i = 0; i < size; ++i) {
84         if ((!parcel.ReadString(item))) {
85             ACCOUNT_LOGE("Read item failed, please check authList item in parcel");
86             return false;
87         }
88         authList.insert(item);
89     }
90     if (!parcel.ReadBool(status)) {
91         ACCOUNT_LOGE("Read status failed, please check status in parcel");
92         return false;
93     }
94     return true;
95 }
96 
ReadFromParcel(Parcel & parcel)97 bool AppAccountStringInfo::ReadFromParcel(Parcel &parcel)
98 {
99     if (!parcel.ReadString(name)) {
100         ACCOUNT_LOGE("Read name failed, please check name in parcel");
101         return false;
102     }
103     if (!parcel.ReadString(owner)) {
104         ACCOUNT_LOGE("Read owner failed, please check owner in parcel");
105         return false;
106     }
107     if (!parcel.ReadString(authType)) {
108         ACCOUNT_LOGE("Read authType failed, please check authType in parcel");
109         return false;
110     }
111     return true;
112 }
113 
Marshalling(Parcel & parcel) const114 bool AppAccountStringInfo::Marshalling(Parcel &parcel) const
115 {
116     if (!parcel.WriteString(name)) {
117         ACCOUNT_LOGE("Write name failed, please check name value or parcel status");
118         return false;
119     }
120     if (!parcel.WriteString(owner)) {
121         ACCOUNT_LOGE("Write owner failed, please check owner value or parcel status");
122         return false;
123     }
124     if (!parcel.WriteString(authType)) {
125         ACCOUNT_LOGE("Write authType failed, please check authType value or parcel status");
126         return false;
127     }
128     return true;
129 }
130 
Unmarshalling(Parcel & parcel)131 AppAccountStringInfo* AppAccountStringInfo::Unmarshalling(Parcel &parcel)
132 {
133     AppAccountStringInfo *info = new (std::nothrow) AppAccountStringInfo();
134     if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
135         ACCOUNT_LOGW("ReadFromParcel failed, please check paecel data");
136         delete info;
137         info = nullptr;
138     }
139     return info;
140 }
141 
AppAccountAuthenticatorStringInfo(std::string name,std::string authType,std::string callerBundleName)142 AppAccountAuthenticatorStringInfo::AppAccountAuthenticatorStringInfo(
143     std::string name, std::string authType, std::string callerBundleName)
144     : name(name), authType(authType), callerBundleName(callerBundleName)
145 {}
146 
ReadFromParcel(Parcel & parcel)147 bool AppAccountAuthenticatorStringInfo::ReadFromParcel(Parcel &parcel)
148 {
149     if (!parcel.ReadString(name)) {
150         ACCOUNT_LOGE("Read name failed, please check name in parcel");
151         return false;
152     }
153     if (!parcel.ReadString(authType)) {
154         ACCOUNT_LOGE("Read authType failed, please check authType in parcel");
155         return false;
156     }
157     if (!parcel.ReadString(callerBundleName)) {
158         ACCOUNT_LOGE("Read callerBundleName failed, please check callerBundleName in parcel");
159         return false;
160     }
161     return true;
162 }
163 
Marshalling(Parcel & parcel) const164 bool AppAccountAuthenticatorStringInfo::Marshalling(Parcel &parcel) const
165 {
166     if (!parcel.WriteString(name)) {
167         ACCOUNT_LOGE("Write name failed, please check name value or parcel status");
168         return false;
169     }
170     if (!parcel.WriteString(authType)) {
171         ACCOUNT_LOGE("Write authType failed, please check authType value or parcel status");
172         return false;
173     }
174     if (!parcel.WriteString(callerBundleName)) {
175         ACCOUNT_LOGE("Write callerBundleName failed, please check callerBundleName value or parcel status");
176         return false;
177     }
178     return true;
179 }
180 
Unmarshalling(Parcel & parcel)181 AppAccountAuthenticatorStringInfo* AppAccountAuthenticatorStringInfo::Unmarshalling(Parcel &parcel)
182 {
183     AppAccountAuthenticatorStringInfo *info = new (std::nothrow) AppAccountAuthenticatorStringInfo();
184     if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
185         ACCOUNT_LOGW("ReadFromParcel failed, please check paecel data");
186         delete info;
187         info = nullptr;
188     }
189     return info;
190 }
191 
192 #ifdef HAS_ASSET_PART
ComputeHash(const std::string & input,std::string & output)193 static void ComputeHash(const std::string &input, std::string &output)
194 {
195     unsigned char hash[HASH_LENGTH] = {0};
196     SHA256_CTX sha256;
197     SHA256_Init(&sha256);
198     SHA256_Update(&sha256, input.c_str(), input.size());
199     SHA256_Final(hash, &sha256);
200 
201     std::stringstream ss;
202     for (std::uint32_t i = 0; i < HASH_LENGTH; ++i) {
203         ss << std::hex << std::uppercase << std::setw(WIDTH_FOR_HEX) << std::setfill('0') << std::uint16_t(hash[i]);
204     }
205     ss >> output;
206 }
207 #endif
208 
AppAccountInfo()209 AppAccountInfo::AppAccountInfo()
210 {
211     owner_ = "";
212     name_ = "";
213     appIndex_ = APP_INDEX;
214     extraInfo_ = "";
215     authorizedApps_.clear();
216     syncEnable_ = false;
217     associatedData_ = "";
218     accountCredential_ = "";
219     alias_ = "";
220     oauthTokens_.clear();
221 }
222 
AppAccountInfo(const std::string & name,const std::string & owner)223 AppAccountInfo::AppAccountInfo(const std::string &name, const std::string &owner)
224 {
225     name_ = name;
226     owner_ = owner;
227     appIndex_ = APP_INDEX;
228     extraInfo_ = "";
229     authorizedApps_.clear();
230     syncEnable_ = false;
231     associatedData_ = "";
232     accountCredential_ = "";
233     alias_ = "";
234     oauthTokens_.clear();
235 }
236 
GetOwner()237 std::string AppAccountInfo::GetOwner()
238 {
239     return owner_;
240 }
241 
GetOwner(std::string & owner)242 void AppAccountInfo::GetOwner(std::string &owner)
243 {
244     owner = owner_;
245 }
246 
SetOwner(const std::string & owner)247 void AppAccountInfo::SetOwner(const std::string &owner)
248 {
249     owner_ = owner;
250     alias_ = "";
251 }
252 
GetName()253 std::string AppAccountInfo::GetName()
254 {
255     return name_;
256 }
257 
GetName(std::string & name) const258 void AppAccountInfo::GetName(std::string &name) const
259 {
260     name = name_;
261 }
262 
SetName(const std::string & name)263 void AppAccountInfo::SetName(const std::string &name)
264 {
265     name_ = name;
266     alias_ = "";
267 }
268 
GetAppIndex()269 uint32_t AppAccountInfo::GetAppIndex()
270 {
271     return appIndex_;
272 }
273 
SetAppIndex(const uint32_t & appIndex)274 void AppAccountInfo::SetAppIndex(const uint32_t &appIndex)
275 {
276     appIndex_ = appIndex;
277     alias_ = "";
278 }
279 
GetExtraInfo(std::string & extraInfo) const280 void AppAccountInfo::GetExtraInfo(std::string &extraInfo) const
281 {
282     extraInfo = extraInfo_;
283 }
284 
SetExtraInfo(const std::string & extraInfo)285 void AppAccountInfo::SetExtraInfo(const std::string &extraInfo)
286 {
287     extraInfo_ = extraInfo;
288 }
289 
EnableAppAccess(const std::string & authorizedApp,const uint32_t apiVersion)290 ErrCode AppAccountInfo::EnableAppAccess(const std::string &authorizedApp, const uint32_t apiVersion)
291 {
292     auto it = authorizedApps_.emplace(authorizedApp);
293     if (!it.second && apiVersion < Constants::API_VERSION9) {
294         return ERR_APPACCOUNT_SERVICE_ENABLE_APP_ACCESS_ALREADY_EXISTS;
295     }
296     if (authorizedApps_.size() > MAX_APP_AUTH_LIST_SIZE) {
297         ACCOUNT_LOGE("the authorization list is too large, whose capacity for each authType is %{public}d",
298             MAX_OAUTH_LIST_SIZE);
299         authorizedApps_.erase(authorizedApp);
300         return ERR_APPACCOUNT_SERVICE_OAUTH_LIST_MAX_SIZE;
301     }
302     return ERR_OK;
303 }
304 
DisableAppAccess(const std::string & authorizedApp,const uint32_t apiVersion)305 ErrCode AppAccountInfo::DisableAppAccess(const std::string &authorizedApp, const uint32_t apiVersion)
306 {
307     auto result = authorizedApps_.erase(authorizedApp);
308     if (result == 0 && apiVersion < Constants::API_VERSION9) {
309         return ERR_APPACCOUNT_SERVICE_DISABLE_APP_ACCESS_NOT_EXISTED;
310     }
311     return ERR_OK;
312 }
313 
CheckAppAccess(const std::string & authorizedApp,bool & isAccessible)314 ErrCode AppAccountInfo::CheckAppAccess(const std::string &authorizedApp, bool &isAccessible)
315 {
316     isAccessible = false;
317     auto it = authorizedApps_.find(authorizedApp);
318     if (it != authorizedApps_.end()) {
319         isAccessible = true;
320     }
321     return ERR_OK;
322 }
323 
GetAuthorizedApps(std::set<std::string> & apps) const324 void AppAccountInfo::GetAuthorizedApps(std::set<std::string> &apps) const
325 {
326     apps = authorizedApps_;
327 }
328 
SetAuthorizedApps(const std::set<std::string> & apps)329 void AppAccountInfo::SetAuthorizedApps(const std::set<std::string> &apps)
330 {
331     authorizedApps_ = apps;
332 }
333 
GetSyncEnable(bool & syncEnable) const334 void AppAccountInfo::GetSyncEnable(bool &syncEnable) const
335 {
336     syncEnable = syncEnable_;
337 }
338 
SetSyncEnable(const bool & syncEnable)339 void AppAccountInfo::SetSyncEnable(const bool &syncEnable)
340 {
341     syncEnable_ = syncEnable;
342 }
343 
InitCustomData(const std::map<std::string,std::string> & data)344 ErrCode AppAccountInfo::InitCustomData(const std::map<std::string, std::string> &data)
345 {
346     CJsonUnique jsonObject = CreateJsonFromMap(data);
347     associatedData_ = PackJsonToString(jsonObject);
348     if (associatedData_.empty()) {
349         ACCOUNT_LOGE("failed to dump json object");
350         return ERR_ACCOUNT_COMMON_DUMP_JSON_ERROR;
351     }
352     return ERR_OK;
353 }
354 
GetAllAssociatedData(std::map<std::string,std::string> & data) const355 ErrCode AppAccountInfo::GetAllAssociatedData(std::map<std::string, std::string> &data) const
356 {
357     auto jsonObject = CreateJsonFromString(associatedData_);
358     if (jsonObject == nullptr || !IsObject(jsonObject)) {
359         ACCOUNT_LOGE("jsonObject is_discarded");
360         return ERR_ACCOUNT_COMMON_DUMP_JSON_ERROR;
361     }
362 
363     data = PackJsonToMap(jsonObject);
364 
365     return ERR_OK;
366 }
367 
GetAssociatedData(const std::string & key,std::string & value) const368 ErrCode AppAccountInfo::GetAssociatedData(const std::string &key, std::string &value) const
369 {
370     auto jsonObject = CreateJsonFromString(associatedData_);
371     if (jsonObject == nullptr) {
372         ACCOUNT_LOGE("jsonObject is_discarded");
373         jsonObject = CreateJson();
374     }
375     if (!IsKeyExist(jsonObject, key)) {
376         ACCOUNT_LOGE("failed to find value, key = %{public}s", key.c_str());
377         return ERR_APPACCOUNT_SERVICE_ASSOCIATED_DATA_KEY_NOT_EXIST;
378     }
379     value = GetStringFromJson(jsonObject, key);
380 
381     return ERR_OK;
382 }
383 
SetAssociatedData(const std::string & key,const std::string & value)384 ErrCode AppAccountInfo::SetAssociatedData(const std::string &key, const std::string &value)
385 {
386     auto jsonObject = CreateJsonFromString(associatedData_);
387     if (jsonObject == nullptr) {
388         ACCOUNT_LOGE("jsonObject is_discarded");
389         jsonObject = CreateJson();
390     }
391 
392     if (!IsKeyExist(jsonObject, key)) {
393         if (static_cast<uint32_t>(GetItemNum(jsonObject)) >= MAX_ASSOCIATED_DATA_NUMBER) {
394             ACCOUNT_LOGW("associated data is over size, the max number is: %{public}d", MAX_ASSOCIATED_DATA_NUMBER);
395             return ERR_APPACCOUNT_SERVICE_ASSOCIATED_DATA_OVER_SIZE;
396         }
397     }
398     AddStringToJson(jsonObject, key, value);
399 
400     associatedData_ = PackJsonToString(jsonObject);
401     if (associatedData_.empty()) {
402         ACCOUNT_LOGE("failed to dump json object");
403         return ERR_ACCOUNT_COMMON_DUMP_JSON_ERROR;
404     }
405     return ERR_OK;
406 }
407 
GetAccountCredential(const std::string & credentialType,std::string & credential) const408 ErrCode AppAccountInfo::GetAccountCredential(const std::string &credentialType, std::string &credential) const
409 {
410     auto jsonObject = CreateJsonFromString(accountCredential_);
411     if (jsonObject == nullptr) {
412         ACCOUNT_LOGE("jsonObject is_discarded");
413         jsonObject = CreateJson();
414     }
415 
416     if (!IsKeyExist(jsonObject, credentialType)) {
417         ACCOUNT_LOGE("failed to find value, credentialType = %{public}s", credentialType.c_str());
418         return ERR_APPACCOUNT_SERVICE_ACCOUNT_CREDENTIAL_NOT_EXIST;
419     }
420 
421     credential = GetStringFromJson(jsonObject, credentialType);
422 
423     return ERR_OK;
424 }
425 
SetAccountCredential(const std::string & credentialType,const std::string & credential)426 ErrCode AppAccountInfo::SetAccountCredential(
427     const std::string &credentialType, const std::string &credential)
428 {
429     CJsonUnique jsonObject = nullptr;
430     if (accountCredential_.empty()) {
431         jsonObject = CreateJson();
432     } else {
433         jsonObject = CreateJsonFromString(accountCredential_);
434         if (jsonObject == nullptr || !IsObject(jsonObject)) {
435             ACCOUNT_LOGE("jsonObject is not an object");
436             return ERR_ACCOUNT_COMMON_BAD_JSON_FORMAT_ERROR;
437         }
438     }
439 #ifndef HAS_ASSET_PART
440     AddStringToJson(jsonObject, credentialType, credential);
441 #else
442     if (!IsKeyExist(jsonObject, credentialType)) {
443         std::string credentialTypeAlias;
444         ComputeHash(credentialType, credentialTypeAlias);
445         AddStringToJson(jsonObject, credentialType, (GetAlias() + credentialTypeAlias));
446     } else {
447         return ERR_OK;
448     }
449 #endif
450 
451     accountCredential_ = PackJsonToString(jsonObject);
452     if (accountCredential_.empty()) {
453         ACCOUNT_LOGE("failed to dump json object");
454         return ERR_ACCOUNT_COMMON_DUMP_JSON_ERROR;
455     }
456     return ERR_OK;
457 }
458 
DeleteAccountCredential(const std::string & credentialType)459 ErrCode AppAccountInfo::DeleteAccountCredential(const std::string &credentialType)
460 {
461     auto jsonObject = CreateJsonFromString(accountCredential_);
462     if (jsonObject == nullptr || !IsObject(jsonObject) || (DeleteItemFromJson(jsonObject, credentialType) == 0)) {
463         ACCOUNT_LOGE("credential not found");
464         return ERR_APPACCOUNT_SERVICE_ACCOUNT_CREDENTIAL_NOT_EXIST;
465     }
466     accountCredential_ = PackJsonToString(jsonObject);
467 
468     return ERR_OK;
469 }
470 
GetOAuthToken(const std::string & authType,std::string & token,const uint32_t apiVersion) const471 ErrCode AppAccountInfo::GetOAuthToken(const std::string &authType, std::string &token, const uint32_t apiVersion) const
472 {
473     token = "";
474     auto it = oauthTokens_.find(authType);
475     if (apiVersion >= Constants::API_VERSION9) {
476         if ((it == oauthTokens_.end()) || (!it->second.status)) {
477             ACCOUNT_LOGE("oauth token not exist");
478             return ERR_APPACCOUNT_SERVICE_OAUTH_TOKEN_NOT_EXIST;
479         }
480     } else {
481         if ((it == oauthTokens_.end()) || (it->second.token.empty())) {
482             ACCOUNT_LOGE("oauth token not exist");
483             return ERR_APPACCOUNT_SERVICE_OAUTH_TOKEN_NOT_EXIST;
484         }
485     }
486     token = it->second.token;
487     return ERR_OK;
488 }
489 
SetOAuthToken(const std::string & authType,const std::string & token)490 ErrCode AppAccountInfo::SetOAuthToken(const std::string &authType, const std::string &token)
491 {
492     auto it = oauthTokens_.find(authType);
493     if (it != oauthTokens_.end()) {
494 #ifndef HAS_ASSET_PART
495         it->second.token = token;
496 #endif
497         it->second.status = true;
498         return ERR_OK;
499     }
500     if (oauthTokens_.size() >= MAX_TOKEN_NUMBER) {
501         ACCOUNT_LOGE("too many types of oauth token, capacity for each account is %{public}d", MAX_TOKEN_NUMBER);
502         return ERR_APPACCOUNT_SERVICE_OAUTH_TOKEN_MAX_SIZE;
503     }
504     OAuthTokenInfo tokenInfo;
505     tokenInfo.status = !token.empty();
506 #ifndef HAS_ASSET_PART
507     tokenInfo.token = token;
508 #else
509     std::string authTypeAlias;
510     ComputeHash(authType, authTypeAlias);
511     tokenInfo.token = GetAlias() + authTypeAlias;
512 #endif
513     oauthTokens_.emplace(authType, tokenInfo);
514     return ERR_OK;
515 }
516 
DeleteOAuthToken(const std::string & authType,const std::string & token)517 ErrCode AppAccountInfo::DeleteOAuthToken(const std::string &authType, const std::string &token)
518 {
519     auto it = oauthTokens_.find(authType);
520     if ((it != oauthTokens_.end()) && (it->second.token == token)) {
521 #ifndef HAS_ASSET_PART
522         it->second.token = "";
523 #endif
524         it->second.status = false;
525         return ERR_OK;
526     }
527     return ERR_APPACCOUNT_SERVICE_OAUTH_TOKEN_NOT_EXIST;
528 }
529 
DeleteAuthToken(const std::string & authType,const std::string & token,bool isOwnerSelf)530 ErrCode AppAccountInfo::DeleteAuthToken(const std::string &authType, const std::string &token, bool isOwnerSelf)
531 {
532     auto it = oauthTokens_.find(authType);
533     if (it == oauthTokens_.end()) {
534         return ERR_APPACCOUNT_SERVICE_OAUTH_TOKEN_NOT_EXIST;
535     }
536     if (it->second.token != token) {
537         return ERR_OK;
538     }
539     if (isOwnerSelf) {
540         oauthTokens_.erase(it);
541     }
542     it->second.status = false;
543     return ERR_OK;
544 }
545 
SetOAuthTokenVisibility(const std::string & authType,const std::string & bundleName,bool isVisible,const uint32_t apiVersion)546 ErrCode AppAccountInfo::SetOAuthTokenVisibility(
547     const std::string &authType, const std::string &bundleName, bool isVisible, const uint32_t apiVersion)
548 {
549     if (bundleName == owner_) {
550         return ERR_OK;
551     }
552     auto it = oauthTokens_.find(authType);
553     if (it == oauthTokens_.end()) {
554         if (apiVersion >= Constants::API_VERSION9) {
555             return ERR_APPACCOUNT_SERVICE_OAUTH_TYPE_NOT_EXIST;
556         }
557         if (!isVisible) {
558             return ERR_OK;
559         }
560         if (oauthTokens_.size() >= MAX_TOKEN_NUMBER) {
561             ACCOUNT_LOGE("too many types of oauth token, capacity for each account is %{public}d", MAX_TOKEN_NUMBER);
562             return ERR_APPACCOUNT_SERVICE_OAUTH_TOKEN_MAX_SIZE;
563         }
564         OAuthTokenInfo tokenInfo;
565         tokenInfo.authList.emplace(bundleName);
566         oauthTokens_.emplace(authType, tokenInfo);
567         return ERR_OK;
568     }
569     if (!isVisible) {
570         it->second.authList.erase(bundleName);
571         return ERR_OK;
572     }
573     it->second.authList.emplace(bundleName);
574     if (it->second.authList.size() > MAX_OAUTH_LIST_SIZE) {
575         ACCOUNT_LOGE("the authorization list is too large, whose capacity for each authType is %{public}d",
576             MAX_OAUTH_LIST_SIZE);
577         it->second.authList.erase(bundleName);
578         return ERR_APPACCOUNT_SERVICE_OAUTH_LIST_MAX_SIZE;
579     }
580     return ERR_OK;
581 }
582 
CheckOAuthTokenVisibility(const std::string & authType,const std::string & bundleName,bool & isVisible,const uint32_t apiVersion) const583 ErrCode AppAccountInfo::CheckOAuthTokenVisibility(
584     const std::string &authType, const std::string &bundleName, bool &isVisible, const uint32_t apiVersion) const
585 {
586     isVisible = false;
587     if (bundleName == owner_) {
588         isVisible = true;
589         return ERR_OK;
590     }
591     auto tokenInfoIt = oauthTokens_.find(authType);
592     if (tokenInfoIt == oauthTokens_.end()) {
593         if (apiVersion >= Constants::API_VERSION9) {
594             return ERR_APPACCOUNT_SERVICE_OAUTH_TYPE_NOT_EXIST;
595         } else {
596             return ERR_OK;
597         }
598     }
599     std::set<std::string> authList = tokenInfoIt->second.authList;
600     auto it = authList.find(bundleName);
601     if (it != authList.end()) {
602         isVisible = true;
603     }
604     return ERR_OK;
605 }
606 
GetAllOAuthTokens(std::vector<OAuthTokenInfo> & tokenInfos) const607 ErrCode AppAccountInfo::GetAllOAuthTokens(std::vector<OAuthTokenInfo> &tokenInfos) const
608 {
609     tokenInfos.clear();
610     for (auto it = oauthTokens_.begin(); it != oauthTokens_.end(); ++it) {
611         tokenInfos.push_back(it->second);
612     }
613     return ERR_OK;
614 }
615 
GetOAuthList(const std::string & authType,std::set<std::string> & oauthList,const uint32_t apiVersion) const616 ErrCode AppAccountInfo::GetOAuthList(
617     const std::string &authType, std::set<std::string> &oauthList, const uint32_t apiVersion) const
618 {
619     oauthList.clear();
620     auto it = oauthTokens_.find(authType);
621     if (it == oauthTokens_.end()) {
622         if (apiVersion >= Constants::API_VERSION9) {
623             return ERR_APPACCOUNT_SERVICE_OAUTH_TYPE_NOT_EXIST;
624         } else {
625             return ERR_OK;
626         }
627     }
628     oauthList = it->second.authList;
629     return ERR_OK;
630 }
631 
Marshalling(Parcel & parcel) const632 bool AppAccountInfo::Marshalling(Parcel &parcel) const
633 {
634     if (!parcel.WriteString(owner_)) {
635         ACCOUNT_LOGE("failed to write string for owner_");
636         return false;
637     }
638 
639     if (!parcel.WriteString(name_)) {
640         ACCOUNT_LOGE("failed to write string for name_");
641         return false;
642     }
643 
644     if (!parcel.WriteString(extraInfo_)) {
645         ACCOUNT_LOGE("failed to write string for extraInfo_");
646         return false;
647     }
648 
649     if (!WriteStringSet(authorizedApps_, parcel)) {
650         ACCOUNT_LOGE("failed to write string set for authorizedApps_");
651         return false;
652     }
653 
654     if (!parcel.WriteBool(syncEnable_)) {
655         ACCOUNT_LOGE("failed to write bool for syncEnable_");
656         return false;
657     }
658 
659     if (!parcel.WriteString(associatedData_)) {
660         ACCOUNT_LOGE("failed to write string for associatedData_");
661         return false;
662     }
663 
664     if (!parcel.WriteString(accountCredential_)) {
665         ACCOUNT_LOGE("failed to write string for accountCredential_");
666         return false;
667     }
668 
669     if (!WriteTokenInfos(oauthTokens_, parcel)) {
670         ACCOUNT_LOGE("failed to write string map for oauthTokens_");
671         return false;
672     }
673     return true;
674 }
675 
Unmarshalling(Parcel & parcel)676 AppAccountInfo *AppAccountInfo::Unmarshalling(Parcel &parcel)
677 {
678     AppAccountInfo *appAccountInfo = new (std::nothrow) AppAccountInfo();
679 
680     if ((appAccountInfo != nullptr) && (!appAccountInfo->ReadFromParcel(parcel))) {
681         ACCOUNT_LOGE("failed to read from parcel");
682         delete appAccountInfo;
683         appAccountInfo = nullptr;
684     }
685 
686     return appAccountInfo;
687 }
688 
ToString() const689 std::string AppAccountInfo::ToString() const
690 {
691     auto jsonObject = ToJson(*this);
692     std::string strValue = PackJsonToString(jsonObject);
693     if (strValue.empty()) {
694         ACCOUNT_LOGE("failed to dump json object");
695         return "";
696     }
697     return strValue;
698 }
699 
GetPrimeKey() const700 std::string AppAccountInfo::GetPrimeKey() const
701 {
702     return (owner_ + HYPHEN + std::to_string(appIndex_) + HYPHEN + name_ + HYPHEN);
703 }
704 
GetAlias()705 std::string AppAccountInfo::GetAlias()
706 {
707 #ifdef HAS_ASSET_PART
708     if (alias_.empty()) {
709         ComputeHash(GetPrimeKey(), alias_);
710     }
711 #endif
712     return alias_;
713 }
714 
ReadFromParcel(Parcel & parcel)715 bool AppAccountInfo::ReadFromParcel(Parcel &parcel)
716 {
717     if (!parcel.ReadString(owner_)) {
718         ACCOUNT_LOGE("failed to read string for owner_");
719         return false;
720     }
721 
722     if (!parcel.ReadString(name_)) {
723         ACCOUNT_LOGE("failed to read string for name_");
724         return false;
725     }
726 
727     if (!parcel.ReadString(extraInfo_)) {
728         ACCOUNT_LOGE("failed to read string for extraInfo_");
729         return false;
730     }
731 
732     if (!ReadStringSet(authorizedApps_, parcel)) {
733         ACCOUNT_LOGE("failed to read string set for authorizedApps_");
734         return false;
735     }
736 
737     if (!parcel.ReadBool(syncEnable_)) {
738         ACCOUNT_LOGE("failed to read string for syncEnable_");
739         return false;
740     }
741 
742     if (!parcel.ReadString(associatedData_)) {
743         ACCOUNT_LOGE("failed to read string for associatedData_");
744         return false;
745     }
746 
747     if (!parcel.ReadString(accountCredential_)) {
748         ACCOUNT_LOGE("failed to read string for accountCredential_");
749         return false;
750     }
751 
752     if (!ReadTokenInfos(oauthTokens_, parcel)) {
753         ACCOUNT_LOGE("failed to read string map for oauthTokens_");
754         return false;
755     }
756     return true;
757 }
758 
WriteStringSet(const std::set<std::string> & stringSet,Parcel & data) const759 bool AppAccountInfo::WriteStringSet(const std::set<std::string> &stringSet, Parcel &data) const
760 {
761     if (!data.WriteUint32(stringSet.size())) {
762         ACCOUNT_LOGE("failed to WriteInt32 for stringSet.size()");
763         return false;
764     }
765 
766     for (auto it : stringSet) {
767         if (!data.WriteString(it)) {
768             ACCOUNT_LOGE("failed to WriteString for it");
769             return false;
770         }
771     }
772 
773     return true;
774 }
775 
ReadStringSet(std::set<std::string> & stringSet,Parcel & data)776 bool AppAccountInfo::ReadStringSet(std::set<std::string> &stringSet, Parcel &data)
777 {
778     uint32_t size = 0;
779     if (!data.ReadUint32(size)) {
780         ACCOUNT_LOGE("failed to ReadInt32 for size");
781         return false;
782     }
783 
784     if (size > Constants::MAX_CUSTOM_DATA_SIZE) {
785         ACCOUNT_LOGE("ReadStringSet oversize");
786         return false;
787     }
788     stringSet.clear();
789     for (uint32_t index = 0; index < size; index += 1) {
790         std::string it = data.ReadString();
791         if (it.size() == 0) {
792             ACCOUNT_LOGE("failed to ReadString for it");
793             return false;
794         }
795         stringSet.emplace(it);
796     }
797 
798     return true;
799 }
800 
WriteStringMap(const std::map<std::string,std::string> & stringMap,Parcel & data) const801 bool AppAccountInfo::WriteStringMap(const std::map<std::string, std::string> &stringMap, Parcel &data) const
802 {
803     if (!data.WriteInt32(stringMap.size())) {
804         ACCOUNT_LOGE("failed to WriteInt32 for stringSet.size()");
805         return false;
806     }
807 
808     for (auto& it : stringMap) {
809         if (!data.WriteString(it.first)) {
810             ACCOUNT_LOGE("failed to WriteString for authType");
811             return false;
812         }
813         if (!data.WriteString(it.second)) {
814             ACCOUNT_LOGE("failed to WriteString for token");
815             return false;
816         }
817     }
818 
819     return true;
820 }
821 
WriteTokenInfos(const std::map<std::string,OAuthTokenInfo> & tokenInfos,Parcel & data) const822 bool AppAccountInfo::WriteTokenInfos(const std::map<std::string, OAuthTokenInfo> &tokenInfos, Parcel &data) const
823 {
824     if (!data.WriteUint32(tokenInfos.size())) {
825         ACCOUNT_LOGE("failed to WriteInt32 for stringSet.size()");
826         return false;
827     }
828     for (auto& it : tokenInfos) {
829         if (!data.WriteString(it.first)) {
830             ACCOUNT_LOGE("failed to WriteString for authType");
831             return false;
832         }
833         if (!data.WriteString(it.second.token)) {
834             ACCOUNT_LOGE("failed to WriteString for token");
835             return false;
836         }
837         if (!WriteStringSet(it.second.authList, data)) {
838             ACCOUNT_LOGE("failed to WriteString for authList");
839             return false;
840         }
841     }
842     return true;
843 }
844 
ReadStringMap(std::map<std::string,std::string> & stringMap,Parcel & data)845 bool AppAccountInfo::ReadStringMap(std::map<std::string, std::string> &stringMap, Parcel &data)
846 {
847     int32_t size = 0;
848     if (!data.ReadInt32(size)) {
849         ACCOUNT_LOGE("failed to ReadInt32 for size");
850         return false;
851     }
852     if ((size < 0) || (size > MAX_MAP_SZIE)) {
853         ACCOUNT_LOGE("ReadStringMap oversize");
854         return false;
855     }
856     stringMap.clear();
857     for (int32_t index = 0; index < size; ++index) {
858         std::string key;
859         std::string value;
860         if (!data.ReadString(key)) {
861             ACCOUNT_LOGE("failed to ReadString for key");
862             return false;
863         }
864         if (!data.ReadString(value)) {
865             ACCOUNT_LOGE("failed to ReadString for value");
866             return false;
867         }
868         stringMap.emplace(key, value);
869     }
870 
871     return true;
872 }
873 
ReadTokenInfos(std::map<std::string,OAuthTokenInfo> & tokenInfos,Parcel & data)874 bool AppAccountInfo::ReadTokenInfos(std::map<std::string, OAuthTokenInfo> &tokenInfos, Parcel &data)
875 {
876     uint32_t size = 0;
877     if (!data.ReadUint32(size)) {
878         ACCOUNT_LOGE("failed to ReadInt32 for size");
879         return false;
880     }
881     if (size > MAX_TOKEN_NUMBER) {
882         ACCOUNT_LOGE("invalid token number");
883         return false;
884     }
885     tokenInfos.clear();
886     for (uint32_t index = 0; index < size; ++index) {
887         OAuthTokenInfo tokenInfo;
888         if (!data.ReadString(tokenInfo.authType)) {
889             ACCOUNT_LOGE("failed to ReadString for authType");
890             return false;
891         }
892         if (!data.ReadString(tokenInfo.token)) {
893             ACCOUNT_LOGE("failed to ReadString for token");
894             return false;
895         }
896         if (!ReadStringSet(tokenInfo.authList, data)) {
897             ACCOUNT_LOGE("failed to ReadString for authList");
898             return false;
899         }
900         tokenInfos.emplace(tokenInfo.authType, tokenInfo);
901     }
902     return true;
903 }
904 }  // namespace AccountSA
905 }  // namespace OHOS
906