Package com.calebleavell.jatui.templates
Class PasswordInput
java.lang.Object
com.calebleavell.jatui.modules.TUIModule.Builder<PasswordInput>
com.calebleavell.jatui.modules.ModuleTemplate<PasswordInput>
com.calebleavell.jatui.templates.PasswordInput
- All Implemented Interfaces:
DirectedGraphNode<TUIModule.Property,TUIModule.Builder<?>, 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
Example usage:
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...
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.calebleavell.jatui.core.DirectedGraphNode
DirectedGraphNode.PropertyUpdateFlag -
Field Summary
Fields inherited from class com.calebleavell.jatui.modules.ModuleTemplate
mainFields inherited from class com.calebleavell.jatui.modules.TUIModule.Builder
ansi, application, children, enableAnsi, logger, name, printStream, propertyUpdateFlags, scanner, type, usedNames -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedprotectedPasswordInput(String name, String displayText, Supplier<char[]> passwordSupplier) Constructs a newPasswordInputbuilder. -
Method Summary
Modifier and TypeMethodDescriptionaddOnInvalidPassword(Runnable onInvalidPassword) Specifies behavior for when the user inputs an incorrect password.addOnInvalidPassword(String name, Supplier<?> onInvalidPassword) Specifies behavior for when the user inputs an incorrect password.addOnValidPassword(Runnable onValidPassword) Specifies behavior for when the user inputs the correct password.addOnValidPassword(String name, Supplier<?> onValidPassword) Specifies behavior for when the user inputs the correct password.build()Builds a newContainerModulewith the configuration from this builder.static PasswordInputConstructs a newPasswordInputbuilder.Configures the module to immediately clean the input and validation result after running.voidCleans any memory that may have been stored from this password input (including the input and whether the password was a match).protected PasswordInputGets a fresh instance of this type of Builder.displayTextis the text to display before getting input (e.g., "Password: ").Sets the name of this module.setDisplayText(String displayText) Set the text to display before getting input (e.g., "Password: ").voidshallowCopy(PasswordInput original) CopiesdisplayText,onValidPassword,onInvalidPassword,storeInput, andstoreMatch, and delegates toTUIModule.Builder.shallowCopy(TUIModule.Builder).booleanshallowStructuralEquals(PasswordInput first, PasswordInput second) Checks equality for properties given by the builder.Configures the module immediately clean the input but store whether the input was a match.Configures the module to immediately clean whether the input was a match but store the input itself in theApplicationModulestate (unencrypted).Configures the module store the input, as well as whether the input was a match, in theApplicationModulestate (unencrypted).booleanvalidatePassword(char[] correct) Validates a password that's been stored in memory.Methods inherited from class com.calebleavell.jatui.modules.ModuleTemplate
deepCopyMethods inherited from class com.calebleavell.jatui.modules.TUIModule.Builder
addChild, addChild, addChildren, addChildren, appendStyle, application, clearChildren, deepCopy, enableAnsi, getAnsi, getAnsiEnabled, getApplication, getChild, getChild, getChildren, getCopy, getDeepCopy, getName, getPrintStream, getPropertyUpdateFlags, getScanner, getType, lockProperty, prependStyle, prependToName, printStream, scanner, self, structuralEquals, style, toString, unlockProperty, updateFlag, updatePropertiesMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface com.calebleavell.jatui.core.DirectedGraphNode
abstractSelf, containsNullNode, containsNullNode, dfs, dfs, forEach, forEachChild, structuralEquals, structuralEquals, updateProperty, updateProperty
-
Constructor Details
-
PasswordInput
Constructs a newPasswordInputbuilder.- 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 newPasswordInputbuilder.- 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
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:
createInstancein classTUIModule.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 viastoreInput(),storeIfMatched(), orstoreInputAndMatch(). -
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 viastoreInput().- Parameters:
correct- The actual password.- Returns:
- Whether the inputted password matches the correct password, if it exists and was saved.
-
addOnValidPassword
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
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 theFunctionModulebeing built internally, as well as the input identifier for the return value ofonValidPasswordthat can be retrieved atApplicationModule.getInput(String).onValidPassword- Logic to execute when the user inputs the correct password. The return value is stored in theApplicationModuleand can be retrieved vianame.- Returns:
- self
-
addOnInvalidPassword
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
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 theFunctionModulebeing built internally, as well as the input identifier for the return value ofonValidPasswordthat can be retrieved atApplicationModule.getInput(String).onInvalidPassword- Logic to execute when the user inputs an incorrect password. The return value is stored in theApplicationModuleand can be retrieved vianame.- Returns:
- self
-
cleanImmediately
Configures the module to immediately clean the input and validation result after running. This is the default setting.
The other settings arestoreIfMatched(),storeInput(), andstoreInputAndMatch(), but these are generally not recommended.- Returns:
- self
-
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., viaTUIModule.Builder.application(ApplicationModule)).
The other settings arecleanImmediately(),storeInput(), andstoreInputAndMatch().- Returns:
- self
-
storeInput
Configures the module to immediately clean whether the input was a match but store the input itself in theApplicationModulestate (unencrypted). This is generally not recommended.
The application state (included stored passwords) can be fully cleaned viaApplicationModule.resetMemory().
Nothing will be stored if this module isn't tied to an application (e.g., viaTUIModule.Builder.application(ApplicationModule)).
The other settings arecleanImmediately(),storeIfMatched(), andstoreInputAndMatch().- Returns:
- self
-
storeInputAndMatch
Configures the module store the input, as well as whether the input was a match, in theApplicationModulestate (unencrypted). This is generally not recommended.
The application state (included stored passwords) can be fully cleaned viaApplicationModule.resetMemory().
Nothing will be stored if this module isn't tied to an application (e.g., viaTUIModule.Builder.application(ApplicationModule)).
The other settings arecleanImmediately(),storeIfMatched(), andstoreInput().- Returns:
- self
-
name
Sets the name of this module.- Overrides:
namein classTUIModule.Builder<PasswordInput>- Parameters:
name- The unique name of this module.- Returns:
- self
- Implementation Note:
- sets the names of the
FunctionModulethat collects input to stay consistent with the new name.
-
setDisplayText
Set the text to display before getting input (e.g., "Password: ").- Parameters:
displayText- The new text to display.- Returns:
- self
-
getDisplayText
displayTextis the text to display before getting input (e.g., "Password: ").- Returns:
displayText
-
build
Builds a newContainerModulewith the configuration from this builder. Ensures theFunctionModulethat collects input is up-to-date with the most recent configuration.- Overrides:
buildin classModuleTemplate<PasswordInput>- Returns:
- self
-
shallowStructuralEquals
Checks equality for properties given by the builder. ForPasswordInput, this includesonValidPassword,onInvalidPassword,displayText,storeInput, andstoreMatch, as well as other requirements provided byTUIModule.Builder.shallowStructuralEquals(TUIModule.Builder, TUIModule.Builder).- Specified by:
shallowStructuralEqualsin interfaceDirectedGraphNode<TUIModule.Property,TUIModule.Builder<?>, PasswordInput> - Overrides:
shallowStructuralEqualsin classTUIModule.Builder<PasswordInput>- Parameters:
first- The first TUIModule to comparesecond- The second TUIModule to compare- Returns:
trueiffirstandsecondare equal according to builder-provided properties
-
shallowCopy
CopiesdisplayText,onValidPassword,onInvalidPassword,storeInput, andstoreMatch, and delegates toTUIModule.Builder.shallowCopy(TUIModule.Builder).- Overrides:
shallowCopyin classTUIModule.Builder<PasswordInput>- Parameters:
original- The builder to copy from.
-