lintel 0.2.0
Loading...
Searching...
No Matches
AuthService.h
Go to the documentation of this file.
1#ifndef LINTEL_AUTHSERVICE_H
2#define LINTEL_AUTHSERVICE_H
3
4#include <atomic>
5#include <chrono>
6#include <cstdint>
7#include <deque>
8#include <map>
9#include <memory>
10#include <mutex>
11#include <vector>
12
17
18namespace lintel {
23 : public IAuthService
24 , public PropertyRegistration<AuthService>
25 , public std::enable_shared_from_this<AuthService>
26{
27private:
28 // properties
29 std::shared_ptr<Property<std::chrono::seconds>> m_scheduleRate;
30 std::shared_ptr<Property<std::chrono::minutes>> m_timeoutLogin;
31 std::shared_ptr<Property<int32_t>> m_maxLoginFailures;
32 std::shared_ptr<Property<std::chrono::seconds>> m_loginLockout;
33 std::shared_ptr<Property<int32_t>> m_maxLoginAttempts;
34 std::shared_ptr<Property<std::chrono::seconds>> m_rateLimitWindow;
35 // variables
36 std::map<std::string, UserToken> m_userTokens;
37 std::mutex m_mutex;
39 struct FailedAttempt
40 {
41 std::deque<std::chrono::steady_clock::time_point> m_timestamps;
42 };
43 std::map<std::string, FailedAttempt> m_failedAttempts;
45 std::map<std::string, std::deque<std::chrono::steady_clock::time_point>> m_loginAttempts;
46 std::shared_ptr<UserRepository> m_userRepository;
47 std::shared_ptr<SchedulerService> m_scheduler;
48 std::atomic_bool m_running = true;
49
51 void onCheck();
52
54 bool isLockedOut(const std::string &ipAddress);
55
57 void recordFailedLogin(const std::string &ipAddress);
58
60 void clearFailedLogins(const std::string &ipAddress);
61
63 bool allowLoginAttempt(const std::string &ipAddress);
64
66 void pruneLoginAttempts();
67
69 void pruneFailedLogins();
70
71public:
78 AuthService(const std::shared_ptr<ProcessName> &processName,
79 std::shared_ptr<UserRepository> userRepository,
80 std::shared_ptr<SchedulerService> schedulerService);
81
82 virtual ~AuthService() = default;
83
89 std::optional<UserToken> onLoginOf(const UserLogin &userLogin);
90
96 std::optional<UserToken> onAccessOf(const UserTokenLogin &userTokenLogin);
97
102 void onLogoutOf(const UserTokenLogin &userToken) override;
103
109 std::vector<UserToken> allTokensOf(const std::string &userName) override;
110
115 void revokeTokenOf(const std::string &id) override;
116
117 void onInitialize() override;
118
119 void onShutdown() override;
120};
121
122}// namespace lintel
123#endif// LINTEL_AUTHSERVICE_H
Definition AuthService.h:26
void onLogoutOf(const UserTokenLogin &userToken) override
void onShutdown() override
std::optional< UserToken > onAccessOf(const UserTokenLogin &userTokenLogin)
void revokeTokenOf(const std::string &id) override
std::vector< UserToken > allTokensOf(const std::string &userName) override
void onInitialize() override
virtual ~AuthService()=default
AuthService(const std::shared_ptr< ProcessName > &processName, std::shared_ptr< UserRepository > userRepository, std::shared_ptr< SchedulerService > schedulerService)
std::optional< UserToken > onLoginOf(const UserLogin &userLogin)
Definition IAuthService.h:41
Definition PropertyRegistration.h:14
Definition BaseFeature.h:6
constexpr std::string_view type_name()
Definition TypeName.h:9
Definition IAuthService.h:15
Definition IAuthService.h:23