Search in sources :

Example 26 with ComponentRepresentation

use of org.keycloak.representations.idm.ComponentRepresentation in project keycloak by keycloak.

the class GeneratedRsaKeyProviderTest method defaultKeysize.

private void defaultKeysize(String providerId, KeyUse keyUse) throws Exception {
    long priority = System.currentTimeMillis();
    ComponentRepresentation rep = createRep("valid", providerId);
    rep.setConfig(new MultivaluedHashMap<>());
    rep.getConfig().putSingle("priority", Long.toString(priority));
    Response response = adminClient.realm("test").components().add(rep);
    String id = ApiUtil.getCreatedId(response);
    getCleanup().addComponentId(id);
    response.close();
    ComponentRepresentation createdRep = adminClient.realm("test").components().component(id).toRepresentation();
    assertEquals(1, createdRep.getConfig().size());
    assertEquals(Long.toString(priority), createdRep.getConfig().getFirst("priority"));
    KeysMetadataRepresentation keys = adminClient.realm("test").keys().getKeyMetadata();
    KeysMetadataRepresentation.KeyMetadataRepresentation key = keys.getKeys().get(0);
    assertEquals(id, key.getProviderId());
    assertEquals(AlgorithmType.RSA.name(), key.getType());
    assertEquals(priority, key.getProviderPriority());
    assertEquals(2048, ((RSAPublicKey) PemUtils.decodePublicKey(keys.getKeys().get(0).getPublicKey())).getModulus().bitLength());
    assertEquals(keyUse, key.getUse());
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) Response(javax.ws.rs.core.Response) KeysMetadataRepresentation(org.keycloak.representations.idm.KeysMetadataRepresentation) RSAPublicKey(java.security.interfaces.RSAPublicKey)

Example 27 with ComponentRepresentation

use of org.keycloak.representations.idm.ComponentRepresentation in project keycloak by keycloak.

the class GeneratedRsaKeyProviderTest method invalidKeysize.

private void invalidKeysize(String providerId) throws Exception {
    ComponentRepresentation rep = createRep("invalid", providerId);
    rep.getConfig().putSingle("keySize", "1234");
    Response response = adminClient.realm("test").components().add(rep);
    assertErrror(response, "'Key size' should be 1024, 2048 or 4096");
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) Response(javax.ws.rs.core.Response)

Example 28 with ComponentRepresentation

use of org.keycloak.representations.idm.ComponentRepresentation in project keycloak by keycloak.

the class GeneratedRsaKeyProviderTest method updatePriority.

private void updatePriority(String providerId, KeyUse keyUse) throws Exception {
    long priority = System.currentTimeMillis();
    ComponentRepresentation rep = createRep("valid", providerId);
    rep.setConfig(new MultivaluedHashMap<>());
    rep.getConfig().putSingle("priority", Long.toString(priority));
    Response response = adminClient.realm("test").components().add(rep);
    String id = ApiUtil.getCreatedId(response);
    getCleanup().addComponentId(id);
    response.close();
    KeysMetadataRepresentation keys = adminClient.realm("test").keys().getKeyMetadata();
    String publicKey = keys.getKeys().get(0).getPublicKey();
    ComponentRepresentation createdRep = adminClient.realm("test").components().component(id).toRepresentation();
    priority += 1000;
    createdRep.getConfig().putSingle("priority", Long.toString(priority));
    adminClient.realm("test").components().component(id).update(createdRep);
    keys = adminClient.realm("test").keys().getKeyMetadata();
    String publicKey2 = keys.getKeys().get(0).getPublicKey();
    assertEquals(publicKey, publicKey2);
    assertEquals(keyUse, keys.getKeys().get(0).getUse());
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) Response(javax.ws.rs.core.Response) KeysMetadataRepresentation(org.keycloak.representations.idm.KeysMetadataRepresentation)

Example 29 with ComponentRepresentation

