1 /*
2  * Copyright (c) 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 #include "app_account_authenticator_proxy.h"
17 
18 #include "account_error_no.h"
19 #include "account_log_wrapper.h"
20 
21 namespace OHOS {
22 namespace AccountSA {
AppAccountAuthenticatorProxy(const sptr<IRemoteObject> & object)23 AppAccountAuthenticatorProxy::AppAccountAuthenticatorProxy(const sptr<IRemoteObject> &object)
24     : IRemoteProxy<IAppAccountAuthenticator>(object)
25 {}
26 
~AppAccountAuthenticatorProxy()27 AppAccountAuthenticatorProxy::~AppAccountAuthenticatorProxy()
28 {}
29 
AddAccountImplicitly(const std::string & authType,const std::string & callerBundleName,const AAFwk::WantParams & options,const sptr<IRemoteObject> & callback)30 ErrCode AppAccountAuthenticatorProxy::AddAccountImplicitly(const std::string &authType,
31     const std::string &callerBundleName, const AAFwk::WantParams &options, const sptr<IRemoteObject> &callback)
32 {
33     MessageParcel data;
34     MessageParcel reply;
35 
36     if (!data.WriteInterfaceToken(GetDescriptor())) {
37         ACCOUNT_LOGE("failed to write descriptor!");
38         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
39     }
40 
41     if (!data.WriteString(authType)) {
42         ACCOUNT_LOGE("failed to write authType");
43         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
44     }
45     if (!data.WriteString(callerBundleName)) {
46         ACCOUNT_LOGE("failed to write callerBundleName");
47         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
48     }
49     if (!data.WriteParcelable(&options)) {
50         ACCOUNT_LOGE("failed to write options");
51         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
52     }
53     if (!data.WriteRemoteObject(callback)) {
54         ACCOUNT_LOGE("failed to write callback");
55         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
56     }
57     ErrCode result = SendRequest(AppAccountAuthenticatorInterfaceCode::ADD_ACCOUNT_IMPLICITLY, data, reply);
58     if (result != ERR_OK) {
59         return result;
60     }
61     return reply.ReadInt32();
62 }
63 
Authenticate(const std::string & name,const std::string & authType,const std::string & callerBundleName,const AAFwk::WantParams & options,const sptr<IRemoteObject> & callback)64 ErrCode AppAccountAuthenticatorProxy::Authenticate(const std::string &name, const std::string &authType,
65     const std::string &callerBundleName, const AAFwk::WantParams &options, const sptr<IRemoteObject> &callback)
66 {
67     MessageParcel data;
68     MessageParcel reply;
69 
70     if (!data.WriteInterfaceToken(GetDescriptor())) {
71         ACCOUNT_LOGE("failed to write descriptor!");
72         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
73     }
74 
75     if (!data.WriteString(name)) {
76         ACCOUNT_LOGE("failed to write name");
77         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
78     }
79     if (!data.WriteString(authType)) {
80         ACCOUNT_LOGE("failed to write authType for authentication");
81         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
82     }
83     if (!data.WriteString(callerBundleName)) {
84         ACCOUNT_LOGE("failed to write callerBundleName for authentication");
85         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
86     }
87     if (!data.WriteParcelable(&options)) {
88         ACCOUNT_LOGE("failed to write options for authentication");
89         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
90     }
91     if (!data.WriteRemoteObject(callback)) {
92         ACCOUNT_LOGE("failed to write callback for authentication");
93         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
94     }
95     ErrCode result = SendRequest(AppAccountAuthenticatorInterfaceCode::AUTHENTICATE, data, reply);
96     if (result != ERR_OK) {
97         return result;
98     }
99     return reply.ReadInt32();
100 }
101 
CreateAccountImplicitly(const CreateAccountImplicitlyOptions & options,const sptr<IRemoteObject> & callback)102 ErrCode AppAccountAuthenticatorProxy::CreateAccountImplicitly(
103     const CreateAccountImplicitlyOptions &options, const sptr<IRemoteObject> &callback)
104 {
105     MessageParcel data;
106     if (!data.WriteInterfaceToken(GetDescriptor())) {
107         ACCOUNT_LOGE("failed to write descriptor!");
108         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
109     }
110     if (!data.WriteParcelable(&options)) {
111         ACCOUNT_LOGE("failed to write options");
112         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
113     }
114     if (!data.WriteRemoteObject(callback)) {
115         ACCOUNT_LOGE("failed to write callback");
116         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
117     }
118     MessageParcel reply;
119     ErrCode result = SendRequest(AppAccountAuthenticatorInterfaceCode::CREATE_ACCOUNT_IMPLICITLY, data, reply);
120     if (result != ERR_OK) {
121         return result;
122     }
123     return reply.ReadInt32();
124 }
125 
Auth(const std::string & name,const std::string & authType,const AAFwk::WantParams & options,const sptr<IRemoteObject> & callback)126 ErrCode AppAccountAuthenticatorProxy::Auth(const std::string &name, const std::string &authType,
127     const AAFwk::WantParams &options, const sptr<IRemoteObject> &callback)
128 {
129     MessageParcel data;
130     if (!data.WriteInterfaceToken(GetDescriptor())) {
131         ACCOUNT_LOGE("failed to write descriptor!");
132         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
133     }
134     if (!data.WriteString(name)) {
135         ACCOUNT_LOGE("failed to write name");
136         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
137     }
138     if (!data.WriteString(authType)) {
139         ACCOUNT_LOGE("failed to write authType");
140         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
141     }
142     if (!data.WriteParcelable(&options)) {
143         ACCOUNT_LOGE("failed to write options");
144         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
145     }
146     if (!data.WriteRemoteObject(callback)) {
147         ACCOUNT_LOGE("failed to write callback");
148         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
149     }
150     MessageParcel reply;
151     ErrCode result = SendRequest(AppAccountAuthenticatorInterfaceCode::AUTH, data, reply);
152     if (result != ERR_OK) {
153         return result;
154     }
155     return reply.ReadInt32();
156 }
157 
VerifyCredential(const std::string & name,const VerifyCredentialOptions & options,const sptr<IRemoteObject> & callback)158 ErrCode AppAccountAuthenticatorProxy::VerifyCredential(
159     const std::string &name, const VerifyCredentialOptions &options, const sptr<IRemoteObject> &callback)
160 {
161     MessageParcel data;
162     MessageParcel reply;
163 
164     if (!data.WriteInterfaceToken(GetDescriptor())) {
165         ACCOUNT_LOGE("failed to write descriptor!");
166         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
167     }
168     if (!data.WriteString(name)) {
169         ACCOUNT_LOGE("failed to write WriteString name");
170         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
171     }
172     if (!data.WriteParcelable(&options)) {
173         ACCOUNT_LOGE("failed to write WriteParcelable options");
174         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
175     }
176     if (!data.WriteRemoteObject(callback)) {
177         ACCOUNT_LOGE("failed to write WriteString callback");
178         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
179     }
180     ErrCode result = SendRequest(AppAccountAuthenticatorInterfaceCode::VERIFY_CREDENTIAL, data, reply);
181     if (result != ERR_OK) {
182         return result;
183     }
184     return reply.ReadInt32();
185 }
186 
CheckAccountLabels(const std::string & name,const std::vector<std::string> & labels,const sptr<IRemoteObject> & callback)187 ErrCode AppAccountAuthenticatorProxy::CheckAccountLabels(
188     const std::string &name, const std::vector<std::string> &labels, const sptr<IRemoteObject> &callback)
189 {
190     MessageParcel data;
191     MessageParcel reply;
192 
193     if (!data.WriteInterfaceToken(GetDescriptor())) {
194         ACCOUNT_LOGE("failed to write descriptor!");
195         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
196     }
197     if (!data.WriteString(name)) {
198         ACCOUNT_LOGE("failed to write WriteString name");
199         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
200     }
201     if (!data.WriteStringVector(labels)) {
202         ACCOUNT_LOGE("failed to write WriteStringVector labels");
203         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
204     }
205     if (!data.WriteRemoteObject(callback)) {
206         ACCOUNT_LOGE("failed to write WriteRemoteObject callback");
207         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
208     }
209     ErrCode result = SendRequest(AppAccountAuthenticatorInterfaceCode::CHECK_ACCOUNT_LABELS, data, reply);
210     if (result != ERR_OK) {
211         return result;
212     }
213     return reply.ReadInt32();
214 }
215 
SetProperties(const SetPropertiesOptions & options,const sptr<IRemoteObject> & callback)216 ErrCode AppAccountAuthenticatorProxy::SetProperties(
217     const SetPropertiesOptions &options, const sptr<IRemoteObject> &callback)
218 {
219     MessageParcel data;
220     MessageParcel reply;
221 
222     if (!data.WriteInterfaceToken(GetDescriptor())) {
223         ACCOUNT_LOGE("failed to write descriptor!");
224         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
225     }
226     if (!data.WriteParcelable(&options)) {
227         ACCOUNT_LOGE("failed to write WriteParcelable options");
228         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
229     }
230     if (!data.WriteRemoteObject(callback)) {
231         ACCOUNT_LOGE("failed to write WriteString callback");
232         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
233     }
234     ErrCode result = SendRequest(AppAccountAuthenticatorInterfaceCode::SET_PROPERTIES, data, reply);
235     if (result != ERR_OK) {
236         return result;
237     }
238     return reply.ReadInt32();
239 }
240 
IsAccountRemovable(const std::string & name,const sptr<IRemoteObject> & callback)241 ErrCode AppAccountAuthenticatorProxy::IsAccountRemovable(const std::string &name, const sptr<IRemoteObject> &callback)
242 {
243     MessageParcel data;
244     MessageParcel reply;
245 
246     if (!data.WriteInterfaceToken(GetDescriptor())) {
247         ACCOUNT_LOGE("failed to write descriptor!");
248         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
249     }
250     if (!data.WriteString(name)) {
251         ACCOUNT_LOGE("failed to write WriteString name");
252         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
253     }
254     if (!data.WriteRemoteObject(callback)) {
255         ACCOUNT_LOGE("failed to write WriteRemoteObject callback");
256         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
257     }
258     ErrCode result = SendRequest(AppAccountAuthenticatorInterfaceCode::IS_ACCOUNT_REMOVABLE, data, reply);
259     if (result != ERR_OK) {
260         return result;
261     }
262     return reply.ReadInt32();
263 }
264 
SendRequest(AppAccountAuthenticatorInterfaceCode code,MessageParcel & data,MessageParcel & reply)265 ErrCode AppAccountAuthenticatorProxy::SendRequest(
266     AppAccountAuthenticatorInterfaceCode code, MessageParcel &data, MessageParcel &reply)
267 {
268     sptr<IRemoteObject> remoteAuthenticator = Remote();
269     if (remoteAuthenticator == nullptr) {
270         ACCOUNT_LOGE("remote is nullptr, code = %{public}d", code);
271         return ERR_ACCOUNT_COMMON_NULL_PTR_ERROR;
272     }
273     MessageOption option(MessageOption::TF_SYNC);
274     int32_t result = remoteAuthenticator->SendRequest(static_cast<uint32_t>(code), data, reply, option);
275     if (result != ERR_OK) {
276         ACCOUNT_LOGE("failed to SendRequest, code = %{public}d, result = %{public}d", code, result);
277         return ERR_APPACCOUNT_KIT_SEND_REQUEST;
278     }
279     return ERR_OK;
280 }
281 }  // namespace AccountSA
282 }  // namespace OHOS
283 
284