diff --git a/wildfly/adduser/src/main/java/org/keycloak/wildfly/adduser/AddUser.java b/wildfly/adduser/src/main/java/org/keycloak/wildfly/adduser/AddUser.java index 9232f9e130..a95ea5e00b 100644 --- a/wildfly/adduser/src/main/java/org/keycloak/wildfly/adduser/AddUser.java +++ b/wildfly/adduser/src/main/java/org/keycloak/wildfly/adduser/AddUser.java @@ -36,6 +36,7 @@ import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.representations.idm.UserRepresentation; import org.keycloak.util.JsonSerialization; +import java.io.Console; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -73,12 +74,16 @@ public class AddUser { printHelp(command); } else { try { + String password = command.getPassword(); checkRequired(command, "user"); - checkRequired(command, "password"); + + if(isEmpty(command, "password")){ + password = promptForInput(); + } File addUserFile = getAddUserFile(command); - createUser(addUserFile, command.getRealm(), command.getUser(), command.getPassword(), command.getRoles(), command.getIterations()); + createUser(addUserFile, command.getRealm(), command.getUser(), password, command.getRoles(), command.getIterations()); } catch (Exception e) { System.err.println(e.getMessage()); System.exit(1); @@ -207,8 +212,7 @@ public class AddUser { } private static void checkRequired(Command command, String field) throws Exception { - Method m = command.getClass().getMethod("get" + Character.toUpperCase(field.charAt(0)) + field.substring(1)); - if (m.invoke(command) == null) { + if (isEmpty(command, field)) { Option option = command.getClass().getDeclaredField(field).getAnnotation(Option.class); String optionName; if (option != null && option.shortName() != '\u0000') { @@ -220,6 +224,27 @@ public class AddUser { } } + private static Boolean isEmpty(Command command, String field) throws Exception { + Method m = command.getClass().getMethod("get" + Character.toUpperCase(field.charAt(0)) + field.substring(1)); + if (m.invoke(command) == null) { + return true; + } + return false; + } + + private static String promptForInput() throws Exception { + Console console = System.console(); + if (console == null) { + throw new Exception("Couldn't get Console instance"); + } + console.printf("Press ctrl-d (Unix) or ctrl-z (Windows) to exit\n"); + char passwordArray[] = console.readPassword("Password: "); + + if(passwordArray == null) System.exit(0); + + return new String(passwordArray); + } + private static void printHelp(Command command) throws CommandNotFoundException { CommandRegistry registry = new AeshCommandRegistryBuilder().command(command).create(); CommandContainer commandContainer = registry.getCommand(command.getClass().getAnnotation(CommandDefinition.class).name(), null);