Created
May 5, 2016 14:29
-
-
Save danielabel/ca335911d4663fd85f79378328902fd2 to your computer and use it in GitHub Desktop.
Code for use with https://nodejs.org/en/docs/guides/simple-profiling/
This file contains 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
var express = require('express'); | |
var crypto = require('crypto'); | |
var app = express(); | |
var users = []; | |
app.get('/', function (req, res) { | |
res.send('Hello World!'); | |
}); | |
app.get('/newUser', function (req, res) { | |
var username = req.query.username || ''; | |
var password = req.query.password || ''; | |
username = username.replace(/[!@#$%^&*]/g, ''); | |
if (!username || !password || users[username]) { | |
return res.sendStatus(400); | |
} | |
var salt = crypto.randomBytes(128).toString('base64'); | |
var hash = crypto.pbkdf2Sync(password, salt, 10000, 512); | |
users[username] = { | |
salt: salt, | |
hash: hash | |
}; | |
res.sendStatus(200); | |
}); | |
app.get('/auth', function (req, res) { | |
var username = req.query.username || ''; | |
var password = req.query.password || ''; | |
username = username.replace(/[!@#$%^&*]/g, ''); | |
if (!username || !password || !users[username]) { | |
return res.sendStatus(400); | |
} | |
var hash = crypto.pbkdf2Sync(password, users[username].salt, 10000, 512); | |
if (users[username].hash.toString() === hash.toString()) { | |
res.sendStatus(200); | |
} else { | |
res.sendStatus(401); | |
} | |
}); | |
app.listen(8080, function () { | |
console.log('Example app listening on port 8080!'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment