Search in sources :

Example 6 with RequiredActionProviderModel

use of org.keycloak.models.RequiredActionProviderModel in project keycloak by keycloak.

the class AuthenticationManagementResource method raiseRequiredActionPriority.

/**
 * Raise required action's priority
 *
 * @param alias Alias of required action
 */
@Path("required-actions/{alias}/raise-priority")
@POST
@NoCache
public void raiseRequiredActionPriority(@PathParam("alias") String alias) {
    auth.realm().requireManageRealm();
    RequiredActionProviderModel model = realm.getRequiredActionProviderByAlias(alias);
    if (model == null) {
        throw new NotFoundException("Failed to find required action.");
    }
    RequiredActionProviderModel previous = null;
    for (RequiredActionProviderModel action : realm.getRequiredActionProvidersStream().collect(Collectors.toList())) {
        if (action.getId().equals(model.getId())) {
            break;
        }
        previous = action;
    }
    if (previous == null)
        return;
    int tmp = previous.getPriority();
    previous.setPriority(model.getPriority());
    realm.updateRequiredActionProvider(previous);
    model.setPriority(tmp);
    realm.updateRequiredActionProvider(model);
    adminEvent.operation(OperationType.UPDATE).resource(ResourceType.REQUIRED_ACTION).resourcePath(session.getContext().getUri()).success();
}
Also used : RequiredActionProviderModel(org.keycloak.models.RequiredActionProviderModel) NotFoundException(javax.ws.rs.NotFoundException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) NoCache(org.jboss.resteasy.annotations.cache.NoCache)

Example 7 with RequiredActionProviderModel

use of org.keycloak.models.RequiredActionProviderModel in project keycloak by keycloak.

the class AuthenticationManagementResource method updateRequiredAction.

/**
 * Update required action
 *
 * @param alias Alias of required action
 * @param rep JSON describing new state of required action
 */
@Path("required-actions/{alias}")
@PUT
@Consumes(MediaType.APPLICATION_JSON)
public void updateRequiredAction(@PathParam("alias") String alias, RequiredActionProviderRepresentation rep) {
    auth.realm().requireManageRealm();
    RequiredActionProviderModel model = realm.getRequiredActionProviderByAlias(alias);
    if (model == null) {
        throw new NotFoundException("Failed to find required action");
    }
    RequiredActionProviderModel update = new RequiredActionProviderModel();
    update.setId(model.getId());
    update.setName(rep.getName());
    update.setAlias(rep.getAlias());
    update.setProviderId(model.getProviderId());
    update.setDefaultAction(rep.isDefaultAction());
    update.setPriority(rep.getPriority());
    update.setEnabled(rep.isEnabled());
    update.setConfig(rep.getConfig());
    realm.updateRequiredActionProvider(update);
    adminEvent.operation(OperationType.UPDATE).resource(ResourceType.REQUIRED_ACTION).resourcePath(session.getContext().getUri()).representation(rep).success();
}
Also used : RequiredActionProviderModel(org.keycloak.models.RequiredActionProviderModel) NotFoundException(javax.ws.rs.NotFoundException) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 8 with RequiredActionProviderModel

use of org.keycloak.models.RequiredActionProviderModel in project keycloak by keycloak.

the class AuthenticationManagementResource method registerRequiredAction.

/**
 * Register a new required actions
 *
 * @param data JSON containing 'providerId', and 'name' attributes.
 */
@Path("register-required-action")
@POST
@Consumes(MediaType.APPLICATION_JSON)
@NoCache
public void registerRequiredAction(Map<String, String> data) {
    auth.realm().requireManageRealm();
    String providerId = data.get("providerId");
    String name = data.get("name");
    RequiredActionProviderModel requiredAction = new RequiredActionProviderModel();
    requiredAction.setAlias(providerId);
    requiredAction.setName(name);
    requiredAction.setProviderId(providerId);
    requiredAction.setDefaultAction(false);
    requiredAction.setPriority(getNextRequiredActionPriority());
    requiredAction.setEnabled(true);
    requiredAction = realm.addRequiredActionProvider(requiredAction);
    data.put("id", requiredAction.getId());
    adminEvent.operation(OperationType.CREATE).resource(ResourceType.REQUIRED_ACTION).resourcePath(session.getContext().getUri()).representation(data).success();
}
Also used : RequiredActionProviderModel(org.keycloak.models.RequiredActionProviderModel) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) NoCache(org.jboss.resteasy.annotations.cache.NoCache)

