Created
July 17, 2018 15:58
-
-
Save wingleung/a33a2ddcdadf487e0ba7d2749e946819 to your computer and use it in GitHub Desktop.
Generates an Authentication Token from username and password for basic authentication
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage