• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef SCREENLOCK_STRONG_AUTH_MANAGER_H
16 #define SCREENLOCK_STRONG_AUTH_MANAGER_H
17 #ifndef IS_SO_CROP_H
18 
19 #include <mutex>
20 #include <string>
21 #include <singleton.h>
22 #include <sys/time.h>
23 #include "iremote_object.h"
24 #include "refbase.h"
25 #include "screenlock_common.h"
26 #include "visibility.h"
27 #include "time_service_client.h"
28 #include "itimer_info.h"
29 #include "user_auth_client.h"
30 #include "user_idm_client.h"
31 
32 namespace OHOS {
33 namespace ScreenLock {
34 
35 // 强认证默认时间 3days
36 const std::int64_t DEFAULT_STRONG_AUTH_TIMEOUT_MS = 3 * 24 * 60 * 60 * 1000;
37 // 变更口令后,第一次强认证时间为4h
38 const std::int64_t CRED_CHANGE_FIRST_STRONG_AUTH_TIMEOUT_MS = 4 * 60 * 60 * 1000;
39 // 变更口令后,第二次强认证时间为24h
40 const std::int64_t CRED_CHANGE_SECOND_STRONG_AUTH_TIMEOUT_MS = 24 * 60 * 60 * 1000;
41 
42 class StrongAuthManger : public RefBase {
43 public:
44     SCREENLOCK_API static sptr<StrongAuthManger> GetInstance();
45 
46     StrongAuthManger();
47     ~StrongAuthManger() override;
48 
49     uint64_t GetTimerId(int32_t userId);
50     void StartStrongAuthTimer(int32_t userId);
51     void ResetStrongAuthTimer(int32_t userId, int64_t triggerPeriod);
52     void DestroyStrongAuthTimer(int32_t userId);
53     void DestroyAllStrongAuthTimer();
54     void SetStrongAuthStat(int32_t userId, int32_t reasonFlag);
55     int32_t GetStrongAuthStat(int32_t userId);
56     void RegistIamEventListener();
57     void UnRegistIamEventListener();
58     void RegistAuthEventListener();
59     void UnRegistAuthEventListener();
60     void InitStrongAuthStat(int32_t userId, int32_t reasonFlag);
61     void DestroyStrongAuthStateInfo(int32_t userId);
62     bool GetCredInfo(int32_t userId);
63     int32_t GetStrongAuthTimeTrigger(int32_t userId);
64     void AccountUnlocked(int32_t userId);
65 
66 public:
67     class AuthEventListenerService : public UserIam::UserAuth::AuthSuccessEventListener {
68     public:
69         AuthEventListenerService() = default;
70         virtual ~AuthEventListenerService() = default;
71         void OnNotifyAuthSuccessEvent(int32_t userId, UserIam::UserAuth::AuthType authType, int32_t callerType,
72             const std::string &bundleName) override;
73     };
74 
75     class CredChangeListenerService : public UserIam::UserAuth::CredChangeEventListener {
76     public:
77         CredChangeListenerService() = default;
78         virtual ~CredChangeListenerService() = default;
79         void OnNotifyCredChangeEvent(int32_t userId, UserIam::UserAuth::AuthType authType,
80             UserIam::UserAuth::CredChangeEventType eventType,
81             const UserIam::UserAuth::CredChangeEventInfo &changeInfo) override;
82     };
83 
84     class authTimer : public MiscServices::ITimerInfo {
85     public:
86         authTimer();
87         authTimer(bool repeat, uint64_t interval, bool isExact, bool isIdle = false);
88         virtual ~authTimer();
89         virtual void OnTrigger() override;
90         virtual void SetType(const int &type) override;
91         virtual void SetRepeat(bool repeat) override;
92         virtual void SetInterval(const uint64_t &interval) override;
93         virtual void SetWantAgent(std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> wantAgent) override;
94         void SetCallbackInfo(const std::function<void(int32_t)> &callBack);
95         int32_t GetUserId();
96         void SetUserId(int32_t userId);
97 
98     private:
99         int32_t userId_ = 0;
100         std::function<void(int32_t)> callBack_ = nullptr;
101     };
102 
103     class StrongAuthGetSecurity : public UserIam::UserAuth::GetCredentialInfoCallback {
104     public:
StrongAuthGetSecurity(int32_t userId)105         explicit StrongAuthGetSecurity(int32_t userId) : userId_(userId)
106         {}
107         virtual ~StrongAuthGetSecurity() = default;
108         void OnCredentialInfo(
109             int32_t result, const std::vector<UserIam::UserAuth::CredentialInfo> &infoList) override;
110     private:
111         int32_t userId_ = 100;
112     };
113 
114 private:
115     void StartStrongAuthTimer(int32_t userId, int64_t triggerPeriod);
116     void SetCredChangeTriggerPeriod(int32_t userId, int64_t triggerPeriod);
117     int64_t GetStrongAuthTriggerPeriod(int32_t userId);
118     bool IsUserExitInStrongAuthInfo(int32_t userId);
119     bool IsUserHasStrongAuthTimer(int32_t userId);
120     void NotifyStrongAuthChange(int32_t userId, int32_t reasonFlag);
121 
122     struct TimerInfo {
123         uint64_t timerId{0};
124         int64_t triggerPeriod{DEFAULT_STRONG_AUTH_TIMEOUT_MS};
125         int64_t credChangeTimerStamp{-1};
126     };
127 
128     std::mutex strongAuthTimerMutex;
129     static std::mutex instanceLock_;
130     static sptr<StrongAuthManger> instance_;
131     std::map<int32_t, int32_t> strongAuthStateInfo;
132     std::map<int32_t, TimerInfo> strongAuthTimerInfo;
133     std::shared_ptr<UserIam::UserAuth::AuthSuccessEventListener> authSuccessListener_;
134     std::shared_ptr<UserIam::UserAuth::CredChangeEventListener> credChangeListener_;
135 };
136 } // namespace OHOS
137 } // namespace ScreenLock
138 #endif // IS_SO_CROP_H
139 #endif // SCREENLOCK_STRONG_AUTH_MANAGER_H