Advertisement
Guest User

Untitled

a guest
Jun 30th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. const fs = require('fs')
  2. const SecurePassword = require('secure-password')
  3.  
  4. const pwd = SecurePassword()
  5. const command = process.argv[2]
  6. const inputUsername = process.argv[3]
  7. const inputPassword = Buffer.from(process.argv[4])
  8.  
  9. if (command === 'createUser') return createUser(inputUsername, inputPassword)
  10. if (command === 'verify') return verify(inputUsername, inputPassword)
  11.  
  12. function createUser (username, password) {
  13. pwd.hash(password, (err, hash) => {
  14. if (err) throw err
  15.  
  16. fs.writeFileSync('./users.csv', `${username},${password}`)
  17. console.log('user created')
  18. })
  19. }
  20.  
  21. function verify (username, password) {
  22. pwd.hash(password, (err, hash) => {
  23. if (err) throw err
  24.  
  25. const userData = fs.readFileSync('./users.csv').toString().split(',')
  26. const foundUsername = userData[0]
  27. const foundPassword = Buffer.from(userData[1])
  28.  
  29. if (username === foundUsername) {
  30. return pwd.verify(foundPassword, hash, (err, result) => {
  31. if (err) throw err
  32.  
  33. if (result === SecurePassword.INVALID) return console.log('wrong password')
  34. if (result === SecurePassword.VALID) return console.log('ACCESS GRANTED')
  35. if (result === SecurePassword.VALID_NEEDS_REHASH) {
  36. console.log('making yer pw safer')
  37.  
  38. pwd.hash(userPassword, (err, improvedHash) => {
  39. if (err) console.error('authd but yer pw aint safer')
  40. })
  41. }
  42. })
  43. }
  44.  
  45. return console.log('no user found')
  46. })
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement