1 /*
2  * Copyright (c) 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 <string>
17 #include <vector>
18 #include "account_log_wrapper.h"
19 #include "domain_account_common.h"
20 #include "parcel.h"
21 
22 namespace OHOS {
23 namespace AccountSA {
24 
DomainAccountInfo()25 DomainAccountInfo::DomainAccountInfo() : domain_(""), accountName_(""), accountId_("")
26 {}
27 
DomainAccountInfo(const std::string & domain,const std::string & domainAccountName)28 DomainAccountInfo::DomainAccountInfo(const std::string &domain, const std::string &domainAccountName)
29     : domain_(domain), accountName_(domainAccountName)
30 {}
31 
DomainAccountInfo(const std::string & domain,const std::string & domainAccountName,const std::string & accountId)32 DomainAccountInfo::DomainAccountInfo(
33     const std::string &domain, const std::string &domainAccountName, const std::string &accountId)
34     : domain_(domain), accountName_(domainAccountName), accountId_(accountId)
35 {}
36 
DomainAccountInfo(const std::string & domain,const std::string & domainAccountName,const std::string & accountId,const bool & isAuthed,const std::string & serverConfigId)37 DomainAccountInfo::DomainAccountInfo(const std::string &domain, const std::string &domainAccountName,
38     const std::string &accountId, const bool &isAuthed, const std::string &serverConfigId)
39     : domain_(domain), accountName_(domainAccountName), accountId_(accountId), isAuthenticated(isAuthed),
40     serverConfigId_(serverConfigId)
41 {}
42 
Clear()43 void DomainAccountInfo::Clear()
44 {
45     domain_.clear();
46     accountName_.clear();
47     accountId_.clear();
48 }
49 
ReadFromParcel(Parcel & parcel)50 bool DomainAccountInfo::ReadFromParcel(Parcel &parcel)
51 {
52     if (!parcel.ReadString(accountName_)) {
53         ACCOUNT_LOGE("failed to read domain account name");
54         return false;
55     }
56     if (!parcel.ReadString(domain_)) {
57         ACCOUNT_LOGE("failed to read domain");
58         return false;
59     }
60     if (!parcel.ReadString(accountId_)) {
61         ACCOUNT_LOGE("failed to read domain accountId");
62         return false;
63     }
64     if (!parcel.ReadBool(isAuthenticated)) {
65         ACCOUNT_LOGE("Failed to read domain isAuthenticated.");
66         return false;
67     }
68     if (!parcel.ReadString(serverConfigId_)) {
69         ACCOUNT_LOGE("Failed to read domain serverConfigId.");
70         return false;
71     }
72     return true;
73 }
74 
Marshalling(Parcel & parcel) const75 bool DomainAccountInfo::Marshalling(Parcel &parcel) const
76 {
77     if (!parcel.WriteString(accountName_)) {
78         ACCOUNT_LOGE("failed to read write account name");
79         return false;
80     }
81     if (!parcel.WriteString(domain_)) {
82         ACCOUNT_LOGE("failed to write domain");
83         return false;
84     }
85     if (!parcel.WriteString(accountId_)) {
86         ACCOUNT_LOGE("failed to read write accountId");
87         return false;
88     }
89     if (!parcel.WriteBool(isAuthenticated)) {
90         ACCOUNT_LOGE("Failed to read write isAuthenticated.");
91         return false;
92     }
93     if (!parcel.WriteString(serverConfigId_)) {
94         ACCOUNT_LOGE("Failed to read write serverConfigId.");
95         return false;
96     }
97     return true;
98 }
99 
Unmarshalling(Parcel & parcel)100 DomainAccountInfo *DomainAccountInfo::Unmarshalling(Parcel &parcel)
101 {
102     DomainAccountInfo *domainAccountInfo = new (std::nothrow) DomainAccountInfo();
103     if (domainAccountInfo == nullptr) {
104         return nullptr;
105     }
106 
107     if (!domainAccountInfo->ReadFromParcel(parcel)) {
108         ACCOUNT_LOGE("failed to read from parcel");
109         delete domainAccountInfo;
110         domainAccountInfo = nullptr;
111     }
112 
113     return domainAccountInfo;
114 }
115 
GetAccessTokenOptions(const int32_t & callingUid,const AAFwk::WantParams & getTokenParams)116 GetAccessTokenOptions::GetAccessTokenOptions(const int32_t &callingUid, const AAFwk::WantParams &getTokenParams)
117     : callingUid_(callingUid), getTokenParams_(getTokenParams)
118 {}
119 
GetAccessTokenOptions()120 GetAccessTokenOptions::GetAccessTokenOptions()
121 {}
122 
ReadFromParcel(Parcel & parcel)123 bool GetAccessTokenOptions::ReadFromParcel(Parcel &parcel)
124 {
125     if (!parcel.ReadInt32(callingUid_)) {
126         ACCOUNT_LOGE("failed to read callingUid");
127         return false;
128     }
129     auto param = parcel.ReadParcelable<AAFwk::WantParams>();
130     if (param == nullptr) {
131         ACCOUNT_LOGE("failed to read wantParams");
132         return false;
133     }
134     getTokenParams_ = (*param);
135     delete param;
136     return true;
137 }
138 
Marshalling(Parcel & parcel) const139 bool GetAccessTokenOptions::Marshalling(Parcel &parcel) const
140 {
141     if (!parcel.WriteInt32(callingUid_)) {
142         ACCOUNT_LOGE("failed to read write callingUid");
143         return false;
144     }
145     if (!parcel.WriteParcelable(&getTokenParams_)) {
146         ACCOUNT_LOGE("failed to write getTokenParams");
147         return false;
148     }
149     return true;
150 }
151 
Unmarshalling(Parcel & parcel)152 GetAccessTokenOptions *GetAccessTokenOptions::Unmarshalling(Parcel &parcel)
153 {
154     GetAccessTokenOptions *getAccessTokenOptions = new (std::nothrow) GetAccessTokenOptions();
155     if (getAccessTokenOptions == nullptr) {
156         return nullptr;
157     }
158 
159     if (!getAccessTokenOptions->ReadFromParcel(parcel)) {
160         ACCOUNT_LOGE("failed to read from parcel");
161         delete getAccessTokenOptions;
162         getAccessTokenOptions = nullptr;
163     }
164 
165     return getAccessTokenOptions;
166 }
167 
ReadFromParcel(Parcel & parcel)168 bool GetDomainAccountInfoOptions::ReadFromParcel(Parcel &parcel)
169 {
170     std::shared_ptr<DomainAccountInfo> infoPtr(parcel.ReadParcelable<DomainAccountInfo>());
171     if (infoPtr == nullptr) {
172         ACCOUNT_LOGE("failed to read authStatusInfo");
173         return false;
174     }
175     accountInfo = *infoPtr;
176     if (!parcel.ReadInt32(callingUid)) {
177         ACCOUNT_LOGE("failed to read callingUid");
178         return false;
179     }
180     return true;
181 }
182 
Marshalling(Parcel & parcel) const183 bool GetDomainAccountInfoOptions::Marshalling(Parcel &parcel) const
184 {
185     if (!parcel.WriteParcelable(&accountInfo)) {
186         ACCOUNT_LOGE("failed to write authStatusInfo");
187         return false;
188     }
189     if (!parcel.WriteInt32(callingUid)) {
190         ACCOUNT_LOGE("failed to read write callingUid");
191         return false;
192     }
193     return true;
194 }
195 
Unmarshalling(Parcel & parcel)196 GetDomainAccountInfoOptions *GetDomainAccountInfoOptions::Unmarshalling(Parcel &parcel)
197 {
198     GetDomainAccountInfoOptions *getAccountInfoOptions = new (std::nothrow) GetDomainAccountInfoOptions();
199     if (getAccountInfoOptions == nullptr) {
200         return nullptr;
201     }
202 
203     if (!getAccountInfoOptions->ReadFromParcel(parcel)) {
204         ACCOUNT_LOGE("failed to read from parcel");
205         delete getAccountInfoOptions;
206         getAccountInfoOptions = nullptr;
207     }
208 
209     return getAccountInfoOptions;
210 }
211 
DomainServerConfig()212 DomainServerConfig::DomainServerConfig()
213 {}
214 
DomainServerConfig(const std::string & parameters,const std::string & id)215 DomainServerConfig::DomainServerConfig(const std::string &parameters, const std::string &id)
216     :parameters_(parameters), id_(id)
217 {}
DomainServerConfig(const std::string & parameters,const std::string & id,const std::string & domain)218 DomainServerConfig::DomainServerConfig(const std::string &parameters, const std::string &id, const std::string &domain)
219     :parameters_(parameters), id_(id), domain_(domain)
220 {}
221 
ReadFromParcel(Parcel & parcel)222 bool DomainServerConfig::ReadFromParcel(Parcel &parcel)
223 {
224     if (!parcel.ReadString(parameters_)) {
225         ACCOUNT_LOGE("Failed to read parameters.");
226         return false;
227     }
228     if (!parcel.ReadString(id_)) {
229         ACCOUNT_LOGE("Failed to read id.");
230         return false;
231     }
232     if (!parcel.ReadString(domain_)) {
233         ACCOUNT_LOGE("Failed to read domain.");
234         return false;
235     }
236     return true;
237 }
238 
Marshalling(Parcel & parcel) const239 bool DomainServerConfig::Marshalling(Parcel &parcel) const
240 {
241     if (!parcel.WriteString(parameters_)) {
242         ACCOUNT_LOGE("Failed to write param.");
243         return false;
244     }
245     if (!parcel.WriteString(id_)) {
246         ACCOUNT_LOGE("Failed to write id.");
247         return false;
248     }
249     if (!parcel.WriteString(domain_)) {
250         ACCOUNT_LOGE("Failed to write domain.");
251         return false;
252     }
253     return true;
254 }
255 
Unmarshalling(Parcel & parcel)256 DomainServerConfig *DomainServerConfig::Unmarshalling(Parcel &parcel)
257 {
258     DomainServerConfig *domainServerConfig = new (std::nothrow) DomainServerConfig();
259     if (domainServerConfig == nullptr) {
260         return nullptr;
261     }
262     if (!domainServerConfig->ReadFromParcel(parcel)) {
263         ACCOUNT_LOGE("Failed to read from parcel.");
264         delete domainServerConfig;
265         domainServerConfig = nullptr;
266     }
267     return domainServerConfig;
268 }
269 
ReadFromParcel(Parcel & parcel)270 bool AuthStatusInfo::ReadFromParcel(Parcel &parcel)
271 {
272     if (!parcel.ReadInt32(remainingTimes)) {
273         ACCOUNT_LOGE("failed to read remainingTimes");
274         return false;
275     }
276     if (!parcel.ReadInt32(freezingTime)) {
277         ACCOUNT_LOGE("failed to read freezingTime");
278         return false;
279     }
280     return true;
281 }
282 
Marshalling(Parcel & parcel) const283 bool AuthStatusInfo::Marshalling(Parcel &parcel) const
284 {
285     if (!parcel.WriteInt32(remainingTimes)) {
286         ACCOUNT_LOGE("failed to read write remainingTimes");
287         return false;
288     }
289     if (!parcel.WriteInt32(freezingTime)) {
290         ACCOUNT_LOGE("failed to write freezingTime");
291         return false;
292     }
293     return true;
294 }
295 
Unmarshalling(Parcel & parcel)296 AuthStatusInfo *AuthStatusInfo::Unmarshalling(Parcel &parcel)
297 {
298     AuthStatusInfo *info = new (std::nothrow) AuthStatusInfo();
299     if (info == nullptr) {
300         ACCOUNT_LOGE("failed to create AuthStatusInfo");
301         return nullptr;
302     }
303     if (!info->ReadFromParcel(parcel)) {
304         ACCOUNT_LOGE("failed to read from parcel");
305         delete info;
306         info = nullptr;
307     }
308     return info;
309 }
310 
ReadFromParcel(Parcel & parcel)311 bool DomainAuthResult::ReadFromParcel(Parcel &parcel)
312 {
313     if (!parcel.ReadUInt8Vector(&token)) {
314         ACCOUNT_LOGE("failed to read remainingTimes");
315         return false;
316     }
317     std::shared_ptr<AuthStatusInfo> infoPtr(parcel.ReadParcelable<AuthStatusInfo>());
318     if (infoPtr == nullptr) {
319         ACCOUNT_LOGE("failed to read authStatusInfo");
320         return false;
321     }
322     authStatusInfo = *infoPtr;
323     return true;
324 }
325 
Marshalling(Parcel & parcel) const326 bool DomainAuthResult::Marshalling(Parcel &parcel) const
327 {
328     if (!parcel.WriteUInt8Vector(token)) {
329         ACCOUNT_LOGE("failed to read write token");
330         return false;
331     }
332     if (!parcel.WriteParcelable(&authStatusInfo)) {
333         ACCOUNT_LOGE("failed to write authStatusInfo");
334         return false;
335     }
336     return true;
337 }
338 
Unmarshalling(Parcel & parcel)339 DomainAuthResult *DomainAuthResult::Unmarshalling(Parcel &parcel)
340 {
341     DomainAuthResult *result = new (std::nothrow) DomainAuthResult();
342     if (result == nullptr) {
343         ACCOUNT_LOGE("failed to create DomainAuthResult");
344         return nullptr;
345     }
346     if (!result->ReadFromParcel(parcel)) {
347         ACCOUNT_LOGE("failed to read from parcel");
348         delete result;
349         result = nullptr;
350     }
351     return result;
352 }
353 
Marshalling(Parcel & parcel) const354 bool CreateOsAccountForDomainOptions::Marshalling(Parcel &parcel) const
355 {
356     if (!parcel.WriteString(shortName)) {
357         ACCOUNT_LOGE("Failed to write shortName");
358         return false;
359     }
360     if (!parcel.WriteBool(hasShortName)) {
361         ACCOUNT_LOGE("Failed to write hasShortName");
362         return false;
363     }
364     return true;
365 }
366 
Unmarshalling(Parcel & parcel)367 CreateOsAccountForDomainOptions *CreateOsAccountForDomainOptions::Unmarshalling(Parcel &parcel)
368 {
369     CreateOsAccountForDomainOptions *options = new (std::nothrow) CreateOsAccountForDomainOptions();
370     if ((options != nullptr) && (!options->ReadFromParcel(parcel))) {
371         ACCOUNT_LOGE("Failed to read from parcel");
372         delete options;
373         options = nullptr;
374     }
375     return options;
376 }
377 
ReadFromParcel(Parcel & parcel)378 bool CreateOsAccountForDomainOptions::ReadFromParcel(Parcel &parcel)
379 {
380     if (!parcel.ReadString(shortName)) {
381         ACCOUNT_LOGE("Failed to read shortName.");
382         return false;
383     }
384     if (!parcel.ReadBool(hasShortName)) {
385         ACCOUNT_LOGE("Failed to read hasShortName.");
386         return false;
387     }
388     return true;
389 }
390 }  // namespace AccountSA
391 }  // namespace OHOS