Example 9 with RequiredActionProviderModel

use of org.keycloak.models.RequiredActionProviderModel in project keycloak by keycloak.

the class AuthenticationManagementResource method getUnregisteredRequiredActions.

/**
 * Get unregistered required actions
 *
 * Returns a stream of unregistered required actions.
 */
@Path("unregistered-required-actions")
@GET
@Produces(MediaType.APPLICATION_JSON)
@NoCache
public Stream<Map<String, String>> getUnregisteredRequiredActions() {
    auth.realm().requireViewRealm();
    Set<String> providerIds = realm.getRequiredActionProvidersStream().map(RequiredActionProviderModel::getProviderId).collect(Collectors.toSet());
    return session.getKeycloakSessionFactory().getProviderFactoriesStream(RequiredActionProvider.class).filter(factory -> !providerIds.contains(factory.getId())).map(factory -> {
        RequiredActionFactory r = (RequiredActionFactory) factory;
        Map<String, String> m = new HashMap<>();
        m.put("name", r.getDisplayText());
        m.put("providerId", r.getId());
        return m;
    });
}
Also used : ConfigPropertyRepresentation(org.keycloak.representations.idm.ConfigPropertyRepresentation) ResourceType(org.keycloak.events.admin.ResourceType) Produces(javax.ws.rs.Produces) RequiredActionFactory(org.keycloak.authentication.RequiredActionFactory) Path(javax.ws.rs.Path) ClientAuthenticatorFactory(org.keycloak.authentication.ClientAuthenticatorFactory) DefaultAuthenticationFlows(org.keycloak.models.utils.DefaultAuthenticationFlows) RepresentationToModel(org.keycloak.models.utils.RepresentationToModel) MediaType(javax.ws.rs.core.MediaType) Consumes(javax.ws.rs.Consumes) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) AuthenticationExecutionRepresentation(org.keycloak.representations.idm.AuthenticationExecutionRepresentation) FormAuthenticator(org.keycloak.authentication.FormAuthenticator) BadRequestException(javax.ws.rs.BadRequestException) UriBuilder(javax.ws.rs.core.UriBuilder) DELETE(javax.ws.rs.DELETE) RealmModel(org.keycloak.models.RealmModel) NOT_FOUND(javax.ws.rs.core.Response.Status.NOT_FOUND) CredentialHelper(org.keycloak.utils.CredentialHelper) Set(java.util.Set) Collectors(java.util.stream.Collectors) NotFoundException(javax.ws.rs.NotFoundException) ReservedCharValidator(org.keycloak.utils.ReservedCharValidator) Objects(java.util.Objects) ClientAuthenticator(org.keycloak.authentication.ClientAuthenticator) ModelToRepresentation(org.keycloak.models.utils.ModelToRepresentation) List(java.util.List) Stream(java.util.stream.Stream) Response(javax.ws.rs.core.Response) Optional(java.util.Optional) RequiredActionProviderModel(org.keycloak.models.RequiredActionProviderModel) OperationType(org.keycloak.events.admin.OperationType) PathParam(javax.ws.rs.PathParam) AuthenticationFlow(org.keycloak.authentication.AuthenticationFlow) AuthenticatorConfigInfoRepresentation(org.keycloak.representations.idm.AuthenticatorConfigInfoRepresentation) GET(javax.ws.rs.GET) Logger(org.jboss.logging.Logger) ProviderConfigProperty(org.keycloak.provider.ProviderConfigProperty) HashMap(java.util.HashMap) AuthenticationExecutionInfoRepresentation(org.keycloak.representations.idm.AuthenticationExecutionInfoRepresentation) AuthenticationExecutionModel(org.keycloak.models.AuthenticationExecutionModel) AuthenticationFlowRepresentation(org.keycloak.representations.idm.AuthenticationFlowRepresentation) AuthenticatorConfigModel(org.keycloak.models.AuthenticatorConfigModel) AuthenticationFlowModel(org.keycloak.models.AuthenticationFlowModel) ConfigurableAuthenticatorFactory(org.keycloak.authentication.ConfigurableAuthenticatorFactory) LinkedList(java.util.LinkedList) ProviderFactory(org.keycloak.provider.ProviderFactory) FormAction(org.keycloak.authentication.FormAction) POST(javax.ws.rs.POST) Authenticator(org.keycloak.authentication.Authenticator) AdminPermissionEvaluator(org.keycloak.services.resources.admin.permissions.AdminPermissionEvaluator) KeycloakSession(org.keycloak.models.KeycloakSession) RequiredActionProviderRepresentation(org.keycloak.representations.idm.RequiredActionProviderRepresentation) RequiredActionProvider(org.keycloak.authentication.RequiredActionProvider) AuthenticatorConfigRepresentation(org.keycloak.representations.idm.AuthenticatorConfigRepresentation) NoCache(org.jboss.resteasy.annotations.cache.NoCache) PUT(javax.ws.rs.PUT) Collections(java.util.Collections) ErrorResponse(org.keycloak.services.ErrorResponse) RequiredActionFactory(org.keycloak.authentication.RequiredActionFactory) HashMap(java.util.HashMap) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) NoCache(org.jboss.resteasy.annotations.cache.NoCache)

