Search in sources :

Example 1 with KeysMetadataRepresentation

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

the class OIDCPublicKeyRotationAdapterTest method getActiveKeyProvider.

private String getActiveKeyProvider() {
    KeysMetadataRepresentation keyMetadata = adminClient.realm(DEMO).keys().getKeyMetadata();
    String activeKid = keyMetadata.getActive().get(Algorithm.RS256);
    for (KeysMetadataRepresentation.KeyMetadataRepresentation rep : keyMetadata.getKeys()) {
        if (rep.getKid().equals(activeKid)) {
            return rep.getProviderId();
        }
    }
    return null;
}
Also used : KeysMetadataRepresentation(org.keycloak.representations.idm.KeysMetadataRepresentation)

Example 2 with KeysMetadataRepresentation

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

the class GeneratedHmacKeyProviderTest method largeKeysize.

@Test
public void largeKeysize() {
    long priority = System.currentTimeMillis();
    ComponentRepresentation rep = createRep("valid", GeneratedHmacKeyProviderFactory.ID);
    rep.setConfig(new MultivaluedHashMap<>());
    rep.getConfig().putSingle("priority", Long.toString(priority));
    rep.getConfig().putSingle("secretSize", "512");
    Response response = adminClient.realm("test").components().add(rep);
    String id = ApiUtil.getCreatedId(response);
    response.close();
    ComponentRepresentation createdRep = adminClient.realm("test").components().component(id).toRepresentation();
    assertEquals(2, createdRep.getConfig().size());
    assertEquals("512", createdRep.getConfig().getFirst("secretSize"));
    KeysMetadataRepresentation keys = adminClient.realm("test").keys().getKeyMetadata();
    KeysMetadataRepresentation.KeyMetadataRepresentation key = null;
    for (KeysMetadataRepresentation.KeyMetadataRepresentation k : keys.getKeys()) {
        if (k.getAlgorithm().equals(Algorithm.HS256)) {
            key = k;
            break;
        }
    }
    assertEquals(id, key.getProviderId());
    assertEquals(KeyType.OCT, key.getType());
    assertEquals(priority, key.getProviderPriority());
    ComponentRepresentation component = testingClient.server("test").fetch(RunHelpers.internalComponent(id));
    assertEquals(512, Base64Url.decode(component.getConfig().getFirst("secret")).length);
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) Response(javax.ws.rs.core.Response) KeysMetadataRepresentation(org.keycloak.representations.idm.KeysMetadataRepresentation) Test(org.junit.Test) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest)

Example 3 with KeysMetadataRepresentation

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

the class ImportedRsaKeyProviderTest method privateKeyOnly.

private void privateKeyOnly(String providerId, KeyUse keyUse, String algorithm) throws Exception {
    long priority = System.currentTimeMillis();
    KeyPair keyPair = KeyUtils.generateRsaKeyPair(2048);
    String kid = KeyUtils.createKeyId(keyPair.getPublic());
    ComponentRepresentation rep = createRep("valid", providerId);
    rep.getConfig().putSingle(Attributes.PRIVATE_KEY_KEY, PemUtils.encodeKey(keyPair.getPrivate()));
    rep.getConfig().putSingle(Attributes.PRIORITY_KEY, Long.toString(priority));
    Response response = adminClient.realm("test").components().add(rep);
    String id = ApiUtil.getCreatedId(response);
    response.close();
    ComponentRepresentation createdRep = adminClient.realm("test").components().component(id).toRepresentation();
    assertEquals(ComponentRepresentation.SECRET_VALUE, createdRep.getConfig().getFirst(Attributes.PRIVATE_KEY_KEY));
    assertNotNull(createdRep.getConfig().getFirst(Attributes.CERTIFICATE_KEY));
    assertEquals(keyPair.getPublic(), PemUtils.decodeCertificate(createdRep.getConfig().getFirst(Attributes.CERTIFICATE_KEY)).getPublicKey());
    KeysMetadataRepresentation keys = adminClient.realm("test").keys().getKeyMetadata();
    assertEquals(kid, keys.getActive().get(algorithm));
    KeysMetadataRepresentation.KeyMetadataRepresentation key = keys.getKeys().get(0);
    assertEquals(id, key.getProviderId());
    assertEquals(AlgorithmType.RSA.name(), key.getType());
    assertEquals(priority, key.getProviderPriority());
    assertEquals(kid, key.getKid());
    assertEquals(PemUtils.encodeKey(keyPair.getPublic()), keys.getKeys().get(0).getPublicKey());
    assertEquals(keyPair.getPublic(), PemUtils.decodeCertificate(key.getCertificate()).getPublicKey());
    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) KeyPair(java.security.KeyPair)

