mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-04-27 07:48:01 -07:00
Added token generation, user activation command and response.
This commit is contained in:
parent
42796b0d0e
commit
ff1aed717e
19 changed files with 223 additions and 19 deletions
|
|
@ -70,3 +70,8 @@ QString PasswordHasher::generateRandomSalt(const int len)
|
|||
|
||||
return ret;
|
||||
}
|
||||
|
||||
QString PasswordHasher::generateActivationToken()
|
||||
{
|
||||
return QCryptographicHash::hash(generateRandomSalt().toUtf8(), QCryptographicHash::Md5).toBase64().left(16);
|
||||
}
|
||||
|
|
@ -8,6 +8,7 @@ public:
|
|||
static void initialize();
|
||||
static QString computeHash(const QString &password, const QString &salt);
|
||||
static QString generateRandomSalt(const int len = 16);
|
||||
static QString generateActivationToken();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -117,11 +117,12 @@ bool Servatrice_DatabaseInterface::registerUser(const QString &userName, const Q
|
|||
return false;
|
||||
|
||||
QString passwordSha512 = PasswordHasher::computeHash(password, PasswordHasher::generateRandomSalt());
|
||||
QString token = PasswordHasher::generateActivationToken();
|
||||
|
||||
QSqlQuery *query = prepareQuery("insert into {prefix}_users "
|
||||
"(name, realname, gender, password_sha512, email, country, registrationDate, active) "
|
||||
"(name, realname, gender, password_sha512, email, country, registrationDate, active, token) "
|
||||
"values "
|
||||
"(:userName, :realName, :gender, :password_sha512, :email, :country, UTC_TIMESTAMP(), :active)");
|
||||
"(:userName, :realName, :gender, :password_sha512, :email, :country, UTC_TIMESTAMP(), :active, :token)");
|
||||
query->bindValue(":userName", userName);
|
||||
query->bindValue(":realName", realName);
|
||||
query->bindValue(":gender", getGenderChar(gender));
|
||||
|
|
@ -129,6 +130,7 @@ bool Servatrice_DatabaseInterface::registerUser(const QString &userName, const Q
|
|||
query->bindValue(":email", emailAddress);
|
||||
query->bindValue(":country", country);
|
||||
query->bindValue(":active", active ? 1 : 0);
|
||||
query->bindValue(":token", token);
|
||||
|
||||
if (!execSqlQuery(query)) {
|
||||
qDebug() << "Failed to insert user: " << query->lastError() << " sql: " << query->lastQuery();
|
||||
|
|
@ -138,6 +140,40 @@ bool Servatrice_DatabaseInterface::registerUser(const QString &userName, const Q
|
|||
return true;
|
||||
}
|
||||
|
||||
bool Servatrice_DatabaseInterface::activateUser(const QString &userName, const QString &token)
|
||||
{
|
||||
if (!checkSql())
|
||||
return false;
|
||||
|
||||
QSqlQuery *activateQuery = prepareQuery("select name from {prefix}_users where active=0 and name=:username and token=:token");
|
||||
|
||||
activateQuery->bindValue(":username", userName);
|
||||
activateQuery->bindValue(":token", token);
|
||||
if (!execSqlQuery(activateQuery)) {
|
||||
qDebug() << "Account activation failed: SQL error." << activateQuery->lastError()<< " sql: " << activateQuery->lastQuery();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (activateQuery->next()) {
|
||||
const QString name = activateQuery->value(0).toString();
|
||||
// redundant check
|
||||
if(name == userName)
|
||||
{
|
||||
|
||||
QSqlQuery *query = prepareQuery("update {prefix}_users set active=1 where name = :userName");
|
||||
query->bindValue(":userName", userName);
|
||||
|
||||
if (!execSqlQuery(query)) {
|
||||
qDebug() << "Failed to activate user: " << query->lastError() << " sql: " << query->lastQuery();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
QChar Servatrice_DatabaseInterface::getGenderChar(ServerInfo_User_Gender const &gender)
|
||||
{
|
||||
switch (gender) {
|
||||
|
|
|
|||
|
|
@ -65,6 +65,7 @@ public:
|
|||
|
||||
bool getRequireRegistration();
|
||||
bool registerUser(const QString &userName, const QString &realName, ServerInfo_User_Gender const &gender, const QString &password, const QString &emailAddress, const QString &country, bool active = false);
|
||||
bool activateUser(const QString &userName, const QString &token);
|
||||
|
||||
void logMessage(const int senderId, const QString &senderName, const QString &senderIp, const QString &logMessage, LogMessage_TargetType targetType, const int targetId, const QString &targetName);
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue