Skip to content

Commit 49e0d55

Browse files
authored
Merge pull request #116 from msmygit/feature/mask_token
Mask token value during input from user
2 parents 68eb41b + 4431a6e commit 49e0d55

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

src/main/java/com/dtsx/astra/cli/config/SetupCmd.java

+23-11
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import com.github.rvesse.airline.annotations.Command;
2929
import com.github.rvesse.airline.annotations.Option;
3030

31+
import java.io.Console;
32+
import java.util.Arrays;
3133
import java.util.Scanner;
3234

3335
import static com.dtsx.astra.cli.core.out.AstraAnsiColors.CYAN_400;
@@ -51,19 +53,29 @@ public class SetupCmd extends AbstractCmd {
5153
public void execute() {
5254
if (tokenParam == null || tokenParam.isBlank()) {
5355
verbose = true;
54-
String token;
56+
String token = null;
5557
AstraCliConsole.banner();
56-
try(Scanner scanner = new Scanner(System.in)) {
57-
boolean validToken = false;
58-
while (!validToken) {
59-
AstraCliConsole.println("$ Enter an Astra token:", CYAN_400);
60-
token = removeQuotesIfAny(scanner.nextLine());
61-
try {
62-
createDefaultSection(token);
63-
validToken = true;
64-
} catch(InvalidTokenException ite) {
65-
LoggerShell.error("Your token in invalid please retry " + ite.getMessage());
58+
boolean validToken = false;
59+
Console cons;
60+
char[] tokenFromConsole;
61+
while (!validToken) {
62+
try {
63+
if ((cons = System.console()) != null &&
64+
(tokenFromConsole = cons.readPassword("[%s]", "$ Enter an Astra token:")) != null) {
65+
token = String.valueOf(tokenFromConsole);
66+
67+
// Clear the password from memory immediately when done
68+
Arrays.fill(tokenFromConsole, ' ');
69+
} else {
70+
try (Scanner scanner = new Scanner(System.in)) {
71+
AstraCliConsole.println("$ Enter an Astra token:", CYAN_400);
72+
token = scanner.nextLine();
73+
}
6674
}
75+
createDefaultSection(removeQuotesIfAny(token));
76+
validToken = true;
77+
} catch(InvalidTokenException ite) {
78+
LoggerShell.error("Your token in invalid please retry " + ite.getMessage());
6779
}
6880
}
6981
} else {

0 commit comments

Comments
 (0)