Merge pull request #2243 from abstractj/KEYCLOAK-2501

KEYCLOAK-2501: Add-user should prompt user for password if not set
This commit is contained in:
Stian Thorgersen 2016-02-22 08:02:33 +00:00
commit 10024f9526

View file

@ -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);