mirror of
https://github.com/TeamPiped/Piped-Backend.git
synced 2024-08-14 23:51:41 +00:00
add route for changing the password
This commit is contained in:
parent
1261adeb31
commit
b900d161f7
4 changed files with 59 additions and 0 deletions
|
@ -14,6 +14,7 @@ import io.activej.inject.module.Module;
|
|||
import io.activej.launchers.http.MultithreadedHttpServerLauncher;
|
||||
import me.kavin.piped.consts.Constants;
|
||||
import me.kavin.piped.utils.*;
|
||||
import me.kavin.piped.utils.resp.ChangePasswordRequest;
|
||||
import me.kavin.piped.utils.resp.DeleteUserRequest;
|
||||
import me.kavin.piped.utils.resp.ErrorResponse;
|
||||
import me.kavin.piped.utils.resp.LoginRequest;
|
||||
|
@ -369,6 +370,17 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
|
|||
} catch (Exception e) {
|
||||
return getErrorResponse(e, request.getPath());
|
||||
}
|
||||
})).map(POST, "/user/password", AsyncServlet.ofBlocking(executor, request -> {
|
||||
try {
|
||||
ChangePasswordRequest body = Constants.mapper.readValue(request.loadBody().getResult().asArray(),
|
||||
ChangePasswordRequest.class);
|
||||
return getJsonResponse(
|
||||
ResponseHelper.changePasswordResponse(
|
||||
request.getHeader(AUTHORIZATION), body.oldPassword, body.newPassword),
|
||||
"private");
|
||||
} catch (Exception e) {
|
||||
return getErrorResponse(e, request.getPath());
|
||||
}
|
||||
})).map(POST, "/logout", AsyncServlet.ofBlocking(executor, request -> {
|
||||
try {
|
||||
return getJsonResponse(ResponseHelper.logoutResponse(request.getHeader(AUTHORIZATION)), "private");
|
||||
|
|
|
@ -772,6 +772,35 @@ public class ResponseHelper {
|
|||
}
|
||||
}
|
||||
|
||||
public static byte[] changePasswordResponse(String session, String oldPass, String newPass) throws IOException {
|
||||
|
||||
if (StringUtils.isBlank(oldPass) || StringUtils.isBlank(newPass))
|
||||
return mapper.writeValueAsBytes(new InvalidRequestResponse());
|
||||
|
||||
try (Session s = DatabaseSessionFactory.createSession()) {
|
||||
User user = DatabaseHelper.getUserFromSession(session);
|
||||
|
||||
if (user == null)
|
||||
return mapper.writeValueAsBytes(new AuthenticationFailureResponse());
|
||||
|
||||
String hash = user.getPassword();
|
||||
|
||||
if (!hashMatch(hash, oldPass))
|
||||
return mapper.writeValueAsBytes(new IncorrectCredentialsResponse());
|
||||
|
||||
try {
|
||||
var tr = s.beginTransaction();
|
||||
user.setPassword(newPass);
|
||||
s.merge(user);
|
||||
tr.commit();
|
||||
} catch (Exception e) {
|
||||
return mapper.writeValueAsBytes(new ErrorResponse(ExceptionUtils.getStackTrace(e), e.getMessage()));
|
||||
}
|
||||
|
||||
return mapper.writeValueAsBytes(new ChangePasswordResponse(user.getUsername()));
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] subscribeResponse(String session, String channelId)
|
||||
throws IOException {
|
||||
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
package me.kavin.piped.utils.resp;
|
||||
|
||||
public class ChangePasswordRequest {
|
||||
|
||||
public String oldPassword;
|
||||
public String newPassword;
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package me.kavin.piped.utils.resp;
|
||||
|
||||
public class ChangePasswordResponse {
|
||||
|
||||
public String username;
|
||||
|
||||
public ChangePasswordResponse(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue