Search in sources :

Example 11 with ClientPoliciesRepresentation

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

the class ClientPoliciesLoadUpdateTest method testUpdateValidProfilesAndPolicies.

@Test
public void testUpdateValidProfilesAndPolicies() throws Exception {
    setupValidProfilesAndPolicies();
    assertExpectedLoadedProfiles((ClientProfilesRepresentation reps) -> {
        ClientProfileRepresentation rep = getProfileRepresentation(reps, "ordinal-test-profile", false);
        assertExpectedProfile(rep, "ordinal-test-profile", "The profile that can be loaded.");
    });
    assertExpectedLoadedPolicies((ClientPoliciesRepresentation reps) -> {
        ClientPolicyRepresentation rep = getPolicyRepresentation(reps, "new-policy");
        assertExpectedPolicy("new-policy", "duplicated profiles are ignored.", true, Arrays.asList("ordinal-test-profile", "lack-of-builtin-field-test-profile"), rep);
    });
    // update existing profiles
    String modifiedProfileDescription = "The profile has been updated.";
    ClientProfilesRepresentation actualProfilesRep = getProfilesWithoutGlobals();
    ClientProfilesBuilder profilesBuilder = new ClientProfilesBuilder();
    actualProfilesRep.getProfiles().stream().forEach(i -> {
        if (i.getName().equals("ordinal-test-profile")) {
            i.setDescription(modifiedProfileDescription);
        }
        profilesBuilder.addProfile(i);
    });
    updateProfiles(profilesBuilder.toString());
    assertExpectedLoadedProfiles((ClientProfilesRepresentation reps) -> {
        ClientProfileRepresentation rep = getProfileRepresentation(reps, "ordinal-test-profile", false);
        assertExpectedProfile(rep, "ordinal-test-profile", modifiedProfileDescription);
    });
    // update existing policies
    String modifiedPolicyDescription = "The policy has also been updated.";
    ClientPoliciesRepresentation actualPoliciesRep = getPolicies();
    ClientPoliciesBuilder policiesBuilder = new ClientPoliciesBuilder();
    actualPoliciesRep.getPolicies().stream().forEach(i -> {
        if (i.getName().equals("new-policy")) {
            i.setDescription(modifiedPolicyDescription);
            i.setEnabled(null);
        }
        policiesBuilder.addPolicy(i);
    });
    updatePolicies(policiesBuilder.toString());
    assertExpectedLoadedPolicies((ClientPoliciesRepresentation reps) -> {
        ClientPolicyRepresentation rep = getPolicyRepresentation(reps, "new-policy");
        assertExpectedPolicy("new-policy", modifiedPolicyDescription, false, Arrays.asList("ordinal-test-profile", "lack-of-builtin-field-test-profile"), rep);
    });
}
Also used : ClientPolicyRepresentation(org.keycloak.representations.idm.ClientPolicyRepresentation) ClientProfileRepresentation(org.keycloak.representations.idm.ClientProfileRepresentation) ClientPoliciesRepresentation(org.keycloak.representations.idm.ClientPoliciesRepresentation) ClientProfilesBuilder(org.keycloak.testsuite.util.ClientPoliciesUtil.ClientProfilesBuilder) ClientPoliciesBuilder(org.keycloak.testsuite.util.ClientPoliciesUtil.ClientPoliciesBuilder) ClientProfilesRepresentation(org.keycloak.representations.idm.ClientProfilesRepresentation) Test(org.junit.Test)

Example 12 with ClientPoliciesRepresentation

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

the class AbstractClientPoliciesTest method updatePolicies.

// TODO: Possibly change this to accept ClientPoliciesRepresentation instead of String to have more type-safety.
protected void updatePolicies(String json) throws ClientPolicyException {
    try {
        ClientPoliciesRepresentation clientPolicies = json == null ? null : JsonSerialization.readValue(json, ClientPoliciesRepresentation.class);
        adminClient.realm(REALM_NAME).clientPoliciesPoliciesResource().updatePolicies(clientPolicies);
    } catch (BadRequestException e) {
        throw new ClientPolicyException("update policies failed", e.getResponse().getStatusInfo().toString());
    } catch (IOException e) {
        throw new ClientPolicyException("update policies failed", e.getMessage());
    }
}
Also used : ClientPoliciesRepresentation(org.keycloak.representations.idm.ClientPoliciesRepresentation) BadRequestException(javax.ws.rs.BadRequestException) IOException(java.io.IOException) ClientPolicyException(org.keycloak.services.clientpolicy.ClientPolicyException)

Example 13 with ClientPoliciesRepresentation

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

the class AbstractClientPoliciesTest method addPolicy.

