From 5ad774abd21981886160972c18afefe7afb8946d Mon Sep 17 00:00:00 2001 From: jeffcheasey88 <66554203+jeffcheasey88@users.noreply.github.com> Date: Fri, 13 Oct 2023 15:00:19 +0200 Subject: [PATCH] Login --- src/be/jeffcheasey88/todo/Main.java | 18 ++- .../todo/model/JsonRepository.java | 112 ++++++++++++++++++ .../jeffcheasey88/todo/model/Repository.java | 15 ++- src/be/jeffcheasey88/todo/model/Result.java | 17 ++- .../jeffcheasey88/todo/model/UserWorker.java | 30 +++++ .../todo/routes/projects/CreateProject.java | 17 +++ .../todo/routes/users/Login.java | 25 ++-- 7 files changed, 213 insertions(+), 21 deletions(-) create mode 100644 src/be/jeffcheasey88/todo/model/UserWorker.java create mode 100644 src/be/jeffcheasey88/todo/routes/projects/CreateProject.java diff --git a/src/be/jeffcheasey88/todo/Main.java b/src/be/jeffcheasey88/todo/Main.java index cb67384..d4a8d83 100644 --- a/src/be/jeffcheasey88/todo/Main.java +++ b/src/be/jeffcheasey88/todo/Main.java @@ -2,8 +2,13 @@ package be.jeffcheasey88.todo; import static dev.peerat.framework.RequestType.OPTIONS; +import java.io.File; import java.util.regex.Matcher; +import org.jose4j.lang.JoseException; + +import be.jeffcheasey88.todo.model.JsonRepository; +import be.jeffcheasey88.todo.model.UserWorker; import be.jeffcheasey88.todo.routes.users.Login; import dev.peerat.framework.Context; import dev.peerat.framework.HttpReader; @@ -16,13 +21,13 @@ import dev.peerat.framework.Router; public class Main { public static void main(String[] args) throws Exception { - Router router = new Router().configureJwt( + 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)) + (claims) -> new UserWorker(claims)) .addDefaultHeaders(RequestType.GET, "Access-Control-Allow-Origin: *") .addDefaultHeaders(RequestType.POST, "Access-Control-Allow-Origin: *"); @@ -43,7 +48,14 @@ public class Main { } }); - router.register(new Login(router)); + JsonRepository json = new JsonRepository(new File("/home/todo/"), (username) -> { + try{ + return router.createAuthUser(new UserWorker(username)); + }catch(JoseException e){} + return null; + }); + + router.register(new Login(json)); router.listen(80, false); } diff --git a/src/be/jeffcheasey88/todo/model/JsonRepository.java b/src/be/jeffcheasey88/todo/model/JsonRepository.java index 967fb10..ef32e14 100644 --- a/src/be/jeffcheasey88/todo/model/JsonRepository.java +++ b/src/be/jeffcheasey88/todo/model/JsonRepository.java @@ -1,5 +1,117 @@ package be.jeffcheasey88.todo.model; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Function; + +import dev.peerat.framework.utils.json.Json; +import dev.peerat.framework.utils.json.JsonArray; +import dev.peerat.framework.utils.json.JsonMap; +import dev.peerat.framework.utils.json.JsonParser; + public class JsonRepository implements Repository{ + + private File dir; + private JsonParser parser; + + private Function tokenGenerator; + + public JsonRepository(File dir, Function tokenGenerator){ + this.dir = dir.getAbsoluteFile(); + this.tokenGenerator = tokenGenerator; + + this.parser = new JsonParser(); + } + + private J read(String path) throws Exception{ + File file = new File(dir, path).getAbsoluteFile(); + if(!file.getAbsolutePath().startsWith(dir.getAbsolutePath())) return null; + BufferedReader reader = new BufferedReader(new FileReader(file)); + String line; + String content = ""; + while((line = reader.readLine()) != null) content+=line; + reader.close(); + return parser.parse(content); + } + + private boolean write(String path, Json json) throws Exception{ + File file = new File(dir, path).getAbsoluteFile(); + if(!file.getAbsolutePath().startsWith(dir.getAbsolutePath())) return false; + BufferedWriter writer = new BufferedWriter(new FileWriter(file)); + writer.write(json.toString()); + writer.flush(); + writer.close(); + return true; + } + + private Result error(int code, String message){ + return new Result<>(code, message); + } + + @Override + public Result login(String username, String password){ + try { + JsonMap map = read("users.json"); + if(map.has(username)){ + JsonMap infos = map.get(username); + return password.equals(infos.get("password")) ? new Result<>(tokenGenerator.apply(username)) : new Result<>(401, "Wrong password"); + } + return new Result<>(401,"Unknow username"); + }catch(Exception e){ + return error(500, "Internal Error"); + } + } + + @Override + public Result> getProjects(UserWorker user){ + try { + JsonMap map = read("users.json"); + List result = new ArrayList<>(); + for(Object o : map.get(user.getUsername()).get("projects").toList()) result.add((String) o); + return new Result<>(result); + }catch(Exception e){ + return error(500, "Internal Error"); + } + } + + @Override + public Result createProject(UserWorker user, String name) { + return null; + } + + @Override + public Result removeProject(UserWorker user, String name) { + return null; + } + + @Override + public Result getProject(UserWorker user, String name) { + return null; + } + + @Override + public Result renameProject(UserWorker user, String lastname, String updatedname) { + return null; + } + + @Override + public Result manageUsers(UserWorker user, String project, List users) { + return null; + } + + @Override + public Result> getUsers() { + return null; + } + + @Override + public Result> getUsers(String project) { + return null; + } } diff --git a/src/be/jeffcheasey88/todo/model/Repository.java b/src/be/jeffcheasey88/todo/model/Repository.java index f7659c5..d05fc6e 100644 --- a/src/be/jeffcheasey88/todo/model/Repository.java +++ b/src/be/jeffcheasey88/todo/model/Repository.java @@ -8,18 +8,21 @@ public interface Repository { Result login(String username, String password); - Result> getProjects(User user); + Result> getProjects(UserWorker user); - Result createProject(User user, String name); + Result createProject(UserWorker user, String name); - Result removeProject(User user, String name); + Result removeProject(UserWorker user, String name); - Result getProject(User user, String name); + Result getProject(UserWorker user, String name); - Result renameProject(User user, String lastname, String updatedname); + Result renameProject(UserWorker user, String lastname, String updatedname); - Result manageUsers(User user, String project, List users); + Result manageUsers(UserWorker user, String project, List users); Result> getUsers(); + Result> getUsers(String project); + + } diff --git a/src/be/jeffcheasey88/todo/model/Result.java b/src/be/jeffcheasey88/todo/model/Result.java index 652f25d..1aed30d 100644 --- a/src/be/jeffcheasey88/todo/model/Result.java +++ b/src/be/jeffcheasey88/todo/model/Result.java @@ -1,22 +1,31 @@ package be.jeffcheasey88.todo.model; -import dev.peerat.framework.utils.json.JsonMap; +import dev.peerat.framework.Context; +import dev.peerat.framework.HttpWriter; public class Result{ private E element; private int errorCode; - private JsonMap error; + private String error; public Result(E element){ this.element = element; } - public Result(int errorCode, JsonMap error){ + public Result(int errorCode, String error){ this.errorCode = errorCode; this.error = error; } + public void send(Context context, HttpWriter writer) throws Exception{ + if(success()) context.response(200); + else{ + context.response(errorCode); + writer.write(error); + } + } + public boolean success(){ return error == null; } @@ -29,7 +38,7 @@ public class Result{ return this.errorCode; } - public JsonMap getError(){ + public String getError(){ return this.error; } diff --git a/src/be/jeffcheasey88/todo/model/UserWorker.java b/src/be/jeffcheasey88/todo/model/UserWorker.java new file mode 100644 index 0000000..aa32c7e --- /dev/null +++ b/src/be/jeffcheasey88/todo/model/UserWorker.java @@ -0,0 +1,30 @@ +package be.jeffcheasey88.todo.model; + +import org.jose4j.jwt.JwtClaims; + +import dev.peerat.framework.User; + +public class UserWorker extends User{ + + private String username; + + public UserWorker(String username){ + this.username = username; + } + + public UserWorker(JwtClaims claims){ + this.username = claims.getClaimValueAsString("username"); + } + + public String getUsername(){ + return this.username; + } + + @Override + public void write(JwtClaims claims){ + claims.setClaim("username", claims); + } + + + +} diff --git a/src/be/jeffcheasey88/todo/routes/projects/CreateProject.java b/src/be/jeffcheasey88/todo/routes/projects/CreateProject.java new file mode 100644 index 0000000..c86ff43 --- /dev/null +++ b/src/be/jeffcheasey88/todo/routes/projects/CreateProject.java @@ -0,0 +1,17 @@ +package be.jeffcheasey88.todo.routes.projects; + +import java.util.regex.Matcher; + +import dev.peerat.framework.Context; +import dev.peerat.framework.HttpReader; +import dev.peerat.framework.HttpWriter; +import dev.peerat.framework.Response; + +public class CreateProject implements Response{ + + @Override + public void exec(Matcher arg0, Context arg1, HttpReader arg2, HttpWriter arg3) throws Exception { + + } + +} diff --git a/src/be/jeffcheasey88/todo/routes/users/Login.java b/src/be/jeffcheasey88/todo/routes/users/Login.java index 70e6d5a..7b97570 100644 --- a/src/be/jeffcheasey88/todo/routes/users/Login.java +++ b/src/be/jeffcheasey88/todo/routes/users/Login.java @@ -3,20 +3,22 @@ package be.jeffcheasey88.todo.routes.users; import java.util.regex.Matcher; import be.jeffcheasey88.todo.User; +import be.jeffcheasey88.todo.model.Repository; +import be.jeffcheasey88.todo.model.Result; 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; +import dev.peerat.framework.utils.json.JsonMap; public class Login implements Response{ - private Router router; + private Repository repo; - public Login(Router router){ - this.router = router; + public Login(Repository repo){ + this.repo = repo; } @Route(path = "^/login$", type = RequestType.POST) @@ -25,10 +27,17 @@ public class Login implements Response{ context.response(403); return; } - context.response(200, - "Access-Control-Allow-Origin: *", - "Access-Control-Expose-Headers: Authorization", - "Authorization: Bearer " + this.router.createAuthUser(new User(""))); + + JsonMap json = reader.readJson(); + Result result = repo.login(json.get("username"), json.get("password")); + if(result.success()){ + context.response(200, + "Access-Control-Allow-Origin: *", + "Access-Control-Expose-Headers: Authorization", + "Authorization: Bearer " + result.getElement()); + }else{ + result.send(context, writer); + } } }