app.js
This file
Path
examples/auth/app.js
126 lines · 3.3 KiB · JavaScript
Last changed by
refactored auth exampleTj Holowaychukbefore adoption8d7e35b
History of this fileTake it
The walk stopped after 200 commits: lines older than f179979 point at it.
Regenerated guideTj Holowaychukbefore adoptionf179979
lines 1–8why, one step back12// Expose modules in ./support for demo purposes3require.paths.unshift(__dirname + '/../../support');45/**6 * Module dependencies.7 */8refactored auth exampleTj Holowaychukbefore adoption8d7e35b
lines 9–10why, one step back9var express = require('../../lib/express')10 , crypto = require('crypto');Regenerated guideTj Holowaychukbefore adoptionf179979
lines 11–24why, one step back1112var app = express.createServer();1314app.set('views', __dirname + '/views');15app.set('view engine', 'ejs');1617app.use(express.bodyDecoder());18app.use(express.cookieDecoder());19app.use(express.session());2021// Message helper, ideally we would use req.flash()22// however this is more light-weight for an example2324app.dynamicHelpers({refactored auth exampleTj Holowaychukbefore adoption8d7e35b
lines 25–32why, one step back25 message: function(req){26 var err = req.session.error27 , msg = req.session.success;28 delete req.session.error;29 delete req.session.success;30 if (err) return '<p class="msg error">' + err + '</p>';31 if (msg) return '<p class="msg success">' + msg + '</p>';32 }Regenerated guideTj Holowaychukbefore adoptionf179979
lines 33–37why, one step back33});3435// Generate a salt for the user to prevent rainbow table attacks3637var users = {refactored auth exampleTj Holowaychukbefore adoption8d7e35b
lines 38–42why, one step back38 tj: {39 name: 'tj'40 , salt: 'randomly-generated-salt'41 , pass: md5('foobar' + 'randomly-generated-salt')42 }Regenerated guideTj Holowaychukbefore adoptionf179979
lines 43–47why, one step back43};4445// Used to generate a hash of the plain-text password + salt4647function md5(str) {refactored auth exampleTj Holowaychukbefore adoption8d7e35b
line 48why, one step back48 return crypto.createHash('md5').update(str).digest('hex');Regenerated guideTj Holowaychukbefore adoptionf179979
lines 49–53why, one step back49}5051// Authenticate using our plain-object database of doom!5253function authenticate(name, pass, fn) {refactored auth exampleTj Holowaychukbefore adoption8d7e35b
lines 54–62why, one step back54 var user = users[name];55 // query the db for the given username56 if (!user) return fn(new Error('cannot find user'));57 // apply the same algorithm to the POSTed password, applying58 // the md5 against the pass / salt, if there is a match we59 // found the user60 if (user.pass == md5(pass + user.salt)) return fn(null, user);61 // Otherwise password is invalid62 fn(new Error('invalid password'));Regenerated guideTj Holowaychukbefore adoptionf179979
lines 63–64why, one step back63}64Merge branch 'features/route-middleware'Tj Holowaychukbefore adoptionae75153
lines 65–74why, one step back65function restrict(req, res, next) {66 if (req.session.user) {67 next();68 } else {69 req.session.error = 'Access denied!';70 res.redirect('/login');71 }72}7374function accessLogger(req, res, next) {refactored auth exampleTj Holowaychukbefore adoption8d7e35b
lines 75–76why, one step back75 console.log('/restricted accessed by %s', req.session.user.name);76 next();Merge branch 'features/route-middleware'Tj Holowaychukbefore adoptionae75153
lines 77–78why, one step back77}78Regenerated guideTj Holowaychukbefore adoptionf179979
line 79why, one step back79app.get('/', function(req, res){refactored auth exampleTj Holowaychukbefore adoption8d7e35b
line 80why, one step back80 res.redirect('/login');Regenerated guideTj Holowaychukbefore adoptionf179979
lines 81–82why, one step back81});82Merge branch 'features/route-middleware'Tj Holowaychukbefore adoptionae75153
line 83why, one step back83app.get('/restricted', restrict, accessLogger, function(req, res){refactored auth exampleTj Holowaychukbefore adoption8d7e35b
line 84why, one step back84 res.send('Wahoo! restricted area');Merge branch 'features/route-middleware'Tj Holowaychukbefore adoptionae75153
lines 85–86why, one step back85});86Regenerated guideTj Holowaychukbefore adoptionf179979
line 87why, one step back87app.get('/logout', function(req, res){refactored auth exampleTj Holowaychukbefore adoption8d7e35b
lines 88–92why, one step back88 // destroy the user's session to log them out89 // will be re-created next request90 req.session.destroy(function(){91 res.redirect('home');92 });Regenerated guideTj Holowaychukbefore adoptionf179979
lines 93–95why, one step back93});9495app.get('/login', function(req, res){refactored auth exampleTj Holowaychukbefore adoption8d7e35b
lines 96–101why, one step back96 if (req.session.user) {97 req.session.success = 'Authenticated as ' + req.session.user.name98 + ' click to <a href="/logout">logout</a>. '99 + ' You may now access <a href="/restricted">/restricted</a>.';100 }101 res.render('login');Regenerated guideTj Holowaychukbefore adoptionf179979
lines 102–104why, one step back102});103104app.post('/login', function(req, res){refactored auth exampleTj Holowaychukbefore adoption8d7e35b
lines 105–122why, one step back105 authenticate(req.body.username, req.body.password, function(err, user){106 if (user) {107 // Regenerate session when signing in108 // to prevent fixation 109 req.session.regenerate(function(){110 // Store the user's primary key 111 // in the session store to be retrieved,112 // or in this case the entire user object113 req.session.user = user;114 res.redirect('back');115 });116 } else {117 req.session.error = 'Authentication failed, please check your '118 + ' username and password.'119 + ' (use "tj" and "foobar")';120 res.redirect('back');121 }122 });Regenerated guideTj Holowaychukbefore adoptionf179979
lines 123–126why, one step back123});124125app.listen(3000);126console.log('Express started on port 3000');