Search in sources :

Example 71 with ComponentRepresentation

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

the class GeneratedEcdsaKeyProviderTest method createRep.

protected ComponentRepresentation createRep(String name, String providerId) {
    ComponentRepresentation rep = new ComponentRepresentation();
    rep.setName(name);
    rep.setParentId(TEST_REALM_NAME);
    rep.setProviderId(providerId);
    rep.setProviderType(KeyProvider.class.getName());
    rep.setConfig(new MultivaluedHashMap<>());
    return rep;
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) KeyProvider(org.keycloak.keys.KeyProvider)

Example 72 with ComponentRepresentation

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

the class GeneratedEcdsaKeyProviderTest method changeCurve.

private void changeCurve(String FromEcInNistRep, String ToEcInNistRep) throws Exception {
    String keyComponentId = supportedEc(FromEcInNistRep);
    KeysMetadataRepresentation keys = adminClient.realm(TEST_REALM_NAME).keys().getKeyMetadata();
    KeysMetadataRepresentation.KeyMetadataRepresentation originalKey = null;
    for (KeyMetadataRepresentation k : keys.getKeys()) {
        if (KeyType.EC.equals(k.getType()) && keyComponentId.equals(k.getProviderId())) {
            originalKey = k;
            break;
        }
    }
    ComponentRepresentation createdRep = adminClient.realm(TEST_REALM_NAME).components().component(keyComponentId).toRepresentation();
    createdRep.getConfig().putSingle(ECDSA_ELLIPTIC_CURVE_KEY, ToEcInNistRep);
    adminClient.realm(TEST_REALM_NAME).components().component(keyComponentId).update(createdRep);
    createdRep = adminClient.realm(TEST_REALM_NAME).components().component(keyComponentId).toRepresentation();
    // stands for the number of properties in the key provider config
    assertEquals(2, createdRep.getConfig().size());
    assertEquals(ToEcInNistRep, createdRep.getConfig().getFirst(ECDSA_ELLIPTIC_CURVE_KEY));
    keys = adminClient.realm(TEST_REALM_NAME).keys().getKeyMetadata();
    KeysMetadataRepresentation.KeyMetadataRepresentation key = null;
    for (KeyMetadataRepresentation k : keys.getKeys()) {
        if (KeyType.EC.equals(k.getType()) && keyComponentId.equals(k.getProviderId())) {
            key = k;
            break;
        }
    }
    assertNotNull(key);
    assertEquals(keyComponentId, key.getProviderId());
    // kid is changed if key was regenerated
    assertNotEquals(originalKey.getKid(), key.getKid());
    assertEquals(KeyType.EC, key.getType());
    assertNotEquals(originalKey.getAlgorithm(), key.getAlgorithm());
    assertEquals(ToEcInNistRep, AbstractEcdsaKeyProviderFactory.convertAlgorithmToECDomainParmNistRep(key.getAlgorithm()));
    assertEquals(ToEcInNistRep, getCurveFromPublicKey(key.getPublicKey()));
}
Also used : KeysMetadataRepresentation(org.keycloak.representations.idm.KeysMetadataRepresentation) ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) KeyMetadataRepresentation(org.keycloak.representations.idm.KeysMetadataRepresentation.KeyMetadataRepresentation) KeyMetadataRepresentation(org.keycloak.representations.idm.KeysMetadataRepresentation.KeyMetadataRepresentation)

Example 73 with ComponentRepresentation

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

the class GeneratedEcdsaKeyProviderTest method unsupportedEc.

private void unsupportedEc(String ecInNistRep) {
    long priority = System.currentTimeMillis();
    ComponentRepresentation rep = createRep("valid", GeneratedEcdsaKeyProviderFactory.ID);
    rep.setConfig(new MultivaluedHashMap<>());
    rep.getConfig().putSingle("priority", Long.toString(priority));
    rep.getConfig().putSingle(ECDSA_ELLIPTIC_CURVE_KEY, ecInNistRep);
    boolean isEcAccepted = true;
    Response response = null;
    try {
        response = adminClient.realm(TEST_REALM_NAME).components().add(rep);
        String id = ApiUtil.getCreatedId(response);
        getCleanup().addComponentId(id);
        response.close();
    } catch (WebApplicationException e) {
        isEcAccepted = false;
    } finally {
        response.close();
    }
    assertEquals(isEcAccepted, false);
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) Response(javax.ws.rs.core.Response) WebApplicationException(javax.ws.rs.WebApplicationException)

Example 74 with ComponentRepresentation

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

the class AbstractMigrationTest method testExtractRealmKeysMasterRealm.

