1 /*
2  * Copyright (c) 2022-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 /**
17  * @addtogroup DomainAccount
18  * @{
19  *
20  * @brief Provides domain account management.
21  *
22  * Provides the capability to manage domain accounts.
23  *
24  * @since 10.0
25  * @version 10.0
26  */
27 
28 /**
29  * @file domain_account_client.h
30  *
31  * @brief Declares domain account manager interfaces.
32  *
33  * @since 10.0
34  * @version 10.0
35  */
36 #ifndef OS_ACCOUNT_INTERFACES_INNERKITS_DOMAIN_ACCOUNT_INCLUDE_DOMAIN_ACCOUNT_CLIENT_H
37 #define OS_ACCOUNT_INTERFACES_INNERKITS_DOMAIN_ACCOUNT_INCLUDE_DOMAIN_ACCOUNT_CLIENT_H
38 
39 #include <map>
40 #include <mutex>
41 #include <set>
42 #include "account_error_no.h"
43 #include "domain_account_callback.h"
44 #include "domain_account_plugin.h"
45 #include "idomain_account_plugin.h"
46 #include "domain_account_status_listener.h"
47 #include "domain_account_status_listener_manager.h"
48 #include "domain_account_callback_service.h"
49 #include "get_access_token_callback.h"
50 #include "idomain_account.h"
51 #include "want.h"
52 
53 namespace OHOS {
54 namespace AccountSA {
55 class DomainAccountClient {
56 public:
57     /**
58      * Gets the instance of DomainAccountClient.
59      *
60      * @return the instance of DomainAccountClient.
61      */
62     static DomainAccountClient &GetInstance();
63 
64     /**
65      * @brief Registers the domain plugin, which provides the capabilities for domain authentication.
66      * @permission ohos.permission.MANAGE_LOCAL_ACCOUNTS
67      * @param plugin - Indicates the domain plugin.
68      * @return error code, see account_error_no.h
69      */
70     ErrCode RegisterPlugin(const std::shared_ptr<DomainAccountPlugin> &plugin);
71 
72     /**
73      * @brief Unregisters domain plugin.
74      * @permission ohos.permission.MANAGE_LOCAL_ACCOUNTS
75      * @return error code, see account_error_no.h
76      */
77     ErrCode UnregisterPlugin();
78 
79     /**
80      * @brief Authenticates the specified domain account with a credential.
81      * @permission ohos.permission.ACCESS_USER_AUTH_INTERNAL
82      * @param domainAccountInfo - Indicates the domain account information.
83      * @param password - Indicates the credential for authentication.
84      * @param callback - Indicates the callback for getting the authentication result.
85      * @return error code, see account_error_no.h
86      */
87     ErrCode Auth(const DomainAccountInfo &info, const std::vector<uint8_t> &password,
88         const std::shared_ptr<DomainAccountCallback> &callback);
89 
90     /**
91      * @brief Authenticates a domain account bound with the specified userId with a credential.
92      * @permission ohos.permission.ACCESS_USER_AUTH_INTERNAL
93      * @param domainAccountInfo - Indicates the domain account information.
94      * @param password - Indicates the credential for authentication.
95      * @param callback - Indicates the callback for getting the authentication result.
96      * @return error code, see account_error_no.h
97      */
98     ErrCode AuthUser(int32_t userId, const std::vector<uint8_t> &password,
99         const std::shared_ptr<DomainAccountCallback> &callback);
100 
101     /**
102      * @brief Authenticates the domain account bound to the specified OS account with a popup.
103      * @permission ohos.permission.ACCESS_USER_AUTH_INTERNAL
104      * @param localId - Indicates the local ID of the specified OS account.
105      * @param callback - Indicates the callback for getting the authentication result.
106      * @return error code, see account_error_no.h
107      */
108     ErrCode AuthWithPopup(int32_t userId, const std::shared_ptr<DomainAccountCallback> &callback);
109 
110     /**
111      * @brief Checks whether the specified domain account exists.
112      * @permission ohos.permission.MANAGE_LOCAL_ACCOUNTS
113      * @param domainAccountInfo - Indicates the domain account information.
114      * @param callback - Indicates the callback for checking whether the specified domain account exists.
115      * @return error code, see account_error_no.h
116      */
117     ErrCode HasAccount(const DomainAccountInfo &info, const std::shared_ptr<DomainAccountCallback> &callback);
118     ErrCode UpdateAccountToken(const DomainAccountInfo &info, const std::vector<uint8_t> &token);
119     ErrCode IsAuthenticationExpired(const DomainAccountInfo &info, bool &isExpired);
120     ErrCode SetAccountPolicy(const DomainAccountPolicy &policy);
121     ErrCode GetAccessToken(const DomainAccountInfo &info, const AAFwk::WantParams &parameters,
122         const std::shared_ptr<GetAccessTokenCallback> &callback);
123     ErrCode GetAccountStatus(const DomainAccountInfo &info, DomainAccountStatus &status);
124     ErrCode GetDomainAccountInfo(const DomainAccountInfo &info, const std::shared_ptr<DomainAccountCallback> &callback);
125     ErrCode UpdateAccountInfo(const DomainAccountInfo &oldAccountInfo, const DomainAccountInfo &newAccountInfo);
126     ErrCode RegisterAccountStatusListener(const std::shared_ptr<DomainAccountStatusListener> &listener);
127     ErrCode UnregisterAccountStatusListener(const std::shared_ptr<DomainAccountStatusListener> &listener);
128     friend std::function<void(int32_t, const std::string &)> callbackFunc();
129 
130     ErrCode AddServerConfig(const std::string &parameters, DomainServerConfig &config);
131     ErrCode RemoveServerConfig(const std::string &configId);
132     ErrCode GetAccountServerConfig(const DomainAccountInfo &info, DomainServerConfig &config);
133 
134 private:
135     DomainAccountClient();
136     ~DomainAccountClient() = default;
137     void RestoreListenerRecords();
138     void RestorePlugin();
139     DISALLOW_COPY_AND_MOVE(DomainAccountClient);
140 
141 private:
142     class DomainAccountDeathRecipient : public IRemoteObject::DeathRecipient {
143     public:
144         DomainAccountDeathRecipient() = default;
145         ~DomainAccountDeathRecipient() override = default;
146         void OnRemoteDied(const wptr<IRemoteObject>& remote) override;
147 
148     private:
149         DISALLOW_COPY_AND_MOVE(DomainAccountDeathRecipient);
150     };
151     sptr<IDomainAccount> GetDomainAccountProxy();
152     void ResetDomainAccountProxy(const wptr<IRemoteObject> &remote);
153     ErrCode AuthProxyInit(const std::shared_ptr<DomainAccountCallback> &callback,
154         sptr<DomainAccountCallbackService> &callbackService, sptr<IDomainAccount> &proxy);
155 
156 private:
157     std::mutex mutex_;
158     std::mutex recordMutex_;
159     sptr<IDomainAccount> proxy_ = nullptr;
160     sptr<DomainAccountDeathRecipient> deathRecipient_ = nullptr;
161     sptr<IDomainAccountPlugin> pluginService_ = nullptr;
162     sptr<IDomainAccountCallback> callback_ = nullptr;
163     std::shared_ptr<DomainAccountStatusListenerManager> listenerManager_ = nullptr;
164 };
165 }  // namespace AccountSA
166 }  // namespace OHOS
167 #endif  // OS_ACCOUNT_INTERFACES_INNERKITS_DOMAIN_ACCOUNT_INCLUDE_DOMAIN_ACCOUNT_CLIENT_H