Integrations · Sign-up and auth
Stop bad emails at Keycloak sign-up
A Keycloak provider that checks the email address of every new self-registration and stops the registrations whose address you do not want: by default addresses that cannot receive mail (invalid) and throwaway inboxes (disposable). It is a step of your realm's registration form, and it runs before Keycloak creates the user. If the check cannot be made, because our API is slow, unreachable or answers with an error, the registration goes through as if the step were not there.
It is one jar with no dependencies of its own, for Keycloak 26 (built for Java 17 or later). Download it: email-validation-keycloak-1.0.0.jar (MIT licence, SHA-256 7eb14141186a5a3db33c8eefae6394aacf16a044d4bf28e0d0307e4ae14e1e26).
We ran it on Keycloak 26.8.2 against a local stand-in of our API (no call reached our real API): registrations through the login page's form with test addresses were stopped and let through as this page describes, and with the stand-in out of reach the registration went through. That run set up the realm and the flow through Keycloak's Admin REST API; the console steps below follow Keycloak's documentation.
What gets checked
Registrations on Keycloak's login page (the Register link), also when the email is the username. Not checked: users an admin creates (admin console, Admin REST API, realm import), the first login through an identity provider such as Google, users from LDAP or another user federation, and a later change of address in the account console.
Set it up
- Create an API key on the API keys page with the permission Validate addresses and a daily credit cap.
- Copy the jar into Keycloak's
providers/folder (in a container image:COPYit to/opt/keycloak/providers/beforekc.sh build), runbin/kc.sh buildand restart Keycloak. - In the admin console, open your realm: Realm settings → Login, turn on User registration.
- Authentication → Flows: duplicate the registration flow (three dots → Duplicate). In the copy, add a step to its registration form, pick email-validation.org address check and set it to Required.
- Open the step's settings (the gear icon): give it an alias and paste your API key. Save.
- Action → Bind flow, for registration.
- Try it while the realm's Verify email switch is off (Realm settings → Login → Email settings; it is off unless someone turned it on) and Verify Email is not a default action (Authentication → Required actions, the column Set as default action): a test address receives no mail, so Keycloak's verification email to it would bounce, which hurts the reputation of whoever sends it. Register with
no_mailbox@sandbox.email-validation.org: the registration is stopped and no user is created. Withaccepted@sandbox.email-validation.org, while Verify email is still off and not a default action, the registration goes through; delete that user afterwards under Users. Test addresses return the result their name says, cost nothing and contact no mail server.
The same setup works through Keycloak's Admin REST API: the step's provider id is email-validation-org, and its settings are the config keys in the table below.
Settings
| Field (config key) | Value |
|---|---|
API key (ev.api.key) | your key, or a vault reference such as ${vault.evApiKey} (see below) |
Region (ev.region) | us, or eu to keep checks in the EU (EU data residency); same keys and credits |
Statuses that stop a registration (ev.stop.statuses) | any of valid, invalid, catch_all, unknown, disposable; nothing chosen means invalid and disposable |
Log only (ev.log.only) | on: results are only written to Keycloak's log and nothing is stopped, to try it first |
Message (ev.deny.message) | optional: your own text under the email field, or a key of your login theme's messages (translated, {0} = the suggested address) |
Your key: Keycloak keeps the field in its database as it is. From Keycloak 26.8 on, the admin console and the Admin REST API show it masked; on earlier 26 releases every admin who can view the realm can read it there. To keep it out of the database and out of sight, put it in Keycloak's vault and write ${vault.evApiKey} in the field: with the file vault (kc.sh build --vault=file, --vault-dir), the key is the file <realm>_evApiKey.
What the person registering sees
The form comes back with what they typed and a message under the email field: for an address that cannot receive mail, "This email address cannot receive email. Please check it or use another address." (with "Did you mean …?" when we spot a typo); for a disposable one, "Please register with a permanent email address, not a disposable one."; for the other statuses you chose, "Please register with a different email address."
When the check cannot be made
The registration waits at most 6 seconds for our answer, then goes on. Any error (a wrong key, no credits left, a rate limit, our API down) lets the registration through and writes one line to Keycloak's log, category org.email_validation.keycloak. After a rate limit or a "service unavailable" answer (HTTP 429, except a key's daily credit cap, or 503), no check is sent from that Keycloak server for the time our API asked for: 30 seconds if it names none, and at most 5 minutes. A check is never retried. unknown results caused by our side (reason timeout or internal_error) never stop a registration.
Costs
Each submitted registration form is one check, also when the person corrects another field and submits again, and also when a captcha step on the same form fails. unknown results and test addresses are free. A daily credit cap on the key limits what a burst of fake registrations can cost.
Privacy
Only the address is sent, to the host of the region you chose. The step's log lines, and the three details it adds to Keycloak's registration event (named ev_status, ev_reason and ev_request_id), hold the status, the reason and our request id, never the address or the key. Keycloak's own registration events record the address, as they do without this step.
Remove it
Bind the realm's registration back to the built-in registration flow, delete the jar from providers/, then run bin/kc.sh build and restart Keycloak.