Skip to content
Myr

app.js

This file

Path

examples/auth/app.js

121 lines · 3.2 KiB · JavaScript

Last changed by

refactoring auth exampleTJ Holowaychukbefore adoption7048be2

History of this file

Take it

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

Added app.del() testTj Holowaychukbefore adoption81652c8

lines 1–8why, one step back
1
2/**3 * Module dependencies.4 */56var express = require('../../lib/express')7 , crypto = require('crypto');8

Merge branch 'integration'TJ Holowaychukbefore adoption0ae024a

line 9why, one step back
9var app = module.exports = express();

Added app.del() testTj Holowaychukbefore adoption81652c8

line 10why, one step back
10

Merge branch 'integration'TJ Holowaychukbefore adoption0ae024a

lines 11–14why, one step back
11app.use(express.bodyParser());
12app.use(express.cookieParser('shhhh, very secret'));13app.use(express.session());14

Added app.del() testTj Holowaychukbefore adoption81652c8

line 15why, one step back
15app.set('view engine', 'ejs');

Merge branch 'integration'TJ Holowaychukbefore adoption0ae024a

line 16why, one step back
16app.set('views', __dirname + '/views');

Added app.del() testTj Holowaychukbefore adoption81652c8

line 17why, one step back
17

Merge branch 'integration'TJ Holowaychukbefore adoption0ae024a

line 18why, one step back
18// Session-persisted message middleware

Added app.del() testTj Holowaychukbefore adoption81652c8

line 19why, one step back
19

Merge branch 'integration'TJ Holowaychukbefore adoption0ae024a

lines 20–28why, one step back
20app.locals.use(function(req,res){
21 var err = req.session.error22 , msg = req.session.success;23 delete req.session.error;24 delete req.session.success;25 res.locals.message = '';26 if (err) res.locals.message = '<p class="msg error">' + err + '</p>';27 if (msg) res.locals.message = '<p class="msg success">' + msg + '</p>';28})

Added app.del() testTj Holowaychukbefore adoption81652c8

lines 29–34why, one step back
29
30// Generate a salt for the user to prevent rainbow table attacks31// for better security take a look at the bcrypt c++ addon:32// https://github.com/ncb000gt/node.bcrypt.js33var users = {34 tj: {

Merge branch 'integration'TJ Holowaychukbefore adoption0ae024a

line 35why, one step back
35 name: 'tj'

Added app.del() testTj Holowaychukbefore adoption81652c8

lines 36–42why, one step back
36 , salt: 'randomly-generated-salt'
37 , pass: hash('foobar', 'randomly-generated-salt')38 }39};4041// Used to generate a hash of the plain-text password + salt42function hash(msg, key) {

refactoring auth exampleTJ Holowaychukbefore adoption7048be2

lines 43–46why, one step back
43 return crypto
44 .createHmac('sha256', key)45 .update(msg)46 .digest('hex');

Added app.del() testTj Holowaychukbefore adoption81652c8

lines 47–48why, one step back
47}
48

Merge branch 'integration'TJ Holowaychukbefore adoption0ae024a

line 49why, one step back
49// Authenticate using our plain-object database of doom!

Added app.del() testTj Holowaychukbefore adoption81652c8

line 50why, one step back
50function authenticate(name, pass, fn) {

refactoring auth exampleTJ Holowaychukbefore adoption7048be2

line 51why, one step back
51 console.log('authenticating %s:%s', name, pass);

Added app.del() testTj Holowaychukbefore adoption81652c8

lines 52–72why, one step back
52 var user = users[name];
53 // query the db for the given username54 if (!user) return fn(new Error('cannot find user'));55 // apply the same algorithm to the POSTed password, applying56 // the hash against the pass / salt, if there is a match we57 // found the user58 if (user.pass == hash(pass, user.salt)) return fn(null, user);59 // Otherwise password is invalid60 fn(new Error('invalid password'));61}6263function restrict(req, res, next) {64 if (req.session.user) {65 next();66 } else {67 req.session.error = 'Access denied!';68 res.redirect('/login');69 }70}7172app.get('/', function(req, res){

Merge branch 'integration'TJ Holowaychukbefore adoption0ae024a

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

Added app.del() testTj Holowaychukbefore adoption81652c8

lines 74–75why, one step back
74});
75

Merge branch 'integration'TJ Holowaychukbefore adoption0ae024a

line 76why, one step back
76app.get('/restricted', restrict, function(req, res){

Added app.del() testTj Holowaychukbefore adoption81652c8

lines 77–83why, one step back
77 res.send('Wahoo! restricted area');
78});7980app.get('/logout', function(req, res){81 // destroy the user's session to log them out82 // will be re-created next request83 req.session.destroy(function(){

Merge branch 'integration'TJ Holowaychukbefore adoption0ae024a

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

Added app.del() testTj Holowaychukbefore adoption81652c8

lines 85–112why, one step back
85 });
86});8788app.get('/login', function(req, res){89 if (req.session.user) {90 req.session.success = 'Authenticated as ' + req.session.user.name91 + ' click to <a href="/logout">logout</a>. '92 + ' You may now access <a href="/restricted">/restricted</a>.';93 }94 res.render('login');95});9697app.post('/login', function(req, res){98 authenticate(req.body.username, req.body.password, function(err, user){99 if (user) {100 // Regenerate session when signing in101 // to prevent fixation 102 req.session.regenerate(function(){103 // Store the user's primary key 104 // in the session store to be retrieved,105 // or in this case the entire user object106 req.session.user = user;107 res.redirect('back');108 });109 } else {110 req.session.error = 'Authentication failed, please check your '111 + ' username and password.'112 + ' (use "tj" and "foobar")';

Merge branch 'integration'TJ Holowaychukbefore adoption0ae024a

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

Added app.del() testTj Holowaychukbefore adoption81652c8

lines 114–117why, one step back
114 }
115 });116});117

Merge branch 'integration'TJ Holowaychukbefore adoption0ae024a

lines 118–121why, one step back
118if (!module.parent) {
119 app.listen(3000);120 console.log('Express started on port 3000');121}