Repository navigation
Make PasswordPolicyProvider SPI more flexible #26517
thomasdarimont
started this conversation in
Ideas
Replies: 2 comments
|
Especially when password policies vary among users, it would be good if each policy can also provide (a configuration dependent) password requirement text in addition to the error text. This should be used to render password requirements to the user on registration or also to the admin who may set a password. |
0 replies
|
Did anything ever come of this idea? |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Overview
Currently, Keycloak supports a decent set of password policy rules that can be used to compose complex password policies on the realm level. However, in many projects, an often raised requirement is the support for multiple password policies, which can be applied to a subset of users, e.g., for all members of a group or user with a specific attribute.
Such requirements are currently challenging to implement because the password policy API and implementations implicitly load their configuration from the
PasswordPolicyobtained from the realm.An example of this case can be seen in the validate method of the LengthPasswordPolicyProvider.
Since the configuration is fetched from the realm, feeding a context-specific password policy into the password validation is challenging.
Design
If we would slightly change the password policy SPI, e.g., by allowing to pass in the
PasswordPolicydirectly to thevalidatemethod, we could effectively have password policies that are computed in a context-specific way, e.g., based on a group/tenant attribute, user attribute, etc.This approach would allow the users to declare flexible password policies that could be derived dynamically.
Goals
Possible Solution
In my branch refactor-password-policy, I experimented with this approach, and I think this could be implemented with just a few minor changes in a backward compatible way.
In my PoC I introduced a
PasswordPolicyContextwhich holds the data necessary for password policy validation.This
PasswordPolicyContextis now passed to a new methodPolicyError validate(String password, PasswordPolicyContext policyContext);which I added to thePasswordPolicyManagerProviderandPasswordPolicyProviderinterfaces and respective implementations, e.g. DefaultPasswordPolicyMnaagerProvider.I adapted the internal password validation code to delegate to the new
PolicyError validate(String password, PasswordPolicyContext policyContext);method. Here is an example for the LengthPasswordPolicyProvider.This change now allows users to perform password policy validation with a custom
PasswordPolicyContext, which can be populated with an explicitPasswordPolicyderived from some context, e.g., a group attribute, etc.IMHO, this provides enough flexibility for context-specific password policy definitions and validations.
As a positive side effect, this eases testing of
PasswordPolicyProviderimplementations with different configurations since we can pass in a customPasswordPolicyvia thePasswordPolicyContextdirectly.If you think this makes sense, I'm more than happy to send a PR :)
All reactions