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:
commit
10024f9526
1 changed files with 29 additions and 4 deletions
|
@ -36,6 +36,7 @@ import org.keycloak.representations.idm.RealmRepresentation;
|
||||||
import org.keycloak.representations.idm.UserRepresentation;
|
import org.keycloak.representations.idm.UserRepresentation;
|
||||||
import org.keycloak.util.JsonSerialization;
|
import org.keycloak.util.JsonSerialization;
|
||||||
|
|
||||||
|
import java.io.Console;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
|
@ -73,12 +74,16 @@ public class AddUser {
|
||||||
printHelp(command);
|
printHelp(command);
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
|
String password = command.getPassword();
|
||||||
checkRequired(command, "user");
|
checkRequired(command, "user");
|
||||||
checkRequired(command, "password");
|
|
||||||
|
if(isEmpty(command, "password")){
|
||||||
|
password = promptForInput();
|
||||||
|
}
|
||||||
|
|
||||||
File addUserFile = getAddUserFile(command);
|
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) {
|
} catch (Exception e) {
|
||||||
System.err.println(e.getMessage());
|
System.err.println(e.getMessage());
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
|
@ -207,8 +212,7 @@ public class AddUser {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void checkRequired(Command command, String field) throws Exception {
|
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 (isEmpty(command, field)) {
|
||||||
if (m.invoke(command) == null) {
|
|
||||||
Option option = command.getClass().getDeclaredField(field).getAnnotation(Option.class);
|
Option option = command.getClass().getDeclaredField(field).getAnnotation(Option.class);
|
||||||
String optionName;
|
String optionName;
|
||||||
if (option != null && option.shortName() != '\u0000') {
|
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 {
|
private static void printHelp(Command command) throws CommandNotFoundException {
|
||||||
CommandRegistry registry = new AeshCommandRegistryBuilder().command(command).create();
|
CommandRegistry registry = new AeshCommandRegistryBuilder().command(command).create();
|
||||||
CommandContainer commandContainer = registry.getCommand(command.getClass().getAnnotation(CommandDefinition.class).name(), null);
|
CommandContainer commandContainer = registry.getCommand(command.getClass().getAnnotation(CommandDefinition.class).name(), null);
|
||||||
|
|
Loading…
Reference in a new issue