Search in sources :

Example 1 with Pbkdf2PasswordHashProvider

use of org.keycloak.credential.hash.Pbkdf2PasswordHashProvider in project keycloak by keycloak.

the class PasswordHashingTest method testPasswordRehashedWhenCredentialImportedWithDifferentKeySize.

@Test
public void testPasswordRehashedWhenCredentialImportedWithDifferentKeySize() {
    setPasswordPolicy("hashAlgorithm(" + Pbkdf2Sha512PasswordHashProviderFactory.ID + ") and hashIterations(" + Pbkdf2Sha512PasswordHashProviderFactory.DEFAULT_ITERATIONS + ")");
    String username = "testPasswordRehashedWhenCredentialImportedWithDifferentKeySize";
    String password = "password";
    // Encode with a specific key size ( 256 instead of default: 512)
    Pbkdf2PasswordHashProvider specificKeySizeHashProvider = new Pbkdf2PasswordHashProvider(Pbkdf2Sha512PasswordHashProviderFactory.ID, Pbkdf2Sha512PasswordHashProviderFactory.PBKDF2_ALGORITHM, Pbkdf2Sha512PasswordHashProviderFactory.DEFAULT_ITERATIONS, 256);
    String encodedPassword = specificKeySizeHashProvider.encode(password, -1);
    // Create a user with the encoded password, simulating a user import from a different system using a specific key size
    UserRepresentation user = UserBuilder.create().username(username).password(encodedPassword).build();
    ApiUtil.createUserWithAdminClient(adminClient.realm("test"), user);
    loginPage.open();
    loginPage.login(username, password);
    PasswordCredentialModel postLoginCredentials = PasswordCredentialModel.createFromCredentialModel(fetchCredentials(username));
    assertEquals(encodedPassword.length() * 2, postLoginCredentials.getPasswordSecretData().getValue().length());
}
Also used : PasswordCredentialModel(org.keycloak.models.credential.PasswordCredentialModel) Pbkdf2PasswordHashProvider(org.keycloak.credential.hash.Pbkdf2PasswordHashProvider) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest)

Aggregations

Test (org.junit.Test)1 Pbkdf2PasswordHashProvider (org.keycloak.credential.hash.Pbkdf2PasswordHashProvider)1 PasswordCredentialModel (org.keycloak.models.credential.PasswordCredentialModel)1 UserRepresentation (org.keycloak.representations.idm.UserRepresentation)1 AbstractTestRealmKeycloakTest (org.keycloak.testsuite.AbstractTestRealmKeycloakTest)1