Compare commits
No commits in common. "743e230f85a5c412897a172f261792b8b4e7adb2" and "97d8c0b7de8f5400f00d6402ce624ad9f7f62477" have entirely different histories.
743e230f85
...
97d8c0b7de
12 changed files with 33 additions and 220 deletions
|
@ -1,10 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="src" path="test"/>
|
||||
<classpathentry exported="true" kind="lib" path="mysql-connector-java-8.0.28.jar"/>
|
||||
|
@ -17,8 +13,5 @@
|
|||
<classpathentry kind="lib" path="Treasure.jar"/>
|
||||
<classpathentry exported="true" kind="lib" path="JDA-5.0.0-beta.8-withDependencies.jar"/>
|
||||
<classpathentry exported="true" kind="lib" path="PeerAtCodeFramework.jar"/>
|
||||
<classpathentry exported="true" kind="lib" path="angus-activation-2.0.1.jar"/>
|
||||
<classpathentry exported="true" kind="lib" path="jakarta.activation-api-2.1.2.jar"/>
|
||||
<classpathentry exported="true" kind="lib" path="jakarta.mail-2.0.2.jar"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
|
|
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -5,4 +5,3 @@ config.txt
|
|||
dist/
|
||||
testApi/
|
||||
.apt_generated/*
|
||||
/.apt_generated/
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -7,8 +7,6 @@ import java.io.FileReader;
|
|||
import java.io.FileWriter;
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import dev.peerat.backend.utils.Mail;
|
||||
|
||||
public class Configuration {
|
||||
|
||||
private String db_host;
|
||||
|
@ -31,12 +29,6 @@ public class Configuration {
|
|||
|
||||
private int groupJoinMinutes;
|
||||
private String groupQuitMinutes;
|
||||
|
||||
private String mailUsername;
|
||||
private String mailPassword;
|
||||
private String mailSmtpHost;
|
||||
private int mailSmptPort;
|
||||
private String mailFromAddress;
|
||||
|
||||
private File file;
|
||||
|
||||
|
@ -181,13 +173,4 @@ public class Configuration {
|
|||
public String getGroupQuitMinutes(){
|
||||
return this.groupQuitMinutes;
|
||||
}
|
||||
|
||||
public Mail getMail(){
|
||||
return new Mail(
|
||||
this.mailUsername,
|
||||
this.mailPassword,
|
||||
this.mailSmtpHost,
|
||||
this.mailSmptPort,
|
||||
this.mailFromAddress);
|
||||
}
|
||||
}
|
|
@ -2,14 +2,11 @@ package dev.peerat.backend;
|
|||
|
||||
import static dev.peerat.framework.RequestType.OPTIONS;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import dev.peerat.backend.model.Completion;
|
||||
import dev.peerat.backend.model.Group;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.model.Player;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.backend.routes.BadgeDetails;
|
||||
import dev.peerat.backend.routes.ChapterElement;
|
||||
|
@ -17,7 +14,6 @@ import dev.peerat.backend.routes.ChapterList;
|
|||
import dev.peerat.backend.routes.DynamicLeaderboard;
|
||||
import dev.peerat.backend.routes.Leaderboard;
|
||||
import dev.peerat.backend.routes.Login;
|
||||
import dev.peerat.backend.routes.MailConfirmation;
|
||||
import dev.peerat.backend.routes.PlayerDetails;
|
||||
import dev.peerat.backend.routes.PuzzleElement;
|
||||
import dev.peerat.backend.routes.PuzzleResponse;
|
||||
|
@ -27,7 +23,6 @@ import dev.peerat.backend.routes.groups.GroupCreate;
|
|||
import dev.peerat.backend.routes.groups.GroupJoin;
|
||||
import dev.peerat.backend.routes.groups.GroupList;
|
||||
import dev.peerat.backend.routes.groups.GroupQuit;
|
||||
import dev.peerat.backend.utils.Mail;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
import dev.peerat.framework.HttpWriter;
|
||||
|
@ -85,12 +80,10 @@ public class Main{
|
|||
}
|
||||
|
||||
private static void initRoutes(Router<PeerAtUser> router, DatabaseRepository repo, Configuration config){
|
||||
Map<Player, Integer> playersWaiting = new HashMap<>();
|
||||
router.register(new ChapterElement(repo));
|
||||
router.register(new ChapterList(repo));
|
||||
router.register(new PuzzleElement(repo));
|
||||
router.register(new Register(repo, router, config.getUsersFiles(), playersWaiting));
|
||||
router.register(new MailConfirmation(repo, router, config.getUsersFiles(), playersWaiting));
|
||||
router.register(new Register(repo, router, config.getUsersFiles()));
|
||||
router.register(new Login(repo, router));
|
||||
router.register(new Result(repo));
|
||||
router.register(new Leaderboard(repo));
|
||||
|
|
|
@ -22,12 +22,12 @@ public class Player implements Comparable<Player> {
|
|||
|
||||
private Set<Badge> badges;
|
||||
|
||||
public Player(String pseudo, String email, String firstname, String lastname) {
|
||||
public Player(String pseudo, String email, String firstname, String lastname, String description) {
|
||||
this.pseudo = pseudo;
|
||||
this.email = email;
|
||||
this.firstname = firstname;
|
||||
this.lastname = lastname;
|
||||
this.description = "";
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public Player(String pseudo, int score, int tries) {
|
||||
|
@ -36,11 +36,6 @@ public class Player implements Comparable<Player> {
|
|||
this.completions.add(new Completion(tries, score));
|
||||
email = ""; // TO make compareTo and equals works as usual
|
||||
}
|
||||
|
||||
public Player(String email) {
|
||||
// For player find in Map during register process
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public String getPseudo() {
|
||||
return this.pseudo;
|
||||
|
|
|
@ -129,7 +129,8 @@ public class DatabaseRepository {
|
|||
|
||||
private Player makePlayer(ResultSet playerResult, int id) throws SQLException {
|
||||
Player p = new Player(playerResult.getString("pseudo"), playerResult.getString("email"),
|
||||
playerResult.getString("firstName"), playerResult.getString("lastName"));
|
||||
playerResult.getString("firstName"), playerResult.getString("lastName"),
|
||||
playerResult.getString("description"));
|
||||
if (hasColumn(playerResult, "avatar")) {
|
||||
p.setAvatar(playerResult.getBytes("avatar"));
|
||||
}
|
||||
|
|
|
@ -1,96 +0,0 @@
|
|||
package dev.peerat.backend.routes;
|
||||
|
||||
import static dev.peerat.framework.RequestType.POST;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.model.Player;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
import dev.peerat.framework.HttpWriter;
|
||||
import dev.peerat.framework.Response;
|
||||
import dev.peerat.framework.Route;
|
||||
import dev.peerat.framework.Router;
|
||||
|
||||
public class MailConfirmation implements Response {
|
||||
|
||||
private DatabaseRepository databaseRepo;
|
||||
private Router<PeerAtUser> router;
|
||||
private String usersFilesPath;
|
||||
private Map<Player, Integer> playersWaiting;
|
||||
|
||||
public MailConfirmation(DatabaseRepository databaseRepo, Router<PeerAtUser> router, String initUsersFilesPath,
|
||||
Map<Player, Integer> playersWaiting) {
|
||||
this.databaseRepo = databaseRepo;
|
||||
this.router = router;
|
||||
usersFilesPath = initUsersFilesPath;
|
||||
}
|
||||
|
||||
@RouteDoc(path = "/confirmation", responseCode = 200, responseDescription = "L'utilisateur est inscrit")
|
||||
@RouteDoc(responseCode = 403, responseDescription = "L'utilisateur est connecté")
|
||||
@RouteDoc(responseCode = 400, responseDescription = "Aucune données fournie / données invalide")
|
||||
|
||||
@Route(path = "^\\/confirmation$", type = POST)
|
||||
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
if (context.getUser() != null) {
|
||||
context.response(403);
|
||||
return;
|
||||
}
|
||||
JSONObject informations = reader.readJson();
|
||||
if (informations != null) {
|
||||
boolean allNecessaryFieldsFilled = informations.containsKey("email") && informations.containsKey("code")
|
||||
&& informations.containsKey("passwd");
|
||||
if (!allNecessaryFieldsFilled) {
|
||||
context.response(400);
|
||||
return;
|
||||
}
|
||||
String email = (String) informations.get("email");
|
||||
String password = (String) informations.get("passwd");
|
||||
int code = (int) informations.get("code");
|
||||
|
||||
Player newPlayer = getPlayerFromEmail(email);
|
||||
if (newPlayer != null && code == playersWaiting.get(newPlayer)) {
|
||||
String pseudo = newPlayer.getPseudo();
|
||||
int id;
|
||||
if ((id = databaseRepo.register(pseudo, email, password, newPlayer.getFirstname(), newPlayer.getLastname(), "", "", "")) >= 0) {
|
||||
context.response(200, "Access-Control-Expose-Headers: Authorization",
|
||||
"Authorization: Bearer " + this.router.createAuthUser(new PeerAtUser(id)));
|
||||
createFolderToSaveSourceCode(pseudo);
|
||||
return;
|
||||
} else {
|
||||
context.response(400);
|
||||
JSONObject error = new JSONObject();
|
||||
error.put("username_valid", pseudo);
|
||||
error.put("email_valid", email);
|
||||
writer.write(error.toJSONString());
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
context.response(400);
|
||||
}
|
||||
|
||||
private void createFolderToSaveSourceCode(String pseudo) throws IOException {
|
||||
|
||||
Files.createDirectories(Paths.get(String.format("%s/%s", usersFilesPath, pseudo)));
|
||||
}
|
||||
|
||||
private Player getPlayerFromEmail(String email) {
|
||||
Player toMatch = new Player(email);
|
||||
for (Player p: playersWaiting.keySet()) {
|
||||
if (p.equals(toMatch)) {
|
||||
return p;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -5,16 +5,12 @@ import static dev.peerat.framework.RequestType.POST;
|
|||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.model.Player;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
|
@ -28,62 +24,69 @@ public class Register implements Response {
|
|||
private DatabaseRepository databaseRepo;
|
||||
private Router<PeerAtUser> router;
|
||||
private String usersFilesPath;
|
||||
private Map<Player, Integer> playersWaiting;
|
||||
|
||||
public Register(DatabaseRepository databaseRepo, Router<PeerAtUser> router, String initUsersFilesPath,
|
||||
Map<Player, Integer> playersWaiting) {
|
||||
public Register(DatabaseRepository databaseRepo, Router<PeerAtUser> router, String initUsersFilesPath) {
|
||||
this.databaseRepo = databaseRepo;
|
||||
this.router = router;
|
||||
usersFilesPath = initUsersFilesPath;
|
||||
}
|
||||
|
||||
|
||||
@RouteDoc(path = "/register", responseCode = 200, responseDescription = "L'utilisateur est inscrit")
|
||||
@RouteDoc(responseCode = 403, responseDescription = "L'utilisateur est connecté")
|
||||
@RouteDoc(responseCode = 400, responseDescription = "Aucune données fournie / données invalide")
|
||||
|
||||
@Route(path = "^\\/register$", type = POST)
|
||||
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
if (context.getUser() != null) {
|
||||
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||
if (context.getUser() != null){
|
||||
context.response(403);
|
||||
return;
|
||||
}
|
||||
JSONObject informations = reader.readJson();
|
||||
if (informations != null) {
|
||||
boolean allNecessaryFieldsFilled = informations.containsKey("pseudo") && informations.containsKey("email")
|
||||
&& informations.containsKey("firstname") && informations.containsKey("lastname");
|
||||
if (!allNecessaryFieldsFilled) {
|
||||
context.response(400);
|
||||
boolean allFieldsFilled = informations.containsKey("pseudo") && informations.containsKey("email")
|
||||
&& informations.containsKey("passwd") && informations.containsKey("firstname")
|
||||
&& informations.containsKey("lastname") && informations.containsKey("description")
|
||||
&& informations.containsKey("sgroup") && informations.containsKey("avatar");
|
||||
if (!allFieldsFilled) {
|
||||
context.response(403);
|
||||
return;
|
||||
}
|
||||
String pseudo = (String) informations.get("pseudo");
|
||||
String email = (String) informations.get("email");
|
||||
String password = (String) informations.get("passwd");
|
||||
String firstname = (String) informations.get("firstname");
|
||||
String lastname = (String) informations.get("lastname");
|
||||
|
||||
String description = (String) informations.get("description");
|
||||
String group = (String) informations.get("sgroup");
|
||||
String avatar = (String) informations.get("avatar");
|
||||
|
||||
boolean pseudoAvailable = databaseRepo.checkPseudoAvailability(pseudo);
|
||||
boolean emailAvailable = databaseRepo.checkEmailAvailability(email);
|
||||
if (pseudoAvailable && emailAvailable) {
|
||||
Player player = new Player(pseudo, email, firstname, lastname);
|
||||
playersWaiting.put(player, codeGenerator());
|
||||
context.response(200);
|
||||
int id;
|
||||
if ((id = databaseRepo.register(pseudo, email, password, firstname, lastname, description, group,
|
||||
avatar)) >= 0) {
|
||||
context.response(200,
|
||||
"Access-Control-Expose-Headers: Authorization",
|
||||
"Authorization: Bearer " + this.router.createAuthUser(new PeerAtUser(id)));
|
||||
createFolderToSaveSourceCode(pseudo);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
context.response(400);
|
||||
JSONObject error = new JSONObject();
|
||||
error.put("username_valid", pseudoAvailable);
|
||||
error.put("email_valid", emailAvailable);
|
||||
writer.write(error.toJSONString());
|
||||
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
context.response(400);
|
||||
}
|
||||
|
||||
private int codeGenerator() {
|
||||
int min = 1000;
|
||||
int max = 9999;
|
||||
return new Random().nextInt((max-min)) + min;
|
||||
private void createFolderToSaveSourceCode(String pseudo) throws IOException {
|
||||
|
||||
Files.createDirectories(Paths.get(String.format("%s/%s", usersFilesPath, pseudo)));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,58 +0,0 @@
|
|||
package dev.peerat.backend.utils;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Properties;
|
||||
|
||||
import jakarta.mail.Authenticator;
|
||||
import jakarta.mail.Message;
|
||||
import jakarta.mail.PasswordAuthentication;
|
||||
import jakarta.mail.Session;
|
||||
import jakarta.mail.Transport;
|
||||
import jakarta.mail.internet.InternetAddress;
|
||||
import jakarta.mail.internet.MimeMessage;
|
||||
|
||||
public class Mail {
|
||||
private Session session;
|
||||
private String fromAddress;
|
||||
|
||||
public Mail(String initUsername, String initPassword, String initSmtpHost, int initSmtpPort, String initFromAddress) {
|
||||
System.out.println("login on "+initUsername+" into "+initSmtpHost+":"+initSmtpPort);
|
||||
Properties props = new Properties();
|
||||
props.put("mail.smtp.host", initSmtpHost);
|
||||
props.put("mail.smtp.port", initSmtpPort);
|
||||
props.put("mail.smtp.auth", "true");
|
||||
props.put("mail.smtp.starttls.enable", "true");
|
||||
|
||||
Authenticator auth = new Authenticator() {
|
||||
//override the getPasswordAuthentication method
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
return new PasswordAuthentication(initUsername, initPassword);
|
||||
}
|
||||
};
|
||||
session = Session.getInstance(props, auth);
|
||||
fromAddress = initFromAddress;
|
||||
}
|
||||
|
||||
public void send(String toAddress, String subject, String text) {
|
||||
try
|
||||
{
|
||||
MimeMessage msg = new MimeMessage(session);
|
||||
//set message headers
|
||||
msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
|
||||
msg.addHeader("format", "flowed");
|
||||
msg.addHeader("Content-Transfer-Encoding", "8bit");
|
||||
|
||||
msg.setFrom(new InternetAddress("ping@peerat.dev", "NoReply-JD"));
|
||||
msg.setReplyTo(InternetAddress.parse("ping@peerat.dev", false));
|
||||
msg.setSubject(subject, "UTF-8");
|
||||
msg.setText(text, "UTF-8");
|
||||
msg.setSentDate(new Date());
|
||||
|
||||
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress, false));
|
||||
Transport.send(msg);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue