From adca38ad2384892b71f06e97f30d9e13d6d7fe49 Mon Sep 17 00:00:00 2001 From: lukasmatusiewicz <77617779+lukasmatusiewicz@users.noreply.github.com> Date: Mon, 2 Oct 2023 12:13:53 +0200 Subject: [PATCH] fix some context problems --- .../action/InitializePIContext.java | 15 +++++++------ .../action/PrivacyIDEAAuthenticator.java | 4 ++-- .../org/privacyidea/context/PIContext.java | 21 ++----------------- .../privacyidea/context/PIFormContext.java | 12 ++++++++--- .../resources/org/privacyidea/views/main.vm | 10 ++++++--- 5 files changed, 27 insertions(+), 35 deletions(-) diff --git a/privacyIDEA-impl/src/main/java/org/privacyidea/action/InitializePIContext.java b/privacyIDEA-impl/src/main/java/org/privacyidea/action/InitializePIContext.java index 8960927..fa61a9b 100644 --- a/privacyIDEA-impl/src/main/java/org/privacyidea/action/InitializePIContext.java +++ b/privacyIDEA-impl/src/main/java/org/privacyidea/action/InitializePIContext.java @@ -65,30 +65,29 @@ protected void doExecute(@NotNull ProfileRequestContext profileRequestContext, @ PIServerConfigContext piServerConfigContext = new PIServerConfigContext(configParams); log.info("{} Create PIServerConfigContext {}", this.getLogPrefix(), piServerConfigContext); authenticationContext.addSubcontext(piServerConfigContext); - PIContext piContext; PIContext piContext = new PIContext(user); + log.info("{} Create PIContext {}", this.getLogPrefix(), piContext); + authenticationContext.addSubcontext(piContext); + + PIFormContext piFormContext; if (otpLength != null) { try { int otpLengthToInt = Integer.parseInt(otpLength); - piContext = new PIContext(user, defaultMessage, otpFieldHint, otpLengthToInt); + piFormContext = new PIFormContext(defaultMessage, otpFieldHint, otpLengthToInt); } catch (NumberFormatException e) { log.info("{} Config option \"otp_length\": Wrong format. Only digits allowed.", getLogPrefix()); - piContext = new PIContext(user, defaultMessage, otpFieldHint, null); + piFormContext = new PIFormContext(defaultMessage, otpFieldHint, null); } } else { - piContext = new PIContext(user, defaultMessage, otpFieldHint, null); + piFormContext = new PIFormContext(defaultMessage, otpFieldHint, null); } - log.info("{} Create PIContext {}", this.getLogPrefix(), piContext); - authenticationContext.addSubcontext(piContext); - - PIFormContext piFormContext = new PIFormContext(defaultMessage, otpFieldHint); log.info("{} Create PIFormContext {}", this.getLogPrefix(), piFormContext); authenticationContext.addSubcontext(piFormContext); } diff --git a/privacyIDEA-impl/src/main/java/org/privacyidea/action/PrivacyIDEAAuthenticator.java b/privacyIDEA-impl/src/main/java/org/privacyidea/action/PrivacyIDEAAuthenticator.java index 3f00371..c1f3b44 100644 --- a/privacyIDEA-impl/src/main/java/org/privacyidea/action/PrivacyIDEAAuthenticator.java +++ b/privacyIDEA-impl/src/main/java/org/privacyidea/action/PrivacyIDEAAuthenticator.java @@ -74,8 +74,8 @@ else if (piContext.getMode().equals("otp")) if (piResponse.error != null) { LOGGER.error("{} privacyIDEA server error: {}!", this.getLogPrefix(), piResponse.error.message); - ActionSupport.buildEvent(profileRequestContext, "AuthenticationException"); - return; + piContext.setFormErrorMessage(piResponse.error.message); + ActionSupport.buildEvent(profileRequestContext, "reload"); } if (!piResponse.multichallenge.isEmpty()) diff --git a/privacyIDEA-impl/src/main/java/org/privacyidea/context/PIContext.java b/privacyIDEA-impl/src/main/java/org/privacyidea/context/PIContext.java index d7ba572..34da862 100644 --- a/privacyIDEA-impl/src/main/java/org/privacyidea/context/PIContext.java +++ b/privacyIDEA-impl/src/main/java/org/privacyidea/context/PIContext.java @@ -1,5 +1,6 @@ package org.privacyidea.context; +import java.util.Objects; import javax.annotation.Nonnull; import javax.annotation.Nullable; import net.shibboleth.utilities.java.support.logic.Constraint; @@ -21,28 +22,16 @@ public class PIContext extends BaseContext private String origin = null; @Nonnull private String mode = "otp"; - @Nonnull - private final String otpFieldHint; - @Nullable - private final Integer otpLength; - public PIContext(@Nonnull User user, @Nullable String defaultMessage, @Nullable String otpFieldHint, @Nullable Integer otpLength) + public PIContext(@Nonnull User user) { this.user = Constraint.isNotNull(user, "User cannot be null."); - this.defaultMessage = Objects.requireNonNullElse(defaultMessage, "Please enter the OTP!"); - this.otpFieldHint = Objects.requireNonNullElse(otpFieldHint, "OTP"); - this.otpLength = otpLength; } // Getters and Setters @Nonnull public String getUsername() {return user.getUsername();} - public void setMessage(String message) {this.message = message;} - - @Nonnull - public String getMessage() {return (!message.isEmpty()) ? message : defaultMessage;} - public void setTransactionID(@Nonnull String transactionID) {this.transactionID = transactionID;} @Nullable @@ -72,10 +61,4 @@ public PIContext(@Nonnull User user, @Nullable String defaultMessage, @Nullable public String getFormErrorMessage() {return formErrorMessage;} public void setFormErrorMessage(@Nullable String formErrorMessage) {this.formErrorMessage = formErrorMessage;} - - @Nonnull - public String getOtpFieldHint() {return otpFieldHint;} - - @Nullable - public Integer getOtpLength() {return otpLength;} } diff --git a/privacyIDEA-impl/src/main/java/org/privacyidea/context/PIFormContext.java b/privacyIDEA-impl/src/main/java/org/privacyidea/context/PIFormContext.java index 4449d21..639275f 100644 --- a/privacyIDEA-impl/src/main/java/org/privacyidea/context/PIFormContext.java +++ b/privacyIDEA-impl/src/main/java/org/privacyidea/context/PIFormContext.java @@ -8,15 +8,18 @@ public class PIFormContext extends BaseContext { @Nonnull - private final String defaultMessage; + private String defaultMessage; private String message = ""; @Nonnull - private final String otpFieldHint; + private String otpFieldHint; + @Nullable + private final Integer otpLength; - public PIFormContext(@Nullable String defaultMessage, @Nullable String otpFieldHint) + public PIFormContext(@Nullable String defaultMessage, @Nullable String otpFieldHint, @Nullable Integer otpLength) { this.defaultMessage = Objects.requireNonNullElse(defaultMessage, "Please enter the OTP!"); this.otpFieldHint = Objects.requireNonNullElse(otpFieldHint, "OTP"); + this.otpLength = otpLength; } public void setMessage(String message) {this.message = message;} @@ -26,4 +29,7 @@ public PIFormContext(@Nullable String defaultMessage, @Nullable String otpFieldH @Nonnull public String getOtpFieldHint() {return otpFieldHint;} + + @Nullable + public Integer getOtpLength() {return otpLength;} } diff --git a/privacyIDEA-impl/src/main/resources/org/privacyidea/views/main.vm b/privacyIDEA-impl/src/main/resources/org/privacyidea/views/main.vm index b47a6a0..9e85f5c 100644 --- a/privacyIDEA-impl/src/main/resources/org/privacyidea/views/main.vm +++ b/privacyIDEA-impl/src/main/resources/org/privacyidea/views/main.vm @@ -68,8 +68,12 @@
Hello $piContext.getUsername()!
$piFormContext.getMessage()
-$piContext.getErrorMessage()
+ + #set ($formErrorMsg = $piContext.getFormErrorMessage()) + #if ($formErrorMsg) +$formErrorMsg
+ #end