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