Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,20 @@ private static void convertDeprecatedCredentialsFormat(UserRepresentation user)
logger.warnf("Using deprecated 'credentials' format in JSON representation for user '%s'. It will be removed in future versions", user.getUsername());

if (PasswordCredentialModel.TYPE.equals(cred.getType()) || PasswordCredentialModel.PASSWORD_HISTORY.equals(cred.getType())) {
if (cred.getHashIterations() == null || cred.getAlgorithm() == null || cred.getHashedSaltedValue() == null || cred.getSalt() == null) {
throw new ModelException("Incomplete deprecated credential format for user '" + user.getUsername()
+ "': password credential requires hashIterations, algorithm, hashedSaltedValue, and salt");
}
PasswordCredentialData credentialData = new PasswordCredentialData(cred.getHashIterations(), cred.getAlgorithm());
cred.setCredentialData(JsonSerialization.writeValueAsString(credentialData));
// Created this manually to avoid conversion from Base64 and back
cred.setSecretData("{\"value\":\"" + cred.getHashedSaltedValue() + "\",\"salt\":\"" + cred.getSalt() + "\"}");
cred.setPriority(10);
} else if (OTPCredentialModel.TOTP.equals(cred.getType()) || OTPCredentialModel.HOTP.equals(cred.getType())) {
if (cred.getDigits() == null || cred.getCounter() == null || cred.getPeriod() == null || cred.getAlgorithm() == null || cred.getHashedSaltedValue() == null) {
throw new ModelException("Incomplete deprecated credential format for user '" + user.getUsername()
+ "': OTP credential requires digits, counter, period, algorithm, and hashedSaltedValue");
}
OTPCredentialData credentialData = new OTPCredentialData(cred.getType(), cred.getDigits(), cred.getCounter(), cred.getPeriod(), cred.getAlgorithm(), null);
OTPSecretData secretData = new OTPSecretData(cred.getHashedSaltedValue());
cred.setCredentialData(JsonSerialization.writeValueAsString(credentialData));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@

import java.util.List;

import jakarta.ws.rs.core.Response;

import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.models.FederatedIdentityModel;
import org.keycloak.models.RealmModel;
import org.keycloak.models.UserProvider;
import org.keycloak.representations.idm.CredentialRepresentation;
import org.keycloak.representations.idm.UserRepresentation;
import org.keycloak.testframework.annotations.InjectRealm;
import org.keycloak.testframework.annotations.KeycloakIntegrationTest;
Expand Down Expand Up @@ -377,6 +380,22 @@ private void assertCaseInsensitiveSearch() {
assertThat(realm.admin().users().search("Use", true), hasSize(0));
}

@Test
public void createUserWithIncompleteDeprecatedPasswordCredentialReturnsBadRequest() {
UserRepresentation user = UserBuilder.create()
.username("incomplete-cred-user")
.enabled(true)
.build();

CredentialRepresentation cred = new CredentialRepresentation();
cred.setType(CredentialRepresentation.PASSWORD);
user.setCredentials(List.of(cred));

try (Response response = realm.admin().users().create(user)) {
assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode()));
}
}

private void createUser(UserRepresentation user) {
realm.admin().users().create(user).close();
}
Expand Down