commit ddddcadb24a3714fd246af975e419c5aa8902763 Author: jeffcheasey88 <66554203+jeffcheasey88@users.noreply.github.com> Date: Sun Oct 1 11:45:56 2023 +0200 first commit diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..5ef5e20 --- /dev/null +++ b/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/PeerAtCodeFramework.jar b/PeerAtCodeFramework.jar new file mode 100644 index 0000000..b673db4 Binary files /dev/null and b/PeerAtCodeFramework.jar differ diff --git a/src/be/jeffcheasey88/todo/Main.java b/src/be/jeffcheasey88/todo/Main.java new file mode 100644 index 0000000..cb67384 --- /dev/null +++ b/src/be/jeffcheasey88/todo/Main.java @@ -0,0 +1,51 @@ +package be.jeffcheasey88.todo; + +import static dev.peerat.framework.RequestType.OPTIONS; + +import java.util.regex.Matcher; + +import be.jeffcheasey88.todo.routes.users.Login; +import dev.peerat.framework.Context; +import dev.peerat.framework.HttpReader; +import dev.peerat.framework.HttpWriter; +import dev.peerat.framework.RequestType; +import dev.peerat.framework.Response; +import dev.peerat.framework.Route; +import dev.peerat.framework.Router; + +public class Main { + + public static void main(String[] args) throws Exception { + Router router = new Router().configureJwt( + (builder) -> builder.setExpectedIssuer("http://localhost"), + (claims) -> { + claims.setIssuer("http://localhost"); // who creates the token and signs it + claims.setExpirationTimeMinutesInTheFuture(100); + }, + (claims) -> new User(claims)) + .addDefaultHeaders(RequestType.GET, "Access-Control-Allow-Origin: *") + .addDefaultHeaders(RequestType.POST, "Access-Control-Allow-Origin: *"); + + router.setDefault((matcher, context, reader, writer) -> { + context.response(404); + writer.write("404 not Found.\n"); + }); + + router.register(new Response(){ + @Route(path = "^(.*)$", type = OPTIONS) + @Override + public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception { + context.response(200, + "Access-Control-Allow-Origin: *", + "Access-Control-Allow-Methods: *", + "Access-Control-Allow-Headers: *" + ); + } + }); + + router.register(new Login(router)); + + router.listen(80, false); + } + +} diff --git a/src/be/jeffcheasey88/todo/User.java b/src/be/jeffcheasey88/todo/User.java new file mode 100644 index 0000000..cc7af2d --- /dev/null +++ b/src/be/jeffcheasey88/todo/User.java @@ -0,0 +1,26 @@ +package be.jeffcheasey88.todo; + +import org.jose4j.jwt.JwtClaims; + +public class User extends dev.peerat.framework.User{ + + private String name; + + public User(String name){ + this.name = name; + } + + public User(JwtClaims claims){ + this.name = claims.getClaimValueAsString("name"); + } + + public String getName(){ + return this.name; + } + + @Override + public void write(JwtClaims claims){ + claims.setClaim("name", this.name); + } + +} diff --git a/src/be/jeffcheasey88/todo/model/JsonRepository.java b/src/be/jeffcheasey88/todo/model/JsonRepository.java new file mode 100644 index 0000000..967fb10 --- /dev/null +++ b/src/be/jeffcheasey88/todo/model/JsonRepository.java @@ -0,0 +1,5 @@ +package be.jeffcheasey88.todo.model; + +public class JsonRepository implements Repository{ + +} diff --git a/src/be/jeffcheasey88/todo/model/JsonSerializable.java b/src/be/jeffcheasey88/todo/model/JsonSerializable.java new file mode 100644 index 0000000..769f96e --- /dev/null +++ b/src/be/jeffcheasey88/todo/model/JsonSerializable.java @@ -0,0 +1,9 @@ +package be.jeffcheasey88.todo.model; + +import dev.peerat.framework.utils.json.JsonMap; + +public interface JsonSerializable { + + void serialize(JsonMap json); + +} diff --git a/src/be/jeffcheasey88/todo/model/Project.java b/src/be/jeffcheasey88/todo/model/Project.java new file mode 100644 index 0000000..a224b42 --- /dev/null +++ b/src/be/jeffcheasey88/todo/model/Project.java @@ -0,0 +1,83 @@ +package be.jeffcheasey88.todo.model; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import dev.peerat.framework.utils.json.JsonArray; +import dev.peerat.framework.utils.json.JsonMap; + +public class Project implements JsonSerializable{ + + private String name; + private List users; + private List tasks; + + private Map states; + + public Project(){ + this.users = new ArrayList<>(); + this.tasks = new ArrayList<>(); + this.states = new LinkedHashMap<>(); + } + + public Project(JsonMap json){ + this(); + this.name = json.get("name"); + for(Object user : json.get("users").toList()){ + this.users.add((String) user); + } + for(Object task : json.get("tasks").toList()){ + this.tasks.add(new Task((JsonMap) task)); + } + for(Entry entries : json.get("states").entries()){ + states.put(entries.getKey(), (String) entries.getValue()); + } + } + + @Override + public void serialize(JsonMap json){ + json.set("name", this.name); + + JsonArray jUsers = new JsonArray(); + for(String user : users) jUsers.add(user); + json.set("users", jUsers); + + JsonArray jTasks = new JsonArray(); + for(Task task : tasks){ + JsonMap jTask = new JsonMap(); + task.serialize(jTask); + jTasks.add(jTask); + } + json.set("tasks", jTasks); + + JsonMap jstates = new JsonMap(); + for(Entry entries : states.entrySet()){ + jstates.set(entries.getKey(), entries.getValue()); + } + json.set("states", jstates); + } + + public String getName(){ + return this.name; + } + + public void setName(String name){ + this.name = name; + } + + public List getUsers(){ + return this.users; + } + + public List getTasks(){ + return this.tasks; + } + + public Map getStates(){ + return this.states; + } + +} diff --git a/src/be/jeffcheasey88/todo/model/Repository.java b/src/be/jeffcheasey88/todo/model/Repository.java new file mode 100644 index 0000000..f7659c5 --- /dev/null +++ b/src/be/jeffcheasey88/todo/model/Repository.java @@ -0,0 +1,25 @@ +package be.jeffcheasey88.todo.model; + +import java.util.List; + +import be.jeffcheasey88.todo.User; + +public interface Repository { + + Result login(String username, String password); + + Result> getProjects(User user); + + Result createProject(User user, String name); + + Result removeProject(User user, String name); + + Result getProject(User user, String name); + + Result renameProject(User user, String lastname, String updatedname); + + Result manageUsers(User user, String project, List users); + + Result> getUsers(); + +} diff --git a/src/be/jeffcheasey88/todo/model/Result.java b/src/be/jeffcheasey88/todo/model/Result.java new file mode 100644 index 0000000..652f25d --- /dev/null +++ b/src/be/jeffcheasey88/todo/model/Result.java @@ -0,0 +1,36 @@ +package be.jeffcheasey88.todo.model; + +import dev.peerat.framework.utils.json.JsonMap; + +public class Result{ + + private E element; + private int errorCode; + private JsonMap error; + + public Result(E element){ + this.element = element; + } + + public Result(int errorCode, JsonMap error){ + this.errorCode = errorCode; + this.error = error; + } + + public boolean success(){ + return error == null; + } + + public E getElement(){ + return this.element; + } + + public int getErrorCode(){ + return this.errorCode; + } + + public JsonMap getError(){ + return this.error; + } + +} diff --git a/src/be/jeffcheasey88/todo/model/Task.java b/src/be/jeffcheasey88/todo/model/Task.java new file mode 100644 index 0000000..a0b4808 --- /dev/null +++ b/src/be/jeffcheasey88/todo/model/Task.java @@ -0,0 +1,55 @@ +package be.jeffcheasey88.todo.model; + +import java.util.ArrayList; +import java.util.List; + +import dev.peerat.framework.utils.json.JsonArray; +import dev.peerat.framework.utils.json.JsonMap; + +public class Task implements JsonSerializable{ + + private List users; + private String name; + private TaskModifier modifier; + + public Task(){ + this.users = new ArrayList<>(); + this.modifier = new TaskModifier(); + } + + public Task(JsonMap json){ + this(); + for(Object user : json.get("users").toList()){ + this.users.add((String) user); + } + this.name = json.get("name"); + this.modifier = new TaskModifier(json.get("modifier")); + } + + @Override + public void serialize(JsonMap json){ + JsonArray jusers = new JsonArray(); + for(String user : users) jusers.add(user); + json.set("users", jusers); + json.set("name", this.name); + JsonMap jmodifier = new JsonMap(); + this.modifier.serialize(jmodifier); + json.set("modifier", jmodifier); + } + + public List getUsers(){ + return this.users; + } + + public String getName(){ + return this.name; + } + + public void setName(String name){ + this.name = name; + } + + public TaskModifier getModifier(){ + return this.modifier; + } +} diff --git a/src/be/jeffcheasey88/todo/model/TaskContainer.java b/src/be/jeffcheasey88/todo/model/TaskContainer.java new file mode 100644 index 0000000..a723733 --- /dev/null +++ b/src/be/jeffcheasey88/todo/model/TaskContainer.java @@ -0,0 +1,38 @@ +package be.jeffcheasey88.todo.model; + +import java.util.ArrayList; +import java.util.List; + +import dev.peerat.framework.utils.json.JsonArray; +import dev.peerat.framework.utils.json.JsonMap; + +public class TaskContainer extends Task implements JsonSerializable{ + + private List tasks; + + public TaskContainer(){ + this.tasks = new ArrayList<>(); + } + + public TaskContainer(JsonMap json){ + this(); + for(Object task : json.get("tasks").toList()){ + tasks.add((Task) task); + } + } + + @Override + public void serialize(JsonMap json){ + JsonArray jTasks = new JsonArray(); + for(Task task : tasks){ + JsonMap jtask = new JsonMap(); + task.serialize(jtask); + jTasks.add(jtask); + } + json.set("tasks", jTasks); + } + + public List getTasks(){ + return this.tasks; + } +} diff --git a/src/be/jeffcheasey88/todo/model/TaskModifier.java b/src/be/jeffcheasey88/todo/model/TaskModifier.java new file mode 100644 index 0000000..e6b8e48 --- /dev/null +++ b/src/be/jeffcheasey88/todo/model/TaskModifier.java @@ -0,0 +1,49 @@ +package be.jeffcheasey88.todo.model; + +import dev.peerat.framework.utils.json.JsonMap; + +public class TaskModifier implements JsonSerializable{ + + private String state; + + private String title; + private String description; + + public TaskModifier(){} + + public TaskModifier(JsonMap json){ + this(); + this.state = json.get("state"); + + this.title = json.get("title"); + this.description = json.get("description"); + } + + @Override + public void serialize(JsonMap json){ + json.set("state", state); + + json.set("title", title); + json.set("description", description); + } + + public void setState(String state){ + this.state = state; + } + + public String getTitle(){ + return this.title; + } + + public void setTitle(String title){ + this.title = title; + } + + public String getDescription(){ + return this.description; + } + + public void setDescription(String description){ + this.description = description; + } +} diff --git a/src/be/jeffcheasey88/todo/routes/users/Login.java b/src/be/jeffcheasey88/todo/routes/users/Login.java new file mode 100644 index 0000000..70e6d5a --- /dev/null +++ b/src/be/jeffcheasey88/todo/routes/users/Login.java @@ -0,0 +1,34 @@ +package be.jeffcheasey88.todo.routes.users; + +import java.util.regex.Matcher; + +import be.jeffcheasey88.todo.User; +import dev.peerat.framework.Context; +import dev.peerat.framework.HttpReader; +import dev.peerat.framework.HttpWriter; +import dev.peerat.framework.RequestType; +import dev.peerat.framework.Response; +import dev.peerat.framework.Route; +import dev.peerat.framework.Router; + +public class Login implements Response{ + + private Router router; + + public Login(Router router){ + this.router = router; + } + + @Route(path = "^/login$", type = RequestType.POST) + public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception { + if (context.isLogged()){ + context.response(403); + return; + } + context.response(200, + "Access-Control-Allow-Origin: *", + "Access-Control-Expose-Headers: Authorization", + "Authorization: Bearer " + this.router.createAuthUser(new User(""))); + } + +}