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