Created
June 30, 2017 23:59
-
-
Save axelav/9b9840d7516fb2c21adf628fe61800ab to your computer and use it in GitHub Desktop.
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
const fs = require('fs') | |
const SecurePassword = require('secure-password') | |
const pwd = SecurePassword() | |
const command = process.argv[2] | |
const inputUsername = process.argv[3] | |
const inputPassword = Buffer.from(process.argv[4]) | |
if (command === 'createUser') return createUser(inputUsername, inputPassword) | |
if (command === 'verify') return verify(inputUsername, inputPassword) | |
function createUser (username, password) { | |
pwd.hash(password, (err, hash) => { | |
if (err) throw err | |
fs.writeFileSync('./users.csv', `${username},${password}`) | |
console.log('user created') | |
}) | |
} | |
function verify (username, password) { | |
pwd.hash(password, (err, hash) => { | |
if (err) throw err | |
const userData = fs.readFileSync('./users.csv').toString().split(',') | |
const foundUsername = userData[0] | |
const foundPassword = Buffer.from(userData[1]) | |
if (username === foundUsername) { | |
return pwd.verify(foundPassword, hash, (err, result) => { | |
if (err) throw err | |
if (result === SecurePassword.INVALID) return console.log('wrong password') | |
if (result === SecurePassword.VALID) return console.log('ACCESS GRANTED') | |
if (result === SecurePassword.VALID_NEEDS_REHASH) { | |
console.log('making yer pw safer') | |
pwd.hash(userPassword, (err, improvedHash) => { | |
if (err) console.error('authd but yer pw aint safer') | |
}) | |
} | |
}) | |
} | |
return console.log('no user found') | |
}) | |
} |
Author
axelav
commented
Jul 1, 2017
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment