Skip to content
Myr

app.js

This file

Path

examples/auth/app.js

126 lines · 3.3 KiB · JavaScript

Last changed by

Renaming usage of old connect middlewareTj Holowaychukbefore adoption2a7a5ae

History of this file

Take it

The walk stopped after 200 commits: lines older than ce2a18f point at it.

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

lines 1–8why, one step back
1
2// Expose modules in ./support for demo purposes3require.paths.unshift(__dirname + '/../../support');45/**6 * Module dependencies.7 */8

refactored auth exampleTj Holowaychukbefore adoption8d7e35b

lines 9–10why, one step back
9var express = require('../../lib/express')
10 , crypto = require('crypto');

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

lines 11–16why, one step back
11
12var app = express.createServer();1314app.set('views', __dirname + '/views');15app.set('view engine', 'ejs');16

Renaming usage of old connect middlewareTj Holowaychukbefore adoption2a7a5ae

lines 17–18why, one step back
17app.use(express.bodyParser());
18app.use(express.cookieParser());

Added secret to session middleware used in examples and generated appTj Holowaychukbefore adoption7687299

line 19why, one step back
19app.use(express.session({ secret: 'keyboard cat' }));

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

lines 20–24why, one step back
20
21// 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 back
25 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 }

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

lines 33–37why, one step back
33});
3435// Generate a salt for the user to prevent rainbow table attacks3637var users = {

refactored auth exampleTj Holowaychukbefore adoption8d7e35b

lines 38–42why, one step back
38 tj: {
39 name: 'tj'40 , salt: 'randomly-generated-salt'41 , pass: md5('foobar' + 'randomly-generated-salt')42 }

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

lines 43–47why, one step back
43};
4445// Used to generate a hash of the plain-text password + salt4647function md5(str) {

refactored auth exampleTj Holowaychukbefore adoption8d7e35b

line 48why, one step back
48 return crypto.createHash('md5').update(str).digest('hex');

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

lines 49–53why, one step back
49}
5051// Authenticate using our plain-object database of doom!5253function authenticate(name, pass, fn) {

refactored auth exampleTj Holowaychukbefore adoption8d7e35b

lines 54–62why, one step back
54 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'));

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

lines 63–74why, one step back
63}
6465function 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 back
75 console.log('/restricted accessed by %s', req.session.user.name);
76 next();

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

lines 77–79why, one step back
77}
7879app.get('/', function(req, res){

refactored auth exampleTj Holowaychukbefore adoption8d7e35b

line 80why, one step back
80 res.redirect('/login');

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

lines 81–83why, one step back
81});
8283app.get('/restricted', restrict, accessLogger, function(req, res){

refactored auth exampleTj Holowaychukbefore adoption8d7e35b

line 84why, one step back
84 res.send('Wahoo! restricted area');

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

lines 85–87why, one step back
85});
8687app.get('/logout', function(req, res){

refactored auth exampleTj Holowaychukbefore adoption8d7e35b

lines 88–92why, one step back
88 // destroy the user's session to log them out
89 // will be re-created next request90 req.session.destroy(function(){91 res.redirect('home');92 });

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

lines 93–95why, one step back
93});
9495app.get('/login', function(req, res){

refactored auth exampleTj Holowaychukbefore adoption8d7e35b

lines 96–101why, one step back
96 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');

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

lines 102–104why, one step back
102});
103104app.post('/login', function(req, res){

refactored auth exampleTj Holowaychukbefore adoption8d7e35b

lines 105–122why, one step back
105 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 });

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

lines 123–126why, one step back
123});
124125app.listen(3000);126console.log('Express started on port 3000');