Example 4 with KeysMetadataRepresentation

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

the class ImportedRsaKeyProviderTest method keyAndCertificate.

private void keyAndCertificate(String providerId, KeyUse keyUse) throws Exception {
    long priority = System.currentTimeMillis();
    KeyPair keyPair = KeyUtils.generateRsaKeyPair(2048);
    Certificate certificate = CertificateUtils.generateV1SelfSignedCertificate(keyPair, "test");
    String certificatePem = PemUtils.encodeCertificate(certificate);
    ComponentRepresentation rep = createRep("valid", providerId);
    rep.getConfig().putSingle(Attributes.PRIVATE_KEY_KEY, PemUtils.encodeKey(keyPair.getPrivate()));
    rep.getConfig().putSingle(Attributes.CERTIFICATE_KEY, certificatePem);
    rep.getConfig().putSingle(Attributes.PRIORITY_KEY, Long.toString(priority));
    Response response = adminClient.realm("test").components().add(rep);
    String id = ApiUtil.getCreatedId(response);
    response.close();
    ComponentRepresentation createdRep = adminClient.realm("test").components().component(id).toRepresentation();
    assertEquals(ComponentRepresentation.SECRET_VALUE, createdRep.getConfig().getFirst(Attributes.PRIVATE_KEY_KEY));
    assertEquals(certificatePem, createdRep.getConfig().getFirst(Attributes.CERTIFICATE_KEY));
    KeysMetadataRepresentation keys = adminClient.realm("test").keys().getKeyMetadata();
    KeysMetadataRepresentation.KeyMetadataRepresentation key = keys.getKeys().get(0);
    assertEquals(certificatePem, key.getCertificate());
    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) KeyPair(java.security.KeyPair) Certificate(java.security.cert.Certificate)

Example 5 with KeysMetadataRepresentation

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

the class GeneratedEcdsaKeyProviderTest method supportedEc.

private String supportedEc(String ecInNistRep) {
    long priority = System.currentTimeMillis();
    ComponentRepresentation rep = createRep("valid", GeneratedEcdsaKeyProviderFactory.ID);
    rep.setConfig(new MultivaluedHashMap<>());
    rep.getConfig().putSingle("priority", Long.toString(priority));
    if (ecInNistRep != null) {
        rep.getConfig().putSingle(ECDSA_ELLIPTIC_CURVE_KEY, ecInNistRep);
    } else {
        ecInNistRep = DEFAULT_EC;
    }
    Response response = adminClient.realm(TEST_REALM_NAME).components().add(rep);
    String id = ApiUtil.getCreatedId(response);
    getCleanup().addComponentId(id);
    response.close();
    ComponentRepresentation createdRep = adminClient.realm(TEST_REALM_NAME).components().component(id).toRepresentation();
    // stands for the number of properties in the key provider config
    assertEquals(2, createdRep.getConfig().size());
    assertEquals(Long.toString(priority), createdRep.getConfig().getFirst("priority"));
    assertEquals(ecInNistRep, createdRep.getConfig().getFirst(ECDSA_ELLIPTIC_CURVE_KEY));
    KeysMetadataRepresentation keys = adminClient.realm(TEST_REALM_NAME).keys().getKeyMetadata();
    KeysMetadataRepresentation.KeyMetadataRepresentation key = null;
    for (KeyMetadataRepresentation k : keys.getKeys()) {
        if (KeyType.EC.equals(k.getType()) && id.equals(k.getProviderId())) {
            key = k;
            break;
        }
    }
    assertNotNull(key);
    assertEquals(id, key.getProviderId());
    assertEquals(KeyType.EC, key.getType());
    assertEquals(priority, key.getProviderPriority());
    // created key's component id
    return id;
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) Response(javax.ws.rs.core.Response) KeysMetadataRepresentation(org.keycloak.representations.idm.KeysMetadataRepresentation) KeyMetadataRepresentation(org.keycloak.representations.idm.KeysMetadataRepresentation.KeyMetadataRepresentation) KeyMetadataRepresentation(org.keycloak.representations.idm.KeysMetadataRepresentation.KeyMetadataRepresentation)

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