Login
This commit is contained in:
parent
ddddcadb24
commit
5ad774abd2
@ -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<User> router = new Router<User>().configureJwt(
|
||||
Router<UserWorker> router = new Router<UserWorker>().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);
|
||||
}
|
||||
|
@ -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<String, String> tokenGenerator;
|
||||
|
||||
public JsonRepository(File dir, Function<String, String> tokenGenerator){
|
||||
this.dir = dir.getAbsoluteFile();
|
||||
this.tokenGenerator = tokenGenerator;
|
||||
|
||||
this.parser = new JsonParser();
|
||||
}
|
||||
|
||||
private <J extends Json> 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 <T> Result<T> error(int code, String message){
|
||||
return new Result<>(code, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<String> 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<List<String>> getProjects(UserWorker user){
|
||||
try {
|
||||
JsonMap map = read("users.json");
|
||||
List<String> result = new ArrayList<>();
|
||||
for(Object o : map.<JsonMap>get(user.getUsername()).<JsonArray>get("projects").toList()) result.add((String) o);
|
||||
return new Result<>(result);
|
||||
}catch(Exception e){
|
||||
return error(500, "Internal Error");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Project> createProject(UserWorker user, String name) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Project> removeProject(UserWorker user, String name) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Project> getProject(UserWorker user, String name) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> renameProject(UserWorker user, String lastname, String updatedname) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> manageUsers(UserWorker user, String project, List<String> users) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<List<String>> getUsers() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<List<String>> getUsers(String project) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -8,18 +8,21 @@ public interface Repository {
|
||||
|
||||
Result<String> login(String username, String password);
|
||||
|
||||
Result<List<Project>> getProjects(User user);
|
||||
Result<List<String>> getProjects(UserWorker user);
|
||||
|
||||
Result<Project> createProject(User user, String name);
|
||||
Result<Project> createProject(UserWorker user, String name);
|
||||
|
||||
Result<Project> removeProject(User user, String name);
|
||||
Result<Project> removeProject(UserWorker user, String name);
|
||||
|
||||
Result<Project> getProject(User user, String name);
|
||||
Result<Project> getProject(UserWorker user, String name);
|
||||
|
||||
Result<Boolean> renameProject(User user, String lastname, String updatedname);
|
||||
Result<Boolean> renameProject(UserWorker user, String lastname, String updatedname);
|
||||
|
||||
Result<Boolean> manageUsers(User user, String project, List<String> users);
|
||||
Result<Boolean> manageUsers(UserWorker user, String project, List<String> users);
|
||||
|
||||
Result<List<String>> getUsers();
|
||||
|
||||
Result<List<String>> getUsers(String project);
|
||||
|
||||
|
||||
}
|
||||
|
@ -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<E>{
|
||||
|
||||
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<E>{
|
||||
return this.errorCode;
|
||||
}
|
||||
|
||||
public JsonMap getError(){
|
||||
public String getError(){
|
||||
return this.error;
|
||||
}
|
||||
|
||||
|
30
src/be/jeffcheasey88/todo/model/UserWorker.java
Normal file
30
src/be/jeffcheasey88/todo/model/UserWorker.java
Normal file
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
17
src/be/jeffcheasey88/todo/routes/projects/CreateProject.java
Normal file
17
src/be/jeffcheasey88/todo/routes/projects/CreateProject.java
Normal file
@ -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 {
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -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<User> router;
|
||||
private Repository repo;
|
||||
|
||||
public Login(Router<User> 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;
|
||||
}
|
||||
|
||||
JsonMap json = reader.readJson();
|
||||
Result<String> 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 " + this.router.createAuthUser(new User("")));
|
||||
"Authorization: Bearer " + result.getElement());
|
||||
}else{
|
||||
result.send(context, writer);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user