|
| 1 | +package net.datafaker.providers.base; |
| 2 | + |
| 3 | +import java.util.regex.Pattern; |
| 4 | +import java.util.regex.PatternSyntaxException; |
| 5 | + |
| 6 | +/** |
| 7 | + * Generates credentials such as usernames, uids and passwords. |
| 8 | + * |
| 9 | + * @since 2.4.5 |
| 10 | + */ |
| 11 | +public class Credential extends AbstractProvider<BaseProviders> { |
| 12 | + |
| 13 | + public static final int MIN_PASSWORD_LENGTH = 8; |
| 14 | + public static final int MAX_PASSWORD_LENGTH = 16; |
| 15 | + |
| 16 | + protected Credential(BaseProviders faker) { |
| 17 | + super(faker); |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * A lowercase username composed of the first_name and last_name joined with a '.'. Some examples are: |
| 22 | + * <ul> |
| 23 | + * <li>(template) {@link Name#firstName()}.{@link Name#lastName()}</li> |
| 24 | + * <li>jim.jones</li> |
| 25 | + * <li>jason.leigh</li> |
| 26 | + * <li>tracy.jordan</li> |
| 27 | + * </ul> |
| 28 | + * |
| 29 | + * @return a random two part username. |
| 30 | + * @see Name#firstName() |
| 31 | + * @see Name#lastName() |
| 32 | + */ |
| 33 | + public String username() { |
| 34 | + StringBuilder result = new StringBuilder(); |
| 35 | + final Name name = faker.name(); |
| 36 | + final String firstName = name.firstName().toLowerCase(faker.getContext().getLocale()) |
| 37 | + + "." + name.lastName().toLowerCase(faker.getContext().getLocale()); |
| 38 | + for (int i = 0; i < firstName.length(); i++) { |
| 39 | + final char c = firstName.charAt(i); |
| 40 | + if (c == '\'' || Character.isWhitespace(c)) { |
| 41 | + continue; |
| 42 | + } |
| 43 | + result.append(c); |
| 44 | + } |
| 45 | + return result.toString(); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Generates a password, only with lowercase letters, numbers and |
| 50 | + * with length between 8 and 16 characters. |
| 51 | + * |
| 52 | + * @return A randomly generated password |
| 53 | + */ |
| 54 | + public String password() { |
| 55 | + return password(MIN_PASSWORD_LENGTH, MAX_PASSWORD_LENGTH); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Generates a password, only with lowercase letters and optionally numbers |
| 60 | + * with length between 8 and 16 characters. |
| 61 | + * |
| 62 | + * @param includeDigit if true, the password will contain at least one digit |
| 63 | + * @return A randomly generated password |
| 64 | + */ |
| 65 | + public String password(boolean includeDigit) { |
| 66 | + return password(MIN_PASSWORD_LENGTH, MAX_PASSWORD_LENGTH, false, false, includeDigit); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Generates a password, only with lowercase letters, numbers and |
| 71 | + * with min and max length defined by the user. |
| 72 | + * |
| 73 | + * @param minimumLength the minimum length of the password |
| 74 | + * @param maximumLength the maximum length of the password |
| 75 | + * @return A randomly generated password |
| 76 | + */ |
| 77 | + public String password(int minimumLength, int maximumLength) { |
| 78 | + return password(minimumLength, maximumLength, false); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Generates a password with lowercase and optionally uppercase letters, numbers |
| 83 | + * and with min and max length defined by the user. |
| 84 | + * |
| 85 | + * @param minimumLength the minimum length of the password |
| 86 | + * @param maximumLength the maximum length of the password |
| 87 | + * @param includeUppercase if true, the password will contain at least one uppercase letter |
| 88 | + * @return A randomly generated password |
| 89 | + */ |
| 90 | + public String password(int minimumLength, int maximumLength, boolean includeUppercase) { |
| 91 | + return password(minimumLength, maximumLength, includeUppercase, false); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Generates a password with lowercase letters, numbers and optionally uppercase letters and |
| 96 | + * special characters and with min and max length defined by the user. |
| 97 | + * |
| 98 | + * @param minimumLength the minimum length of the password |
| 99 | + * @param maximumLength the maximum length of the password |
| 100 | + * @param includeUppercase if true, the password will contain at least one uppercase letter |
| 101 | + * @param includeSpecial if true, the password will contain at least one special character |
| 102 | + * @return A randomly generated password |
| 103 | + */ |
| 104 | + public String password(int minimumLength, int maximumLength, boolean includeUppercase, boolean includeSpecial) { |
| 105 | + return password(minimumLength, maximumLength, includeUppercase, includeSpecial, true); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Generates a password with lowercase letters and optionally uppercase letters, numbers and |
| 110 | + * special characters and with min and max length defined by the user. |
| 111 | + * |
| 112 | + * @param minimumLength the minimum length of the password |
| 113 | + * @param maximumLength the maximum length of the password |
| 114 | + * @param includeUppercase if true, the password will contain at least one uppercase letter |
| 115 | + * @param includeSpecial if true, the password will contain at least one special character |
| 116 | + * @param includeDigit if true, the password will contain at least one digit |
| 117 | + * @return A randomly generated password |
| 118 | + */ |
| 119 | + public String password(int minimumLength, int maximumLength, boolean includeUppercase, boolean includeSpecial, boolean includeDigit) { |
| 120 | + return faker.text().text(minimumLength, maximumLength, includeUppercase, includeSpecial, includeDigit); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Returns a weak password from a pre-defined list of common weak passwords. |
| 125 | + * |
| 126 | + * Some examples are: |
| 127 | + * <ul> |
| 128 | + * <li>123456</li> |
| 129 | + * <li>password</li> |
| 130 | + * <li>qwerty</li> |
| 131 | + * </ul> |
| 132 | + * |
| 133 | + * @return a random weak password. |
| 134 | + */ |
| 135 | + public String weakPassword() { |
| 136 | + return resolve("credential.weak_password"); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Generates a user ID based on the regex pattern defined in the resource file. |
| 141 | + * If the regex is null or invalid, it returns null. |
| 142 | + * |
| 143 | + * @return A randomly generated user ID based on the regex or null if the regex is null or invalid |
| 144 | + */ |
| 145 | + public String userId() { |
| 146 | + return userId(resolve("credential.uid_pattern")); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Generates a user ID based on the provided regex pattern. |
| 151 | + * If the regex is null or invalid, it returns null. |
| 152 | + * |
| 153 | + * @param regex The regex pattern to generate the user ID |
| 154 | + * @return A randomly generated user ID based on the regex or null if the regex is null or invalid |
| 155 | + */ |
| 156 | + public String userId(String regex) { |
| 157 | + if(regex == null) { |
| 158 | + return null; |
| 159 | + } |
| 160 | + |
| 161 | + try { |
| 162 | + Pattern.compile(regex); |
| 163 | + } catch (PatternSyntaxException e) { |
| 164 | + return null; |
| 165 | + } |
| 166 | + |
| 167 | + return faker.regexify(regex); |
| 168 | + } |
| 169 | +} |
0 commit comments