• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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_common.h"
17 
18 #include "account_error_no.h"
19 #include "account_log_wrapper.h"
20 #include "app_account_constants.h"
21 
22 namespace OHOS {
23 namespace AccountSA {
24 namespace {
25 constexpr uint32_t MAX_VEC_SIZE = 1024;
26 }
27 
Marshalling(Parcel & parcel) const28 bool AuthenticatorInfo::Marshalling(Parcel &parcel) const
29 {
30     if (!parcel.WriteString(owner)) {
31         ACCOUNT_LOGE("Write owner failed, please check owner value or parcel status");
32         return false;
33     }
34     if (!parcel.WriteString(abilityName)) {
35         ACCOUNT_LOGE("Write abilityName failed, please check abilityName value or parcel status");
36         return false;
37     }
38     if (!parcel.WriteUint32(iconId)) {
39         ACCOUNT_LOGE("Write iconId failed, please check iconId value or parcel status");
40         return false;
41     }
42     if (!parcel.WriteUint32(labelId)) {
43         ACCOUNT_LOGE("Write labelId failed, please check labelId value or parcel status");
44         return false;
45     }
46     return true;
47 }
48 
Unmarshalling(Parcel & parcel)49 AuthenticatorInfo *AuthenticatorInfo::Unmarshalling(Parcel &parcel)
50 {
51     AuthenticatorInfo *info = new (std::nothrow) AuthenticatorInfo();
52     if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
53         ACCOUNT_LOGW("Read from parcel failed, please check parcel data");
54         delete info;
55         info = nullptr;
56     }
57     return info;
58 }
59 
ReadFromParcel(Parcel & parcel)60 bool AuthenticatorInfo::ReadFromParcel(Parcel &parcel)
61 {
62     if (!parcel.ReadString(owner)) {
63         ACCOUNT_LOGE("Read owner failed, please check owner data in parcel");
64         return false;
65     }
66     if (!parcel.ReadString(abilityName)) {
67         ACCOUNT_LOGE("Read abilityName failed, please check abilityName data in parcel");
68         return false;
69     }
70     if (!parcel.ReadUint32(iconId)) {
71         ACCOUNT_LOGE("Read iconId failed, please check iconId data in parcel");
72         return false;
73     }
74     if (!parcel.ReadUint32(labelId)) {
75         ACCOUNT_LOGE("Read labelId failed, please check labelId data in parcel");
76         return false;
77     }
78     return true;
79 }
80 
Marshalling(Parcel & parcel) const81 bool SelectAccountsOptions::Marshalling(Parcel &parcel) const
82 {
83     if (!parcel.WriteBool(hasAccounts) || !parcel.WriteBool(hasOwners) || !parcel.WriteBool(hasLabels)) {
84         ACCOUNT_LOGE("WriteBool failed");
85         return false;
86     }
87     if (!parcel.WriteUint32(allowedAccounts.size())) {
88         ACCOUNT_LOGE("WriteUint32 failed");
89         return false;
90     }
91     for (auto item : allowedAccounts) {
92         if ((!parcel.WriteString(item.first)) || (!parcel.WriteString(item.second))) {
93             ACCOUNT_LOGE("WriteString failed");
94             return false;
95         }
96     }
97     return parcel.WriteStringVector(allowedOwners) && parcel.WriteStringVector(requiredLabels);
98 }
99 
Unmarshalling(Parcel & parcel)100 SelectAccountsOptions *SelectAccountsOptions::Unmarshalling(Parcel &parcel)
101 {
102     SelectAccountsOptions *info = new (std::nothrow) SelectAccountsOptions();
103     if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
104         ACCOUNT_LOGW("read from parcel failed");
105         delete info;
106         info = nullptr;
107     }
108     return info;
109 }
110 
ReadFromParcel(Parcel & parcel)111 bool SelectAccountsOptions::ReadFromParcel(Parcel &parcel)
112 {
113     if (!parcel.ReadBool(hasAccounts) || !parcel.ReadBool(hasOwners) || !parcel.ReadBool(hasLabels)) {
114         return false;
115     }
116     uint32_t size = 0;
117     if (!parcel.ReadUint32(size)) {
118         return false;
119     }
120     if (size > MAX_VEC_SIZE) {
121         ACCOUNT_LOGE("option is oversize, the limit is %{public}d", MAX_VEC_SIZE);
122         return false;
123     }
124     std::string name;
125     std::string type;
126     for (uint32_t i = 0; i < size; ++i) {
127         if ((!parcel.ReadString(name)) || (!parcel.ReadString(type))) {
128             return false;
129         }
130         allowedAccounts.push_back(std::make_pair(name, type));
131     }
132     return parcel.ReadStringVector(&allowedOwners) && parcel.ReadStringVector(&requiredLabels);
133 }
134 
Marshalling(Parcel & parcel) const135 bool VerifyCredentialOptions::Marshalling(Parcel &parcel) const
136 {
137     return parcel.WriteString(credentialType) && parcel.WriteString(credential) && parcel.WriteParcelable(&parameters);
138 }
139 
Unmarshalling(Parcel & parcel)140 VerifyCredentialOptions *VerifyCredentialOptions::Unmarshalling(Parcel &parcel)
141 {
142     VerifyCredentialOptions *info = new (std::nothrow) VerifyCredentialOptions();
143     if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
144         ACCOUNT_LOGW("read from parcel failed");
145         delete info;
146         info = nullptr;
147     }
148     return info;
149 }
150 
ReadFromParcel(Parcel & parcel)151 bool VerifyCredentialOptions::ReadFromParcel(Parcel &parcel)
152 {
153     if ((!parcel.ReadString(credentialType)) || (!parcel.ReadString(credential))) {
154         return false;
155     }
156     sptr<AAFwk::WantParams> wantParams = parcel.ReadParcelable<AAFwk::WantParams>();
157     if (wantParams == nullptr) {
158         return false;
159     }
160     parameters = *wantParams;
161     return true;
162 }
163 
Marshalling(Parcel & parcel) const164 bool SetPropertiesOptions::Marshalling(Parcel &parcel) const
165 {
166     return parcel.WriteParcelable(&properties) && parcel.WriteParcelable(&parameters);
167 }
168 
Unmarshalling(Parcel & parcel)169 SetPropertiesOptions *SetPropertiesOptions::Unmarshalling(Parcel &parcel)
170 {
171     SetPropertiesOptions *info = new (std::nothrow) SetPropertiesOptions();
172     if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
173         ACCOUNT_LOGW("read from parcel failed");
174         delete info;
175         info = nullptr;
176     }
177     return info;
178 }
179 
ReadFromParcel(Parcel & parcel)180 bool SetPropertiesOptions::ReadFromParcel(Parcel &parcel)
181 {
182     sptr<AAFwk::WantParams> propPtr = parcel.ReadParcelable<AAFwk::WantParams>();
183     if (propPtr == nullptr) {
184         return false;
185     }
186     properties = *propPtr;
187     sptr<AAFwk::WantParams> paramsPtr = parcel.ReadParcelable<AAFwk::WantParams>();
188     if (paramsPtr == nullptr) {
189         return false;
190     }
191     parameters = *paramsPtr;
192     return true;
193 }
194 
Marshalling(Parcel & parcel) const195 bool CreateAccountOptions::Marshalling(Parcel &parcel) const
196 {
197     if (!parcel.WriteUint32(customData.size())) {
198         ACCOUNT_LOGE("failed to write custom data size");
199         return false;
200     }
201     for (const auto& it : customData) {
202         if (!parcel.WriteString(it.first)) {
203             ACCOUNT_LOGE("failed to write key");
204             return false;
205         }
206         if (!parcel.WriteString(it.second)) {
207             ACCOUNT_LOGE("failed to write value");
208             return false;
209         }
210     }
211     return true;
212 }
213 
Unmarshalling(Parcel & parcel)214 CreateAccountOptions *CreateAccountOptions::Unmarshalling(Parcel &parcel)
215 {
216     CreateAccountOptions *info = new (std::nothrow) CreateAccountOptions();
217     if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
218         ACCOUNT_LOGW("read from parcel failed");
219         delete info;
220         info = nullptr;
221     }
222     return info;
223 }
224 
ReadFromParcel(Parcel & parcel)225 bool CreateAccountOptions::ReadFromParcel(Parcel &parcel)
226 {
227     customData.clear();
228     uint32_t size = 0;
229     if (!parcel.ReadUint32(size)) {
230         ACCOUNT_LOGE("fail to read custom data size");
231         return false;
232     }
233     if (size > MAX_VEC_SIZE) {
234         ACCOUNT_LOGE("custom data is oversize, the limit is %{public}d", MAX_VEC_SIZE);
235         return false;
236     }
237     for (uint32_t i = 0; i < size; ++i) {
238         std::string key;
239         if (!parcel.ReadString(key)) {
240             ACCOUNT_LOGE("fail to read custom data key");
241             return false;
242         }
243         std::string value;
244         if (!parcel.ReadString(value)) {
245             ACCOUNT_LOGE("fail to read custom data value");
246             return false;
247         }
248         customData.emplace(key, value);
249     }
250     return true;
251 }
252 
Marshalling(Parcel & parcel) const253 bool CreateAccountImplicitlyOptions::Marshalling(Parcel &parcel) const
254 {
255     return parcel.WriteBool(hasAuthType) && parcel.WriteBool(hasRequiredLabels) && parcel.WriteString(authType) &&
256         parcel.WriteStringVector(requiredLabels) && parcel.WriteParcelable(&parameters);
257 }
258 
Unmarshalling(Parcel & parcel)259 CreateAccountImplicitlyOptions *CreateAccountImplicitlyOptions::Unmarshalling(Parcel &parcel)
260 {
261     CreateAccountImplicitlyOptions *info = new (std::nothrow) CreateAccountImplicitlyOptions();
262     if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
263         ACCOUNT_LOGW("read from parcel failed");
264         delete info;
265         info = nullptr;
266     }
267     return info;
268 }
269 
ReadFromParcel(Parcel & parcel)270 bool CreateAccountImplicitlyOptions::ReadFromParcel(Parcel &parcel)
271 {
272     bool result = parcel.ReadBool(hasAuthType) && parcel.ReadBool(hasRequiredLabels) && parcel.ReadString(authType) &&
273         parcel.ReadStringVector(&requiredLabels);
274     sptr<AAFwk::Want> params = parcel.ReadParcelable<AAFwk::Want>();
275     if ((!result) || (params == nullptr)) {
276         return false;
277     }
278     parameters = *params;
279     return true;
280 }
281 
Marshalling(Parcel & parcel) const282 bool AccountCapabilityRequest::Marshalling(Parcel &parcel) const
283 {
284     return parcel.WriteBool(isEnableContext) && parcel.WriteString(bundleName) && parcel.WriteString(abilityName) &&
285            parcel.WriteParcelable(&parameters);
286 }
287 
Unmarshalling(Parcel & parcel)288 AccountCapabilityRequest *AccountCapabilityRequest::Unmarshalling(Parcel &parcel)
289 {
290     AccountCapabilityRequest *info = new (std::nothrow) AccountCapabilityRequest();
291     if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
292         ACCOUNT_LOGE("read from parcel failed");
293         delete info;
294         info = nullptr;
295     }
296     return info;
297 }
298 
ReadFromParcel(Parcel & parcel)299 bool AccountCapabilityRequest::ReadFromParcel(Parcel &parcel)
300 {
301     if ((!parcel.ReadBool(isEnableContext)) || (!parcel.ReadString(bundleName)) || (!parcel.ReadString(abilityName))) {
302         return false;
303     }
304     sptr<AAFwk::WantParams> paramsPtr = parcel.ReadParcelable<AAFwk::WantParams>();
305     if (paramsPtr == nullptr) {
306         return false;
307     }
308     parameters = *paramsPtr;
309     return true;
310 }
311 
ConvertOtherJSErrCodeV8(int32_t errCode)312 int32_t ConvertOtherJSErrCodeV8(int32_t errCode)
313 {
314     switch (errCode) {
315         case ERR_OK:
316             return ERR_JS_SUCCESS_V8;
317         case ERR_APPACCOUNT_SERVICE_ACCOUNT_NOT_EXIST:
318             return ERR_JS_ACCOUNT_NOT_EXIST;
319         case ERR_APPACCOUNT_SERVICE_OAUTH_AUTHENTICATOR_NOT_EXIST:
320             return ERR_JS_OAUTH_AUTHENTICATOR_NOT_EXIST;
321         case ERR_APPACCOUNT_SERVICE_OAUTH_BUSY:
322             return ERR_JS_OAUTH_SERVICE_BUSY;
323         case ERR_APPACCOUNT_SERVICE_OAUTH_LIST_MAX_SIZE:
324             return ERR_JS_OAUTH_LIST_TOO_LARGE;
325         case ERR_APPACCOUNT_SERVICE_OAUTH_SESSION_NOT_EXIST:
326             return ERR_JS_OAUTH_SESSION_NOT_EXIST;
327         case ERR_APPACCOUNT_SERVICE_OAUTH_TOKEN_NOT_EXIST:
328             return ERR_JS_OAUTH_TOKEN_NOT_EXIST;
329         case ERR_APPACCOUNT_SERVICE_OAUTH_TOKEN_MAX_SIZE:
330             return ERR_JS_OAUTH_TOKEN_TOO_MANY;
331         case ERR_ACCOUNT_COMMON_PERMISSION_DENIED:
332             return ERR_JS_PERMISSION_DENIED_V8;
333         default:
334             return ERR_JS_APP_ACCOUNT_SERVICE_EXCEPTION;
335     }
336 }
337 
ConvertToJSErrCodeV8(int32_t errCode)338 int32_t ConvertToJSErrCodeV8(int32_t errCode)
339 {
340     if ((errCode == ERR_ACCOUNT_COMMON_INVALID_PARAMETER) ||
341         (errCode >= ERR_APPACCOUNT_KIT_SUBSCRIBER_IS_NULLPTR && errCode <= ERR_APPACCOUNT_KIT_SEND_REQUEST) ||
342         (errCode >= ERR_APPACCOUNT_SERVICE_ADD_EXISTING_ACCOUNT &&
343         errCode <= ERR_APPACCOUNT_SERVICE_DISABLE_APP_ACCESS_NOT_EXISTED)) {
344         return ERR_JS_INVALID_REQUEST;
345     } else if ((errCode >= ERR_APPACCOUNT_KIT_READ_PARCELABLE_APP_ACCOUNT_INFO &&
346         errCode <= ERR_APPACCOUNT_KIT_READ_PARCELABLE_VECTOR_ACCOUNT_INFO) ||
347         (errCode == ERR_APPACCOUNT_SERVICE_OAUTH_INVALID_RESPONSE) ||
348         (errCode == ERR_APPACCOUNT_SERVICE_OAUTH_AUTHENTICATOR_CALLBACK_NOT_EXIST)) {
349         return ERR_JS_INVALID_RESPONSE;
350     } else {
351         return ConvertOtherJSErrCodeV8(errCode);
352     }
353 }
354 }  // namespace AccountSA
355 }  // namespace OHOS