R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
AuthService.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** Created by hugo on 06/12/2025
4** File description:
5** AuthService.hpp
6*/
7
8#pragma once
9
10#include <memory>
11#include <string>
12#include <unordered_map>
13#include <unordered_set>
16
17namespace server {
18
19 struct AccountData {
20 std::string username;
21 std::string passwordHash; // Argon2id hash
22 uint64_t createdAt; // timestamp
23 uint64_t lastLogin; // timestamp
24 bool autoMatchmaking{false}; // Auto-matchmaking preference
25 };
26
27 class AuthService : public IAuthService {
28 public:
30 explicit AuthService(const std::string &accountsFile);
31 ~AuthService() override;
32
39 bool authenticate(const std::string &username, const std::string &password) override;
40
46 std::string generateToken(const std::string &username);
47
53 bool validateToken(const std::string &token);
54
59 void revokeToken(const std::string &token);
60
66 bool isUserAuthenticated(const std::string &username) const;
67
74 bool registerUser(const std::string &username, const std::string &password);
75
82 bool updateAutoMatchmaking(const std::string &username, bool enabled);
83
89 bool getAutoMatchmaking(const std::string &username) const;
90
94 void loadAccounts();
95
99 void saveAccounts();
100
101 private:
102 std::string _accountsFile = "accounts.json";
103 std::unique_ptr<IPasswordHasher> _passwordHasher;
104 std::unordered_set<std::string> _authenticatedUsers;
105 std::unordered_map<std::string, std::string> _activeTokens;
106 std::unordered_map<std::string, AccountData> _accounts;
107
108 bool _accountsDirty = false;
109 uint64_t _lastSaveTime = 0;
110 static constexpr uint64_t SAVE_INTERVAL_SECONDS = 60;
111 };
112
113} // namespace server
std::unordered_map< std::string, AccountData > _accounts
Map of username to account data.
std::unique_ptr< IPasswordHasher > _passwordHasher
Password hashing implementation.
bool validateToken(const std::string &token)
Validate a token.
bool getAutoMatchmaking(const std::string &username) const
Get auto-matchmaking preference for a user.
std::unordered_map< std::string, std::string > _activeTokens
Map of tokens to usernames.
void loadAccounts()
Load user accounts from JSON file.
std::unordered_set< std::string > _authenticatedUsers
Set of authenticated usernames.
static constexpr uint64_t SAVE_INTERVAL_SECONDS
Save every 60 seconds.
bool isUserAuthenticated(const std::string &username) const
Check if a user is authenticated.
std::string generateToken(const std::string &username)
Generate an authentication token for a user.
bool registerUser(const std::string &username, const std::string &password)
Register a new user account.
void saveAccounts()
Save user accounts to JSON file.
uint64_t _lastSaveTime
Timestamp of last save.
~AuthService() override
std::string _accountsFile
JSON file to store accounts.
bool _accountsDirty
Flag indicating unsaved changes.
bool updateAutoMatchmaking(const std::string &username, bool enabled)
Update auto-matchmaking preference for a user.
bool authenticate(const std::string &username, const std::string &password) override
Authenticate a user with username and password.
void revokeToken(const std::string &token)
Revoke a token.
std::string passwordHash