No Special Chars in KeyCloak - Password Policy

Hello good.

I try to add the No special characters policy in the password.

I thought it was as simple as adding a 0 to the policy, however, this is not working.

Reviewing the code in the provider I found this:

As you can see in the code, the “count” variable is compared with “min” (the value that was defined in the policy)
It is possible to validate when the number of characters found in the password is less than the defined number, generating the error, but when the min variable is 0, the error is never generated.

I am looking for a way to prevent special characters from being used in the password. Does anyone know a way to do it.?

An alternative may be with regular expressions, but I don’t know if there is a simpler way with any other policy.

Hello @Abeldlfa1, if you need to prevent special characters from being used in the password, the simplest (and probably best) way is to use a regular expression password policy.

Another way to achieve this would be to code your own custom policy, called “NoSpecialCharsPasswordPolicy”. But this approach is more complexe if you’ve never develop custom providers, and by the way, you’ll probably end up coding a “validate” method that ressemble a lot to the “validate” method of the “RegexPatternsPasswordPolicyProvider”. Not worth it.

1 Like

I used a regex to achieve it because I did not find another way to do it with the current password policy.

I get it. Thanks a lot for your response, skydrinker-tox