Java Code Examples for java.io.Console#readPassword()

The following examples show how to use java.io.Console#readPassword() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AuthTool.java    From floodlight_with_topoguard with Apache License 2.0 6 votes vote down vote up
protected void init(String[] args) {
    try {
        parser.parseArgument(args);
    } catch (CmdLineException e) {
        System.err.println(e.getMessage());
        parser.printUsage(System.err);
        System.exit(1);
    }
    if (help) {
        parser.printUsage(System.err);
        System.exit(1);
    }
    if (!AuthScheme.NO_AUTH.equals(authScheme)) {
        if (keyStorePath == null) {
            System.err.println("keyStorePath is required when " + 
                               "authScheme is " + authScheme);
            parser.printUsage(System.err);
            System.exit(1);
        }
        if (keyStorePassword == null) {
            Console con = System.console();
            char[] password = con.readPassword("Enter key store password: ");
            keyStorePassword = new String(password);
        }
    }
}
 
Example 2
Source File: KnoxCLI.java    From knox with Apache License 2.0 6 votes vote down vote up
protected char[] promptUserForPassword() {
  char[] password = null;
  Console c = System.console();
  if (c == null) {
    System.err
        .println("No console to fetch password from user.Consider setting via --generate or --value.");
    System.exit(1);
  }

  boolean noMatch;
  do {
    char[] newPassword1 = c.readPassword("Enter password: ");
    char[] newPassword2 = c.readPassword("Enter password again: ");
    noMatch = !Arrays.equals(newPassword1, newPassword2);
    if (noMatch) {
      c.format("Passwords don't match. Try again.%n");
    } else {
      password = Arrays.copyOf(newPassword1, newPassword1.length);
    }
    Arrays.fill(newPassword1, ' ');
    Arrays.fill(newPassword2, ' ');
  } while (noMatch);
  return password;
}
 
Example 3
Source File: CMFMasterService.java    From knox with Apache License 2.0 6 votes vote down vote up
protected void promptUser() {
  Console c = System.console();
  if (c == null) {
    LOG.unableToPromptForMasterUseKnoxCLI();
    System.err.println("No console.");
    System.exit(1);
  }

  boolean valid = false;
  do {
      char [] newPassword1 = c.readPassword("Enter master secret: ");
      char [] newPassword2 = c.readPassword("Enter master secret again: ");
      if ( newPassword1.length == 0 ) {
          c.format("Password too short. Try again.%n");
      } else if (!Arrays.equals(newPassword1, newPassword2) ) {
          c.format("Passwords don't match. Try again.%n");
      } else {
          this.master = Arrays.copyOf(newPassword1, newPassword1.length);
          valid = true;
      }
      Arrays.fill(newPassword1, ' ');
      Arrays.fill(newPassword2, ' ');
  } while (!valid);
}
 
Example 4
Source File: CredentialReader.java    From obevo with Apache License 2.0 6 votes vote down vote up
/**
 * @return
 */
private Credential getCredentialFromConsole(Credential credential) {
    Console cons = System.console();

    if (credential.getUsername() == null) {
        LOG.info("Enter your kerberos (or, DB user name if DB is not DACT enabled): ");
        credential.setUsername(cons.readLine());
    }

    if (!credential.isAuthenticationMethodProvided()) {
        char[] passwd = cons.readPassword("%s", "Password:");
        if (passwd != null) {
            credential.setPassword(new String(passwd));
        }
    }
    return credential;
}
 
Example 5
Source File: AuthenticationUtil.java    From atlas with Apache License 2.0 6 votes vote down vote up
public static String[] getBasicAuthenticationInput() {
    String username = null;
    String password = null;

    try {
        Console console = System.console();
        if (console == null) {
            System.err.println("Couldn't get a console object for user input");
            System.exit(1);
        }

        username = console.readLine("Enter username for atlas :- ");

        char[] pwdChar = console.readPassword("Enter password for atlas :- ");
        if(pwdChar != null) {
            password = new String(pwdChar);
        }

    } catch (Exception e) {
        System.out.print("Error while reading user input");
        System.exit(1);
    }
    return new String[]{username, password};
}
 
