4.1 KiB
4.1 KiB
Enforce OTP (TOTP) 2FA for Internal Users in Keycloak
Goal: Enforce OTP-based two-factor authentication (2FA) for internal users managed inside Keycloak in the
Digitalboardrealm, while external users coming from Microsoft Entra ID as Identity Provider are not prompted for OTP.
Prerequisites
- Admin access to the Keycloak Admin Console for the
Digitalboardrealm. - A running Keycloak instance (TLS termination handled by Traefik or Keycloak itself).
- Microsoft Entra ID already set up as an Identity Provider for the realm.
- Internal users stored directly in Keycloak (username/password authentication).
Steps
Step 1 — Verify External Identity Provider
- In the Digitalboard realm, open the Admin Console.
- Navigate to Identity Providers.
- Confirm that Microsoft Entra ID is configured (OpenID Connect or SAML).
- Ensure that external users sign in via this IdP.
Step 2 — Configure OTP Policy
- In the left-hand menu of the Digitalboard realm, go to Authentication → OTP Policy (or Realm Settings → Security Defenses → OTP Policy, depending on Keycloak version).
- Configure the following:
- Type:
totp(time based) - Digits:
6 - Period:
30seconds - Algorithm:
sha512 - Look ahead window:
1-2 - Reusable token:
off
- Type:
Click Save.
Step 3 — Copy the Browser Flow
- Navigate to Authentication → Flows.
- Locate the built-in Browser flow.
- Click Copy.
- Enter the new name:
browser-internal-otp.
This custom flow will handle internal (local) user logins for the Digitalboard realm.
Step 4 — Add OTP Requirement
- Open the
browser-internal-otpflow. - Inside the Browser Forms sub-flow, locate the Username Password Form.
- After it, click Add execution → select OTP Form (or Conditional OTP if available).
- Set requirement to Required.
This ensures OTP is enforced for all username/password logins (internal users).
Step 5 — Enable “Configure OTP” Required Action
- In the Digitalboard realm, go to Authentication → Required Actions.
- Locate Configure OTP.
- Ensure the status is Enabled.
With this enabled, internal users without an OTP configured will be prompted to set it up during login.
Step 6 — Bind the Flow
- In the Digitalboard realm, go to Authentication → Flows.
- Click the flow browser-internal-otp to open it.
- In the top-right, open Actions → Bind flow.
- In the dialog, choose Browser flow and click Save.
This makes
browser-internal-otpthe default Browser flow, so internal (local) users who log in with username/password must use OTP.
Important for external (Entra) users:
- Go to Identity Providers → (Microsoft Entra) and ensure Post Login Flow / Post Broker Login Flow is None (or a flow without OTP), so external users don't get a Keycloak OTP prompt after IdP login.
Step 7 — Test the Setup
- Log in with an internal Keycloak user in the
Digitalboardrealm:- After entering username and password, you should be prompted for OTP (or OTP setup if not already configured).
- Log in with an external Microsoft Entra user:
- You should be redirected to Entra ID for login.
- After successful login, you should be signed in without an additional OTP prompt from Keycloak.
Troubleshooting
- External users see OTP prompt: Check that Post Broker Login Flow is not set to
browser-internal-otp. - Internal users not asked for OTP: Verify that the OTP Form is present in
browser-internal-otpand set to Required. - Looping on OTP setup: Ensure realm time synchronization (NTP) and correct OTP policy values.
Next Steps
- Roll out OTP to internal users gradually (pilot group).
- Provide instructions for enrolling an authenticator app (Google Authenticator, Microsoft Authenticator, FreeOTP).
- Monitor login events in the
Digitalboardrealm to confirm expected OTP usage.