Example 10 with RequiredActionProviderModel

use of org.keycloak.models.RequiredActionProviderModel in project keycloak by keycloak.

the class AuthenticationManagementResource method lowerRequiredActionPriority.

/**
 * Lower required action's priority
 *
 * @param alias Alias of required action
 */
@Path("/required-actions/{alias}/lower-priority")
@POST
@NoCache
public void lowerRequiredActionPriority(@PathParam("alias") String alias) {
    auth.realm().requireManageRealm();
    RequiredActionProviderModel model = realm.getRequiredActionProviderByAlias(alias);
    if (model == null) {
        throw new NotFoundException("Failed to find required action.");
    }
    List<RequiredActionProviderModel> actions = realm.getRequiredActionProvidersStream().collect(Collectors.toList());
    int i;
    for (i = 0; i < actions.size(); i++) {
        if (actions.get(i).getId().equals(model.getId())) {
            break;
        }
    }
    if (i + 1 >= actions.size())
        return;
    RequiredActionProviderModel next = actions.get(i + 1);
    int tmp = model.getPriority();
    model.setPriority(next.getPriority());
    realm.updateRequiredActionProvider(model);
    next.setPriority(tmp);
    realm.updateRequiredActionProvider(next);
    adminEvent.operation(OperationType.UPDATE).resource(ResourceType.REQUIRED_ACTION).resourcePath(session.getContext().getUri()).success();
}
Also used : RequiredActionProviderModel(org.keycloak.models.RequiredActionProviderModel) NotFoundException(javax.ws.rs.NotFoundException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) NoCache(org.jboss.resteasy.annotations.cache.NoCache)

Aggregations

RequiredActionProviderModel (org.keycloak.models.RequiredActionProviderModel)19 Path (javax.ws.rs.Path)7 NotFoundException (javax.ws.rs.NotFoundException)6 NoCache (org.jboss.resteasy.annotations.cache.NoCache)5 POST (javax.ws.rs.POST)4 Consumes (javax.ws.rs.Consumes)3 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Map (java.util.Map)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 DELETE (javax.ws.rs.DELETE)2 GET (javax.ws.rs.GET)2 PUT (javax.ws.rs.PUT)2 Produces (javax.ws.rs.Produces)2 UserModel (org.keycloak.models.UserModel)2 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 Objects (java.util.Objects)1