Search in sources :

Example 76 with ComponentRepresentation

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

the class ImportedRsaKeyProviderTest method invalidActive.

private void invalidActive(String providerId) throws Exception {
    KeyPair keyPair = KeyUtils.generateRsaKeyPair(2048);
    ComponentRepresentation rep = createRep("invalid", providerId);
    rep.getConfig().putSingle(Attributes.PRIVATE_KEY_KEY, PemUtils.encodeKey(keyPair.getPrivate()));
    rep.getConfig().putSingle(Attributes.ACTIVE_KEY, "invalid");
    Response response = adminClient.realm("test").components().add(rep);
    assertErrror(response, "'Active' should be 'true' or 'false'");
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) Response(javax.ws.rs.core.Response) KeyPair(java.security.KeyPair)

Example 77 with ComponentRepresentation

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

the class KeyRotationTest method createKeys.

private Map<String, String> createKeys(String priority) throws Exception {
    KeyPair keyPair = KeyUtils.generateRsaKeyPair(1024);
    String privateKeyPem = PemUtils.encodeKey(keyPair.getPrivate());
    PublicKey publicKey = keyPair.getPublic();
    ComponentRepresentation rep = new ComponentRepresentation();
    rep.setName("mycomponent");
    rep.setParentId("test");
    rep.setProviderId(ImportedRsaKeyProviderFactory.ID);
    rep.setProviderType(KeyProvider.class.getName());
    org.keycloak.common.util.MultivaluedHashMap config = new org.keycloak.common.util.MultivaluedHashMap();
    config.addFirst("priority", priority);
    config.addFirst(Attributes.PRIVATE_KEY_KEY, privateKeyPem);
    rep.setConfig(config);
    Response response = adminClient.realm("test").components().add(rep);
    response.close();
    rep = new ComponentRepresentation();
    rep.setName("mycomponent2");
    rep.setParentId("test");
    rep.setProviderId(GeneratedHmacKeyProviderFactory.ID);
    rep.setProviderType(KeyProvider.class.getName());
    config = new org.keycloak.common.util.MultivaluedHashMap();
    config.addFirst("priority", priority);
    rep.setConfig(config);
    response = adminClient.realm("test").components().add(rep);
    response.close();
    return realmsResouce().realm("test").keys().getKeyMetadata().getActive();
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) KeyProvider(org.keycloak.keys.KeyProvider) MultivaluedHashMap(org.keycloak.common.util.MultivaluedHashMap) Response(javax.ws.rs.core.Response) KeyPair(java.security.KeyPair) PublicKey(java.security.PublicKey) MultivaluedHashMap(org.keycloak.common.util.MultivaluedHashMap)

Example 78 with ComponentRepresentation

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

the class KeyRotationTest method dropKeys.

private void dropKeys(String priority) {
    int r = 0;
    for (ComponentRepresentation c : adminClient.realm("test").components().query("test", KeyProvider.class.getName())) {
        if (c.getConfig().getFirst("priority").equals(priority)) {
            adminClient.realm("test").components().component(c.getId()).remove();
            r++;
        }
    }
    if (r != 2) {
        throw new RuntimeException("Failed to find keys1");
    }
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) KeyProvider(org.keycloak.keys.KeyProvider)

Example 79 with ComponentRepresentation

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

the class KeyRotationTest method rotateKeys.

@Test
public void rotateKeys() throws InterruptedException {
    for (int i = 0; i < 10; i++) {
        String activeKid = adminClient.realm("test").keys().getKeyMetadata().getActive().get(Algorithm.RS256);
        // Rotate public keys on the parent broker
        String realmId = adminClient.realm("test").toRepresentation().getId();
        ComponentRepresentation keys = new ComponentRepresentation();
        keys.setName("generated" + i);
        keys.setProviderType(KeyProvider.class.getName());
        keys.setProviderId("rsa-generated");
        keys.setParentId(realmId);
        keys.setConfig(new MultivaluedHashMap<>());
        keys.getConfig().putSingle("priority", "1000" + i);
        Response response = adminClient.realm("test").components().add(keys);
        assertEquals(201, response.getStatus());
        String newId = ApiUtil.getCreatedId(response);
        getCleanup().addComponentId(newId);
        response.close();
        String updatedActiveKid = adminClient.realm("test").keys().getKeyMetadata().getActive().get(Algorithm.RS256);
        assertNotEquals(activeKid, updatedActiveKid);
    }
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) KeyProvider(org.keycloak.keys.KeyProvider) Response(javax.ws.rs.core.Response) Test(org.junit.Test) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest)

Example 80 with ComponentRepresentation

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

the class GeneratedRsaKeyProviderTest method largeKeysize.

private void largeKeysize(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));
    rep.getConfig().putSingle("keySize", "4096");
    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(2, createdRep.getConfig().size());
    assertEquals("4096", createdRep.getConfig().getFirst("keySize"));
    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(4096, ((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)

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