2023-12-08 14:23:25 +00:00
|
|
|
package com.github.thomasdarimont.keycloak.auth;
|
2023-04-17 18:29:02 +00:00
|
|
|
|
|
|
|
import org.keycloak.authentication.FormContext;
|
|
|
|
import org.keycloak.forms.login.LoginFormsProvider;
|
|
|
|
import org.keycloak.provider.ProviderConfigProperty;
|
2023-09-14 23:41:35 +00:00
|
|
|
import org.keycloak.models.AuthenticatorConfigModel;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
2023-04-17 18:29:02 +00:00
|
|
|
|
2023-04-19 01:29:40 +00:00
|
|
|
public class RegistrationProfileWithDomainBlock extends RegistrationProfileDomainValidation {
|
2023-04-17 18:29:02 +00:00
|
|
|
|
|
|
|
public static final String PROVIDER_ID = "registration-domain-block-action";
|
2023-09-14 23:41:35 +00:00
|
|
|
|
2023-04-19 01:29:40 +00:00
|
|
|
private static final List<ProviderConfigProperty> CONFIG_PROPERTIES = new ArrayList<>();
|
|
|
|
|
2023-09-14 23:41:35 +00:00
|
|
|
public static String domainListConfigName = "invalidDomains";
|
2023-04-19 01:29:40 +00:00
|
|
|
|
2023-09-14 23:41:35 +00:00
|
|
|
static {
|
2023-04-19 01:29:40 +00:00
|
|
|
ProviderConfigProperty property;
|
|
|
|
property = new ProviderConfigProperty();
|
|
|
|
property.setName(domainListConfigName);
|
|
|
|
property.setLabel("Invalid domain for emails");
|
|
|
|
property.setType(ProviderConfigProperty.STRING_TYPE);
|
|
|
|
property.setHelpText("List mail domains not authorized to register, separated by '##'");
|
|
|
|
CONFIG_PROPERTIES.add(property);
|
|
|
|
}
|
2023-04-17 18:29:02 +00:00
|
|
|
|
|
|
|
@Override
|
2023-09-14 23:41:35 +00:00
|
|
|
public String getDisplayType() {
|
2023-04-17 18:29:02 +00:00
|
|
|
return "Profile Validation with domain block";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getId() {
|
|
|
|
return PROVIDER_ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getHelpText() {
|
|
|
|
return "Adds validation of not accepted domain emails for registration";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<ProviderConfigProperty> getConfigProperties() {
|
|
|
|
return CONFIG_PROPERTIES;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-04-19 01:29:40 +00:00
|
|
|
public void buildPage(FormContext context, LoginFormsProvider form) {
|
|
|
|
List<String> unauthorizedMailDomains = Arrays.asList(
|
|
|
|
context.getAuthenticatorConfig().getConfig().getOrDefault(domainListConfigName, DEFAULT_DOMAIN_LIST).split(DOMAIN_LIST_SEPARATOR));
|
|
|
|
form.setAttribute("unauthorizedMailDomains", unauthorizedMailDomains);
|
|
|
|
}
|
2023-04-17 18:29:02 +00:00
|
|
|
|
2023-09-14 23:41:35 +00:00
|
|
|
@Override
|
|
|
|
public String[] getDomainList(AuthenticatorConfigModel mailDomainConfig) {
|
|
|
|
return mailDomainConfig.getConfig().getOrDefault(domainListConfigName, DEFAULT_DOMAIN_LIST).split(DOMAIN_LIST_SEPARATOR);
|
|
|
|
}
|
|
|
|
|
2023-04-19 01:29:40 +00:00
|
|
|
@Override
|
|
|
|
public boolean isEmailValid(String email, String[] domains) {
|
2023-04-17 18:29:02 +00:00
|
|
|
for (String domain : domains) {
|
2023-04-19 01:29:40 +00:00
|
|
|
if (email.endsWith("@" + domain) || email.equals(domain) || globmatches(email, "*@" + domain)) {
|
|
|
|
return false;
|
2023-04-17 18:29:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-19 01:29:40 +00:00
|
|
|
return true;
|
2023-04-17 18:29:02 +00:00
|
|
|
}
|
|
|
|
}
|