Search in sources :

Example 26 with AuthenticationExecutionModel

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

the class FlowUtil method addAuthenticatorExecution.

public FlowUtil addAuthenticatorExecution(Requirement requirement, String providerId, int priority, Consumer<AuthenticatorConfigModel> configInitializer) {
    maxPriority = Math.max(maxPriority, priority);
    AuthenticationExecutionModel execution = new AuthenticationExecutionModel();
    execution.setRequirement(requirement);
    execution.setAuthenticatorFlow(false);
    execution.setAuthenticator(providerId);
    execution.setPriority(priority);
    execution.setParentFlow(currentFlow.getId());
    if (configInitializer != null) {
        AuthenticatorConfigModel authConfig = new AuthenticatorConfigModel();
        authConfig.setId(UUID.randomUUID().toString());
        // Caller is free to update this alias
        authConfig.setAlias("cfg" + authConfig.getId().hashCode());
        authConfig.setConfig(new HashMap<>());
        configInitializer.accept(authConfig);
        realm.addAuthenticatorConfig(authConfig);
        execution.setAuthenticatorConfig(authConfig.getId());
    }
    realm.addAuthenticatorExecution(execution);
    return this;
}
Also used : AuthenticationExecutionModel(org.keycloak.models.AuthenticationExecutionModel) AuthenticatorConfigModel(org.keycloak.models.AuthenticatorConfigModel)

Example 27 with AuthenticationExecutionModel

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

the class AuthenticatorSubflowsTest2 method setupFlows.

