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