[keycloak-user] Custom error message in "Authenticator Execution" Script

Felix Gustavsson felix at 0b1.se
Tue Mar 12 17:04:32 EDT 2019


Hi

I'm trying to create a Browser Authentication flow in Keycloak which rejects the user if it does not have the required role, however I am unable to show the user a customized message on a rendered error page. How does one show a custom error message defined in the script?

I've been able to show custom error in JSON using the code below, however I would like it to be rendered using for example the error.ftl template i.e keycloak/themes/src/main/resources/theme/base/login/error.ftl

AuthenticationFlowError = Java.type("org.keycloak.authentication.AuthenticationFlowError");
ErrorRepresentation = Java.type("org.keycloak.representations.idm.ErrorRepresentation");
Response = Java.type("javax.ws.rs.core.Response");
MediaType = Java.type("javax.ws.rs.core.MediaType");

function authenticate(context) {
    /* CODE FOR AUTH CHECK */
    if (authShouldFail) {
        var errorRep = new ErrorRepresentation();
        errorRep.setErrorMessage("You are not authorized to use this application");
        var response = Response.status(401).entity(errorRep).type(MediaType.APPLICATION_JSON_TYPE ).build();
        LOG.info(script.name + " failed auth for: " + username);
        context.failure(AuthenticationFlowError.INVALID_USER, response);
        return;
    }
    context.success();
}


More information about the keycloak-user mailing list