feat(password): simplify password validation rules
This commit is contained in:
committed by
Aditya Chandel
parent
4e5b9b633b
commit
e70a8e2497
@@ -32,7 +32,7 @@ public enum ApiError {
|
||||
CANNOT_DELETE_ADMIN(HttpStatus.FORBIDDEN, "Admin user cannot be deleted"),
|
||||
UNAUTHORIZED(HttpStatus.FORBIDDEN, "%s"),
|
||||
PASSWORD_INCORRECT(HttpStatus.BAD_REQUEST, "Incorrect current password"),
|
||||
PASSWORD_WEAK(HttpStatus.BAD_REQUEST, "New password is not strong enough. It must be at least 8 characters long, contain an uppercase letter, a lowercase letter, a number, and a special character."),
|
||||
PASSWORD_TOO_SHORT(HttpStatus.BAD_REQUEST, "Password must be at least 6 characters long"),
|
||||
PASSWORD_SAME_AS_CURRENT(HttpStatus.BAD_REQUEST, "New password cannot be the same as the current password"),
|
||||
INVALID_CREDENTIALS(HttpStatus.BAD_REQUEST, "Invalid credentials");
|
||||
|
||||
|
||||
@@ -103,8 +103,8 @@ public class UserService {
|
||||
throw ApiError.PASSWORD_SAME_AS_CURRENT.createException();
|
||||
}
|
||||
|
||||
if (!isPasswordStrong(changePasswordRequest.getNewPassword())) {
|
||||
throw ApiError.PASSWORD_WEAK.createException();
|
||||
if (!isValidPassword(changePasswordRequest.getNewPassword())) {
|
||||
throw ApiError.PASSWORD_TOO_SHORT.createException();
|
||||
}
|
||||
|
||||
bookLoreUserEntity.setDefaultPassword(false);
|
||||
@@ -114,15 +114,14 @@ public class UserService {
|
||||
|
||||
public void changeUserPassword(ChangeUserPasswordRequest request) {
|
||||
BookLoreUserEntity userEntity = userRepository.findById(request.getUserId()).orElseThrow(() -> ApiError.USER_NOT_FOUND.createException(request.getUserId()));
|
||||
if (!isPasswordStrong(request.getNewPassword())) {
|
||||
throw ApiError.PASSWORD_WEAK.createException();
|
||||
if (!isValidPassword(request.getNewPassword())) {
|
||||
throw ApiError.PASSWORD_TOO_SHORT.createException();
|
||||
}
|
||||
userEntity.setPasswordHash(passwordEncoder.encode(request.getNewPassword()));
|
||||
userRepository.save(userEntity);
|
||||
}
|
||||
|
||||
private boolean isPasswordStrong(String password) {
|
||||
String passwordPattern = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*?&])[A-Za-z\\d@$!%*?&]{8,}$";
|
||||
return password.matches(passwordPattern);
|
||||
private boolean isValidPassword(String password) {
|
||||
return password != null && password.length() >= 6;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user