use of org.keycloak.models.credential.PasswordCredentialModel in project keycloak by keycloak.
the class CredentialModelBackwardsCompatibilityTest method testCredentialModelPassword.
@Test
public void testCredentialModelPassword() {
byte[] salt = { 1, 2, 3 };
CredentialModel password = PasswordCredentialModel.createFromValues("foo", salt, 1000, "pass");
Assert.assertEquals("pass", password.getValue());
Assert.assertTrue(Arrays.areEqual(salt, password.getSalt()));
Assert.assertEquals(1000, password.getHashIterations());
Assert.assertEquals("foo", password.getAlgorithm());
// Change something and assert it is changed
password.setValue("789789");
Assert.assertEquals("789789", password.getValue());
// Test clone
PasswordCredentialModel cloned = PasswordCredentialModel.createFromCredentialModel(password);
Assert.assertEquals("789789", cloned.getPasswordSecretData().getValue());
Assert.assertEquals(1000, cloned.getPasswordCredentialData().getHashIterations());
Assert.assertEquals(1000, cloned.getPasswordCredentialData().getHashIterations());
Assert.assertEquals("foo", cloned.getPasswordCredentialData().getAlgorithm());
}
use of org.keycloak.models.credential.PasswordCredentialModel in project keycloak by keycloak.
the class PasswordHashProvider method encode.
/**
* @deprecated Exists due the backwards compatibility. It is recommended to use {@link #encodedCredential(String, int)}}
*/
@Deprecated
default void encode(String rawPassword, int iterations, CredentialModel credential) {
PasswordCredentialModel passwordCred = encodedCredential(rawPassword, iterations);
credential.setCredentialData(passwordCred.getCredentialData());
credential.setSecretData(passwordCred.getSecretData());
}
use of org.keycloak.models.credential.PasswordCredentialModel in project keycloak by keycloak.
the class PasswordCredentialProvider method isValid.
@Override
public boolean isValid(RealmModel realm, UserModel user, CredentialInput input) {
if (!(input instanceof UserCredentialModel)) {
logger.debug("Expected instance of UserCredentialModel for CredentialInput");
return false;
}
if (input.getChallengeResponse() == null) {
logger.debugv("Input password was null for user {0} ", user.getUsername());
return false;
}
PasswordCredentialModel password = getPassword(realm, user);
if (password == null) {
logger.debugv("No password cached or stored for user {0} ", user.getUsername());
return false;
}
PasswordHashProvider hash = session.getProvider(PasswordHashProvider.class, password.getPasswordCredentialData().getAlgorithm());
if (hash == null) {
logger.debugv("PasswordHashProvider {0} not found for user {1} ", password.getPasswordCredentialData().getAlgorithm(), user.getUsername());
return false;
}
if (!hash.verify(input.getChallengeResponse(), password)) {
logger.debugv("Failed password validation for user {0} ", user.getUsername());
return false;
}
PasswordPolicy policy = realm.getPasswordPolicy();
if (policy == null) {
return true;
}
hash = getHashProvider(policy);
if (hash == null) {
return true;
}
if (hash.policyCheck(policy, password)) {
return true;
}
PasswordCredentialModel newPassword = hash.encodedCredential(input.getChallengeResponse(), policy.getHashIterations());
newPassword.setId(password.getId());
newPassword.setCreatedDate(password.getCreatedDate());
newPassword.setUserLabel(password.getUserLabel());
getCredentialStore().updateCredential(realm, user, newPassword);
UserCache userCache = session.userCache();
if (userCache != null) {
userCache.evict(realm, user);
}
return true;
}
use of org.keycloak.models.credential.PasswordCredentialModel in project keycloak by keycloak.
the class PasswordCredentialProvider method getPassword.
public PasswordCredentialModel getPassword(RealmModel realm, UserModel user) {
List<CredentialModel> passwords = null;
if (user instanceof CachedUserModel && !((CachedUserModel) user).isMarkedForEviction()) {
CachedUserModel cached = (CachedUserModel) user;
passwords = (List<CredentialModel>) cached.getCachedWith().get(PASSWORD_CACHE_KEY);
}
// if the model was marked for eviction while passwords were initialized, override it from credentialStore
if (!(user instanceof CachedUserModel) || ((CachedUserModel) user).isMarkedForEviction()) {
passwords = getCredentialStore().getStoredCredentialsByTypeStream(realm, user, getType()).collect(Collectors.toList());
}
if (passwords == null || passwords.isEmpty())
return null;
return PasswordCredentialModel.createFromCredentialModel(passwords.get(0));
}
use of org.keycloak.models.credential.PasswordCredentialModel in project keycloak by keycloak.
the class PasswordCredentialProvider method createCredential.
public boolean createCredential(RealmModel realm, UserModel user, String password) {
PasswordPolicy policy = realm.getPasswordPolicy();
PolicyError error = session.getProvider(PasswordPolicyManagerProvider.class).validate(realm, user, password);
if (error != null)
throw new ModelException(error.getMessage(), error.getParameters());
PasswordHashProvider hash = getHashProvider(policy);
if (hash == null) {
return false;
}
PasswordCredentialModel credentialModel = hash.encodedCredential(password, policy.getHashIterations());
credentialModel.setCreatedDate(Time.currentTimeMillis());
createCredential(realm, user, credentialModel);
return true;
}
Aggregations