Skip to content

Commit cc88997

Browse files
committed
Update Password.readPasswordFromConsole()
The Password.readPasswordFromConsole() has been modified to read the password from the standard input in case the system console is not available (e.g. in CI environment). Assisted-by: Gemini Code Assist
1 parent 7eb0818 commit cc88997

1 file changed

Lines changed: 31 additions & 1 deletion

File tree

base/src/main/java/org/mozilla/jss/util/Password.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,37 @@ public static void wipeChars(char[] charArray) {
286286
public static Password readPasswordFromConsole() throws PasswordCallback.GiveUpException {
287287

288288
Console console = System.console();
289-
char[] password = console.readPassword();
289+
290+
char[] password;
291+
if (console == null) {
292+
// no console, read password from standard input
293+
try {
294+
char[] buffer = new char[128];
295+
int len = 0;
296+
int c;
297+
298+
// read a single line into buffer
299+
while ((c = System.in.read()) != -1 && c != '\n' && c != '\r') {
300+
if (len == buffer.length) {
301+
// buffer full, resize buffer
302+
buffer = Arrays.copyOf(buffer, buffer.length * 2);
303+
}
304+
buffer[len++] = (char) c;
305+
}
306+
307+
// get password from buffer
308+
password = Arrays.copyOf(buffer, len);
309+
Arrays.fill(buffer, (char) 0);
310+
311+
} catch (java.io.IOException e) {
312+
logger.error("Unable to read password: " + e.getMessage(), e);
313+
password = null;
314+
}
315+
316+
} else {
317+
// read password from console
318+
password = console.readPassword();
319+
}
290320

291321
if (password == null || password.length == 0) {
292322
throw new PasswordCallback.GiveUpException();

0 commit comments

Comments
 (0)