refactored auth example
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 1349eea · Browse files · 1 file·+58 added−58 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+58 added−58 removed
- other:+58 added−58 removed
Provenance
- Commit
8d7e35b- Tree
b910163- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
1349eea
1 file changed+58 added−58 removed
modified:examples/auth/app.js+58 added-58 removed
4 unmodified lines5678910910111213146 unmodified lines2122232425262728293031322526272829303132333435363738394041423839404142434445464748484950515253545556575859606162545556575859606162636465664 unmodified lines7172737475767576777879808081828384848586878889909192888990919293949596979899100101969798991001011021031041051061071081091101111121131141151161171181191201211221051061071081091101111121131141151161171181191201211221231241251264 unmodified lines/** * Module dependencies. */
var express = require('../../lib/express'), crypto = require('crypto');var express = require('../../lib/express') , crypto = require('crypto');
var app = express.createServer();
app.set('views', __dirname + '/views');6 unmodified lines// Message helper, ideally we would use req.flash()// however this is more light-weight for an example
app.dynamicHelpers({ message: function(req){ var err = req.session.error, msg = req.session.success; delete req.session.error; delete req.session.success; if (err) return '<p class="msg error">' + err + '</p>'; if (msg) return '<p class="msg success">' + msg + '</p>'; } message: function(req){ var err = req.session.error , msg = req.session.success; delete req.session.error; delete req.session.success; if (err) return '<p class="msg error">' + err + '</p>'; if (msg) return '<p class="msg success">' + msg + '</p>'; }});
// Generate a salt for the user to prevent rainbow table attacks
var users = { tj: { name: 'tj', salt: 'randomly-generated-salt', pass: md5('foobar' + 'randomly-generated-salt') } tj: { name: 'tj' , salt: 'randomly-generated-salt' , pass: md5('foobar' + 'randomly-generated-salt') }};
// Used to generate a hash of the plain-text password + salt
function md5(str) { return crypto.createHash('md5').update(str).digest('hex'); return crypto.createHash('md5').update(str).digest('hex');}
// Authenticate using our plain-object database of doom!
function authenticate(name, pass, fn) { 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 md5 against the pass / salt, if there is a match we // found the user if (user.pass == md5(pass + user.salt)) return fn(null, user); // Otherwise password is invalid fn(new Error('invalid password')); 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 md5 against the pass / salt, if there is a match we // found the user if (user.pass == md5(pass + user.salt)) return fn(null, user); // Otherwise password is invalid fn(new Error('invalid password'));}
function restrict(req, res, next) { if (req.session.user) {4 unmodified lines }}
function accessLogger(req, res, next) { console.log('/restricted accessed by %s', req.session.user.name); next(); console.log('/restricted accessed by %s', req.session.user.name); next();}
app.get('/', function(req, res){ res.redirect('/login'); res.redirect('/login');});
app.get('/restricted', restrict, accessLogger, function(req, res){ res.send('Wahoo! restricted area'); res.send('Wahoo! restricted area');});
app.get('/logout', function(req, res){ // destroy the user's session to log them out // will be re-created next request req.session.destroy(function(){ res.redirect('home'); }); // destroy the user's session to log them out // will be re-created next request req.session.destroy(function(){ res.redirect('home'); });});
app.get('/login', function(req, res){ if (req.session.user) { req.session.success = 'Authenticated as ' + req.session.user.name + ' click to <a href="/logout">logout</a>. ' + ' You may now access <a href="/restricted">/restricted</a>.'; } res.render('login'); if (req.session.user) { req.session.success = 'Authenticated as ' + req.session.user.name + ' click to <a href="/logout">logout</a>. ' + ' You may now access <a href="/restricted">/restricted</a>.'; } res.render('login');});
app.post('/login', function(req, res){ authenticate(req.body.username, req.body.password, function(err, user){ if (user) { // Regenerate session when signing in // to prevent fixation req.session.regenerate(function(){ // Store the user's primary key // in the session store to be retrieved, // or in this case the entire user object req.session.user = user; res.redirect('back'); }); } else { req.session.error = 'Authentication failed, please check your ' + ' username and password.' + ' (use "tj" and "foobar")'; res.redirect('back'); } }); authenticate(req.body.username, req.body.password, function(err, user){ if (user) { // Regenerate session when signing in // to prevent fixation req.session.regenerate(function(){ // Store the user's primary key // in the session store to be retrieved, // or in this case the entire user object req.session.user = user; res.redirect('back'); }); } else { req.session.error = 'Authentication failed, please check your ' + ' username and password.' + ' (use "tj" and "foobar")'; res.redirect('back'); } });});
app.listen(3000);console.log('Express started on port 3000');No newline at end of fileNo newline at end of file