1 /*
2 * Copyright (c) 2022 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 SelectAccountsOptions::Marshalling(Parcel &parcel) const
29 {
30 if (!parcel.WriteBool(hasAccounts) || !parcel.WriteBool(hasOwners) || !parcel.WriteBool(hasLabels)) {
31 return false;
32 }
33 if (!parcel.WriteUint32(allowedAccounts.size())) {
34 return false;
35 }
36 for (auto item : allowedAccounts) {
37 if ((!parcel.WriteString(item.first)) || (!parcel.WriteString(item.second))) {
38 ACCOUNT_LOGE("WriteString failed");
39 return false;
40 }
41 }
42 return parcel.WriteStringVector(allowedOwners) && parcel.WriteStringVector(requiredLabels);
43 }
44
Unmarshalling(Parcel & parcel)45 SelectAccountsOptions *SelectAccountsOptions::Unmarshalling(Parcel &parcel)
46 {
47 SelectAccountsOptions *info = new (std::nothrow) SelectAccountsOptions();
48 if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
49 ACCOUNT_LOGW("read from parcel failed");
50 delete info;
51 info = nullptr;
52 }
53 return info;
54 }
55
ReadFromParcel(Parcel & parcel)56 bool SelectAccountsOptions::ReadFromParcel(Parcel &parcel)
57 {
58 if (!parcel.ReadBool(hasAccounts) || !parcel.ReadBool(hasOwners) || !parcel.ReadBool(hasLabels)) {
59 return false;
60 }
61 uint32_t size = 0;
62 if (!parcel.ReadUint32(size)) {
63 return false;
64 }
65 if (size > MAX_VEC_SIZE) {
66 ACCOUNT_LOGE("option is oversize, the limit is %{public}d", MAX_VEC_SIZE);
67 return false;
68 }
69 std::string name;
70 std::string type;
71 for (uint32_t i = 0; i < size; ++i) {
72 if ((!parcel.ReadString(name)) || (!parcel.ReadString(type))) {
73 return false;
74 }
75 allowedAccounts.push_back(std::make_pair(name, type));
76 }
77 return parcel.ReadStringVector(&allowedOwners) && parcel.ReadStringVector(&requiredLabels);
78 }
79
AuthorizationRequest(const int32_t & callingUid,const AAFwk::WantParams & parameters,const sptr<IAppAccountAuthorizationExtensionCallback> & callback)80 AuthorizationRequest::AuthorizationRequest(const int32_t &callingUid, const AAFwk::WantParams ¶meters,
81 const sptr<IAppAccountAuthorizationExtensionCallback> &callback)
82 : callerUid(callingUid), parameters(parameters), callback(callback)
83 {}
84
AuthorizationRequest()85 AuthorizationRequest::AuthorizationRequest()
86 {}
87
Marshalling(Parcel & parcel) const88 bool VerifyCredentialOptions::Marshalling(Parcel &parcel) const
89 {
90 return parcel.WriteString(credentialType) && parcel.WriteString(credential) && parcel.WriteParcelable(¶meters);
91 }
92
Unmarshalling(Parcel & parcel)93 VerifyCredentialOptions *VerifyCredentialOptions::Unmarshalling(Parcel &parcel)
94 {
95 VerifyCredentialOptions *info = new (std::nothrow) VerifyCredentialOptions();
96 if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
97 ACCOUNT_LOGW("read from parcel failed");
98 delete info;
99 info = nullptr;
100 }
101 return info;
102 }
103
ReadFromParcel(Parcel & parcel)104 bool VerifyCredentialOptions::ReadFromParcel(Parcel &parcel)
105 {
106 if ((!parcel.ReadString(credentialType)) || (!parcel.ReadString(credential))) {
107 return false;
108 }
109 sptr<AAFwk::WantParams> wantParams = parcel.ReadParcelable<AAFwk::WantParams>();
110 if (wantParams == nullptr) {
111 return false;
112 }
113 parameters = *wantParams;
114 return true;
115 }
116
Marshalling(Parcel & parcel) const117 bool SetPropertiesOptions::Marshalling(Parcel &parcel) const
118 {
119 return parcel.WriteParcelable(&properties) && parcel.WriteParcelable(¶meters);
120 }
121
Unmarshalling(Parcel & parcel)122 SetPropertiesOptions *SetPropertiesOptions::Unmarshalling(Parcel &parcel)
123 {
124 SetPropertiesOptions *info = new (std::nothrow) SetPropertiesOptions();
125 if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
126 ACCOUNT_LOGW("read from parcel failed");
127 delete info;
128 info = nullptr;
129 }
130 return info;
131 }
132
ReadFromParcel(Parcel & parcel)133 bool SetPropertiesOptions::ReadFromParcel(Parcel &parcel)
134 {
135 sptr<AAFwk::WantParams> propPtr = parcel.ReadParcelable<AAFwk::WantParams>();
136 if (propPtr == nullptr) {
137 return false;
138 }
139 properties = *propPtr;
140 sptr<AAFwk::WantParams> paramsPtr = parcel.ReadParcelable<AAFwk::WantParams>();
141 if (paramsPtr == nullptr) {
142 return false;
143 }
144 parameters = *paramsPtr;
145 return true;
146 }
147
Marshalling(Parcel & parcel) const148 bool CreateAccountOptions::Marshalling(Parcel &parcel) const
149 {
150 if (!parcel.WriteUint32(customData.size())) {
151 ACCOUNT_LOGE("failed to write custom data size");
152 return false;
153 }
154 for (const auto& it : customData) {
155 if (!parcel.WriteString(it.first)) {
156 ACCOUNT_LOGE("failed to write key");
157 return false;
158 }
159 if (!parcel.WriteString(it.second)) {
160 ACCOUNT_LOGE("failed to write value");
161 return false;
162 }
163 }
164 return true;
165 }
166
Unmarshalling(Parcel & parcel)167 CreateAccountOptions *CreateAccountOptions::Unmarshalling(Parcel &parcel)
168 {
169 CreateAccountOptions *info = new (std::nothrow) CreateAccountOptions();
170 if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
171 ACCOUNT_LOGW("read from parcel failed");
172 delete info;
173 info = nullptr;
174 }
175 return info;
176 }
177
ReadFromParcel(Parcel & parcel)178 bool CreateAccountOptions::ReadFromParcel(Parcel &parcel)
179 {
180 customData.clear();
181 uint32_t size = 0;
182 if (!parcel.ReadUint32(size)) {
183 ACCOUNT_LOGE("fail to read custom data size");
184 return false;
185 }
186 if (size > MAX_VEC_SIZE) {
187 ACCOUNT_LOGE("custom data is oversize, the limit is %{public}d", MAX_VEC_SIZE);
188 return false;
189 }
190 for (uint32_t i = 0; i < size; ++i) {
191 std::string key;
192 if (!parcel.ReadString(key)) {
193 ACCOUNT_LOGE("fail to read custom data key");
194 return false;
195 }
196 std::string value;
197 if (!parcel.ReadString(value)) {
198 ACCOUNT_LOGE("fail to read custom data value");
199 return false;
200 }
201 customData.emplace(key, value);
202 }
203 return true;
204 }
205
Marshalling(Parcel & parcel) const206 bool CreateAccountImplicitlyOptions::Marshalling(Parcel &parcel) const
207 {
208 return parcel.WriteBool(hasAuthType) && parcel.WriteBool(hasRequiredLabels) && parcel.WriteString(authType) &&
209 parcel.WriteStringVector(requiredLabels) && parcel.WriteParcelable(¶meters);
210 }
211
Unmarshalling(Parcel & parcel)212 CreateAccountImplicitlyOptions *CreateAccountImplicitlyOptions::Unmarshalling(Parcel &parcel)
213 {
214 CreateAccountImplicitlyOptions *info = new (std::nothrow) CreateAccountImplicitlyOptions();
215 if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
216 ACCOUNT_LOGW("read from parcel failed");
217 delete info;
218 info = nullptr;
219 }
220 return info;
221 }
222
ReadFromParcel(Parcel & parcel)223 bool CreateAccountImplicitlyOptions::ReadFromParcel(Parcel &parcel)
224 {
225 bool result = parcel.ReadBool(hasAuthType) && parcel.ReadBool(hasRequiredLabels) && parcel.ReadString(authType) &&
226 parcel.ReadStringVector(&requiredLabels);
227 sptr<AAFwk::Want> params = parcel.ReadParcelable<AAFwk::Want>();
228 if ((!result) || (params == nullptr)) {
229 return false;
230 }
231 parameters = *params;
232 return true;
233 }
234
Marshalling(Parcel & parcel) const235 bool AccountCapabilityRequest::Marshalling(Parcel &parcel) const
236 {
237 return parcel.WriteBool(isEnableContext) && parcel.WriteString(bundleName) && parcel.WriteString(abilityName) &&
238 parcel.WriteParcelable(¶meters);
239 }
240
Unmarshalling(Parcel & parcel)241 AccountCapabilityRequest *AccountCapabilityRequest::Unmarshalling(Parcel &parcel)
242 {
243 AccountCapabilityRequest *info = new (std::nothrow) AccountCapabilityRequest();
244 if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
245 ACCOUNT_LOGE("read from parcel failed");
246 delete info;
247 info = nullptr;
248 }
249 return info;
250 }
251
ReadFromParcel(Parcel & parcel)252 bool AccountCapabilityRequest::ReadFromParcel(Parcel &parcel)
253 {
254 if ((!parcel.ReadBool(isEnableContext)) || (!parcel.ReadString(bundleName)) || (!parcel.ReadString(abilityName))) {
255 return false;
256 }
257 sptr<AAFwk::WantParams> paramsPtr = parcel.ReadParcelable<AAFwk::WantParams>();
258 if (paramsPtr == nullptr) {
259 return false;
260 }
261 parameters = *paramsPtr;
262 return true;
263 }
264
ConvertOtherJSErrCodeV8(int32_t errCode)265 int32_t ConvertOtherJSErrCodeV8(int32_t errCode)
266 {
267 switch (errCode) {
268 case ERR_OK:
269 return ERR_JS_SUCCESS_V8;
270 case ERR_APPACCOUNT_SERVICE_ACCOUNT_NOT_EXIST:
271 return ERR_JS_ACCOUNT_NOT_EXIST;
272 case ERR_APPACCOUNT_SERVICE_OAUTH_AUTHENTICATOR_NOT_EXIST:
273 return ERR_JS_OAUTH_AUTHENTICATOR_NOT_EXIST;
274 case ERR_APPACCOUNT_SERVICE_OAUTH_BUSY:
275 return ERR_JS_OAUTH_SERVICE_BUSY;
276 case ERR_APPACCOUNT_SERVICE_OAUTH_LIST_MAX_SIZE:
277 return ERR_JS_OAUTH_LIST_TOO_LARGE;
278 case ERR_APPACCOUNT_SERVICE_OAUTH_SESSION_NOT_EXIST:
279 return ERR_JS_OAUTH_SESSION_NOT_EXIST;
280 case ERR_APPACCOUNT_SERVICE_OAUTH_TOKEN_NOT_EXIST:
281 return ERR_JS_OAUTH_TOKEN_NOT_EXIST;
282 case ERR_APPACCOUNT_SERVICE_OAUTH_TOKEN_MAX_SIZE:
283 return ERR_JS_OAUTH_TOKEN_TOO_MANY;
284 case ERR_ACCOUNT_COMMON_PERMISSION_DENIED:
285 return ERR_JS_PERMISSION_DENIED_V8;
286 default:
287 return ERR_JS_APP_ACCOUNT_SERVICE_EXCEPTION;
288 }
289 }
290
ConvertToJSErrCodeV8(int32_t errCode)291 int32_t ConvertToJSErrCodeV8(int32_t errCode)
292 {
293 if ((errCode == ERR_ACCOUNT_COMMON_INVALID_PARAMETER) ||
294 (errCode >= ERR_APPACCOUNT_KIT_SUBSCRIBER_IS_NULLPTR && errCode <= ERR_APPACCOUNT_KIT_SEND_REQUEST) ||
295 (errCode >= ERR_APPACCOUNT_SERVICE_ADD_EXISTING_ACCOUNT &&
296 errCode <= ERR_APPACCOUNT_SERVICE_DISABLE_APP_ACCESS_NOT_EXISTED)) {
297 return ERR_JS_INVALID_REQUEST;
298 } else if ((errCode >= ERR_APPACCOUNT_KIT_READ_PARCELABLE_APP_ACCOUNT_INFO &&
299 errCode <= ERR_APPACCOUNT_KIT_READ_PARCELABLE_VECTOR_ACCOUNT_INFO) ||
300 (errCode == ERR_APPACCOUNT_SERVICE_OAUTH_INVALID_RESPONSE) ||
301 (errCode == ERR_APPACCOUNT_SERVICE_OAUTH_AUTHENTICATOR_CALLBACK_NOT_EXIST)) {
302 return ERR_JS_INVALID_RESPONSE;
303 } else {
304 return ConvertOtherJSErrCodeV8(errCode);
305 }
306 }
307 } // namespace AccountSA
308 } // namespace OHOS