Change password

This commit is contained in:
jeffcheasey88 2023-09-04 11:16:22 +02:00
parent c720d1e542
commit 5fde1c1747
6 changed files with 57 additions and 9 deletions

View file

@ -34,6 +34,7 @@ import be.jeffcheasey88.peeratcode.routes.groups.GroupCreate;
import be.jeffcheasey88.peeratcode.routes.groups.GroupJoin;
import be.jeffcheasey88.peeratcode.routes.groups.GroupList;
import be.jeffcheasey88.peeratcode.routes.groups.GroupQuit;
import be.jeffcheasey88.peeratcode.routes.users.ChangePassword;
import be.jeffcheasey88.peeratcode.routes.users.Login;
import be.jeffcheasey88.peeratcode.routes.users.ProfileSettings;
import be.jeffcheasey88.peeratcode.routes.users.Register;
@ -77,6 +78,7 @@ public class Main{
router.register(new Register(repo, router, config.getUsersFiles()));
router.register(new Login(repo, router));
router.register(new ProfileSettings(repo));
router.register(new ChangePassword(repo));
router.register(new ChapterElement(repo));
router.register(new ChapterList(repo));

View file

@ -81,7 +81,8 @@ public enum DatabaseQuery {
GET_PLAYER_COMPLETIONS("select c.*, p.name from completions c left join puzzles p on c.fk_puzzle = p.id_puzzle where fk_player = ?;"),
GET_PLAYER_RANK("SELECT * FROM (SELECT fk_player, RANK() OVER(ORDER BY SUM(score) DESC) rank FROM completions c LEFT JOIN players p ON p.id_player = c.fk_player GROUP BY fk_player ORDER BY rank) AS ranks WHERE ranks.fk_player = ?;"),
UPDATE_PLAYE_INFO("UPDATE players SET pseudo = ?, email = ?, first_name = ?, last_name = ? WHERE id_player = ?"),
UPDATE_PLAYER_INFO("UPDATE players SET pseudo = ?, email = ?, first_name = ?, last_name = ? WHERE id_player = ?"),
UPDATE_PLAYER_PASSWORD("UPDATE players SET passwd = ? WHERE id_player = ?"),
// BADGES
GET_BADGE("SELECT * FROM badges WHERE id_badge = ?"), GET_BADGES_OF_PLAYER(

View file

@ -223,7 +223,7 @@ public class DatabaseRepository {
statment.setString(1, pseudo);
ResultSet result = statment.executeQuery();
if(result.next()) return false;
statment = DatabaseQuery.UPDATE_PLAYE_INFO.prepare(this.con);
statment = DatabaseQuery.UPDATE_PLAYER_INFO.prepare(this.con);
statment.setString(1, player.getPseudo());
statment.setString(2, player.getEmail());
statment.setString(3, player.getFirstname());
@ -238,7 +238,7 @@ public class DatabaseRepository {
public void updateProfile(int id, Player player, String lastname, String firstname){
try{
PreparedStatement statment = DatabaseQuery.UPDATE_PLAYE_INFO.prepare(this.con);
PreparedStatement statment = DatabaseQuery.UPDATE_PLAYER_INFO.prepare(this.con);
statment.setString(1, player.getPseudo());
statment.setString(2, player.getEmail());
statment.setString(3, firstname);
@ -249,6 +249,17 @@ public class DatabaseRepository {
e.printStackTrace();
}
}
public void updatePassword(int id, String password){
try{
PreparedStatement statment = DatabaseQuery.UPDATE_PLAYER_PASSWORD.prepare(this.con);
statment.setString(1, Password.hash(password).withArgon2().getResult());
statment.setInt(2, id);
statment.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}
}
public Player getPlayerDetails(int idPlayer) {
return getPlayerDetails(idPlayer, null);
@ -492,7 +503,6 @@ public class DatabaseRepository {
*/
public int register(String pseudo, String email, String password, String firstname, String lastname,
String description, String sgroup, String avatar) {
Hash hash = Password.hash(password).withArgon2();
try {
ensureConnection();
con.setAutoCommit(false);
@ -500,7 +510,7 @@ public class DatabaseRepository {
Statement.RETURN_GENERATED_KEYS)) {
playerStatement.setString(1, pseudo);
playerStatement.setString(2, email);
playerStatement.setString(3, hash.getResult());
playerStatement.setString(3, Password.hash(password).withArgon2().getResult());
playerStatement.setString(4, firstname);
playerStatement.setString(5, lastname);
playerStatement.setString(6, description);

View file

@ -0,0 +1,35 @@
package be.jeffcheasey88.peeratcode.routes.users;
import java.util.regex.Matcher;
import org.jose4j.json.internal.json_simple.JSONObject;
import be.jeffcheasey88.peeratcode.bonus.extract.RouteDoc;
import be.jeffcheasey88.peeratcode.framework.HttpReader;
import be.jeffcheasey88.peeratcode.framework.HttpWriter;
import be.jeffcheasey88.peeratcode.framework.RequestType;
import be.jeffcheasey88.peeratcode.framework.Response;
import be.jeffcheasey88.peeratcode.framework.Route;
import be.jeffcheasey88.peeratcode.framework.User;
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
public class ChangePassword implements Response{
private DatabaseRepository repo;
public ChangePassword(DatabaseRepository repo){
this.repo = repo;
}
@RouteDoc(path = "/user/cpw", responseCode = 200, responseDescription = "L'utilisateur a mis à jours sont mots de passe")
@RouteDoc(responseCode = 400, responseDescription = "L'utilisateur a envoyer un mots de passe invalide")
@Route(path = "^/user/cpw$", type = RequestType.POST, needLogin = true)
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
String password = (String) reader.<JSONObject>readJson().get("password");
repo.updatePassword(user.getId(), password);
writer.response(200, "Access-Control-Allow-Origin: *");
}
}

View file

@ -15,12 +15,12 @@ import be.jeffcheasey88.peeratcode.framework.Router;
import be.jeffcheasey88.peeratcode.framework.User;
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
public class Login implements Response {
public class Login implements Response{
private DatabaseRepository databaseRepo;
private Router router;
public Login(DatabaseRepository databaseRepo, Router router) {
public Login(DatabaseRepository databaseRepo, Router router){
this.databaseRepo = databaseRepo;
this.router = router;
}

View file

@ -22,10 +22,10 @@ public class ProfileSettings implements Response{
this.repo = repo;
}
@RouteDoc(path = "/settings", responseCode = 200, responseDescription = "L'utilisateur a mis à jours sont profile")
@RouteDoc(path = "/user/settings", responseCode = 200, responseDescription = "L'utilisateur a mis à jours sont profile")
@RouteDoc(responseCode = 400, responseDescription = "L'utilisateur a envoyer une donnée unique, déjà utilisée")
@Route(path = "^/settings$", type = RequestType.POST, needLogin = true)
@Route(path = "^/user/settings$", type = RequestType.POST, needLogin = true)
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
JSONObject json = reader.readJson();