1 /*
2 * Copyright (C) 2024 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 "strongauthmanager.h"
17 #include "screenlock_common.h"
18 #include "sclock_log.h"
19 #include "screenlock_system_ability.h"
20 #include "user_auth_client_callback.h"
21 #include "user_auth_client_impl.h"
22
23 namespace OHOS {
24 namespace ScreenLock {
25 std::mutex StrongAuthManger::instanceLock_;
26 sptr<StrongAuthManger> StrongAuthManger::instance_;
27 using namespace OHOS::UserIam::UserAuth;
28
29 // 强认证默认时间 3days
30 const std::int64_t DEFAULT_STRONG_AUTH_TIMEOUT_MS = 3 * 24 * 60 * 60 * 1000;
31
StrongAuthManger()32 StrongAuthManger::StrongAuthManger() {}
33
~StrongAuthManger()34 StrongAuthManger::~StrongAuthManger() {}
35
authTimer()36 StrongAuthManger::authTimer::authTimer()
37 {
38 userId_ = 0;
39 }
40
authTimer(bool repeat,uint64_t interval,bool isExact,bool isIdle)41 StrongAuthManger::authTimer::authTimer(bool repeat, uint64_t interval, bool isExact, bool isIdle)
42 {
43 this->repeat = repeat;
44 this->interval = interval;
45 this->type = TIMER_TYPE_WAKEUP;
46 if (isExact) {
47 this->type = TIMER_TYPE_WAKEUP + TIMER_TYPE_REALTIME;
48 }
49 if (isIdle) {
50 this->type = TIMER_TYPE_IDLE;
51 }
52 userId_ = 0;
53 }
54
~authTimer()55 StrongAuthManger::authTimer::~authTimer() {}
56
OnTrigger()57 void StrongAuthManger::authTimer::OnTrigger()
58 {
59 SCLOCK_HILOGI("%{public}d, OnTrigger enter", userId_);
60 if (callBack_) {
61 callBack_(userId_);
62 }
63 }
64
SetType(const int & type)65 void StrongAuthManger::authTimer::SetType(const int &type)
66 {
67 this->type = type;
68 }
69
SetRepeat(bool repeat)70 void StrongAuthManger::authTimer::SetRepeat(bool repeat)
71 {
72 this->repeat = repeat;
73 }
74
SetInterval(const uint64_t & interval)75 void StrongAuthManger::authTimer::SetInterval(const uint64_t &interval)
76 {
77 this->interval = interval;
78 }
79
SetWantAgent(std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> wantAgent)80 void StrongAuthManger::authTimer::SetWantAgent(std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> wantAgent)
81 {
82 this->wantAgent = wantAgent;
83 }
84
SetCallbackInfo(const std::function<void (int32_t)> & callBack)85 void StrongAuthManger::authTimer::SetCallbackInfo(const std::function<void(int32_t)> &callBack)
86 {
87 this->callBack_ = callBack;
88 }
89
GetUserId()90 int32_t StrongAuthManger::authTimer::GetUserId()
91 {
92 return userId_;
93 }
94
SetUserId(int32_t userId)95 void StrongAuthManger::authTimer::SetUserId(int32_t userId)
96 {
97 userId_ = userId;
98 }
99
StrongAuthTimerCallback(int32_t userId)100 static void StrongAuthTimerCallback(int32_t userId)
101 {
102 SCLOCK_HILOGI("%{public}s, enter", __FUNCTION__);
103 uint64_t timerId = StrongAuthManger::GetInstance()->GetTimerId(userId);
104 int32_t reasonFlag = static_cast<int32_t>(StrongAuthReasonFlags::AFTER_TIMEOUT);
105 StrongAuthManger::GetInstance()->ResetStrongAuthTimer(userId);
106 StrongAuthManger::GetInstance()->SetStrongAuthStat(userId, reasonFlag);
107 ScreenLockSystemAbility::GetInstance()->StrongAuthChanged(userId, reasonFlag);
108 return;
109 }
110
GetInstance()111 sptr<StrongAuthManger> StrongAuthManger::GetInstance()
112 {
113 if (instance_ == nullptr) {
114 std::lock_guard<std::mutex> autoLock(instanceLock_);
115 if (instance_ == nullptr) {
116 instance_ = new StrongAuthManger;
117 }
118 }
119 return instance_;
120 }
121
122
GetTimerId(int32_t userId)123 uint64_t StrongAuthManger::GetTimerId(int32_t userId)
124 {
125 uint64_t timerId = 0;
126 auto iter = strongAuthTimerInfo.find(userId);
127 if (iter != strongAuthTimerInfo.end()) {
128 timerId = iter->second;
129 }
130 return timerId;
131 }
132
RegistUserAuthSuccessEventListener()133 void StrongAuthManger::RegistUserAuthSuccessEventListener()
134 {
135 SCLOCK_HILOGD("RegistUserAuthSuccessEventListener start");
136 std::vector<UserIam::UserAuth::AuthType> authTypeList;
137 authTypeList.emplace_back(AuthType::PIN);
138 authTypeList.emplace_back(AuthType::FACE);
139 authTypeList.emplace_back(AuthType::FINGERPRINT);
140
141 if (listener_ == nullptr) {
142 sptr<UserIam::UserAuth::AuthEventListenerInterface> wrapper(new (std::nothrow) AuthEventListenerService());
143 if (wrapper == nullptr) {
144 SCLOCK_HILOGE("get listener failed");
145 return;
146 }
147 listener_ = wrapper;
148 int32_t ret = UserIam::UserAuth::UserAuthClientImpl::GetInstance().RegistUserAuthSuccessEventListener(
149 authTypeList, listener_);
150 SCLOCK_HILOGI("RegistUserAuthSuccessEventListener ret: %{public}d", ret);
151 }
152
153 return;
154 }
155
OnNotifyAuthSuccessEvent(int32_t userId,UserIam::UserAuth::AuthType authType,int32_t callerType,std::string & bundleName)156 void StrongAuthManger::AuthEventListenerService::OnNotifyAuthSuccessEvent(int32_t userId,
157 UserIam::UserAuth::AuthType authType, int32_t callerType, std::string &bundleName)
158 {
159 SCLOCK_HILOGI("OnNotifyAuthSuccessEvent: %{public}d, %{public}d, %{public}s, callerType: %{public}d", userId,
160 static_cast<int32_t>(authType), bundleName.c_str(), callerType);
161 if (authType == AuthType::PIN) {
162 StrongAuthManger::GetInstance()->SetStrongAuthStat(userId, static_cast<int32_t>(StrongAuthReasonFlags::NONE));
163 StrongAuthManger::GetInstance()->ResetStrongAuthTimer(userId);
164 }
165 return;
166 }
167
UnRegistUserAuthSuccessEventListener()168 void StrongAuthManger::UnRegistUserAuthSuccessEventListener()
169 {
170 if (listener_ != nullptr) {
171 int32_t ret =
172 UserIam::UserAuth::UserAuthClientImpl::GetInstance().UnRegistUserAuthSuccessEventListener(listener_);
173 SCLOCK_HILOGI("UnRegistUserAuthSuccessEventListener ret: %{public}d", ret);
174 }
175 }
176
StartStrongAuthTimer(int32_t userId)177 void StrongAuthManger::StartStrongAuthTimer(int32_t userId)
178 {
179 std::unique_lock<std::mutex> lock(strongAuthTimerMutex);
180 uint64_t timerId = GetTimerId(userId);
181 if (timerId != 0) {
182 SCLOCK_HILOGI("StrongAuthTimer exist. userId:%{public}d", userId);
183 return;
184 }
185
186 std::shared_ptr<authTimer> timer = std::make_shared<authTimer>(true, DEFAULT_STRONG_AUTH_TIMEOUT_MS, true, false);
187 timer->SetCallbackInfo(StrongAuthTimerCallback);
188 timer->SetUserId(userId);
189 timerId = MiscServices::TimeServiceClient::GetInstance()->CreateTimer(timer);
190 int64_t currentTime = MiscServices::TimeServiceClient::GetInstance()->GetBootTimeMs();
191 MiscServices::TimeServiceClient::GetInstance()->StartTimer(timerId, currentTime + DEFAULT_STRONG_AUTH_TIMEOUT_MS);
192 strongAuthTimerInfo.insert(std::make_pair(userId, timerId));
193 return;
194 }
195
ResetStrongAuthTimer(int32_t userId)196 void StrongAuthManger::ResetStrongAuthTimer(int32_t userId)
197 {
198 uint64_t timerId = GetTimerId(userId);
199 if (timerId == 0) {
200 StartStrongAuthTimer(userId);
201 return;
202 }
203 int64_t currentTime = MiscServices::TimeServiceClient::GetInstance()->GetBootTimeMs();
204 MiscServices::TimeServiceClient::GetInstance()->StopTimer(timerId);
205 MiscServices::TimeServiceClient::GetInstance()->StartTimer(timerId, currentTime + DEFAULT_STRONG_AUTH_TIMEOUT_MS);
206 return;
207 }
208
DestroyAllStrongAuthTimer()209 void StrongAuthManger::DestroyAllStrongAuthTimer()
210 {
211 for (auto iter = strongAuthStateInfo.begin(); iter != strongAuthStateInfo.end(); ++iter) {
212 DestroyStrongAuthTimer(iter->first);
213 }
214 return;
215 }
216
DestroyStrongAuthTimer(int32_t userId)217 void StrongAuthManger::DestroyStrongAuthTimer(int32_t userId)
218 {
219 std::unique_lock<std::mutex> lock(strongAuthTimerMutex);
220 uint64_t timerId = GetTimerId(userId);
221 if (timerId == 0) {
222 return;
223 }
224 MiscServices::TimeServiceClient::GetInstance()->StopTimer(timerId);
225 MiscServices::TimeServiceClient::GetInstance()->DestroyTimer(timerId);
226 strongAuthTimerInfo.erase(userId);
227 return;
228 }
229
SetStrongAuthStat(int32_t userId,int32_t reasonFlag)230 void StrongAuthManger::SetStrongAuthStat(int32_t userId, int32_t reasonFlag)
231 {
232 auto iter = strongAuthStateInfo.find(userId);
233 if (iter != strongAuthStateInfo.end()) {
234 iter->second = reasonFlag;
235 SCLOCK_HILOGI("SetStrongAuthStat, reasonFlag:%{public}u", reasonFlag);
236 return;
237 }
238 strongAuthStateInfo.insert(std::make_pair(userId, reasonFlag));
239 return;
240 }
241
GetStrongAuthStat(int32_t userId)242 int32_t StrongAuthManger::GetStrongAuthStat(int32_t userId)
243 {
244 int32_t reasonFlag = static_cast<int32_t>(StrongAuthReasonFlags::AFTER_BOOT);
245 auto iter = strongAuthStateInfo.find(userId);
246 if (iter != strongAuthStateInfo.end()) {
247 reasonFlag = iter->second;
248 SCLOCK_HILOGI("GetStrongAuthStat, reasonFlag:%{public}u", reasonFlag);
249 }
250 return reasonFlag;
251 }
252 } // namespace ScreenLock
253 } // namespace OHOS