001/*
002 * This file is part of the Keycloak Moodle authenticator
003 * Copyright (C) 2024 Michael N. Lipp
004 *
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Lesser General Public License as published
007 * by the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
013 * License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jdrupes.keycloak.moodleauth;
020
021import java.util.List;
022import org.keycloak.Config.Scope;
023import org.keycloak.authentication.Authenticator;
024import org.keycloak.authentication.AuthenticatorFactory;
025import org.keycloak.models.AuthenticationExecutionModel.Requirement;
026import org.keycloak.models.KeycloakSession;
027import org.keycloak.models.KeycloakSessionFactory;
028import org.keycloak.provider.ProviderConfigProperty;
029
030public class MoodleAuthenticatorFactory implements AuthenticatorFactory {
031
032    /* default */ static final String MOODLE_URL = "moodleUrl";
033    private static final String TYPE = "delegator";
034    private static final String PROVIDER_ID = "org.jdrupes.keycloak.moodleauth";
035    private static final MoodleAuthenticator SINGLETON
036        = new MoodleAuthenticator();
037
038    @Override
039    public Authenticator create(KeycloakSession session) {
040        return SINGLETON;
041    }
042
043    @Override
044    public void init(Scope config) {
045    }
046
047    @Override
048    public void postInit(KeycloakSessionFactory factory) {
049    }
050
051    @Override
052    public void close() {
053    }
054
055    @Override
056    public String getId() {
057        return PROVIDER_ID;
058    }
059
060    @Override
061    public String getReferenceCategory() {
062        return TYPE;
063    }
064
065    @Override
066    public boolean isConfigurable() {
067        return true;
068    }
069
070    @Override
071    public String getDisplayType() {
072        return "Moodle Username Password Form";
073    }
074
075    @Override
076    public Requirement[] getRequirementChoices() {
077        return new Requirement[] { Requirement.ALTERNATIVE,
078            Requirement.REQUIRED, Requirement.DISABLED };
079    }
080
081    /**
082     * Checks if is user setup allowed (it isn't for this provider).
083     *
084     * @return false
085     */
086    @Override
087    public boolean isUserSetupAllowed() {
088        return false;
089    }
090
091    @Override
092    public String getHelpText() {
093        return "Validates a username and password against a moodle instance.";
094    }
095
096    @Override
097    public List<ProviderConfigProperty> getConfigProperties() {
098        return List.of(new ProviderConfigProperty(MOODLE_URL, "Moodle URL",
099            "The URL of the Moodle instance",
100            ProviderConfigProperty.STRING_TYPE, ""));
101    }
102
103}