• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.h"
17 
18 #include "account_error_no.h"
19 #include "account_log_wrapper.h"
20 #include "account_proxy.h"
21 #include "app_account_common.h"
22 #include "app_account_constants.h"
23 #include "app_account_death_recipient.h"
24 #include "ohos_account_kits_impl.h"
25 
26 namespace OHOS {
27 namespace AccountSA {
28 #define RETURN_IF_STRING_CONTAINS_SPECIAL_CHAR(str)         \
29     if (CheckSpecialCharacters(str) != ERR_OK) {            \
30         ACCOUNT_LOGE("failed to check special characters"); \
31         return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;        \
32     }                                                       \
33 
34 #define RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(str, maxSize, msg)                                                \
35     if ((str).empty() || ((str).size() > (maxSize))) {                                                            \
36         ACCOUNT_LOGE("%{public}s, input size: %{public}zu, max size: %{public}zu", msg, (str).size(), maxSize); \
37         return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;                                                            \
38     }
39 
40 #define RETURN_IF_STRING_IS_OVERSIZE(str, maxSize, msg)                                                         \
41     if ((str).size() > (maxSize)) {                                                                             \
42         ACCOUNT_LOGE("%{public}s, input size: %{public}zu, max size: %{public}zu", msg, (str).size(), maxSize); \
43         return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;                                                            \
44     }                                                                                                           \
45 
GetInstance()46 AppAccount &AppAccount::GetInstance()
47 {
48     static AppAccount *instance = new (std::nothrow) AppAccount();
49     return *instance;
50 }
51 
AddAccount(const std::string & name,const std::string & extraInfo)52 ErrCode AppAccount::AddAccount(const std::string &name, const std::string &extraInfo)
53 {
54     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize");
55     RETURN_IF_STRING_CONTAINS_SPECIAL_CHAR(name);
56     RETURN_IF_STRING_IS_OVERSIZE(extraInfo, Constants::EXTRA_INFO_MAX_SIZE, "extraInfo is empty or oversize");
57     auto proxy = GetAppAccountProxy();
58     if (proxy == nullptr) {
59         return ERR_ACCOUNT_COMMON_GET_PROXY;
60     }
61     return proxy->AddAccount(name, extraInfo);
62 }
63 
AddAccountImplicitly(const std::string & owner,const std::string & authType,const AAFwk::Want & options,const sptr<IAppAccountAuthenticatorCallback> & callback)64 ErrCode AppAccount::AddAccountImplicitly(const std::string &owner, const std::string &authType,
65     const AAFwk::Want &options, const sptr<IAppAccountAuthenticatorCallback> &callback)
66 {
67     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE, "owner is empty or oversize");
68     RETURN_IF_STRING_IS_OVERSIZE(authType, Constants::AUTH_TYPE_MAX_SIZE, "authType is oversize");
69     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(options.GetStringParam(Constants::KEY_CALLER_ABILITY_NAME),
70         Constants::ABILITY_NAME_MAX_SIZE, "abilityName is empty or oversize");
71     auto proxy = GetAppAccountProxy();
72     if (proxy == nullptr) {
73         return ERR_ACCOUNT_COMMON_GET_PROXY;
74     }
75     return proxy->AddAccountImplicitly(owner, authType, options, callback);
76 }
77 
CreateAccount(const std::string & name,const CreateAccountOptions & options)78 ErrCode AppAccount::CreateAccount(const std::string &name, const CreateAccountOptions &options)
79 {
80     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize");
81     auto proxy = GetAppAccountProxy();
82     if (proxy == nullptr) {
83         return ERR_ACCOUNT_COMMON_GET_PROXY;
84     }
85     if (options.customData.size() > Constants::MAX_CUSTOM_DATA_SIZE) {
86         return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
87     }
88     for (auto it : options.customData) {
89         RETURN_IF_STRING_IS_OVERSIZE(it.first, Constants::ASSOCIATED_KEY_MAX_SIZE, "customData key is oversize");
90         RETURN_IF_STRING_IS_OVERSIZE(it.second, Constants::ASSOCIATED_VALUE_MAX_SIZE, "customData value is oversize");
91     }
92     return proxy->CreateAccount(name, options);
93 }
94 
CreateAccountImplicitly(const std::string & owner,const CreateAccountImplicitlyOptions & options,const sptr<IAppAccountAuthenticatorCallback> & callback)95 ErrCode AppAccount::CreateAccountImplicitly(const std::string &owner, const CreateAccountImplicitlyOptions &options,
96     const sptr<IAppAccountAuthenticatorCallback> &callback)
97 {
98     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE, "owner is empty or oversize");
99     RETURN_IF_STRING_IS_OVERSIZE(options.authType, Constants::AUTH_TYPE_MAX_SIZE, "authType is empty or oversize");
100     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(options.parameters.GetStringParam(Constants::KEY_CALLER_ABILITY_NAME),
101         Constants::ABILITY_NAME_MAX_SIZE, "abilityName is empty or oversize");
102     RETURN_IF_STRING_IS_OVERSIZE(
103         options.requiredLabels, Constants::MAX_ALLOWED_ARRAY_SIZE_INPUT, "requiredLabels array is oversize");
104     auto proxy = GetAppAccountProxy();
105     if (proxy == nullptr) {
106         return ERR_ACCOUNT_COMMON_GET_PROXY;
107     }
108     return proxy->CreateAccountImplicitly(owner, options, callback);
109 }
110 
DeleteAccount(const std::string & name)111 ErrCode AppAccount::DeleteAccount(const std::string &name)
112 {
113     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize");
114     auto proxy = GetAppAccountProxy();
115     if (proxy == nullptr) {
116         return ERR_ACCOUNT_COMMON_GET_PROXY;
117     }
118     return proxy->DeleteAccount(name);
119 }
120 
GetAccountExtraInfo(const std::string & name,std::string & extraInfo)121 ErrCode AppAccount::GetAccountExtraInfo(const std::string &name, std::string &extraInfo)
122 {
123     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize");
124     RETURN_IF_STRING_CONTAINS_SPECIAL_CHAR(name);
125     RETURN_IF_STRING_IS_OVERSIZE(extraInfo, Constants::EXTRA_INFO_MAX_SIZE, "extraInfo is empty or oversize");
126     auto proxy = GetAppAccountProxy();
127     if (proxy == nullptr) {
128         return ERR_ACCOUNT_COMMON_GET_PROXY;
129     }
130     return proxy->GetAccountExtraInfo(name, extraInfo);
131 }
132 
SetAccountExtraInfo(const std::string & name,const std::string & extraInfo)133 ErrCode AppAccount::SetAccountExtraInfo(const std::string &name, const std::string &extraInfo)
134 {
135     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize");
136     RETURN_IF_STRING_CONTAINS_SPECIAL_CHAR(name);
137     RETURN_IF_STRING_IS_OVERSIZE(extraInfo, Constants::EXTRA_INFO_MAX_SIZE, "extraInfo is empty or oversize");
138     auto proxy = GetAppAccountProxy();
139     if (proxy == nullptr) {
140         return ERR_ACCOUNT_COMMON_GET_PROXY;
141     }
142     return proxy->SetAccountExtraInfo(name, extraInfo);
143 }
144 
EnableAppAccess(const std::string & name,const std::string & bundleName)145 ErrCode AppAccount::EnableAppAccess(const std::string &name, const std::string &bundleName)
146 {
147     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize");
148     RETURN_IF_STRING_CONTAINS_SPECIAL_CHAR(name);
149     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(bundleName, Constants::BUNDLE_NAME_MAX_SIZE,
150         "bundleName is empty or oversize");
151     auto proxy = GetAppAccountProxy();
152     if (proxy == nullptr) {
153         return ERR_ACCOUNT_COMMON_GET_PROXY;
154     }
155     return proxy->EnableAppAccess(name, bundleName);
156 }
157 
DisableAppAccess(const std::string & name,const std::string & bundleName)158 ErrCode AppAccount::DisableAppAccess(const std::string &name, const std::string &bundleName)
159 {
160     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize");
161     RETURN_IF_STRING_CONTAINS_SPECIAL_CHAR(name);
162     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(bundleName, Constants::BUNDLE_NAME_MAX_SIZE,
163         "bundleName is empty or oversize");
164     auto proxy = GetAppAccountProxy();
165     if (proxy == nullptr) {
166         return ERR_ACCOUNT_COMMON_GET_PROXY;
167     }
168     return proxy->DisableAppAccess(name, bundleName);
169 }
170 
SetAppAccess(const std::string & name,const std::string & authorizedApp,bool isAccessible)171 ErrCode AppAccount::SetAppAccess(const std::string &name, const std::string &authorizedApp, bool isAccessible)
172 {
173     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize");
174     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(authorizedApp, Constants::BUNDLE_NAME_MAX_SIZE,
175         "authorizedApp name is empty or oversize");
176     auto proxy = GetAppAccountProxy();
177     if (proxy == nullptr) {
178         return ERR_ACCOUNT_COMMON_GET_PROXY;
179     }
180     return proxy->SetAppAccess(name, authorizedApp, isAccessible);
181 }
182 
CheckAppAccountSyncEnable(const std::string & name,bool & syncEnable)183 ErrCode AppAccount::CheckAppAccountSyncEnable(const std::string &name, bool &syncEnable)
184 {
185     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize");
186     auto proxy = GetAppAccountProxy();
187     if (proxy == nullptr) {
188         return ERR_ACCOUNT_COMMON_GET_PROXY;
189     }
190     return proxy->CheckAppAccountSyncEnable(name, syncEnable);
191 }
192 
SetAppAccountSyncEnable(const std::string & name,const bool & syncEnable)193 ErrCode AppAccount::SetAppAccountSyncEnable(const std::string &name, const bool &syncEnable)
194 {
195     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize");
196     auto proxy = GetAppAccountProxy();
197     if (proxy == nullptr) {
198         return ERR_ACCOUNT_COMMON_GET_PROXY;
199     }
200     return proxy->SetAppAccountSyncEnable(name, syncEnable);
201 }
202 
GetAssociatedData(const std::string & name,const std::string & key,std::string & value)203 ErrCode AppAccount::GetAssociatedData(const std::string &name, const std::string &key, std::string &value)
204 {
205     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize");
206     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(key, Constants::ASSOCIATED_KEY_MAX_SIZE, "key is empty or oversize");
207     auto proxy = GetAppAccountProxy();
208     if (proxy == nullptr) {
209         return ERR_ACCOUNT_COMMON_GET_PROXY;
210     }
211     return proxy->GetAssociatedData(name, key, value);
212 }
213 
SetAssociatedData(const std::string & name,const std::string & key,const std::string & value)214 ErrCode AppAccount::SetAssociatedData(const std::string &name, const std::string &key, const std::string &value)
215 {
216     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize");
217     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(key, Constants::ASSOCIATED_KEY_MAX_SIZE, "key is empty or oversize");
218     RETURN_IF_STRING_IS_OVERSIZE(value, Constants::ASSOCIATED_VALUE_MAX_SIZE, "value is oversize");
219     auto proxy = GetAppAccountProxy();
220     if (proxy == nullptr) {
221         return ERR_ACCOUNT_COMMON_GET_PROXY;
222     }
223     return proxy->SetAssociatedData(name, key, value);
224 }
225 
GetAccountCredential(const std::string & name,const std::string & credentialType,std::string & credential)226 ErrCode AppAccount::GetAccountCredential(
227     const std::string &name, const std::string &credentialType, std::string &credential)
228 {
229     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize");
230     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(credentialType, Constants::CREDENTIAL_TYPE_MAX_SIZE,
231         "credentialType is empty or oversize");
232     auto proxy = GetAppAccountProxy();
233     if (proxy == nullptr) {
234         return ERR_ACCOUNT_COMMON_GET_PROXY;
235     }
236     return proxy->GetAccountCredential(name, credentialType, credential);
237 }
238 
SetAccountCredential(const std::string & name,const std::string & credentialType,const std::string & credential)239 ErrCode AppAccount::SetAccountCredential(
240     const std::string &name, const std::string &credentialType, const std::string &credential)
241 {
242     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize");
243     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(credentialType, Constants::CREDENTIAL_TYPE_MAX_SIZE,
244         "credentialType is empty or oversize");
245     RETURN_IF_STRING_IS_OVERSIZE(credential, Constants::CREDENTIAL_MAX_SIZE, "credential is empty or oversize");
246     auto proxy = GetAppAccountProxy();
247     if (proxy == nullptr) {
248         return ERR_ACCOUNT_COMMON_GET_PROXY;
249     }
250     return proxy->SetAccountCredential(name, credentialType, credential);
251 }
252 
Authenticate(const std::string & name,const std::string & owner,const std::string & authType,const AAFwk::Want & options,const sptr<IAppAccountAuthenticatorCallback> & callback)253 ErrCode AppAccount::Authenticate(const std::string &name, const std::string &owner, const std::string &authType,
254     const AAFwk::Want &options, const sptr<IAppAccountAuthenticatorCallback> &callback)
255 {
256     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize");
257     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE, "owner is empty or oversize");
258     RETURN_IF_STRING_IS_OVERSIZE(authType, Constants::AUTH_TYPE_MAX_SIZE, "authType is oversize");
259     RETURN_IF_STRING_IS_OVERSIZE(options.GetStringParam(Constants::KEY_CALLER_ABILITY_NAME),
260         Constants::ABILITY_NAME_MAX_SIZE, "abilityName is empty or oversize");
261     auto proxy = GetAppAccountProxy();
262     if (proxy == nullptr) {
263         return ERR_ACCOUNT_COMMON_GET_PROXY;
264     }
265     return proxy->Authenticate(name, owner, authType, options, callback);
266 }
267 
GetOAuthToken(const std::string & name,const std::string & owner,const std::string & authType,std::string & token)268 ErrCode AppAccount::GetOAuthToken(
269     const std::string &name, const std::string &owner, const std::string &authType, std::string &token)
270 {
271     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize");
272     RETURN_IF_STRING_CONTAINS_SPECIAL_CHAR(name);
273     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE, "owner is empty or oversize");
274     RETURN_IF_STRING_IS_OVERSIZE(authType, Constants::AUTH_TYPE_MAX_SIZE, "authType is oversize");
275     auto proxy = GetAppAccountProxy();
276     if (proxy == nullptr) {
277         return ERR_ACCOUNT_COMMON_GET_PROXY;
278     }
279     return proxy->GetOAuthToken(name, owner, authType, token);
280 }
281 
GetAuthToken(const std::string & name,const std::string & owner,const std::string & authType,std::string & token)282 ErrCode AppAccount::GetAuthToken(
283     const std::string &name, const std::string &owner, const std::string &authType, std::string &token)
284 {
285     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize");
286     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE, "owner is empty or oversize");
287     RETURN_IF_STRING_IS_OVERSIZE(authType, Constants::AUTH_TYPE_MAX_SIZE, "authType is oversize");
288     auto proxy = GetAppAccountProxy();
289     if (proxy == nullptr) {
290         return ERR_ACCOUNT_COMMON_GET_PROXY;
291     }
292     return proxy->GetAuthToken(name, owner, authType, token);
293 }
294 
SetOAuthToken(const std::string & name,const std::string & authType,const std::string & token)295 ErrCode AppAccount::SetOAuthToken(const std::string &name, const std::string &authType, const std::string &token)
296 {
297     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize");
298     RETURN_IF_STRING_IS_OVERSIZE(authType, Constants::AUTH_TYPE_MAX_SIZE, "authType is oversize");
299     RETURN_IF_STRING_IS_OVERSIZE(token, Constants::TOKEN_MAX_SIZE, "token is oversize");
300     auto proxy = GetAppAccountProxy();
301     if (proxy == nullptr) {
302         return ERR_ACCOUNT_COMMON_GET_PROXY;
303     }
304     return proxy->SetOAuthToken(name, authType, token);
305 }
306 
DeleteOAuthToken(const std::string & name,const std::string & owner,const std::string & authType,const std::string & token)307 ErrCode AppAccount::DeleteOAuthToken(
308     const std::string &name, const std::string &owner, const std::string &authType, const std::string &token)
309 {
310     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize");
311     RETURN_IF_STRING_CONTAINS_SPECIAL_CHAR(name);
312     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE, "owner is empty or oversize");
313     RETURN_IF_STRING_IS_OVERSIZE(authType, Constants::AUTH_TYPE_MAX_SIZE, "authType is oversize");
314     RETURN_IF_STRING_IS_OVERSIZE(token, Constants::TOKEN_MAX_SIZE, "token is oversize");
315     auto proxy = GetAppAccountProxy();
316     if (proxy == nullptr) {
317         return ERR_ACCOUNT_COMMON_GET_PROXY;
318     }
319     return proxy->DeleteOAuthToken(name, owner, authType, token);
320 }
321 
DeleteAuthToken(const std::string & name,const std::string & owner,const std::string & authType,const std::string & token)322 ErrCode AppAccount::DeleteAuthToken(
323     const std::string &name, const std::string &owner, const std::string &authType, const std::string &token)
324 {
325     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize");
326     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE, "owner is empty or oversize");
327     RETURN_IF_STRING_IS_OVERSIZE(authType, Constants::AUTH_TYPE_MAX_SIZE, "authType is oversize");
328     RETURN_IF_STRING_IS_OVERSIZE(token, Constants::TOKEN_MAX_SIZE, "token is oversize");
329     auto proxy = GetAppAccountProxy();
330     if (proxy == nullptr) {
331         return ERR_ACCOUNT_COMMON_GET_PROXY;
332     }
333     return proxy->DeleteAuthToken(name, owner, authType, token);
334 }
335 
CheckTokenVisibilityParam(const std::string & name,const std::string & authType,const std::string & bundleName)336 ErrCode AppAccount::CheckTokenVisibilityParam(
337     const std::string &name, const std::string &authType, const std::string &bundleName)
338 {
339     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize");
340     RETURN_IF_STRING_IS_OVERSIZE(authType, Constants::AUTH_TYPE_MAX_SIZE, "authType is oversize");
341     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(bundleName, Constants::BUNDLE_NAME_MAX_SIZE,
342         "bundleName is empty or oversize");
343     return ERR_OK;
344 }
345 
SetAuthTokenVisibility(const std::string & name,const std::string & authType,const std::string & bundleName,bool isVisible)346 ErrCode AppAccount::SetAuthTokenVisibility(
347     const std::string &name, const std::string &authType, const std::string &bundleName, bool isVisible)
348 {
349     ErrCode ret = CheckTokenVisibilityParam(name, authType, bundleName);
350     if (ret != ERR_OK) {
351         return ret;
352     }
353     auto proxy = GetAppAccountProxy();
354     if (proxy == nullptr) {
355         return ERR_ACCOUNT_COMMON_GET_PROXY;
356     }
357     return proxy->SetAuthTokenVisibility(name, authType, bundleName, isVisible);
358 }
359 
SetOAuthTokenVisibility(const std::string & name,const std::string & authType,const std::string & bundleName,bool isVisible)360 ErrCode AppAccount::SetOAuthTokenVisibility(
361     const std::string &name, const std::string &authType, const std::string &bundleName, bool isVisible)
362 {
363     RETURN_IF_STRING_CONTAINS_SPECIAL_CHAR(name);
364     ErrCode ret = CheckTokenVisibilityParam(name, authType, bundleName);
365     if (ret != ERR_OK) {
366         return ret;
367     }
368     auto proxy = GetAppAccountProxy();
369     if (proxy == nullptr) {
370         return ERR_ACCOUNT_COMMON_GET_PROXY;
371     }
372     return proxy->SetOAuthTokenVisibility(name, authType, bundleName, isVisible);
373 }
374 
CheckAuthTokenVisibility(const std::string & name,const std::string & authType,const std::string & bundleName,bool & isVisible)375 ErrCode AppAccount::CheckAuthTokenVisibility(
376     const std::string &name, const std::string &authType, const std::string &bundleName, bool &isVisible)
377 {
378     ErrCode ret = CheckTokenVisibilityParam(name, authType, bundleName);
379     if (ret != ERR_OK) {
380         return ret;
381     }
382     auto proxy = GetAppAccountProxy();
383     if (proxy == nullptr) {
384         return ERR_ACCOUNT_COMMON_GET_PROXY;
385     }
386     return proxy->CheckAuthTokenVisibility(name, authType, bundleName, isVisible);
387 }
388 
CheckOAuthTokenVisibility(const std::string & name,const std::string & authType,const std::string & bundleName,bool & isVisible)389 ErrCode AppAccount::CheckOAuthTokenVisibility(
390     const std::string &name, const std::string &authType, const std::string &bundleName, bool &isVisible)
391 {
392     RETURN_IF_STRING_CONTAINS_SPECIAL_CHAR(name);
393     ErrCode ret = CheckTokenVisibilityParam(name, authType, bundleName);
394     if (ret != ERR_OK) {
395         return ret;
396     }
397     auto proxy = GetAppAccountProxy();
398     if (proxy == nullptr) {
399         return ERR_ACCOUNT_COMMON_GET_PROXY;
400     }
401     return proxy->CheckOAuthTokenVisibility(name, authType, bundleName, isVisible);
402 }
403 
GetAuthenticatorInfo(const std::string & owner,AuthenticatorInfo & info)404 ErrCode AppAccount::GetAuthenticatorInfo(const std::string &owner, AuthenticatorInfo &info)
405 {
406     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE, "owner is empty or oversize");
407     auto proxy = GetAppAccountProxy();
408     if (proxy == nullptr) {
409         return ERR_ACCOUNT_COMMON_GET_PROXY;
410     }
411     return proxy->GetAuthenticatorInfo(owner, info);
412 }
413 
GetAllOAuthTokens(const std::string & name,const std::string & owner,std::vector<OAuthTokenInfo> & tokenInfos)414 ErrCode AppAccount::GetAllOAuthTokens(
415     const std::string &name, const std::string &owner, std::vector<OAuthTokenInfo> &tokenInfos)
416 {
417     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize");
418     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE, "owner is empty or oversize");
419     auto proxy = GetAppAccountProxy();
420     if (proxy == nullptr) {
421         return ERR_ACCOUNT_COMMON_GET_PROXY;
422     }
423     return proxy->GetAllOAuthTokens(name, owner, tokenInfos);
424 }
425 
GetOAuthList(const std::string & name,const std::string & authType,std::set<std::string> & oauthList)426 ErrCode AppAccount::GetOAuthList(
427     const std::string &name, const std::string &authType, std::set<std::string> &oauthList)
428 {
429     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize");
430     RETURN_IF_STRING_CONTAINS_SPECIAL_CHAR(name);
431     RETURN_IF_STRING_IS_OVERSIZE(authType, Constants::AUTH_TYPE_MAX_SIZE, "authType is oversize");
432     auto proxy = GetAppAccountProxy();
433     if (proxy == nullptr) {
434         return ERR_ACCOUNT_COMMON_GET_PROXY;
435     }
436     return proxy->GetOAuthList(name, authType, oauthList);
437 }
438 
GetAuthList(const std::string & name,const std::string & authType,std::set<std::string> & oauthList)439 ErrCode AppAccount::GetAuthList(
440     const std::string &name, const std::string &authType, std::set<std::string> &oauthList)
441 {
442     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize");
443     RETURN_IF_STRING_IS_OVERSIZE(authType, Constants::AUTH_TYPE_MAX_SIZE, "authType is oversize");
444     auto proxy = GetAppAccountProxy();
445     if (proxy == nullptr) {
446         return ERR_ACCOUNT_COMMON_GET_PROXY;
447     }
448     return proxy->GetAuthList(name, authType, oauthList);
449 }
450 
GetAuthenticatorCallback(const std::string & sessionId,sptr<IRemoteObject> & callback)451 ErrCode AppAccount::GetAuthenticatorCallback(const std::string &sessionId, sptr<IRemoteObject> &callback)
452 {
453     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(sessionId, Constants::SESSION_ID_MAX_SIZE,
454         "session id is empty or oversize");
455     auto proxy = GetAppAccountProxy();
456     if (proxy == nullptr) {
457         return ERR_ACCOUNT_COMMON_GET_PROXY;
458     }
459     return proxy->GetAuthenticatorCallback(sessionId, callback);
460 }
461 
GetAllAccounts(const std::string & owner,std::vector<AppAccountInfo> & appAccounts)462 ErrCode AppAccount::GetAllAccounts(const std::string &owner, std::vector<AppAccountInfo> &appAccounts)
463 {
464     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE, "owner is empty or oversize");
465     auto proxy = GetAppAccountProxy();
466     if (proxy == nullptr) {
467         return ERR_ACCOUNT_COMMON_GET_PROXY;
468     }
469     return proxy->GetAllAccounts(owner, appAccounts);
470 }
471 
CheckAppAccess(const std::string & name,const std::string & authorizedApp,bool & isAccessible)472 ErrCode AppAccount::CheckAppAccess(const std::string &name, const std::string &authorizedApp, bool &isAccessible)
473 {
474     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize");
475     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(authorizedApp, Constants::BUNDLE_NAME_MAX_SIZE,
476         "authorizedApp is empty or oversize");
477     auto proxy = GetAppAccountProxy();
478     if (proxy == nullptr) {
479         return ERR_ACCOUNT_COMMON_GET_PROXY;
480     }
481     return proxy->CheckAppAccess(name, authorizedApp, isAccessible);
482 }
483 
DeleteAccountCredential(const std::string & name,const std::string & credentialType)484 ErrCode AppAccount::DeleteAccountCredential(const std::string &name, const std::string &credentialType)
485 {
486     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize");
487     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(credentialType, Constants::CREDENTIAL_TYPE_MAX_SIZE,
488         "credential type is empty or oversize");
489     auto proxy = GetAppAccountProxy();
490     if (proxy == nullptr) {
491         return ERR_ACCOUNT_COMMON_GET_PROXY;
492     }
493     return proxy->DeleteAccountCredential(name, credentialType);
494 }
495 
SelectAccountsByOptions(const SelectAccountsOptions & options,const sptr<IAppAccountAuthenticatorCallback> & callback)496 ErrCode AppAccount::SelectAccountsByOptions(
497     const SelectAccountsOptions &options, const sptr<IAppAccountAuthenticatorCallback> &callback)
498 {
499     RETURN_IF_STRING_IS_OVERSIZE(
500         options.allowedAccounts, Constants::MAX_ALLOWED_ARRAY_SIZE_INPUT, "allowedAccounts array is oversize");
501     RETURN_IF_STRING_IS_OVERSIZE(
502         options.allowedOwners, Constants::MAX_ALLOWED_ARRAY_SIZE_INPUT, "allowedOwners array is oversize");
503     RETURN_IF_STRING_IS_OVERSIZE(
504         options.requiredLabels, Constants::MAX_ALLOWED_ARRAY_SIZE_INPUT, "requiredLabels array is oversize");
505     auto proxy = GetAppAccountProxy();
506     if (proxy == nullptr) {
507         return ERR_ACCOUNT_COMMON_GET_PROXY;
508     }
509     return proxy->SelectAccountsByOptions(options, callback);
510 }
511 
VerifyCredential(const std::string & name,const std::string & owner,const VerifyCredentialOptions & options,const sptr<IAppAccountAuthenticatorCallback> & callback)512 ErrCode AppAccount::VerifyCredential(const std::string &name, const std::string &owner,
513     const VerifyCredentialOptions &options, const sptr<IAppAccountAuthenticatorCallback> &callback)
514 {
515     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize");
516     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE, "owner is empty or oversize");
517     RETURN_IF_STRING_IS_OVERSIZE(
518         options.credentialType, Constants::CREDENTIAL_TYPE_MAX_SIZE, "the credential type is oversize");
519     RETURN_IF_STRING_IS_OVERSIZE(options.credential, Constants::CREDENTIAL_MAX_SIZE, "the credential is oversize");
520     auto proxy = GetAppAccountProxy();
521     if (proxy == nullptr) {
522         return ERR_ACCOUNT_COMMON_GET_PROXY;
523     }
524     return proxy->VerifyCredential(name, owner, options, callback);
525 }
526 
CheckAccountLabels(const std::string & name,const std::string & owner,const std::vector<std::string> & labels,const sptr<IAppAccountAuthenticatorCallback> & callback)527 ErrCode AppAccount::CheckAccountLabels(const std::string &name, const std::string &owner,
528     const std::vector<std::string> &labels, const sptr<IAppAccountAuthenticatorCallback> &callback)
529 {
530     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(name, Constants::NAME_MAX_SIZE, "name is empty or oversize");
531     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE, "owner is empty or oversize");
532     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(
533         labels, Constants::MAX_ALLOWED_ARRAY_SIZE_INPUT, "labels array is empty or oversize");
534     auto proxy = GetAppAccountProxy();
535     if (proxy == nullptr) {
536         return ERR_ACCOUNT_COMMON_GET_PROXY;
537     }
538     return proxy->CheckAccountLabels(name, owner, labels, callback);
539 }
540 
SetAuthenticatorProperties(const std::string & owner,const SetPropertiesOptions & options,const sptr<IAppAccountAuthenticatorCallback> & callback)541 ErrCode AppAccount::SetAuthenticatorProperties(const std::string &owner,
542     const SetPropertiesOptions &options, const sptr<IAppAccountAuthenticatorCallback> &callback)
543 {
544     RETURN_IF_STRING_IS_EMPTY_OR_OVERSIZE(owner, Constants::OWNER_MAX_SIZE, "owner is empty or oversize");
545     auto proxy = GetAppAccountProxy();
546     if (proxy == nullptr) {
547         return ERR_ACCOUNT_COMMON_GET_PROXY;
548     }
549     return proxy->SetAuthenticatorProperties(owner, options, callback);
550 }
551 
GetAllAccessibleAccounts(std::vector<AppAccountInfo> & appAccounts)552 ErrCode AppAccount::GetAllAccessibleAccounts(std::vector<AppAccountInfo> &appAccounts)
553 {
554     auto proxy = GetAppAccountProxy();
555     if (proxy == nullptr) {
556         return ERR_ACCOUNT_COMMON_GET_PROXY;
557     }
558     return proxy->GetAllAccessibleAccounts(appAccounts);
559 }
560 
QueryAllAccessibleAccounts(const std::string & owner,std::vector<AppAccountInfo> & appAccounts)561 ErrCode AppAccount::QueryAllAccessibleAccounts(
562     const std::string &owner, std::vector<AppAccountInfo> &appAccounts)
563 {
564     auto proxy = GetAppAccountProxy();
565     if (proxy == nullptr) {
566         return ERR_ACCOUNT_COMMON_GET_PROXY;
567     }
568     RETURN_IF_STRING_IS_OVERSIZE(owner, Constants::OWNER_MAX_SIZE, "owner is oversize");
569     return proxy->QueryAllAccessibleAccounts(owner, appAccounts);
570 }
571 
SubscribeAppAccount(const std::shared_ptr<AppAccountSubscriber> & subscriber)572 ErrCode AppAccount::SubscribeAppAccount(const std::shared_ptr<AppAccountSubscriber> &subscriber)
573 {
574     auto proxy = GetAppAccountProxy();
575     if (proxy == nullptr) {
576         return ERR_ACCOUNT_COMMON_GET_PROXY;
577     }
578 
579     if (subscriber == nullptr) {
580         ACCOUNT_LOGE("subscriber is nullptr");
581         return ERR_APPACCOUNT_KIT_SUBSCRIBER_IS_NULLPTR;
582     }
583 
584     AppAccountSubscribeInfo subscribeInfo;
585     if (subscriber->GetSubscribeInfo(subscribeInfo) != ERR_OK) {
586         ACCOUNT_LOGE("get subscribeInfo failed");
587         return ERR_APPACCOUNT_KIT_GET_SUBSCRIBE_INFO;
588     }
589 
590     std::vector<std::string> owners;
591     if (subscribeInfo.GetOwners(owners) != ERR_OK) {
592         ACCOUNT_LOGE("failed to get owners");
593         return ERR_APPACCOUNT_KIT_GET_OWNERS;
594     }
595 
596     if (owners.size() == 0) {
597         return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
598     }
599 
600     // remove duplicate ones
601     std::sort(owners.begin(), owners.end());
602     owners.erase(std::unique(owners.begin(), owners.end()), owners.end());
603     if (subscribeInfo.SetOwners(owners) != ERR_OK) {
604         ACCOUNT_LOGE("failed to set owners");
605         return ERR_APPACCOUNT_KIT_SET_OWNERS;
606     }
607 
608     for (auto owner : owners) {
609         if (owner.size() > Constants::OWNER_MAX_SIZE) {
610             ACCOUNT_LOGE("owner is out of range, owner.size() = %{public}zu", owner.size());
611             return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
612         }
613     }
614 
615     sptr<IRemoteObject> appAccountEventListener = nullptr;
616     ErrCode subscribeState = CreateAppAccountEventListener(subscriber, appAccountEventListener);
617     if (subscribeState == INITIAL_SUBSCRIPTION) {
618         subscribeState = proxy->SubscribeAppAccount(subscribeInfo, appAccountEventListener);
619         if (subscribeState != ERR_OK) {
620             std::lock_guard<std::mutex> lock(eventListenersMutex_);
621             eventListeners_.erase(subscriber);
622         }
623         return subscribeState;
624     } else if (subscribeState == ALREADY_SUBSCRIBED) {
625         return ERR_APPACCOUNT_SUBSCRIBER_ALREADY_REGISTERED;
626     } else {
627         return ERR_APPACCOUNT_KIT_SUBSCRIBE;
628     }
629 }
630 
UnsubscribeAppAccount(const std::shared_ptr<AppAccountSubscriber> & subscriber)631 ErrCode AppAccount::UnsubscribeAppAccount(const std::shared_ptr<AppAccountSubscriber> &subscriber)
632 {
633     auto proxy = GetAppAccountProxy();
634     if (proxy == nullptr) {
635         return ERR_ACCOUNT_COMMON_GET_PROXY;
636     }
637     if (subscriber == nullptr) {
638         ACCOUNT_LOGE("subscriber is nullptr");
639         return ERR_APPACCOUNT_KIT_SUBSCRIBER_IS_NULLPTR;
640     }
641 
642     std::lock_guard<std::mutex> lock(eventListenersMutex_);
643 
644     auto eventListener = eventListeners_.find(subscriber);
645     if (eventListener != eventListeners_.end()) {
646         ErrCode result = proxy->UnsubscribeAppAccount(eventListener->second->AsObject());
647         if (result == ERR_OK) {
648             eventListener->second->Stop();
649             eventListeners_.erase(eventListener);
650         }
651 
652         return result;
653     } else {
654         ACCOUNT_LOGE("no specified subscriber has been registered");
655         return ERR_APPACCOUNT_KIT_NO_SPECIFIED_SUBSCRIBER_HAS_BEEN_REGISTERED;
656     }
657 }
658 
ResetAppAccountProxy()659 ErrCode AppAccount::ResetAppAccountProxy()
660 {
661     std::lock_guard<std::mutex> lock(mutex_);
662     if ((proxy_ != nullptr) && (proxy_->AsObject() != nullptr)) {
663         proxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
664     }
665     proxy_ = nullptr;
666     return ERR_OK;
667 }
668 
CheckSpecialCharacters(const std::string & name)669 ErrCode AppAccount::CheckSpecialCharacters(const std::string &name)
670 {
671     for (auto specialCharacter : Constants::SPECIAL_CHARACTERS) {
672         std::size_t found = name.find(specialCharacter);
673         if (found != std::string::npos) {
674             ACCOUNT_LOGE("found a special character, specialCharacter = %{public}c", specialCharacter);
675             return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
676         }
677     }
678 
679     return ERR_OK;
680 }
681 
GetAppAccountProxy()682 sptr<IAppAccount> AppAccount::GetAppAccountProxy()
683 {
684     std::lock_guard<std::mutex> lock(mutex_);
685     if (proxy_ != nullptr) {
686         return proxy_;
687     }
688     sptr<IRemoteObject> object = OhosAccountKitsImpl::GetInstance().GetAppAccountService();
689     if (object == nullptr) {
690         ACCOUNT_LOGE("failed to get app account service");
691         return nullptr;
692     }
693     deathRecipient_ = new (std::nothrow) AppAccountDeathRecipient();
694     if (deathRecipient_ == nullptr) {
695         ACCOUNT_LOGE("failed to create app account death recipient");
696         return nullptr;
697     }
698     if (!object->AddDeathRecipient(deathRecipient_)) {
699         ACCOUNT_LOGE("failed to add app account death recipient");
700         deathRecipient_ = nullptr;
701         return nullptr;
702     }
703     proxy_ = iface_cast<IAppAccount>(object);
704     return proxy_;
705 }
706 
CreateAppAccountEventListener(const std::shared_ptr<AppAccountSubscriber> & subscriber,sptr<IRemoteObject> & appAccountEventListener)707 ErrCode AppAccount::CreateAppAccountEventListener(
708     const std::shared_ptr<AppAccountSubscriber> &subscriber, sptr<IRemoteObject> &appAccountEventListener)
709 {
710     if (subscriber == nullptr) {
711         ACCOUNT_LOGE("subscriber is nullptr");
712         return SUBSCRIBE_FAILED;
713     }
714 
715     std::lock_guard<std::mutex> lock(eventListenersMutex_);
716 
717     auto eventListener = eventListeners_.find(subscriber);
718     if (eventListener != eventListeners_.end()) {
719         appAccountEventListener = eventListener->second->AsObject();
720         ACCOUNT_LOGI("subscriber already has app account event listener");
721         return ALREADY_SUBSCRIBED;
722     } else {
723         if (eventListeners_.size() == Constants::APP_ACCOUNT_SUBSCRIBER_MAX_SIZE) {
724             ACCOUNT_LOGE("the maximum number of subscribers has been reached");
725             return SUBSCRIBE_FAILED;
726         }
727 
728         sptr<AppAccountEventListener> listener = new (std::nothrow) AppAccountEventListener(subscriber);
729         if (!listener) {
730             ACCOUNT_LOGE("the app account event listener is null");
731             return SUBSCRIBE_FAILED;
732         }
733         appAccountEventListener = listener->AsObject();
734         eventListeners_[subscriber] = listener;
735     }
736 
737     return INITIAL_SUBSCRIPTION;
738 }
739 }  // namespace AccountSA
740 }  // namespace OHOS
741