Example 6
Source File: AuthenticationUtil.java    From incubator-atlas with Apache License 2.0 6 votes vote down vote up
public static String[] getBasicAuthenticationInput() {
    String username = null;
    String password = null;

    try {
        Console console = System.console();
        username = console.readLine("Enter username for atlas :- ");

        char[] pwdChar = console.readPassword("Enter password for atlas :- ");
        if(pwdChar != null) {
            password = new String(pwdChar);
        }

    } catch (Exception e) {
        System.out.print("Error while reading ");
        System.exit(1);
    }
    return new String[]{username, password};
}
 
Example 7
Source File: TerminalIo.java    From tmc-cli with MIT License 6 votes vote down vote up
@Override
public String readPassword(String prompt) {
    Console console = System.console();
    if (console != null) {
        try {
            return new String(console.readPassword(prompt));
        } catch (Exception e) {
            logger.warn("Password could not be read.", e);
        }
    } else {
        logger.warn("Failed to read password due to System.console()");
    }
    println("Unable to read password securely. Reading password in cleartext.");
    println("Press Ctrl+C to abort");
    return readLine(prompt);
}
 
Example 8
Source File: VaultInteractiveSession.java    From tomcat-vault with Apache License 2.0 6 votes vote down vote up
public static char[] getSensitiveValue(String passwordPrompt) {
    while (true) {
        if (passwordPrompt == null)
            passwordPrompt = "Enter your password";

        Console console = System.console();

        char[] passwd = console.readPassword(passwordPrompt + ": ");
        char[] passwd1 = console.readPassword(passwordPrompt + " again: ");
        boolean noMatch = !Arrays.equals(passwd, passwd1);
        if (noMatch)
            System.out.println("Values entered don't match");
        else {
            System.out.println("Values match");
            return passwd;
        }
    }
}
 
Example 9
Source File: Utils.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieve value via the console
 *
 * @param msg        message to display in the inquiry
 * @param isPassword is the requested value a password or not
 * @return value provided by the user
 */
public static String getValueFromConsole(String msg, boolean isPassword) {
    Console console = System.console();
    if (console != null) {
        if (isPassword) {
            char[] password;
            if ((password = console.readPassword("[%s]", msg)) != null) {
                return String.valueOf(password);
            }
        } else {
            String value;
            if ((value = console.readLine("[%s]", msg)) != null) {
                return value;
            }
        }
    }
    throw new SecureVaultException("String cannot be null");
}
 
Example 10
Source File: AddUser.java    From keycloak with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 11
Source File: ConsoleDemo.java    From JavaCommon with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
	Console console = System.console();
	if (console != null) {
		String user = new String(console.readLine("Enter User:", new Object[0]));
		String pwd = new String(console.readPassword("Enter Password:", new Object[0]));
		console.printf("User name is:%s", new Object[] { user });
		console.printf("Password is:%s", new Object[] { pwd });
	} else {
		System.out.println("No Console!");
	}
}
 
Example 12
Source File: PasswordReader.java    From gocd-filebased-authentication-plugin with Apache License 2.0 5 votes vote down vote up
public String readPassword() {
    final Console console = System.console();
    try {
        String newPassword = new String(console.readPassword("New password: "));
        String reTypePassword = new String(console.readPassword("Re-type new password: "));

        if (isNotBlank(newPassword) && newPassword.equals(reTypePassword)) {
            return newPassword;
        }
    } finally {
        console.flush();
    }

    throw new RuntimeException("Password verification error.");
}
 
Example 13
Source File: WinCli.java    From incubator-iotdb with Apache License 2.0 5 votes vote down vote up
private static String readPassword() {
  Console c = System.console();
  if (c == null) { // IN ECLIENTPSE IDE
    print(IOTDB_CLI_PREFIX + "> please input password: ");
    Scanner scanner = new Scanner(System.in);
    return scanner.nextLine();
  } else { // Outside Eclipse IDE
    return new String(c.readPassword(IOTDB_CLI_PREFIX + "> please input password: "));
  }
}
 
