Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
increase minimum password strength
- Loading branch information
Showing
7 changed files
with
89 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/com/commafeed/frontend/auth/PasswordConstraintValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.commafeed.frontend.auth; | ||
|
||
import java.util.List; | ||
|
||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
|
||
import org.passay.CharacterRule; | ||
import org.passay.EnglishCharacterData; | ||
import org.passay.LengthRule; | ||
import org.passay.PasswordData; | ||
import org.passay.PasswordValidator; | ||
import org.passay.RuleResult; | ||
import org.passay.WhitespaceRule; | ||
|
||
public class PasswordConstraintValidator implements ConstraintValidator<ValidPassword, String> { | ||
|
||
@Override | ||
public void initialize(ValidPassword constraintAnnotation) { | ||
// nothing to do | ||
} | ||
|
||
@Override | ||
public boolean isValid(String value, ConstraintValidatorContext context) { | ||
PasswordValidator validator = buildPasswordValidator(); | ||
RuleResult result = validator.validate(new PasswordData(value)); | ||
|
||
if (result.isValid()) { | ||
return true; | ||
} | ||
|
||
List<String> messages = validator.getMessages(result); | ||
String message = String.join(System.lineSeparator(), messages); | ||
context.buildConstraintViolationWithTemplate(message).addConstraintViolation().disableDefaultConstraintViolation(); | ||
return false; | ||
} | ||
|
||
private PasswordValidator buildPasswordValidator() { | ||
return new PasswordValidator( | ||
// length | ||
new LengthRule(8, 128), | ||
// 1 uppercase char | ||
new CharacterRule(EnglishCharacterData.UpperCase, 1), | ||
// 1 lowercase char | ||
new CharacterRule(EnglishCharacterData.LowerCase, 1), | ||
// 1 digit | ||
new CharacterRule(EnglishCharacterData.Digit, 1), | ||
// 1 special char | ||
new CharacterRule(EnglishCharacterData.Special, 1), | ||
// no whitespace | ||
new WhitespaceRule()); | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/commafeed/frontend/auth/ValidPassword.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.commafeed.frontend.auth; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import javax.validation.Constraint; | ||
import javax.validation.Payload; | ||
|
||
@Documented | ||
@Constraint(validatedBy = PasswordConstraintValidator.class) | ||
@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.ANNOTATION_TYPE }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface ValidPassword { | ||
|
||
String message() default "Invalid Password"; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters