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());
}
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());
}
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());
}
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());
}
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;
}
Aggregations