Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configure required and optional symbols for password generation #251

Merged
merged 1 commit into from
Jul 31, 2022

Conversation

snuyanzin
Copy link
Collaborator

@snuyanzin snuyanzin commented Jul 25, 2022

The PR allows configuration of optional and required symbols for password generation.
For instance, this code snippet leads to generation of passwords containing at least 1 lowercase letter, 1 uppercase letter, 1 digit and 1 "special" symbol.

final Password.PasswordRuleConfig config = Password.PasswordSymbolsBuilder.builder()
    .withMinLength(6)
    .withMaxLength(10)
    .with(EN_LOWERCASE, 1)
    .with(EN_UPPERCASE, 1)
    .with(DIGITS, 1)
    .with(DEFAULT_SPECIAL, 1).build();
String password = faker.password().password(config);

in case we want to generate a password containing at least 1 lowercase letter, 2 uppercase letters, 3 digits and 4 "special" symbols we need to adopt times

final Password.PasswordRuleConfig config = Password.PasswordSymbolsBuilder.builder()
    .withMinLength(6)
    .withMaxLength(10)
    .with(EN_LOWERCASE, 1)
    .with(EN_UPPERCASE, 2)
    .with(DIGITS, 3)
    .with(DEFAULT_SPECIAL, 4).build();
String password = faker.password().password(config);

In case number of required symbols is larger than a length of pass to generate than there are 2 options:

  • return an empty string (old behavior) and it is default behavior
  • generate an IllegalArgumentException to do this invoke .throwIfLengthSmall(true) for Password.PasswordSymbolsBuilder

Furthermore now it allows specify other symbols to generate password
for instance, this code will allow to generate a password containing small ru and en letters and custom special symbols:

final Password.PasswordRuleConfig config = Password.PasswordSymbolsBuilder.builder()
    .with(EN_LOWERCASE, 1)
    .with("абвгдеёжзийклмнопрстуфхцчшщъыьэюя", 1)
    .with("!@#$%^&*;'][{}", 1).build();
String password = faker.password().password(config);

@codecov-commenter
Copy link

codecov-commenter commented Jul 25, 2022

Codecov Report

Merging #251 (26ff3f0) into master (ff9bf48) will decrease coverage by 0.04%.
The diff coverage is 92.30%.

@@             Coverage Diff              @@
##             master     #251      +/-   ##
============================================
- Coverage     94.69%   94.64%   -0.05%     
+ Complexity     1888     1877      -11     
============================================
  Files           193      194       +1     
  Lines          3955     3958       +3     
  Branches        393      391       -2     
============================================
+ Hits           3745     3746       +1     
- Misses          110      112       +2     
  Partials        100      100              
Impacted Files Coverage Δ
src/main/java/net/datafaker/Lorem.java 89.79% <85.71%> (-3.25%) ⬇️
src/main/java/net/datafaker/Password.java 91.80% <91.80%> (ø)
src/main/java/net/datafaker/Faker.java 98.19% <100.00%> (+<0.01%) ⬆️
src/main/java/net/datafaker/Internet.java 89.11% <100.00%> (+0.54%) ⬆️

📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more

@bodiam
Copy link
Contributor

bodiam commented Jul 25, 2022

I like it at lot, but do you think it should be part of Lorem? I know the underlying implementation is using Lorem, but wouldn't the builder be more at home in Internet?

@snuyanzin
Copy link
Collaborator Author

Yes it looks also a bit strange for me...
I would extract it into a separate PasswordProvider with wider selection of predefined symbols sets and make reuse it from both Lorem and Internet

@snuyanzin snuyanzin force-pushed the datafaker243 branch 2 times, most recently from aa29526 to c1b67c1 Compare July 30, 2022 16:01
@snuyanzin
Copy link
Collaborator Author

I moved the logic to net.datafaker.Password.
@bodiam could you please have a look?
Once you do not have objections I'll merge it

@bodiam
Copy link
Contributor

bodiam commented Jul 31, 2022

Just had a look, looks fine to me!

@snuyanzin snuyanzin merged commit 64d6054 into datafaker-net:master Jul 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants