8000 Emit error event when OTP setup code is invalid by tagliala · Pull Request #55640 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

Emit error event when OTP setup code is invalid - #55640

Draft
tagliala wants to merge 1 commit into
keycloak:mainfrom
tagliala:totp-setup-error-event-main
Draft

tagliala wants to merge 1 commit into
keycloak:mainfrom
tagliala:totp-setup-error-event-main

Conversation

@tagliala
@tagliala tagliala commented Nov 27, 2026 •
Copy link
Copy Markdown
Contributor

Closes #55639

Problem

The Configure OTP required action (UpdateTotp) shows "Invalid authenticator code." when the submitted code does not match the new secret, but it emits no event. A failed OTP setup leaves no trace in the event store or in event listeners. Update Password and WebAuthn Register emit an error event when they reject the submitted credential. Without it, an administrator cannot tell whether a user who fails to complete the setup entered a wrong code or never submitted one.

Change

UpdateTotp.processAction already sets the event type to UPDATE_CREDENTIAL. On the two branches that reject the code, it now calls:

event.error(Errors.INVALID_USER_CREDENTIALS);

EventBuilder.error appends _ERROR to the type, so this stores UPDATE_CREDENTIAL_ERROR. It carries credential_type=otp and the user, client and login details already on the builder, for example username and code_id.

  • Error value: invalid_user_credentials is what the OTP login form records for a wrong code, and what WebAuthn Register records when it rejects a registration response. No new Errors constant is added.
  • Deprecated type: UPDATE_TOTP_ERROR is not emitted. Remove deprecated event types for password/otp update or removal #32685 removes the deprecated event types, and one event per failed attempt keeps the volume down. It can be added to mirror Update Password if reviewers prefer.
  • Branches covered: the code does not validate against the new secret, and CredentialHelper.createOTPCredential returns false. Both show the same "Invalid authenticator code." message.
  • Branches not covered: blank code, missing device name and duplicate device name. These are form validation and no code is checked. This also keeps the event volume down, the concern raised in the review of Publish UPDATE_TOTP_ERROR event when invalid TOTP at setup #12259.

No new event type, no admin console change, no API change.

Tests

  • RequiredActionTotpSetupTest#setupTotpRegister already submits a wrong code. It now asserts the UPDATE_CREDENTIAL_ERROR event with invalid_user_credentials and credential_type=otp before the success events.
  • UserStorageOTPTest#testUpdateOTP submits a code the new secret rejects. It now clears the queued error event, so its next UPDATE_TOTP assertion reads the success event.

Test status

Run on a full local build of this branch (./mvnw clean install -DskipTests, then spotless:check, both successful):

  • RequiredActionTotpSetupTest, AppInitiatedActionTotpSetupTest and UserStorageOTPTest pass with the change: 33 tests, 0 failures, 0 errors (-Pauth-server-quarkus).
  • RequiredActionTotpSetupTest#setupTotpRegister fails without the UpdateTotp change (NullPointerException: the expected error event is not queued) and passes with it.
  • UserStorageOTPTest#testUpdateOTP passes with and without the change. Its edit only clears the new error event from the queue, so the following UPDATE_TOTP assertion still reads the success event. It does not assert the new event itself.

Before/After screenshots

The two screenshots (Events > User events, before and after) are attached in the first comment.

Same steps, data and window size on both builds: a realm with user events enabled, the user has the required action Configure OTP, and submits the wrong code 000000 twice.

Before (26.8.2): Events > User events lists nothing.

After (this PR): Events > User events lists one UPDATE_CREDENTIAL_ERROR per wrong code, with error invalid_user_credentials.

Compatibility

  • Realms that save events with the default saved types now store one extra event per wrong code during OTP setup.
  • The Email event listener does not include this error type by default, so no new emails are sent. Deployments that added UPDATE_CREDENTIAL_ERROR to include-events now get those events for OTP setup failures too.
  • Custom listeners and SIEM rules that read UPDATE_CREDENTIAL_ERROR now also receive credential_type=otp entries.
  • Brute force protection does not change. It is driven by the authentication flow, not by events.

AI assistance

An AI coding agent drafted this change, its tests and this description from a prompt.

🤖 Generated with Claude Code

The Configure OTP required action shows "Invalid authenticator code"
when the submitted code does not match the new secret, but it emits
no event. A failed OTP setup therefore leaves no trace in the event
store or in event listeners, while Update Password and WebAuthn
Register emit an error event when they reject the submitted
credential.

Emit UPDATE_CREDENTIAL_ERROR with error invalid_user_credentials on
both branches that reject the code. invalid_user_credentials is the
error the OTP login form records for a wrong code. The deprecated
UPDATE_TOTP_ERROR is not emitted, as it is scheduled for removal.

A blank code, a missing device name and a duplicate device name stay
without an event: they are form validation and no code is checked.

Closes keycloak#55639

Signed-off-by: Geremia Taglialatela <tagliala.dev@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Configure OTP required action emits no event for an invalid code

1 participant

0