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_stub.h"
17 
18 #include <securec.h>
19 #include "account_error_no.h"
20 #include "account_log_wrapper.h"
21 #include "ipc_skeleton.h"
22 #include "want.h"
23 
24 namespace OHOS {
25 namespace AccountSA {
26 
DomainAccountPluginStub()27 DomainAccountPluginStub::DomainAccountPluginStub()
28 {}
29 
~DomainAccountPluginStub()30 DomainAccountPluginStub::~DomainAccountPluginStub()
31 {}
32 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)33 int DomainAccountPluginStub::OnRemoteRequest(
34     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
35 {
36     ACCOUNT_LOGD("Received stub message: %{public}d, callingUid: %{public}d", code, IPCSkeleton::GetCallingUid());
37     if (data.ReadInterfaceToken() != GetDescriptor()) {
38         ACCOUNT_LOGE("check descriptor failed! code %{public}u.", code);
39         return ERR_ACCOUNT_COMMON_CHECK_DESCRIPTOR_ERROR;
40     }
41     switch (static_cast<DomainAccountPluginInterfaceCode>(code)) {
42         case DomainAccountPluginInterfaceCode::DOMAIN_PLUGIN_AUTH:
43             return ProcAuthCommonInterface(data, reply);
44         case DomainAccountPluginInterfaceCode::DOMAIN_PLUGIN_GET_AUTH_STATUS_INFO:
45             return ProcGetAuthStatusInfo(data, reply);
46         case DomainAccountPluginInterfaceCode::DOMAIN_PLUGIN_GET_DOMAIN_ACCOUNT_INFO:
47             return ProcGetDomainAccountInfo(data, reply);
48         case DomainAccountPluginInterfaceCode::DOMAIN_PLUGIN_ON_ACCOUNT_BOUND:
49             return ProcOnAccountBound(data, reply);
50         case DomainAccountPluginInterfaceCode::DOMAIN_PLUGIN_ON_ACCOUNT_UNBOUND:
51             return ProcOnAccountUnBound(data, reply);
52         case DomainAccountPluginInterfaceCode::DOMAIN_PLUGIN_IS_ACCOUNT_TOKEN_VALID:
53             return ProcIsAccountTokenValid(data, reply);
54         case DomainAccountPluginInterfaceCode::DOMAIN_PLUGIN_GET_ACCESS_TOKEN:
55             return ProcGetAccessToken(data, reply);
56         default:
57             break;
58     }
59     ACCOUNT_LOGW("remote request unhandled: %{public}d", code);
60     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
61 }
62 
ProcAuthCommonInterface(MessageParcel & data,MessageParcel & reply)63 ErrCode DomainAccountPluginStub::ProcAuthCommonInterface(MessageParcel &data, MessageParcel &reply)
64 {
65     std::shared_ptr<DomainAccountInfo> info(data.ReadParcelable<DomainAccountInfo>());
66     if (info == nullptr) {
67         ACCOUNT_LOGE("failed to read domain account info");
68         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
69     }
70     std::vector<uint8_t> authData;
71     if (!data.ReadUInt8Vector(&authData)) {
72         ACCOUNT_LOGE("failed to read password");
73         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
74     }
75     sptr<IDomainAccountCallback> callbackProxy = iface_cast<IDomainAccountCallback>(data.ReadRemoteObject());
76     int32_t mode = -1;
77     if (!data.ReadInt32(mode)) {
78         ACCOUNT_LOGE("failed to read authMode");
79         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
80     }
81     AuthMode authMode = static_cast<AuthMode>(mode);
82     ErrCode result = ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
83     if (callbackProxy == nullptr) {
84         ACCOUNT_LOGE("invalid callback");
85     } else {
86         switch (authMode) {
87             case AUTH_WITH_CREDENTIAL_MODE: {
88                 result = Auth(*info, authData, callbackProxy);
89                 break;
90             }
91             case AUTH_WITH_POPUP_MODE: {
92                 result = AuthWithPopup(*info, callbackProxy);
93                 break;
94             }
95             case AUTH_WITH_TOKEN_MODE: {
96                 result = AuthWithToken(*info, authData, callbackProxy);
97                 break;
98             }
99             default: {
100                 ACCOUNT_LOGE("invalid case");
101                 result = ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
102             }
103         }
104     }
105     (void)memset_s(authData.data(), authData.size(), 0, authData.size());
106     if (!reply.WriteInt32(result)) {
107         ACCOUNT_LOGE("failed to write result");
108         return IPC_STUB_WRITE_PARCEL_ERR;
109     }
110     return ERR_NONE;
111 }
112 
ProcGetAuthStatusInfo(MessageParcel & data,MessageParcel & reply)113 ErrCode DomainAccountPluginStub::ProcGetAuthStatusInfo(MessageParcel &data, MessageParcel &reply)
114 {
115     std::shared_ptr<DomainAccountInfo> info(data.ReadParcelable<DomainAccountInfo>());
116     if (info == nullptr) {
117         ACCOUNT_LOGE("failed to read domain account info");
118         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
119     }
120     sptr<IDomainAccountCallback> callback = iface_cast<IDomainAccountCallback>(data.ReadRemoteObject());
121     if (callback == nullptr) {
122         ACCOUNT_LOGE("failed to read domain account callback");
123         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
124     }
125     ErrCode result = GetAuthStatusInfo(*info, callback);
126     if (!reply.WriteInt32(result)) {
127         ACCOUNT_LOGE("failed to write result");
128         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
129     }
130     return ERR_NONE;
131 }
132 
ProcIsAccountTokenValid(MessageParcel & data,MessageParcel & reply)133 ErrCode DomainAccountPluginStub::ProcIsAccountTokenValid(MessageParcel &data, MessageParcel &reply)
134 {
135     std::shared_ptr<DomainAccountInfo> info(data.ReadParcelable<DomainAccountInfo>());
136     if (info == nullptr) {
137         ACCOUNT_LOGE("failed to read domain account info");
138         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
139     }
140     std::vector<uint8_t> token;
141     if (!data.ReadUInt8Vector(&token)) {
142         ACCOUNT_LOGE("failed to read token");
143         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
144     }
145     auto callback = iface_cast<IDomainAccountCallback>(data.ReadRemoteObject());
146     if (callback == nullptr) {
147         ACCOUNT_LOGE("failed to read callback");
148         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
149     }
150     ErrCode result = IsAccountTokenValid(*info, token, callback);
151     if (!reply.WriteInt32(result)) {
152         ACCOUNT_LOGE("failed to write result");
153         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
154     }
155     return ERR_NONE;
156 }
157 
ProcGetAccessToken(MessageParcel & data,MessageParcel & reply)158 ErrCode DomainAccountPluginStub::ProcGetAccessToken(MessageParcel &data, MessageParcel &reply)
159 {
160     std::shared_ptr<DomainAccountInfo> domainInfo(data.ReadParcelable<DomainAccountInfo>());
161     if (domainInfo == nullptr) {
162         ACCOUNT_LOGE("failed to read domain account info");
163         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
164     }
165     std::vector<uint8_t> accountToken;
166     if (!data.ReadUInt8Vector(&accountToken)) {
167         ACCOUNT_LOGE("failed to read user token");
168         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
169     }
170     std::shared_ptr<GetAccessTokenOptions> option(data.ReadParcelable<GetAccessTokenOptions>());
171     if (option == nullptr) {
172         ACCOUNT_LOGE("failed to read option");
173         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
174     }
175     auto callback = iface_cast<IDomainAccountCallback>(data.ReadRemoteObject());
176     if (callback == nullptr) {
177         ACCOUNT_LOGE("failed to read callback");
178         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
179     }
180     ErrCode result = GetAccessToken(*domainInfo, accountToken, *option, callback);
181     if (!reply.WriteInt32(result)) {
182         ACCOUNT_LOGE("failed to write result");
183         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
184     }
185     return ERR_NONE;
186 }
187 
ProcGetDomainAccountInfo(MessageParcel & data,MessageParcel & reply)188 ErrCode DomainAccountPluginStub::ProcGetDomainAccountInfo(MessageParcel &data, MessageParcel &reply)
189 {
190     std::shared_ptr<GetDomainAccountInfoOptions> options(data.ReadParcelable<GetDomainAccountInfoOptions>());
191     if (options == nullptr) {
192         ACCOUNT_LOGE("failed to read option");
193         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
194     }
195     auto callback = iface_cast<IDomainAccountCallback>(data.ReadRemoteObject());
196     if (callback == nullptr) {
197         ACCOUNT_LOGE("failed to read callback");
198         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
199     }
200     ErrCode result = GetDomainAccountInfo(*options, callback);
201     if (!reply.WriteInt32(result)) {
202         ACCOUNT_LOGE("failed to write result");
203         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
204     }
205     return ERR_NONE;
206 }
207 
ProcOnAccountBound(MessageParcel & data,MessageParcel & reply)208 ErrCode DomainAccountPluginStub::ProcOnAccountBound(MessageParcel &data, MessageParcel &reply)
209 {
210     std::shared_ptr<DomainAccountInfo> info(data.ReadParcelable<DomainAccountInfo>());
211     if (info == nullptr) {
212         ACCOUNT_LOGE("failed to read domain account info");
213         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
214     }
215     int32_t localId;
216     if (!data.ReadInt32(localId)) {
217         ACCOUNT_LOGE("fail to read localId");
218         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
219     }
220     auto callback = iface_cast<IDomainAccountCallback>(data.ReadRemoteObject());
221     if (callback == nullptr) {
222         ACCOUNT_LOGE("failed to read callback");
223         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
224     }
225     ErrCode result = OnAccountBound(*info, localId, callback);
226     if (!reply.WriteInt32(result)) {
227         ACCOUNT_LOGE("failed to write result");
228         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
229     }
230     return ERR_NONE;
231 }
232 
ProcOnAccountUnBound(MessageParcel & data,MessageParcel & reply)233 ErrCode DomainAccountPluginStub::ProcOnAccountUnBound(MessageParcel &data, MessageParcel &reply)
234 {
235     std::shared_ptr<DomainAccountInfo> info(data.ReadParcelable<DomainAccountInfo>());
236     if (info == nullptr) {
237         ACCOUNT_LOGE("failed to read domain account info");
238         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
239     }
240     auto callback = iface_cast<IDomainAccountCallback>(data.ReadRemoteObject());
241     if (callback == nullptr) {
242         ACCOUNT_LOGE("failed to read callback");
243         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
244     }
245     ErrCode result = OnAccountUnBound(*info, callback);
246     if (!reply.WriteInt32(result)) {
247         ACCOUNT_LOGE("failed to write result");
248         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
249     }
250     return ERR_NONE;
251 }
252 }  // namespace AccountSA
253 }  // namespace OHOS