Add the list of authorized mails in registration forms

This work by adding the `authorizedMailDomains` attribute in the form before displaying it.
This commit is contained in:
Cédric Couralet 2021-04-14 22:10:46 +02:00
parent e3cf32d0d6
commit ac8ab8b623
2 changed files with 24 additions and 0 deletions

View File

@ -17,3 +17,15 @@ Simply drop the jar in `$KEYCLOAK_HOME\standalone\deployments`, it will be autom
- Configure this new execution (otherwise, keycloak will only accept "exemple.org" domains) - Configure this new execution (otherwise, keycloak will only accept "exemple.org" domains)
- Change the registration binding to this new flow - Change the registration binding to this new flow
- Configure the realm to accept registration and verify email (this is important!) - Configure the realm to accept registration and verify email (this is important!)
## Display authorized mail domains in register forms
This extension provides the list of authorized patterns in the `authorizedMailDomains` attribute of the registration page.
This can be used like this :
```
<div class="${properties.kcLabelWrapperClass!}">
<label for="email" class="${properties.kcLabelClass!}">${msg("email")} (only ${authorizedMailDomains?join(", ")})</label>
</div>
```

View File

@ -1,16 +1,19 @@
package net.micedre.keycloak.registration; package net.micedre.keycloak.registration;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.MultivaluedMap;
import org.keycloak.authentication.FormAction; import org.keycloak.authentication.FormAction;
import org.keycloak.authentication.FormContext;
import org.keycloak.authentication.ValidationContext; import org.keycloak.authentication.ValidationContext;
import org.keycloak.authentication.forms.RegistrationPage; import org.keycloak.authentication.forms.RegistrationPage;
import org.keycloak.authentication.forms.RegistrationProfile; import org.keycloak.authentication.forms.RegistrationProfile;
import org.keycloak.events.Details; import org.keycloak.events.Details;
import org.keycloak.events.Errors; import org.keycloak.events.Errors;
import org.keycloak.forms.login.LoginFormsProvider;
import org.keycloak.models.AuthenticatorConfigModel; import org.keycloak.models.AuthenticatorConfigModel;
import org.keycloak.models.utils.FormMessage; import org.keycloak.models.utils.FormMessage;
import org.keycloak.provider.ProviderConfigProperty; import org.keycloak.provider.ProviderConfigProperty;
@ -55,6 +58,7 @@ public class RegistrationProfileWithMailDomainCheck extends RegistrationProfile
CONFIG_PROPERTIES.add(property); CONFIG_PROPERTIES.add(property);
} }
private static final boolean globmatches(String text, String glob) { private static final boolean globmatches(String text, String glob) {
if (text.length() > 200) { if (text.length() > 200) {
return false; return false;
@ -136,4 +140,12 @@ public class RegistrationProfileWithMailDomainCheck extends RegistrationProfile
} }
@Override
public void buildPage(FormContext context, LoginFormsProvider form) {
List<String> authorizedMailDomains = Arrays.asList(
context.getAuthenticatorConfig().getConfig().getOrDefault("validDomains","exemple.org").split("##"));
form.setAttribute("authorizedMailDomains", authorizedMailDomains);
}
} }