Search in sources :

Example 1 with KeyMetadataRepresentation

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

Example 2 with KeyMetadataRepresentation

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

Aggregations

ComponentRepresentation (org.keycloak.representations.idm.ComponentRepresentation)2 KeysMetadataRepresentation (org.keycloak.representations.idm.KeysMetadataRepresentation)2 KeyMetadataRepresentation (org.keycloak.representations.idm.KeysMetadataRepresentation.KeyMetadataRepresentation)2 Response (javax.ws.rs.core.Response)1