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 errCode = ERR_OK;
362 AsyncContextForUnSubscribe *context = new (std::nothrow) AsyncContextForUnSubscribe();
363 if (context == nullptr) {
364 ACCOUNT_LOGE("asyncContextForOff is null");
365 return ERR_CJ_NO_MEMORY;
366 }
367 context->type = type;
368 context->callbackRef = CJLambda::Create(callback);
369 std::vector<std::shared_ptr<SubscribePtr>> subscribers = {nullptr};
370 GetSubscriberByUnsubscribe(subscribers);
371 context->subscribers = subscribers;
372 if (callback == nullptr) {
373 std::lock_guard<std::mutex> lock(mutex_);
374 for (auto offSubscriber : context->subscribers) {
375 int32_t ret = ConvertToJSErrCode(AppAccountManager::UnsubscribeAppAccount(offSubscriber));
376 if (ret != ERR_OK) {
377 errCode = ret;
378 }
379 }
380 g_appAccountSubscribes.clear();
381 } else {
382 std::lock_guard<std::mutex> lock(mutex_);
383 for (size_t idx = 0; idx < context->subscribers.size(); ++idx) {
384 if (!IsSameFunction(&context->callbackRef, &context->subscribers[idx]->ref_)) {
385 continue;
386 }
387 int32_t ret = ConvertToJSErrCode(
388 AppAccountManager::UnsubscribeAppAccount(context->subscribers[idx]));
389 if (ret != ERR_OK) {
390 errCode = ret;
391 }
392 g_appAccountSubscribes.erase(g_appAccountSubscribes.begin() + idx);
393 break;
394 }
395 }
396 delete context;
397 return errCode;
398 }
399
ParseContextForCheckAccountLabels(std::string name,std::string owner,CArrString labels,const std::function<void (RetDataBool)> & callbackRef,std::unique_ptr<CheckAccountLabelsContext> & context)400 bool CJAppAccountImpl::ParseContextForCheckAccountLabels(std::string name, std::string owner, CArrString labels,
401 const std::function<void(RetDataBool)> &callbackRef, std::unique_ptr<CheckAccountLabelsContext> &context)
402 {
403 context->name = name;
404 context->owner = owner;
405 if (labels.size == 0) {
406 return false;
407 }
408 context->labels = Convert2VecString(labels);
409 context->callbackRef = callbackRef;
410 return true;
411 }
412
checkAccountLabels(std::string name,std::string owner,CArrString labels,const std::function<void (RetDataBool)> & callbackRef)413 int32_t CJAppAccountImpl::checkAccountLabels(std::string name, std::string owner, CArrString labels,
414 const std::function<void(RetDataBool)> &callbackRef)
415 {
416 RetDataBool ret = { .code = ERR_CJ_INVALID_INSTANCE_CODE, .data = 0 };
417 auto context = std::make_unique<CheckAccountLabelsContext>();
418 if (!ParseContextForCheckAccountLabels(name, owner, labels, callbackRef, context)) {
419 ret.code = ERR_CJ_PARAMETER_ERROR;
420 callbackRef(ret);
421 return ret.code;
422 }
423 sptr<AuthenticatorAsyncCallback> callback = new (std::nothrow) AuthenticatorAsyncCallback(
424 context->callbackRef, nullptr);
425 if (callback == nullptr) {
426 context->errCode = ERR_CJ_INVALID_INSTANCE_CODE;
427 callbackRef(ret);
428 return ERR_CJ_INVALID_INSTANCE_CODE;
429 }
430 int err = ConvertToJSErrCode(
431 AppAccountManager::CheckAccountLabels(name, owner, Convert2VecString(labels), callback));
432 if (callback->errCode == ERR_OK) {
433 ret.data = callback->onResultRetBool;
434 } else {
435 ret.code = callback->errCode;
436 }
437 if (err != ERR_OK) {
438 ACCOUNT_LOGE("CheckAccountLabels failed");
439 callbackRef(ret);
440 return err;
441 }
442 callbackRef(ret);
443 return err;
444 }
445
ParseContextForSelectAccount(CSelectAccountsOptions cOptions,const std::function<void (ErrCArrAppAccountInfo)> & callbackRef,std::unique_ptr<SelectAccountsContext> & context)446 bool CJAppAccountImpl::ParseContextForSelectAccount(CSelectAccountsOptions cOptions,
447 const std::function<void(ErrCArrAppAccountInfo)> &callbackRef,
448 std::unique_ptr<SelectAccountsContext> &context)
449 {
450 SelectAccountsOptions options{};
451 Convert2SelectAccountsOptions(cOptions, options);
452 if (options.allowedAccounts.size() != 0) {
453 options.hasAccounts = true;
454 } else {
455 options.hasAccounts = false;
456 }
457 if (options.allowedOwners.size() != 0) {
458 options.hasOwners = true;
459 } else {
460 options.hasOwners = false;
461 }
462 if (options.requiredLabels.size() != 0) {
463 options.hasLabels = true;
464 } else {
465 options.hasLabels = false;
466 }
467 context->options = options;
468 context->callbackRef = callbackRef;
469 return true;
470 }
471
selectAccountByOptions(CSelectAccountsOptions cOptions,const std::function<void (ErrCArrAppAccountInfo)> & callbackRef)472 int32_t CJAppAccountImpl::selectAccountByOptions(
473 CSelectAccountsOptions cOptions, const std::function<void(ErrCArrAppAccountInfo)> &callbackRef)
474 {
475 ErrCArrAppAccountInfo ret = {.err = ERR_CJ_INVALID_INSTANCE_CODE, .cArrAppAccountInfo = {
476 .head = nullptr, .size = 0}};
477 auto context = std::make_unique<SelectAccountsContext>();
478 if (!ParseContextForSelectAccount(cOptions, callbackRef, context)) {
479 ret.err = ERR_CJ_PARAMETER_ERROR;
480 callbackRef(ret);
481 return ret.err;
482 }
483 sptr<AuthenticatorAsyncCallback> callback = new (std::nothrow) AuthenticatorAsyncCallback(nullptr,
484 context->callbackRef);
485 if (callback == nullptr) {
486 context->errCode = ERR_CJ_INVALID_INSTANCE_CODE;
487 callbackRef(ret);
488 return ERR_CJ_INVALID_INSTANCE_CODE;
489 }
490 int err = ConvertToJSErrCode(
491 AppAccountManager::SelectAccountsByOptions(context->options, callback));
492 std::vector<std::string> names = callback->onResultRetNames;
493 std::vector<std::string> owners = callback->onResultRetOwners;
494 if (names.size() != owners.size()) {
495 callback->errCode = ERR_CJ_ACCOUNT_AUTHENTICATOR_SERVICE_EXCEPTION;
496 }
497 if (callback->errCode == ERR_OK) {
498 ret.cArrAppAccountInfo = Convert2CArrAppAccountInfo(names, owners);
499 } else {
500 ret.err = callback->errCode;
501 }
502 if (err != ERR_OK) {
503 ACCOUNT_LOGE("selectAccountByOptions failed");
504 callbackRef(ret);
505 return err;
506 }
507 callbackRef(ret);
508 return err;
509 }
510
ParseContextForVerifyCredential(CAuthCallback callbackId,CVerifyCredentialOptions cOptions,JSAuthCallback & callback,VerifyCredentialOptions & options)511 void CJAppAccountImpl::ParseContextForVerifyCredential(
512 CAuthCallback callbackId,
513 CVerifyCredentialOptions cOptions,
514 JSAuthCallback &callback,
515 VerifyCredentialOptions &options)
516 {
517 options.credential = cOptions.credential;
518 options.credentialType = cOptions.credentialType;
519 if (cOptions.parameters.size != 0) {
520 SetDataParameters(cOptions.parameters, options.parameters);
521 }
522 if (callbackId.onRequestContinued != nullptr) {
523 callback.onRequestContinued = CJLambda::Create(callbackId.onRequestContinued);
524 }
525 callback.onResult = CJLambda::Create(callbackId.onResult);
526 callback.onRequestRedirected = CJLambda::Create(callbackId.onRequestRedirected);
527 }
528
verifyCredential(std::string name,std::string owner,CAuthCallback callbackId,CVerifyCredentialOptions cOptions)529 int32_t CJAppAccountImpl::verifyCredential(
530 std::string name, std::string owner, CAuthCallback callbackId, CVerifyCredentialOptions cOptions)
531 {
532 VerifyCredentialOptions options;
533 JSAuthCallback callback;
534 ParseContextForVerifyCredential(callbackId, cOptions, callback, options);
535 sptr<AppAccountManagerCallback> appAccountMgrCb = new (std::nothrow) AppAccountManagerCallback(callback);
536 if (appAccountMgrCb == nullptr) {
537 ACCOUNT_LOGE("Failed to create AppAccountManagerCallback for insufficient memory");
538 AAFwk::Want result;
539 std::string value = std::string();
540 callback.onResult(ERR_CJ_SYSTEM_SERVICE_EXCEPTION, Convert2CAuthResult(value, value, value, value));
541 return ERR_CJ_SYSTEM_SERVICE_EXCEPTION;
542 }
543 int32_t errCode = ConvertToJSErrCode(
544 AppAccountManager::VerifyCredential(name, owner, options, appAccountMgrCb));
545 if (errCode != ERR_OK) {
546 ACCOUNT_LOGE("verifyCredential failed");
547 AAFwk::Want result;
548 std::string value = std::string();
549 appAccountMgrCb->OnResult(ERR_CJ_SYSTEM_SERVICE_EXCEPTION, result);
550 callback.onResult(ERR_CJ_SYSTEM_SERVICE_EXCEPTION, Convert2CAuthResult(value, value, value, value));
551 return errCode;
552 }
553 // account: AppAccountInfo
554 std::string nameResult = appAccountMgrCb->nameResult;
555 std::string ownerResult = appAccountMgrCb->ownerResult;
556 //tokenInfo: AuthTokenInfo
557 std::string authTypeResult = appAccountMgrCb->authTypeResult;
558 std::string tokenResult = appAccountMgrCb->tokenResult;
559 CAuthResult result = Convert2CAuthResult(nameResult, ownerResult, authTypeResult, tokenResult);
560 callback.onResult(errCode, result);
561 return errCode;
562 }
563
ParseContextForSetAuthenticatorProperties(CAuthCallback callbackId,CSetPropertiesOptions cOptions,JSAuthCallback & callback,SetPropertiesOptions & options)564 void CJAppAccountImpl::ParseContextForSetAuthenticatorProperties(
565 CAuthCallback callbackId, CSetPropertiesOptions cOptions, JSAuthCallback &callback, SetPropertiesOptions &options)
566 {
567 if (cOptions.properties.size != 0) {
568 SetDataParameters(cOptions.properties, options.properties);
569 }
570 if (cOptions.parameters.size != 0) {
571 SetDataParameters(cOptions.parameters, options.parameters);
572 }
573 if (callbackId.onRequestContinued != nullptr) {
574 callback.onRequestContinued = CJLambda::Create(callbackId.onRequestContinued);
575 }
576 callback.onResult = CJLambda::Create(callbackId.onResult);
577 callback.onRequestRedirected = CJLambda::Create(callbackId.onRequestRedirected);
578 }
579
setAuthenticatorProperties(std::string owner,CAuthCallback callbackId,CSetPropertiesOptions cOptions)580 int32_t CJAppAccountImpl::setAuthenticatorProperties(
581 std::string owner, CAuthCallback callbackId, CSetPropertiesOptions cOptions)
582 {
583 SetPropertiesOptions options;
584 JSAuthCallback callback;
585 ParseContextForSetAuthenticatorProperties(callbackId, cOptions, callback, options);
586 sptr<AppAccountManagerCallback> appAccountMgrCb = new (std::nothrow) AppAccountManagerCallback(callback);
587 if (appAccountMgrCb == nullptr) {
588 ACCOUNT_LOGD("failed to create AppAccountManagerCallback for insufficient memory");
589 AAFwk::Want result;
590 std::string value = std::string();
591 callback.onResult(ERR_CJ_SYSTEM_SERVICE_EXCEPTION, Convert2CAuthResult(value, value, value, value));
592 return ERR_CJ_SYSTEM_SERVICE_EXCEPTION;
593 }
594 int32_t errCode = ConvertToJSErrCode(
595 AppAccountManager::SetAuthenticatorProperties(owner, options, appAccountMgrCb));
596 if (errCode != ERR_OK) {
597 ACCOUNT_LOGE("setAuthenticatorProperties failed");
598 AAFwk::Want result;
599 std::string value = std::string();
600 appAccountMgrCb->OnResult(ERR_CJ_SYSTEM_SERVICE_EXCEPTION, result);
601 callback.onResult(ERR_CJ_SYSTEM_SERVICE_EXCEPTION, Convert2CAuthResult(value, value, value, value));
602 return errCode;
603 }
604 // account: AppAccountInfo
605 std::string nameResult = appAccountMgrCb->nameResult;
606 std::string ownerResult = appAccountMgrCb->ownerResult;
607 //tokenInfo: AuthTokenInfo
608 std::string authTypeResult = appAccountMgrCb->authTypeResult;
609 std::string tokenResult = appAccountMgrCb->tokenResult;
610 CAuthResult result = Convert2CAuthResult(nameResult, ownerResult, authTypeResult, tokenResult);
611 callback.onResult(errCode, result);
612 return errCode;
613 }
614
ConvertCArr2Map(const CHashStrStrArr & cHeaders)615 std::map<std::string, std::string> CJAppAccountImpl::ConvertCArr2Map(const CHashStrStrArr &cHeaders)
616 {
617 std::map<std::string, std::string> res;
618 for (int64_t i = 0; i < cHeaders.size; ++i) {
619 const CHashStrStrPair *cHeader = &cHeaders.headers[i];
620 res[cHeader->key] = cHeader->value;
621 }
622 return res;
623 }
624
Convert2CreateAccountOptions(CCreateAccountOptions & in,CreateAccountOptions & out)625 void CJAppAccountImpl::Convert2CreateAccountOptions(CCreateAccountOptions &in, CreateAccountOptions &out)
626 {
627 out.customData = ConvertCArr2Map(in.customData);
628 }
629
Convert2CArrAppAccountInfo(const std::vector<AppAccountInfo> & in)630 CArrAppAccountInfo CJAppAccountImpl::Convert2CArrAppAccountInfo(const std::vector<AppAccountInfo> &in)
631 {
632 CArrAppAccountInfo res{};
633 if (in.size() <= 0) {
634 return res;
635 }
636 res.head = static_cast<CAppAccountInfo *>(malloc(sizeof(CAppAccountInfo) * in.size()));
637 if (res.head == nullptr) {
638 return res;
639 }
640 size_t i = 0;
641 for (auto item : in) {
642 std::string owner;
643 item.GetOwner(owner);
644 res.head[i].owner = MallocCString(owner);
645 std::string name;
646 item.GetName(name);
647 res.head[i].name = MallocCString(name);
648 i++;
649 }
650 res.size = static_cast<int64_t>(i);
651 return res;
652 }
653
Convert2CArrAppAccountInfo(const std::vector<std::string> & names,const std::vector<std::string> & owners)654 CArrAppAccountInfo CJAppAccountImpl::Convert2CArrAppAccountInfo(
655 const std::vector<std::string> &names, const std::vector<std::string> &owners)
656 {
657 CArrAppAccountInfo res{};
658 if (names.size() <= 0) {
659 return res;
660 }
661 res.head = static_cast<CAppAccountInfo *>(malloc(sizeof(CAppAccountInfo) * names.size()));
662 if (res.head == nullptr) {
663 return res;
664 }
665 size_t i = 0;
666 for (; i < names.size(); ++i) {
667 CAppAccountInfo tmp;
668 tmp.owner = MallocCString(owners[i]);
669 tmp.name = MallocCString(names[i]);
670 res.head[i] = tmp;
671 }
672 res.size = static_cast<int64_t>(i);
673 return res;
674 }
675
Convert2CArrAuthTokenInfo(const std::vector<OAuthTokenInfo> & in)676 CArrAuthTokenInfo CJAppAccountImpl::Convert2CArrAuthTokenInfo(const std::vector<OAuthTokenInfo> &in)
677 {
678 CArrAuthTokenInfo res{};
679 if (in.size() <= 0) {
680 return res;
681 }
682 res.head = static_cast<CAuthTokenInfo *>(malloc(sizeof(CAuthTokenInfo) * in.size()));
683 if (res.head == nullptr) {
684 return res;
685 }
686 size_t i = 0;
687 for (; i < in.size(); ++i) {
688 res.head[i].authType = MallocCString(in[i].authType);
689 res.head[i].token = MallocCString(in[i].token);
690 res.head[i].account.owner = MallocCString(std::string());
691 res.head[i].account.name = MallocCString(std::string());
692 }
693 res.size = static_cast<int64_t>(i);
694 return res;
695 }
696
697
Convert2VecString(CArrString & in)698 std::vector<std::string> CJAppAccountImpl::Convert2VecString(CArrString &in)
699 {
700 std::vector<std::string> ret;
701 for (int i = 0; i < in.size; ++i) {
702 ret.emplace_back(std::string(in.head[i]));
703 }
704 return ret;
705 }
706
clearCharPointer(char ** ptr,int count)707 void CJAppAccountImpl::clearCharPointer(char **ptr, int count)
708 {
709 for (int i = 0; i < count; ++i) {
710 free(ptr[i]);
711 }
712 }
713
ConvertSet2CArrString(std::set<std::string> & in)714 CArrString CJAppAccountImpl::ConvertSet2CArrString(std::set<std::string> &in)
715 {
716 CArrString arrStr{0};
717 if (in.empty()) {
718 return arrStr;
719 }
720 arrStr.size = static_cast<int64_t>(in.size());
721 char **retValue = static_cast<char**>(malloc(sizeof(char *) * arrStr.size));
722 if (retValue == nullptr) {
723 return arrStr;
724 }
725 size_t i = 0;
726 for (auto idx = in.begin(); idx != in.end(); idx++) {
727 retValue[i] = MallocCString(*idx);
728 if (retValue[i] == nullptr) {
729 clearCharPointer(retValue, i);
730 free(retValue);
731 return {nullptr, 0};
732 }
733 }
734 arrStr.head = retValue;
735 return arrStr;
736 }
737
ConvertVec2CArrString(std::vector<std::string> & in)738 CArrString CJAppAccountImpl::ConvertVec2CArrString(std::vector<std::string> &in)
739 {
740 CArrString arrStr{0};
741 if (in.empty()) {
742 return arrStr;
743 }
744 arrStr.size = static_cast<int64_t>(in.size());
745 char **retValue = static_cast<char**>(malloc(sizeof(char *) * arrStr.size));
746 if (retValue == nullptr) {
747 return arrStr;
748 }
749 for (size_t i = 0; i < in.size(); i++) {
750 retValue[i] = MallocCString(in[i]);
751 if (retValue[i] == nullptr) {
752 clearCharPointer(retValue, i);
753 free(retValue);
754 return {nullptr, 0};
755 }
756 }
757 arrStr.head = retValue;
758 return arrStr;
759 }
760
Convert2CAuthenticatorInfo(AuthenticatorInfo & in)761 CAuthenticatorInfo CJAppAccountImpl::Convert2CAuthenticatorInfo(AuthenticatorInfo &in)
762 {
763 CAuthenticatorInfo cInfo{};
764 cInfo.owner = MallocCString(in.owner);
765 cInfo.iconId = static_cast<int32_t>(in.iconId);
766 cInfo.labelId = static_cast<int32_t>(in.labelId);
767 return cInfo;
768 }
769
Convert2VecAppAccountInfo(CArrAppAccountInfo & in)770 std::vector<std::pair<std::string, std::string>> CJAppAccountImpl::Convert2VecAppAccountInfo(CArrAppAccountInfo &in)
771 {
772 std::vector<std::pair<std::string, std::string>> ret;
773 for (int i = 0; i < in.size; ++i) {
774 ret.push_back({in.head[i].owner, in.head[i].name});
775 }
776 return ret;
777 }
778
Convert2SelectAccountsOptions(CSelectAccountsOptions & in,SelectAccountsOptions & out)779 void CJAppAccountImpl::Convert2SelectAccountsOptions(CSelectAccountsOptions &in, SelectAccountsOptions &out)
780 {
781 out.allowedAccounts = Convert2VecAppAccountInfo(in.allowedAccounts);
782 out.allowedOwners = Convert2VecString(in.allowedOwners);
783 out.requiredLabels = Convert2VecString(in.requiredLabels);
784 }
785
Convert2CAuthResult(std::string name,std::string owner,std::string authType,std::string token)786 CAuthResult CJAppAccountImpl::Convert2CAuthResult(
787 std::string name, std::string owner, std::string authType, std::string token)
788 {
789 bool flag = true;
790 CAuthResult res{};
791 CAppAccountInfo account{};
792 CAuthTokenInfo tokenInfo{};
793 if (name.empty() || owner.empty() || authType.empty() || token.empty()) {
794 flag = false;
795 res.flag = flag;
796 res.account = account;
797 res.tokenInfo = tokenInfo;
798 return res;
799 }
800 account.name = MallocCString(name);
801 account.owner = MallocCString(owner);
802 res.account = account;
803 tokenInfo.authType = MallocCString(authType);
804 tokenInfo.token = MallocCString(token);
805 res.tokenInfo = tokenInfo;
806 res.flag = flag;
807 return res;
808 }
809 } // namespace::OHOS::AccountSA