protected void addPolicy(ClientPolicyRepresentation policyRep) throws ClientPolicyException {
    ClientPoliciesRepresentation reps = getPolicies();
    if (reps == null || reps.getPolicies() == null)
        return;
    reps.getPolicies().add(policyRep);
    updatePolicies(convertToPoliciesJson(reps));
    return;
}
Also used : ClientPoliciesRepresentation(org.keycloak.representations.idm.ClientPoliciesRepresentation)

Example 14 with ClientPoliciesRepresentation

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

the class AbstractClientPoliciesTest method updatePolicy.

protected void updatePolicy(ClientPolicyRepresentation policyRep) throws ClientPolicyException {
    if (policyRep == null || policyRep.getName() == null)
        return;
    String policyName = policyRep.getName();
    ClientPoliciesRepresentation reps = getPolicies();
    if (reps.getPolicies().stream().anyMatch(i -> policyName.equals(i.getName()))) {
        ClientPolicyRepresentation rep = reps.getPolicies().stream().filter(i -> policyName.equals(i.getName())).collect(Collectors.toList()).get(0);
        reps.getPolicies().remove(rep);
        reps.getPolicies().add(policyRep);
        updatePolicies(convertToPoliciesJson(reps));
    } else {
        return;
    }
}
Also used : ClientPolicyRepresentation(org.keycloak.representations.idm.ClientPolicyRepresentation) ClientPoliciesRepresentation(org.keycloak.representations.idm.ClientPoliciesRepresentation)

Example 15 with ClientPoliciesRepresentation

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

the class ClientPoliciesTest method testPoliciesJsonView.

@Test
public void testPoliciesJsonView() throws Exception {
    clientPoliciesJsonPage.navigateTo();
    assertEquals(new ClientPoliciesRepresentation(), clientPoliciesJsonPage.form().getPolicies());
    ClientPoliciesRepresentation policies = new ClientPoliciesBuilder().addPolicy(new ClientPolicyBuilder().createPolicy("prof", "desc", false).addCondition(ClientAccessTypeConditionFactory.PROVIDER_ID, createClientAccessTypeConditionConfig(Arrays.asList(ClientAccessTypeConditionFactory.TYPE_CONFIDENTIAL, ClientAccessTypeConditionFactory.TYPE_BEARERONLY, ClientAccessTypeConditionFactory.TYPE_PUBLIC))).toRepresentation()).toRepresentation();
    testRealmResource().clientPoliciesPoliciesResource().updatePolicies(policies);
    refreshPageAndWaitForLoad();
    assertEquals(policies, clientPoliciesJsonPage.form().getPolicies());
    policies.getPolicies().add(new ClientPolicyBuilder().createPolicy("prof2", "desc2", true).toRepresentation());
    clientPoliciesJsonPage.form().setPolicies(policies);
    clientPoliciesJsonPage.form().save();
    assertAlertSuccess();
    assertClientPolicy(policies);
    clientPoliciesJsonPage.form().setPoliciesAsString("aaa");
    clientPoliciesJsonPage.form().save();
    assertAlertDanger();
}
Also used : ClientPoliciesRepresentation(org.keycloak.representations.idm.ClientPoliciesRepresentation) ClientPoliciesBuilder(org.keycloak.testsuite.util.ClientPoliciesUtil.ClientPoliciesBuilder) ClientPolicyBuilder(org.keycloak.testsuite.util.ClientPoliciesUtil.ClientPolicyBuilder) Test(org.junit.Test)

Aggregations

ClientPoliciesRepresentation (org.keycloak.representations.idm.ClientPoliciesRepresentation)21 ClientPolicyRepresentation (org.keycloak.representations.idm.ClientPolicyRepresentation)9 ClientProfilesRepresentation (org.keycloak.representations.idm.ClientProfilesRepresentation)9 Test (org.junit.Test)6 ClientProfileRepresentation (org.keycloak.representations.idm.ClientProfileRepresentation)4 ClientPolicyConditionRepresentation (org.keycloak.representations.idm.ClientPolicyConditionRepresentation)3 ClientPoliciesBuilder (org.keycloak.testsuite.util.ClientPoliciesUtil.ClientPoliciesBuilder)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 ClientPolicyConditionConfigurationRepresentation (org.keycloak.representations.idm.ClientPolicyConditionConfigurationRepresentation)2 ClientPolicyException (org.keycloak.services.clientpolicy.ClientPolicyException)2 ClientPolicyConditionProvider (org.keycloak.services.clientpolicy.condition.ClientPolicyConditionProvider)2 ClientPolicyBuilder (org.keycloak.testsuite.util.ClientPoliciesUtil.ClientPolicyBuilder)2 ClientProfilesBuilder (org.keycloak.testsuite.util.ClientPoliciesUtil.ClientProfilesBuilder)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Collections (java.util.Collections)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Set (java.util.Set)1