認証モジュールの流れ

認証モジュール本体
IpdAccountExistsAuthenticator.java

     package local.example;

     import org.jboss.logging.Logger;
     import org.keycloak.authentication.AuthenticationFlowContext;
     import org.keycloak.authentication.AuthenticationFlowError;
     import org.keycloak.authentication.AuthenticationFlowException;
     import org.keycloak.authentication.authenticators.broker.AbstractIdpAuthenticator;
     import org.keycloak.authentication.authenticators.broker.util.SerializedBrokeredIdentityContext;
     import org.keycloak.broker.provider.BrokeredIdentityContext;
     import org.keycloak.models.KeycloakSession;
     import org.keycloak.models.RealmModel;
     import org.keycloak.models.UserModel;

     import java.util.Objects;

    public class IpdAccountExistsAuthenticator extends AbstractIdpAuthenticator {

    private static Logger logger = Logger.getLogger(IpdAccountExistsAuthenticator.class);

    /**
     * 認証処理
     *
     * @param context
     * @param serializedCtx
     * @param brokerContext
     */
    @Override
    protected void authenticateImpl(AuthenticationFlowContext context, SerializedBrokeredIdentityContext serializedCtx, BrokeredIdentityContext brokerContext) {

        logger.info("Start IpdAccountExistsAuthenticator");

        // ユーザがKeycloakに登録済みであればOK
        UserModel user = context.getSession().users().getUserByUsername(brokerContext.getEmail(), context.getRealm());

        if (Objects.isNull(user)) {
            // ユーザが取れなければ認証失敗にする
            throw new AuthenticationFlowException("そんなユーザいません : " + brokerContext.getEmail(), AuthenticationFlowError.UNKNOWN_USER);
        }

        user.setEnabled(true);
        user.setEmail(brokerContext.getEmail());
        user.setFirstName(brokerContext.getFirstName());
        user.setLastName(brokerContext.getLastName());

        logger.info("認証OK : " + user.getUsername());

        // 認証成功
        context.setUser(user);
        context.getAuthenticationSession().setAuthNote(BROKER_REGISTERED_NEW_USER, "true");
        context.getAuthenticationSession().setAuthenticatedUser(user);
        context.success();
    }

    @Override
    protected void actionImpl(AuthenticationFlowContext context, SerializedBrokeredIdentityContext
            serializedCtx, BrokeredIdentityContext brokerContext) {

    }

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

    @Override
    public boolean configuredFor(KeycloakSession session, RealmModel realm, UserModel user) {
        return false;
     }
     }

ファクトリー
IpdAccountExistsAuthenticatorFactory.java

    package local.example;

    import org.keycloak.authentication.Authenticator;
    import org.keycloak.authentication.AuthenticatorFactory;
    import org.keycloak.models.AuthenticationExecutionModel;
    import org.keycloak.models.KeycloakSession;
    import org.keycloak.models.KeycloakSessionFactory;
    import org.keycloak.provider.ProviderConfigProperty;

    import java.util.List;

    public class IpdAccountExistsAuthenticatorFactory implements AuthenticatorFactory {

    public static final String ID = "exists-account-from-keycloak";
    public static final AuthenticationExecutionModel.Requirement[] REQUIREMENT_CHOICES = {
            AuthenticationExecutionModel.Requirement.ALTERNATIVE,
            AuthenticationExecutionModel.Requirement.REQUIRED,
            AuthenticationExecutionModel.Requirement.DISABLED};

    @Override
    public Authenticator create(KeycloakSession keycloakSession) {
        return new IpdAccountExistsAuthenticator();
    }

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

    }

    @Override
    public void postInit(KeycloakSessionFactory keycloakSessionFactory) {

    }

    @Override
    public void close() {

    }

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

    @Override
    public String getReferenceCategory() {
        return "exists-account";
    }

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

    @Override
    public AuthenticationExecutionModel.Requirement[] getRequirementChoices() {
        return REQUIREMENT_CHOICES;
    }

    @Override
    public String getDisplayType() {
        return ID;
    }

    @Override
    public String getHelpText() {
        return "ipd exists accoount";
    }

    public boolean isUserSetupAllowed() {
        return true;
    }

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

クラス登録用定義ファイル
org.keycloak.authentication.AuthenticatorFactory

     local.example.IpdAccountExistsAuthenticatorFactory