@Before
public void setupFlows() {
    testingClient.server().run(session -> {
        RealmModel realm = session.realms().getRealmByName("test");
        if (realm.getBrowserFlow().getAlias().equals("parent-flow")) {
            return;
        }
        // Parent flow
        AuthenticationFlowModel browser = new AuthenticationFlowModel();
        browser.setAlias("parent-flow");
        browser.setDescription("browser based authentication");
        browser.setProviderId("basic-flow");
        browser.setTopLevel(true);
        browser.setBuiltIn(true);
        browser = realm.addAuthenticationFlow(browser);
        realm.setBrowserFlow(browser);
        // Subflow1
        AuthenticationFlowModel subflow1 = new AuthenticationFlowModel();
        subflow1.setTopLevel(false);
        subflow1.setBuiltIn(true);
        subflow1.setAlias("subflow-1");
        subflow1.setDescription("Parameter 'foo=bar1' AND username+password");
        subflow1.setProviderId("basic-flow");
        subflow1 = realm.addAuthenticationFlow(subflow1);
        AuthenticationExecutionModel execution = new AuthenticationExecutionModel();
        execution.setParentFlow(browser.getId());
        execution.setRequirement(AuthenticationExecutionModel.Requirement.ALTERNATIVE);
        execution.setFlowId(subflow1.getId());
        execution.setPriority(10);
        execution.setAuthenticatorFlow(true);
        realm.addAuthenticatorExecution(execution);
        // Subflow1 - username password
        execution = new AuthenticationExecutionModel();
        execution.setParentFlow(subflow1.getId());
        execution.setRequirement(AuthenticationExecutionModel.Requirement.REQUIRED);
        execution.setAuthenticator(UsernamePasswordFormFactory.PROVIDER_ID);
        execution.setPriority(10);
        execution.setAuthenticatorFlow(false);
        realm.addAuthenticatorExecution(execution);
        // Subflow1 - foo=bar1
        execution = new AuthenticationExecutionModel();
        execution.setParentFlow(subflow1.getId());
        execution.setRequirement(AuthenticationExecutionModel.Requirement.REQUIRED);
        execution.setAuthenticator(ExpectedParamAuthenticatorFactory.PROVIDER_ID);
        execution.setPriority(20);
        execution.setAuthenticatorFlow(false);
        AuthenticatorConfigModel configModel = new AuthenticatorConfigModel();
        configModel.setAlias("bar1");
        Map<String, String> config = new HashMap<>();
        config.put(ExpectedParamAuthenticator.EXPECTED_VALUE, "bar1");
        configModel.setConfig(config);
        configModel = realm.addAuthenticatorConfig(configModel);
        execution.setAuthenticatorConfig(configModel.getId());
        realm.addAuthenticatorExecution(execution);
        // Subflow2
        AuthenticationFlowModel subflow2 = new AuthenticationFlowModel();
        subflow2.setTopLevel(false);
        subflow2.setBuiltIn(true);
        subflow2.setAlias("subflow-2");
        subflow2.setDescription("username+password AND pushButton");
        subflow2.setProviderId("basic-flow");
        subflow2 = realm.addAuthenticationFlow(subflow2);
        execution = new AuthenticationExecutionModel();
        execution.setParentFlow(browser.getId());
        execution.setRequirement(AuthenticationExecutionModel.Requirement.ALTERNATIVE);
        execution.setFlowId(subflow2.getId());
        execution.setPriority(20);
        execution.setAuthenticatorFlow(true);
        realm.addAuthenticatorExecution(execution);
        // Subflow2 - push the button
        execution = new AuthenticationExecutionModel();
        execution.setParentFlow(subflow2.getId());
        execution.setRequirement(AuthenticationExecutionModel.Requirement.REQUIRED);
        execution.setAuthenticator(PushButtonAuthenticatorFactory.PROVIDER_ID);
        execution.setPriority(10);
        execution.setAuthenticatorFlow(false);
        realm.addAuthenticatorExecution(execution);
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) HashMap(java.util.HashMap) AuthenticationExecutionModel(org.keycloak.models.AuthenticationExecutionModel) AuthenticationFlowModel(org.keycloak.models.AuthenticationFlowModel) AuthenticatorConfigModel(org.keycloak.models.AuthenticatorConfigModel) Before(org.junit.Before)

Example 28 with AuthenticationExecutionModel

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

the class AuthenticatorSubflowsTest method setupFlows.

@Before
public void setupFlows() {
    testingClient.server().run(session -> {
        RealmModel realm = session.realms().getRealmByName("test");
        if (realm.getBrowserFlow().getAlias().equals("parent-flow")) {
            return;
        }
        // Parent flow
        AuthenticationFlowModel browser = new AuthenticationFlowModel();
        browser.setAlias("parent-flow");
        browser.setDescription("browser based authentication");
        browser.setProviderId("basic-flow");
        browser.setTopLevel(true);
        browser.setBuiltIn(true);
        browser = realm.addAuthenticationFlow(browser);
        realm.setBrowserFlow(browser);
        // Subflow1
        AuthenticationFlowModel subflow1 = new AuthenticationFlowModel();
        subflow1.setTopLevel(false);
        subflow1.setBuiltIn(true);
        subflow1.setAlias("subflow-1");
        subflow1.setDescription("Parameter 'foo=bar1' AND username+password");
        subflow1.setProviderId("basic-flow");
        subflow1 = realm.addAuthenticationFlow(subflow1);
        AuthenticationExecutionModel execution = new AuthenticationExecutionModel();
        execution.setParentFlow(browser.getId());
        execution.setRequirement(AuthenticationExecutionModel.Requirement.ALTERNATIVE);
        execution.setFlowId(subflow1.getId());
        execution.setPriority(10);
        execution.setAuthenticatorFlow(true);
        realm.addAuthenticatorExecution(execution);
        // Subflow1 - foo=bar1
        execution = new AuthenticationExecutionModel();
        execution.setParentFlow(subflow1.getId());
        execution.setRequirement(AuthenticationExecutionModel.Requirement.REQUIRED);
        execution.setAuthenticator(ExpectedParamAuthenticatorFactory.PROVIDER_ID);
        execution.setPriority(10);
        execution.setAuthenticatorFlow(false);
        AuthenticatorConfigModel configModel = new AuthenticatorConfigModel();
        configModel.setAlias("bar1");
        Map<String, String> config = new HashMap<>();
        config.put(ExpectedParamAuthenticator.EXPECTED_VALUE, "bar1");
        configModel.setConfig(config);
        configModel = realm.addAuthenticatorConfig(configModel);
        execution.setAuthenticatorConfig(configModel.getId());
        realm.addAuthenticatorExecution(execution);
        // Subflow1 - username password
        execution = new AuthenticationExecutionModel();
        execution.setParentFlow(subflow1.getId());
        execution.setRequirement(AuthenticationExecutionModel.Requirement.REQUIRED);
        execution.setAuthenticator(UsernamePasswordFormFactory.PROVIDER_ID);
        execution.setPriority(20);
        execution.setAuthenticatorFlow(false);
        realm.addAuthenticatorExecution(execution);
        // Subflow2
        AuthenticationFlowModel subflow2 = new AuthenticationFlowModel();
        subflow2.setTopLevel(false);
        subflow2.setBuiltIn(true);
        subflow2.setAlias("subflow-2");
        subflow2.setDescription("username+password AND pushButton");
        subflow2.setProviderId("basic-flow");
        subflow2 = realm.addAuthenticationFlow(subflow2);
        execution = new AuthenticationExecutionModel();
        execution.setParentFlow(browser.getId());
        execution.setRequirement(AuthenticationExecutionModel.Requirement.ALTERNATIVE);
        execution.setFlowId(subflow2.getId());
        execution.setPriority(20);
        execution.setAuthenticatorFlow(true);
        realm.addAuthenticatorExecution(execution);
        // Subflow2 - push the button
        execution = new AuthenticationExecutionModel();
        execution.setParentFlow(subflow2.getId());
        execution.setRequirement(AuthenticationExecutionModel.Requirement.REQUIRED);
        execution.setAuthenticator(PushButtonAuthenticatorFactory.PROVIDER_ID);
        execution.setPriority(10);
        execution.setAuthenticatorFlow(false);
        realm.addAuthenticatorExecution(execution);
        // Subflow2 - username-password
        execution = new AuthenticationExecutionModel();
        execution.setParentFlow(subflow2.getId());
        execution.setRequirement(AuthenticationExecutionModel.Requirement.REQUIRED);
        execution.setAuthenticator(UsernamePasswordFormFactory.PROVIDER_ID);
        execution.setPriority(20);
        execution.setAuthenticatorFlow(false);
        realm.addAuthenticatorExecution(execution);
        // Subflow3
        AuthenticationFlowModel subflow3 = new AuthenticationFlowModel();
        subflow3.setTopLevel(false);
        subflow3.setBuiltIn(true);
        subflow3.setAlias("subflow-3");
        subflow3.setDescription("alternative subflow with child subflows");
        subflow3.setProviderId("basic-flow");
        subflow3 = realm.addAuthenticationFlow(subflow3);
        execution = new AuthenticationExecutionModel();
        execution.setParentFlow(browser.getId());
        execution.setRequirement(AuthenticationExecutionModel.Requirement.ALTERNATIVE);
        execution.setFlowId(subflow3.getId());
        execution.setPriority(30);
        execution.setAuthenticatorFlow(true);
        realm.addAuthenticatorExecution(execution);
        // Subflow3-1
        AuthenticationFlowModel subflow31 = new AuthenticationFlowModel();
        subflow31.setTopLevel(false);
        subflow31.setBuiltIn(true);
        subflow31.setAlias("subflow-31");
        subflow31.setDescription("subflow-31");
        subflow31.setProviderId("basic-flow");
        subflow31 = realm.addAuthenticationFlow(subflow31);
        execution = new AuthenticationExecutionModel();
        execution.setParentFlow(subflow3.getId());
        execution.setRequirement(AuthenticationExecutionModel.Requirement.ALTERNATIVE);
        execution.setFlowId(subflow31.getId());
        execution.setPriority(10);
        execution.setAuthenticatorFlow(true);
        realm.addAuthenticatorExecution(execution);
        // Subflow3-1 - foo=bar2
        execution = new AuthenticationExecutionModel();
        execution.setParentFlow(subflow31.getId());
        execution.setRequirement(AuthenticationExecutionModel.Requirement.REQUIRED);
        execution.setAuthenticator(ExpectedParamAuthenticatorFactory.PROVIDER_ID);
        execution.setPriority(10);
        execution.setAuthenticatorFlow(false);
        configModel = new AuthenticatorConfigModel();
        configModel.setAlias("bar2");
        config = new HashMap<>();
        config.put(ExpectedParamAuthenticator.EXPECTED_VALUE, "bar2");
        config.put(ExpectedParamAuthenticator.LOGGED_USER, "john-doh@localhost");
        configModel.setConfig(config);
        configModel = realm.addAuthenticatorConfig(configModel);
        execution.setAuthenticatorConfig(configModel.getId());
        realm.addAuthenticatorExecution(execution);
        // Subflow3-1 - push the button
        execution = new AuthenticationExecutionModel();
        execution.setParentFlow(subflow31.getId());
        execution.setRequirement(AuthenticationExecutionModel.Requirement.REQUIRED);
        execution.setAuthenticator(PushButtonAuthenticatorFactory.PROVIDER_ID);
        execution.setPriority(20);
        execution.setAuthenticatorFlow(false);
        realm.addAuthenticatorExecution(execution);
        // Subflow3  - foo=bar3
        execution = new AuthenticationExecutionModel();
        execution.setParentFlow(subflow3.getId());
        execution.setRequirement(AuthenticationExecutionModel.Requirement.ALTERNATIVE);
        execution.setAuthenticator(ExpectedParamAuthenticatorFactory.PROVIDER_ID);
        execution.setPriority(20);
        execution.setAuthenticatorFlow(false);
        configModel = new AuthenticatorConfigModel();
        configModel.setAlias("bar3");
        config = new HashMap<>();
        config.put(ExpectedParamAuthenticator.EXPECTED_VALUE, "bar3");
        config.put(ExpectedParamAuthenticator.LOGGED_USER, "keycloak-user@localhost");
        configModel.setConfig(config);
        configModel = realm.addAuthenticatorConfig(configModel);
        execution.setAuthenticatorConfig(configModel.getId());
        realm.addAuthenticatorExecution(execution);
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) HashMap(java.util.HashMap) AuthenticationExecutionModel(org.keycloak.models.AuthenticationExecutionModel) AuthenticationFlowModel(org.keycloak.models.AuthenticationFlowModel) AuthenticatorConfigModel(org.keycloak.models.AuthenticatorConfigModel) Before(org.junit.Before)

Example 29 with AuthenticationExecutionModel

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

the class AuthenticationManagementResource method newExecutionConfig.

/**
 * Update execution with new configuration
 *
 * @param execution Execution id
 * @param json JSON with new configuration
 * @return
 */
@Path("/executions/{executionId}/config")
@POST
@NoCache
@Consumes(MediaType.APPLICATION_JSON)
public Response newExecutionConfig(@PathParam("executionId") String execution, AuthenticatorConfigRepresentation json) {
    auth.realm().requireManageRealm();
    ReservedCharValidator.validate(json.getAlias());
    AuthenticationExecutionModel model = realm.getAuthenticationExecutionById(execution);
    if (model == null) {
        session.getTransactionManager().setRollbackOnly();
        throw new NotFoundException("Illegal execution");
    }
    AuthenticatorConfigModel config = RepresentationToModel.toModel(json);
    if (config.getAlias() == null) {
        return ErrorResponse.error("Alias missing", Response.Status.BAD_REQUEST);
    }
    config = realm.addAuthenticatorConfig(config);
    model.setAuthenticatorConfig(config.getId());
    realm.updateAuthenticatorExecution(model);
    json.setId(config.getId());
    adminEvent.operation(OperationType.CREATE).resource(ResourceType.AUTH_EXECUTION).resourcePath(session.getContext().getUri()).representation(json).success();
    return Response.created(session.getContext().getUri().getAbsolutePathBuilder().path(config.getId()).build()).build();
}
Also used : AuthenticationExecutionModel(org.keycloak.models.AuthenticationExecutionModel) NotFoundException(javax.ws.rs.NotFoundException) AuthenticatorConfigModel(org.keycloak.models.AuthenticatorConfigModel) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) NoCache(org.jboss.resteasy.annotations.cache.NoCache)

Example 30 with AuthenticationExecutionModel

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

the class AuthenticationManagementResource method addExecution.

/**
 * Add new authentication execution
 *
 * @param execution JSON model describing authentication execution
 */
@Path("/executions")
@POST
@NoCache
@Consumes(MediaType.APPLICATION_JSON)
public Response addExecution(AuthenticationExecutionRepresentation execution) {
    auth.realm().requireManageRealm();
    AuthenticationExecutionModel model = RepresentationToModel.toModel(realm, execution);
    AuthenticationFlowModel parentFlow = getParentFlow(model);
    if (parentFlow.isBuiltIn()) {
        throw new BadRequestException("It is illegal to add execution to a built in flow");
    }
    model.setPriority(getNextPriority(parentFlow));
    model = realm.addAuthenticatorExecution(model);
    adminEvent.operation(OperationType.CREATE).resource(ResourceType.AUTH_EXECUTION).resourcePath(session.getContext().getUri(), model.getId()).representation(execution).success();
    return Response.created(session.getContext().getUri().getAbsolutePathBuilder().path(model.getId()).build()).build();
}
Also used : AuthenticationExecutionModel(org.keycloak.models.AuthenticationExecutionModel) AuthenticationFlowModel(org.keycloak.models.AuthenticationFlowModel) BadRequestException(javax.ws.rs.BadRequestException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) NoCache(org.jboss.resteasy.annotations.cache.NoCache)

Aggregations

AuthenticationExecutionModel (org.keycloak.models.AuthenticationExecutionModel)51 AuthenticationFlowModel (org.keycloak.models.AuthenticationFlowModel)32 AuthenticatorConfigModel (org.keycloak.models.AuthenticatorConfigModel)11 Path (javax.ws.rs.Path)8 NoCache (org.jboss.resteasy.annotations.cache.NoCache)8 HashMap (java.util.HashMap)7 Response (javax.ws.rs.core.Response)7 RealmModel (org.keycloak.models.RealmModel)7 BadRequestException (javax.ws.rs.BadRequestException)6 NotFoundException (javax.ws.rs.NotFoundException)6 POST (javax.ws.rs.POST)6 ArrayList (java.util.ArrayList)5 LinkedList (java.util.LinkedList)5 Consumes (javax.ws.rs.Consumes)5 Before (org.junit.Before)5 ClientModel (org.keycloak.models.ClientModel)4 List (java.util.List)3 UserModel (org.keycloak.models.UserModel)3 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)2 Logger (org.jboss.logging.Logger)2