use of org.keycloak.representations.idm.ClientProfilesRepresentation in project keycloak by keycloak.
the class ClientPoliciesUtil method getValidatedGlobalClientProfilesRepresentation.
/**
* get validated and modified global (built-in) client profiles set on keycloak app as representation.
* it is loaded from json file enclosed in keycloak's binary.
* not return null.
*/
static List<ClientProfileRepresentation> getValidatedGlobalClientProfilesRepresentation(KeycloakSession session, InputStream is) throws ClientPolicyException {
// load builtin client profiles representation
ClientProfilesRepresentation proposedProfilesRep = null;
try {
proposedProfilesRep = JsonSerialization.readValue(is, ClientProfilesRepresentation.class);
} catch (Exception e) {
throw new ClientPolicyException("failed to deserialize global proposed client profiles json string.", e.getMessage());
}
if (proposedProfilesRep == null) {
return Collections.emptyList();
}
// no profile contained (it is valid)
List<ClientProfileRepresentation> proposedProfileRepList = proposedProfilesRep.getProfiles();
if (proposedProfileRepList == null || proposedProfileRepList.isEmpty()) {
return Collections.emptyList();
}
// duplicated profile name is not allowed.
if (proposedProfileRepList.size() != proposedProfileRepList.stream().map(i -> i.getName()).distinct().count()) {
throw new ClientPolicyException("proposed global client profile name duplicated.");
}
// construct validated and modified profiles from builtin profiles in JSON file enclosed in keycloak binary.
List<ClientProfileRepresentation> updatingProfileList = new LinkedList<>();
for (ClientProfileRepresentation proposedProfileRep : proposedProfilesRep.getProfiles()) {
if (proposedProfileRep.getName() == null) {
throw new ClientPolicyException("client profile without its name not allowed.");
}
ClientProfileRepresentation profileRep = new ClientProfileRepresentation();
profileRep.setName(proposedProfileRep.getName());
profileRep.setDescription(proposedProfileRep.getDescription());
// to prevent returning null
profileRep.setExecutors(new ArrayList<>());
if (proposedProfileRep.getExecutors() != null) {
for (ClientPolicyExecutorRepresentation executorRep : proposedProfileRep.getExecutors()) {
// Skip the check if feature is disabled as then the executor implementations are disabled
if (Profile.isFeatureEnabled(Profile.Feature.CLIENT_POLICIES) && !isValidExecutor(session, executorRep.getExecutorProviderId())) {
throw new ClientPolicyException("proposed client profile contains the executor with its invalid configuration.");
}
profileRep.getExecutors().add(executorRep);
}
}
updatingProfileList.add(profileRep);
}
return updatingProfileList;
}
use of org.keycloak.representations.idm.ClientProfilesRepresentation in project keycloak by keycloak.
the class DefaultClientPolicyManager method updateClientProfiles.
@Override
public void updateClientProfiles(RealmModel realm, ClientProfilesRepresentation clientProfiles) throws ClientPolicyException {
try {
if (clientProfiles == null) {
throw new ClientPolicyException("Passing null clientProfiles not allowed");
}
ClientProfilesRepresentation validatedProfilesRep = ClientPoliciesUtil.getValidatedClientProfilesForUpdate(session, realm, clientProfiles, globalClientProfilesSupplier.get());
String validatedJsonString = ClientPoliciesUtil.convertClientProfilesRepresentationToJson(validatedProfilesRep);
ClientPoliciesUtil.setClientProfilesJsonString(realm, validatedJsonString);
logger.tracev("UPDATE PROFILES :: realm = {0}, validated and modified PUT = {1}", realm.getName(), validatedJsonString);
} catch (ClientPolicyException e) {
logger.warnv("VALIDATE SERIALIZE PROFILES FAILED :: error = {0}, error detail = {1}", e.getError(), e.getErrorDetail());
throw e;
}
}
use of org.keycloak.representations.idm.ClientProfilesRepresentation in project keycloak by keycloak.
the class DefaultClientPolicyManager method updateRealmRepresentationFromModel.
@Override
public void updateRealmRepresentationFromModel(RealmModel realm, RealmRepresentation rep) {
try {
// client profiles that filter out global profiles..
ClientProfilesRepresentation filteredOutProfiles = getClientProfiles(realm, false);
rep.setParsedClientProfiles(filteredOutProfiles);
ClientPoliciesRepresentation filteredOutPolicies = getClientPolicies(realm);
rep.setParsedClientPolicies(filteredOutPolicies);
} catch (ClientPolicyException cpe) {
throw new IllegalStateException("Exception during export client profiles or client policies", cpe);
}
}
use of org.keycloak.representations.idm.ClientProfilesRepresentation in project keycloak by keycloak.
the class ClientPoliciesTest method testProfilesFormView.
@Test
public void testProfilesFormView() throws Exception {
final String profileName = "mega-profile";
final String profileName2 = "mega-profile^2";
final String profileDesc = "mega-desc";
clientProfilesPage.navigateTo();
clientProfilesPage.assertCurrent();
clientProfilesPage.profilesTable().clickCreateProfile();
createClientProfilePage.assertCurrent();
// create profile
createClientProfilePage.form().setProfileName(profileName);
createClientProfilePage.form().setDescription(profileDesc);
createClientProfilePage.form().save();
assertAlertSuccess();
clientProfilePage.setProfileName(profileName);
clientProfilePage.assertCurrent();
assertEquals(profileName, clientProfilePage.form().getProfileName());
clientProfilePage.executorsTable().clickCreateExecutor();
// create executors
createExecutorPage.setProfileName(profileName);
createExecutorPage.assertCurrent();
createExecutorPage.form().setExecutorType(SecureClientAuthenticatorExecutorFactory.PROVIDER_ID);
assertTrue(createExecutorPage.form().getSelect2SelectedItems().isEmpty());
createExecutorPage.form().selectSelect2Item(JWTClientAuthenticator.PROVIDER_ID);
createExecutorPage.form().selectSelect2Item(ClientIdAndSecretAuthenticator.PROVIDER_ID);
createExecutorPage.form().save();
assertAlertSuccess();
clientProfilePage.assertCurrent();
clientProfilePage.executorsTable().clickEditExecutor(SecureClientAuthenticatorExecutorFactory.PROVIDER_ID);
executorPage.setUriParameters(profileName, 0);
executorPage.assertCurrent();
assertEquals(Stream.of(JWTClientAuthenticator.PROVIDER_ID, ClientIdAndSecretAuthenticator.PROVIDER_ID).collect(Collectors.toSet()), executorPage.form().getSelect2SelectedItems());
createExecutorPage.navigateTo();
createExecutorPage.form().setExecutorType(HolderOfKeyEnforcerExecutorFactory.PROVIDER_ID);
assertFalse(createExecutorPage.form().isAutoConfigure());
createExecutorPage.form().setAutoConfigure(true);
createExecutorPage.form().save();
clientProfilePage.executorsTable().clickEditExecutor(HolderOfKeyEnforcerExecutorFactory.PROVIDER_ID);
executorPage.setUriParameters(profileName, 1);
executorPage.assertCurrent();
assertTrue(executorPage.form().isAutoConfigure());
// assert JSON
ClientProfilesRepresentation expected = new ClientProfilesBuilder().addProfile(new ClientProfileBuilder().createProfile(profileName, profileDesc).addExecutor(SecureClientAuthenticatorExecutorFactory.PROVIDER_ID, createSecureClientAuthenticatorExecutorConfig(Arrays.asList(JWTClientAuthenticator.PROVIDER_ID, ClientIdAndSecretAuthenticator.PROVIDER_ID), JWTClientAuthenticator.PROVIDER_ID)).addExecutor(HolderOfKeyEnforcerExecutorFactory.PROVIDER_ID, createHolderOfKeyEnforceExecutorConfig(true)).toRepresentation()).toRepresentation();
assertClientProfile(expected, false);
// remove executor
clientProfilePage.navigateTo();
clientProfilePage.executorsTable().clickDeleteExecutor(SecureClientAuthenticatorExecutorFactory.PROVIDER_ID);
modalDialog.confirmDeletion();
assertAlertSuccess();
expected.getProfiles().get(0).getExecutors().remove(0);
assertClientProfile(expected, false);
assertFalse(clientProfilePage.executorsTable().isRowPresent(SecureClientAuthenticatorExecutorFactory.PROVIDER_ID));
// edit executor
clientProfilePage.executorsTable().clickEditExecutor(HolderOfKeyEnforcerExecutorFactory.PROVIDER_ID);
executorPage.form().setAutoConfigure(false);
executorPage.form().save();
expected.getProfiles().get(0).getExecutors().get(0).setConfiguration(JsonSerialization.mapper.readValue(JsonSerialization.mapper.writeValueAsBytes(createHolderOfKeyEnforceExecutorConfig(false)), JsonNode.class));
assertClientProfile(expected, false);
// edit profile
clientProfilePage.form().setProfileName(profileName2);
clientProfilePage.form().save();
assertAlertSuccess();
clientProfilesPage.navigateTo();
assertEquals(profileDesc, clientProfilesPage.profilesTable().getDescription(profileName2));
// remove profile
clientProfilesPage.profilesTable().clickDeleteProfile(profileName2);
modalDialog.confirmDeletion();
assertAlertSuccess();
assertClientProfile(new ClientProfilesRepresentation(), false);
assertFalse(clientProfilesPage.profilesTable().isRowPresent(profileName2));
}
use of org.keycloak.representations.idm.ClientProfilesRepresentation in project keycloak by keycloak.
the class ClientPoliciesTest method testProfilesJsonView.
@Test
public void testProfilesJsonView() throws Exception {
clientProfilesJsonPage.navigateTo();
ClientProfilesRepresentation profiles = testRealmResource().clientPoliciesProfilesResource().getProfiles(true);
assertEquals(profiles, clientProfilesJsonPage.form().getProfiles());
profiles.getProfiles().add(new ClientProfileBuilder().createProfile("prof", "desc").addExecutor(HolderOfKeyEnforcerExecutorFactory.PROVIDER_ID, createHolderOfKeyEnforceExecutorConfig(true)).toRepresentation());
testRealmResource().clientPoliciesProfilesResource().updateProfiles(profiles);
refreshPageAndWaitForLoad();
assertEquals(profiles, clientProfilesJsonPage.form().getProfiles());
profiles.getProfiles().add(new ClientProfileBuilder().createProfile("prof2", "desc2").toRepresentation());
clientProfilesJsonPage.form().setProfiles(profiles);
clientProfilesJsonPage.form().save();
assertAlertSuccess();
assertClientProfile(profiles, true);
clientProfilesJsonPage.form().setProfilesAsString("aaa");
clientProfilesJsonPage.form().save();
assertAlertDanger();
}
Aggregations