• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "appaccount_impl.h"
17 
18 #include "account_error_no.h"
19 #include "account_log_wrapper.h"
20 #include "app_account_manager.h"
21 #include "cj_lambda.h"
22 #include "securec.h"
23 #include "appaccount_common.h"
24 #include "appaccount_error.h"
25 #include "appaccount_defination.h"
26 #include "appaccount_parameter_parse.h"
27 
28 namespace OHOS::AccountSA {
createAccount(std::string name,CCreateAccountOptions cOptions)29 int32_t CJAppAccountImpl::createAccount(std::string name, CCreateAccountOptions cOptions)
30 {
31     CreateAccountOptions options{};
32     Convert2CreateAccountOptions(cOptions, options);
33     int32_t ret = ConvertToJSErrCode(AppAccountManager::CreateAccount(name, options));
34     if (ret != ERR_OK) {
35         ACCOUNT_LOGE("create account failed");
36         return ret;
37     }
38     return ret;
39 }
40 
removeAccount(std::string name)41 int32_t CJAppAccountImpl::removeAccount(std::string name)
42 {
43     int32_t ret = ConvertToJSErrCode(AppAccountManager::DeleteAccount(name));
44     if (ret != ERR_OK) {
45         ACCOUNT_LOGE("remove account failed");
46         return ret;
47     }
48     return ret;
49 }
50 
setAppAccess(std::string name,std::string bundleName,bool isAccessible)51 int32_t CJAppAccountImpl::setAppAccess(std::string name, std::string bundleName, bool isAccessible)
52 {
53     int32_t ret = ConvertToJSErrCode(AppAccountManager::SetAppAccess(name, bundleName, isAccessible));
54     if (ret != ERR_OK) {
55         ACCOUNT_LOGE("setAppAccess failed");
56         return ret;
57     }
58     return ret;
59 }
60 
checkAppAccess(std::string name,std::string bundleName)61 RetDataBool CJAppAccountImpl::checkAppAccess(std::string name, std::string bundleName)
62 {
63     RetDataBool ret = { .code = ERR_CJ_INVALID_INSTANCE_CODE, .data = 0 };
64     bool isAccessible;
65     int32_t err = ConvertToJSErrCode(AppAccountManager::CheckAppAccess(name, bundleName, isAccessible));
66     if (err != ERR_OK) {
67         ACCOUNT_LOGE("checkAppAccess failed");
68         ret.code = err;
69         ret.data = false;
70         return ret;
71     }
72     ret.code = err;
73     ret.data = isAccessible;
74     return ret;
75 }
76 
checkDataSyncEnabled(std::string name)77 RetDataBool CJAppAccountImpl::checkDataSyncEnabled(std::string name)
78 {
79     RetDataBool ret = { .code = ERR_CJ_INVALID_INSTANCE_CODE, .data = 0 };
80     bool result;
81     int32_t err = ConvertToJSErrCode(AppAccountManager::CheckAppAccountSyncEnable(name, result));
82     if (err != ERR_OK) {
83         ACCOUNT_LOGE("checkDataSyncEnabled failed");
84         ret.code = err;
85         ret.data = false;
86         return ret;
87     }
88     ret.code = err;
89     ret.data = result;
90     return ret;
91 }
92 
setCredential(std::string name,std::string credentialType,std::string credential)93 int32_t CJAppAccountImpl::setCredential(std::string name, std::string credentialType, std::string credential)
94 {
95     int32_t ret = ConvertToJSErrCode(AppAccountManager::SetAccountCredential(name, credentialType, credential));
96     if (ret != ERR_OK) {
97         ACCOUNT_LOGE("setCredential failed");
98         return ret;
99     }
100     return ret;
101 }
102 
setDataSyncEnabled(std::string name,bool isEnabled)103 int32_t CJAppAccountImpl::setDataSyncEnabled(std::string name, bool isEnabled)
104 {
105     int32_t ret = ConvertToJSErrCode(AppAccountManager::SetAppAccountSyncEnable(name, isEnabled));
106     if (ret != ERR_OK) {
107         ACCOUNT_LOGE("setDataSyncEnabled failed");
108         return ret;
109     }
110     return ret;
111 }
112 
setCustomData(std::string name,std::string key,std::string value)113 int32_t CJAppAccountImpl::setCustomData(std::string name, std::string key, std::string value)
114 {
115     int32_t ret = ConvertToJSErrCode(AppAccountManager::SetAssociatedData(name, key, value));
116     if (ret != ERR_OK) {
117         ACCOUNT_LOGE("setCustomData failed");
118         return ret;
119     }
120     return ret;
121 }
122 
getAccountsByOwner(std::string owner)123 ErrCArrAppAccountInfo CJAppAccountImpl::getAccountsByOwner(std::string owner)
124 {
125     ErrCArrAppAccountInfo res{};
126     std::vector<AppAccountInfo> appAccounts;
127     int32_t err = ConvertToJSErrCode(AppAccountManager::QueryAllAccessibleAccounts(owner, appAccounts));
128     if (err != ERR_OK) {
129         ACCOUNT_LOGE("getAccountsByOwner failed");
130         res.err = err;
131         return res;
132     }
133     res.err = err;
134     res.cArrAppAccountInfo = Convert2CArrAppAccountInfo(appAccounts);
135     return res;
136 }
137 
getCredential(std::string name,std::string credentialType)138 RetDataCString CJAppAccountImpl::getCredential(std::string name, std::string credentialType)
139 {
140     RetDataCString ret = { .code = ERR_CJ_SUCCESS, .data = nullptr };
141     std::string credential;
142     int32_t err = ConvertToJSErrCode(AppAccountManager::GetAccountCredential(name, credentialType, credential));
143     if (err != ERR_OK) {
144         ACCOUNT_LOGE("getCredential failed");
145         ret.code = err;
146         ret.data = nullptr;
147         return ret;
148     }
149     ret.code = err;
150     ret.data = MallocCString(credential);
151     return ret;
152 }
153 
getCustomData(std::string name,std::string key)154 RetDataCString CJAppAccountImpl::getCustomData(std::string name, std::string key)
155 {
156     RetDataCString ret = { .code = ERR_CJ_SUCCESS, .data = nullptr };
157     std::string value;
158     int32_t err = ConvertToJSErrCode(AppAccountManager::GetAssociatedData(name, key, value));
159     if (err != ERR_OK) {
160         ACCOUNT_LOGE("getCustomDataSync failed");
161         ret.code = err;
162         ret.data = nullptr;
163         return ret;
164     }
165     ret.code = err;
166     ret.data = MallocCString(value);
167     return ret;
168 }
169 
getAuthToken(std::string name,std::string owner,std::string authType)170 RetDataCString CJAppAccountImpl::getAuthToken(std::string name, std::string owner, std::string authType)
171 {
172     RetDataCString ret = { .code = ERR_CJ_SUCCESS, .data = nullptr };
173     std::string token;
174     int32_t err = ConvertToJSErrCode(AppAccountManager::GetAuthToken(name, owner, authType, token));
175     if (err != ERR_OK) {
176         ACCOUNT_LOGE("getAuthToken failed");
177         ret.code = err;
178         ret.data = nullptr;
179         return ret;
180     }
181     ret.code = err;
182     ret.data = MallocCString(token);
183     return ret;
184 }
185 
setAuthToken(std::string name,std::string authType,std::string token)186 int32_t CJAppAccountImpl::setAuthToken(std::string name, std::string authType, std::string token)
187 {
188     int32_t err = ConvertToJSErrCode(AppAccountManager::SetOAuthToken(name, authType, token));
189     if (err != ERR_OK) {
190         ACCOUNT_LOGE("setAuthToken failed");
191         return err;
192     }
193     return err;
194 }
195 
deleteAuthToken(std::string name,std::string owner,std::string authType,std::string token)196 int32_t CJAppAccountImpl::deleteAuthToken(
197     std::string name, std::string owner, std::string authType, std::string token)
198 {
199     int32_t err = ConvertToJSErrCode(AppAccountManager::DeleteAuthToken(name, owner, authType, token));
200     if (err != ERR_OK) {
201         ACCOUNT_LOGE("deleteAuthToken failed");
202         return err;
203     }
204     return err;
205 }
206 
setAuthTokenVisibility(std::string name,std::string authType,std::string bundleName,bool isVisible)207 int32_t CJAppAccountImpl::setAuthTokenVisibility(
208     std::string name, std::string authType, std::string bundleName, bool isVisible)
209 {
210     int32_t err = ConvertToJSErrCode(AppAccountManager::SetAuthTokenVisibility(name, authType, bundleName, isVisible));
211     if (err != ERR_OK) {
212         ACCOUNT_LOGE("setAuthTokenVisibility failed");
213         return err;
214     }
215     return err;
216 }
217 
checkAuthTokenVisibility(std::string name,std::string authType,std::string bundleName)218 RetDataBool CJAppAccountImpl::checkAuthTokenVisibility(std::string name, std::string authType, std::string bundleName)
219 {
220     RetDataBool ret = { .code = ERR_CJ_INVALID_INSTANCE_CODE, .data = 0 };
221     bool isVisible;
222     int32_t err = ConvertToJSErrCode(
223         AppAccountManager::CheckAuthTokenVisibility(name, authType, bundleName, isVisible));
224     if (err != ERR_OK) {
225         ACCOUNT_LOGE("checkAuthTokenVisibility failed");
226         ret.code = err;
227         ret.data = false;
228         return ret;
229     }
230     ret.code = err;
231     ret.data = isVisible;
232     return ret;
233 }
234 
getAllAuthTokens(std::string name,std::string owner)235 ErrCArrAuthTokenInfo CJAppAccountImpl::getAllAuthTokens(std::string name, std::string owner)
236 {
237     ErrCArrAuthTokenInfo res{};
238     std::vector<OAuthTokenInfo> tokenInfos;
239     int32_t err = ConvertToJSErrCode(AppAccountManager::GetAllOAuthTokens(name, owner, tokenInfos));
240     if (err != ERR_OK) {
241         ACCOUNT_LOGE("getAllAuthTokens failed");
242         res.err = err;
243         return res;
244     }
245     res.err = err;
246     res.cArrAuthTokenInfo = Convert2CArrAuthTokenInfo(tokenInfos);
247     return res;
248 }
249 
getAuthList(std::string name,std::string authType)250 RetDataCArrString CJAppAccountImpl::getAuthList(std::string name, std::string authType)
251 {
252     RetDataCArrString res = { .code = ERR_CJ_INVALID_INSTANCE_CODE, .data = {.head = nullptr, .size = 0}};
253     std::set<std::string> authList;
254     int32_t err = ConvertToJSErrCode(AppAccountManager::GetAuthList(name, authType, authList));
255     if (err != ERR_OK) {
256         ACCOUNT_LOGE("getAuthList failed");
257         res.code = err;
258         return res;
259     }
260     res.code = err;
261     res.data = ConvertSet2CArrString(authList);
262     return res;
263 }
264 
queryAuthenticatorInfo(std::string owner)265 ErrCAuthenticatorInfo CJAppAccountImpl::queryAuthenticatorInfo(std::string owner)
266 {
267     ErrCAuthenticatorInfo res{};
268     AuthenticatorInfo info;
269     int32_t err = ConvertToJSErrCode(AppAccountManager::GetAuthenticatorInfo(owner, info));
270     if (err != ERR_OK) {
271         ACCOUNT_LOGE("queryAuthenticatorInfo failed");
272         return {err, {}};
273     }
274     res.err = err;
275     res.cAuthenticatorInfo = Convert2CAuthenticatorInfo(info);
276     return res;
277 }
278 
deleteCredential(std::string name,std::string credentialType)279 int32_t CJAppAccountImpl::deleteCredential(std::string name, std::string credentialType)
280 {
281     int32_t err = ConvertToJSErrCode(AppAccountManager::DeleteAccountCredential(name, credentialType));
282     if (err != ERR_OK) {
283         ACCOUNT_LOGE("deleteCredential failed");
284         return err;
285     }
286     return err;
287 }
288 
getAllAccounts()289 ErrCArrAppAccountInfo CJAppAccountImpl::getAllAccounts()
290 {
291     ErrCArrAppAccountInfo res{};
292     std::vector<AppAccountInfo> appAccounts;
293     int32_t err = ConvertToJSErrCode(AppAccountManager::GetAllAccessibleAccounts(appAccounts));
294     if (err != ERR_OK) {
295         ACCOUNT_LOGE("getAllAccounts failed");
296         res.err = err;
297         return res;
298     }
299     res.err = err;
300     res.cArrAppAccountInfo = Convert2CArrAppAccountInfo(appAccounts);
301     return res;
302 }
303 
IsSameFunction(const std::function<void (CArrAppAccountInfo)> * f1,const std::function<void (CArrAppAccountInfo)> * f2)304 bool CJAppAccountImpl::IsSameFunction(
305     const std::function<void(CArrAppAccountInfo)> *f1, const std::function<void(CArrAppAccountInfo)> *f2)
306 {
307     if (f1 == nullptr || f2 == nullptr) {
308         return false;
309     }
310     return f1 == f2;
311 }
312 
IsExitSubscibe(AsyncContextForSubscribe * context)313 bool CJAppAccountImpl::IsExitSubscibe(AsyncContextForSubscribe *context)
314 {
315     for (size_t idx = 0; idx < g_appAccountSubscribes.size(); ++idx) {
316         if (IsSameFunction(&context->callbackRef, &g_appAccountSubscribes[idx]->callbackRef)) {
317             return true;
318         }
319     }
320     return false;
321 }
322 
on(std::string type,CArrString owners,void (* callback)(CArrAppAccountInfo cArrAppAccountInfo))323 int32_t CJAppAccountImpl::on(
324     std::string type, CArrString owners, void (*callback)(CArrAppAccountInfo cArrAppAccountInfo))
325 {
326     std::vector<std::string> ownersVec = Convert2VecString(owners);
327     auto context = std::make_unique<AsyncContextForSubscribe>();
328     context->type = type;
329     context->owners = ownersVec;
330     context->callbackRef = CJLambda::Create(callback);
331     AppAccountSubscribeInfo subscribeInfo(context->owners);
332     context->subscriber = std::make_shared<SubscribePtr>(subscribeInfo);
333     if (context->subscriber == nullptr) {
334         return ERR_CJ_INVALID_INSTANCE_CODE;
335     }
336     context->subscriber->SetCallbackRef(context->callbackRef);
337     std::lock_guard<std::mutex> lock(mutex_);
338     if (IsExitSubscibe(context.get())) {
339         return ERR_CJ_INVALID_INSTANCE_CODE;
340     }
341     int32_t ret = ConvertToJSErrCode(AppAccountManager::SubscribeAppAccount(context->subscriber));
342     if (ret != ERR_OK) {
343         ACCOUNT_LOGE("accountChange subscribe failed");
344         return ret;
345     }
346     g_appAccountSubscribes.emplace_back(context.get());
347     context.release();
348     return ret;
349 }
350 
GetSubscriberByUnsubscribe(std::vector<std::shared_ptr<SubscribePtr>> & subscribers)351 void CJAppAccountImpl::GetSubscriberByUnsubscribe(std::vector<std::shared_ptr<SubscribePtr>> &subscribers)
352 {
353     std::lock_guard<std::mutex> lock(mutex_);
354     for (auto item : g_appAccountSubscribes) {
355         subscribers.emplace_back(item->subscriber);
356     }
357 }
358 
off(std::string type,void (* callback)(CArrAppAccountInfo cArrAppAccountInfo))359 int32_t CJAppAccountImpl::off(std::string type, void (*callback)(CArrAppAccountInfo cArrAppAccountInfo))
360 {
361     int32_t ret;
362     bool hasFailed = false;
363     int32_t E_ERROR = 0;
364     AsyncContextForUnSubscribe *context = new (std::nothrow) AsyncContextForUnSubscribe();
365     if (context == nullptr) {
366         ACCOUNT_LOGE("asyncContextForOff is null");
367         return ERR_CJ_NO_MEMORY;
368     }
369     context->type = type;
370     context->callbackRef = CJLambda::Create(callback);
371     std::vector<std::shared_ptr<SubscribePtr>> subscribers = {nullptr};
372     GetSubscriberByUnsubscribe(subscribers);
373     context->subscribers = subscribers;
374     if (callback == nullptr) {
375         std::lock_guard<std::mutex> lock(mutex_);
376         for (auto offSubscriber : context->subscribers) {
377             ret = ConvertToJSErrCode(AppAccountManager::UnsubscribeAppAccount(offSubscriber));
378             if (ret != ERR_OK) {
379                 hasFailed = true;
380                 E_ERROR = ret;
381             }
382         }
383         g_appAccountSubscribes.clear();
384     } else {
385         std::lock_guard<std::mutex> lock(mutex_);
386         for (size_t idx = 0; idx < context->subscribers.size(); ++idx) {
387             if (!IsSameFunction(&context->callbackRef, &context->subscribers[idx]->ref_)) {
388                 continue;
389             }
390             ret = ConvertToJSErrCode(
391                 AppAccountManager::UnsubscribeAppAccount(context->subscribers[idx]));
392             if (ret != ERR_OK) {
393                 hasFailed = true;
394                 E_ERROR = ret;
395             }
396             g_appAccountSubscribes.erase(g_appAccountSubscribes.begin() + idx);
397             break;
398         }
399     }
400     return hasFailed ? E_ERROR : ERR_OK;
401 }
402 
ParseContextForCheckAccountLabels(std::string name,std::string owner,CArrString labels,const std::function<void (RetDataBool)> & callbackRef,std::unique_ptr<CheckAccountLabelsContext> & context)403 bool CJAppAccountImpl::ParseContextForCheckAccountLabels(std::string name, std::string owner, CArrString labels,
404     const std::function<void(RetDataBool)> &callbackRef, std::unique_ptr<CheckAccountLabelsContext> &context)
405 {
406     context->name = name;
407     context->owner = owner;
408     if (labels.size == 0) {
409         return false;
410     }
411     context->labels = Convert2VecString(labels);
412     context->callbackRef = callbackRef;
413     return true;
414 }
415 
checkAccountLabels(std::string name,std::string owner,CArrString labels,const std::function<void (RetDataBool)> & callbackRef)416 int32_t CJAppAccountImpl::checkAccountLabels(std::string name, std::string owner, CArrString labels,
417     const std::function<void(RetDataBool)> &callbackRef)
418 {
419     RetDataBool ret = { .code = ERR_CJ_INVALID_INSTANCE_CODE, .data = 0 };
420     auto context = std::make_unique<CheckAccountLabelsContext>();
421     if (!ParseContextForCheckAccountLabels(name, owner, labels, callbackRef, context)) {
422         ret.code = ERR_CJ_PARAMETER_ERROR;
423         callbackRef(ret);
424         return ret.code;
425     }
426     sptr<AuthenticatorAsyncCallback> callback = new (std::nothrow) AuthenticatorAsyncCallback(
427         context->callbackRef, nullptr);
428     if (callback == nullptr) {
429         context->errCode = ERR_CJ_INVALID_INSTANCE_CODE;
430         callbackRef(ret);
431         return ERR_CJ_INVALID_INSTANCE_CODE;
432     }
433     int err =  ConvertToJSErrCode(
434         AppAccountManager::CheckAccountLabels(name, owner, Convert2VecString(labels), callback));
435     if (callback->errCode == ERR_OK) {
436         ret.data = callback->onResultRetBool;
437     } else {
438         ret.code = callback->errCode;
439     }
440     if (err != ERR_OK) {
441         ACCOUNT_LOGE("CheckAccountLabels failed");
442         callbackRef(ret);
443         return err;
444     }
445     callbackRef(ret);
446     return err;
447 }
448 
ParseContextForSelectAccount(CSelectAccountsOptions cOptions,const std::function<void (ErrCArrAppAccountInfo)> & callbackRef,std::unique_ptr<SelectAccountsContext> & context)449 bool CJAppAccountImpl::ParseContextForSelectAccount(CSelectAccountsOptions cOptions,
450     const std::function<void(ErrCArrAppAccountInfo)> &callbackRef,
451     std::unique_ptr<SelectAccountsContext> &context)
452 {
453     SelectAccountsOptions options{};
454     Convert2SelectAccountsOptions(cOptions, options);
455     if (options.allowedAccounts.size() != 0) {
456         options.hasAccounts = true;
457     } else {
458         options.hasAccounts = false;
459     }
460     if (options.allowedOwners.size() != 0) {
461         options.hasOwners = true;
462     } else {
463         options.hasOwners = false;
464     }
465     if (options.requiredLabels.size() != 0) {
466         options.hasLabels = true;
467     } else {
468         options.hasLabels = false;
469     }
470     context->options = options;
471     context->callbackRef = callbackRef;
472     return true;
473 }
474 
selectAccountByOptions(CSelectAccountsOptions cOptions,const std::function<void (ErrCArrAppAccountInfo)> & callbackRef)475 int32_t CJAppAccountImpl::selectAccountByOptions(
476     CSelectAccountsOptions cOptions, const std::function<void(ErrCArrAppAccountInfo)> &callbackRef)
477 {
478     ErrCArrAppAccountInfo ret = {.err = ERR_CJ_INVALID_INSTANCE_CODE, .cArrAppAccountInfo = {
479         .head = nullptr, .size = 0}};
480     auto context = std::make_unique<SelectAccountsContext>();
481     if (!ParseContextForSelectAccount(cOptions, callbackRef, context)) {
482         ret.err = ERR_CJ_PARAMETER_ERROR;
483         callbackRef(ret);
484         return ret.err;
485     }
486     sptr<AuthenticatorAsyncCallback> callback = new (std::nothrow) AuthenticatorAsyncCallback(nullptr,
487         context->callbackRef);
488     if (callback == nullptr) {
489         context->errCode = ERR_CJ_INVALID_INSTANCE_CODE;
490         callbackRef(ret);
491         return ERR_CJ_INVALID_INSTANCE_CODE;
492     }
493     int err = ConvertToJSErrCode(
494         AppAccountManager::SelectAccountsByOptions(context->options, callback));
495     std::vector<std::string> names = callback->onResultRetNames;
496     std::vector<std::string> owners = callback->onResultRetOwners;
497     if (names.size() != owners.size()) {
498         callback->errCode = ERR_CJ_ACCOUNT_AUTHENTICATOR_SERVICE_EXCEPTION;
499     }
500     if (callback->errCode == ERR_OK) {
501         ret.cArrAppAccountInfo = Convert2CArrAppAccountInfo(names, owners);
502     } else {
503         ret.err = callback->errCode;
504     }
505     if (err != ERR_OK) {
506         ACCOUNT_LOGE("selectAccountByOptions failed");
507         callbackRef(ret);
508         return err;
509     }
510     callbackRef(ret);
511     return err;
512 }
513 
ParseContextForVerifyCredential(CAuthCallback callbackId,CVerifyCredentialOptions cOptions,JSAuthCallback & callback,VerifyCredentialOptions & options)514 void CJAppAccountImpl::ParseContextForVerifyCredential(
515     CAuthCallback callbackId,
516     CVerifyCredentialOptions cOptions,
517     JSAuthCallback &callback,
518     VerifyCredentialOptions &options)
519 {
520     options.credential = cOptions.credential;
521     options.credentialType = cOptions.credentialType;
522     if (cOptions.parameters.size != 0) {
523         SetDataParameters(cOptions.parameters, options.parameters);
524     }
525     if (callbackId.onRequestContinued != nullptr) {
526         callback.onRequestContinued = CJLambda::Create(callbackId.onRequestContinued);
527     }
528     callback.onResult = CJLambda::Create(callbackId.onResult);
529     callback.onRequestRedirected = CJLambda::Create(callbackId.onRequestRedirected);
530 }
531 
verifyCredential(std::string name,std::string owner,CAuthCallback callbackId,CVerifyCredentialOptions cOptions)532 int32_t CJAppAccountImpl::verifyCredential(
533     std::string name, std::string owner, CAuthCallback callbackId, CVerifyCredentialOptions cOptions)
534 {
535     VerifyCredentialOptions options;
536     JSAuthCallback callback;
537     ParseContextForVerifyCredential(callbackId, cOptions, callback, options);
538     sptr<AppAccountManagerCallback> appAccountMgrCb = new (std::nothrow) AppAccountManagerCallback(callback);
539     if (appAccountMgrCb == nullptr) {
540         ACCOUNT_LOGD("failed to create AppAccountManagerCallback for insufficient memory");
541         AAFwk::Want result;
542         std::string value = std::string();
543         callback.onResult(ERR_CJ_SYSTEM_SERVICE_EXCEPTION, Convert2CAuthResult(value, value, value, value));
544         return ERR_CJ_SYSTEM_SERVICE_EXCEPTION;
545     }
546     int32_t errCode = ConvertToJSErrCode(
547         AppAccountManager::VerifyCredential(name, owner, options, appAccountMgrCb));
548     if (errCode != ERR_OK) {
549         ACCOUNT_LOGE("verifyCredential failed");
550         AAFwk::Want result;
551         std::string value = std::string();
552         appAccountMgrCb->OnResult(ERR_CJ_SYSTEM_SERVICE_EXCEPTION, result);
553         callback.onResult(ERR_CJ_SYSTEM_SERVICE_EXCEPTION, Convert2CAuthResult(value, value, value, value));
554         return errCode;
555     }
556     // account: AppAccountInfo
557     std::string nameResult = appAccountMgrCb->nameResult;
558     std::string ownerResult = appAccountMgrCb->ownerResult;
559     //tokenInfo: AuthTokenInfo
560     std::string authTypeResult = appAccountMgrCb->authTypeResult;
561     std::string tokenResult = appAccountMgrCb->tokenResult;
562     CAuthResult result = Convert2CAuthResult(nameResult, ownerResult, authTypeResult, tokenResult);
563     callback.onResult(errCode, result);
564     return errCode;
565 }
566 
ParseContextForSetAuthenticatorProperties(CAuthCallback callbackId,CSetPropertiesOptions cOptions,JSAuthCallback & callback,SetPropertiesOptions & options)567 void CJAppAccountImpl::ParseContextForSetAuthenticatorProperties(
568     CAuthCallback callbackId, CSetPropertiesOptions cOptions, JSAuthCallback &callback, SetPropertiesOptions &options)
569 {
570     if (cOptions.properties.size != 0) {
571         SetDataParameters(cOptions.properties, options.properties);
572     }
573     if (cOptions.parameters.size != 0) {
574         SetDataParameters(cOptions.parameters, options.parameters);
575     }
576     if (callbackId.onRequestContinued != nullptr) {
577         callback.onRequestContinued = CJLambda::Create(callbackId.onRequestContinued);
578     }
579     callback.onResult = CJLambda::Create(callbackId.onResult);
580     callback.onRequestRedirected = CJLambda::Create(callbackId.onRequestRedirected);
581 }
582 
setAuthenticatorProperties(std::string owner,CAuthCallback callbackId,CSetPropertiesOptions cOptions)583 int32_t CJAppAccountImpl::setAuthenticatorProperties(
584     std::string owner, CAuthCallback callbackId, CSetPropertiesOptions cOptions)
585 {
586     SetPropertiesOptions options;
587     JSAuthCallback callback;
588     ParseContextForSetAuthenticatorProperties(callbackId, cOptions, callback, options);
589     sptr<AppAccountManagerCallback> appAccountMgrCb = new (std::nothrow) AppAccountManagerCallback(callback);
590     if (appAccountMgrCb == nullptr) {
591         ACCOUNT_LOGD("failed to create AppAccountManagerCallback for insufficient memory");
592         AAFwk::Want result;
593         std::string value = std::string();
594         callback.onResult(ERR_CJ_SYSTEM_SERVICE_EXCEPTION, Convert2CAuthResult(value, value, value, value));
595         return ERR_CJ_SYSTEM_SERVICE_EXCEPTION;
596     }
597     int32_t errCode = ConvertToJSErrCode(
598         AppAccountManager::SetAuthenticatorProperties(owner, options, appAccountMgrCb));
599     if (errCode != ERR_OK) {
600         ACCOUNT_LOGE("setAuthenticatorProperties failed");
601         AAFwk::Want result;
602         std::string value = std::string();
603         appAccountMgrCb->OnResult(ERR_CJ_SYSTEM_SERVICE_EXCEPTION, result);
604         callback.onResult(ERR_CJ_SYSTEM_SERVICE_EXCEPTION, Convert2CAuthResult(value, value, value, value));
605         return errCode;
606     }
607     // account: AppAccountInfo
608     std::string nameResult = appAccountMgrCb->nameResult;
609     std::string ownerResult = appAccountMgrCb->ownerResult;
610     //tokenInfo: AuthTokenInfo
611     std::string authTypeResult = appAccountMgrCb->authTypeResult;
612     std::string tokenResult = appAccountMgrCb->tokenResult;
613     CAuthResult result = Convert2CAuthResult(nameResult, ownerResult, authTypeResult, tokenResult);
614     callback.onResult(errCode, result);
615     return errCode;
616 }
617 
ConvertCArr2Map(const CHashStrStrArr & cHeaders)618 std::map<std::string, std::string> CJAppAccountImpl::ConvertCArr2Map(const CHashStrStrArr &cHeaders)
619 {
620     std::map<std::string, std::string> res;
621     for (int64_t i = 0; i < cHeaders.size; ++i) {
622         const CHashStrStrPair *cHeader = &cHeaders.headers[i];
623         res[cHeader->key] = cHeader->value;
624     }
625     return res;
626 }
627 
Convert2CreateAccountOptions(CCreateAccountOptions & in,CreateAccountOptions & out)628 void CJAppAccountImpl::Convert2CreateAccountOptions(CCreateAccountOptions &in, CreateAccountOptions &out)
629 {
630     out.customData = ConvertCArr2Map(in.customData);
631 }
632 
Convert2CArrAppAccountInfo(const std::vector<AppAccountInfo> & in)633 CArrAppAccountInfo CJAppAccountImpl::Convert2CArrAppAccountInfo(const std::vector<AppAccountInfo> &in)
634 {
635     CArrAppAccountInfo res{};
636     if (in.size() <= 0) {
637         return res;
638     }
639     res.head = static_cast<CAppAccountInfo *>(malloc(sizeof(CAppAccountInfo) * in.size()));
640     if (res.head == nullptr) {
641         return res;
642     }
643     size_t i = 0;
644     for (auto item : in) {
645         std::string owner;
646         item.GetOwner(owner);
647         res.head[i].owner = MallocCString(owner);
648         std::string name;
649         item.GetName(name);
650         res.head[i].name = MallocCString(name);
651         i++;
652     }
653     res.size = i;
654     return res;
655 }
656 
Convert2CArrAppAccountInfo(const std::vector<std::string> & names,const std::vector<std::string> & owners)657 CArrAppAccountInfo CJAppAccountImpl::Convert2CArrAppAccountInfo(
658     const std::vector<std::string> &names, const std::vector<std::string> &owners)
659 {
660     CArrAppAccountInfo res{};
661     if (names.size() <= 0) {
662         return res;
663     }
664     res.head = static_cast<CAppAccountInfo *>(malloc(sizeof(CAppAccountInfo) * names.size()));
665     if (res.head == nullptr) {
666         return res;
667     }
668     size_t i = 0;
669     for (; i < names.size(); ++i) {
670         CAppAccountInfo tmp;
671         tmp.owner = MallocCString(owners[i]);
672         tmp.name = MallocCString(names[i]);
673         res.head[i] = tmp;
674     }
675     res.size = i;
676     return res;
677 }
678 
Convert2CArrAuthTokenInfo(const std::vector<OAuthTokenInfo> & in)679 CArrAuthTokenInfo CJAppAccountImpl::Convert2CArrAuthTokenInfo(const std::vector<OAuthTokenInfo> &in)
680 {
681     CArrAuthTokenInfo res{};
682     if (in.size() <= 0) {
683         return res;
684     }
685     res.head = static_cast<CAuthTokenInfo *>(malloc(sizeof(CAuthTokenInfo) * in.size()));
686     if (res.head == nullptr) {
687         return res;
688     }
689     size_t i = 0;
690     for (; i < in.size(); ++i) {
691         res.head[i].authType = MallocCString(in[i].authType);
692         res.head[i].token = MallocCString(in[i].token);
693         res.head[i].account.owner = MallocCString(std::string());
694         res.head[i].account.name = MallocCString(std::string());
695     }
696     res.size = i;
697     return res;
698 }
699 
700 
Convert2VecString(CArrString & in)701 std::vector<std::string> CJAppAccountImpl::Convert2VecString(CArrString &in)
702 {
703     std::vector<std::string> ret;
704     for (int i = 0; i < in.size; ++i) {
705         ret.emplace_back(std::string(in.head[i]));
706     }
707     return ret;
708 }
709 
clearCharPointer(char ** ptr,int count)710 void CJAppAccountImpl::clearCharPointer(char **ptr, int count)
711 {
712     for (int i = 0; i < count; ++i) {
713         free(ptr[i]);
714     }
715 }
716 
ConvertSet2CArrString(std::set<std::string> & in)717 CArrString CJAppAccountImpl::ConvertSet2CArrString(std::set<std::string> &in)
718 {
719     CArrString arrStr{0};
720     if (in.empty()) {
721         return arrStr;
722     }
723     arrStr.size = static_cast<int64_t>(in.size());
724     char **retValue = static_cast<char**>(malloc(sizeof(char *) * arrStr.size));
725     if (retValue == nullptr) {
726         return arrStr;
727     }
728     size_t i = 0;
729     for (auto idx = in.begin(); idx != in.end(); idx++) {
730         retValue[i] =  MallocCString(*idx);
731         if (retValue[i] == nullptr) {
732             clearCharPointer(retValue, i);
733             free(retValue);
734             return {nullptr, 0};
735         }
736     }
737     arrStr.head = retValue;
738     return arrStr;
739 }
740 
ConvertVec2CArrString(std::vector<std::string> & in)741 CArrString CJAppAccountImpl::ConvertVec2CArrString(std::vector<std::string> &in)
742 {
743     CArrString arrStr{0};
744     if (in.empty()) {
745         return arrStr;
746     }
747     arrStr.size = static_cast<int64_t>(in.size());
748     char **retValue = static_cast<char**>(malloc(sizeof(char *) * arrStr.size));
749     if (retValue == nullptr) {
750         return arrStr;
751     }
752     for (size_t i = 0; i < in.size(); i++) {
753         retValue[i] =  MallocCString(in[i]);
754         if (retValue[i] == nullptr) {
755             clearCharPointer(retValue, i);
756             free(retValue);
757             return {nullptr, 0};
758         }
759     }
760     arrStr.head = retValue;
761     return arrStr;
762 }
763 
Convert2CAuthenticatorInfo(AuthenticatorInfo & in)764 CAuthenticatorInfo CJAppAccountImpl::Convert2CAuthenticatorInfo(AuthenticatorInfo &in)
765 {
766     CAuthenticatorInfo cInfo{};
767     cInfo.owner = MallocCString(in.owner);
768     cInfo.iconId = in.iconId;
769     cInfo.labelId = in.labelId;
770     return cInfo;
771 }
772 
Convert2VecAppAccountInfo(CArrAppAccountInfo & in)773 std::vector<std::pair<std::string, std::string>> CJAppAccountImpl::Convert2VecAppAccountInfo(CArrAppAccountInfo &in)
774 {
775     std::vector<std::pair<std::string, std::string>> ret;
776     for (int i = 0; i < in.size; ++i) {
777         ret.push_back({in.head[i].owner, in.head[i].name});
778     }
779     return ret;
780 }
781 
Convert2SelectAccountsOptions(CSelectAccountsOptions & in,SelectAccountsOptions & out)782 void CJAppAccountImpl::Convert2SelectAccountsOptions(CSelectAccountsOptions &in, SelectAccountsOptions &out)
783 {
784     out.allowedAccounts = Convert2VecAppAccountInfo(in.allowedAccounts);
785     out.allowedOwners = Convert2VecString(in.allowedOwners);
786     out.requiredLabels = Convert2VecString(in.requiredLabels);
787 }
788 
Convert2CAuthResult(std::string name,std::string owner,std::string authType,std::string token)789 CAuthResult CJAppAccountImpl::Convert2CAuthResult(
790     std::string name, std::string owner, std::string authType, std::string token)
791 {
792     bool flag = true;
793     CAuthResult res{};
794     CAppAccountInfo account{};
795     CAuthTokenInfo tokenInfo{};
796     if (name.empty() || owner.empty() || authType.empty() || token.empty()) {
797         flag = false;
798         res.flag = flag;
799         res.account = account;
800         res.tokenInfo = tokenInfo;
801         return res;
802     }
803     account.name = MallocCString(name);
804     account.owner = MallocCString(owner);
805     res.account = account;
806     tokenInfo.authType = MallocCString(authType);
807     tokenInfo.token = MallocCString(token);
808     res.tokenInfo = tokenInfo;
809     res.flag = flag;
810     return res;
811 }
812 } // namespace::OHOS::AccountSA