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_plugin_proxy.h"
17 
18 #include "account_error_no.h"
19 #include "account_log_wrapper.h"
20 
21 namespace OHOS {
22 namespace AccountSA {
DomainAccountPluginProxy(const sptr<IRemoteObject> & object)23 DomainAccountPluginProxy::DomainAccountPluginProxy(const sptr<IRemoteObject> &object)
24     : IRemoteProxy<IDomainAccountPlugin>(object)
25 {}
26 
~DomainAccountPluginProxy()27 DomainAccountPluginProxy::~DomainAccountPluginProxy()
28 {}
29 
SendRequest(DomainAccountPluginInterfaceCode code,MessageParcel & data,MessageParcel & reply)30 ErrCode DomainAccountPluginProxy::SendRequest(
31     DomainAccountPluginInterfaceCode code, MessageParcel &data, MessageParcel &reply)
32 {
33     sptr<IRemoteObject> remote = Remote();
34     if (remote == nullptr) {
35         ACCOUNT_LOGD("remote is nullptr, code = %{public}d", code);
36         return ERR_ACCOUNT_COMMON_NULL_PTR_ERROR;
37     }
38     MessageOption option(MessageOption::TF_SYNC);
39     ErrCode result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
40     if (result != ERR_OK) {
41         ACCOUNT_LOGE("failed to send request, result: %{public}d", result);
42         return result;
43     }
44     if (!reply.ReadInt32(result)) {
45         ACCOUNT_LOGE("failed to read result");
46         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
47     }
48     return result;
49 }
50 
WriteCommonData(MessageParcel & data,const std::u16string & descriptor,const DomainAccountInfo & info)51 static ErrCode WriteCommonData(MessageParcel &data, const std::u16string &descriptor, const DomainAccountInfo &info)
52 {
53     if (!data.WriteInterfaceToken(descriptor)) {
54         ACCOUNT_LOGE("fail to write descriptor");
55         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
56     }
57     if (!data.WriteParcelable(&info)) {
58         ACCOUNT_LOGE("fail to write name");
59         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
60     }
61     return ERR_OK;
62 }
63 
AuthCommonInterface(const DomainAccountInfo & info,const std::vector<uint8_t> & authData,const sptr<IDomainAccountCallback> & callback,AuthMode authMode)64 ErrCode DomainAccountPluginProxy::AuthCommonInterface(const DomainAccountInfo &info,
65     const std::vector<uint8_t> &authData, const sptr<IDomainAccountCallback> &callback, AuthMode authMode)
66 {
67     MessageParcel data;
68     ErrCode result = WriteCommonData(data, GetDescriptor(), info);
69     if (result != ERR_OK) {
70         return result;
71     }
72     if (!data.WriteUInt8Vector(authData)) {
73         ACCOUNT_LOGE("failed to write authData");
74         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
75     }
76     if ((callback == nullptr) || (!data.WriteRemoteObject(callback->AsObject()))) {
77         ACCOUNT_LOGE("failed to write callback");
78         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
79     }
80     if (!data.WriteInt32(static_cast<int32_t>(authMode))) {
81         ACCOUNT_LOGE("failed to write authMode");
82         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
83     }
84     MessageParcel reply;
85     return SendRequest(DomainAccountPluginInterfaceCode::DOMAIN_PLUGIN_AUTH, data, reply);
86 }
87 
IsAccountTokenValid(const DomainAccountInfo & info,const std::vector<uint8_t> & token,const sptr<IDomainAccountCallback> & callback)88 ErrCode DomainAccountPluginProxy::IsAccountTokenValid(
89     const DomainAccountInfo &info, const std::vector<uint8_t> &token, const sptr<IDomainAccountCallback> &callback)
90 {
91     MessageParcel data;
92     if (!data.WriteInterfaceToken(GetDescriptor())) {
93         ACCOUNT_LOGE("fail to write descriptor");
94         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
95     }
96     if (!data.WriteParcelable(&info)) {
97         ACCOUNT_LOGE("failed to write domain account info");
98         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
99     }
100     if (!data.WriteUInt8Vector(token)) {
101         ACCOUNT_LOGE("failed to write token");
102         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
103     }
104     if ((callback == nullptr) || (!data.WriteRemoteObject(callback->AsObject()))) {
105         ACCOUNT_LOGE("fail to write callback");
106         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
107     }
108     MessageParcel reply;
109     return SendRequest(DomainAccountPluginInterfaceCode::DOMAIN_PLUGIN_IS_ACCOUNT_TOKEN_VALID, data, reply);
110 }
111 
Auth(const DomainAccountInfo & info,const std::vector<uint8_t> & password,const sptr<IDomainAccountCallback> & callback)112 ErrCode DomainAccountPluginProxy::Auth(const DomainAccountInfo &info, const std::vector<uint8_t> &password,
113     const sptr<IDomainAccountCallback> &callback)
114 {
115     return AuthCommonInterface(info, password, callback, AUTH_WITH_CREDENTIAL_MODE);
116 }
117 
GetAccessToken(const DomainAccountInfo & domainInfo,const std::vector<uint8_t> & accountToken,const GetAccessTokenOptions & option,const sptr<IDomainAccountCallback> & callback)118 ErrCode DomainAccountPluginProxy::GetAccessToken(const DomainAccountInfo &domainInfo,
119     const std::vector<uint8_t> &accountToken, const GetAccessTokenOptions &option,
120     const sptr<IDomainAccountCallback> &callback)
121 {
122     MessageParcel data;
123     ErrCode result = WriteCommonData(data, GetDescriptor(), domainInfo);
124     if (result != ERR_OK) {
125         return result;
126     }
127     if (!data.WriteUInt8Vector(accountToken)) {
128         ACCOUNT_LOGE("failed to write accountToken");
129         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
130     }
131     if (!data.WriteParcelable(&option)) {
132         ACCOUNT_LOGE("failed to write option");
133         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
134     }
135     if ((callback == nullptr) || (!data.WriteRemoteObject(callback->AsObject()))) {
136         ACCOUNT_LOGE("fail to write callback");
137         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
138     }
139     MessageParcel reply;
140     return SendRequest(DomainAccountPluginInterfaceCode::DOMAIN_PLUGIN_GET_ACCESS_TOKEN, data, reply);
141 }
142 
AuthWithPopup(const DomainAccountInfo & info,const sptr<IDomainAccountCallback> & callback)143 ErrCode DomainAccountPluginProxy::AuthWithPopup(
144     const DomainAccountInfo &info, const sptr<IDomainAccountCallback> &callback)
145 {
146     return AuthCommonInterface(info, {}, callback, AUTH_WITH_POPUP_MODE);
147 }
148 
AuthWithToken(const DomainAccountInfo & info,const std::vector<uint8_t> & token,const sptr<IDomainAccountCallback> & callback)149 ErrCode DomainAccountPluginProxy::AuthWithToken(const DomainAccountInfo &info, const std::vector<uint8_t> &token,
150     const sptr<IDomainAccountCallback> &callback)
151 {
152     return AuthCommonInterface(info, token, callback, AUTH_WITH_TOKEN_MODE);
153 }
154 
GetAuthStatusInfo(const DomainAccountInfo & info,const sptr<IDomainAccountCallback> & callback)155 ErrCode DomainAccountPluginProxy::GetAuthStatusInfo(
156     const DomainAccountInfo &info, const sptr<IDomainAccountCallback> &callback)
157 {
158     MessageParcel data;
159     ErrCode result = WriteCommonData(data, GetDescriptor(), info);
160     if (result != ERR_OK) {
161         return result;
162     }
163     if ((callback == nullptr) || (!data.WriteRemoteObject(callback->AsObject()))) {
164         ACCOUNT_LOGE("failed to write callback");
165         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
166     }
167     MessageParcel reply;
168     return SendRequest(DomainAccountPluginInterfaceCode::DOMAIN_PLUGIN_GET_AUTH_STATUS_INFO, data, reply);
169 }
170 
GetDomainAccountInfo(const GetDomainAccountInfoOptions & options,const sptr<IDomainAccountCallback> & callback)171 ErrCode DomainAccountPluginProxy::GetDomainAccountInfo(
172     const GetDomainAccountInfoOptions &options, const sptr<IDomainAccountCallback> &callback)
173 {
174     MessageParcel data;
175     if (!data.WriteInterfaceToken(GetDescriptor())) {
176         ACCOUNT_LOGE("fail to write descriptor");
177         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
178     }
179     if (!data.WriteParcelable(&options)) {
180         ACCOUNT_LOGE("failed to write option");
181         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
182     }
183     if ((callback == nullptr) || (!data.WriteRemoteObject(callback->AsObject()))) {
184         ACCOUNT_LOGE("fail to write callback");
185         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
186     }
187     MessageParcel reply;
188     return SendRequest(DomainAccountPluginInterfaceCode::DOMAIN_PLUGIN_GET_DOMAIN_ACCOUNT_INFO, data, reply);
189 }
190 
OnAccountBound(const DomainAccountInfo & info,const int32_t localId,const sptr<IDomainAccountCallback> & callback)191 ErrCode DomainAccountPluginProxy::OnAccountBound(const DomainAccountInfo &info, const int32_t localId,
192     const sptr<IDomainAccountCallback> &callback)
193 {
194     MessageParcel data;
195     ErrCode result = WriteCommonData(data, GetDescriptor(), info);
196     if (result != ERR_OK) {
197         return result;
198     }
199     if (!data.WriteInt32(localId)) {
200         ACCOUNT_LOGE("failed to write localId");
201         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
202     }
203     if ((callback == nullptr) || (!data.WriteRemoteObject(callback->AsObject()))) {
204         ACCOUNT_LOGE("fail to write callback");
205         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
206     }
207     MessageParcel reply;
208     return SendRequest(DomainAccountPluginInterfaceCode::DOMAIN_PLUGIN_ON_ACCOUNT_BOUND, data, reply);
209 }
210 
OnAccountUnBound(const DomainAccountInfo & info,const sptr<IDomainAccountCallback> & callback)211 ErrCode DomainAccountPluginProxy::OnAccountUnBound(const DomainAccountInfo &info,
212     const sptr<IDomainAccountCallback> &callback)
213 {
214     MessageParcel data;
215     ErrCode result = WriteCommonData(data, GetDescriptor(), info);
216     if (result != ERR_OK) {
217         return result;
218     }
219     if ((callback == nullptr) || (!data.WriteRemoteObject(callback->AsObject()))) {
220         ACCOUNT_LOGE("fail to write callback");
221         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
222     }
223     MessageParcel reply;
224     return SendRequest(DomainAccountPluginInterfaceCode::DOMAIN_PLUGIN_ON_ACCOUNT_UNBOUND, data, reply);
225 }
226 }  // namespace AccountSA
227 }  // namespace OHOS
228