use of org.keycloak.representations.idm.ComponentRepresentation in project keycloak by keycloak.

the class GeneratedRsaKeyProviderTest method updateKeysize.

private void updateKeysize(String providerId, KeyUse keyUse) throws Exception {
    long priority = System.currentTimeMillis();
    ComponentRepresentation rep = createRep("valid", providerId);
    rep.setConfig(new MultivaluedHashMap<>());
    rep.getConfig().putSingle("priority", Long.toString(priority));
    Response response = adminClient.realm("test").components().add(rep);
    String id = ApiUtil.getCreatedId(response);
    getCleanup().addComponentId(id);
    response.close();
    KeysMetadataRepresentation keys = adminClient.realm("test").keys().getKeyMetadata();
    String publicKey = keys.getKeys().get(0).getPublicKey();
    ComponentRepresentation createdRep = adminClient.realm("test").components().component(id).toRepresentation();
    createdRep.getConfig().putSingle("keySize", "4096");
    adminClient.realm("test").components().component(id).update(createdRep);
    keys = adminClient.realm("test").keys().getKeyMetadata();
    String publicKey2 = keys.getKeys().get(0).getPublicKey();
    assertNotEquals(publicKey, publicKey2);
    assertEquals(2048, ((RSAPublicKey) PemUtils.decodePublicKey(publicKey)).getModulus().bitLength());
    assertEquals(4096, ((RSAPublicKey) PemUtils.decodePublicKey(publicKey2)).getModulus().bitLength());
    assertEquals(keyUse, keys.getKeys().get(0).getUse());
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) Response(javax.ws.rs.core.Response) KeysMetadataRepresentation(org.keycloak.representations.idm.KeysMetadataRepresentation) RSAPublicKey(java.security.interfaces.RSAPublicKey)

Example 30 with ComponentRepresentation

use of org.keycloak.representations.idm.ComponentRepresentation in project keycloak by keycloak.

the class JavaKeystoreKeyProviderTest method invalidKeyPassword.

@Test
@AuthServerContainerExclude(AuthServer.REMOTE)
public void invalidKeyPassword() throws Exception {
    ComponentRepresentation rep = createRep("valid", System.currentTimeMillis());
    rep.getConfig().putSingle("keyPassword", "invalid");
    Response response = adminClient.realm("test").components().add(rep);
    assertErrror(response, "Failed to load keys. Keystore on server can not be recovered.");
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) Response(javax.ws.rs.core.Response) AuthServerContainerExclude(org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude) Test(org.junit.Test) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest)

Aggregations

ComponentRepresentation (org.keycloak.representations.idm.ComponentRepresentation)120 Test (org.junit.Test)56 Response (javax.ws.rs.core.Response)44 UserStorageProvider (org.keycloak.storage.UserStorageProvider)22 KeyProvider (org.keycloak.keys.KeyProvider)17 AbstractKeycloakTest (org.keycloak.testsuite.AbstractKeycloakTest)13 Before (org.junit.Before)12 KeysMetadataRepresentation (org.keycloak.representations.idm.KeysMetadataRepresentation)12 RealmModel (org.keycloak.models.RealmModel)10 AuthServerContainerExclude (org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude)10 KeyPair (java.security.KeyPair)9 UserModel (org.keycloak.models.UserModel)9 AbstractAuthenticationTest (org.keycloak.testsuite.admin.authentication.AbstractAuthenticationTest)8 RealmResource (org.keycloak.admin.client.resource.RealmResource)7 MultivaluedHashMap (org.keycloak.common.util.MultivaluedHashMap)7 ComponentModel (org.keycloak.component.ComponentModel)7 UserRepresentation (org.keycloak.representations.idm.UserRepresentation)7 OIDCClientRepresentation (org.keycloak.representations.oidc.OIDCClientRepresentation)6 AbstractAuthTest (org.keycloak.testsuite.AbstractAuthTest)6 List (java.util.List)5