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