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