Skip to content

Commit

Permalink
fix some context problems
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasmatusiewicz committed Oct 2, 2023
1 parent 0fd9705 commit adca38a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;}
Expand All @@ -26,4 +29,7 @@ public PIFormContext(@Nullable String defaultMessage, @Nullable String otpFieldH

@Nonnull
public String getOtpFieldHint() {return otpFieldHint;}

@Nullable
public Integer getOtpLength() {return otpLength;}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,12 @@
<p>Hello $piContext.getUsername()!</p>
<br>
<p>$piFormContext.getMessage()</p>
<br>
<p>$piContext.getErrorMessage()</p>

#set ($formErrorMsg = $piContext.getFormErrorMessage())
#if ($formErrorMsg)
<br>
<p>$formErrorMsg</p>
#end
<br>
</div>

Expand All @@ -85,7 +89,7 @@
</script>

<div class="form-element-wrapper">
<input id="pi_otp_input" class="form-element form-field" name="pi_otp_input" type="password" onKeyUp="autoSubmit(this, $piContext.getOtpLength())" autofocus placeholder=$piContext.getOtpFieldHint() value="">
<input id="pi_otp_input" class="form-element form-field" name="pi_otp_input" type="password" onKeyUp="autoSubmit(this, $piFormContext.getOtpLength())" autofocus placeholder=$piFormContext.getOtpFieldHint() value="">
<br>
<button class="form-element form-button" id="pi-form-submit-button" type="submit" name="_eventId_proceed">Submit</button>
<br><br>
Expand Down

0 comments on commit adca38a

Please sign in to comment.