8000 Publish UPDATE_TOTP_ERROR event when invalid TOTP at setup by MariuszCwikla · Pull Request #12259 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,6 @@ public interface Errors {
String SLOW_DOWN = "slow_down";
String GENERIC_AUTHENTICATION_ERROR= "generic_authentication_error";

String INVALID_TOTP = "invalid_totp";

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.keycloak.credential.CredentialModel;
import org.keycloak.credential.CredentialProvider;
import org.keycloak.credential.OTPCredentialProvider;
import org.keycloak.events.Errors;
import org.keycloak.events.EventBuilder;
import org.keycloak.events.EventType;
import org.keycloak.models.KeycloakSession;
Expand Down Expand Up @@ -91,6 +92,7 @@ public void processAction(RequiredActionContext context) {
.setAttribute("mode", mode)
.addError(new FormMessage(Validation.FIELD_OTP_CODE, Messages.INVALID_TOTP))
.createResponse(UserModel.RequiredAction.CONFIGURE_TOTP);
event.clone().event(EventType.UPDATE_TOTP_ERROR).error(Errors.INVALID_TOTP);
context.challenge(challenge);
return;
}
Expand All @@ -112,6 +114,7 @@ public void processAction(RequiredActionContext context) {
.setAttribute("mode", mode)
.addError(new FormMessage(Validation.FIELD_OTP_CODE, Messages.INVALID_TOTP))
.createResponse(UserModel.RequiredAction.CONFIGURE_TOTP);
event.clone().event(EventType.UPDATE_TOTP_ERROR).error(Errors.INVALID_TOTP);
context.challenge(challenge);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,16 +536,20 @@ public Response processTotpUpdate() {

OTPPolicy policy = realm.getOTPPolicy();
OTPCredentialModel credentialModel = OTPCredentialModel.createFromPolicy(realm, totpSecret, userLabel);

EventBuilder errorEvent = event.clone().event(EventType.UPDATE_TOTP_ERROR).client(auth.getClient()).user(auth.getUser());
if (Validation.isBlank(challengeResponse)) {
setReferrerOnPage();
return account.setError(Status.OK, Messages.MISSING_TOTP).createResponse(AccountPages.TOTP);
} else if (!CredentialValidation.validOTP(challengeResponse, credentialModel, policy.getLookAheadWindow())) {
setReferrerOnPage();
errorEvent.error(Errors.INVALID_TOTP);
return account.setError(Status.OK, Messages.INVALID_TOTP).createResponse(AccountPages.TOTP);
}

if (!CredentialHelper.createOTPCredential(session, realm, user, challengeResponse, credentialModel)) {
setReferrerOnPage();
errorEvent.error(Errors.INVALID_TOTP);
return account.setError(Status.OK, Messages.INVALID_TOTP).createResponse(AccountPages.TOTP);
}
event.event(EventType.UPDATE_TOTP).client(auth.getClient()).user(auth.getUser()).success();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,8 @@ public void setupTotp() {

Assert.assertEquals("Invalid authenticator code.", profilePage.getError());

events.expectAccount(EventType.UPDATE_TOTP_ERROR).assertEvent();

totpPage.configure(totp.generateTOTP(totpPage.getTotpSecret()));

Assert.assertEquals("Mobile authenticator configured.", profilePage.getSuccess());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.junit.Test;
import org.keycloak.common.Profile;
import org.keycloak.events.Details;
import org.keycloak.events.Errors;
import org.keycloak.events.EventType;
import org.keycloak.events.admin.OperationType;
import org.keycloak.models.credential.OTPCredentialModel;
Expand Down Expand Up @@ -77,6 +78,10 @@ public void setupTotp() {

Assert.assertFalse(driver.getPageSource().contains("Remove Google"));

totpPage.configure(totp.generateTOTP(totpPage.getTotpSecret() + "123"));

events.expectAccount(EventType.UPDATE_TOTP_ERROR).error(Errors.INVALID_TOTP).assertEvent();

totpPage.configure(totp.generateTOTP(totpPage.getTotpSecret()));

Assert.assertEquals("Mobile authenticator configured.", profilePage.getSuccess());
Expand Down
0