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