Using a custom authentication class in keycloak instead of AbstractUsernameFormAuthenticator

226 views Asked by At

So I'm trying to use a custom class which will override the functionality of the

AbstractUsernameFormAuthenticator 

class already used in the keycloak.

1- I created a custom provider 2- Generated a jar 3- Added the jar to the provider directory in keycloak.

I'm using keycloak version 22.0.4

So what am I missing?

Below is the class with overridden method

public class CustomAuthenticator extends UsernamePasswordForm {

    @Override
    protected String disabledByBruteForceError() {
        return Messages.ACCOUNT_TEMPORARILY_DISABLED;
    }
}

And this is the CustomAuthenticatorFactory

public class CustomAuthenticatorFactory implements AuthenticatorFactory, ConfigurableAuthenticatorFactory {

    public static final String PROVIDER_ID = "custom-authenticator";
    private static final CustomAuthenticator SINGLETON = new CustomAuthenticator();

    @Override
    public String getId() {
        return PROVIDER_ID;
    }

    @Override
    public String getDisplayType() {
        return "Custom Authenticator";
    }

    @Override
    public String getReferenceCategory() {
        return null;
    }

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

    @Override
    public AuthenticationExecutionModel.Requirement[] getRequirementChoices() {
        return Arrays.asList(AuthenticationExecutionModel.Requirement.REQUIRED).toArray(new AuthenticationExecutionModel.Requirement[0]);
    }

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

    @Override
    public String getHelpText() {
        return null;
    }

    @Override
    public List<ProviderConfigProperty> getConfigProperties() {
        return null;
    }

    @Override
    public void init(Config.Scope scope) {

    }

    @Override
    public void postInit(KeycloakSessionFactory keycloakSessionFactory) {

    }

    @Override
    public void close() {

    }

    @Override
    public Authenticator create(KeycloakSession session) {
        return SINGLETON;
    }
}

This is the content of org.keycloak.authentication.AuthenticatorFactory in the resources/META-INF/services

com.identicum.keycloak.CustomAuthenticatorFactory

And finally the jboss-deployment-structure

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
    <deployment>
        <module-alias name="deployment.keycloak-custom-authenticator"/>
        <dependencies>
            <module name="org.keycloak.keycloak-core"/>
            <module name="org.keycloak.keycloak-server-spi"/>
            <module name="org.keycloak.keycloak-server-spi-private"/>
            <module name="org.keycloak.keycloak-services"/>
        </dependencies>
    </deployment>
</jboss-deployment-structure>

1- I created a custom provider 2- Generated a jar 3- Added the jar to the provider directory in keycloak.

I'm using keycloak version 22.0.4

1

There are 1 answers

0
omar amer On

After some trials, I was finally able to do it. All you need to do is: 1- Add the compiled jar to the providers directory in your keycloak installation. 2- Duplicate the login flow and add an custom authentication step to the duplicated flow. 3- Bind the flow so that it is the main flow in use.