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