use of org.keycloak.models.credential.dto.PasswordCredentialData in project keycloak by keycloak.
the class CredentialModelTest method canCreatedExtendedCredentialModel.
@Test
public void canCreatedExtendedCredentialModel() throws IOException {
PasswordCredentialData credentialData = new PasswordCredentialData(1000, "bcrypt", Collections.singletonMap("cost", Collections.singletonList("18")));
PasswordSecretData secretData = new PasswordSecretData("secretValue", "AAAAAAAAAAAAAAAA", Collections.singletonMap("salt2", Collections.singletonList("BBBBBBBBBBBBBBBB")));
PasswordCredentialModel model = PasswordCredentialModel.createFromValues(credentialData, secretData);
assertThat(model.getPasswordCredentialData(), notNullValue());
assertThat(model.getPasswordCredentialData().getAlgorithm(), equalTo("bcrypt"));
assertThat(model.getPasswordCredentialData().getHashIterations(), equalTo(1000));
assertThat(model.getPasswordCredentialData().getAdditionalParameters(), equalTo(Collections.singletonMap("cost", Collections.singletonList("18"))));
assertThat(model.getPasswordSecretData(), notNullValue());
assertThat(model.getPasswordSecretData().getAdditionalParameters(), equalTo(Collections.singletonMap("salt2", Collections.singletonList("BBBBBBBBBBBBBBBB"))));
assertThat(model.getPasswordSecretData().getValue(), equalTo("secretValue"));
assertThat(Arrays.equals(model.getPasswordSecretData().getSalt(), new byte[12]), is(true));
}
use of org.keycloak.models.credential.dto.PasswordCredentialData in project keycloak by keycloak.
the class CredentialModelTest method roundTripAndVerify.
private void roundTripAndVerify(PasswordCredentialModel model) throws IOException {
PasswordCredentialData pcdOriginal = model.getPasswordCredentialData();
PasswordCredentialData pcdRoundtrip = mapper.readValue(mapper.writeValueAsString(pcdOriginal), PasswordCredentialData.class);
assertThat(pcdRoundtrip.getAdditionalParameters(), equalTo(pcdOriginal.getAdditionalParameters()));
assertThat(pcdRoundtrip.getAlgorithm(), equalTo(pcdOriginal.getAlgorithm()));
assertThat(pcdRoundtrip.getHashIterations(), equalTo(pcdOriginal.getHashIterations()));
PasswordSecretData psdOriginal = model.getPasswordSecretData();
PasswordSecretData psdRoundtrip = mapper.readValue(mapper.writeValueAsString(psdOriginal), PasswordSecretData.class);
assertThat(psdRoundtrip.getValue(), equalTo(psdOriginal.getValue()));
assertThat(psdRoundtrip.getSalt(), equalTo(psdOriginal.getSalt()));
assertThat(psdRoundtrip.getAdditionalParameters(), equalTo(psdRoundtrip.getAdditionalParameters()));
}
use of org.keycloak.models.credential.dto.PasswordCredentialData in project keycloak by keycloak.
the class CredentialModelTest method roudtripToJsonExtendedCredentialModel.
@Test
public void roudtripToJsonExtendedCredentialModel() throws IOException {
PasswordCredentialData credentialData = new PasswordCredentialData(1000, "bcrypt", Collections.singletonMap("cost", Collections.singletonList("18")));
PasswordSecretData secretData = new PasswordSecretData("secretValue", "AAAAAAAAAAAAAAAA", Collections.singletonMap("salt2", Collections.singletonList("BBBBBBBBBBBBBBBB")));
PasswordCredentialModel model = PasswordCredentialModel.createFromValues(credentialData, secretData);
roundTripAndVerify(model);
}
use of org.keycloak.models.credential.dto.PasswordCredentialData in project keycloak by keycloak.
the class RepresentationToModel method convertDeprecatedCredentialsFormat.
private static void convertDeprecatedCredentialsFormat(UserRepresentation user) {
if (user.getCredentials() != null) {
for (CredentialRepresentation cred : user.getCredentials()) {
try {
if ((cred.getCredentialData() == null || cred.getSecretData() == null) && cred.getValue() == null) {
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())) {
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())) {
OTPCredentialData credentialData = new OTPCredentialData(cred.getType(), cred.getDigits(), cred.getCounter(), cred.getPeriod(), cred.getAlgorithm());
OTPSecretData secretData = new OTPSecretData(cred.getHashedSaltedValue());
cred.setCredentialData(JsonSerialization.writeValueAsString(credentialData));
cred.setSecretData(JsonSerialization.writeValueAsString(secretData));
cred.setPriority(20);
cred.setType(OTPCredentialModel.TYPE);
}
}
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
}
}
}
use of org.keycloak.models.credential.dto.PasswordCredentialData in project keycloak by keycloak.
the class PasswordCredentialModel method createFromCredentialModel.
public static PasswordCredentialModel createFromCredentialModel(CredentialModel credentialModel) {
try {
PasswordCredentialData credentialData = JsonSerialization.readValue(credentialModel.getCredentialData(), PasswordCredentialData.class);
PasswordSecretData secretData = JsonSerialization.readValue(credentialModel.getSecretData(), PasswordSecretData.class);
PasswordCredentialModel passwordCredentialModel = new PasswordCredentialModel(credentialData, secretData);
passwordCredentialModel.setCreatedDate(credentialModel.getCreatedDate());
passwordCredentialModel.setCredentialData(credentialModel.getCredentialData());
passwordCredentialModel.setId(credentialModel.getId());
passwordCredentialModel.setSecretData(credentialModel.getSecretData());
passwordCredentialModel.setType(credentialModel.getType());
passwordCredentialModel.setUserLabel(credentialModel.getUserLabel());
return passwordCredentialModel;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Aggregations