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