Skip to content

Commit a8cd4a2

Browse files
rcriosbrbodiam
andauthored
Creates new Credential provider; Deprecated Internet provider methods (#1628)
* Creates new Credential provider; Deprecated Internet provider methods. Improved coverage * Update src/test/java/net/datafaker/providers/base/CredentialTest.java --------- Co-authored-by: Erik Pragt <erik.pragt@gmail.com>
1 parent 47d23e9 commit a8cd4a2

File tree

7 files changed

+407
-0
lines changed

7 files changed

+407
-0
lines changed

src/main/java/net/datafaker/providers/base/BaseProviders.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ default CPF cpf() {
137137
return getProvider(CPF.class, CPF::new);
138138
}
139139

140+
default Credential credential() {
141+
return getProvider(Credential.class, Credential::new);
142+
}
143+
140144
default CryptoCoin cryptoCoin() {
141145
return getProvider(CryptoCoin.class, CryptoCoin::new);
142146
}
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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+
}

src/main/java/net/datafaker/providers/base/Internet.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ protected Internet(BaseProviders faker) {
4040
* @see Name#firstName()
4141
* @see Name#lastName()
4242
*/
43+
@Deprecated(since = "2.4.5", forRemoval = true)
4344
public String username() {
4445
StringBuilder result = new StringBuilder();
4546
final Name name = faker.name();
@@ -259,26 +260,50 @@ public String httpMethod() {
259260
return resolve("internet.http_method");
260261
}
261262

263+
/**
264+
* @deprecated since 2.4.5. Use {@link net.datafaker.providers.base.Credential#password()} instead.
265+
*/
266+
@Deprecated(since = "2.4.5", forRemoval = true)
262267
public String password() {
263268
return password(8, 16);
264269
}
265270

271+
/**
272+
* @deprecated since 2.4.5. Use {@link net.datafaker.providers.base.Credential#password(boolean)} instead.
273+
*/
274+
@Deprecated(since = "2.4.5", forRemoval = true)
266275
public String password(boolean includeDigit) {
267276
return password(8, 16, false, false, includeDigit);
268277
}
269278

279+
/**
280+
* @deprecated since 2.4.5. Use {@link net.datafaker.providers.base.Credential#password(int, int)} instead.
281+
*/
282+
@Deprecated(since = "2.4.5", forRemoval = true)
270283
public String password(int minimumLength, int maximumLength) {
271284
return password(minimumLength, maximumLength, false);
272285
}
273286

287+
/**
288+
* @deprecated since 2.4.5. Use {@link net.datafaker.providers.base.Credential#password(int, int, boolean)} instead.
289+
*/
290+
@Deprecated(since = "2.4.5", forRemoval = true)
274291
public String password(int minimumLength, int maximumLength, boolean includeUppercase) {
275292
return password(minimumLength, maximumLength, includeUppercase, false);
276293
}
277294

295+
/**
296+
* @deprecated since 2.4.5. Use {@link net.datafaker.providers.base.Credential#password(int, int, boolean, boolean)} instead.
297+
*/
298+
@Deprecated(since = "2.4.5", forRemoval = true)
278299
public String password(int minimumLength, int maximumLength, boolean includeUppercase, boolean includeSpecial) {
279300
return password(minimumLength, maximumLength, includeUppercase, includeSpecial, true);
280301
}
281302

303+
/**
304+
* @deprecated since 2.4.5. Use {@link net.datafaker.providers.base.Credential#password(int, int, boolean, boolean, boolean)} instead.
305+
*/
306+
@Deprecated(since = "2.4.5", forRemoval = true)
282307
public String password(int minimumLength, int maximumLength, boolean includeUppercase, boolean includeSpecial, boolean includeDigit) {
283308
return faker.text().text(minimumLength, maximumLength, includeUppercase, includeSpecial, includeDigit);
284309
}

src/main/java/net/datafaker/service/files/EnFile.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public String getPath() {
7777
"cosmere.yml",
7878
"country.yml",
7979
"cowboy_bebop.yml",
80+
"credential.yml",
8081
"cricket.yml",
8182
"crypto_coin.yml",
8283
"culture_series.yml",
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
en:
2+
faker:
3+
credential:
4+
weak_password:
5+
- "123456"
6+
- "password"
7+
- "123456789"
8+
- "12345678"
9+
- "12345"
10+
- "111111"
11+
- "1234567"
12+
- "sunshine"
13+
- "qwerty"
14+
- "iloveyou"
15+
- "princess"
16+
- "admin"
17+
- "welcome"
18+
- "666666"
19+
- "abc123"
20+
- "football"
21+
- "123123"
22+
- "monkey"
23+
- "654321"
24+
- "!@#$%^&*"
25+
- "charlie"
26+
- "aa123456"
27+
- "donald"
28+
- "password1"
29+
- "qwerty123"
30+
uid_pattern:
31+
- "\\d{6}"
32+
- "[A-Z]{1}\\d{5}"

0 commit comments

Comments
 (0)