only change password, docs and mail left

This commit is contained in:
Jose
2026-01-30 22:37:39 +01:00
parent 33b3a460ca
commit 4303caaf74
10 changed files with 131 additions and 48 deletions

View File

@@ -4,6 +4,7 @@ import java.util.List;
import java.util.Map;
import java.util.UUID;
import net.miarma.backlib.dto.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.password.PasswordEncoder;
@@ -14,25 +15,16 @@ import net.miarma.backend.core.model.Credential;
import net.miarma.backend.core.service.AuthService;
import net.miarma.backend.core.service.CredentialService;
import net.miarma.backlib.security.JwtService;
import net.miarma.backlib.dto.ChangePasswordRequest;
import net.miarma.backlib.dto.LoginRequest;
import net.miarma.backlib.dto.LoginResponse;
import net.miarma.backlib.dto.RegisterRequest;
@RestController
@RequestMapping("/auth")
public class AuthController {
private final CredentialService credentialService;
private final JwtService jwtService;
private final PasswordEncoder passwordEncoder;
private final AuthService authService;
public AuthController(CredentialService credentialService, JwtService jwtService,
PasswordEncoder passwordEncoder, AuthService authService) {
this.credentialService = credentialService;
public AuthController(JwtService jwtService, AuthService authService) {
this.jwtService = jwtService;
this.passwordEncoder = passwordEncoder;
this.authService = authService;
}
@@ -52,12 +44,26 @@ public class AuthController {
@PostMapping("/refresh")
public ResponseEntity<?> refreshToken(@RequestHeader("Authorization") String authHeader) {
if (authHeader == null || !authHeader.startsWith("Bearer ")) {
return ResponseEntity.status(401).body("Token missing");
return ResponseEntity.status(401).body(
new ApiErrorDto(
401,
"Unauthorized",
"No hay token",
"/v2/core/auth/change-password"
)
);
}
String token = authHeader.substring(7);
if (!jwtService.validateToken(token)) {
return ResponseEntity.status(401).body("Invalid token");
return ResponseEntity.status(401).body(
new ApiErrorDto(
401,
"Unauthorized",
"Invalid token",
"/v2/core/auth/change-password"
)
);
}
UUID userId = jwtService.getUserId(token);
@@ -71,40 +77,42 @@ public class AuthController {
"serviceId", serviceId
));
}
@PostMapping("/change-password")
public ResponseEntity<?> changePassword(
@RequestHeader("Authorization") String authHeader,
@Valid @RequestBody ChangePasswordRequest request
@RequestBody ChangePasswordRequest request
) {
if (authHeader == null || !authHeader.startsWith("Bearer ")) {
return ResponseEntity.status(401).body("Token missing");
return ResponseEntity.status(401).body(
new ApiErrorDto(
401,
"Unauthorized",
"No hay token",
"/v2/core/auth/change-password"
)
);
}
String token = authHeader.substring(7);
if (!jwtService.validateToken(token)) {
return ResponseEntity.status(401).body("Invalid token");
return ResponseEntity.status(401).body(
new ApiErrorDto(
401,
"Unauthorized",
"Invalid token",
"/v2/core/auth/change-password"
)
);
}
UUID userId = jwtService.getUserId(token);
Credential cred = credentialService.getByUserId(userId)
.stream()
.filter(c -> c.getServiceId().equals(request.serviceId()))
.findFirst().get();
if (cred == null) {
return ResponseEntity.status(404).body("Credential not found");
}
if (!passwordEncoder.matches(request.oldPassword(), cred.getPassword())) {
return ResponseEntity.status(400).body("Old password is incorrect");
}
credentialService.updatePassword(cred.getCredentialId(), request);
return ResponseEntity.ok(Map.of("message", "Password changed successfully"));
authService.changePassword(userId, request);
return ResponseEntity.ok(Map.of("message", "Contraseña cambiada correctamente"));
}
@GetMapping("/validate")
public ResponseEntity<Boolean> validate(@RequestHeader("Authorization") String authHeader) {
String token = authHeader.substring(7);

View File

@@ -3,9 +3,7 @@ package net.miarma.backend.core.service;
import java.util.UUID;
import net.miarma.backlib.dto.*;
import net.miarma.backlib.exception.ConflictException;
import net.miarma.backlib.exception.ForbiddenException;
import net.miarma.backlib.exception.UnauthorizedException;
import net.miarma.backlib.exception.*;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
@@ -75,4 +73,22 @@ public class AuthService {
return new LoginResponse(token, UserMapper.toDto(user), CredentialMapper.toDto(cred));
}
public void changePassword(UUID userId, ChangePasswordRequest request) {
Credential cred = credentialService.getByUserId(userId)
.stream()
.filter(c -> c.getServiceId().equals(request.serviceId()))
.findFirst()
.orElseThrow(() -> new NotFoundException("Cuenta no encontrada"));
if (!passwordEncoder.matches(request.oldPassword(), cred.getPassword())) {
throw new ValidationException("oldPassword", "La contraseña actual es incorrecta");
}
if (request.newPassword().length() < 8) {
throw new ValidationException("newPassword", "La nueva contraseña debe tener al menos 8 caracteres");
}
credentialService.updatePassword(cred.getCredentialId(), request);
}
}