Add Form Utils & Make all Register & Login tests pass
This commit is contained in:
parent
e3b18ace64
commit
9e2a4735d3
5 changed files with 103 additions and 56 deletions
|
@ -35,6 +35,7 @@ public class DatabaseRepository {
|
|||
|
||||
private void ensureConnection() throws SQLException {
|
||||
if (con == null || (!con.isValid(5))){
|
||||
System.out.println("connecting "+config.getDbHost()+" on "+config.getDbDatabase());
|
||||
this.con = DriverManager.getConnection(
|
||||
"jdbc:mysql://" + config.getDbHost() + ":" + config.getDbPort() + "/" + config.getDbDatabase() + "",
|
||||
config.getDbUser(), config.getDbPassword());
|
||||
|
|
|
@ -14,8 +14,9 @@ import be.jeffcheasey88.peeratcode.framework.Route;
|
|||
import be.jeffcheasey88.peeratcode.framework.Router;
|
||||
import be.jeffcheasey88.peeratcode.framework.User;
|
||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
||||
import be.jeffcheasey88.peeratcode.utils.FormResponse;
|
||||
|
||||
public class Login implements Response{
|
||||
public class Login extends FormResponse{
|
||||
|
||||
private DatabaseRepository databaseRepo;
|
||||
private Router router;
|
||||
|
@ -31,23 +32,24 @@ public class Login implements Response{
|
|||
|
||||
@Route(path = "^\\/login$", type = POST)
|
||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
if (user != null) {
|
||||
if(user != null){
|
||||
writer.response(403, "Access-Control-Allow-Origin: *");
|
||||
return;
|
||||
}
|
||||
JSONObject informations = reader.readJson();
|
||||
if (informations != null) {
|
||||
String pseudo = (String) informations.get("pseudo");
|
||||
String password = (String) informations.get("passwd");
|
||||
int id;
|
||||
if ((id = databaseRepo.login(pseudo, password)) >= 0) {
|
||||
writer.response(200, "Access-Control-Allow-Origin: *",
|
||||
"Access-Control-Expose-Headers: Authorization",
|
||||
"Authorization: Bearer " + this.router.createAuthUser(id));
|
||||
return;
|
||||
}
|
||||
JSONObject json = json(reader);
|
||||
if(!areValids("pseudo", "passwd")){
|
||||
writer.response(400, "Access-Control-Allow-Origin: *");
|
||||
return;
|
||||
}
|
||||
int id;
|
||||
if((id = databaseRepo.login((String)json.get("pseudo"), (String)json.get("passwd"))) >= 0){
|
||||
writer.response(200,
|
||||
"Access-Control-Allow-Origin: *",
|
||||
"Access-Control-Expose-Headers: Authorization",
|
||||
"Authorization: Bearer " + this.router.createAuthUser(id));
|
||||
}else{
|
||||
writer.response(400,"Access-Control-Allow-Origin: *");
|
||||
}
|
||||
writer.response(400, "Access-Control-Allow-Origin: *");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -17,8 +17,9 @@ import be.jeffcheasey88.peeratcode.framework.Route;
|
|||
import be.jeffcheasey88.peeratcode.framework.Router;
|
||||
import be.jeffcheasey88.peeratcode.framework.User;
|
||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
||||
import be.jeffcheasey88.peeratcode.utils.FormResponse;
|
||||
|
||||
public class Register implements Response {
|
||||
public class Register extends FormResponse {
|
||||
|
||||
private DatabaseRepository databaseRepo;
|
||||
private Router router;
|
||||
|
@ -40,47 +41,42 @@ public class Register implements Response {
|
|||
writer.response(403, "Access-Control-Allow-Origin: *");
|
||||
return;
|
||||
}
|
||||
JSONObject informations = reader.readJson();
|
||||
if (informations != null) {
|
||||
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) {
|
||||
writer.response(403, "Access-Control-Allow-Origin: *");
|
||||
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) {
|
||||
int id;
|
||||
if ((id = databaseRepo.register(pseudo, email, password, firstname, lastname, description, group,
|
||||
avatar)) >= 0) {
|
||||
writer.response(200, "Access-Control-Allow-Origin: *",
|
||||
"Access-Control-Expose-Headers: Authorization",
|
||||
"Authorization: Bearer " + this.router.createAuthUser(id));
|
||||
createFolderToSaveSourceCode(pseudo);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
writer.response(400, "Access-Control-Allow-Origin: *");
|
||||
JSONObject error = new JSONObject();
|
||||
error.put("username_valid", pseudoAvailable);
|
||||
error.put("email_valid", emailAvailable);
|
||||
writer.write(error.toJSONString());
|
||||
return;
|
||||
}
|
||||
JSONObject json = json(reader);
|
||||
if(!areValids("pseudo","email","passwd","firstname","lastname")){
|
||||
writer.response(400, "Access-Control-Allow-Origin: *");
|
||||
return;
|
||||
}
|
||||
|
||||
String pseudo = (String) json.get("pseudo");
|
||||
String email = (String) json.get("email");
|
||||
String password = (String) json.get("passwd");
|
||||
String firstname = (String) json.get("firstname");
|
||||
String lastname = (String) json.get("lastname");
|
||||
String description = (String) json.get("description");
|
||||
String group = (String) json.get("sgroup");
|
||||
String avatar = (String) json.get("avatar");
|
||||
|
||||
boolean pseudoAvailable = databaseRepo.checkPseudoAvailability(pseudo);
|
||||
boolean emailAvailable = databaseRepo.checkEmailAvailability(email);
|
||||
if(pseudoAvailable && emailAvailable){
|
||||
int id;
|
||||
if((id = databaseRepo.register(pseudo, email, password, firstname, lastname, description, group,
|
||||
avatar)) >= 0) {
|
||||
writer.response(200,
|
||||
"Access-Control-Allow-Origin: *",
|
||||
"Access-Control-Expose-Headers: Authorization",
|
||||
"Authorization: Bearer " + this.router.createAuthUser(id));
|
||||
createFolderToSaveSourceCode(pseudo);
|
||||
}else{
|
||||
writer.response(400, "Access-Control-Allow-Origin: *");
|
||||
}
|
||||
}else{
|
||||
writer.response(400, "Access-Control-Allow-Origin: *");
|
||||
JSONObject error = new JSONObject();
|
||||
error.put("username_valid", pseudoAvailable);
|
||||
error.put("email_valid", emailAvailable);
|
||||
writer.write(error.toJSONString());
|
||||
}
|
||||
writer.response(400, "Access-Control-Allow-Origin: *");
|
||||
}
|
||||
|
||||
private void createFolderToSaveSourceCode(String pseudo) throws IOException {
|
||||
|
|
49
src/be/jeffcheasey88/peeratcode/utils/FormResponse.java
Normal file
49
src/be/jeffcheasey88/peeratcode/utils/FormResponse.java
Normal file
|
@ -0,0 +1,49 @@
|
|||
package be.jeffcheasey88.peeratcode.utils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.json.simple.JSONAware;
|
||||
|
||||
import be.jeffcheasey88.peeratcode.framework.HttpReader;
|
||||
import be.jeffcheasey88.peeratcode.framework.Response;
|
||||
|
||||
public abstract class FormResponse implements Response{
|
||||
|
||||
private JSONAware json;
|
||||
private Map<String, Pattern> checker;
|
||||
|
||||
public FormResponse(){
|
||||
this.checker = new HashMap<>();
|
||||
}
|
||||
|
||||
public void validator(String key, Pattern regex){
|
||||
this.checker.put(key, regex);
|
||||
}
|
||||
|
||||
public <T extends JSONAware> T json(HttpReader reader) throws Exception{
|
||||
return (T) (this.json = reader.readJson());
|
||||
}
|
||||
|
||||
public boolean hasFields(String... fields){
|
||||
Map<?,?> map = (Map<?,?>)json;
|
||||
for(String field : fields){
|
||||
if(!map.containsKey(field)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean areValids(String... fields){
|
||||
Map<?,?> map = (Map<?,?>)json;
|
||||
for(String field : fields){
|
||||
String value = (String) map.get(field);
|
||||
if(value == null) return false;
|
||||
if(value.isEmpty()) return false;
|
||||
Pattern pattern = checker.get(field);
|
||||
if(pattern == null) continue;
|
||||
if(!pattern.matcher(value).matches()) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -38,7 +38,6 @@ public class TestDatabaseRepository extends DatabaseRepository{
|
|||
Field field = DatabaseRepository.class.getDeclaredField("con");
|
||||
field.setAccessible(true);
|
||||
this.con = (Connection) field.get(this);
|
||||
System.out.println("connected !");
|
||||
}catch(Exception e){
|
||||
e.getCause().printStackTrace();
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue