Skip to content

Commit

Permalink
Merge pull request #116 from msmygit/feature/mask_token
Browse files Browse the repository at this point in the history
Mask token value during input from user
  • Loading branch information
msmygit authored Oct 5, 2023
2 parents 68eb41b + 4431a6e commit 49e0d55
Showing 1 changed file with 23 additions and 11 deletions.
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

0 comments on commit 49e0d55

Please sign in to comment.