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 #include "domain_account_proxy.h"
17 
18 #include "account_error_no.h"
19 #include "account_log_wrapper.h"
20 
21 namespace OHOS {
22 namespace AccountSA {
DomainAccountProxy(const sptr<IRemoteObject> & object)23 DomainAccountProxy::DomainAccountProxy(const sptr<IRemoteObject> &object) : IRemoteProxy<IDomainAccount>(object)
24 {}
25 
~DomainAccountProxy()26 DomainAccountProxy::~DomainAccountProxy()
27 {}
28 
SendRequest(DomainAccountInterfaceCode code,MessageParcel & data,MessageParcel & reply)29 ErrCode DomainAccountProxy::SendRequest(DomainAccountInterfaceCode code, MessageParcel &data, MessageParcel &reply)
30 {
31     sptr<IRemoteObject> remote = Remote();
32     if (remote == nullptr) {
33         ACCOUNT_LOGE("remote is nullptr, code = %{public}d", code);
34         return ERR_ACCOUNT_COMMON_NULL_PTR_ERROR;
35     }
36     MessageOption option(MessageOption::TF_SYNC);
37     ErrCode result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
38     if (result != ERR_OK) {
39         ACCOUNT_LOGE("failed to send domain account request, error code: %{public}d.", result);
40         return result;
41     }
42     if (!reply.ReadInt32(result)) {
43         ACCOUNT_LOGE("fail to read result");
44         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
45     }
46     return result;
47 }
48 
HasDomainAccount(const DomainAccountInfo & info,const sptr<IDomainAccountCallback> & callback)49 ErrCode DomainAccountProxy::HasDomainAccount(
50     const DomainAccountInfo &info, const sptr<IDomainAccountCallback> &callback)
51 {
52     MessageParcel data;
53     if (!data.WriteInterfaceToken(GetDescriptor())) {
54         ACCOUNT_LOGE("failed to write descriptor!");
55         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
56     }
57     if (!data.WriteParcelable(&info)) {
58         ACCOUNT_LOGE("fail to write parcelable");
59         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
60     }
61     if ((callback == nullptr) || (!data.WriteRemoteObject(callback->AsObject()))) {
62         ACCOUNT_LOGE("fail to write callback");
63         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
64     }
65     MessageParcel reply;
66     return SendRequest(DomainAccountInterfaceCode::DOMAIN_HAS_DOMAIN_ACCOUNT, data, reply);
67 }
68 
RegisterPlugin(const sptr<IDomainAccountPlugin> & plugin)69 ErrCode DomainAccountProxy::RegisterPlugin(const sptr<IDomainAccountPlugin> &plugin)
70 {
71     MessageParcel data;
72     if (!data.WriteInterfaceToken(GetDescriptor())) {
73         ACCOUNT_LOGE("fail to write descriptor");
74         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
75     }
76     if ((plugin == nullptr) || (!data.WriteRemoteObject(plugin->AsObject()))) {
77         ACCOUNT_LOGE("fail to write plugin");
78         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
79     }
80     MessageParcel reply;
81     return SendRequest(DomainAccountInterfaceCode::REGISTER_PLUGIN, data, reply);
82 }
83 
UnregisterPlugin()84 ErrCode DomainAccountProxy::UnregisterPlugin()
85 {
86     MessageParcel data;
87     if (!data.WriteInterfaceToken(GetDescriptor())) {
88         ACCOUNT_LOGE("fail to write descriptor");
89         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
90     }
91     MessageParcel reply;
92     return SendRequest(DomainAccountInterfaceCode::UNREGISTER_PLUGIN, data, reply);
93 }
94 
GetAccountStatus(const DomainAccountInfo & info,DomainAccountStatus & status)95 ErrCode DomainAccountProxy::GetAccountStatus(const DomainAccountInfo &info, DomainAccountStatus &status)
96 {
97     MessageParcel data;
98     if (!data.WriteInterfaceToken(GetDescriptor())) {
99         ACCOUNT_LOGE("fail to write descriptor");
100         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
101     }
102     if (!data.WriteParcelable(&info)) {
103         ACCOUNT_LOGE("fail to write parcelable");
104         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
105     }
106 
107     MessageParcel reply;
108     ErrCode result = SendRequest(DomainAccountInterfaceCode::DOMAIN_ACCOUNT_STATUS_ENQUIRY, data, reply);
109     if (result != ERR_OK) {
110         ACCOUNT_LOGE("fail to read result");
111         return result;
112     }
113     int replyStatus;
114     if (!reply.ReadInt32(replyStatus)) {
115         ACCOUNT_LOGE("fail to read result");
116         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
117     }
118     status = static_cast<DomainAccountStatus>(replyStatus);
119     return ERR_OK;
120 }
121 
RegisterAccountStatusListener(const sptr<IDomainAccountCallback> & listener)122 ErrCode DomainAccountProxy::RegisterAccountStatusListener(const sptr<IDomainAccountCallback> &listener)
123 {
124     MessageParcel data;
125     if (!data.WriteInterfaceToken(GetDescriptor())) {
126         ACCOUNT_LOGE("failed to write descriptor!");
127         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
128     }
129     if ((listener == nullptr) || (!data.WriteRemoteObject(listener->AsObject()))) {
130         ACCOUNT_LOGE("fail to write callback");
131         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
132     }
133     MessageParcel reply;
134     return SendRequest(DomainAccountInterfaceCode::DOMAIN_ACCOUNT_STATUS_LISTENER_REGISTER, data, reply);
135 }
136 
UnregisterAccountStatusListener(const sptr<IDomainAccountCallback> & listener)137 ErrCode DomainAccountProxy::UnregisterAccountStatusListener(const sptr<IDomainAccountCallback> &listener)
138 {
139     MessageParcel data;
140     if (!data.WriteInterfaceToken(GetDescriptor())) {
141         ACCOUNT_LOGE("failed to write descriptor!");
142         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
143     }
144     if ((listener == nullptr) || (!data.WriteRemoteObject(listener->AsObject()))) {
145         ACCOUNT_LOGE("fail to write callback");
146         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
147     }
148 
149     MessageParcel reply;
150     return SendRequest(DomainAccountInterfaceCode::DOMAIN_ACCOUNT_STATUS_LISTENER_UNREGISTER, data, reply);
151 }
152 
Auth(const DomainAccountInfo & info,const std::vector<uint8_t> & password,const sptr<IDomainAccountCallback> & callback)153 ErrCode DomainAccountProxy::Auth(
154     const DomainAccountInfo &info, const std::vector<uint8_t> &password, const sptr<IDomainAccountCallback> &callback)
155 {
156     MessageParcel data;
157     if (!data.WriteInterfaceToken(GetDescriptor())) {
158         ACCOUNT_LOGE("fail to write descriptor");
159         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
160     }
161     if (!data.WriteString(info.accountName_)) {
162         ACCOUNT_LOGE("fail to write name");
163         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
164     }
165     if (!data.WriteString(info.domain_)) {
166         ACCOUNT_LOGE("fail to write domain");
167         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
168     }
169     if (!data.WriteUInt8Vector(password)) {
170         ACCOUNT_LOGE("fail to write password");
171         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
172     }
173     if (!data.WriteString(info.serverConfigId_)) {
174         ACCOUNT_LOGE("Fail to write serverConfigId");
175         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
176     }
177     if ((callback == nullptr) || (!data.WriteRemoteObject(callback->AsObject()))) {
178         ACCOUNT_LOGE("fail to write callback");
179         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
180     }
181     MessageParcel reply;
182     return SendRequest(DomainAccountInterfaceCode::DOMAIN_AUTH, data, reply);
183 }
184 
AuthUser(int32_t userId,const std::vector<uint8_t> & password,const sptr<IDomainAccountCallback> & callback)185 ErrCode DomainAccountProxy::AuthUser(
186     int32_t userId, const std::vector<uint8_t> &password, const sptr<IDomainAccountCallback> &callback)
187 {
188     MessageParcel data;
189     if (!data.WriteInterfaceToken(GetDescriptor())) {
190         ACCOUNT_LOGE("fail to write descriptor");
191         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
192     }
193     if (!data.WriteInt32(userId)) {
194         ACCOUNT_LOGE("fail to write userId for authUser");
195         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
196     }
197     if (!data.WriteUInt8Vector(password)) {
198         ACCOUNT_LOGE("fail to write password");
199         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
200     }
201     if ((callback == nullptr) || (!data.WriteRemoteObject(callback->AsObject()))) {
202         ACCOUNT_LOGE("fail to write callback");
203         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
204     }
205     MessageParcel reply;
206     return SendRequest(DomainAccountInterfaceCode::DOMAIN_AUTH_USER, data, reply);
207 }
208 
AuthWithPopup(int32_t userId,const sptr<IDomainAccountCallback> & callback)209 ErrCode DomainAccountProxy::AuthWithPopup(int32_t userId, const sptr<IDomainAccountCallback> &callback)
210 {
211     MessageParcel data;
212     if (!data.WriteInterfaceToken(GetDescriptor())) {
213         ACCOUNT_LOGE("fail to write descriptor");
214         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
215     }
216     if (!data.WriteInt32(userId)) {
217         ACCOUNT_LOGE("fail to write userId for authWithPopup");
218         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
219     }
220     if ((callback == nullptr) || (!data.WriteRemoteObject(callback->AsObject()))) {
221         ACCOUNT_LOGE("fail to write callback");
222         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
223     }
224     MessageParcel reply;
225     return SendRequest(DomainAccountInterfaceCode::DOMAIN_AUTH_WITH_POPUP, data, reply);
226 }
227 
UpdateAccountToken(const DomainAccountInfo & info,const std::vector<uint8_t> & token)228 ErrCode DomainAccountProxy::UpdateAccountToken(const DomainAccountInfo &info, const std::vector<uint8_t> &token)
229 {
230     MessageParcel data;
231     if (!data.WriteInterfaceToken(GetDescriptor())) {
232         ACCOUNT_LOGE("fail to write descriptor");
233         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
234     }
235     if (!data.WriteParcelable(&info)) {
236         ACCOUNT_LOGE("fail to write parcelable");
237         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
238     }
239     if (!data.WriteUInt8Vector(token)) {
240         ACCOUNT_LOGE("fail to write token");
241         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
242     }
243     MessageParcel reply;
244     return SendRequest(DomainAccountInterfaceCode::DOMAIN_UPDATE_ACCOUNT_TOKEN, data, reply);
245 }
246 
IsAuthenticationExpired(const DomainAccountInfo & info,bool & isExpired)247 ErrCode DomainAccountProxy::IsAuthenticationExpired(const DomainAccountInfo &info, bool &isExpired)
248 {
249     MessageParcel data;
250     if (!data.WriteInterfaceToken(GetDescriptor())) {
251         ACCOUNT_LOGE("Write descriptor failed.");
252         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
253     }
254     if (!data.WriteParcelable(&info)) {
255         ACCOUNT_LOGE("Write domainAccountInfo failed.");
256         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
257     }
258     MessageParcel reply;
259     ErrCode result = SendRequest(DomainAccountInterfaceCode::DOMAIN_IS_AUTHENTICATION_EXPIRED, data, reply);
260     if (result != ERR_OK) {
261         return result;
262     }
263 
264     if (!reply.ReadBool(isExpired)) {
265         ACCOUNT_LOGE("Read isExpired failed.");
266         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
267     }
268     return ERR_OK;
269 }
270 
SetAccountPolicy(const DomainAccountPolicy & policy)271 ErrCode DomainAccountProxy::SetAccountPolicy(const DomainAccountPolicy &policy)
272 {
273     MessageParcel data;
274     if (!data.WriteInterfaceToken(GetDescriptor())) {
275         ACCOUNT_LOGE("Write descriptor failed.");
276         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
277     }
278 
279     if (!data.WriteInt32(policy.authenicationValidityPeriod)) {
280         ACCOUNT_LOGE("Write threshold failed.");
281         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
282     }
283     MessageParcel reply;
284     return SendRequest(DomainAccountInterfaceCode::DOMAIN_SET_ACCOUNT_POLICY, data, reply);
285 }
286 
GetAccessToken(const DomainAccountInfo & info,const AAFwk::WantParams & parameters,const sptr<IDomainAccountCallback> & callback)287 ErrCode DomainAccountProxy::GetAccessToken(
288     const DomainAccountInfo &info, const AAFwk::WantParams &parameters, const sptr<IDomainAccountCallback> &callback)
289 {
290     MessageParcel data;
291     if (!data.WriteInterfaceToken(GetDescriptor())) {
292         ACCOUNT_LOGE("fail to write descriptor");
293         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
294     }
295     if (!data.WriteParcelable(&info)) {
296         ACCOUNT_LOGE("fail to write info");
297         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
298     }
299     if (!data.WriteParcelable(&parameters)) {
300         ACCOUNT_LOGE("failed to write write parameters");
301         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
302     }
303     if ((callback == nullptr) || (!data.WriteRemoteObject(callback->AsObject()))) {
304         ACCOUNT_LOGE("fail to write callback");
305         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
306     }
307     MessageParcel reply;
308     return SendRequest(DomainAccountInterfaceCode::DOMAIN_GET_ACCESS_TOKEN, data, reply);
309 }
310 
GetDomainAccountInfo(const DomainAccountInfo & info,const sptr<IDomainAccountCallback> & callback)311 ErrCode DomainAccountProxy::GetDomainAccountInfo(
312     const DomainAccountInfo &info, const sptr<IDomainAccountCallback> &callback)
313 {
314     MessageParcel data;
315     if (!data.WriteInterfaceToken(GetDescriptor())) {
316         ACCOUNT_LOGE("fail to write descriptor");
317         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
318     }
319     if (!data.WriteParcelable(&info)) {
320         ACCOUNT_LOGE("fail to write accountInfo");
321         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
322     }
323     if ((callback == nullptr) || (!data.WriteRemoteObject(callback->AsObject()))) {
324         ACCOUNT_LOGE("fail to write callback");
325         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
326     }
327     MessageParcel reply;
328     return SendRequest(DomainAccountInterfaceCode::DOMAIN_GET_ACCOUNT_INFO, data, reply);
329 }
330 
AddServerConfig(const std::string & parameters,DomainServerConfig & config)331 ErrCode DomainAccountProxy::AddServerConfig(const std::string &parameters, DomainServerConfig &config)
332 {
333     MessageParcel data;
334     if (!data.WriteInterfaceToken(GetDescriptor())) {
335         ACCOUNT_LOGE("Fail to write descriptor.");
336         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
337     }
338     if (!data.WriteString(parameters)) {
339         ACCOUNT_LOGE("Fail to write config.");
340         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
341     }
342 
343     MessageParcel reply;
344     ErrCode result = SendRequest(DomainAccountInterfaceCode::ADD_SERVER_CONFIG, data, reply);
345     if (result != ERR_OK) {
346         ACCOUNT_LOGE("Result is error=%{public}d.", result);
347         return result;
348     }
349     std::unique_ptr<DomainServerConfig> serverConfig(reply.ReadParcelable<DomainServerConfig>());
350     if (serverConfig == nullptr) {
351         ACCOUNT_LOGE("ReadParcelable domainServerConfig fail");
352         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
353     }
354     config = *serverConfig;
355     return ERR_OK;
356 }
357 
RemoveServerConfig(const std::string & configId)358 ErrCode DomainAccountProxy::RemoveServerConfig(const std::string &configId)
359 {
360     MessageParcel data;
361     if (!data.WriteInterfaceToken(GetDescriptor())) {
362         ACCOUNT_LOGE("Fail to write descriptor.");
363         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
364     }
365     if (!data.WriteString(configId)) {
366         ACCOUNT_LOGE("Fail to write config.");
367         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
368     }
369 
370     MessageParcel reply;
371     ErrCode result = SendRequest(DomainAccountInterfaceCode::REMOVE_SERVER_CONFIG, data, reply);
372     if (result != ERR_OK) {
373         ACCOUNT_LOGE("Result is error=%{public}d.", result);
374     }
375     return result;
376 }
377 
GetAccountServerConfig(const DomainAccountInfo & info,DomainServerConfig & config)378 ErrCode DomainAccountProxy::GetAccountServerConfig(const DomainAccountInfo &info, DomainServerConfig &config)
379 {
380     MessageParcel data;
381     if (!data.WriteInterfaceToken(GetDescriptor())) {
382         ACCOUNT_LOGE("Fail to write descriptor.");
383         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
384     }
385     if (!data.WriteParcelable(&info)) {
386         ACCOUNT_LOGE("Fail to write info.");
387         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
388     }
389 
390     MessageParcel reply;
391     ErrCode result = SendRequest(DomainAccountInterfaceCode::GET_ACCOUNT_SERVER_CONFIG, data, reply);
392     if (result != ERR_OK) {
393         ACCOUNT_LOGE("Result is error=%{public}d.", result);
394         return result;
395     }
396     std::unique_ptr<DomainServerConfig> serverConfig(reply.ReadParcelable<DomainServerConfig>());
397     if (serverConfig == nullptr) {
398         ACCOUNT_LOGE("ReadParcelable domainServerConfig fail");
399         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
400     }
401     config = *serverConfig;
402     return ERR_OK;
403 }
404 
UpdateAccountInfo(const DomainAccountInfo & oldAccountInfo,const DomainAccountInfo & newAccountInfo)405 ErrCode DomainAccountProxy::UpdateAccountInfo(
406     const DomainAccountInfo &oldAccountInfo, const DomainAccountInfo &newAccountInfo)
407 {
408     MessageParcel data;
409     if (!data.WriteInterfaceToken(GetDescriptor())) {
410         ACCOUNT_LOGE("Fail to write descriptor");
411         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
412     }
413     if (!data.WriteParcelable(&oldAccountInfo)) {
414         ACCOUNT_LOGE("Fail to write oldAccountInfo");
415         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
416     }
417     if (!data.WriteParcelable(&newAccountInfo)) {
418         ACCOUNT_LOGE("Fail to write newAccountInfo");
419         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
420     }
421     MessageParcel reply;
422     return SendRequest(DomainAccountInterfaceCode::DOMAIN_UPDATE_ACCOUNT_INFO, data, reply);
423 }
424 } // namespace AccountSA
425 } // namespace OHOS