Skip to content

Instantly share code, notes, and snippets.

@wingleung
Created July 17, 2018 15:58
Show Gist options
  • Save wingleung/a33a2ddcdadf487e0ba7d2749e946819 to your computer and use it in GitHub Desktop.
Save wingleung/a33a2ddcdadf487e0ba7d2749e946819 to your computer and use it in GitHub Desktop.
Generates an Authentication Token from username and password for basic authentication
'use strict';
const ENCODING = 'base64';
function token(user, pass) {
return base64([user, pass].join(':'));
}
function base64(string) {
return new Buffer(string).toString(ENCODING);
}
const args = process.argv.slice(2);
const username = args[0] ? args[0] : null;
const password = args[1] ? args[1] : null;
if (!username) {
console.log('please provide a username as first argument');
return;
}
if (!password) {
console.log('please provide a password as second argument');
return;
}
const authToken = token(username, password);
console.log(authToken);
@wingleung
Copy link
Author

Usage

chmod +x generateToken.js
node generateToken.js username password
# output: dXNlcm5hbWU6cGFzc3dvcmQ=

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment