Search in sources :

Example 16 with ComponentRepresentation

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

the class ImportedRsaKeyProviderTest method invalidPriority.

private void invalidPriority(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.PRIORITY_KEY, "invalid");
    Response response = adminClient.realm("test").components().add(rep);
    assertErrror(response, "'Priority' should be a number");
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) Response(javax.ws.rs.core.Response) KeyPair(java.security.KeyPair)

Example 17 with ComponentRepresentation

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

the class ImportedRsaKeyProviderTest method invalidEnabled.

private void invalidEnabled(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.ENABLED_KEY, "invalid");
    Response response = adminClient.realm("test").components().add(rep);
    assertErrror(response, "'Enabled' should be 'true' or 'false'");
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) Response(javax.ws.rs.core.Response) KeyPair(java.security.KeyPair)

Example 18 with ComponentRepresentation

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

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

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

the class AbstractMigrationTest method testExtractRealmKeysMigrationRealm.

protected void testExtractRealmKeysMigrationRealm(RealmResource migrationRealm) {
    log.info("testing extract realm keys");
    String expectedMigrationRealmKey = "MIIEpAIBAAKCAQEApt6gCllWkVTZ7fy/oRIx6Bxjt9x3eKKyKGFXvN4iaafrNqpYU9lcqPngWJ9DyXGqUf8RpjPaQWiLWLxjw3xGBqLk2E1/Frb9e/dy8rj//fHGq6bujN1iguzyFwxPGT5Asd7jflRI3qU04M8JE52PArqPhGL2Fn+FiSK5SWRIGm+hVL7Ck/E/tVxM25sFG1/UTQqvrROm4q76TmP8FsyZaTLVf7cCwW2QPIX0N5HTVb3QbBb5KIsk4kKmk/g7uUxS9r42tu533LISzRr5CTyWZAL2XFRuF2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/HeSU12QDK4cC8Vgv+jyPy7zaZtLR6NduUPrBRvfiyCOkr8WrwKBgQCY0h4RCdNFhr0KKLLmJipAtV8wBCGcg1jY1KoWKQswbcykfBKwHbF6EooVqkRW0ITjWB7ZZCf8TnSUxe0NXCUAkVBrhzS4DScgtoSZYOOUaSHgOxpfwgnQ3oYotKi98Yg3IsaLs1j4RuPG5Sp1z6o+ELP1uvr8azyn9YlLa+523Q==";
    List<ComponentRepresentation> components = migrationRealm.components().query(MIGRATION, KeyProvider.class.getName());
    assertEquals(3, components.size());
    components = migrationRealm.components().query(MIGRATION, KeyProvider.class.getName(), "rsa");
    assertEquals(1, components.size());
    ComponentRepresentation component = testingClient.server(MIGRATION).fetch(RunHelpers.internalComponent(components.get(0).getId()));
    assertEquals(expectedMigrationRealmKey, component.getConfig().getFirst("privateKey"));
    components = migrationRealm.components().query(MIGRATION, KeyProvider.class.getName(), "hmac-generated");
    assertEquals(1, components.size());
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) KeyProvider(org.keycloak.keys.KeyProvider)

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