Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide password in the nodejs console?

I want to hide password input. I see many answers in stackoverflow but I can't verify value if I press backspace. The condition return false.

I tried several solution to overwrite the function but I got an issue with buffer if I press backspace, I got invisible character \b.

I press : "A", backspace, "B", I have in my buffer this : "\u0041\u0008\u0042" (toString() = 'A\bB') and not "B".

I have :

var readline = require('readline');  var rl = readline.createInterface({     input: process.stdin,     output: process.stdout });  rl.question("password : ", function(password) {     console.log("Your password : " + password); }); 
like image 211
Sky Voyager Avatar asked Jun 04 '14 12:06

Sky Voyager


People also ask

How do I clear the node JS console log?

clear() operates like the clear shell command in terminal. On Windows, console. clear() will clear the current terminal viewport for the Node.

How do I use console commands in node JS?

Unit Testing and Test Driven Development in NodeJS Node. js console is a global object and is used to print different levels of messages to stdout and stderr. There are built-in methods to be used for printing informational, warning, and error messages.

What is the use of console log () method in node JS?

log() function from console class of Node. js is used to display the messages on the console. It prints to stdout with newline. Parameter: This function contains multiple parameters which are to be printed.

How does NodeJS validate username and password?

body. username; var password = sReq. body. password; if (username=='myusername' && password == 'mypassword') { // do something here with a valid login } else { // user or password doesn't match } });


2 Answers

This can be handled with readline by intercepting the output through a muted stream, as is done in the read project on npm (https://github.com/isaacs/read/blob/master/lib/read.js):

var readline = require('readline'); var Writable = require('stream').Writable;  var mutableStdout = new Writable({   write: function(chunk, encoding, callback) {     if (!this.muted)       process.stdout.write(chunk, encoding);     callback();   } });  mutableStdout.muted = false;  var rl = readline.createInterface({   input: process.stdin,   output: mutableStdout,   terminal: true });  rl.question('Password: ', function(password) {   console.log('\nPassword is ' + password);   rl.close(); });  mutableStdout.muted = true; 
like image 74
guybedford Avatar answered Oct 07 '22 02:10

guybedford


Overwrite _writeToOutput of application's readline interface : https://github.com/nodejs/node/blob/v9.5.0/lib/readline.js#L291

To hide your password input, you can use :

FIRST SOLUTION : "password : [=-]"

This solution has animation when you press a touch :

password : [-=] password : [=-] 

The code :

var readline = require('readline');  var rl = readline.createInterface({   input: process.stdin,   output: process.stdout });  rl.stdoutMuted = true;  rl.query = "Password : "; rl.question(rl.query, function(password) {   console.log('\nPassword is ' + password);   rl.close(); });  rl._writeToOutput = function _writeToOutput(stringToWrite) {   if (rl.stdoutMuted)     rl.output.write("\x1B[2K\x1B[200D"+rl.query+"["+((rl.line.length%2==1)?"=-":"-=")+"]");   else     rl.output.write(stringToWrite); }; 

This sequence "\x1B[2K\x1BD" uses two escapes sequences :

  • Esc [2K : clear entire line.
  • Esc D : move/scroll window up one line.

To learn more, read this : http://ascii-table.com/ansi-escape-sequences-vt-100.php

SECOND SOLUTION : "password : ****"

var readline = require('readline');  var rl = readline.createInterface({   input: process.stdin,   output: process.stdout });  rl.stdoutMuted = true;  rl.question('Password: ', function(password) {   console.log('\nPassword is ' + password);   rl.close(); });  rl._writeToOutput = function _writeToOutput(stringToWrite) {   if (rl.stdoutMuted)     rl.output.write("*");   else     rl.output.write(stringToWrite); }; 

You can clear history with :

rl.history = rl.history.slice(1); 
like image 30
Sky Voyager Avatar answered Oct 07 '22 02:10

Sky Voyager