R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
server::AuthService Class Reference

#include <AuthService.hpp>

Inheritance diagram for server::AuthService:
Inheritance graph
Collaboration diagram for server::AuthService:
Collaboration graph

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.
 

Detailed Description

Definition at line 27 of file AuthService.hpp.

Constructor & Destructor Documentation

◆ AuthService() [1/2]

server::AuthService::AuthService ( )

Definition at line 23 of file AuthService.cpp.

References loadAccounts().

Here is the call graph for this function:

◆ AuthService() [2/2]

server::AuthService::AuthService ( const std::string &  accountsFile)
explicit

Definition at line 27 of file AuthService.cpp.

References loadAccounts().

Here is the call graph for this function:

◆ ~AuthService()

server::AuthService::~AuthService ( )
override

Definition at line 32 of file AuthService.cpp.

References _accountsDirty, and saveAccounts().

Here is the call graph for this function:

Member Function Documentation

◆ authenticate()

bool server::AuthService::authenticate ( const std::string &  username,
const std::string &  password 
)
overridevirtual

Authenticate a user with username and password.

Parameters
usernameThe username
passwordThe password (plaintext, will be verified against hash)
Returns
bool True if authentication succeeds

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().

Here is the call graph for this function:

◆ generateToken()

std::string server::AuthService::generateToken ( const std::string &  username)

Generate an authentication token for a user.

Parameters
usernameThe username
Returns
std::string The generated token

Definition at line 91 of file AuthService.cpp.

References _activeTokens.

◆ getAutoMatchmaking()

bool server::AuthService::getAutoMatchmaking ( const std::string &  username) const

Get auto-matchmaking preference for a user.

Parameters
usernameThe username
Returns
bool Auto-matchmaking preference (default: false)

Definition at line 285 of file AuthService.cpp.

References _accounts.

◆ isUserAuthenticated()

bool server::AuthService::isUserAuthenticated ( const std::string &  username) const

Check if a user is authenticated.

Parameters
usernameThe username to check
Returns
bool True if user is authenticated

Definition at line 119 of file AuthService.cpp.

References _authenticatedUsers.

◆ loadAccounts()

◆ registerUser()

bool server::AuthService::registerUser ( const std::string &  username,
const std::string &  password 
)

Register a new user account.

Parameters
usernameThe username
passwordThe password (plaintext, will be hashed)
Returns
bool True if registration succeeds

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.

Here is the call graph for this function:

◆ revokeToken()

void server::AuthService::revokeToken ( const std::string &  token)

Revoke a token.

Parameters
tokenThe token to revoke

Definition at line 112 of file AuthService.cpp.

References _activeTokens.

◆ saveAccounts()

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().

◆ updateAutoMatchmaking()

bool server::AuthService::updateAutoMatchmaking ( const std::string &  username,
bool  enabled 
)

Update auto-matchmaking preference for a user.

Parameters
usernameThe username
enabledWhether auto-matchmaking should be enabled
Returns
bool True if update succeeds

Definition at line 265 of file AuthService.cpp.

References _accounts, _accountsDirty, _lastSaveTime, LOG_INFO, LOG_WARNING, and saveAccounts().

Here is the call graph for this function:

◆ validateToken()

bool server::AuthService::validateToken ( const std::string &  token)

Validate a token.

Parameters
tokenThe token to validate
Returns
bool True if token is valid

Definition at line 108 of file AuthService.cpp.

References _activeTokens.

Member Data Documentation

◆ _accounts

std::unordered_map<std::string, AccountData> server::AuthService::_accounts
private

Map of username to account data.

Definition at line 106 of file AuthService.hpp.

Referenced by authenticate(), getAutoMatchmaking(), loadAccounts(), registerUser(), saveAccounts(), and updateAutoMatchmaking().

◆ _accountsDirty

bool server::AuthService::_accountsDirty = false
private

Flag indicating unsaved changes.

Definition at line 108 of file AuthService.hpp.

Referenced by authenticate(), registerUser(), updateAutoMatchmaking(), and ~AuthService().

◆ _accountsFile

std::string server::AuthService::_accountsFile = "accounts.json"
private

JSON file to store accounts.

Definition at line 102 of file AuthService.hpp.

Referenced by loadAccounts(), and saveAccounts().

◆ _activeTokens

std::unordered_map<std::string, std::string> server::AuthService::_activeTokens
private

Map of tokens to usernames.

Definition at line 105 of file AuthService.hpp.

Referenced by generateToken(), revokeToken(), and validateToken().

◆ _authenticatedUsers

std::unordered_set<std::string> server::AuthService::_authenticatedUsers
private

Set of authenticated usernames.

Definition at line 104 of file AuthService.hpp.

Referenced by authenticate(), and isUserAuthenticated().

◆ _lastSaveTime

uint64_t server::AuthService::_lastSaveTime = 0
private

Timestamp of last save.

Definition at line 109 of file AuthService.hpp.

Referenced by authenticate(), and updateAutoMatchmaking().

◆ _passwordHasher

std::unique_ptr<IPasswordHasher> server::AuthService::_passwordHasher
private

Password hashing implementation.

Definition at line 103 of file AuthService.hpp.

Referenced by authenticate(), and registerUser().

◆ SAVE_INTERVAL_SECONDS

constexpr uint64_t server::AuthService::SAVE_INTERVAL_SECONDS = 60
staticconstexprprivate

Save every 60 seconds.

Definition at line 110 of file AuthService.hpp.

Referenced by authenticate().


The documentation for this class was generated from the following files: