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