IllegalSequenceRule with custom character sequences example 1

In previous post under Passay, I showed with example the purpose of “IllegalSequenceRule”.

For recap, this rule makes sure that the password doesn’t contain the given character sequence, for example “qwerty” character sequence.

So if the password is “qwerty” or “sam_qwerty_@”, the validation will fail but if the password is “aabb”, the validation passes.

When explaining the usage in the previous post, I used out of the box provided character sequence.

In this post I will show how to create our own custom character sequence, which we can then use with “IllegalSequenceRule”.

For our example I will create two character sequences “12345” and “asdf”.

To create this I will create a enum class that will have these two character sequences and this enum class should implement “SequenceData” interface as shown below

CustomSequenceData

enum CustomSequenceData implements SequenceData {
    CUSTOM_SEQUENCE_1("CUSTOM_SEQUENCE_1", new CharacterSequence[] {new CharacterSequence("12345")}),
    CUSTOM_SEQUENCE_2("CUSTOM_SEQUENCE_2", new CharacterSequence[] {new CharacterSequence("asdf")});

    private String errorCode;
    private CharacterSequence[] characterSequences;

    CustomSequenceData(String errorCode, CharacterSequence[] characterSequences) {
        this.errorCode = errorCode;
        this.characterSequences = characterSequences;
    }

    @Override
    public String getErrorCode() {
        return this.errorCode;
    }

    @Override
    public CharacterSequence[] getSequences() {
        return this.characterSequences;
    }
}

In the above code, I have created enum “CustomSequenceData” which implements “SequenceData” interface provided by Passay and provide logic for its two methods “getErrorCode” and “getSequences”.

At line 2 and 3, when creating instances of enum “CustomSequenceData” we provide the sequences “12345” and “asdf” as constructor arguments.

Next we will show how to use it with “IllegalSequenceRule” class in main method.

Main class

public class Example24 {
    public static void main(String[] args) {
        IllegalSequenceRule illegalSequenceRule = new IllegalSequenceRule(CustomSequenceData.CUSTOM_SEQUENCE_1);
        PasswordValidator passwordValidator = new PasswordValidator(illegalSequenceRule);

        PasswordData passwordData = new PasswordData("aabb");
        RuleResult ruleResult = passwordValidator.validate(passwordData);
        System.out.println("Validation of password 'aabb':" + ruleResult.isValid());

        passwordData = new PasswordData("12345");
        ruleResult = passwordValidator.validate(passwordData);
        System.out.println("Validation of password '12345':" + ruleResult.isValid());

        passwordData = new PasswordData("sam_12345_@");
        ruleResult = passwordValidator.validate(passwordData);
        System.out.println("Validation of password 'sam_12345_@':" + ruleResult.isValid());

        illegalSequenceRule = new IllegalSequenceRule(CustomSequenceData.CUSTOM_SEQUENCE_2, 4, false);
        passwordValidator = new PasswordValidator(illegalSequenceRule);

        passwordData = new PasswordData("aabb");
        ruleResult = passwordValidator.validate(passwordData);
        System.out.println("Validation of password 'aabb':" + ruleResult.isValid());

        passwordData = new PasswordData("asdf");
        ruleResult = passwordValidator.validate(passwordData);
        System.out.println("Validation of password 'asdf':" + ruleResult.isValid());

        passwordData = new PasswordData("sam_asdf_@");
        ruleResult = passwordValidator.validate(passwordData);
        System.out.println("Validation of password 'sam_asdf_@':" + ruleResult.isValid());
    }
}

In the above code, at line 3, I create an instance of “IllegalSequenceRule” with “CUSTOM_SEQUENCE_1”.

This will verify that the password doesn’t contain the sequence “12345”.

Then at line 18, I create another instance of “IllegalSequenceRule” with “CUSTOM_SEQUENCE_2”.

Here I am passing two more constructor arguments to “IllegalSequenceRule” constructor in addition to “CUSTOM_SEQUENCE_2” and they are
1) sequence length –> by default the sequence length used by “IllegalSequenceRule” is 5 but in our case it is less then 5. So we have to override that and set it to 4.
2) boolean to indicate whether to wrap the sequence or not

This new instance will verify that the password doesn’t contain the sequence “asdf”

In this way we can use “IllegalSequenceRule” with custom character sequences.

Below is the output

Output

Validation of password 'aabb':true
Validation of password '12345':false
Validation of password 'sam_12345_@':false
Validation of password 'aabb':true
Validation of password 'asdf':false
Validation of password 'sam_asdf_@':false

Leave a comment