• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_stub.h"
17 
18 #include "account_error_no.h"
19 #include "account_log_wrapper.h"
20 
21 namespace OHOS {
22 namespace AccountSA {
AppAccountAuthenticatorStub()23 AppAccountAuthenticatorStub::AppAccountAuthenticatorStub()
24 {}
25 
~AppAccountAuthenticatorStub()26 AppAccountAuthenticatorStub::~AppAccountAuthenticatorStub()
27 {}
28 
29 const std::map<uint32_t, AppAccountAuthenticatorStub::MessageProcFunction> AppAccountAuthenticatorStub::funcMap_ = {
30     {
31         static_cast<uint32_t>(IAppAccountAuthenticator::Message::ADD_ACCOUNT_IMPLICITLY),
32         &AppAccountAuthenticatorStub::ProcAddAccountImplicitly,
33     },
34     {
35         static_cast<uint32_t>(IAppAccountAuthenticator::Message::AUTHENTICATE),
36         &AppAccountAuthenticatorStub::ProcAuthenticate,
37     },
38     {
39         static_cast<uint32_t>(IAppAccountAuthenticator::Message::VERIFY_CREDENTIAL),
40         &AppAccountAuthenticatorStub::ProcVerifyCredential,
41     },
42     {
43         static_cast<uint32_t>(IAppAccountAuthenticator::Message::CHECK_ACCOUNT_LABELS),
44         &AppAccountAuthenticatorStub::ProcCheckAccountLabels,
45     },
46     {
47         static_cast<uint32_t>(IAppAccountAuthenticator::Message::SET_PROPERTIES),
48         &AppAccountAuthenticatorStub::ProcSetProperties,
49     },
50     {
51         static_cast<uint32_t>(IAppAccountAuthenticator::Message::IS_ACCOUNT_REMOVABLE),
52         &AppAccountAuthenticatorStub::ProcIsAccountRemovable,
53     },
54     {
55         static_cast<uint32_t>(IAppAccountAuthenticator::Message::CREATE_ACCOUNT_IMPLICITLY),
56         &AppAccountAuthenticatorStub::ProcCreateAccountImplicitly,
57     },
58     {
59         static_cast<uint32_t>(IAppAccountAuthenticator::Message::AUTH),
60         &AppAccountAuthenticatorStub::ProcAuth,
61     }
62 };
63 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)64 int AppAccountAuthenticatorStub::OnRemoteRequest(
65     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
66 {
67     if (data.ReadInterfaceToken() != GetDescriptor()) {
68         ACCOUNT_LOGE("failed to check descriptor! code %{public}u.", code);
69         return ERR_ACCOUNT_COMMON_CHECK_DESCRIPTOR_ERROR;
70     }
71 
72     auto messageProc = funcMap_.find(code);
73     if (messageProc != funcMap_.end()) {
74         auto messageProcFunction = messageProc->second;
75         if (messageProcFunction != nullptr) {
76             return (this->*messageProcFunction)(data, reply);
77         }
78     }
79 
80     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
81 }
82 
ProcAddAccountImplicitly(MessageParcel & data,MessageParcel & reply)83 ErrCode AppAccountAuthenticatorStub::ProcAddAccountImplicitly(MessageParcel &data, MessageParcel &reply)
84 {
85     std::string authType = data.ReadString();
86     std::string callerBundleName = data.ReadString();
87     std::shared_ptr<AAFwk::WantParams> options(data.ReadParcelable<AAFwk::WantParams>());
88     sptr<IRemoteObject> callback = data.ReadRemoteObject();
89     ErrCode result = ERR_OK;
90     if ((options == nullptr) || (callback == nullptr)) {
91         ACCOUNT_LOGE("invalid request parameters");
92         result = ERR_APPACCOUNT_SERVICE_INVALID_PARAMETER;
93     } else {
94         result = AddAccountImplicitly(authType, callerBundleName, *options, callback);
95     }
96     if (!reply.WriteInt32(result)) {
97         ACCOUNT_LOGE("failed to write reply");
98         return IPC_STUB_WRITE_PARCEL_ERR;
99     }
100     return ERR_NONE;
101 }
102 
ProcAuthenticate(MessageParcel & data,MessageParcel & reply)103 ErrCode AppAccountAuthenticatorStub::ProcAuthenticate(MessageParcel &data, MessageParcel &reply)
104 {
105     std::string name = data.ReadString();
106     std::string authType = data.ReadString();
107     std::string callerBundleName = data.ReadString();
108     std::shared_ptr<AAFwk::WantParams> options(data.ReadParcelable<AAFwk::WantParams>());
109     sptr<IRemoteObject> callback = data.ReadRemoteObject();
110     ErrCode result = ERR_OK;
111     if ((options == nullptr) || (callback == nullptr)) {
112         ACCOUNT_LOGE("invalid request parameters");
113         result = ERR_APPACCOUNT_SERVICE_INVALID_PARAMETER;
114     } else {
115         result = Authenticate(name, authType, callerBundleName, *options, callback);
116     }
117     if (!reply.WriteInt32(result)) {
118         ACCOUNT_LOGE("failed to write reply");
119         return IPC_STUB_WRITE_PARCEL_ERR;
120     }
121     return ERR_NONE;
122 }
123 
ProcCreateAccountImplicitly(MessageParcel & data,MessageParcel & reply)124 ErrCode AppAccountAuthenticatorStub::ProcCreateAccountImplicitly(MessageParcel &data, MessageParcel &reply)
125 {
126     sptr<CreateAccountImplicitlyOptions> options = data.ReadParcelable<CreateAccountImplicitlyOptions>();
127     sptr<IRemoteObject> callback = data.ReadRemoteObject();
128     ErrCode result = ERR_OK;
129     if ((options == nullptr) || (callback == nullptr)) {
130         ACCOUNT_LOGE("invalid request parameters");
131         result = ERR_APPACCOUNT_SERVICE_INVALID_PARAMETER;
132     } else {
133         result = CreateAccountImplicitly(*options, callback);
134     }
135     if (!reply.WriteInt32(result)) {
136         ACCOUNT_LOGE("failed to write reply");
137         return IPC_STUB_WRITE_PARCEL_ERR;
138     }
139     return ERR_NONE;
140 }
141 
ProcAuth(MessageParcel & data,MessageParcel & reply)142 ErrCode AppAccountAuthenticatorStub::ProcAuth(MessageParcel &data, MessageParcel &reply)
143 {
144     std::string name = data.ReadString();
145     std::string authType = data.ReadString();
146     std::shared_ptr<AAFwk::WantParams> options(data.ReadParcelable<AAFwk::WantParams>());
147     sptr<IRemoteObject> callback = data.ReadRemoteObject();
148     ErrCode result = ERR_OK;
149     if ((options == nullptr) || (callback == nullptr)) {
150         ACCOUNT_LOGE("invalid request parameters");
151         result = ERR_APPACCOUNT_SERVICE_INVALID_PARAMETER;
152     } else {
153         result = Auth(name, authType, *options, callback);
154     }
155     if (!reply.WriteInt32(result)) {
156         ACCOUNT_LOGE("failed to write reply");
157         return IPC_STUB_WRITE_PARCEL_ERR;
158     }
159     return ERR_NONE;
160 }
161 
ProcVerifyCredential(MessageParcel & data,MessageParcel & reply)162 ErrCode AppAccountAuthenticatorStub::ProcVerifyCredential(MessageParcel &data, MessageParcel &reply)
163 {
164     std::string name = data.ReadString();
165     sptr<VerifyCredentialOptions> options = data.ReadParcelable<VerifyCredentialOptions>();
166     sptr<IRemoteObject> callback = data.ReadRemoteObject();
167     ErrCode result = ERR_OK;
168     if ((options == nullptr) || (callback == nullptr)) {
169         ACCOUNT_LOGE("invalid request parameters");
170         result = ERR_APPACCOUNT_SERVICE_INVALID_PARAMETER;
171     } else {
172         result = VerifyCredential(name, *options, callback);
173     }
174     if (!reply.WriteInt32(result)) {
175         ACCOUNT_LOGE("failed to write reply");
176         return IPC_STUB_WRITE_PARCEL_ERR;
177     }
178     return ERR_NONE;
179 }
180 
ProcCheckAccountLabels(MessageParcel & data,MessageParcel & reply)181 ErrCode AppAccountAuthenticatorStub::ProcCheckAccountLabels(MessageParcel &data, MessageParcel &reply)
182 {
183     std::string name = data.ReadString();
184     std::vector<std::string> labels;
185     data.ReadStringVector(&labels);
186     sptr<IRemoteObject> callback = data.ReadRemoteObject();
187     ErrCode result = ERR_OK;
188     if (callback == nullptr) {
189         ACCOUNT_LOGE("invalid request parameters");
190         result = ERR_APPACCOUNT_SERVICE_INVALID_PARAMETER;
191     } else {
192         result = CheckAccountLabels(name, labels, callback);
193     }
194     if (!reply.WriteInt32(result)) {
195         ACCOUNT_LOGE("failed to write reply");
196         return IPC_STUB_WRITE_PARCEL_ERR;
197     }
198     return ERR_NONE;
199 }
200 
ProcSetProperties(MessageParcel & data,MessageParcel & reply)201 ErrCode AppAccountAuthenticatorStub::ProcSetProperties(MessageParcel &data, MessageParcel &reply)
202 {
203     sptr<SetPropertiesOptions> options = data.ReadParcelable<SetPropertiesOptions>();
204     sptr<IRemoteObject> callback = data.ReadRemoteObject();
205     ErrCode result = ERR_OK;
206     if ((options == nullptr) || (callback == nullptr)) {
207         ACCOUNT_LOGE("invalid request parameters");
208         result = ERR_APPACCOUNT_SERVICE_INVALID_PARAMETER;
209     } else {
210         result = SetProperties(*options, callback);
211     }
212     if (!reply.WriteInt32(result)) {
213         ACCOUNT_LOGE("failed to write reply");
214         return IPC_STUB_WRITE_PARCEL_ERR;
215     }
216     return ERR_NONE;
217 }
218 
ProcIsAccountRemovable(MessageParcel & data,MessageParcel & reply)219 ErrCode AppAccountAuthenticatorStub::ProcIsAccountRemovable(MessageParcel &data, MessageParcel &reply)
220 {
221     std::string name = data.ReadString();
222     sptr<IRemoteObject> callback = data.ReadRemoteObject();
223     ErrCode result = ERR_OK;
224     if (callback == nullptr) {
225         ACCOUNT_LOGE("invalid request parameters");
226         result = ERR_APPACCOUNT_SERVICE_INVALID_PARAMETER;
227     } else {
228         result = IsAccountRemovable(name, callback);
229     }
230     if (!reply.WriteInt32(result)) {
231         ACCOUNT_LOGE("failed to write reply");
232         return IPC_STUB_WRITE_PARCEL_ERR;
233     }
234     return ERR_NONE;
235 }
236 }  // namespace AccountSA
237 }  // namespace OHOS
238