Keycloak extension not getting added

218 Views Asked by At

I have created one keycloak extension to add some validation and required customization in registration form flow of keycloak. It also worked incase of validation I think. But not even showing its log in console. Also I checked for configuring it from registration flows, but I am not able to find any option to configure it.

Here is its code,

public class PlaceholderRegistrationProfile implements FormAction {
    //    protected static final Logger log = Logger.getLogger(PlaceholderRegistrationProfile.class);
    private static final Logger log = Logger.getLogger(PlaceholderRegistrationProfile.class.getName());

    @Override
    public void buildPage(FormContext formContext, LoginFormsProvider form) {
        // complete
        form.setAttribute("passwordRequired", true);
    }

    @Override
    public void validate(ValidationContext context) {
        MultivaluedMap<String, String> formData = context.getHttpRequest().getDecodedFormParameters();
        List<FormMessage> errors = new ArrayList<>();

        context.getEvent().detail(Details.REGISTER_METHOD, "form");
        String eventError = Errors.INVALID_REGISTRATION;

        String email = formData.getFirst(Validation.FIELD_EMAIL);
        boolean emailValid = true;
        if (Validation.isBlank(email)) {
            errors.add(new FormMessage(RegistrationPage.FIELD_EMAIL, Messages.MISSING_EMAIL));
            emailValid = false;
        } else if (!Validation.isEmailValid(email)) {
            context.getEvent().detail(Details.EMAIL, email);
            errors.add(new FormMessage(RegistrationPage.FIELD_EMAIL, Messages.INVALID_EMAIL));
            emailValid = false;
        }

        if (emailValid && !context.getRealm().isDuplicateEmailsAllowed() && context.getSession().users().getUserByEmail(context.getRealm(), email) != null) {
            eventError = Errors.EMAIL_IN_USE;
            formData.remove(Validation.FIELD_EMAIL);
            context.getEvent().detail(Details.EMAIL, email);
            errors.add(new FormMessage(RegistrationPage.FIELD_EMAIL, Messages.EMAIL_EXISTS));
        }

        if (!errors.isEmpty()) {
            context.error(eventError);
            context.validationError(formData, errors);

        } else {
            UserModel user = context.getUser();
            user.setEmail(formData.getFirst(RegistrationPage.FIELD_EMAIL));
            log.info("EMAIL ADDED");
            String role = formData.getFirst("user.attributes.type");
            var x = context.getRealm().getRolesStream()
                    .filter(roleModel -> roleModel.getName().equalsIgnoreCase(role)).findFirst();
            x.ifPresent(user::grantRole);
            context.setUser(user);
            log.info("Form validated");
            context.success();
        }
    }

    @Override
    public void success(FormContext context) {
        log.info("FORM SUCCESS EVENT");
        UserModel user = context.getUser();
        MultivaluedMap<String, String> formData = context.getHttpRequest().getDecodedFormParameters();
//        user.setFirstName(formData.getFirst(RegistrationPage.FIELD_FIRST_NAME));
//        user.setLastName(formData.getFirst(RegistrationPage.FIELD_LAST_NAME));
        user.setEmail(formData.getFirst(RegistrationPage.FIELD_EMAIL));
        log.info("EMAIL ADDED");
        String role = formData.getFirst("user.attributes.type");
        var x = context.getRealm().getRolesStream()
                .filter(roleModel -> roleModel.getName().equalsIgnoreCase(role)).findFirst();
        x.ifPresent(user::grantRole);

//        user.setSingleAttribute("type", role);
//        x.ifPresent(roleModel -> user.getRealmRoleMappingsStream().toList().add(roleModel));
//        context.setUser(user);
    }

    @Override
    public boolean requiresUser() {
        return false;
    }

    @Override
    public boolean configuredFor(KeycloakSession keycloakSession, RealmModel realmModel, UserModel userModel) {
        return true;
    }

    @Override
    public void setRequiredActions(KeycloakSession keycloakSession, RealmModel realmModel, UserModel userModel) {
        // TODO document why this method is empty
    }

    @Override
    public void close() {
        // TODO document why this method is empty
    }

image of console

image of keycloak showing provider

I want it to at least show logs in console and get to know is it added to registration flows. Also if there is any way, to add it in registration it would be really helpful. For now, I have added its jar in providers folder, and it showed like above image.

Note: I am using quay.io/keycloak/keycloak:21.1.1 keycloak docker image.

0

There are 0 best solutions below