From aea67ae8ec8f9394936ff30374db888fb99e3236 Mon Sep 17 00:00:00 2001 From: Gravit Date: Sun, 1 Sep 2019 20:25:15 +0700 Subject: [PATCH] =?UTF-8?q?[FEATURE]=20=D0=A1=D0=BC=D0=B5=D0=BD=D0=B0=20?= =?UTF-8?q?=D0=BF=D0=B0=D1=80=D0=BE=D0=BB=D1=8F=20=D0=BF=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D0=B7=D0=BE=D0=B2=D0=B0=D1=82=D0=B5=D0=BB=D0=B5=D0=BC=20=D0=B1?= =?UTF-8?q?=D0=B5=D0=B7=20=D1=83=D1=87=D0=B0=D1=81=D1=82=D0=B8=D1=8F=20?= =?UTF-8?q?=D0=B0=D0=B4=D0=BC=D0=B8=D0=BD=D0=B8=D1=81=D1=82=D1=80=D0=B0?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pro/gravit/launchserver/dao/User.java | 2 +- .../launchserver/socket/WebSocketService.java | 11 +--- .../response/auth/SetPasswordResponse.java | 55 +++++++++++++++++++ .../request/SetPasswordRequestEvent.java | 10 ++++ .../websockets/ClientWebSocketService.java | 21 +------ 5 files changed, 70 insertions(+), 29 deletions(-) create mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/SetPasswordResponse.java create mode 100644 LauncherAPI/src/main/java/pro/gravit/launcher/events/request/SetPasswordRequestEvent.java diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/User.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/User.java index c25d8c13..c6766218 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/User.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/User.java @@ -30,13 +30,13 @@ public class User { private long id; @Column(unique = true) public String username; + public String email; @Column(unique = true) public UUID uuid; @Column(name = "password") private byte[] password; private String accessToken; public String serverID; - public String email; private String password_salt; public long permissions; //TODO: заменить EAGER на LASY и придумать способ сохранить сессию diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/WebSocketService.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/WebSocketService.java index dedf17da..a564b4aa 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/WebSocketService.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/WebSocketService.java @@ -1,7 +1,6 @@ package pro.gravit.launchserver.socket; import java.lang.reflect.Type; -import java.util.HashMap; import com.google.gson.Gson; @@ -21,14 +20,7 @@ import pro.gravit.launchserver.socket.response.WebSocketServerResponse; import pro.gravit.launchserver.socket.response.admin.AddLogListenerResponse; import pro.gravit.launchserver.socket.response.admin.ExecCommandResponse; -import pro.gravit.launchserver.socket.response.auth.AuthResponse; -import pro.gravit.launchserver.socket.response.auth.CheckServerResponse; -import pro.gravit.launchserver.socket.response.auth.GetAvailabilityAuthResponse; -import pro.gravit.launchserver.socket.response.auth.JoinServerResponse; -import pro.gravit.launchserver.socket.response.auth.ProfilesResponse; -import pro.gravit.launchserver.socket.response.auth.RegisterResponse; -import pro.gravit.launchserver.socket.response.auth.RestoreSessionResponse; -import pro.gravit.launchserver.socket.response.auth.SetProfileResponse; +import pro.gravit.launchserver.socket.response.auth.*; import pro.gravit.launchserver.socket.response.profile.BatchProfileByUsername; import pro.gravit.launchserver.socket.response.profile.ProfileByUUIDResponse; import pro.gravit.launchserver.socket.response.profile.ProfileByUsername; @@ -132,6 +124,7 @@ public static void registerResponses() { providers.register("verifySecureToken", VerifySecureTokenResponse.class); providers.register("getAvailabilityAuth", GetAvailabilityAuthResponse.class); providers.register("register", RegisterResponse.class); + providers.register("setPassword", SetPasswordResponse.class); } public void sendObject(ChannelHandlerContext ctx, Object obj) { diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/SetPasswordResponse.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/SetPasswordResponse.java new file mode 100644 index 00000000..93f2fb26 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/SetPasswordResponse.java @@ -0,0 +1,55 @@ +package pro.gravit.launchserver.socket.response.auth; + +import io.netty.channel.ChannelHandlerContext; +import pro.gravit.launcher.events.request.SetPasswordRequestEvent; +import pro.gravit.launchserver.dao.User; +import pro.gravit.launchserver.socket.Client; +import pro.gravit.launchserver.socket.response.SimpleResponse; + +public class SetPasswordResponse extends SimpleResponse { + public String oldPassword; + public String newPassword; + public String username; + @Override + public String getType() { + return "setPassword"; + } + + @Override + public void execute(ChannelHandlerContext ctx, Client client) throws Exception { + if(( oldPassword == null && username == null ) || newPassword == null) + { + sendError("Request invalid"); + return; + } + if(!client.isAuth) + { + sendError("You not authorized"); + return; + } + if(username != null && !client.permissions.canAdmin) + { + sendError("You not admin"); + return; + } + if(username != null) + { + User user = server.config.dao.userService.findUserByUsername(username); + user.setPassword(newPassword); + sendResult(new SetPasswordRequestEvent()); + } + else + { + User user = server.config.dao.userService.findUserByUsername(client.username); + if(user.verifyPassword(oldPassword)) + { + user.setPassword(newPassword); + sendResult(new SetPasswordRequestEvent()); + } + else + { + sendError("Old password incorrect"); + } + } + } +} diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/events/request/SetPasswordRequestEvent.java b/LauncherAPI/src/main/java/pro/gravit/launcher/events/request/SetPasswordRequestEvent.java new file mode 100644 index 00000000..96efa2c0 --- /dev/null +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/events/request/SetPasswordRequestEvent.java @@ -0,0 +1,10 @@ +package pro.gravit.launcher.events.request; + +import pro.gravit.launcher.events.RequestEvent; + +public class SetPasswordRequestEvent extends RequestEvent { + @Override + public String getType() { + return "setPassword"; + } +} diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/ClientWebSocketService.java b/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/ClientWebSocketService.java index c16300c8..5b62226d 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/ClientWebSocketService.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/ClientWebSocketService.java @@ -12,25 +12,7 @@ import pro.gravit.launcher.Launcher; import pro.gravit.launcher.events.ExceptionEvent; -import pro.gravit.launcher.events.request.AuthRequestEvent; -import pro.gravit.launcher.events.request.BatchProfileByUsernameRequestEvent; -import pro.gravit.launcher.events.request.CheckServerRequestEvent; -import pro.gravit.launcher.events.request.ErrorRequestEvent; -import pro.gravit.launcher.events.request.ExecCommandRequestEvent; -import pro.gravit.launcher.events.request.GetAvailabilityAuthRequestEvent; -import pro.gravit.launcher.events.request.GetSecureTokenRequestEvent; -import pro.gravit.launcher.events.request.JoinServerRequestEvent; -import pro.gravit.launcher.events.request.LauncherRequestEvent; -import pro.gravit.launcher.events.request.LogEvent; -import pro.gravit.launcher.events.request.ProfileByUUIDRequestEvent; -import pro.gravit.launcher.events.request.ProfileByUsernameRequestEvent; -import pro.gravit.launcher.events.request.ProfilesRequestEvent; -import pro.gravit.launcher.events.request.RegisterRequestEvent; -import pro.gravit.launcher.events.request.RestoreSessionRequestEvent; -import pro.gravit.launcher.events.request.SetProfileRequestEvent; -import pro.gravit.launcher.events.request.UpdateListRequestEvent; -import pro.gravit.launcher.events.request.UpdateRequestEvent; -import pro.gravit.launcher.events.request.VerifySecureTokenRequestEvent; +import pro.gravit.launcher.events.request.*; import pro.gravit.launcher.hasher.HashedEntry; import pro.gravit.launcher.hasher.HashedEntryAdapter; import pro.gravit.launcher.request.WebSocketEvent; @@ -126,6 +108,7 @@ public void registerResults() { results.register("getAvailabilityAuth", GetAvailabilityAuthRequestEvent.class); results.register("exception", ExceptionEvent.class); results.register("register", RegisterRequestEvent.class); + results.register("setpassword", SetPasswordRequestEvent.class); } public void registerHandler(EventHandler eventHandler) {