Search in sources :

Example 6 with KeysMetadataRepresentation

use of org.keycloak.representations.idm.KeysMetadataRepresentation 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 7 with KeysMetadataRepresentation

use of org.keycloak.representations.idm.KeysMetadataRepresentation 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 8 with KeysMetadataRepresentation

use of org.keycloak.representations.idm.KeysMetadataRepresentation 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 9 with KeysMetadataRepresentation

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

the class JavaKeystoreKeyProviderTest method create.

@Test
@AuthServerContainerExclude(AuthServer.REMOTE)
public void create() throws Exception {
    long priority = System.currentTimeMillis();
    ComponentRepresentation rep = createRep("valid", priority);
    Response response = adminClient.realm("test").components().add(rep);
    String id = ApiUtil.getCreatedId(response);
    ComponentRepresentation createdRep = adminClient.realm("test").components().component(id).toRepresentation();
    assertEquals(5, createdRep.getConfig().size());
    assertEquals(Long.toString(priority), createdRep.getConfig().getFirst("priority"));
    assertEquals(ComponentRepresentation.SECRET_VALUE, createdRep.getConfig().getFirst("keystorePassword"));
    assertEquals(ComponentRepresentation.SECRET_VALUE, createdRep.getConfig().getFirst("keyPassword"));
    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(PUBLIC_KEY, key.getPublicKey());
    assertEquals(CERTIFICATE, key.getCertificate());
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) Response(javax.ws.rs.core.Response) KeysMetadataRepresentation(org.keycloak.representations.idm.KeysMetadataRepresentation) AuthServerContainerExclude(org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude) Test(org.junit.Test) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest)

Example 10 with KeysMetadataRepresentation

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

the class KeyResource method getKeyMetadata.

@GET
@NoCache
@Produces(MediaType.APPLICATION_JSON)
public KeysMetadataRepresentation getKeyMetadata() {
    auth.realm().requireViewRealm();
    KeysMetadataRepresentation keys = new KeysMetadataRepresentation();
    keys.setActive(new HashMap<>());
    List<KeysMetadataRepresentation.KeyMetadataRepresentation> realmKeys = session.keys().getKeysStream(realm).map(key -> {
        if (key.getStatus().isActive()) {
            if (!keys.getActive().containsKey(key.getAlgorithmOrDefault())) {
                keys.getActive().put(key.getAlgorithmOrDefault(), key.getKid());
            }
        }
        return toKeyMetadataRepresentation(key);
    }).collect(Collectors.toList());
    keys.setKeys(realmKeys);
    return keys;
}
Also used : KeysMetadataRepresentation(org.keycloak.representations.idm.KeysMetadataRepresentation) KeyWrapper(org.keycloak.crypto.KeyWrapper) RealmModel(org.keycloak.models.RealmModel) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) AdminPermissionEvaluator(org.keycloak.services.resources.admin.permissions.AdminPermissionEvaluator) KeycloakSession(org.keycloak.models.KeycloakSession) HashMap(java.util.HashMap) PemUtils(org.keycloak.common.util.PemUtils) Collectors(java.util.stream.Collectors) KeysMetadataRepresentation(org.keycloak.representations.idm.KeysMetadataRepresentation) MediaType(javax.ws.rs.core.MediaType) List(java.util.List) NoCache(org.jboss.resteasy.annotations.cache.NoCache) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) NoCache(org.jboss.resteasy.annotations.cache.NoCache)

Aggregations

KeysMetadataRepresentation (org.keycloak.representations.idm.KeysMetadataRepresentation)15 ComponentRepresentation (org.keycloak.representations.idm.ComponentRepresentation)12 Response (javax.ws.rs.core.Response)10 RSAPublicKey (java.security.interfaces.RSAPublicKey)3 Test (org.junit.Test)3 AbstractKeycloakTest (org.keycloak.testsuite.AbstractKeycloakTest)3 KeyPair (java.security.KeyPair)2 List (java.util.List)2 KeyMetadataRepresentation (org.keycloak.representations.idm.KeysMetadataRepresentation.KeyMetadataRepresentation)2 IOException (java.io.IOException)1 KeyFactory (java.security.KeyFactory)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 PublicKey (java.security.PublicKey)1 Certificate (java.security.cert.Certificate)1 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)1 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)1 HashMap (java.util.HashMap)1 Collectors (java.util.stream.Collectors)1 GET (javax.ws.rs.GET)1 Produces (javax.ws.rs.Produces)1