Skip to content
This repository has been archived by the owner on Jan 12, 2025. It is now read-only.

Commit

Permalink
Add change password, email and receive news toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
Quackster committed Jul 30, 2022
1 parent 8605ab6 commit e7086b7
Show file tree
Hide file tree
Showing 11 changed files with 335 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,38 @@ public static boolean login(PlayerDetails player, String username, String passwo
return success;
}

public static boolean login(String username, String password) {
boolean success = false;

Connection sqlConnection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;

try {
sqlConnection = Storage.getStorage().getConnection();
preparedStatement = Storage.getStorage().prepare("SELECT id, password FROM users WHERE username = ? LIMIT 1", sqlConnection);
preparedStatement.setString(1, username);
resultSet = preparedStatement.executeQuery();

if (resultSet.next()) {
byte[] hashedPassword = (resultSet.getString("password") + '\0').getBytes(StandardCharsets.UTF_8);
byte[] pass = password.getBytes(StandardCharsets.UTF_8);

PwHash.Native pwHash = (PwHash.Native) Kepler.getLibSodium();
success = pwHash.cryptoPwHashStrVerify(hashedPassword, pass, pass.length);
}

} catch (Exception e) {
Storage.logError(e);
} finally {
Storage.closeSilently(resultSet);
Storage.closeSilently(preparedStatement);
Storage.closeSilently(sqlConnection);
}

return success;
}

/**
* Clear SSO ticket
* Protects against replay attacks
Expand Down Expand Up @@ -441,6 +473,86 @@ public static void saveMotto(PlayerDetails details) {
}
}

public static void saveReceiveMail(PlayerDetails details) {
Connection sqlConnection = null;
PreparedStatement preparedStatement = null;

try {
sqlConnection = Storage.getStorage().getConnection();
preparedStatement = Storage.getStorage().prepare("UPDATE users SET receive_email = ? WHERE id = ?", sqlConnection);
preparedStatement.setBoolean(1, details.isReceiveNews());
preparedStatement.setInt(2, details.getId());
preparedStatement.execute();

} catch (Exception e) {
Storage.logError(e);
} finally {
Storage.closeSilently(preparedStatement);
Storage.closeSilently(sqlConnection);
}
}

/**
* Update details.
*/
public static void savePassword(int userId, String password) {
Connection sqlConnection = null;
PreparedStatement preparedStatement = null;

try {
sqlConnection = Storage.getStorage().getConnection();
preparedStatement = Storage.getStorage().prepare("UPDATE users SET password = ? WHERE id = ?", sqlConnection);
preparedStatement.setString(1, password);
preparedStatement.setInt(2, userId);
preparedStatement.execute();

} catch (Exception e) {
Storage.logError(e);
} finally {
Storage.closeSilently(preparedStatement);
Storage.closeSilently(sqlConnection);
}
}

public static void saveBirthday(int userId, String birthday) {
Connection sqlConnection = null;
PreparedStatement preparedStatement = null;

try {
sqlConnection = Storage.getStorage().getConnection();
preparedStatement = Storage.getStorage().prepare("UPDATE users SET birthday = ? WHERE id = ?", sqlConnection);
preparedStatement.setString(1, birthday);
preparedStatement.setInt(2, userId);
preparedStatement.execute();

} catch (Exception e) {
Storage.logError(e);
} finally {
Storage.closeSilently(preparedStatement);
Storage.closeSilently(sqlConnection);
}
}

public static void saveEmail(int userId, String email) {
Connection sqlConnection = null;
PreparedStatement preparedStatement = null;

try {
sqlConnection = Storage.getStorage().getConnection();
preparedStatement = Storage.getStorage().prepare("UPDATE users SET email = ? WHERE id = ?", sqlConnection);
preparedStatement.setString(1, email);
preparedStatement.setInt(2, userId);
preparedStatement.execute();

} catch (Exception e) {
Storage.logError(e);
} finally {
Storage.closeSilently(preparedStatement);
Storage.closeSilently(sqlConnection);
}
}


