1  /*
2   * Copyright (c) 2021-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 "app_account_stub.h"
17  
18  #include "account_error_no.h"
19  #include "account_log_wrapper.h"
20  #include "app_account_constants.h"
21  #include "account_constants.h"
22  #include "ipc_skeleton.h"
23  #include "memory_guard.h"
24  #ifdef HICOLLIE_ENABLE
25  #include "xcollie/xcollie.h"
26  #endif // HICOLLIE_ENABLE
27  
28  namespace OHOS {
29  namespace AccountSA {
30  #define RETURN_IF_STRING_CONTAINS_SPECIAL_CHAR(str, reply)                        \
31      if (CheckSpecialCharacters(str) != ERR_OK) {                           \
32          ACCOUNT_LOGE("fail to check special characters");                  \
33          if (!(reply).WriteInt32(ERR_ACCOUNT_COMMON_INVALID_PARAMETER)) { \
34              ACCOUNT_LOGE("failed to write reply");                         \
35              return IPC_STUB_WRITE_PARCEL_ERR;                              \
36          }                                                                  \
37          return ERR_NONE;                                                   \
38      }                                                                      \
39  
40  #define RETURN_IF_STRING_IS_OVERSIZE(str, maxSize, msg, reply)                                                         \
41      if ((str).size() > (maxSize)) {                                                                             \
42          ACCOUNT_LOGE("%{public}s, input size: %{public}zu, max size: %{public}zu", msg, (str).size(), maxSize); \
43          if (!(reply).WriteInt32(ERR_ACCOUNT_COMMON_INVALID_PARAMETER)) {                                      \
44              ACCOUNT_LOGE("failed to write reply");                                                              \
45              return IPC_STUB_WRITE_PARCEL_ERR;                                                                   \
46          }                                                                                                       \
47          return ERR_NONE;                                                                                        \
48      }                                                                                                           \
49  
50  #define RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(str, maxSize, msg, reply)                                                \
51      if ((str).empty() || ((str).size() > (maxSize))) {                                                          \
52          ACCOUNT_LOGE("%{public}s, input size: %{public}zu, max size: %{public}zu", msg, (str).size(), maxSize); \
53          if (!(reply).WriteInt32(ERR_ACCOUNT_COMMON_INVALID_PARAMETER)) {                                      \
54              ACCOUNT_LOGE("failed to write reply");                                                              \
55              return IPC_STUB_WRITE_PARCEL_ERR;                                                                   \
56          }                                                                                                       \
57          return ERR_NONE;                                                                                        \
58      }                                                                                                           \
59  
60  const std::map<uint32_t, AppAccountStub::MessageProcFunction> messageProcMap = {
61      {
62          static_cast<uint32_t>(AppAccountInterfaceCode::ADD_ACCOUNT),
__anon506252a80102() 63          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
64              return ptr->ProcAddAccount(code, data, reply); }
65      },
66      {
67          static_cast<uint32_t>(AppAccountInterfaceCode::ADD_ACCOUNT_IMPLICITLY),
__anon506252a80202() 68          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
69              return ptr->ProcAddAccountImplicitly(code, data, reply); }
70      },
71      {
72          static_cast<uint32_t>(AppAccountInterfaceCode::CREATE_ACCOUNT),
__anon506252a80302() 73          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
74              return ptr->ProcCreateAccount(code, data, reply); }
75      },
76      {
77          static_cast<uint32_t>(AppAccountInterfaceCode::CREATE_ACCOUNT_IMPLICITLY),
__anon506252a80402() 78          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
79              return ptr->ProcCreateAccountImplicitly(code, data, reply); }
80      },
81      {
82          static_cast<uint32_t>(AppAccountInterfaceCode::DELETE_ACCOUNT),
__anon506252a80502() 83          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
84              return ptr->ProcDeleteAccount(code, data, reply); }
85      },
86      {
87          static_cast<uint32_t>(AppAccountInterfaceCode::GET_ACCOUNT_EXTRA_INFO),
__anon506252a80602() 88          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
89              return ptr->ProcGetAccountExtraInfo(code, data, reply); }
90      },
91      {
92          static_cast<uint32_t>(AppAccountInterfaceCode::SET_ACCOUNT_EXTRA_INFO),
__anon506252a80702() 93          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
94              return ptr->ProcSetAccountExtraInfo(code, data, reply); }
95      },
96      {
97          static_cast<uint32_t>(AppAccountInterfaceCode::ENABLE_APP_ACCESS),
__anon506252a80802() 98          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
99              return ptr->ProcSetAppAccess(code, data, reply); }
100      },
101      {
102          static_cast<uint32_t>(AppAccountInterfaceCode::DISABLE_APP_ACCESS),
__anon506252a80902() 103          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
104              return ptr->ProcSetAppAccess(code, data, reply); }
105      },
106      {
107          static_cast<uint32_t>(AppAccountInterfaceCode::SET_APP_ACCESS),
__anon506252a80a02() 108          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
109              return ptr->ProcSetAppAccess(code, data, reply); }
110      },
111      {
112          static_cast<uint32_t>(AppAccountInterfaceCode::CHECK_APP_ACCESS),
__anon506252a80b02() 113          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
114              return ptr->ProcCheckAppAccess(code, data, reply); }
115      },
116      {
117          static_cast<uint32_t>(AppAccountInterfaceCode::CHECK_APP_ACCOUNT_SYNC_ENABLE),
__anon506252a80c02() 118          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
119              return ptr->ProcCheckAppAccountSyncEnable(code, data, reply); }
120      },
121      {
122          static_cast<uint32_t>(AppAccountInterfaceCode::SET_APP_ACCOUNT_SYNC_ENABLE),
__anon506252a80d02() 123          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
124              return ptr->ProcSetAppAccountSyncEnable(code, data, reply); }
125      },
126      {
127          static_cast<uint32_t>(AppAccountInterfaceCode::GET_ASSOCIATED_DATA),
__anon506252a80e02() 128          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
129              return ptr->ProcGetAssociatedData(code, data, reply); }
130      },
131      {
132          static_cast<uint32_t>(AppAccountInterfaceCode::SET_ASSOCIATED_DATA),
__anon506252a80f02() 133          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
134              return ptr->ProcSetAssociatedData(code, data, reply); }
135      },
136      {
137          static_cast<uint32_t>(AppAccountInterfaceCode::GET_ACCOUNT_CREDENTIAL),
__anon506252a81002() 138          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
139              return ptr->ProcGetAccountCredential(code, data, reply); }
140      },
141      {
142          static_cast<uint32_t>(AppAccountInterfaceCode::SET_ACCOUNT_CREDENTIAL),
__anon506252a81102() 143          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
144              return ptr->ProcSetAccountCredential(code, data, reply); }
145      },
146      {
147          static_cast<uint32_t>(AppAccountInterfaceCode::DELETE_ACCOUNT_CREDENTIAL),
__anon506252a81202() 148          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
149              return ptr->ProcDeleteAccountCredential(code, data, reply); }
150      },
151      {
152          static_cast<uint32_t>(AppAccountInterfaceCode::AUTHENTICATE),
__anon506252a81302() 153          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
154              return ptr->ProcAuthenticate(code, data, reply); }
155      },
156      {
157          static_cast<uint32_t>(AppAccountInterfaceCode::GET_OAUTH_TOKEN),
__anon506252a81402() 158          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
159              return ptr->ProcGetAuthToken(code, data, reply); }
160      },
161      {
162          static_cast<uint32_t>(AppAccountInterfaceCode::GET_AUTH_TOKEN),
__anon506252a81502() 163          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
164              return ptr->ProcGetAuthToken(code, data, reply); }
165      },
166      {
167          static_cast<uint32_t>(AppAccountInterfaceCode::SET_OAUTH_TOKEN),
__anon506252a81602() 168          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
169              return ptr->ProcSetOAuthToken(code, data, reply); }
170      },
171      {
172          static_cast<uint32_t>(AppAccountInterfaceCode::DELETE_OAUTH_TOKEN),
__anon506252a81702() 173          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
174              return ptr->ProcDeleteAuthToken(code, data, reply); }
175      },
176      {
177          static_cast<uint32_t>(AppAccountInterfaceCode::DELETE_AUTH_TOKEN),
__anon506252a81802() 178          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
179              return ptr->ProcDeleteAuthToken(code, data, reply); }
180      },
181      {
182          static_cast<uint32_t>(AppAccountInterfaceCode::SET_OAUTH_TOKEN_VISIBILITY),
__anon506252a81902() 183          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
184              return ptr->ProcSetAuthTokenVisibility(code, data, reply); }
185      },
186      {
187          static_cast<uint32_t>(AppAccountInterfaceCode::SET_AUTH_TOKEN_VISIBILITY),
__anon506252a81a02() 188          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
189              return ptr->ProcSetAuthTokenVisibility(code, data, reply); }
190      },
191      {
192          static_cast<uint32_t>(AppAccountInterfaceCode::CHECK_OAUTH_TOKEN_VISIBILITY),
__anon506252a81b02() 193          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
194              return ptr->ProcCheckAuthTokenVisibility(code, data, reply); }
195      },
196      {
197          static_cast<uint32_t>(AppAccountInterfaceCode::CHECK_AUTH_TOKEN_VISIBILITY),
__anon506252a81c02() 198          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
199              return ptr->ProcCheckAuthTokenVisibility(code, data, reply); }
200      },
201      {
202          static_cast<uint32_t>(AppAccountInterfaceCode::GET_AUTHENTICATOR_CALLBACK),
__anon506252a81d02() 203          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
204              return ptr->ProcGetAuthenticatorCallback(code, data, reply); }
205      },
206      {
207          static_cast<uint32_t>(AppAccountInterfaceCode::GET_AUTHENTICATOR_INFO),
__anon506252a81e02() 208          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
209              return ptr->ProcGetAuthenticatorInfo(code, data, reply); }
210      },
211      {
212          static_cast<uint32_t>(AppAccountInterfaceCode::GET_ALL_OAUTH_TOKENS),
__anon506252a81f02() 213          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
214              return ptr->ProcGetAllOAuthTokens(code, data, reply); }
215      },
216      {
217          static_cast<uint32_t>(AppAccountInterfaceCode::GET_OAUTH_LIST),
__anon506252a82002() 218          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
219              return ptr->ProcGetAuthList(code, data, reply); }
220      },
221      {
222          static_cast<uint32_t>(AppAccountInterfaceCode::GET_AUTH_LIST),
__anon506252a82102() 223          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
224              return ptr->ProcGetAuthList(code, data, reply); }
225      },
226      {
227          static_cast<uint32_t>(AppAccountInterfaceCode::GET_ALL_ACCOUNTS),
__anon506252a82202() 228          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
229              return ptr->ProcGetAllAccounts(code, data, reply); }
230      },
231      {
232          static_cast<uint32_t>(AppAccountInterfaceCode::GET_ALL_ACCESSIBLE_ACCOUNTS),
__anon506252a82302() 233          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
234              return ptr->ProcGetAllAccessibleAccounts(code, data, reply); }
235      },
236      {
237          static_cast<uint32_t>(AppAccountInterfaceCode::QUERY_ALL_ACCESSIBLE_ACCOUNTS),
__anon506252a82402() 238          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
239              return ptr->ProcGetAllAccessibleAccounts(code, data, reply); }
240      },
241      {
242          static_cast<uint32_t>(AppAccountInterfaceCode::SELECT_ACCOUNTS_BY_OPTIONS),
__anon506252a82502() 243          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
244              return ptr->ProcSelectAccountsByOptions(code, data, reply); }
245      },
246      {
247          static_cast<uint32_t>(AppAccountInterfaceCode::VERIFY_CREDENTIAL),
__anon506252a82602() 248          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
249              return ptr->ProcVerifyCredential(code, data, reply); }
250      },
251      {
252          static_cast<uint32_t>(AppAccountInterfaceCode::CHECK_ACCOUNT_LABELS),
__anon506252a82702() 253          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
254              return ptr->ProcCheckAccountLabels(code, data, reply); }
255      },
256      {
257          static_cast<uint32_t>(AppAccountInterfaceCode::SET_AUTHENTICATOR_PROPERTIES),
__anon506252a82802() 258          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
259              return ptr->ProcSetAuthenticatorProperties(code, data, reply); }
260      },
261      {
262          static_cast<uint32_t>(AppAccountInterfaceCode::SUBSCRIBE_ACCOUNT),
__anon506252a82902() 263          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
264              return ptr->ProcSubscribeAccount(code, data, reply); }
265      },
266      {
267          static_cast<uint32_t>(AppAccountInterfaceCode::UNSUBSCRIBE_ACCOUNT),
__anon506252a82a02() 268          [] (AppAccountStub *ptr, uint32_t code, MessageParcel &data, MessageParcel &reply) {
269              return ptr->ProcUnsubscribeAccount(code, data, reply); }
270      },
271  };
272  
AppAccountStub()273  AppAccountStub::AppAccountStub()
274  {
275      messageProcMap_ = messageProcMap;
276  }
277  
~AppAccountStub()278  AppAccountStub::~AppAccountStub()
279  {}
280  
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)281  int AppAccountStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
282  {
283      ACCOUNT_LOGD("Received stub message: %{public}d, callingUid: %{public}d", code, IPCSkeleton::GetCallingUid());
284      MemoryGuard cacheGuard;
285      if (data.ReadInterfaceToken() != GetDescriptor()) {
286          ACCOUNT_LOGE("failed to check descriptor! code %{public}u.", code);
287          return ERR_ACCOUNT_COMMON_CHECK_DESCRIPTOR_ERROR;
288      }
289  
290  #ifdef HICOLLIE_ENABLE
291      int timerId =
292          HiviewDFX::XCollie::GetInstance().SetTimer(TIMER_NAME, TIMEOUT, nullptr, nullptr, HiviewDFX::XCOLLIE_FLAG_LOG);
293  #endif // HICOLLIE_ENABLE
294  
295      auto messageProc = messageProcMap_.find(code);
296      if (messageProc != messageProcMap_.end()) {
297          auto messageProcFunction = messageProc->second;
298          if (messageProcFunction != nullptr) {
299              int ret = (messageProcFunction)(this, code, data, reply);
300  #ifdef HICOLLIE_ENABLE
301              HiviewDFX::XCollie::GetInstance().CancelTimer(timerId);
302  #endif // HICOLLIE_ENABLE
303              return ret;
304          }
305      }
306  
307      ACCOUNT_LOGD("end, code = %{public}u, flags = %{public}u", code, option.GetFlags());
308  #ifdef HICOLLIE_ENABLE
309      HiviewDFX::XCollie::GetInstance().CancelTimer(timerId);
310  #endif // HICOLLIE_ENABLE
311  
312      return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
313  }
314  
315  template<typename T>
WriteParcelableVector(const std::vector<T> & parcelableVector,MessageParcel & data)316  bool AppAccountStub::WriteParcelableVector(const std::vector<T> &parcelableVector, MessageParcel &data)
317  {
318      if (!data.WriteUint32(parcelableVector.size())) {
319          ACCOUNT_LOGE("failed to WriteInt32 for parcelableVector.size()");
320          return false;
321      }
322  
323      for (const auto &parcelable : parcelableVector) {
324          if (!data.WriteParcelable(&parcelable)) {
325              ACCOUNT_LOGE("failed to WriteParcelable for parcelable");
326              return false;
327          }
328      }
329  
330      return true;
331  }
332  
CheckSpecialCharacters(const std::string & str)333  static ErrCode CheckSpecialCharacters(const std::string &str)
334  {
335      for (auto specialCharacter : Constants::SPECIAL_CHARACTERS) {
336          std::size_t found = str.find(specialCharacter);
337          if (found != std::string::npos) {
338              ACCOUNT_LOGE("found a special character, specialCharacter = %{public}c", specialCharacter);
339              return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
340          }
341      }
342      return ERR_OK;
343  }
344  
ProcAddAccount(uint32_t code,MessageParcel & data,MessageParcel & reply)345  ErrCode AppAccountStub::ProcAddAccount(uint32_t code, MessageParcel &data, MessageParcel &reply)
346  {
347      std::string name = data.ReadString();
348      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize", reply);
349      RETURN_IF_STRING_CONTAINS_SPECIAL_CHAR(name, reply);
350      std::string extraInfo = data.ReadString();
351      RETURN_IF_STRING_IS_OVERSIZE(extraInfo, Constants::EXTRA_INFO_MAX_SIZE, "extraInfo is oversize", reply);
352      ErrCode result = AddAccount(name, extraInfo);
353      if (!reply.WriteInt32(result)) {
354          ACCOUNT_LOGE("failed to write reply");
355          return IPC_STUB_WRITE_PARCEL_ERR;
356      }
357      return ERR_NONE;
358  }
359  
ProcAddAccountImplicitly(uint32_t code,MessageParcel & data,MessageParcel & reply)360  ErrCode AppAccountStub::ProcAddAccountImplicitly(uint32_t code, MessageParcel &data, MessageParcel &reply)
361  {
362      std::string owner = data.ReadString();
363      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE, "owner is empty or oversize", reply);
364      std::string authType = data.ReadString();
365      RETURN_IF_STRING_IS_OVERSIZE(authType, Constants::AUTH_TYPE_MAX_SIZE, "authType is oversize", reply);
366      std::shared_ptr<AAFwk::Want> options(data.ReadParcelable<AAFwk::Want>());
367      auto callback = iface_cast<IAppAccountAuthenticatorCallback>(data.ReadRemoteObject());
368      ErrCode result = ERR_OK;
369      if ((options == nullptr) || (callback == nullptr)) {
370          ACCOUNT_LOGE("invalid options");
371          result = ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
372      } else {
373          RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(options->GetStringParam(Constants::KEY_CALLER_ABILITY_NAME),
374              Constants::ABILITY_NAME_MAX_SIZE, "abilityName is empty or oversize", reply);
375          result = AddAccountImplicitly(owner, authType, *options, callback);
376      }
377      if (!reply.WriteInt32(result)) {
378          ACCOUNT_LOGE("failed to write reply");
379          return IPC_STUB_WRITE_PARCEL_ERR;
380      }
381      return ERR_NONE;
382  }
383  
ProcCreateAccount(uint32_t code,MessageParcel & data,MessageParcel & reply)384  ErrCode AppAccountStub::ProcCreateAccount(uint32_t code, MessageParcel &data, MessageParcel &reply)
385  {
386      std::string name = data.ReadString();
387      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize", reply);
388      sptr<CreateAccountOptions> options = data.ReadParcelable<CreateAccountOptions>();
389      ErrCode result = ERR_OK;
390      if (options == nullptr) {
391          ACCOUNT_LOGE("invalid options");
392          result = ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
393      } else {
394          RETURN_IF_STRING_IS_OVERSIZE(
395              options->customData, Constants::MAX_CUSTOM_DATA_SIZE, "customData is oversize", reply);
396          for (const auto &it : options->customData) {
397              RETURN_IF_STRING_IS_OVERSIZE(
398                  it.first, Constants::ASSOCIATED_KEY_MAX_SIZE, "customData key is oversize", reply);
399              RETURN_IF_STRING_IS_OVERSIZE(
400                  it.second, Constants::ASSOCIATED_VALUE_MAX_SIZE, "customData value is oversize", reply);
401          }
402          result = CreateAccount(name, *options);
403      }
404      if (!reply.WriteInt32(result)) {
405          ACCOUNT_LOGE("failed to write reply");
406          return IPC_STUB_WRITE_PARCEL_ERR;
407      }
408      return ERR_NONE;
409  }
410  
ProcCreateAccountImplicitly(uint32_t code,MessageParcel & data,MessageParcel & reply)411  ErrCode AppAccountStub::ProcCreateAccountImplicitly(uint32_t code, MessageParcel &data, MessageParcel &reply)
412  {
413      std::string owner = data.ReadString();
414      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE, "owner is empty or oversize", reply);
415      sptr<CreateAccountImplicitlyOptions> options = data.ReadParcelable<CreateAccountImplicitlyOptions>();
416      auto callback = iface_cast<IAppAccountAuthenticatorCallback>(data.ReadRemoteObject());
417      ErrCode result = ERR_OK;
418      if ((options == nullptr) || (callback == nullptr)) {
419          ACCOUNT_LOGE("invalid options");
420          result = ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
421      } else {
422          RETURN_IF_STRING_IS_OVERSIZE(
423              options->authType, Constants::AUTH_TYPE_MAX_SIZE, "authType is empty or oversize", reply);
424          RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(options->parameters.GetStringParam(Constants::KEY_CALLER_ABILITY_NAME),
425              Constants::ABILITY_NAME_MAX_SIZE, "abilityName is empty or oversize", reply);
426          RETURN_IF_STRING_IS_OVERSIZE(options->requiredLabels,
427              Constants::MAX_ALLOWED_ARRAY_SIZE_INPUT, "requiredLabels array is oversize", reply);
428          result = CreateAccountImplicitly(owner, *options, callback);
429      }
430      if (!reply.WriteInt32(result)) {
431          ACCOUNT_LOGE("failed to write reply");
432          return IPC_STUB_WRITE_PARCEL_ERR;
433      }
434      return ERR_NONE;
435  }
436  
ProcDeleteAccount(uint32_t code,MessageParcel & data,MessageParcel & reply)437  ErrCode AppAccountStub::ProcDeleteAccount(uint32_t code, MessageParcel &data, MessageParcel &reply)
438  {
439      std::string name = data.ReadString();
440      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize", reply);
441      ErrCode result = DeleteAccount(name);
442      if (!reply.WriteInt32(result)) {
443          ACCOUNT_LOGE("failed to write reply");
444          return IPC_STUB_WRITE_PARCEL_ERR;
445      }
446  
447      return ERR_NONE;
448  }
449  
ProcGetAccountExtraInfo(uint32_t code,MessageParcel & data,MessageParcel & reply)450  ErrCode AppAccountStub::ProcGetAccountExtraInfo(uint32_t code, MessageParcel &data, MessageParcel &reply)
451  {
452      std::string name = data.ReadString();
453      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize", reply);
454      RETURN_IF_STRING_CONTAINS_SPECIAL_CHAR(name, reply);
455      std::string extraInfo;
456      ErrCode result = GetAccountExtraInfo(name, extraInfo);
457      if (!reply.WriteInt32(result)) {
458          ACCOUNT_LOGE("failed to write reply");
459          return IPC_STUB_WRITE_PARCEL_ERR;
460      }
461      if (!reply.WriteString(extraInfo)) {
462          ACCOUNT_LOGE("failed to write string for extra info");
463          return IPC_STUB_WRITE_PARCEL_ERR;
464      }
465      return ERR_NONE;
466  }
467  
ProcSetAccountExtraInfo(uint32_t code,MessageParcel & data,MessageParcel & reply)468  ErrCode AppAccountStub::ProcSetAccountExtraInfo(uint32_t code, MessageParcel &data, MessageParcel &reply)
469  {
470      std::string name = data.ReadString();
471      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize", reply);
472      RETURN_IF_STRING_CONTAINS_SPECIAL_CHAR(name, reply);
473      std::string extraInfo = data.ReadString();
474      RETURN_IF_STRING_IS_OVERSIZE(extraInfo, Constants::EXTRA_INFO_MAX_SIZE, "extraInfo is oversize", reply);
475      ErrCode result = SetAccountExtraInfo(name, extraInfo);
476      if (!reply.WriteInt32(result)) {
477          ACCOUNT_LOGE("failed to write reply");
478          return IPC_STUB_WRITE_PARCEL_ERR;
479      }
480      return ERR_NONE;
481  }
482  
ProcSetAppAccess(uint32_t code,MessageParcel & data,MessageParcel & reply)483  ErrCode AppAccountStub::ProcSetAppAccess(uint32_t code, MessageParcel &data, MessageParcel &reply)
484  {
485      std::string name = data.ReadString();
486      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize", reply);
487      if (code != static_cast<uint32_t>(AppAccountInterfaceCode::SET_APP_ACCESS)) {
488          RETURN_IF_STRING_CONTAINS_SPECIAL_CHAR(name, reply);
489      }
490  
491      std::string authorizedApp = data.ReadString();
492      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(authorizedApp, Constants::BUNDLE_NAME_MAX_SIZE,
493          "bundleName is empty or oversize", reply);
494  
495      ErrCode result = ERR_OK;
496      if (code == static_cast<uint32_t>(AppAccountInterfaceCode::ENABLE_APP_ACCESS)) {
497          result = EnableAppAccess(name, authorizedApp);
498      } else if (code == static_cast<uint32_t>(AppAccountInterfaceCode::DISABLE_APP_ACCESS)) {
499          result = DisableAppAccess(name, authorizedApp);
500      } else if (code == static_cast<uint32_t>(AppAccountInterfaceCode::SET_APP_ACCESS)) {
501          bool isAccessible = data.ReadBool();
502          result = SetAppAccess(name, authorizedApp, isAccessible);
503      } else {
504          return IPC_INVOKER_ERR;
505      }
506  
507      if (!reply.WriteInt32(result)) {
508          ACCOUNT_LOGE("failed to write result");
509          return IPC_STUB_WRITE_PARCEL_ERR;
510      }
511      return ERR_NONE;
512  }
513  
ProcCheckAppAccountSyncEnable(uint32_t code,MessageParcel & data,MessageParcel & reply)514  ErrCode AppAccountStub::ProcCheckAppAccountSyncEnable(uint32_t code, MessageParcel &data, MessageParcel &reply)
515  {
516      std::string name = data.ReadString();
517      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize", reply);
518      bool syncEnable = false;
519      ErrCode result = CheckAppAccountSyncEnable(name, syncEnable);
520      if (!reply.WriteInt32(result)) {
521          ACCOUNT_LOGE("failed to write reply");
522          return IPC_STUB_WRITE_PARCEL_ERR;
523      }
524      if (!reply.WriteBool(syncEnable)) {
525          ACCOUNT_LOGE("failed to write bool for syncEnable");
526          return IPC_STUB_WRITE_PARCEL_ERR;
527      }
528      return ERR_NONE;
529  }
530  
ProcSetAppAccountSyncEnable(uint32_t code,MessageParcel & data,MessageParcel & reply)531  ErrCode AppAccountStub::ProcSetAppAccountSyncEnable(uint32_t code, MessageParcel &data, MessageParcel &reply)
532  {
533      std::string name = data.ReadString();
534      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize", reply);
535      bool syncEnable = data.ReadBool();
536      ErrCode result = SetAppAccountSyncEnable(name, syncEnable);
537      if (!reply.WriteInt32(result)) {
538          ACCOUNT_LOGE("failed to write reply");
539          return IPC_STUB_WRITE_PARCEL_ERR;
540      }
541      return ERR_NONE;
542  }
543  
ProcGetAssociatedData(uint32_t code,MessageParcel & data,MessageParcel & reply)544  ErrCode AppAccountStub::ProcGetAssociatedData(uint32_t code, MessageParcel &data, MessageParcel &reply)
545  {
546      std::string name = data.ReadString();
547      std::string key = data.ReadString();
548      std::string value;
549      ErrCode result = GetAssociatedData(name, key, value);
550      if (!reply.WriteInt32(result)) {
551          ACCOUNT_LOGE("failed to write reply");
552          return IPC_STUB_WRITE_PARCEL_ERR;
553      }
554      if (!reply.WriteString(value)) {
555          ACCOUNT_LOGE("failed to write string for value");
556          return IPC_STUB_WRITE_PARCEL_ERR;
557      }
558      return ERR_NONE;
559  }
560  
ProcSetAssociatedData(uint32_t code,MessageParcel & data,MessageParcel & reply)561  ErrCode AppAccountStub::ProcSetAssociatedData(uint32_t code, MessageParcel &data, MessageParcel &reply)
562  {
563      std::string name = data.ReadString();
564      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize", reply);
565      std::string key = data.ReadString();
566      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(key, Constants::ASSOCIATED_KEY_MAX_SIZE, "key is empty or oversize", reply);
567      std::string value = data.ReadString();
568      RETURN_IF_STRING_IS_OVERSIZE(value, Constants::ASSOCIATED_VALUE_MAX_SIZE, "value is oversize", reply);
569      ErrCode result = SetAssociatedData(name, key, value);
570      if (!reply.WriteInt32(result)) {
571          ACCOUNT_LOGE("failed to write reply");
572          return IPC_STUB_WRITE_PARCEL_ERR;
573      }
574      return ERR_NONE;
575  }
576  
ProcGetAccountCredential(uint32_t code,MessageParcel & data,MessageParcel & reply)577  ErrCode AppAccountStub::ProcGetAccountCredential(uint32_t code, MessageParcel &data, MessageParcel &reply)
578  {
579      std::string name = data.ReadString();
580      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize", reply);
581      std::string credentialType = data.ReadString();
582      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(credentialType, Constants::CREDENTIAL_TYPE_MAX_SIZE,
583          "credentialType is empty or oversize", reply);
584      std::string credential;
585      ErrCode result = GetAccountCredential(name, credentialType, credential);
586      if (!reply.WriteInt32(result)) {
587          ACCOUNT_LOGE("failed to write reply");
588          return IPC_STUB_WRITE_PARCEL_ERR;
589      }
590      if (!reply.WriteString(credential)) {
591          ACCOUNT_LOGE("failed to write string for credential");
592          return IPC_STUB_WRITE_PARCEL_ERR;
593      }
594      return ERR_NONE;
595  }
596  
ProcSetAccountCredential(uint32_t code,MessageParcel & data,MessageParcel & reply)597  ErrCode AppAccountStub::ProcSetAccountCredential(uint32_t code, MessageParcel &data, MessageParcel &reply)
598  {
599      std::string name = data.ReadString();
600      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize", reply);
601      std::string credentialType = data.ReadString();
602      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(credentialType, Constants::CREDENTIAL_TYPE_MAX_SIZE,
603          "credentialType is empty or oversize", reply);
604      std::string credential = data.ReadString();
605      RETURN_IF_STRING_IS_OVERSIZE(credential, Constants::CREDENTIAL_MAX_SIZE, "credential is oversize", reply);
606      ErrCode result = SetAccountCredential(name, credentialType, credential);
607      if (!reply.WriteInt32(result)) {
608          ACCOUNT_LOGE("failed to write reply");
609          return IPC_STUB_WRITE_PARCEL_ERR;
610      }
611      return ERR_NONE;
612  }
613  
ProcAuthenticate(uint32_t code,MessageParcel & data,MessageParcel & reply)614  ErrCode AppAccountStub::ProcAuthenticate(uint32_t code, MessageParcel &data, MessageParcel &reply)
615  {
616      std::string name = data.ReadString();
617      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize", reply);
618      std::string owner = data.ReadString();
619      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE, "owner is empty or oversize", reply);
620      std::string authType = data.ReadString();
621      RETURN_IF_STRING_IS_OVERSIZE(authType, Constants::AUTH_TYPE_MAX_SIZE, "authType is oversize", reply);
622      std::shared_ptr<AAFwk::Want> options(data.ReadParcelable<AAFwk::Want>());
623      auto callback = iface_cast<IAppAccountAuthenticatorCallback>(data.ReadRemoteObject());
624      ErrCode result = ERR_OK;
625      if ((options == nullptr) || (callback == nullptr)) {
626          ACCOUNT_LOGE("invalid options");
627          result = ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
628      } else {
629          RETURN_IF_STRING_IS_OVERSIZE(options->GetStringParam(Constants::KEY_CALLER_ABILITY_NAME),
630              Constants::ABILITY_NAME_MAX_SIZE, "abilityName is empty or oversize", reply);
631          result = Authenticate(name, owner, authType, *options, callback);
632      }
633      if (!reply.WriteInt32(result)) {
634          ACCOUNT_LOGE("failed to write reply");
635          return IPC_STUB_WRITE_PARCEL_ERR;
636      }
637      return ERR_NONE;
638  }
639  
ProcGetAuthToken(uint32_t code,MessageParcel & data,MessageParcel & reply)640  ErrCode AppAccountStub::ProcGetAuthToken(uint32_t code, MessageParcel &data, MessageParcel &reply)
641  {
642      std::string name = data.ReadString();
643      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize", reply);
644      std::string owner = data.ReadString();
645      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE, "owner is empty or oversize", reply);
646      std::string authType = data.ReadString();
647      RETURN_IF_STRING_IS_OVERSIZE(authType, Constants::AUTH_TYPE_MAX_SIZE, "authType is oversize", reply);
648      std::string token;
649      ErrCode result = ERR_OK;
650      if (code == static_cast<uint32_t>(AppAccountInterfaceCode::GET_OAUTH_TOKEN)) {
651          RETURN_IF_STRING_CONTAINS_SPECIAL_CHAR(name, reply);
652          result = GetOAuthToken(name, owner, authType, token);
653      } else if (code == static_cast<uint32_t>(AppAccountInterfaceCode::GET_AUTH_TOKEN)) {
654          result = GetAuthToken(name, owner, authType, token);
655      } else {
656          ACCOUNT_LOGE("stub code is invalid");
657          return IPC_INVOKER_ERR;
658      }
659      if ((!reply.WriteInt32(result)) || (!reply.WriteString(token))) {
660          ACCOUNT_LOGE("failed to write reply");
661          return IPC_STUB_WRITE_PARCEL_ERR;
662      }
663      return ERR_NONE;
664  }
665  
ProcSetOAuthToken(uint32_t code,MessageParcel & data,MessageParcel & reply)666  ErrCode AppAccountStub::ProcSetOAuthToken(uint32_t code, MessageParcel &data, MessageParcel &reply)
667  {
668      std::string name = data.ReadString();
669      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize", reply);
670      std::string authType = data.ReadString();
671      RETURN_IF_STRING_IS_OVERSIZE(authType, Constants::AUTH_TYPE_MAX_SIZE, "authType is oversize", reply);
672      std::string token = data.ReadString();
673      RETURN_IF_STRING_IS_OVERSIZE(token, Constants::TOKEN_MAX_SIZE, "token is oversize", reply);
674      ErrCode result = SetOAuthToken(name, authType, token);
675      if (!reply.WriteInt32(result)) {
676          ACCOUNT_LOGE("failed to write reply");
677          return IPC_STUB_WRITE_PARCEL_ERR;
678      }
679      return ERR_NONE;
680  }
681  
ProcDeleteAuthToken(uint32_t code,MessageParcel & data,MessageParcel & reply)682  ErrCode AppAccountStub::ProcDeleteAuthToken(uint32_t code, MessageParcel &data, MessageParcel &reply)
683  {
684      std::string name = data.ReadString();
685      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize", reply);
686      std::string owner = data.ReadString();
687      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE, "owner is empty or oversize", reply);
688      std::string authType = data.ReadString();
689      RETURN_IF_STRING_IS_OVERSIZE(authType, Constants::AUTH_TYPE_MAX_SIZE, "authType is oversize", reply);
690      std::string token = data.ReadString();
691      RETURN_IF_STRING_IS_OVERSIZE(token, Constants::TOKEN_MAX_SIZE, "token is oversize", reply);
692  
693      ErrCode result = ERR_OK;
694      if (code == static_cast<uint32_t>(AppAccountInterfaceCode::DELETE_OAUTH_TOKEN)) {
695          RETURN_IF_STRING_CONTAINS_SPECIAL_CHAR(name, reply);
696          result = DeleteOAuthToken(name, owner, authType, token);
697      } else if (code == static_cast<uint32_t>(AppAccountInterfaceCode::DELETE_AUTH_TOKEN)) {
698          result = DeleteAuthToken(name, owner, authType, token);
699      } else {
700          return IPC_INVOKER_ERR;
701      }
702  
703      if (!reply.WriteInt32(result)) {
704          ACCOUNT_LOGE("failed to write result");
705          return IPC_STUB_WRITE_PARCEL_ERR;
706      }
707      return ERR_NONE;
708  }
709  
ProcSetAuthTokenVisibility(uint32_t code,MessageParcel & data,MessageParcel & reply)710  ErrCode AppAccountStub::ProcSetAuthTokenVisibility(uint32_t code, MessageParcel &data, MessageParcel &reply)
711  {
712      std::string name = data.ReadString();
713      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize", reply);
714      std::string authType = data.ReadString();
715      RETURN_IF_STRING_IS_OVERSIZE(authType, Constants::AUTH_TYPE_MAX_SIZE, "authType is oversize", reply);
716      std::string bundleName = data.ReadString();
717      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(bundleName, Constants::BUNDLE_NAME_MAX_SIZE,
718          "bundleName is empty or oversize", reply);
719      bool isVisible = data.ReadBool();
720      ErrCode result = ERR_OK;
721      if (code == static_cast<uint32_t>(AppAccountInterfaceCode::SET_OAUTH_TOKEN_VISIBILITY)) {
722          RETURN_IF_STRING_CONTAINS_SPECIAL_CHAR(name, reply);
723          result = SetOAuthTokenVisibility(name, authType, bundleName, isVisible);
724      } else if (code == static_cast<uint32_t>(AppAccountInterfaceCode::SET_AUTH_TOKEN_VISIBILITY)) {
725          result = SetAuthTokenVisibility(name, authType, bundleName, isVisible);
726      } else {
727          return IPC_INVOKER_ERR;
728      }
729  
730      if (!reply.WriteInt32(result)) {
731          ACCOUNT_LOGE("failed to write result");
732          return IPC_STUB_WRITE_PARCEL_ERR;
733      }
734      return ERR_NONE;
735  }
736  
ProcCheckAuthTokenVisibility(uint32_t code,MessageParcel & data,MessageParcel & reply)737  ErrCode AppAccountStub::ProcCheckAuthTokenVisibility(uint32_t code, MessageParcel &data, MessageParcel &reply)
738  {
739      std::string name = data.ReadString();
740      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize", reply);
741      std::string authType = data.ReadString();
742      RETURN_IF_STRING_IS_OVERSIZE(authType, Constants::AUTH_TYPE_MAX_SIZE, "authType is oversize", reply);
743      std::string bundleName = data.ReadString();
744      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(bundleName, Constants::BUNDLE_NAME_MAX_SIZE,
745          "bundleName is empty or oversize", reply);
746      bool isVisible = false;
747      ErrCode result = ERR_OK;
748      if (code == static_cast<uint32_t>(AppAccountInterfaceCode::CHECK_OAUTH_TOKEN_VISIBILITY)) {
749          RETURN_IF_STRING_CONTAINS_SPECIAL_CHAR(name, reply);
750          result = CheckOAuthTokenVisibility(name, authType, bundleName, isVisible);
751      } else if (code == static_cast<uint32_t>(AppAccountInterfaceCode::CHECK_AUTH_TOKEN_VISIBILITY)) {
752          result = CheckAuthTokenVisibility(name, authType, bundleName, isVisible);
753      } else {
754          ACCOUNT_LOGE("stub code is invalid");
755          return IPC_INVOKER_ERR;
756      }
757  
758      if ((!reply.WriteInt32(result)) || (!reply.WriteBool(isVisible))) {
759          ACCOUNT_LOGE("failed to write reply");
760          return IPC_STUB_WRITE_PARCEL_ERR;
761      }
762      return ERR_NONE;
763  }
764  
ProcGetAuthenticatorInfo(uint32_t code,MessageParcel & data,MessageParcel & reply)765  ErrCode AppAccountStub::ProcGetAuthenticatorInfo(uint32_t code, MessageParcel &data, MessageParcel &reply)
766  {
767      std::string owner = data.ReadString();
768      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE, "owner is empty or oversize", reply);
769      AuthenticatorInfo info;
770      ErrCode result = GetAuthenticatorInfo(owner, info);
771      if ((!reply.WriteInt32(result)) || (!reply.WriteString(info.owner)) ||
772          (!reply.WriteInt32(info.iconId)) || (!reply.WriteInt32(info.labelId))) {
773          ACCOUNT_LOGE("failed to write reply");
774          return IPC_STUB_WRITE_PARCEL_ERR;
775      }
776      return ERR_NONE;
777  }
778  
ProcGetAllOAuthTokens(uint32_t code,MessageParcel & data,MessageParcel & reply)779  ErrCode AppAccountStub::ProcGetAllOAuthTokens(uint32_t code, MessageParcel &data, MessageParcel &reply)
780  {
781      std::string name = data.ReadString();
782      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize", reply);
783      std::string owner = data.ReadString();
784      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE, "owner is empty or oversize", reply);
785      std::vector<OAuthTokenInfo> tokenInfos;
786      ErrCode result = GetAllOAuthTokens(name, owner, tokenInfos);
787      if ((!reply.WriteInt32(result)) || (!reply.WriteUint32(tokenInfos.size()))) {
788          ACCOUNT_LOGE("failed to write reply");
789          return IPC_STUB_WRITE_PARCEL_ERR;
790      }
791      for (auto tokenInfo : tokenInfos) {
792          if ((!reply.WriteString(tokenInfo.token)) || (!reply.WriteString(tokenInfo.authType))) {
793              ACCOUNT_LOGE("failed to write reply");
794              return IPC_STUB_WRITE_PARCEL_ERR;
795          }
796      }
797      return ERR_NONE;
798  }
799  
ProcGetAuthList(uint32_t code,MessageParcel & data,MessageParcel & reply)800  ErrCode AppAccountStub::ProcGetAuthList(uint32_t code, MessageParcel &data, MessageParcel &reply)
801  {
802      std::string name = data.ReadString();
803      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize", reply);
804      std::string authType = data.ReadString();
805      RETURN_IF_STRING_IS_OVERSIZE(authType, Constants::OWNER_MAX_SIZE, "authType is oversize", reply);
806      std::set<std::string> oauthList;
807      ErrCode result = ERR_OK;
808      if (code == static_cast<uint32_t>(AppAccountInterfaceCode::GET_OAUTH_LIST)) {
809          RETURN_IF_STRING_CONTAINS_SPECIAL_CHAR(name, reply);
810          result = GetOAuthList(name, authType, oauthList);
811      } else if (code == static_cast<uint32_t>(AppAccountInterfaceCode::GET_AUTH_LIST)) {
812          result = GetAuthList(name, authType, oauthList);
813      } else {
814          ACCOUNT_LOGE("stub code is invalid");
815          return IPC_INVOKER_ERR;
816      }
817      if ((!reply.WriteInt32(result)) || (!reply.WriteUint32(oauthList.size()))) {
818          ACCOUNT_LOGE("failed to write reply");
819          return IPC_STUB_WRITE_PARCEL_ERR;
820      }
821      for (auto bundleName : oauthList) {
822          if (!reply.WriteString(bundleName)) {
823              ACCOUNT_LOGE("failed to WriteString for bundleName");
824              return IPC_STUB_WRITE_PARCEL_ERR;
825          }
826      }
827      return ERR_NONE;
828  }
829  
ProcGetAuthenticatorCallback(uint32_t code,MessageParcel & data,MessageParcel & reply)830  ErrCode AppAccountStub::ProcGetAuthenticatorCallback(uint32_t code, MessageParcel &data, MessageParcel &reply)
831  {
832      std::string sessionId = data.ReadString();
833      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(sessionId, Constants::SESSION_ID_MAX_SIZE,
834          "sessionId is empty or oversize", reply);
835      sptr<IRemoteObject> callback;
836      ErrCode result = GetAuthenticatorCallback(sessionId, callback);
837      if ((!reply.WriteInt32(result)) || (!reply.WriteRemoteObject(callback))) {
838          ACCOUNT_LOGE("failed to write reply");
839          return IPC_STUB_WRITE_PARCEL_ERR;
840      }
841      return ERR_NONE;
842  }
843  
ProcGetAllAccounts(uint32_t code,MessageParcel & data,MessageParcel & reply)844  ErrCode AppAccountStub::ProcGetAllAccounts(uint32_t code, MessageParcel &data, MessageParcel &reply)
845  {
846      std::string owner = data.ReadString();
847      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE, "owner is empty or oversize", reply);
848      std::vector<AppAccountInfo> appAccounts;
849      ErrCode result = GetAllAccounts(owner, appAccounts);
850      if (!reply.WriteInt32(result)) {
851          ACCOUNT_LOGE("failed to write reply");
852          return IPC_STUB_WRITE_PARCEL_ERR;
853      }
854      if (!WriteParcelableVector(appAccounts, reply)) {
855          ACCOUNT_LOGE("failed to write accounts");
856          return IPC_STUB_WRITE_PARCEL_ERR;
857      }
858      return ERR_NONE;
859  }
860  
ProcGetAllAccessibleAccounts(uint32_t code,MessageParcel & data,MessageParcel & reply)861  ErrCode AppAccountStub::ProcGetAllAccessibleAccounts(uint32_t code, MessageParcel &data, MessageParcel &reply)
862  {
863      std::vector<AppAccountInfo> appAccounts;
864      ErrCode result = ERR_OK;
865      if (code == static_cast<uint32_t>(AppAccountInterfaceCode::GET_ALL_ACCESSIBLE_ACCOUNTS)) {
866          result = GetAllAccessibleAccounts(appAccounts);
867      } else if (code == static_cast<uint32_t>(AppAccountInterfaceCode::QUERY_ALL_ACCESSIBLE_ACCOUNTS)) {
868          std::string owner = data.ReadString();
869          RETURN_IF_STRING_IS_OVERSIZE(owner, Constants::OWNER_MAX_SIZE, "owner is or oversize", reply);
870          result = QueryAllAccessibleAccounts(owner, appAccounts);
871      } else {
872          return IPC_INVOKER_ERR;
873      }
874      if (!reply.WriteInt32(result)) {
875          ACCOUNT_LOGE("failed to write result");
876          return IPC_STUB_WRITE_PARCEL_ERR;
877      }
878      if (!WriteParcelableVector(appAccounts, reply)) {
879          ACCOUNT_LOGE("failed to write accessible accounts");
880          return IPC_STUB_WRITE_PARCEL_ERR;
881      }
882      return ERR_NONE;
883  }
884  
ProcCheckAppAccess(uint32_t code,MessageParcel & data,MessageParcel & reply)885  ErrCode AppAccountStub::ProcCheckAppAccess(uint32_t code, MessageParcel &data, MessageParcel &reply)
886  {
887      std::string name = data.ReadString();
888      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize", reply);
889      std::string bundleName = data.ReadString();
890      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(bundleName, Constants::BUNDLE_NAME_MAX_SIZE,
891          "bundleName is empty or oversize", reply);
892      bool isAccessible = false;
893      ErrCode result = CheckAppAccess(name, bundleName, isAccessible);
894      if ((!reply.WriteInt32(result)) || (!reply.WriteBool(isAccessible))) {
895          ACCOUNT_LOGE("failed to write reply");
896          return IPC_STUB_WRITE_PARCEL_ERR;
897      }
898      return ERR_NONE;
899  }
900  
ProcDeleteAccountCredential(uint32_t code,MessageParcel & data,MessageParcel & reply)901  ErrCode AppAccountStub::ProcDeleteAccountCredential(uint32_t code, MessageParcel &data, MessageParcel &reply)
902  {
903      std::string name = data.ReadString();
904      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize", reply);
905      std::string credentialType = data.ReadString();
906      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(credentialType, Constants::CREDENTIAL_TYPE_MAX_SIZE,
907          "credentialType is empty or oversize", reply);
908      ErrCode result = DeleteAccountCredential(name, credentialType);
909      if (!reply.WriteInt32(result)) {
910          ACCOUNT_LOGE("failed to write reply");
911          return IPC_STUB_WRITE_PARCEL_ERR;
912      }
913      return ERR_NONE;
914  }
915  
ProcSelectAccountsByOptions(uint32_t code,MessageParcel & data,MessageParcel & reply)916  ErrCode AppAccountStub::ProcSelectAccountsByOptions(uint32_t code, MessageParcel &data, MessageParcel &reply)
917  {
918      std::shared_ptr<SelectAccountsOptions> options(data.ReadParcelable<SelectAccountsOptions>());
919      auto callback = iface_cast<IAppAccountAuthenticatorCallback>(data.ReadRemoteObject());
920      ErrCode result = ERR_OK;
921      if ((options == nullptr) || (callback == nullptr)) {
922          ACCOUNT_LOGE("invalid parameters");
923          result = ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
924      } else {
925          RETURN_IF_STRING_IS_OVERSIZE(options->allowedAccounts,
926              Constants::MAX_ALLOWED_ARRAY_SIZE_INPUT, "allowedAccounts array is oversize", reply);
927          RETURN_IF_STRING_IS_OVERSIZE(options->allowedOwners,
928              Constants::MAX_ALLOWED_ARRAY_SIZE_INPUT, "allowedOwners array is oversize", reply);
929          RETURN_IF_STRING_IS_OVERSIZE(options->requiredLabels,
930              Constants::MAX_ALLOWED_ARRAY_SIZE_INPUT, "requiredLabels array is oversize", reply);
931          result = SelectAccountsByOptions(*options, callback);
932      }
933      if (!reply.WriteInt32(result)) {
934          ACCOUNT_LOGE("failed to write reply");
935          return IPC_STUB_WRITE_PARCEL_ERR;
936      }
937      return ERR_NONE;
938  }
939  
ProcVerifyCredential(uint32_t code,MessageParcel & data,MessageParcel & reply)940  ErrCode AppAccountStub::ProcVerifyCredential(uint32_t code, MessageParcel &data, MessageParcel &reply)
941  {
942      std::string name = data.ReadString();
943      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize", reply);
944      std::string owner = data.ReadString();
945      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE, "owner is empty or oversize", reply);
946      std::shared_ptr<VerifyCredentialOptions> options(data.ReadParcelable<VerifyCredentialOptions>());
947      auto callback = iface_cast<IAppAccountAuthenticatorCallback>(data.ReadRemoteObject());
948      ErrCode result = ERR_OK;
949      if ((options == nullptr) || (callback == nullptr)) {
950          ACCOUNT_LOGE("invalid parameters");
951          result = ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
952      } else {
953          RETURN_IF_STRING_IS_OVERSIZE(
954              options->credentialType, Constants::CREDENTIAL_TYPE_MAX_SIZE, "the credential type is oversize", reply);
955          RETURN_IF_STRING_IS_OVERSIZE(
956              options->credential, Constants::CREDENTIAL_MAX_SIZE, "the credential is oversize", reply);
957          result = VerifyCredential(name, owner, *options, callback);
958      }
959      if (!reply.WriteInt32(result)) {
960          ACCOUNT_LOGE("failed to write reply");
961          return IPC_STUB_WRITE_PARCEL_ERR;
962      }
963      return ERR_NONE;
964  }
965  
ProcCheckAccountLabels(uint32_t code,MessageParcel & data,MessageParcel & reply)966  ErrCode AppAccountStub::ProcCheckAccountLabels(uint32_t code, MessageParcel &data, MessageParcel &reply)
967  {
968      std::string name = data.ReadString();
969      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize", reply);
970      std::string owner = data.ReadString();
971      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE, "owner is empty or oversize", reply);
972      std::vector<std::string> labels;
973      data.ReadStringVector(&labels);
974      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(
975          labels, Constants::MAX_ALLOWED_ARRAY_SIZE_INPUT, "labels array is empty or oversize", reply);
976      auto callback = iface_cast<IAppAccountAuthenticatorCallback>(data.ReadRemoteObject());
977      ErrCode result = ERR_OK;
978      if (callback == nullptr) {
979          ACCOUNT_LOGE("invalid options");
980          result = ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
981      } else {
982          result = CheckAccountLabels(name, owner, labels, callback);
983      }
984      if (!reply.WriteInt32(result)) {
985          ACCOUNT_LOGE("failed to write reply");
986          return IPC_STUB_WRITE_PARCEL_ERR;
987      }
988      return ERR_NONE;
989  }
990  
ProcSetAuthenticatorProperties(uint32_t code,MessageParcel & data,MessageParcel & reply)991  ErrCode AppAccountStub::ProcSetAuthenticatorProperties(uint32_t code, MessageParcel &data, MessageParcel &reply)
992  {
993      std::string owner = data.ReadString();
994      RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE,  "owner is empty or oversize", reply);
995      std::shared_ptr<SetPropertiesOptions> options(data.ReadParcelable<SetPropertiesOptions>());
996      auto callback = iface_cast<IAppAccountAuthenticatorCallback>(data.ReadRemoteObject());
997      ErrCode result = ERR_OK;
998      if ((options == nullptr) || (callback == nullptr)) {
999          ACCOUNT_LOGE("invalid parameters");
1000          result = ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
1001      } else {
1002          result = SetAuthenticatorProperties(owner, *options, callback);
1003      }
1004      if (!reply.WriteInt32(result)) {
1005          ACCOUNT_LOGE("failed to write reply");
1006          return IPC_STUB_WRITE_PARCEL_ERR;
1007      }
1008      return ERR_NONE;
1009  }
1010  
ProcSubscribeAccount(uint32_t code,MessageParcel & data,MessageParcel & reply)1011  ErrCode AppAccountStub::ProcSubscribeAccount(uint32_t code, MessageParcel &data, MessageParcel &reply)
1012  {
1013      std::unique_ptr<AppAccountSubscribeInfo> subscribeInfo(data.ReadParcelable<AppAccountSubscribeInfo>());
1014      if (!subscribeInfo) {
1015          ACCOUNT_LOGE("failed to read parcelable for subscribeInfo");
1016          return IPC_STUB_INVALID_DATA_ERR;
1017      }
1018      sptr<IRemoteObject> eventListener = data.ReadRemoteObject();
1019      if (eventListener == nullptr) {
1020          ACCOUNT_LOGE("failed to read remote object for eventListener");
1021          return IPC_STUB_INVALID_DATA_ERR;
1022      }
1023      ErrCode result = SubscribeAppAccount(*subscribeInfo, eventListener);
1024      if (!reply.WriteInt32(result)) {
1025          ACCOUNT_LOGE("failed to write reply");
1026          return IPC_STUB_WRITE_PARCEL_ERR;
1027      }
1028      return ERR_NONE;
1029  }
1030  
ProcUnsubscribeAccount(uint32_t code,MessageParcel & data,MessageParcel & reply)1031  ErrCode AppAccountStub::ProcUnsubscribeAccount(uint32_t code, MessageParcel &data, MessageParcel &reply)
1032  {
1033      sptr<IRemoteObject> eventListener = data.ReadRemoteObject();
1034      if (eventListener == nullptr) {
1035          ACCOUNT_LOGE("failed to read remote object for eventListener");
1036          return IPC_STUB_INVALID_DATA_ERR;
1037      }
1038      ErrCode result = UnsubscribeAppAccount(eventListener);
1039      if (!reply.WriteInt32(result)) {
1040          ACCOUNT_LOGE("failed to write reply");
1041          return IPC_STUB_WRITE_PARCEL_ERR;
1042      }
1043      return ERR_NONE;
1044  }
1045  }  // namespace AccountSA
1046  }  // namespace OHOS
1047