@Override public void onExport(Policy policy, PolicyRepresentation representation, AuthorizationProvider authorization) { ClientPolicyRepresentation userRep = toRepresentation(policy, authorization); Map<String, String> config = new HashMap<>(); try { RealmModel realm = authorization.getRealm(); config.put("clients", JsonSerialization.writeValueAsString(userRep.getClients().stream().map(id -> realm.getClientById(id).getClientId()).collect(Collectors.toList()))); } catch (IOException cause) { throw new RuntimeException("Failed to export user policy [" + policy.getName() + "]", cause); } representation.setConfig(config); }
public void addClientScopeRolesForApplication(String authToken, String realmName, String clientApp, String appName, List<RoleRepresentation> roles) throws Exception { HttpPost post = new HttpPost(clientScopeForApplicationsEndpoint(realmName, clientApp, appName)); addAuthzHeader(post, authToken); post.setEntity(new StringEntity(JsonSerialization.writeValueAsString(roles), ContentType.create(MediaType.JSON.toString(), "UTF-8"))); exec(post, 204, "Unable to add roles to client application " + clientApp, new VoidHandler()); }
throw new RuntimeException("Failed to set attribute '" + e.getAttributeName() + "' on document type '" + result.getClass().getName() + "'", e); ctx.setContent(JsonSerialization.writeValueAsString(result));
@Override public void onExport(Policy policy, PolicyRepresentation representation, AuthorizationProvider authorizationProvider) { UserPolicyRepresentation userRep = toRepresentation(policy, authorizationProvider); Map<String, String> config = new HashMap<>(); try { UserProvider userProvider = authorizationProvider.getKeycloakSession().users(); RealmModel realm = authorizationProvider.getRealm(); config.put("users", JsonSerialization.writeValueAsString(userRep.getUsers().stream().map(id -> userProvider.getUserById(id, realm).getUsername()).collect(Collectors.toList()))); } catch (IOException cause) { throw new RuntimeException("Failed to export user policy [" + policy.getName() + "]", cause); } representation.setConfig(config); }
private void outputResult(Object result) throws IOException { if (outputClient) { if (compressed) { printOut(JsonSerialization.writeValueAsString(result)); } else { printOut(JsonSerialization.writeValueAsPrettyString(result)); } } }
public PersistentUserSessionModel getUpdatedModel() { try { String updatedData = JsonSerialization.writeValueAsString(getData()); this.model.setData(updatedData); } catch (IOException ioe) { throw new ModelException(ioe); } return this.model; }
public PersistentClientSessionModel getUpdatedModel() { try { String updatedData = JsonSerialization.writeValueAsString(getData()); this.model.setData(updatedData); } catch (IOException ioe) { throw new ModelException(ioe); } return this.model; }
public void updateApplication(String authToken, String realmName, ApplicationRepresentation app) throws Exception { HttpPut put = new HttpPut(applicationEndpoint(realmName, app.getName())); addAuthzHeader(put, authToken); put.setEntity(new StringEntity(JsonSerialization.writeValueAsString(app), ContentType.create(MediaType.JSON.toString(), "UTF-8"))); exec(put, 204, "Unable to update application named " + app.getName() + " in " + realmName + " realm", new VoidHandler()); }
public void createApplicationRole(String authToken, String realmName, String appName, String roleName) throws Exception { RoleRepresentation role = new RoleRepresentation(); role.setName(roleName); HttpPost post = new HttpPost(applicationRolesEndpoint(realmName, appName)); addAuthzHeader(post, authToken); post.setEntity(new StringEntity(JsonSerialization.writeValueAsString(role), ContentType.create(MediaType.JSON.toString(), "UTF-8"))); exec(post, 201, "Unable to create application role named " + roleName + " in application " + appName, new VoidHandler()); }
public void createApplication(String authToken, String realmName, String appName) throws Exception { ApplicationRepresentation app = new ApplicationRepresentation(); app.setName(appName); HttpPost post = new HttpPost(realmApplicationsEndpoint(realmName)); addAuthzHeader(post, authToken); post.setEntity(new StringEntity(JsonSerialization.writeValueAsString(app), ContentType.create(MediaType.JSON.toString(), "UTF-8"))); exec(post, 201, "Unable to create application named " + appName + " in " + realmName + " realm", new VoidHandler()); }
private void updateClients(Policy policy, Set<String> clients, AuthorizationProvider authorization) { RealmModel realm = authorization.getRealm(); if (clients == null || clients.isEmpty()) { throw new RuntimeException("No client provided."); } Set<String> updatedClients = new HashSet<>(); for (String id : clients) { ClientModel client = realm.getClientByClientId(id); if (client == null) { client = realm.getClientById(id); } if (client == null) { throw new RuntimeException("Error while updating policy [" + policy.getName() + "]. Client [" + id + "] could not be found."); } updatedClients.add(client.getId()); } try { policy.putConfig("clients", JsonSerialization.writeValueAsString(updatedClients)); } catch (IOException cause) { throw new RuntimeException("Failed to serialize clients", cause); } }
private void updateResourceServer(ClientModel clientModel, RoleModel removedRole, ResourceServerStore resourceServerStore, PolicyStore policyStore) { ResourceServer resourceServer = resourceServerStore.findById(clientModel.getId()); if (resourceServer != null) { policyStore.findByType(getId(), resourceServer.getId()).forEach(policy -> { List<Map> roles = new ArrayList<>(); for (Map<String,Object> role : getRoles(policy)) { if (!role.get("id").equals(removedRole.getId())) { Map updated = new HashMap(); updated.put("id", role.get("id")); Object required = role.get("required"); if (required != null) { updated.put("required", required); } roles.add(updated); } } try { if (roles.isEmpty()) { policyStore.delete(policy.getId()); } else { policy.putConfig("roles", JsonSerialization.writeValueAsString(roles)); } } catch (IOException e) { throw new RuntimeException("Error while synchronizing roles with policy [" + policy.getName() + "].", e); } }); } }
public void removeClientScopeRolesForApplication(String authToken, String realmName, String clientApp, String appName, List<RoleRepresentation> roles) throws Exception { HttpDeleteWithEntity delete = new HttpDeleteWithEntity(clientScopeForApplicationsEndpoint(realmName, clientApp, appName)); addAuthzHeader(delete, authToken); delete.setEntity(new StringEntity(JsonSerialization.writeValueAsString(roles), ContentType.create(MediaType.JSON.toString(), "UTF-8"))); exec(delete, 204, "Unable to delete roles on client application " + clientApp, new VoidHandler()); }
private void outputResult(String clientId, Object result) throws IOException { if (returnClientId) { printOut(clientId); } else if (outputClient) { if (compressed) { printOut(JsonSerialization.writeValueAsString(result)); } else { printOut(JsonSerialization.writeValueAsPrettyString(result)); } } else { printErr("Registered new client with client_id '" + clientId + "'"); } }
private void updateUsers(Policy policy, AuthorizationProvider authorization, Set<String> users) { KeycloakSession session = authorization.getKeycloakSession(); RealmModel realm = authorization.getRealm(); UserProvider userProvider = session.users(); Set<String> updatedUsers = new HashSet<>(); if (users != null) { for (String userId : users) { UserModel user = null; try { user = userProvider.getUserByUsername(userId, realm); } catch (Exception ignore) { } if (user == null) { user = userProvider.getUserById(userId, realm); } if (user == null) { throw new RuntimeException("Error while updating policy [" + policy.getName() + "]. User [" + userId + "] could not be found."); } updatedUsers.add(user.getId()); } } try { policy.putConfig("users", JsonSerialization.writeValueAsString(updatedUsers)); } catch (IOException cause) { throw new RuntimeException("Failed to serialize users", cause); } }
@Override public void onExport(Policy policy, PolicyRepresentation representation, AuthorizationProvider authorization) { Map<String, String> config = new HashMap<>(); GroupPolicyRepresentation groupPolicy = toRepresentation(policy, authorization); Set<GroupPolicyRepresentation.GroupDefinition> groups = groupPolicy.getGroups(); for (GroupPolicyRepresentation.GroupDefinition definition: groups) { GroupModel group = authorization.getRealm().getGroupById(definition.getId()); definition.setId(null); definition.setPath(ModelToRepresentation.buildGroupPath(group)); } try { String groupsClaim = groupPolicy.getGroupsClaim(); if (groupsClaim != null) { config.put("groupsClaim", groupsClaim); } config.put("groups", JsonSerialization.writeValueAsString(groups)); } catch (IOException cause) { throw new RuntimeException("Failed to export group policy [" + policy.getName() + "]", cause); } representation.setConfig(config); }
@Override public void onExport(Policy policy, PolicyRepresentation representation, AuthorizationProvider authorizationProvider) { Map<String, String> config = new HashMap<>(); Set<RolePolicyRepresentation.RoleDefinition> roles = toRepresentation(policy, authorizationProvider).getRoles(); for (RolePolicyRepresentation.RoleDefinition roleDefinition : roles) { RoleModel role = authorizationProvider.getRealm().getRoleById(roleDefinition.getId()); if (role.isClientRole()) { roleDefinition.setId(ClientModel.class.cast(role.getContainer()).getClientId() + "/" + role.getName()); } else { roleDefinition.setId(role.getName()); } } try { config.put("roles", JsonSerialization.writeValueAsString(roles)); } catch (IOException cause) { throw new RuntimeException("Failed to export role policy [" + policy.getName() + "]", cause); } representation.setConfig(config); }
policyStore.delete(policy.getId()); } else { policy.putConfig("clients", JsonSerialization.writeValueAsString(clients));
policyStore.delete(policy.getId()); } else { policy.putConfig("users", JsonSerialization.writeValueAsString(users));
throw new RuntimeException("Failed to set attribute '" + e.getAttributeName() + "' on document type '" + type.getName() + "'", e); content = JsonSerialization.writeValueAsString(rep); } else { throw new RuntimeException("Setting attributes is not supported for type: " + type.getName());