Added token generation, user activation command and response.

This commit is contained in:
Fabio Bas 2015-05-24 00:37:45 +02:00
parent 42796b0d0e
commit ff1aed717e
19 changed files with 223 additions and 19 deletions

View file

@ -30,6 +30,7 @@ enum ClientStatus {
StatusDisconnecting,
StatusConnecting,
StatusRegistering,
StatusActivating,
StatusLoggingIn,
StatusLoggedIn,
};
@ -60,6 +61,8 @@ signals:
void ignoreListReceived(const QList<ServerInfo_User> &ignoreList);
void replayAddedEventReceived(const Event_ReplayAdded &event);
void registerAccepted();
void registerAcceptedNeedsActivate();
void activateAccepted();
void sigQueuePendingCommand(PendingCommand *pend);
private:
@ -72,7 +75,7 @@ protected slots:
void processProtocolItem(const ServerMessage &item);
protected:
QMap<int, PendingCommand *> pendingCommands;
QString userName, password, email, country, realName;
QString userName, password, email, country, realName, token;
int gender;
void setStatus(ClientStatus _status);
int getNewCmdId() { return nextCmdId++; }

View file

@ -7,6 +7,7 @@
#include "pb/session_commands.pb.h"
#include "pb/response_login.pb.h"
#include "pb/response_register.pb.h"
#include "pb/response_activate.pb.h"
#include "pb/server_message.pb.h"
#include "pb/event_server_identification.pb.h"
@ -30,6 +31,7 @@ RemoteClient::RemoteClient(QObject *parent)
connect(this, SIGNAL(sigConnectToServer(QString, unsigned int, QString, QString)), this, SLOT(doConnectToServer(QString, unsigned int, QString, QString)));
connect(this, SIGNAL(sigDisconnectFromServer()), this, SLOT(doDisconnectFromServer()));
connect(this, SIGNAL(sigRegisterToServer(QString, unsigned int, QString, QString, QString, int, QString, QString)), this, SLOT(doRegisterToServer(QString, unsigned int, QString, QString, QString, int, QString, QString)));
connect(this, SIGNAL(sigActivateToServer(QString)), this, SLOT(doActivateToServer(QString)));
}
RemoteClient::~RemoteClient()
@ -81,6 +83,24 @@ void RemoteClient::processServerIdentificationEvent(const Event_ServerIdentifica
return;
}
if(getStatus() == StatusActivating)
{
Command_Activate cmdActivate;
cmdActivate.set_user_name(userName.toStdString());
cmdActivate.set_token(token.toStdString());
PendingCommand *pend = prepareSessionCommand(cmdActivate);
connect(pend, SIGNAL(finished(Response, CommandContainer, QVariant)), this, SLOT(activateResponse(Response)));
sendCommand(pend);
return;
}
doLogin();
}
void RemoteClient::doLogin()
{
setStatus(StatusLoggingIn);
Command_Login cmdLogin;
@ -122,13 +142,34 @@ void RemoteClient::loginResponse(const Response &response)
void RemoteClient::registerResponse(const Response &response)
{
const Response_Register &resp = response.GetExtension(Response_Register::ext);
if (response.response_code() == Response::RespRegistrationAccepted) {
emit registerAccepted();
} else {
emit registerError(response.response_code(), QString::fromStdString(resp.denied_reason_str()), resp.denied_end_time());
switch(response.response_code())
{
case Response::RespRegistrationAccepted:
emit registerAccepted();
doLogin();
break;
case Response::RespRegistrationAcceptedNeedsActivation:
emit registerAcceptedNeedsActivate();
doLogin();
break;
default:
emit registerError(response.response_code(), QString::fromStdString(resp.denied_reason_str()), resp.denied_end_time());
setStatus(StatusDisconnecting);
doDisconnectFromServer();
break;
}
}
void RemoteClient::activateResponse(const Response &response)
{
const Response_Activate &resp = response.GetExtension(Response_Activate::ext);
if (response.response_code() == Response::RespActivationAccepted) {
emit activateAccepted();
doLogin();
} else {
emit activateError();
}
setStatus(StatusDisconnecting);
doDisconnectFromServer();
}
void RemoteClient::readData()
@ -201,6 +242,9 @@ void RemoteClient::doConnectToServer(const QString &hostname, unsigned int port,
userName = _userName;
password = _password;
lastHostname = hostname;
lastPort = port;
socket->connectToHost(hostname, port);
setStatus(StatusConnecting);
}
@ -215,11 +259,23 @@ void RemoteClient::doRegisterToServer(const QString &hostname, unsigned int port
gender = _gender;
country = _country;
realName = _realname;
lastHostname = hostname;
lastPort = port;
socket->connectToHost(hostname, port);
setStatus(StatusRegistering);
}
void RemoteClient::doActivateToServer(const QString &_token)
{
doDisconnectFromServer();
token = _token;
socket->connectToHost(lastHostname, lastPort);
setStatus(StatusActivating);
}
void RemoteClient::doDisconnectFromServer()
{
timer->stop();
@ -275,6 +331,11 @@ void RemoteClient::registerToServer(const QString &hostname, unsigned int port,
emit sigRegisterToServer(hostname, port, _userName, _password, _email, _gender, _country, _realname);
}
void RemoteClient::activateToServer(const QString &_token)
{
emit sigActivateToServer(_token);
}
void RemoteClient::disconnectFromServer()
{
emit sigDisconnectFromServer();

View file

@ -13,11 +13,13 @@ signals:
void serverTimeout();
void loginError(Response::ResponseCode resp, QString reasonStr, quint32 endTime);
void registerError(Response::ResponseCode resp, QString reasonStr, quint32 endTime);
void activateError();
void socketError(const QString &errorString);
void protocolVersionMismatch(int clientVersion, int serverVersion);
void protocolError();
void sigConnectToServer(const QString &hostname, unsigned int port, const QString &_userName, const QString &_password);
void sigRegisterToServer(const QString &hostname, unsigned int port, const QString &_userName, const QString &_password, const QString &_email, const int _gender, const QString &_country, const QString &_realname);
void sigActivateToServer(const QString &_token);
void sigDisconnectFromServer();
private slots:
void slotConnected();
@ -28,9 +30,13 @@ private slots:
void processConnectionClosedEvent(const Event_ConnectionClosed &event);
void loginResponse(const Response &response);
void registerResponse(const Response &response);
void activateResponse(const Response &response);
void doConnectToServer(const QString &hostname, unsigned int port, const QString &_userName, const QString &_password);
void doRegisterToServer(const QString &hostname, unsigned int port, const QString &_userName, const QString &_password, const QString &_email, const int _gender, const QString &_country, const QString &_realname);
void doLogin();
void doDisconnectFromServer();
void doActivateToServer(const QString &_token);
private:
static const int maxTimeout = 10;
int timeRunning, lastDataReceived;
@ -42,6 +48,8 @@ private:
QTimer *timer;
QTcpSocket *socket;
QString lastHostname;
int lastPort;
protected slots:
void sendCommandContainer(const CommandContainer &cont);
public:
@ -50,7 +58,7 @@ public:
QString peerName() const { return socket->peerName(); }
void connectToServer(const QString &hostname, unsigned int port, const QString &_userName, const QString &_password);
void registerToServer(const QString &hostname, unsigned int port, const QString &_userName, const QString &_password, const QString &_email, const int _gender, const QString &_country, const QString &_realname);
void activateToServer(const QString &_token);
void disconnectFromServer();
};

View file

@ -126,8 +126,17 @@ void MainWindow::userInfoReceived(const ServerInfo_User &info)
void MainWindow::registerAccepted()
{
QMessageBox::information(this, tr("Success"), tr("Registration accepted.\nNow check your email for instructions on how to activate your account."));
actConnect();
QMessageBox::information(this, tr("Success"), tr("Registration accepted.\nWill now login."));
}
void MainWindow::registerAcceptedNeedsActivate()
{
// nothing
}
void MainWindow::activateAccepted()
{
QMessageBox::information(this, tr("Success"), tr("Account activation accepted.\nWill now login."));
}
// Actions
@ -299,11 +308,20 @@ void MainWindow::loginError(Response::ResponseCode r, QString reasonStr, quint32
actRegister();
}
break;
case Response::RespAccountNotActivated:
QMessageBox::critical(this, tr("Error"), tr("Your account has not been activated yet."));
case Response::RespAccountNotActivated: {
bool ok = false;
QString token = QInputDialog::getText(this, tr("Account activation"), tr("Your account has not been activated yet.\n You need to provide the activation token received in the activation email"), QLineEdit::Normal, QString(), &ok);
if(ok && !token.isEmpty())
{
client->activateToServer(token);
return;
}
client->disconnectFromServer();
break;
}
default:
QMessageBox::critical(this, tr("Error"), tr("Unknown login error: %1").arg(static_cast<int>(r)));
break;
}
actConnect();
}
@ -350,6 +368,13 @@ void MainWindow::registerError(Response::ResponseCode r, QString reasonStr, quin
actRegister();
}
void MainWindow::activateError()
{
QMessageBox::critical(this, tr("Error"), tr("Account activation failed"));
client->disconnectFromServer();
actConnect();
}
void MainWindow::socketError(const QString &errorStr)
{
QMessageBox::critical(this, tr("Error"), tr("Socket error: %1").arg(errorStr));
@ -488,7 +513,10 @@ MainWindow::MainWindow(QWidget *parent)
connect(client, SIGNAL(userInfoChanged(const ServerInfo_User &)), this, SLOT(userInfoReceived(const ServerInfo_User &)), Qt::BlockingQueuedConnection);
connect(client, SIGNAL(registerAccepted()), this, SLOT(registerAccepted()));
connect(client, SIGNAL(registerAcceptedNeedsActivate()), this, SLOT(registerAcceptedNeedsActivate()));
connect(client, SIGNAL(registerError(Response::ResponseCode, QString, quint32)), this, SLOT(registerError(Response::ResponseCode, QString, quint32)));
connect(client, SIGNAL(activateAccepted()), this, SLOT(activateAccepted()));
connect(client, SIGNAL(activateError()), this, SLOT(activateError()));
clientThread = new QThread(this);
client->moveToThread(clientThread);

View file

@ -44,10 +44,13 @@ private slots:
void serverTimeout();
void loginError(Response::ResponseCode r, QString reasonStr, quint32 endTime);
void registerError(Response::ResponseCode r, QString reasonStr, quint32 endTime);
void activateError();
void socketError(const QString &errorStr);
void protocolVersionMismatch(int localVersion, int remoteVersion);
void userInfoReceived(const ServerInfo_User &userInfo);
void registerAccepted();
void registerAcceptedNeedsActivate();
void activateAccepted();
void localGameEnded();
void pixmapCacheSizeChanged(int newSizeInMBs);