send_email_through_keyclock
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| send_email_through_keyclock [2026/03/09 07:57] – removed sonali | send_email_through_keyclock [2026/03/09 08:00] (current) – sonali | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | < | ||
| + | |||
| + | A system where users can log in using their **email Id /Username + Email OTP** instead of username/ | ||
| + | |||
| + | **Make Email OTP Java SPI ** | ||
| + | |||
| + | # Run this to find the Keycloak container: | ||
| + | |||
| + | <code bash> | ||
| + | docker ps | grep -i keycloak | ||
| + | |||
| + | Result will look like | ||
| + | 2550aa1a95b7 | ||
| + | fcca38958118 | ||
| + | |||
| + | |||
| + | </ | ||
| + | |||
| + | Run this in the droplet console to find the exact JAR download URL: | ||
| + | < | ||
| + | |||
| + | curl -s https:// | ||
| + | |||
| + | </ | ||
| + | |||
| + | We need email-otp-authenticator JAR if it is not available | ||
| + | |||
| + | **1. Download the email-otp-authenticator JAR** | ||
| + | < | ||
| + | |||
| + | curl -L -o email-otp-authenticator.jar https:// | ||
| + | |||
| + | </ | ||
| + | |||
| + | **2. Copy into the running container** | ||
| + | < | ||
| + | docker cp email-otp-authenticator.jar keycloak_app:/ | ||
| + | |||
| + | </ | ||
| + | |||
| + | **3. Run build inside the container (registers the provider)** | ||
| + | < | ||
| + | # Verify it's there | ||
| + | docker exec keycloak_app ls / | ||
| + | |||
| + | </ | ||
| + | |||
| + | < | ||
| + | docker exec keycloak_app / | ||
| + | |||
| + | </ | ||
| + | |||
| + | **4. Restart the container** | ||
| + | < | ||
| + | docker restart keycloak_app | ||
| + | |||
| + | </ | ||
| + | |||
| + | # Now let's set up the Email OTP flow. Go to Keycloak Admin Console at [[https:// | ||
| + | |||
| + | **1. First configure SMTP (if not already done)** | ||
| + | |||
| + | Realm Settings → Email | ||
| + | |||
| + | Host: smtp.gmail.com, | ||
| + | |||
| + | Port: 587 | ||
| + | |||
| + | From: from email id | ||
| + | |||
| + | Username: your username, | ||
| + | |||
| + | Password: your app | ||
| + | |||
| + | password Enable StartTLS → Save → Test connection | ||
| + | |||
| + | **2. Create Email OTP Authentication Flow** | ||
| + | < | ||
| + | |||
| + | Go to Authentication → Flows → Create flow Name: Browser Email OTP | ||
| + | → Save Add step → Username Password Form → Required Add step → | ||
| + | Email OTP → Required | ||
| + | |||
| + | </ | ||
| + | |||
| + | **3. Bind the flow** | ||
| + | |||
| + | Client → account → Advance Override realm authentication flow bindings. →Browser Flow → Browser email otp | ||
| + | |||
| + | **Customize email content** | ||
| + | < | ||
| + | python3 -c " | ||
| + | |||
| + | </ | ||
| + | |||
| + | # check current email template | ||
| + | |||
| + | < | ||
| + | python3 -c " | ||
| + | import zipfile | ||
| + | with zipfile.ZipFile(' | ||
| + | print(z.read(' | ||
| + | " | ||
| + | |||
| + | </ | ||
| + | |||
| + | # To customize the email text, create a custom Keycloak theme. Run these commands on the droplet: | ||
| + | |||
| + | Step 1: Create theme directory \structure | ||
| + | |||
| + | < | ||
| + | docker exec keycloak_app mkdir -p / | ||
| + | |||
| + | </ | ||
| + | |||
| + | Step 2: Create theme.\properties | ||
| + | |||
| + | < | ||
| + | docker exec keycloak_app sh -c ' | ||
| + | parent=\base | ||
| + | EOF' | ||
| + | |||
| + | </ | ||
| + | |||
| + | Step 3: Create custom messages (edit the text as you like) | ||
| + | |||
| + | < | ||
| + | docker exec keycloak_app sh -c ' | ||
| + | emailOtpSubject=Your Cotrav OTP \Code | ||
| + | emailOtpYourAccessCode=Your one-time login code is: | ||
| + | emailOtpExpiration=This code will expire in {0} minutes. Do not share it with anyone. | ||
| + | EOF' | ||
| + | |||
| + | </ | ||
| + | |||
| + | Step 4: Set realm to use this \theme | ||
| + | |||
| + | < | ||
| + | TOKEN=$(curl -s -X POST http:// | ||
| + | -d " | ||
| + | | grep -o '" | ||
| + | |||
| + | curl -s -X PUT http:// | ||
| + | -H " | ||
| + | -H " | ||
| + | -d ' | ||
| + | |||
| + | </ | ||
| + | |||
| + | Then test by logging in again — you should see your custom text in the OTP email. | ||
| + | |||
| + | # Invalid otp issue\\ | ||
| + | The OTP field name sent by our server might not match what the extension expects. Let me check: | ||
| + | < | ||
| + | |||
| + | python3 -c "\ | ||
| + | import zipfile\ | ||
| + | with zipfile.ZipFile(' | ||
| + | print(z.read(' | ||
| + | " | ||
| + | |||
| + | </ | ||
| + | |||
| + | **# Browser email otp Flow order should be** \\ | ||
| + | Username Form → Required (first)\\ | ||
| + | Email OTP Form → Required (second) | ||
| + | |||
| + | **# Dont do this** | ||
| + | |||
| + | - No required user action available in user details\\ | ||
| + | - Set Email & password | ||
| + | |||
send_email_through_keyclock.1773043029.txt.gz · Last modified: by sonali
