feat: switched-on password rules for the hint, named refusals
The system exposes the switched-on admin password rules. mobile-api serves them to signed-in admins and, rate limited, on an exact public route for the first-login screen. Every admin password path now refuses with PASSWORD_RULE naming the first rule broken. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
1 parent
0b201a51ca
commit
5e68478670
15 files changed
+494
-3
No files matched your search
+2
@@ -56,6 +56,8 @@ public class RestApiSystemServiceConstants {
|
||||
|
||||
//Race
|
||||
|
||||
public static final String API_REST_SYSTEM_ADMIN_ACTIVE_PASSWORD_RULES = "/passwordRule/adminActive";
|
||||
|
||||
//Message
|
||||
public static final String API_REST_SYSTEM_MESSAGE_JSON_BY_AREA_AND_LOCALE = "/message/getMessageJsonByAreaAndLocale/{area}/{locale}";
|
||||
|
||||
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.solvway.mobile.api.controller;
|
||||
|
||||
import com.solvway.core.annotation.AppAuth;
|
||||
import com.solvway.mobile.api.service.PasswordRuleHintService;
|
||||
import com.solvway.mobile.api.useraccess.AdminModule;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/** Switched-on admin password rules. Must stay under {@code controller..} for AppAccessAspect. */
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
public class PasswordRuleController {
|
||||
private final PasswordRuleHintService rules;
|
||||
|
||||
@AdminModule(AdminModule.ANY_ADMIN)
|
||||
@AppAuth
|
||||
@GetMapping("/api/admin/password-rules/active")
|
||||
public List<Map<String, Object>> adminActive() {
|
||||
return rules.active();
|
||||
}
|
||||
|
||||
@AppAuth(skipValidate = true)
|
||||
@GetMapping("/api/public/password-rules")
|
||||
public List<Map<String, Object>> publicActive() {
|
||||
return rules.publicActive();
|
||||
}
|
||||
}
|
||||
+5
-1
@@ -104,6 +104,7 @@ public class AdminAuthController {
|
||||
return ResponseEntity.badRequest().body(Map.of("message", "New password is required."));
|
||||
}
|
||||
if (!adminAuthenticationService.firstTimeLogin(u.getId(), request.newPassword())) {
|
||||
adminAuthenticationService.requirePasswordRules(request.newPassword()); // throws PASSWORD_RULE naming the rule
|
||||
return ResponseEntity.badRequest().body(Map.of("message", "New password does not meet the password policy."));
|
||||
}
|
||||
return ResponseEntity.ok(Map.of("result", "true"));
|
||||
@@ -140,7 +141,10 @@ public class AdminAuthController {
|
||||
throw coded(HttpStatus.BAD_REQUEST, "Current password is incorrect", "CURRENT_PASSWORD_WRONG");
|
||||
}
|
||||
case "PASSWORD_SAME" -> throw coded(HttpStatus.BAD_REQUEST, "The new password must differ from the current one", "PASSWORD_SAME");
|
||||
case "PASSWORD_POLICY" -> throw coded(HttpStatus.BAD_REQUEST, "Password does not meet the password policy", "PASSWORD_POLICY");
|
||||
case "PASSWORD_POLICY" -> {
|
||||
adminAuthenticationService.requirePasswordRules(request.newPassword()); // throws PASSWORD_RULE naming the rule
|
||||
throw coded(HttpStatus.BAD_REQUEST, "Password does not meet the password policy", "PASSWORD_POLICY");
|
||||
}
|
||||
default -> throw coded(HttpStatus.INTERNAL_SERVER_ERROR, "The password was not changed", "SAVE_FAILED");
|
||||
}
|
||||
}
|
||||
|
||||
+13
@@ -5,6 +5,7 @@ import com.solvway.mobile.api.constant.api.RestApiAuthServiceConstants;
|
||||
import com.solvway.mobile.api.constant.api.RestApiSystemServiceConstants;
|
||||
import com.solvway.mobile.api.model.AuthRequest;
|
||||
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
|
||||
import com.solvway.mobile.api.errorhandler.CodedResponseStatusException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -56,6 +57,18 @@ public class AdminAuthenticationService {
|
||||
return ent.getBody() != null && ent.getBody().isEmpty();
|
||||
}
|
||||
|
||||
/** Throws 400 PASSWORD_RULE naming the first failed rule (param, value) when the password breaks a switched-on rule. */
|
||||
public void requirePasswordRules(String password) {
|
||||
ResponseEntity<List> ent = restTemplateUtil.postForEntity(
|
||||
restTemplateUtil.getURLforApiService(RestApiSystemServiceConstants.API_REST_SYSTEM_ADMIN_VALIDATE_PASSWORD_RULES),
|
||||
List.class, Map.of("password", password));
|
||||
if (ent.getBody() == null || ent.getBody().isEmpty() || !(ent.getBody().get(0) instanceof Map<?, ?> first)) return;
|
||||
CodedResponseStatusException e = new CodedResponseStatusException(HttpStatus.BAD_REQUEST, "PASSWORD_RULE", "PASSWORD_RULE");
|
||||
e.getBody().setProperty("param", first.get("param"));
|
||||
e.getBody().setProperty("value", first.get("value"));
|
||||
throw e;
|
||||
}
|
||||
|
||||
/** Stores an admin-typed password with a forced change at next login; returns rows updated (0 = refused). */
|
||||
public int setAdminPassword(Long id, String password) {
|
||||
ResponseEntity<Integer> ent = restTemplateUtil.postForEntity(
|
||||
|
||||
@@ -218,6 +218,7 @@ public class AdminUserService {
|
||||
if (!StringUtils.hasText(password)) throw error(HttpStatus.BAD_REQUEST, "Password is required", "REQUIRED_FIELD");
|
||||
if (!password.equals(confirmPassword)) throw error(HttpStatus.BAD_REQUEST, "Passwords do not match", "PASSWORD_MISMATCH");
|
||||
if (!adminAuthenticationService.passesAdminPasswordRules(password)) {
|
||||
adminAuthenticationService.requirePasswordRules(password); // throws PASSWORD_RULE naming the rule
|
||||
throw error(HttpStatus.BAD_REQUEST, "Password does not meet the password policy", "PASSWORD_POLICY");
|
||||
}
|
||||
}
|
||||
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.solvway.mobile.api.service;
|
||||
|
||||
import com.solvway.mobile.api.constant.api.RestApiSystemServiceConstants;
|
||||
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/** The switched-on admin password rules, for the hint on password screens. */
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class PasswordRuleHintService {
|
||||
private static final ParameterizedTypeReference<List<Map<String, Object>>> RULES = new ParameterizedTypeReference<>() {};
|
||||
private static final int PUBLIC_LIMIT = 60;
|
||||
|
||||
private final RestTemplateCustomUtil rest;
|
||||
private final PublicRegistrationService limiter;
|
||||
|
||||
public List<Map<String, Object>> active() {
|
||||
List<Map<String, Object>> body = rest.getForEntity(
|
||||
rest.getURLforApiService(RestApiSystemServiceConstants.API_REST_SYSTEM_ADMIN_ACTIVE_PASSWORD_RULES), RULES).getBody();
|
||||
return body == null ? List.of() : body;
|
||||
}
|
||||
|
||||
/** Same list with no sign-in, counted per IP in the registration limiter (fixed 10-minute window). */
|
||||
public List<Map<String, Object>> publicActive() {
|
||||
limiter.limit("pwr:" + PublicRegistrationService.ip(), PUBLIC_LIMIT, "Too many requests from this device. Try again later.");
|
||||
return active();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user