• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 #ifndef OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_APPACCOUNT_APP_ACCOUNT_CONTROL_MANAGER_H
17 #define OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_APPACCOUNT_APP_ACCOUNT_CONTROL_MANAGER_H
18 
19 #include "app_mgr_interface.h"
20 #include "app_account_authenticator_manager.h"
21 #include "app_account_data_storage.h"
22 #include "iapp_account_authenticator_callback.h"
23 #include "iremote_object.h"
24 #include "want_params.h"
25 #include "safe_map.h"
26 
27 namespace OHOS {
28 namespace AccountSA {
29 struct AssociatedDataCacheItem {
30     std::map<std::string, std::string> data;
31     std::string name;
32     int32_t freq;
33 };
34 
35 class AppAccountControlManager {
36 public:
37     static AppAccountControlManager &GetInstance();
38     ErrCode AddAccount(const std::string &name, const std::string &extraInfo, const uid_t &uid,
39         const std::string &bundleName, AppAccountInfo &appAccountInfo);
40     ErrCode CreateAccount(const std::string &name, const CreateAccountOptions &options, const uid_t &uid,
41         const std::string &bundleName, AppAccountInfo &appAccountInfo);
42     ErrCode DeleteAccount(
43         const std::string &name, const uid_t &uid, const std::string &bundleName, AppAccountInfo &appAccountInfo);
44 
45     ErrCode GetAccountExtraInfo(const std::string &name, std::string &extraInfo,
46         const uid_t &uid, const std::string &bundleName, const uint32_t &appIndex);
47     ErrCode SetAccountExtraInfo(const std::string &name, const std::string &extraInfo, const uid_t &uid,
48         const std::string &bundleName, AppAccountInfo &appAccountInfo);
49 
50     ErrCode EnableAppAccess(const std::string &name, const std::string &authorizedApp,
51         AppAccountCallingInfo &appAccountCallingInfo, AppAccountInfo &appAccountInfo,
52         const uint32_t apiVersion = Constants::API_VERSION7);
53     ErrCode DisableAppAccess(const std::string &name, const std::string &authorizedApp,
54         AppAccountCallingInfo &appAccountCallingInfo, AppAccountInfo &appAccountInfo,
55         const uint32_t apiVersion = Constants::API_VERSION7);
56     ErrCode CheckAppAccess(const std::string &name, const std::string &authorizedApp, bool &isAccessible,
57         const AppAccountCallingInfo &appAccountCallingInfo);
58 
59     ErrCode CheckAppAccountSyncEnable(const std::string &name, bool &syncEnable,
60         const uid_t &uid, const std::string &bundleName, const uint32_t &appIndex);
61     ErrCode SetAppAccountSyncEnable(const std::string &name, const bool &syncEnable, const uid_t &uid,
62         const std::string &bundleName, AppAccountInfo &appAccountInfo);
63     ErrCode GetAssociatedData(const std::string &name, const std::string &key,
64         std::string &value, const uid_t &uid);
65     ErrCode SetAssociatedData(const std::string &name, const std::string &key, const std::string &value,
66         const AppAccountCallingInfo &appAccountCallingInfo);
67 
68     ErrCode GetAccountCredential(const std::string &name, const std::string &credentialType, std::string &credential,
69         const AppAccountCallingInfo &appAccountCallingInfo);
70     ErrCode SetAccountCredential(const std::string &name, const std::string &credentialType,
71         const std::string &credential, const AppAccountCallingInfo &appAccountCallingInfo);
72     ErrCode DeleteAccountCredential(const std::string &name, const std::string &credentialType,
73         const AppAccountCallingInfo &callingInfo);
74 
75     ErrCode GetOAuthToken(const AuthenticatorSessionRequest &request,
76         std::string &token, const uint32_t apiVersion = Constants::API_VERSION8);
77     ErrCode SetOAuthToken(const AuthenticatorSessionRequest &request);
78     ErrCode DeleteOAuthToken(
79         const AuthenticatorSessionRequest &request, const uint32_t apiVersion = Constants::API_VERSION8);
80     ErrCode SetOAuthTokenVisibility(
81         const AuthenticatorSessionRequest &request, const uint32_t apiVersion = Constants::API_VERSION8);
82     ErrCode CheckOAuthTokenVisibility(const AuthenticatorSessionRequest &request,
83         bool &isVisible, const uint32_t apiVersion = Constants::API_VERSION8);
84     ErrCode GetAllOAuthTokens(const AuthenticatorSessionRequest &request, std::vector<OAuthTokenInfo> &tokenInfos);
85     ErrCode GetOAuthList(const AuthenticatorSessionRequest &request,
86         std::set<std::string> &oauthList, const uint32_t apiVersion = Constants::API_VERSION8);
87 
88     ErrCode GetAllAccounts(const std::string &owner, std::vector<AppAccountInfo> &appAccounts, const uid_t &uid,
89         const std::string &bundleName, const uint32_t &appIndex);
90     ErrCode GetAllAccessibleAccounts(std::vector<AppAccountInfo> &appAccounts,
91         const uid_t &uid, const std::string &bundleName, const uint32_t &appIndex);
92 
93     ErrCode SelectAccountsByOptions(
94         const SelectAccountsOptions &options, const sptr<IAppAccountAuthenticatorCallback> &callback,
95         const uid_t &uid, const std::string &bundleName, const uint32_t &appIndex);
96 
97     ErrCode OnPackageRemoved(const uid_t &uid, const std::string &bundleName, const uint32_t &appIndex);
98     ErrCode OnUserStopping(int32_t userId);
99     ErrCode OnUserRemoved(int32_t userId);
100 
101     ErrCode GetAllAccountsFromDataStorage(const std::string &owner, std::vector<AppAccountInfo> &appAccounts,
102         const std::string &bundleName, const std::shared_ptr<AppAccountDataStorage> &dataStoragePtr);
103     ErrCode GetAllAccessibleAccountsFromDataStorage(std::vector<AppAccountInfo> &appAccounts,
104         const std::string &bundleName, const std::shared_ptr<AppAccountDataStorage> &dataStoragePtr,
105         const uint32_t &appIndex);
106     ErrCode GetAccountInfoFromDataStorage(
107         AppAccountInfo &appAccountInfo, std::shared_ptr<AppAccountDataStorage> &dataStoragePtr);
108 #ifndef SQLITE_DLCLOSE_ENABLE
109     std::shared_ptr<AppAccountDataStorage> GetDataStorage(const uid_t &uid, const bool &autoSync = false,
110         DistributedKv::SecurityLevel securityLevel = DistributedKv::SecurityLevel::S1);
111 #else
112     std::shared_ptr<AppAccountDataStorage> GetDataStorage(const uid_t &uid, const bool &autoSync = false,
113         DbAdapterSecurityLevel securityLevel = DbAdapterSecurityLevel::S1);
114 #endif // SQLITE_DLCLOSE_ENABLE
115     void CloseDataStorage();
116 
117     void AddMigratedAccount(int32_t localId);
118     void MoveData();
119     void SetOsAccountRemoved(int32_t localId, bool isRemoved);
120 
121 private:
122     AppAccountControlManager() = default;
123     ~AppAccountControlManager() = default;
124     DISALLOW_COPY_AND_MOVE(AppAccountControlManager);
125     void PopDataFromAssociatedDataCache();
126     void RemoveAssociatedDataCacheByUid(const uid_t &uid);
127     void RemoveAssociatedDataCacheByAccount(const uid_t &uid, const std::string &name);
128     ErrCode GetAssociatedDataFromStorage(const std::string &name, const std::string &key, std::string &value,
129         const uid_t &uid, const uint32_t &appIndex);
130 #ifndef SQLITE_DLCLOSE_ENABLE
131     std::shared_ptr<AppAccountDataStorage> GetDataStorageByUserId(int32_t userId, const bool &autoSync = false,
132         DistributedKv::SecurityLevel securityLevel = DistributedKv::SecurityLevel::S1);
133 #else
134     std::shared_ptr<AppAccountDataStorage> GetDataStorageByUserId(int32_t userId, const bool &autoSync = false,
135         DbAdapterSecurityLevel securityLevel = DbAdapterSecurityLevel::S1);
136 #endif // SQLITE_DLCLOSE_ENABLE
137     bool NeedSyncDataStorage(const AppAccountInfo &appAccountInfo);
138     ErrCode AddAccountInfoIntoDataStorage(AppAccountInfo &appAccountInfo,
139         const std::shared_ptr<AppAccountDataStorage> &dataStoragePtr, const uid_t &uid);
140     ErrCode SaveAccountInfoIntoDataStorage(AppAccountInfo &appAccountInfo,
141         const std::shared_ptr<AppAccountDataStorage> &dataStoragePtr, const uid_t &uid);
142     ErrCode DeleteAccountInfoFromDataStorage(
143         AppAccountInfo &appAccountInfo, std::shared_ptr<AppAccountDataStorage> &dataStoragePtr, const uid_t &uid);
144 
145     ErrCode SaveAuthorizedAccount(const std::string &authorizedApp, AppAccountInfo &appAccountInfo,
146         const std::shared_ptr<AppAccountDataStorage> &dataStoragePtr, const uid_t &uid);
147     ErrCode RemoveAuthorizedAccount(const std::string &authorizedApp, AppAccountInfo &appAccountInfo,
148         const std::shared_ptr<AppAccountDataStorage> &dataStoragePtr, const uid_t &uid);
149     ErrCode SaveAuthorizedAccountIntoDataStorage(const std::string &authorizedApp, AppAccountInfo &appAccountInfo,
150         const std::shared_ptr<AppAccountDataStorage> &dataStoragePtr);
151     ErrCode RemoveAuthorizedAccountFromDataStorage(const std::string &authorizedApp, AppAccountInfo &appAccountInfo,
152         const std::shared_ptr<AppAccountDataStorage> &dataStoragePtr);
153     bool IsOsAccountRemoved(int32_t localId);
154     ErrCode RemoveAppAccountData(const uid_t &uid, const std::string &bundleName, const uint32_t &appIndex);
155     std::string GetBundleKeySuffix(const uint32_t &appIndex);
156     ErrCode RemoveAppAccountDataFromDataStorage(const std::shared_ptr<AppAccountDataStorage> &dataStoragePtr,
157         const std::string &key, const uint32_t &appIndex,
158         const std::shared_ptr<AppAccountDataStorage> &dataStorageSyncPtr);
159 
160 private:
161     std::mutex mutex_;
162     std::mutex storePtrMutex_;
163     std::mutex associatedDataMutex_;
164     std::map<uid_t, AssociatedDataCacheItem> associatedDataCache_;
165     std::map<std::string, std::shared_ptr<AppAccountDataStorage>> storePtrMap_;
166     std::mutex migratedAccountMutex_;
167     std::set<int32_t> migratedAccounts_;
168     SafeMap<int32_t, bool> removedOsAccounts_;
169     std::size_t ACCOUNT_MAX_SIZE = 1000;
170     std::size_t ASSOCIATED_DATA_CACHE_MAX_SIZE = 5;
171 };
172 }  // namespace AccountSA
173 }  // namespace OHOS
174 
175 #endif  // OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_APPACCOUNT_APP_ACCOUNT_CONTROL_MANAGER_H
176