Search in sources :

Example 1 with PasswordSecretData

use of org.keycloak.models.credential.dto.PasswordSecretData 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));
}
Also used : PasswordCredentialData(org.keycloak.models.credential.dto.PasswordCredentialData) PasswordSecretData(org.keycloak.models.credential.dto.PasswordSecretData) Test(org.junit.Test)

Example 2 with PasswordSecretData

use of org.keycloak.models.credential.dto.PasswordSecretData 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()));
}
Also used : PasswordCredentialData(org.keycloak.models.credential.dto.PasswordCredentialData) PasswordSecretData(org.keycloak.models.credential.dto.PasswordSecretData)

Example 3 with PasswordSecretData

use of org.keycloak.models.credential.dto.PasswordSecretData 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);
}
Also used : PasswordCredentialData(org.keycloak.models.credential.dto.PasswordCredentialData) PasswordSecretData(org.keycloak.models.credential.dto.PasswordSecretData) Test(org.junit.Test)

Example 4 with PasswordSecretData

use of org.keycloak.models.credential.dto.PasswordSecretData 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);
    }
}
Also used : PasswordCredentialData(org.keycloak.models.credential.dto.PasswordCredentialData) IOException(java.io.IOException) PasswordSecretData(org.keycloak.models.credential.dto.PasswordSecretData)

Example 5 with PasswordSecretData

use of org.keycloak.models.credential.dto.PasswordSecretData in project keycloak by keycloak.

the class PasswordCredentialModel method createFromValues.

public static PasswordCredentialModel createFromValues(String algorithm, byte[] salt, int hashIterations, String encodedPassword) {
    PasswordCredentialData credentialData = new PasswordCredentialData(hashIterations, algorithm);
    PasswordSecretData secretData = new PasswordSecretData(encodedPassword, salt);
    PasswordCredentialModel passwordCredentialModel = new PasswordCredentialModel(credentialData, secretData);
    try {
        passwordCredentialModel.setCredentialData(JsonSerialization.writeValueAsString(credentialData));
        passwordCredentialModel.setSecretData(JsonSerialization.writeValueAsString(secretData));
        passwordCredentialModel.setType(TYPE);
        return passwordCredentialModel;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : PasswordCredentialData(org.keycloak.models.credential.dto.PasswordCredentialData) IOException(java.io.IOException) PasswordSecretData(org.keycloak.models.credential.dto.PasswordSecretData)

Aggregations

PasswordCredentialData (org.keycloak.models.credential.dto.PasswordCredentialData)5 PasswordSecretData (org.keycloak.models.credential.dto.PasswordSecretData)5 IOException (java.io.IOException)2 Test (org.junit.Test)2