wip: compiles and doesn't break startup

This commit is contained in:
knoflook 2023-12-08 16:07:46 +01:00
parent 86cfd9cf57
commit cd738a4a6f
3 changed files with 71 additions and 6 deletions

View File

@ -60,6 +60,12 @@
<scope>provided</scope>
<version>${keycloak.version}</version>
</dependency>
<dependency>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service</artifactId>
<version>1.0</version>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>

View File

@ -12,10 +12,12 @@ import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
import org.keycloak.models.UserModel;
import org.keycloak.models.utils.FormMessage;
import org.keycloak.provider.ProviderConfigProperty;
import org.keycloak.services.messages.Messages;
import org.keycloak.services.validation.Validation;
import jakarta.ws.rs.core.MultivaluedMap;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
//
@ -26,7 +28,10 @@ import org.keycloak.events.Errors;
import org.keycloak.forms.login.LoginFormsProvider;
import org.keycloak.models.utils.FormMessage;
public abstract class RegistrationProfileDomainValidation extends RegistrationUserCreation {
import com.google.auto.service.AutoService;
@AutoService(FormActionFactory.class)
public class RegistrationProfileDomainValidation extends RegistrationUserCreation {
protected static final Logger logger = Logger.getLogger(RegistrationProfileDomainValidation.class);
protected static final String DEFAULT_DOMAIN_LIST = "example.org";
@ -108,8 +113,62 @@ public abstract class RegistrationProfileDomainValidation extends RegistrationUs
}
}
public abstract String[] getDomainList(AuthenticatorConfigModel mailDomainConfig);
public String[] getDomainList(AuthenticatorConfigModel mailDomainConfig) {
return mailDomainConfig.getConfig().getOrDefault(domainListConfigName, DEFAULT_DOMAIN_LIST).split(DOMAIN_LIST_SEPARATOR);
}
public boolean isEmailValid(String email, String[] domains) {
for (String domain : domains) {
if (email.endsWith("@" + domain) || email.equals(domain) || globmatches(email, "*@" + domain)) {
return true;
}
}
return false;
}
public static final String PROVIDER_ID = "registration-mail-check-action";
private static final List<ProviderConfigProperty> CONFIG_PROPERTIES = new ArrayList<>();
public static String domainListConfigName = "validDomains";
static {
ProviderConfigProperty property;
property = new ProviderConfigProperty();
property.setName(domainListConfigName);
property.setLabel("Valid domains for emails");
property.setType(ProviderConfigProperty.STRING_TYPE);
property.setHelpText("List mail domains authorized to register, separated by '##'");
CONFIG_PROPERTIES.add(property);
}
@Override
public String getDisplayType() {
return "Profile Validation with email domain check";
}
@Override
public String getId() {
return PROVIDER_ID;
}
@Override
public String getHelpText() {
return "Adds validation of domain emails for registration";
}
@Override
public List<ProviderConfigProperty> getConfigProperties() {
return CONFIG_PROPERTIES;
}
@Override
public void buildPage(FormContext context, LoginFormsProvider form) {
List<String> authorizedMailDomains = Arrays.asList(
context.getAuthenticatorConfig().getConfig().getOrDefault(domainListConfigName,DEFAULT_DOMAIN_LIST).split(DOMAIN_LIST_SEPARATOR));
form.setAttribute("authorizedMailDomains", authorizedMailDomains);
}
public abstract boolean isEmailValid(String email, String[] domains);
}

View File

@ -1,3 +1,3 @@
com.thomasdarimont.keycloak.auth.CustomRegistrationUserCreation
com.thomasdarimont.keycloak.auth.RegistrationProfileWithDomainBlock
com.thomasdarimont.keycloak.auth.RegistrationProfileWithMailDomainCheck
com.github.thomasdarimont.keycloak.auth.RegistrationProfileDomainValidation
com.github.thomasdarimont.keycloak.auth.RegistrationProfileWithDomainBlock
com.github.thomasdarimont.keycloak.auth.RegistrationProfileWithMailDomainCheck