More work

* Refactored code out of common/ into servatrice/
 * added smtp client library
 * disable registration when connected
 * validate email address
 * send activation token via email
This commit is contained in:
Fabio Bas 2015-05-24 23:02:51 +02:00
parent 8cf4461616
commit 471f6371b5
38 changed files with 2699 additions and 146 deletions

View file

@ -175,64 +175,6 @@ AuthenticationResult Server::loginUser(Server_ProtocolHandler *session, QString
return authState;
}
RegistrationResult Server::registerUserAccount(const QString &ipAddress, const Command_Register &cmd, QString &banReason, int &banSecondsRemaining)
{
if (!registrationEnabled)
return RegistrationDisabled;
QString emailAddress = QString::fromStdString(cmd.email());
if (requireEmailForRegistration && emailAddress.isEmpty())
return EmailRequired;
Server_DatabaseInterface *databaseInterface = getDatabaseInterface();
// TODO: Move this method outside of the db interface
QString userName = QString::fromStdString(cmd.user_name());
if (!databaseInterface->usernameIsValid(userName))
return InvalidUsername;
if(databaseInterface->userExists(userName))
return UserAlreadyExists;
if (databaseInterface->checkUserIsBanned(ipAddress, userName, banReason, banSecondsRemaining))
return ClientIsBanned;
if (tooManyRegistrationAttempts(ipAddress))
return TooManyRequests;
QString realName = QString::fromStdString(cmd.real_name());
ServerInfo_User_Gender gender = cmd.gender();
QString country = QString::fromStdString(cmd.country());
QString password = QString::fromStdString(cmd.password());
if(password.length() < 6)
return PasswordTooShort;
bool regSucceeded = databaseInterface->registerUser(userName, realName, gender, password, emailAddress, country, !requireEmailForRegistration);
if(regSucceeded)
return requireEmailForRegistration ? AcceptedNeedsActivation : Accepted;
else
return Failed;
}
bool Server::activateUserAccount(const Command_Activate &cmd)
{
QString userName = QString::fromStdString(cmd.user_name());
QString token = QString::fromStdString(cmd.token());
Server_DatabaseInterface *databaseInterface = getDatabaseInterface();
return databaseInterface->activateUser(userName, token);
}
bool Server::tooManyRegistrationAttempts(const QString &ipAddress)
{
// TODO: implement
Q_UNUSED(ipAddress);
return false;
}
void Server::addPersistentPlayer(const QString &userName, int roomId, int gameId, int playerId)
{
QWriteLocker locker(&persistentPlayersLock);

View file

@ -29,7 +29,6 @@ class CommandContainer;
class Command_JoinGame;
enum AuthenticationResult { NotLoggedIn, PasswordRight, UnknownUser, WouldOverwriteOldSession, UserIsBanned, UsernameInvalid, RegistrationRequired, UserIsInactive };
enum RegistrationResult { Accepted, UserAlreadyExists, EmailRequired, TooManyRequests, InvalidUsername, ClientIsBanned, RegistrationDisabled, Failed, PasswordTooShort, AcceptedNeedsActivation };
class Server : public QObject
{
@ -47,19 +46,6 @@ public:
void setThreaded(bool _threaded) { threaded = _threaded; }
AuthenticationResult loginUser(Server_ProtocolHandler *session, QString &name, const QString &password, QString &reason, int &secondsLeft);
/**
* Registers a user account.
* @param ipAddress The address of the connection from the user
* @param userName The username to attempt to register
* @param emailAddress The email address to associate with the new account (and to use for activation)
* @param banReason If the client is banned, the reason for the ban will be included in this string.
* @param banSecondsRemaining If the client is banned, the time left will be included in this. 0 if the ban is permanent.
* @return RegistrationResult member indicating whether it succeeded or failed.
*/
RegistrationResult registerUserAccount(const QString &ipAddress, const Command_Register &cmd, QString &banReason, int &banSecondsRemaining);
bool activateUserAccount(const Command_Activate &cmd);
bool tooManyRegistrationAttempts(const QString &ipAddress);
const QMap<int, Server_Room *> &getRooms() { return rooms; }
Server_AbstractUserInterface *findUser(const QString &userName) const;
@ -131,9 +117,6 @@ protected:
int getUsersCount() const;
int getGamesCount() const;
void addRoom(Server_Room *newRoom);
bool registrationEnabled;
bool requireEmailForRegistration;
};
#endif

View file

@ -9,7 +9,6 @@
#include "pb/commands.pb.h"
#include "pb/response.pb.h"
#include "pb/response_login.pb.h"
#include "pb/response_register.pb.h"
#include "pb/response_list_users.pb.h"
#include "pb/response_get_games_of_user.pb.h"
#include "pb/response_get_user_info.pb.h"
@ -145,8 +144,6 @@ Response::ResponseCode Server_ProtocolHandler::processSessionCommandContainer(co
switch ((SessionCommand::SessionCommandType) num) {
case SessionCommand::PING: resp = cmdPing(sc.GetExtension(Command_Ping::ext), rc); break;
case SessionCommand::LOGIN: resp = cmdLogin(sc.GetExtension(Command_Login::ext), rc); break;
case SessionCommand::REGISTER: resp = cmdRegisterAccount(sc.GetExtension(Command_Register::ext), rc); break;
case SessionCommand::ACTIVATE: resp = cmdActivateAccount(sc.GetExtension(Command_Activate::ext), rc); break;
case SessionCommand::MESSAGE: resp = cmdMessage(sc.GetExtension(Command_Message::ext), rc); break;
case SessionCommand::GET_GAMES_OF_USER: resp = cmdGetGamesOfUser(sc.GetExtension(Command_GetGamesOfUser::ext), rc); break;
case SessionCommand::GET_USER_INFO: resp = cmdGetUserInfo(sc.GetExtension(Command_GetUserInfo::ext), rc); break;
@ -421,64 +418,6 @@ Response::ResponseCode Server_ProtocolHandler::cmdLogin(const Command_Login &cmd
return Response::RespOk;
}
Response::ResponseCode Server_ProtocolHandler::cmdRegisterAccount(const Command_Register &cmd, ResponseContainer &rc)
{
qDebug() << "Got register command: " << QString::fromStdString(cmd.user_name());
QString banReason;
int banSecondsRemaining;
RegistrationResult result =
server->registerUserAccount(
this->getAddress(),
cmd,
banReason,
banSecondsRemaining);
qDebug() << "Register command result:" << result;
switch (result) {
case RegistrationDisabled:
return Response::RespRegistrationDisabled;
case Accepted:
return Response::RespRegistrationAccepted;
case AcceptedNeedsActivation:
// TODO SEND EMAIL WITH TOKEN TO THE USER
return Response::RespRegistrationAcceptedNeedsActivation;
case UserAlreadyExists:
return Response::RespUserAlreadyExists;
case EmailRequired:
return Response::RespEmailRequiredToRegister;
case TooManyRequests:
return Response::RespTooManyRequests;
case PasswordTooShort:
return Response::RespPasswordTooShort;
case InvalidUsername:
return Response::RespUsernameInvalid;
case Failed:
return Response::RespRegistrationFailed;
case ClientIsBanned:
Response_Register *re = new Response_Register;
re->set_denied_reason_str(banReason.toStdString());
if (banSecondsRemaining != 0)
re->set_denied_end_time(QDateTime::currentDateTime().addSecs(banSecondsRemaining).toTime_t());
rc.setResponseExtension(re);
return Response::RespUserIsBanned;
}
return Response::RespInvalidCommand;
}
Response::ResponseCode Server_ProtocolHandler::cmdActivateAccount(const Command_Activate &cmd, ResponseContainer & /*rc*/)
{
if(server->activateUserAccount(cmd))
{
qDebug() << "Accepted activation for user" << QString::fromStdString(cmd.user_name());
return Response::RespActivationAccepted;
} else {
qDebug() << "Failed activation for user" << QString::fromStdString(cmd.user_name());
return Response::RespActivationFailed;
}
}
Response::ResponseCode Server_ProtocolHandler::cmdMessage(const Command_Message &cmd, ResponseContainer &rc)
{
if (authState == NotLoggedIn)

View file

@ -60,8 +60,6 @@ private:
Response::ResponseCode cmdPing(const Command_Ping &cmd, ResponseContainer &rc);
Response::ResponseCode cmdLogin(const Command_Login &cmd, ResponseContainer &rc);
Response::ResponseCode cmdRegisterAccount(const Command_Register &cmd, ResponseContainer &rc);
Response::ResponseCode cmdActivateAccount(const Command_Activate &cmd, ResponseContainer & /* rc */);
Response::ResponseCode cmdMessage(const Command_Message &cmd, ResponseContainer &rc);
Response::ResponseCode cmdGetGamesOfUser(const Command_GetGamesOfUser &cmd, ResponseContainer &rc);
Response::ResponseCode cmdGetUserInfo(const Command_GetUserInfo &cmd, ResponseContainer &rc);
@ -101,6 +99,7 @@ public:
void sendProtocolItem(const SessionEvent &item);
void sendProtocolItem(const GameEventContainer &item);
void sendProtocolItem(const RoomEvent &item);
};
#endif