Skip to content

Instantly share code, notes, and snippets.

@axelav
Created June 30, 2017 23:59
Show Gist options
  • Save axelav/9b9840d7516fb2c21adf628fe61800ab to your computer and use it in GitHub Desktop.
Save axelav/9b9840d7516fb2c21adf628fe61800ab to your computer and use it in GitHub Desktop.
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')
})
}
@axelav
Copy link
Author

axelav commented Jul 1, 2017

$ node index.js createUser axel whatever // "user created"
$ node index.js verify axel whatever // "ACCESS GRANTED"
$ node index.js verify axel notright // "wrong password"
$ node index.js verify whoami whatever // "no user found"

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