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