Example 14
Source File: AddUserCommand.java    From keywhiz with Apache License 2.0 5 votes vote down vote up
@Override protected void run(Bootstrap<KeywhizConfig> bootstrap, Namespace namespace,
                             KeywhizConfig config) throws Exception {
  DataSource dataSource = config.getDataSourceFactory()
    .build(new MetricRegistry(), "add-user-datasource");

  Console console = System.console();
  System.out.format("New username:");
  String user = console.readLine();
  System.out.format("password for '%s': ", user);
  char[] password = console.readPassword();
  DSLContext dslContext = DSLContexts.databaseAgnostic(dataSource);
  new UserDAO(dslContext).createUser(user, new String(password));
}
 
Example 15
Source File: IoUtil.java    From xipki with Apache License 2.0 5 votes vote down vote up
public static char[] readPasswordFromConsole(String prompt) {
  Console console = System.console();
  if (console == null) {
    throw new IllegalStateException("No console is available for input");
  }
  System.out.println(prompt == null ? "Enter the password" : prompt);
  return console.readPassword();
}
 
Example 16
Source File: PasswordReader.java    From aion with MIT License 5 votes vote down vote up
/**
 * Returns a password after prompting the user to enter it. This method attempts first to read
 * user input from a console environment and if one is not available it instead attempts to read
 * from reader.
 *
 * @param prompt The read-password prompt to display to the user.
 * @return The user-entered password.
 * @throws NullPointerException if prompt is null or if console unavailable and reader is null.
 */
public String readPassword(String prompt, BufferedReader reader) {
    if (prompt == null) {
        throw new NullPointerException("readPassword given null prompt.");
    }

    Console console = System.console();
    if (console == null) {
        return readPasswordFromReader(prompt, reader);
    }
    return new String(console.readPassword(prompt));
}
 
Example 17
Source File: GfxdServerLauncher.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
protected void readPassword(Map<String, String> envArgs) throws Exception {
  final Console cons = System.console();
  if (cons == null) {
    throw new IllegalStateException(
        "No console found for reading the password.");
  }
  final char[] pwd = cons.readPassword(LocalizedResource
      .getMessage("UTIL_password_Prompt"));
  if (pwd != null) {
    final String passwd = new String(pwd);
    // encrypt the password with predefined key that is salted with host IP
    final byte[] keyBytes = getBytesEnv();
    envArgs.put(ENV1, GemFireXDUtils.encrypt(passwd, null, keyBytes));
  }
}
 
Example 18
Source File: PromptUtils.java    From knox with Apache License 2.0 5 votes vote down vote up
public static UsernamePassword challengeUserNamePassword(String prompt1, String prompt2) {
  UsernamePassword response;
  Console c = System.console();
  if (c == null) {
    System.err.println("No console.");
    System.exit(1);
  }

  String username = c.readLine(prompt1 + ": ");
  char [] pwd = c.readPassword(prompt2 + ": ");
  response = new UsernamePassword(username, pwd);

  return response;
}
 
Example 19
Source File: CredentialShell.java    From hadoop with Apache License 2.0 4 votes vote down vote up
public char[] readPassword(String prompt) {
  Console console = System.console();
  char[] pass = console.readPassword(prompt);
  return pass;
}
 
Example 20
Source File: ClientUtils.java    From keywhiz with Apache License 2.0 3 votes vote down vote up
/**
 * Read password from console.
 *
 * Note that when the System.console() is null, there is no secure way of entering a password
 * without exposing it in the clear on the console (it is echoed onto the screen).
 *
 * For this reason, it is suggested that the user login prior to using functionality such as
 * input redirection since this could result in a null console.
 *
 * @param user who we are prompting a password for
 * @return user-inputted password
 */
public static char[] readPassword(String user) {
  Console console = System.console();
  if (console != null) {
    System.out.format("password for '%s': ", user);
    return console.readPassword();
  } else {
    throw new RuntimeException("Please login by running a command without piping.\n"
        + "For example: keywhiz.cli login");
  }
}