Class PasswordInput

All Implemented Interfaces:
DirectedGraphNode<TUIModule.Property,TUIModule.Builder<?>,PasswordInput>

public class PasswordInput extends ModuleTemplate<PasswordInput>
Handles collecting, validating, and cleaning passwords from the user. Zeroes out the password char array after collection unless explicitly set to do otherwise via storeInput() (not recommended).

Example usage:

 ApplicationModule app = ApplicationModule.builder("app").build();

 TextModule.Builder homePage = TextModule.builder("home", "Home Page");

 Supplier<char[]> supplyPassword = "password"::toCharArray; // would likely reference some database in practice

 PasswordInput myInput = PasswordInput.builder("pw-input", "Password: ", supplyPassword)
   .addOnValidPassword(() -> app.navigateTo(homePage))
   .addOnInvalidPassword(app::restart);

 app.setHome(myInput);
 app.run();
 
Output:
 Password: incorrect
 Password: password
 Home Page
 Exiting...
 
  • Constructor Details

    • PasswordInput

      protected PasswordInput(String name, String displayText, Supplier<char[]> passwordSupplier)
      Constructs a new PasswordInput builder.
      Parameters:
      name - The name of the module.
      displayText - The text to display before getting input (e.g., "Password: ").
      passwordSupplier - The supplier of the correct password for input validation. There is no restriction on what it may reference (e.g., a database, some hardcoded value, etc.). The supplier should return a fresh copy of the array per invocation, as the array is zeroed out after use.
    • PasswordInput

      protected PasswordInput()
  • Method Details

    • builder

      public static PasswordInput builder(String name, String displayText, Supplier<char[]> passwordSupplier)
      Constructs a new PasswordInput builder.
      Parameters:
      name - The name of the module.
      displayText - The text to display before getting input (e.g., "Password: ").
      passwordSupplier - The supplier of the correct password for input validation. There is no restriction on what it may reference (e.g., a database, some hardcoded value, etc.).
    • createInstance

      protected PasswordInput createInstance()
      Gets a fresh instance of this type of Builder. Note, this is intended only for copying utility and may have unknown consequences if used in other ways.
      Specified by:
      createInstance in class TUIModule.Builder<PasswordInput>
      Returns:
      A fresh, empty instance.
    • cleanMemory

      public void cleanMemory()
      Cleans any memory that may have been stored from this password input (including the input and whether the password was a match).

      Note: Memory is automatically cleaned unless you explicitly set this module to store it via storeInput(), storeIfMatched(), or storeInputAndMatch().
    • validatePassword

      public boolean validatePassword(char[] correct)
      Validates a password that's been stored in memory.

      Note: this will always return false if the behavior hasn't been set to store the password via storeInput().
      Parameters:
      correct - The actual password.
      Returns:
      Whether the inputted password matches the correct password, if it exists and was saved.
    • addOnValidPassword

      public PasswordInput addOnValidPassword(Runnable onValidPassword)
      Specifies behavior for when the user inputs the correct password. Input is validated automatically, but input is not re-collected automatically on an invalid password.
      Parameters:
      onValidPassword - Logic to execute when the user inputs the correct password.
      Returns:
      self
    • addOnValidPassword

      public PasswordInput addOnValidPassword(String name, Supplier<?> onValidPassword)
      Specifies behavior for when the user inputs the correct password. Input is validated automatically, but input is not re-collected automatically on an invalid password.
      Parameters:
      name - The name of the FunctionModule being built internally, as well as the input identifier for the return value of onValidPassword that can be retrieved at ApplicationModule.getInput(String).
      onValidPassword - Logic to execute when the user inputs the correct password. The return value is stored in the ApplicationModule and can be retrieved via name.
      Returns:
      self
    • addOnInvalidPassword

      public PasswordInput addOnInvalidPassword(Runnable onInvalidPassword)
      Specifies behavior for when the user inputs an incorrect password. Input is validated automatically, but input is not re-collected automatically on an invalid password.
      Parameters:
      onInvalidPassword - Logic to execute when the user inputs an incorrect password.
      Returns:
      self
    • addOnInvalidPassword

      public PasswordInput addOnInvalidPassword(String name, Supplier<?> onInvalidPassword)
      Specifies behavior for when the user inputs an incorrect password. Input is validated automatically, but input is not re-collected automatically on an invalid password.
      Parameters:
      name - The name of the FunctionModule being built internally, as well as the input identifier for the return value of onValidPassword that can be retrieved at ApplicationModule.getInput(String).
      onInvalidPassword - Logic to execute when the user inputs an incorrect password. The return value is stored in the ApplicationModule and can be retrieved via name.
      Returns:
      self
    • cleanImmediately

      public PasswordInput cleanImmediately()
      Configures the module to immediately clean the input and validation result after running. This is the default setting.
      The other settings are storeIfMatched(), storeInput(), and storeInputAndMatch(), but these are generally not recommended.
      Returns:
      self
    • storeIfMatched

      public PasswordInput storeIfMatched()
      Configures the module immediately clean the input but store whether the input was a match. This is generally not required.
      Nothing will be stored if this module isn't tied to an application (e.g., via TUIModule.Builder.application(ApplicationModule)).
      The other settings are cleanImmediately(), storeInput(), and storeInputAndMatch().
      Returns:
      self
    • storeInput

      public PasswordInput storeInput()
      Configures the module to immediately clean whether the input was a match but store the input itself in the ApplicationModule state (unencrypted). This is generally not recommended.
      The application state (included stored passwords) can be fully cleaned via ApplicationModule.resetMemory().
      Nothing will be stored if this module isn't tied to an application (e.g., via TUIModule.Builder.application(ApplicationModule)).
      The other settings are cleanImmediately(), storeIfMatched(), and storeInputAndMatch().
      Returns:
      self
    • storeInputAndMatch

      public PasswordInput storeInputAndMatch()
      Configures the module store the input, as well as whether the input was a match, in the ApplicationModule state (unencrypted). This is generally not recommended.
      The application state (included stored passwords) can be fully cleaned via ApplicationModule.resetMemory().
      Nothing will be stored if this module isn't tied to an application (e.g., via TUIModule.Builder.application(ApplicationModule)).
      The other settings are cleanImmediately(), storeIfMatched(), and storeInput().
      Returns:
      self
    • name

      public PasswordInput name(String name)
      Sets the name of this module.
      Overrides:
      name in class TUIModule.Builder<PasswordInput>
      Parameters:
      name - The unique name of this module.
      Returns:
      self
      Implementation Note:
      sets the names of the FunctionModule that collects input to stay consistent with the new name.
    • setDisplayText

      public PasswordInput setDisplayText(String displayText)
      Set the text to display before getting input (e.g., "Password: ").
      Parameters:
      displayText - The new text to display.
      Returns:
      self
    • getDisplayText

      public String getDisplayText()
      displayText is the text to display before getting input (e.g., "Password: ").
      Returns:
      displayText
    • build

      public ContainerModule build()
      Builds a new ContainerModule with the configuration from this builder. Ensures the FunctionModule that collects input is up-to-date with the most recent configuration.
      Overrides:
      build in class ModuleTemplate<PasswordInput>
      Returns:
      self
    • shallowStructuralEquals

      public boolean shallowStructuralEquals(PasswordInput first, PasswordInput second)
      Checks equality for properties given by the builder. For PasswordInput, this includes onValidPassword, onInvalidPassword, displayText, storeInput, and storeMatch, as well as other requirements provided by TUIModule.Builder.shallowStructuralEquals(TUIModule.Builder, TUIModule.Builder).
      Specified by:
      shallowStructuralEquals in interface DirectedGraphNode<TUIModule.Property,TUIModule.Builder<?>,PasswordInput>
      Overrides:
      shallowStructuralEquals in class TUIModule.Builder<PasswordInput>
      Parameters:
      first - The first TUIModule to compare
      second - The second TUIModule to compare
      Returns:
      true if first and second are equal according to builder-provided properties
    • shallowCopy

      public void shallowCopy(PasswordInput original)
      Copies displayText, onValidPassword, onInvalidPassword, storeInput, and storeMatch, and delegates to TUIModule.Builder.shallowCopy(TUIModule.Builder).
      Overrides:
      shallowCopy in class TUIModule.Builder<PasswordInput>
      Parameters:
      original - The builder to copy from.