|
| 1 | +package com.auth0.exception; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Map; |
| 6 | + |
| 7 | +@SuppressWarnings("unchecked") |
| 8 | +class PasswordStrengthErrorParser { |
| 9 | + private static final String RULE_TYPE_LENGTH_AT_LEAST = "lengthAtLeast"; |
| 10 | + private static final String RULE_TYPE_CONTAINS_AT_LEAST = "containsAtLeast"; |
| 11 | + private static final String RULE_TYPE_SHOULD_CONTAIN = "shouldContain"; |
| 12 | + private static final String RULE_TYPE_IDENTICAL_CHARS = "identicalChars"; |
| 13 | + |
| 14 | + private static final String KEY_RULES = "rules"; |
| 15 | + private static final String KEY_CODE = "code"; |
| 16 | + private static final String KEY_VERIFIED = "verified"; |
| 17 | + private static final String KEY_FORMAT = "format"; |
| 18 | + private static final String KEY_ITEMS = "items"; |
| 19 | + private static final String KEY_MESSAGE = "message"; |
| 20 | + |
| 21 | + private String description; |
| 22 | + |
| 23 | + PasswordStrengthErrorParser(Map<String, Object> descriptionMap) { |
| 24 | + List<Map<String, Object>> rules = (List<Map<String, Object>>) descriptionMap.get(KEY_RULES); |
| 25 | + parseRules(rules); |
| 26 | + } |
| 27 | + |
| 28 | + public String getDescription() { |
| 29 | + return description; |
| 30 | + } |
| 31 | + |
| 32 | + private void parseRules(List<Map<String, Object>> rules) { |
| 33 | + List<String> items = new ArrayList<>(); |
| 34 | + for (Map<String, Object> rule : rules) { |
| 35 | + boolean isVerified = (boolean) rule.get(KEY_VERIFIED); |
| 36 | + if (isVerified) { |
| 37 | + continue; |
| 38 | + } |
| 39 | + String code = (String) rule.get(KEY_CODE); |
| 40 | + switch (code) { |
| 41 | + case RULE_TYPE_LENGTH_AT_LEAST: |
| 42 | + items.add(asLengthAtLeast(rule)); |
| 43 | + break; |
| 44 | + case RULE_TYPE_IDENTICAL_CHARS: |
| 45 | + items.add(asIdenticalChars(rule)); |
| 46 | + break; |
| 47 | + case RULE_TYPE_CONTAINS_AT_LEAST: |
| 48 | + case RULE_TYPE_SHOULD_CONTAIN: |
| 49 | + items.add(asContainsCharset(rule)); |
| 50 | + break; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + this.description = joinStrings("; ", items); |
| 55 | + } |
| 56 | + |
| 57 | + private String asLengthAtLeast(Map<String, Object> rule) { |
| 58 | + List<Number> length = (List<Number>) rule.get(KEY_FORMAT); |
| 59 | + String message = (String) rule.get(KEY_MESSAGE); |
| 60 | + return String.format(message, length.get(0).intValue()); |
| 61 | + } |
| 62 | + |
| 63 | + private String asContainsCharset(Map<String, Object> rule) { |
| 64 | + List<Map<String, Object>> itemsList = (List<Map<String, Object>>) rule.get(KEY_ITEMS); |
| 65 | + List<String> items = new ArrayList<>(); |
| 66 | + for (Map<String, Object> i : itemsList) { |
| 67 | + items.add((String) i.get(KEY_MESSAGE)); |
| 68 | + } |
| 69 | + String requiredItems = joinStrings(", ", items); |
| 70 | + String message = (String) rule.get(KEY_MESSAGE); |
| 71 | + |
| 72 | + if (rule.containsKey(KEY_FORMAT)) { |
| 73 | + List<Number> quantity = (List<Number>) rule.get(KEY_FORMAT); |
| 74 | + message = String.format(message, quantity.get(0).intValue(), quantity.get(1).intValue()); |
| 75 | + } |
| 76 | + |
| 77 | + return String.format("%s %s", message, requiredItems); |
| 78 | + } |
| 79 | + |
| 80 | + private String asIdenticalChars(Map<String, Object> rule) { |
| 81 | + List<Object> example = (List<Object>) rule.get(KEY_FORMAT); |
| 82 | + Number count = (Number) example.get(0); |
| 83 | + String message = (String) rule.get(KEY_MESSAGE); |
| 84 | + return String.format(message, count.intValue(), example.get(1)); |
| 85 | + } |
| 86 | + |
| 87 | + private String joinStrings(String delimiter, List<String> items) { |
| 88 | + StringBuilder sb = new StringBuilder(); |
| 89 | + int i; |
| 90 | + for (i = 0; i < items.size() - 1; i++) { |
| 91 | + sb.append(items.get(i)).append(delimiter); |
| 92 | + } |
| 93 | + sb.append(items.get(i)); |
| 94 | + return sb.toString(); |
| 95 | + } |
| 96 | +} |
0 commit comments