Search in sources :

Example 1 with PasswordCredentialData

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

Example 2 with PasswordCredentialData

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

Example 3 with PasswordCredentialData

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

Example 4 with PasswordCredentialData

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);
            }
        }
    }
}
Also used : CredentialRepresentation(org.keycloak.representations.idm.CredentialRepresentation) OTPSecretData(org.keycloak.models.credential.dto.OTPSecretData) PasswordCredentialData(org.keycloak.models.credential.dto.PasswordCredentialData) OTPCredentialData(org.keycloak.models.credential.dto.OTPCredentialData) IOException(java.io.IOException)

Example 5 with PasswordCredentialData

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);
    }
}
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)7 PasswordSecretData (org.keycloak.models.credential.dto.PasswordSecretData)5 IOException (java.io.IOException)3 Test (org.junit.Test)2 CredentialRepresentation (org.keycloak.representations.idm.CredentialRepresentation)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 ClientResource (org.keycloak.admin.client.resource.ClientResource)1 RealmResource (org.keycloak.admin.client.resource.RealmResource)1 UserResource (org.keycloak.admin.client.resource.UserResource)1 OTPCredentialData (org.keycloak.models.credential.dto.OTPCredentialData)1 OTPSecretData (org.keycloak.models.credential.dto.OTPSecretData)1 AuthenticationFlowRepresentation (org.keycloak.representations.idm.AuthenticationFlowRepresentation)1 ClientRepresentation (org.keycloak.representations.idm.ClientRepresentation)1 ClientScopeRepresentation (org.keycloak.representations.idm.ClientScopeRepresentation)1 ComponentRepresentation (org.keycloak.representations.idm.ComponentRepresentation)1 FederatedIdentityRepresentation (org.keycloak.representations.idm.FederatedIdentityRepresentation)1 IdentityProviderRepresentation (org.keycloak.representations.idm.IdentityProviderRepresentation)1