forked from keycloak/keycloak
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: password age in days policy
Closes keycloak#30210 Signed-off-by: Maciej Mierzwa <[email protected]>
- Loading branch information
1 parent
7d42ab8
commit 6b903ab
Showing
6 changed files
with
397 additions
and
4 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
server-spi-private/src/main/java/org/keycloak/policy/AgePasswordPolicyProvider.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,70 @@ | ||
package org.keycloak.policy; | ||
|
||
import org.keycloak.common.util.Time; | ||
import org.keycloak.credential.hash.PasswordHashProvider; | ||
import org.keycloak.models.KeycloakSession; | ||
import org.keycloak.models.PasswordPolicy; | ||
import org.keycloak.models.RealmModel; | ||
import org.keycloak.models.UserModel; | ||
import org.keycloak.models.credential.PasswordCredentialModel; | ||
import org.jboss.logging.Logger; | ||
|
||
import java.time.Duration; | ||
|
||
public class AgePasswordPolicyProvider implements PasswordPolicyProvider { | ||
private static final String ERROR_MESSAGE = "invalidPasswordGenericMessage"; | ||
public static final Logger logger = Logger.getLogger(AgePasswordPolicyProvider.class); | ||
private final KeycloakSession session; | ||
|
||
public AgePasswordPolicyProvider(KeycloakSession session) { | ||
this.session = session; | ||
} | ||
|
||
@Override | ||
public PolicyError validate(String user, String password) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public PolicyError validate(RealmModel realm, UserModel user, String password) { | ||
PasswordPolicy policy = session.getContext().getRealm().getPasswordPolicy(); | ||
int passwordAgePolicyValue = policy.getPolicyConfig(PasswordPolicy.PASSWORD_AGE); | ||
|
||
if (passwordAgePolicyValue != -1) { | ||
//current password check | ||
if (user.credentialManager().getStoredCredentialsByTypeStream(PasswordCredentialModel.TYPE) | ||
.map(PasswordCredentialModel::createFromCredentialModel) | ||
.anyMatch(passwordCredential -> { | ||
PasswordHashProvider hash = session.getProvider(PasswordHashProvider.class, | ||
passwordCredential.getPasswordCredentialData().getAlgorithm()); | ||
return hash != null && hash.verify(password, passwordCredential); | ||
})) { | ||
return new PolicyError(ERROR_MESSAGE, passwordAgePolicyValue); | ||
} | ||
|
||
final long passwordMaxAgeMillis = Time.currentTimeMillis() - Duration.ofDays(passwordAgePolicyValue).toMillis(); | ||
if (passwordAgePolicyValue > 0) { | ||
if (user.credentialManager().getStoredCredentialsByTypeStream(PasswordCredentialModel.PASSWORD_HISTORY) | ||
.filter(credentialModel -> credentialModel.getCreatedDate() > passwordMaxAgeMillis) | ||
.map(PasswordCredentialModel::createFromCredentialModel) | ||
.anyMatch(passwordCredential -> { | ||
PasswordHashProvider hash = session.getProvider(PasswordHashProvider.class, | ||
passwordCredential.getPasswordCredentialData().getAlgorithm()); | ||
return hash.verify(password, passwordCredential); | ||
})) { | ||
return new PolicyError(ERROR_MESSAGE, passwordAgePolicyValue); | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public Object parseConfig(String value) { | ||
return parseInteger(value, AgePasswordPolicyProviderFactory.DEFAULT_AGE_DAYS); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
server-spi-private/src/main/java/org/keycloak/policy/AgePasswordPolicyProviderFactory.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,55 @@ | ||
package org.keycloak.policy; | ||
|
||
import org.keycloak.Config; | ||
import org.keycloak.models.KeycloakSession; | ||
import org.keycloak.models.KeycloakSessionFactory; | ||
import org.keycloak.models.PasswordPolicy; | ||
|
||
public class AgePasswordPolicyProviderFactory implements PasswordPolicyProviderFactory { | ||
public static final Integer DEFAULT_AGE_DAYS = 30; | ||
|
||
@Override | ||
public String getId() { | ||
return PasswordPolicy.PASSWORD_AGE; | ||
} | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Not Recently Used (In Days)"; | ||
} | ||
|
||
@Override | ||
public String getConfigType() { | ||
return PasswordPolicyProvider.INT_CONFIG_TYPE; | ||
} | ||
|
||
@Override | ||
public String getDefaultConfigValue() { | ||
return String.valueOf(DEFAULT_AGE_DAYS); | ||
} | ||
|
||
@Override | ||
public boolean isMultiplSupported() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public PasswordPolicyProvider create(KeycloakSession session) { | ||
return new AgePasswordPolicyProvider(session); | ||
} | ||
|
||
@Override | ||
public void init(Config.Scope config) { | ||
|
||
} | ||
|
||
@Override | ||
public void postInit(KeycloakSessionFactory factory) { | ||
|
||
} | ||
|
||
@Override | ||
public void close() { | ||
|
||
} | ||
} |
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
Oops, something went wrong.