|
R-Type
Distributed multiplayer game engine in C++
|
#include <AuthService.hpp>


Public Member Functions | |
| AuthService () | |
| AuthService (const std::string &accountsFile) | |
| ~AuthService () override | |
| bool | authenticate (const std::string &username, const std::string &password) override |
| Authenticate a user with username and password. | |
| std::string | generateToken (const std::string &username) |
| Generate an authentication token for a user. | |
| bool | validateToken (const std::string &token) |
| Validate a token. | |
| void | revokeToken (const std::string &token) |
| Revoke a token. | |
| bool | isUserAuthenticated (const std::string &username) const |
| Check if a user is authenticated. | |
| bool | registerUser (const std::string &username, const std::string &password) |
| Register a new user account. | |
| bool | updateAutoMatchmaking (const std::string &username, bool enabled) |
| Update auto-matchmaking preference for a user. | |
| bool | getAutoMatchmaking (const std::string &username) const |
| Get auto-matchmaking preference for a user. | |
| void | loadAccounts () |
| Load user accounts from JSON file. | |
| void | saveAccounts () |
| Save user accounts to JSON file. | |
Public Member Functions inherited from server::IAuthService | |
| virtual | ~IAuthService ()=default |
Private Attributes | |
| std::string | _accountsFile = "accounts.json" |
| JSON file to store accounts. | |
| std::unique_ptr< IPasswordHasher > | _passwordHasher |
| Password hashing implementation. | |
| std::unordered_set< std::string > | _authenticatedUsers |
| Set of authenticated usernames. | |
| std::unordered_map< std::string, std::string > | _activeTokens |
| Map of tokens to usernames. | |
| std::unordered_map< std::string, AccountData > | _accounts |
| Map of username to account data. | |
| bool | _accountsDirty = false |
| Flag indicating unsaved changes. | |
| uint64_t | _lastSaveTime = 0 |
| Timestamp of last save. | |
Static Private Attributes | |
| static constexpr uint64_t | SAVE_INTERVAL_SECONDS = 60 |
| Save every 60 seconds. | |
Definition at line 27 of file AuthService.hpp.
| server::AuthService::AuthService | ( | ) |
Definition at line 23 of file AuthService.cpp.
References loadAccounts().

|
explicit |
Definition at line 27 of file AuthService.cpp.
References loadAccounts().

|
override |
Definition at line 32 of file AuthService.cpp.
References _accountsDirty, and saveAccounts().

|
overridevirtual |
Authenticate a user with username and password.
| username | The username |
| password | The password (plaintext, will be verified against hash) |
Implements server::IAuthService.
Definition at line 38 of file AuthService.cpp.
References _accounts, _accountsDirty, _authenticatedUsers, _lastSaveTime, _passwordHasher, LOG_INFO, LOG_WARNING, SAVE_INTERVAL_SECONDS, and saveAccounts().

| std::string server::AuthService::generateToken | ( | const std::string & | username | ) |
Generate an authentication token for a user.
| username | The username |
Definition at line 91 of file AuthService.cpp.
References _activeTokens.
| bool server::AuthService::getAutoMatchmaking | ( | const std::string & | username | ) | const |
Get auto-matchmaking preference for a user.
| username | The username |
Definition at line 285 of file AuthService.cpp.
References _accounts.
| bool server::AuthService::isUserAuthenticated | ( | const std::string & | username | ) | const |
Check if a user is authenticated.
| username | The username to check |
Definition at line 119 of file AuthService.cpp.
References _authenticatedUsers.
| void server::AuthService::loadAccounts | ( | ) |
Load user accounts from JSON file.
Definition at line 180 of file AuthService.cpp.
References _accounts, _accountsFile, server::AccountData::autoMatchmaking, server::AccountData::createdAt, server::AccountData::lastLogin, LOG_ERROR, LOG_INFO, LOG_WARNING, server::AccountData::passwordHash, and server::AccountData::username.
Referenced by AuthService(), and AuthService().
| bool server::AuthService::registerUser | ( | const std::string & | username, |
| const std::string & | password | ||
| ) |
Register a new user account.
| username | The username |
| password | The password (plaintext, will be hashed) |
Definition at line 123 of file AuthService.cpp.
References _accounts, _accountsDirty, _passwordHasher, server::AccountData::createdAt, server::AccountData::lastLogin, LOG_ERROR, LOG_INFO, LOG_WARNING, server::AccountData::passwordHash, saveAccounts(), and server::AccountData::username.

| void server::AuthService::revokeToken | ( | const std::string & | token | ) |
Revoke a token.
| token | The token to revoke |
Definition at line 112 of file AuthService.cpp.
References _activeTokens.
| void server::AuthService::saveAccounts | ( | ) |
Save user accounts to JSON file.
Definition at line 232 of file AuthService.cpp.
References _accounts, _accountsFile, LOG_ERROR, and LOG_INFO.
Referenced by authenticate(), registerUser(), updateAutoMatchmaking(), and ~AuthService().
| bool server::AuthService::updateAutoMatchmaking | ( | const std::string & | username, |
| bool | enabled | ||
| ) |
Update auto-matchmaking preference for a user.
| username | The username |
| enabled | Whether auto-matchmaking should be enabled |
Definition at line 265 of file AuthService.cpp.
References _accounts, _accountsDirty, _lastSaveTime, LOG_INFO, LOG_WARNING, and saveAccounts().

| bool server::AuthService::validateToken | ( | const std::string & | token | ) |
Validate a token.
| token | The token to validate |
Definition at line 108 of file AuthService.cpp.
References _activeTokens.
|
private |
Map of username to account data.
Definition at line 106 of file AuthService.hpp.
Referenced by authenticate(), getAutoMatchmaking(), loadAccounts(), registerUser(), saveAccounts(), and updateAutoMatchmaking().
|
private |
Flag indicating unsaved changes.
Definition at line 108 of file AuthService.hpp.
Referenced by authenticate(), registerUser(), updateAutoMatchmaking(), and ~AuthService().
|
private |
JSON file to store accounts.
Definition at line 102 of file AuthService.hpp.
Referenced by loadAccounts(), and saveAccounts().
|
private |
Map of tokens to usernames.
Definition at line 105 of file AuthService.hpp.
Referenced by generateToken(), revokeToken(), and validateToken().
|
private |
Set of authenticated usernames.
Definition at line 104 of file AuthService.hpp.
Referenced by authenticate(), and isUserAuthenticated().
|
private |
Timestamp of last save.
Definition at line 109 of file AuthService.hpp.
Referenced by authenticate(), and updateAutoMatchmaking().
|
private |
Password hashing implementation.
Definition at line 103 of file AuthService.hpp.
Referenced by authenticate(), and registerUser().
|
staticconstexprprivate |
Save every 60 seconds.
Definition at line 110 of file AuthService.hpp.
Referenced by authenticate().