Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mask token value during input from user #116

Merged
merged 2 commits into from
Oct 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions src/main/java/com/dtsx/astra/cli/config/SetupCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import com.github.rvesse.airline.annotations.Command;
import com.github.rvesse.airline.annotations.Option;

import java.io.Console;
import java.util.Arrays;
import java.util.Scanner;

import static com.dtsx.astra.cli.core.out.AstraAnsiColors.CYAN_400;
Expand All @@ -51,19 +53,29 @@ public class SetupCmd extends AbstractCmd {
public void execute() {
if (tokenParam == null || tokenParam.isBlank()) {
verbose = true;
String token;
String token = null;
AstraCliConsole.banner();
try(Scanner scanner = new Scanner(System.in)) {
boolean validToken = false;
while (!validToken) {
AstraCliConsole.println("$ Enter an Astra token:", CYAN_400);
token = removeQuotesIfAny(scanner.nextLine());
try {
createDefaultSection(token);
validToken = true;
} catch(InvalidTokenException ite) {
LoggerShell.error("Your token in invalid please retry " + ite.getMessage());
boolean validToken = false;
Console cons;
char[] tokenFromConsole;
while (!validToken) {
try {
if ((cons = System.console()) != null &&
(tokenFromConsole = cons.readPassword("[%s]", "$ Enter an Astra token:")) != null) {
token = String.valueOf(tokenFromConsole);

// Clear the password from memory immediately when done
Arrays.fill(tokenFromConsole, ' ');
} else {
try (Scanner scanner = new Scanner(System.in)) {
AstraCliConsole.println("$ Enter an Astra token:", CYAN_400);
token = scanner.nextLine();
}
}
createDefaultSection(removeQuotesIfAny(token));
validToken = true;
} catch(InvalidTokenException ite) {
LoggerShell.error("Your token in invalid please retry " + ite.getMessage());
}
}
} else {
Expand Down
Loading