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
Marshalling(Parcel & parcel) const80 bool VerifyCredentialOptions::Marshalling(Parcel &parcel) const
81 {
82 return parcel.WriteString(credentialType) && parcel.WriteString(credential) && parcel.WriteParcelable(¶meters);
83 }
84
Unmarshalling(Parcel & parcel)85 VerifyCredentialOptions *VerifyCredentialOptions::Unmarshalling(Parcel &parcel)
86 {
87 VerifyCredentialOptions *info = new (std::nothrow) VerifyCredentialOptions();
88 if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
89 ACCOUNT_LOGW("read from parcel failed");
90 delete info;
91 info = nullptr;
92 }
93 return info;
94 }
95
ReadFromParcel(Parcel & parcel)96 bool VerifyCredentialOptions::ReadFromParcel(Parcel &parcel)
97 {
98 if ((!parcel.ReadString(credentialType)) || (!parcel.ReadString(credential))) {
99 return false;
100 }
101 sptr<AAFwk::WantParams> wantParams = parcel.ReadParcelable<AAFwk::WantParams>();
102 if (wantParams == nullptr) {
103 return false;
104 }
105 parameters = *wantParams;
106 return true;
107 }
108
Marshalling(Parcel & parcel) const109 bool SetPropertiesOptions::Marshalling(Parcel &parcel) const
110 {
111 return parcel.WriteParcelable(&properties) && parcel.WriteParcelable(¶meters);
112 }
113
Unmarshalling(Parcel & parcel)114 SetPropertiesOptions *SetPropertiesOptions::Unmarshalling(Parcel &parcel)
115 {
116 SetPropertiesOptions *info = new (std::nothrow) SetPropertiesOptions();
117 if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
118 ACCOUNT_LOGW("read from parcel failed");
119 delete info;
120 info = nullptr;
121 }
122 return info;
123 }
124
ReadFromParcel(Parcel & parcel)125 bool SetPropertiesOptions::ReadFromParcel(Parcel &parcel)
126 {
127 sptr<AAFwk::WantParams> propPtr = parcel.ReadParcelable<AAFwk::WantParams>();
128 if (propPtr == nullptr) {
129 return false;
130 }
131 properties = *propPtr;
132 sptr<AAFwk::WantParams> paramsPtr = parcel.ReadParcelable<AAFwk::WantParams>();
133 if (paramsPtr == nullptr) {
134 return false;
135 }
136 parameters = *paramsPtr;
137 return true;
138 }
139
Marshalling(Parcel & parcel) const140 bool CreateAccountOptions::Marshalling(Parcel &parcel) const
141 {
142 if (!parcel.WriteUint32(customData.size())) {
143 ACCOUNT_LOGE("failed to write custom data size");
144 return false;
145 }
146 for (const auto& it : customData) {
147 if (!parcel.WriteString(it.first)) {
148 ACCOUNT_LOGE("failed to write key");
149 return false;
150 }
151 if (!parcel.WriteString(it.second)) {
152 ACCOUNT_LOGE("failed to write value");
153 return false;
154 }
155 }
156 return true;
157 }
158
Unmarshalling(Parcel & parcel)159 CreateAccountOptions *CreateAccountOptions::Unmarshalling(Parcel &parcel)
160 {
161 CreateAccountOptions *info = new (std::nothrow) CreateAccountOptions();
162 if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
163 ACCOUNT_LOGW("read from parcel failed");
164 delete info;
165 info = nullptr;
166 }
167 return info;
168 }
169
ReadFromParcel(Parcel & parcel)170 bool CreateAccountOptions::ReadFromParcel(Parcel &parcel)
171 {
172 customData.clear();
173 uint32_t size = 0;
174 if (!parcel.ReadUint32(size)) {
175 ACCOUNT_LOGE("fail to read custom data size");
176 return false;
177 }
178 if (size > MAX_VEC_SIZE) {
179 ACCOUNT_LOGE("custom data is oversize, the limit is %{public}d", MAX_VEC_SIZE);
180 return false;
181 }
182 for (uint32_t i = 0; i < size; ++i) {
183 std::string key;
184 if (!parcel.ReadString(key)) {
185 ACCOUNT_LOGE("fail to read custom data key");
186 return false;
187 }
188 std::string value;
189 if (!parcel.ReadString(value)) {
190 ACCOUNT_LOGE("fail to read custom data value");
191 return false;
192 }
193 customData.emplace(key, value);
194 }
195 return true;
196 }
197
Marshalling(Parcel & parcel) const198 bool CreateAccountImplicitlyOptions::Marshalling(Parcel &parcel) const
199 {
200 return parcel.WriteBool(hasAuthType) && parcel.WriteBool(hasRequiredLabels) && parcel.WriteString(authType) &&
201 parcel.WriteStringVector(requiredLabels) && parcel.WriteParcelable(¶meters);
202 }
203
Unmarshalling(Parcel & parcel)204 CreateAccountImplicitlyOptions *CreateAccountImplicitlyOptions::Unmarshalling(Parcel &parcel)
205 {
206 CreateAccountImplicitlyOptions *info = new (std::nothrow) CreateAccountImplicitlyOptions();
207 if ((info != nullptr) && (!info->ReadFromParcel(parcel))) {
208 ACCOUNT_LOGW("read from parcel failed");
209 delete info;
210 info = nullptr;
211 }
212 return info;
213 }
214
ReadFromParcel(Parcel & parcel)215 bool CreateAccountImplicitlyOptions::ReadFromParcel(Parcel &parcel)
216 {
217 bool result = parcel.ReadBool(hasAuthType) && parcel.ReadBool(hasRequiredLabels) && parcel.ReadString(authType) &&
218 parcel.ReadStringVector(&requiredLabels);
219 sptr<AAFwk::Want> params = parcel.ReadParcelable<AAFwk::Want>();
220 if ((!result) || (params == nullptr)) {
221 return false;
222 }
223 parameters = *params;
224 return true;
225 }
226
ConvertOtherJSErrCodeV8(int32_t errCode)227 int32_t ConvertOtherJSErrCodeV8(int32_t errCode)
228 {
229 switch (errCode) {
230 case ERR_OK:
231 return ERR_JS_SUCCESS_V8;
232 case ERR_APPACCOUNT_SERVICE_ACCOUNT_NOT_EXIST:
233 return ERR_JS_ACCOUNT_NOT_EXIST;
234 case ERR_APPACCOUNT_SERVICE_OAUTH_AUTHENTICATOR_NOT_EXIST:
235 return ERR_JS_OAUTH_AUTHENTICATOR_NOT_EXIST;
236 case ERR_APPACCOUNT_SERVICE_OAUTH_BUSY:
237 return ERR_JS_OAUTH_SERVICE_BUSY;
238 case ERR_APPACCOUNT_SERVICE_OAUTH_LIST_MAX_SIZE:
239 return ERR_JS_OAUTH_LIST_TOO_LARGE;
240 case ERR_APPACCOUNT_SERVICE_OAUTH_SESSION_NOT_EXIST:
241 return ERR_JS_OAUTH_SESSION_NOT_EXIST;
242 case ERR_APPACCOUNT_SERVICE_OAUTH_TOKEN_NOT_EXIST:
243 return ERR_JS_OAUTH_TOKEN_NOT_EXIST;
244 case ERR_APPACCOUNT_SERVICE_OAUTH_TOKEN_MAX_SIZE:
245 return ERR_JS_OAUTH_TOKEN_TOO_MANY;
246 case ERR_APPACCOUNT_SERVICE_PERMISSION_DENIED:
247 case ERR_APPACCOUNT_SERVICE_SUBSCRIBE_PERMISSION_DENIED:
248 case ERR_ACCOUNT_ZIDL_CHECK_PERMISSION_ERROR:
249 return ERR_JS_PERMISSION_DENIED_V8;
250 default:
251 return ERR_JS_APP_ACCOUNT_SERVICE_EXCEPTION;
252 }
253 }
254
ConvertToJSErrCodeV8(int32_t errCode)255 int32_t ConvertToJSErrCodeV8(int32_t errCode)
256 {
257 if ((errCode >= ERR_APPACCOUNT_KIT_NAME_IS_EMPTY && errCode <= ERR_APPACCOUNT_KIT_SEND_REQUEST) ||
258 (errCode >= ERR_APPACCOUNT_SERVICE_NAME_IS_EMPTY && errCode <= ERR_APPACCOUNT_SERVICE_INVALID_PARAMETER) ||
259 (errCode >= ERR_APPACCOUNT_SERVICE_ADD_EXISTING_ACCOUNT &&
260 errCode <= ERR_APPACCOUNT_SERVICE_DISABLE_APP_ACCESS_NOT_EXISTED)) {
261 return ERR_JS_INVALID_REQUEST;
262 } else if ((errCode >= ERR_APPACCOUNT_KIT_READ_PARCELABLE_APP_ACCOUNT_INFO &&
263 errCode <= ERR_APPACCOUNT_KIT_READ_PARCELABLE_VECTOR_ACCOUNT_INFO) ||
264 (errCode == ERR_APPACCOUNT_SERVICE_OAUTH_INVALID_RESPONSE) ||
265 (errCode == ERR_APPACCOUNT_SERVICE_OAUTH_AUTHENTICATOR_CALLBACK_NOT_EXIST)) {
266 return ERR_JS_INVALID_RESPONSE;
267 } else {
268 return ConvertOtherJSErrCodeV8(errCode);
269 }
270 }
271 } // namespace AccountSA
272 } // namespace OHOS