/**
* Update details.
*
Expand Down Expand Up @@ -482,10 +594,10 @@ private static void fill(PlayerDetails details, ResultSet row) throws SQLExcepti

details.fill(row.getInt("id"), row.getString("username"), row.getString("figure"),
row.getString("pool_figure"), row.getInt("credits"), row.getString("motto"),
row.getString("console_motto"), row.getString("sex"), row.getInt("tickets"),
row.getInt("film"), row.getInt("rank"), row.getLong("last_online"),
row.getLong("club_subscribed"), row.getLong("club_expiration"), row.getLong("club_gift_due"),
row.getString("badge"),
row.getString("console_motto"), row.getString("sex"), row.getString("birthday"),
row.getInt("tickets"), row.getInt("film"), row.getInt("rank"),
row.getLong("last_online"), row.getLong("club_subscribed"), row.getLong("club_expiration"),
row.getLong("club_gift_due"), row.getString("badge"),
row.getBoolean("badge_active"), row.getBoolean("allow_stalking"),
row.getBoolean("allow_friend_requests"), row.getBoolean("sound_enabled"),
row.getBoolean("tutorial_finished"), row.getInt("battleball_points"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class PlayerDetails {
private String motto;
private String consoleMotto;
private char sex;
private String birthday;
private boolean receiveNews;

// Currencies
Expand Down Expand Up @@ -80,7 +81,7 @@ public PlayerDetails() {
* @param battleballPoints the points accumulated when playing battleball
* @param snowstormPoints the points accumulated when playing snowstorm
*/
public void fill(int id, String username, String figure, String poolFigure, int credits, String motto, String consoleMotto, String sex, int tickets, int film, int rank, long lastOnline, long firstClubSubscription, long clubExpiration, long clubGiftDue, String currentBadge, boolean showBadge, boolean allowStalking, boolean allowFriendRequests, boolean soundEnabled,
public void fill(int id, String username, String figure, String poolFigure, int credits, String motto, String consoleMotto, String sex, String birthday, int tickets, int film, int rank, long lastOnline, long firstClubSubscription, long clubExpiration, long clubGiftDue, String currentBadge, boolean showBadge, boolean allowStalking, boolean allowFriendRequests, boolean soundEnabled,
boolean tutorialFinished, int battleballPoints, int snowstormPoints) {
this.id = id;
this.username = StringUtil.filterInput(username, true);
Expand All @@ -89,6 +90,7 @@ public void fill(int id, String username, String figure, String poolFigure, int
this.motto = StringUtil.filterInput(motto, true);
this.consoleMotto = StringUtil.filterInput(consoleMotto, true);
this.sex = sex.toLowerCase().equals("f") ? 'F' : 'M';
this.birthday = birthday;
this.credits = credits;
this.tickets = tickets;
this.film = film;
Expand Down Expand Up @@ -214,6 +216,7 @@ public void setMotto(String motto) {
this.motto = motto;
}


public String getConsoleMotto() {
return consoleMotto;
}
Expand Down Expand Up @@ -383,4 +386,12 @@ public boolean isReceiveNews() {
public void setReceiveNews(boolean receiveNews) {
this.receiveNews = receiveNews;
}

public String getBirthday() {
return birthday;
}

public void setBirthday(String birthday) {
this.birthday = birthday;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -313,19 +313,19 @@ public String createPassword(String password) throws Exception {
*/
public LinkedHashMap<Integer, RegisterValue> getRegisterValues() {
var registerValues = new LinkedHashMap<Integer, RegisterValue>();
registerValues.put(1, new RegisterValue("parentagree", 1, RegisterDataType.BOOLEAN));
registerValues.put(2, new RegisterValue("name", 2, RegisterDataType.STRING));
registerValues.put(3, new RegisterValue("password", 3, RegisterDataType.STRING));
registerValues.put(4, new RegisterValue("figure", 4, RegisterDataType.STRING));
registerValues.put(5, new RegisterValue("sex", 5, RegisterDataType.STRING));
registerValues.put(6, new RegisterValue("customData", 6, RegisterDataType.STRING));
registerValues.put(7, new RegisterValue("email", 7, RegisterDataType.STRING));
registerValues.put(8, new RegisterValue("birthday", 8, RegisterDataType.STRING));
registerValues.put(9, new RegisterValue("directMail", 9, RegisterDataType.BOOLEAN));
registerValues.put(10, new RegisterValue("has_read_agreement", 10, RegisterDataType.BOOLEAN));
registerValues.put(11, new RegisterValue("isp_id", 11, RegisterDataType.STRING));
registerValues.put(12, new RegisterValue("partnersite", 12, RegisterDataType.STRING));
registerValues.put(13, new RegisterValue("oldpassword", 13, RegisterDataType.STRING));
registerValues.put(1, new RegisterValue("parentagree", RegisterDataType.BOOLEAN));
registerValues.put(2, new RegisterValue("name", RegisterDataType.STRING));
registerValues.put(3, new RegisterValue("password", RegisterDataType.STRING));
registerValues.put(4, new RegisterValue("figure", RegisterDataType.STRING));
registerValues.put(5, new RegisterValue("sex", RegisterDataType.STRING));
registerValues.put(6, new RegisterValue("customData", RegisterDataType.STRING));
registerValues.put(7, new RegisterValue("email", RegisterDataType.STRING));
registerValues.put(8, new RegisterValue("birthday", RegisterDataType.STRING));
registerValues.put(9, new RegisterValue("directMail", RegisterDataType.BOOLEAN));
registerValues.put(10, new RegisterValue("has_read_agreement", RegisterDataType.BOOLEAN));
registerValues.put(11, new RegisterValue("isp_id", RegisterDataType.STRING));
registerValues.put(12, new RegisterValue("partnersite", RegisterDataType.STRING));
registerValues.put(13, new RegisterValue("oldpassword", RegisterDataType.STRING));
return registerValues;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
package org.alexdev.kepler.game.player.register;

public class RegisterValue {
private int id;
private String label;
private RegisterDataType dataType;
private String value;
private boolean flag;

public RegisterValue(String label, int id, RegisterDataType dataType) {
public RegisterValue(String label, RegisterDataType dataType) {
this.label = label;
this.id = id;
this.dataType = dataType;
}

public RegisterDataType getDataType() {
return dataType;
}

public int getId() {
return id;
}

public boolean getFlag() {
return flag;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.alexdev.kepler.messages.incoming.user.*;
import org.alexdev.kepler.messages.incoming.user.settings.GET_ACCOUNT_PREFERENCES;
import org.alexdev.kepler.messages.incoming.user.settings.GET_SOUND_SETTING;
import org.alexdev.kepler.messages.incoming.user.settings.UPDATE_ACCOUNT;
import org.alexdev.kepler.messages.incoming.welcomingparty.ACCEPT_TUTOR_INVITATION;
import org.alexdev.kepler.messages.incoming.welcomingparty.REJECT_TUTOR_INVITATION;
import org.alexdev.kepler.messages.incoming.wobblesquabble.PTM;
Expand Down Expand Up @@ -158,6 +159,7 @@ private void registerUserPackets() {
registerEvent(322, new UNIGNORE_USER());
registerEvent(228, new GET_SOUND_SETTING());
registerEvent(9, new GETAVAILABLESETS());
registerEvent(149, new UPDATE_ACCOUNT());
//registerEvent(315, new TEST_LATENCY());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import org.alexdev.kepler.dao.mysql.PlayerDao;
import org.alexdev.kepler.game.player.Player;
import org.alexdev.kepler.game.player.PlayerManager;
import org.alexdev.kepler.messages.types.MessageEvent;
import org.alexdev.kepler.server.netty.streams.NettyRequest;
import org.alexdev.kepler.util.StringUtil;

public class UPDATE implements MessageEvent {
@Override
Expand All @@ -13,48 +13,52 @@ public void handle(Player player, NettyRequest reader) throws Exception {
return;
}

var registerValues = PlayerManager.getInstance().getRegisterValues();

while (reader.remainingBytes().length > 0) {
int updateId = reader.readBase64();
System.out.println(updateId);
var valueId = reader.readBase64();

switch (updateId) {
case 9:
{
boolean receiveNews = reader.readBytes(1)[0] == 'A';
player.getDetails().setReceiveNews(receiveNews);
break;
}
case 4:
{
String figure = StringUtil.filterInput(reader.readString(), true);
player.getDetails().setFigure(figure);
break;
}
case 5:
{
char sex = StringUtil.filterInput(reader.readString(), true).toCharArray()[0];
if (!registerValues.containsKey(valueId)) {
return;
}

if (sex != player.getDetails().getSex()) {
player.getDetails().setSex(sex);
}
var value = registerValues.get(valueId);

break;
}
case 6:
switch (value.getDataType()) {
case STRING:
{
String motto = StringUtil.filterInput(reader.readString(), true);
player.getDetails().setMotto(motto);
value.setValue(reader.readString());
break;
}
default:
case BOOLEAN:
{
System.out.println("Unknown: " + new String(reader.remainingBytes()));
reader.readBytes(reader.remainingBytes().length);
value.setFlag(reader.readBytes(1)[0] == 'A');
break;
}
}
}

Object directMail = PlayerManager.getInstance().getRegisterValue(registerValues, "directMail");
if (directMail != null) {
player.getDetails().setReceiveNews((boolean) directMail);
PlayerDao.saveReceiveMail(player.getDetails());
}

Object motto = PlayerManager.getInstance().getRegisterValue(registerValues, "customData");
if (motto != null) {
player.getDetails().setMotto((String) motto);
}

Object figure = PlayerManager.getInstance().getRegisterValue(registerValues, "figure");
if (figure != null) {
player.getDetails().setFigure((String) figure);
}

Object sex = PlayerManager.getInstance().getRegisterValue(registerValues, "sex");
if (sex != null) {
player.getDetails().setSex(((String) sex).toCharArray()[0]);
}

PlayerDao.saveDetails(player.getDetails());
PlayerDao.saveMotto(player.getDetails());

Expand Down
Loading

0 comments on commit e7086b7

Please sign in to comment.