• 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 "user_idm_client_impl.h"
17 
18 #include "system_ability_definition.h"
19 
20 #include "iam_logger.h"
21 #include "ipc_client_utils.h"
22 #include "user_idm_callback_service.h"
23 
24 #define LOG_LABEL UserIam::Common::LABEL_USER_AUTH_SDK
25 
26 namespace OHOS {
27 namespace UserIam {
28 namespace UserAuth {
OpenSession(int32_t userId)29 std::vector<uint8_t> UserIdmClientImpl::OpenSession(int32_t userId)
30 {
31     auto proxy = GetProxy();
32     if (!proxy) {
33         IAM_LOGE("proxy is nullptr");
34         return {};
35     }
36 
37     std::vector<uint8_t> challenge;
38     auto success = proxy->OpenSession(userId, challenge);
39     if (success != SUCCESS) {
40         IAM_LOGE("OpenSession ret = %{public}d", success);
41     }
42 
43     return challenge;
44 }
45 
CloseSession(int32_t userId)46 void UserIdmClientImpl::CloseSession(int32_t userId)
47 {
48     auto proxy = GetProxy();
49     if (!proxy) {
50         IAM_LOGE("proxy is nullptr");
51         return;
52     }
53 
54     proxy->CloseSession(userId);
55 }
56 
AddCredential(int32_t userId,const CredentialParameters & para,const std::shared_ptr<UserIdmClientCallback> & callback)57 void UserIdmClientImpl::AddCredential(int32_t userId, const CredentialParameters &para,
58     const std::shared_ptr<UserIdmClientCallback> &callback)
59 {
60     if (!callback) {
61         IAM_LOGE("user idm client callback is nullptr");
62         return;
63     }
64     auto proxy = GetProxy();
65     if (!proxy) {
66         IAM_LOGE("proxy is nullptr");
67         Attributes extraInfo;
68         callback->OnResult(GENERAL_ERROR, extraInfo);
69         return;
70     }
71 
72     sptr<IdmCallbackInterface> wrapper = new (std::nothrow) IdmCallbackService(callback);
73     if (wrapper == nullptr) {
74         IAM_LOGE("failed to create wrapper");
75         Attributes extraInfo;
76         callback->OnResult(GENERAL_ERROR, extraInfo);
77         return;
78     }
79     UserIdmInterface::CredentialPara credPara = {};
80     credPara.authType = para.authType;
81     credPara.pinType = para.pinType.value_or(PIN_SIX);
82     credPara.token = std::move(para.token);
83     proxy->AddCredential(userId, credPara, wrapper, false);
84 }
85 
UpdateCredential(int32_t userId,const CredentialParameters & para,const std::shared_ptr<UserIdmClientCallback> & callback)86 void UserIdmClientImpl::UpdateCredential(int32_t userId, const CredentialParameters &para,
87     const std::shared_ptr<UserIdmClientCallback> &callback)
88 {
89     if (!callback) {
90         IAM_LOGE("user idm client callback is nullptr");
91         return;
92     }
93     auto proxy = GetProxy();
94     if (!proxy) {
95         IAM_LOGE("proxy is nullptr");
96         Attributes extraInfo;
97         callback->OnResult(GENERAL_ERROR, extraInfo);
98         return;
99     }
100 
101     sptr<IdmCallbackInterface> wrapper = new (std::nothrow) IdmCallbackService(callback);
102     if (wrapper == nullptr) {
103         IAM_LOGE("failed to create wrapper");
104         Attributes extraInfo;
105         callback->OnResult(GENERAL_ERROR, extraInfo);
106         return;
107     }
108     UserIdmInterface::CredentialPara credPara = {};
109     credPara.authType = para.authType;
110     credPara.pinType = para.pinType.value_or(PIN_SIX);
111     credPara.token = std::move(para.token);
112     proxy->UpdateCredential(userId, credPara, wrapper);
113 }
114 
Cancel(int32_t userId)115 int32_t UserIdmClientImpl::Cancel(int32_t userId)
116 {
117     auto proxy = GetProxy();
118     if (!proxy) {
119         IAM_LOGE("proxy is nullptr");
120         return GENERAL_ERROR;
121     }
122 
123     return proxy->Cancel(userId);
124 }
125 
DeleteCredential(int32_t userId,uint64_t credentialId,const std::vector<uint8_t> & authToken,const std::shared_ptr<UserIdmClientCallback> & callback)126 void UserIdmClientImpl::DeleteCredential(int32_t userId, uint64_t credentialId, const std::vector<uint8_t> &authToken,
127     const std::shared_ptr<UserIdmClientCallback> &callback)
128 {
129     if (!callback) {
130         IAM_LOGE("user idm client callback is nullptr");
131         return;
132     }
133     auto proxy = GetProxy();
134     if (!proxy) {
135         IAM_LOGE("proxy is nullptr");
136         Attributes extraInfo;
137         callback->OnResult(GENERAL_ERROR, extraInfo);
138         return;
139     }
140 
141     sptr<IdmCallbackInterface> wrapper = new (std::nothrow) IdmCallbackService(callback);
142     if (wrapper == nullptr) {
143         IAM_LOGE("failed to create wrapper");
144         Attributes extraInfo;
145         callback->OnResult(GENERAL_ERROR, extraInfo);
146         return;
147     }
148     proxy->DelCredential(userId, credentialId, authToken, wrapper);
149 }
150 
DeleteUser(int32_t userId,const std::vector<uint8_t> & authToken,const std::shared_ptr<UserIdmClientCallback> & callback)151 void UserIdmClientImpl::DeleteUser(int32_t userId, const std::vector<uint8_t> &authToken,
152     const std::shared_ptr<UserIdmClientCallback> &callback)
153 {
154     if (!callback) {
155         IAM_LOGE("user idm client callback is nullptr");
156         return;
157     }
158     auto proxy = GetProxy();
159     if (!proxy) {
160         IAM_LOGE("proxy is nullptr");
161         Attributes extraInfo;
162         callback->OnResult(GENERAL_ERROR, extraInfo);
163         return;
164     }
165 
166     sptr<IdmCallbackInterface> wrapper = new (std::nothrow) IdmCallbackService(callback);
167     if (wrapper == nullptr) {
168         IAM_LOGE("failed to create wrapper");
169         Attributes extraInfo;
170         callback->OnResult(GENERAL_ERROR, extraInfo);
171         return;
172     }
173     proxy->DelUser(userId, authToken, wrapper);
174 }
175 
EraseUser(int32_t userId,const std::shared_ptr<UserIdmClientCallback> & callback)176 int32_t UserIdmClientImpl::EraseUser(int32_t userId, const std::shared_ptr<UserIdmClientCallback> &callback)
177 {
178     if (!callback) {
179         IAM_LOGE("user idm client callback is nullptr");
180         return GENERAL_ERROR;
181     }
182     auto proxy = GetProxy();
183     if (!proxy) {
184         IAM_LOGE("proxy is nullptr");
185         Attributes extraInfo;
186         callback->OnResult(GENERAL_ERROR, extraInfo);
187         return GENERAL_ERROR;
188     }
189     sptr<IdmCallbackInterface> wrapper = new (std::nothrow) IdmCallbackService(callback);
190     if (wrapper == nullptr) {
191         IAM_LOGE("failed to create wrapper");
192         Attributes extraInfo;
193         callback->OnResult(GENERAL_ERROR, extraInfo);
194         return GENERAL_ERROR;
195     }
196     return proxy->EnforceDelUser(userId, wrapper);
197 }
198 
GetCredentialInfo(int32_t userId,AuthType authType,const std::shared_ptr<GetCredentialInfoCallback> & callback)199 int32_t UserIdmClientImpl::GetCredentialInfo(int32_t userId, AuthType authType,
200     const std::shared_ptr<GetCredentialInfoCallback> &callback)
201 {
202     if (!callback) {
203         IAM_LOGE("get credential info callback is nullptr");
204         return GENERAL_ERROR;
205     }
206 
207     auto proxy = GetProxy();
208     if (!proxy) {
209         IAM_LOGE("proxy is nullptr");
210         std::vector<CredentialInfo> infoList;
211         callback->OnCredentialInfo(infoList);
212         return GENERAL_ERROR;
213     }
214 
215     sptr<IdmGetCredInfoCallbackInterface> wrapper = new (std::nothrow) IdmGetCredInfoCallbackService(callback);
216     if (wrapper == nullptr) {
217         IAM_LOGE("failed to create wrapper");
218         std::vector<CredentialInfo> infoList;
219         callback->OnCredentialInfo(infoList);
220         return GENERAL_ERROR;
221     }
222     return proxy->GetCredentialInfo(userId, authType, wrapper);
223 }
224 
GetSecUserInfo(int32_t userId,const std::shared_ptr<GetSecUserInfoCallback> & callback)225 int32_t UserIdmClientImpl::GetSecUserInfo(int32_t userId, const std::shared_ptr<GetSecUserInfoCallback> &callback)
226 {
227     if (!callback) {
228         IAM_LOGE("get secure info callback is nullptr");
229         return GENERAL_ERROR;
230     }
231 
232     auto proxy = GetProxy();
233     if (!proxy) {
234         IAM_LOGE("proxy is nullptr");
235         SecUserInfo info = {};
236         callback->OnSecUserInfo(info);
237         return GENERAL_ERROR;
238     }
239 
240     sptr<IdmGetSecureUserInfoCallbackInterface> wrapper =
241         new (std::nothrow) IdmGetSecureUserInfoCallbackService(callback);
242     if (wrapper == nullptr) {
243         IAM_LOGE("failed to create wrapper");
244         SecUserInfo info = {};
245         callback->OnSecUserInfo(info);
246         return GENERAL_ERROR;
247     }
248     return proxy->GetSecInfo(userId, wrapper);
249 }
250 
GetProxy()251 sptr<UserIdmInterface> UserIdmClientImpl::GetProxy()
252 {
253     auto obj = IpcClientUtils::GetRemoteObject(SUBSYS_USERIAM_SYS_ABILITY_USERIDM);
254     if (obj == nullptr) {
255         IAM_LOGE("failed to get useridm service");
256         return nullptr;
257     }
258 
259     return iface_cast<UserIdmInterface>(obj);
260 }
261 
GetInstance()262 UserIdmClient &UserIdmClient::GetInstance()
263 {
264     static UserIdmClientImpl impl;
265     return impl;
266 }
267 } // namespace UserAuth
268 } // namespace UserIam
269 } // namespace OHOS