protected void testExtractRealmKeysMasterRealm(RealmResource masterRealm) {
    log.info("testing extract realm keys");
    String expectedMasterRealmKey = "MIIEowIBAAKCAQEAiU54OXoCbHy0L0gHn1yasctcnKHRU1pHFIJnWvaI7rClJydet9dDJaiYXOxMKseiBm3eYznfN3cPyU8udYmRnMuKjiocZ77LT2IEttAjXb6Ggazx7loriFHRy0IOJeX4KxXhAPWmxqa3mkFNfLBEvFqVaBgUDHQ60cmnPvNSHYudBTW9K80s8nvmP2pso7HTwWJ1+Xatj1Ey/gTmB3CXlyqBegGWC9TeuErEYpYhdh+11TVWasgMBZyUCtL3NRPaBuhaPg1LpW8lWGk05nS+YM6dvTk3Mppv+z2RygEpxyO09oT3b4G+Zfwit1STqn0AvDTGzINdoKcNtFScV0j8TwIDAQABAoIBAHcbPKsPLZ8SJfOF1iblW8OzFulAbaaSf2pJHIMJrQrw7LKkMkPjVXoLX+/rgr7xYZmWIP2OLBWfEHCeYTzQUyHiZpSf7vgHx7Fa45/5uVQOe/ttHIiYa37bCtP4vvEdJkOpvP7qGPvljwsebqsk9Ns28LfVez66bHOjK5Mt2yOIulbTeEs7ch//h39YwKJv96vc+CHbV2O6qoOxZessO6y+287cOBvbFXmS2GaGle5Nx/EwncBNS4b7czoetmm70+9ht3yX+kxaP311YUT31KQjuaJt275kOiKsrXr27PvgO++bsIyGuSzqyS7G7fmxF2zUyphEqEpalyDGMKMnrAECgYEA1fCgFox03rPDjm0MhW/ThoS2Ld27sbWQ6reS+PBMdUTJZVZIU1D2//h6VXDnlddhk6avKjA4smdy1aDKzmjz3pt9AKn+kgkXqtTC2fD3wp+fC9hND0z+rQPGe/Gk7ZUnTdsqnfyowxr+woIgzdnRukOUrG+xQiP3RUUT7tt6NQECgYEApEz2xvgqMm+9/f/YxjLdsFUfLqc4WlafB863stYEVqlCYy5ujyo0VQ0ahKSKJkLDnf52+aMUqPOpwaGePpu3O6VkvpcKfPY2MUlZW7/6Sa9et9hxNkdTS7Gui2d1ELpaCBe1Bc62sk8EA01iHXE1PpvyUqDWrhNh+NrDICA9oU8CgYBgGDYACtTP11TmW2r9YK5VRLUDww30k4ZlN1GnyV++aMhBYVEZQ0u+y+A/EnijIFwu0vbo70H4OGknNZMCxbeMbLDoJHM5KyZbUDe5ZvgSjloFGwH59m6KTiDQOUkIgi9mVCQ/VGaFRFHcElEjxUvj60kTbxPijn8ZuR5r8l9hAQKBgQCQ9jL5pHWeoIayN20smi6M6N2lTPbkhe60dcgQatHTIG2pkosLl8IqlHAkPgSB84AiwyR351JQKwRJCm7TcJI/dxMnMZ6YWKfB3qSP1hdfsfJRJQ/mQxIUBAYrizF3e+P5peka4aLCOgMhYsJBlePThMZN7wja99EGPwXQL4IQ8wKBgB8Nis1lQK6Z30GCp9u4dYleGfEP71Lwqvk/eJb89/uz0fjF9CTpJMULFc+nA5u4yHP3LFnRg3zCU6aEwfwUyk4GH9lWGV/qIAisQtgrCEraVe4qxz0DVE59C7qjO26IhU2U66TEzPAqvQ3zqey+woDn/cz/JMWK1vpcSk+TKn3K";
    List<ComponentRepresentation> components = masterRealm.components().query(MASTER, KeyProvider.class.getName());
    assertEquals(3, components.size());
    components = masterRealm.components().query(MASTER, KeyProvider.class.getName(), "rsa");
    assertEquals(1, components.size());
    ComponentRepresentation component = testingClient.server(MASTER).fetch(RunHelpers.internalComponent(components.get(0).getId()));
    assertEquals(expectedMasterRealmKey, component.getConfig().getFirst("privateKey"));
    components = masterRealm.components().query(MASTER, KeyProvider.class.getName(), "hmac-generated");
    assertEquals(1, components.size());
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) KeyProvider(org.keycloak.keys.KeyProvider)

Example 75 with ComponentRepresentation

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

the class ImportedRsaKeyProviderTest method invalidPrivateKey.

private void invalidPrivateKey(String providerId) throws Exception {
    KeyPair keyPair = KeyUtils.generateRsaKeyPair(2048);
    ComponentRepresentation rep = createRep("invalid", providerId);
    Response response = adminClient.realm("test").components().add(rep);
    assertErrror(response, "'Private RSA Key' is required");
    rep.getConfig().putSingle(Attributes.PRIVATE_KEY_KEY, "nonsense");
    response = adminClient.realm("test").components().add(rep);
    assertErrror(response, "Failed to decode private key");
    rep.getConfig().putSingle(Attributes.PRIVATE_KEY_KEY, PemUtils.encodeKey(keyPair.getPublic()));
    response = adminClient.realm("test").components().add(rep);
    assertErrror(response, "Failed to decode private key");
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) Response(javax.ws.rs.core.Response) KeyPair(java.security.KeyPair)

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