update auth example to utilize cores pbkdf2
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by TJ Holowaychuk · committed by TJ Holowaychuk · parent 0f5560e · Browse files · 2 files·+63 added−19 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+63 added−19 removed
- other:+63 added−19 removed
Provenance
- Commit
26fab2a- Tree
b1cc7af- Author
TJ Holowaychuk <tj@vision-media.ca>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
0f5560e
2 files changed+63 added−19 removed
modified:examples/auth/app.js+19 added-19 removed
2 unmodified lines34567789101114 unmodified lines26272829303132303132343536373833343541424344454647363738394041424344454647484950515253545558596056575859606162636457 unmodified lines2 unmodified lines * Module dependencies. */
var express = require('../../lib/express') , crypto = require('crypto'); , hash = require('./pass').hash;
var app = module.exports = express();
app.use(express.bodyParser());14 unmodified lines if (err) res.locals.message = '<p class="msg error">' + err + '</p>'; if (msg) res.locals.message = '<p class="msg success">' + msg + '</p>';})
// Generate a salt for the user to prevent rainbow table attacks// for better security take a look at the bcrypt c++ addon:// https://github.com/ncb000gt/node.bcrypt.js// dummy database
var users = { tj: { name: 'tj' , salt: 'randomly-generated-salt' , pass: hash('foobar', 'randomly-generated-salt') } tj: { name: 'tj' }};
// Used to generate a hash of the plain-text password + saltfunction hash(msg, key) { return crypto .createHmac('sha256', key) .update(msg) .digest('hex');}// when you create a user, generate a salt// and hash the password ('foobar' is the pass here)
hash('foobar', function(err, salt, hash){ if (err) throw err; // store the salt & hash in the "db" users.tj.salt = salt; users.tj.hash = hash;});
// Authenticate using our plain-object database of doom!function authenticate(name, pass, fn) { if (!module.parent) console.log('authenticating %s:%s', name, pass); var user = users[name]; // query the db for the given username if (!user) return fn(new Error('cannot find user')); // apply the same algorithm to the POSTed password, applying // the hash against the pass / salt, if there is a match we // found the user if (user.pass == hash(pass, user.salt)) return fn(null, user); // Otherwise password is invalid fn(new Error('invalid password')); hash(pass, user.salt, function(err, hash){ if (err) return fn(err); if (hash == user.hash) return fn(null, user); fn(new Error('invalid password')); })}
function restrict(req, res, next) { if (req.session.user) {57 unmodified linesadded:examples/auth/pass.js+44 added
1234567891011121314151617181920212223242526272829303132333435363738394041424344
/** * Module dependencies. */
var crypto = require('crypto');
/** * Bytesize. */
var len = 128;
/** * Iterations. ~300ms */
var iterations = 12000;
/** * Hashes a password with optional `salt`, otherwise * generate a salt for `pass` and invoke `fn(err, salt, hash)`. * * @param {String} password to hash * @param {String} optional salt * @param {Function} callback * @api public */
exports.hash = function (pwd, salt, fn) { if (3 == arguments.length) { crypto.pbkdf2(pwd, salt, iterations, len, fn); } else { fn = salt; crypto.randomBytes(len, function(err, salt){ if (err) return fn(err); salt = salt.toString('base64'); crypto.pbkdf2(pwd, salt, iterations, len, function(err, hash){ if (err) return fn(err); fn(null, salt, hash); }); }); }};