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_session_controller_impl.h"
17
18 #include <optional>
19
20 #include "hdi_wrapper.h"
21 #include "iam_logger.h"
22 #include "iam_common_defines.h"
23
24 namespace OHOS {
25 namespace UserIam {
26 namespace UserAuth {
27 #define LOG_TAG "USER_AUTH_SA"
28
OpenSession(int32_t userId,std::vector<uint8_t> & challenge)29 bool UserIdmSessionControllerImpl::OpenSession(int32_t userId, std::vector<uint8_t> &challenge)
30 {
31 auto hdi = HdiWrapper::GetHdiInstance();
32 if (hdi == nullptr) {
33 IAM_LOGE("bad hdi");
34 return false;
35 }
36 std::lock_guard<std::mutex> lock(mutex_);
37 if (!sessionSet_.empty()) {
38 IAM_LOGW("old session is not closed");
39 }
40
41 int32_t ret = hdi->OpenSession(userId, challenge);
42 if (ret != HDF_SUCCESS) {
43 IAM_LOGE("failed to open session, error code : %{public}d", ret);
44 return false;
45 }
46 sessionSet_.insert(userId);
47 return true;
48 }
49
CloseSession(int32_t userId)50 bool UserIdmSessionControllerImpl::CloseSession(int32_t userId)
51 {
52 auto hdi = HdiWrapper::GetHdiInstance();
53 if (hdi == nullptr) {
54 IAM_LOGE("bad hdi");
55 return false;
56 }
57 std::lock_guard<std::mutex> lock(mutex_);
58 int32_t ret = hdi->CloseSession(userId);
59 if (ret != HDF_SUCCESS) {
60 IAM_LOGE("failed to close session, error code : %{public}d", ret);
61 return false;
62 }
63
64 sessionSet_.erase(userId);
65 return true;
66 }
67
IsSessionOpened(int32_t userId) const68 bool UserIdmSessionControllerImpl::IsSessionOpened(int32_t userId) const
69 {
70 std::lock_guard<std::mutex> lock(mutex_);
71 return sessionSet_.find(userId) != sessionSet_.end();
72 }
73
ForceReset()74 bool UserIdmSessionControllerImpl::ForceReset()
75 {
76 std::lock_guard<std::mutex> lock(mutex_);
77 sessionSet_.clear();
78 return true;
79 }
80
Instance()81 UserIdmSessionController &UserIdmSessionController::Instance()
82 {
83 return UserIdmSessionControllerImpl::GetInstance();
84 }
85 } // namespace UserAuth
86 } // namespace UserIam
87 } // namespace OHOS