Last active
February 27, 2018 09:08
-
-
Save agreatfool/a55f5cbdd66d097ba4855e47d42338e5 to your computer and use it in GitHub Desktop.
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
{ | |
"name": "profile", | |
"version": "0.0.1", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "MIT", | |
"dependencies": { | |
"express": "^4.16.2" | |
} | |
} |
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
#!/usr/bin/env bash | |
curl -X GET "http://localhost:5000/newUser?username=matt&password=password" | |
ab -k -c 20 -n 250 "http://localhost:5000/auth?username=matt&password=password" |
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
#!/usr/bin/env bash | |
BASEDIR=$(dirname "$0") | |
cd ${BASEDIR}/../ | |
NODE_ENV=production node --prof profile/server.js |
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
'use strict'; | |
const crypto = require('crypto'); | |
const express = require('express'); | |
const app = express(); | |
let users = []; | |
app.get('/newUser', (req, res) => { | |
let username = req.query.username || ''; | |
const password = req.query.password || ''; | |
username = username.replace(/[!@#$%^&*]/g, ''); | |
if (!username || !password || users.username) { | |
return res.sendStatus(400); | |
} | |
const salt = crypto.randomBytes(128).toString('base64'); | |
const hash = crypto.pbkdf2Sync(password, salt, 10000, 512, 'sha512'); | |
users[username] = { salt, hash }; | |
res.sendStatus(200); | |
}); | |
app.get('/auth', (req, res) => { | |
let username = req.query.username || ''; | |
const password = req.query.password || ''; | |
username = username.replace(/[!@#$%^&*]/g, ''); | |
if (!username || !password || !users[username]) { | |
return res.sendStatus(400); | |
} | |
const hash = crypto.pbkdf2Sync(password, users[username].salt, 10000, 512, 'sha512'); | |
if (users[username].hash.toString() === hash.toString()) { | |
res.sendStatus(200); | |
} else { | |
res.sendStatus(401); | |
} | |
}); | |
app.listen(5000); | |
console.log('Server started, listening on port 5000 ...'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment