diff --git a/src/main/java/iz/_11a/passmetric/PasswordController.java b/src/main/java/iz/_11a/passmetric/PasswordController.java index 87f8974..da64119 100644 --- a/src/main/java/iz/_11a/passmetric/PasswordController.java +++ b/src/main/java/iz/_11a/passmetric/PasswordController.java @@ -3,7 +3,7 @@ package iz._11a.passmetric; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; -import java.util.Map; +import java.util.*; @Controller public class PasswordController { @@ -15,19 +15,38 @@ public class PasswordController { @PostMapping(path = "/api/password/strength") @ResponseBody - public Map checkPasswordLive(@RequestParam("password") String password) { - return Map.of("message", analyze(password)); + public Map checkPasswordLive(@RequestParam("password") String password) { + + Map response = new HashMap<>(); + + int score = calculateScore(password); + String strengthText = strengthText(score); + List tips = generateTips(password); + + response.put("strengthText", strengthText); + response.put("progress", score * 20); // pasek postępu 0–100% + response.put("tips", tips); + + return response; } - private String analyze(String password) { + // ------------------------------------------ + // Ocena siły hasła + // ------------------------------------------ + private int calculateScore(String password) { int score = 0; - if (password != null) { - if (password.matches(".*[a-z].*")) score++; - if (password.matches(".*[A-Z].*")) score++; - if (password.matches(".*[0-9].*")) score++; - if (password.matches(".*[@$!%*?&#].*")) score++; - if (password.length() >= 8) score++; - } + if (password == null || password.isEmpty()) return 0; + + if (password.matches(".*[a-z].*")) score++; + if (password.matches(".*[A-Z].*")) score++; + if (password.matches(".*[0-9].*")) score++; + if (password.matches(".*[@$!%*?&#].*")) score++; + if (password.length() >= 12) score++; + + return score; + } + + private String strengthText(int score) { return switch (score) { case 5 -> "Bardzo silne hasło"; case 4 -> "Silne hasło"; @@ -36,4 +55,31 @@ public class PasswordController { default -> "Bardzo słabe hasło"; }; } + + // ------------------------------------------ + // Stopniowe podpowiedzi + // ------------------------------------------ + private List generateTips(String password) { + List tips = new ArrayList<>(); + + if (password == null || password.isEmpty()) { + tips.add("Wpisz hasło, aby rozpocząć analizę."); + return tips; + } + + + if (!password.matches(".*[A-Z].*")) + tips.add("Dodaj co najmniej jedną dużą litere."); + if (!password.matches(".*[0-9].*")) + tips.add("Dodaj co najmniej jedną cyfre."); + if (!password.matches(".*[@$!%*?&#].*")) + tips.add("Dodaj co najmniej jeden znnak specjalny (np. ! @ # $)."); + if (password.length() < 12) + tips.add("Wydłuż hasło do co najmniej 12 znaków."); + + if (tips.isEmpty()) + tips.add("Świetnie! Twoje hasło jest bardzo silne."); + + return tips; + } } diff --git a/src/main/resources/templates/password.html b/src/main/resources/templates/password.html index b4a08b8..49acbb8 100644 --- a/src/main/resources/templates/password.html +++ b/src/main/resources/templates/password.html @@ -5,6 +5,32 @@ PassMetric +

Sprawdź siłę hasła

@@ -14,17 +40,35 @@ + +
+
+
+ +

+ +
    +