|
| 1 | +'use strict' |
| 2 | + |
| 3 | +/** |
| 4 | + * Module dependencies. |
| 5 | + */ |
| 6 | + |
| 7 | +var express = require("express"); |
| 8 | +var hash = require('pbkdf2-password')() |
| 9 | +var path = require('path'); |
| 10 | +var session = require('express-session'); |
| 11 | + |
| 12 | +var app = module.exports = express(); |
| 13 | + |
| 14 | +// config |
| 15 | + |
| 16 | +app.set('view engine', 'ejs'); |
| 17 | +app.set('views', path.join(__dirname, 'views')); |
| 18 | + |
| 19 | +// middleware |
| 20 | + |
| 21 | +app.use(express.urlencoded({ extended: false })) |
| 22 | +app.use(session({ |
| 23 | + resave: false, // don't save session if unmodified |
| 24 | + saveUninitialized: false, // don't create session until something stored |
| 25 | + secret: 'shhhh, very secret' |
| 26 | +})); |
| 27 | + |
| 28 | +// Session-persisted message middleware |
| 29 | + |
| 30 | +app.use(function(req, res, next){ |
| 31 | + var err = req.session.error; |
| 32 | + var msg = req.session.success; |
| 33 | + delete req.session.error; |
| 34 | + delete req.session.success; |
| 35 | + res.locals.message = ''; |
| 36 | + if (err) res.locals.message = '<p class="msg error">' + err + '</p>'; |
| 37 | + if (msg) res.locals.message = '<p class="msg success">' + msg + '</p>'; |
| 38 | + next(); |
| 39 | +}); |
| 40 | + |
| 41 | +// dummy database |
| 42 | + |
| 43 | +var users = { |
| 44 | + tj: { name: 'tj' } |
| 45 | +}; |
| 46 | + |
| 47 | +// when you create a user, generate a salt |
| 48 | +// and hash the password ('foobar' is the pass here) |
| 49 | + |
| 50 | +hash({ password: 'foobar' }, function (err, pass, salt, hash) { |
| 51 | + if (err) throw err; |
| 52 | + // store the salt & hash in the "db" |
| 53 | + users.tj.salt = salt; |
| 54 | + users.tj.hash = hash; |
| 55 | +}); |
| 56 | + |
| 57 | + |
| 58 | +// Authenticate using our plain-object database of doom! |
| 59 | + |
| 60 | +function authenticate(name, pass, fn) { |
| 61 | + if (!module.parent) console.log('authenticating %s:%s', name, pass); |
| 62 | + var user = users[name]; |
| 63 | + // query the db for the given username |
| 64 | + if (!user) return fn(null, null) |
| 65 | + // apply the same algorithm to the POSTed password, applying |
| 66 | + // the hash against the pass / salt, if there is a match we |
| 67 | + // found the user |
| 68 | + hash({ password: pass, salt: user.salt }, function (err, pass, salt, hash) { |
| 69 | + if (err) return fn(err); |
| 70 | + if (hash === user.hash) return fn(null, user) |
| 71 | + fn(null, null) |
| 72 | + }); |
| 73 | +} |
| 74 | + |
| 75 | +function restrict(req, res, next) { |
| 76 | + if (req.session.user) { |
| 77 | + next(); |
| 78 | + } else { |
| 79 | + req.session.error = 'Access denied!'; |
| 80 | + res.redirect('/login'); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +app.get('/', function(req, res){ |
| 85 | + res.redirect('/login'); |
| 86 | +}); |
| 87 | + |
| 88 | +app.get('/restricted', restrict, function(req, res){ |
| 89 | + res.send('Wahoo! restricted area, click to <a href="/logout">logout</a>'); |
| 90 | +}); |
| 91 | + |
| 92 | +app.get('/logout', function(req, res){ |
| 93 | + // destroy the user's session to log them out |
| 94 | + // will be re-created next request |
| 95 | + req.session.destroy(function(){ |
| 96 | + res.redirect('/'); |
| 97 | + }); |
| 98 | +}); |
| 99 | + |
| 100 | +app.get('/login', function(req, res){ |
| 101 | + res.render('login'); |
| 102 | +}); |
| 103 | + |
| 104 | +app.post('/login', function (req, res, next) { |
| 105 | + authenticate(req.body.username, req.body.password, function(err, user){ |
| 106 | + if (err) return next(err) |
| 107 | + if (user) { |
| 108 | + // Regenerate session when signing in |
| 109 | + // to prevent fixation |
| 110 | + req.session.regenerate(function(){ |
| 111 | + // Store the user's primary key |
| 112 | + // in the session store to be retrieved, |
| 113 | + // or in this case the entire user object |
| 114 | + req.session.user = user; |
| 115 | + req.session.success = 'Authenticated as ' + user.name |
| 116 | + + ' click to <a href="/logout">logout</a>. ' |
| 117 | + + ' You may now access <a href="/restricted">/restricted</a>.'; |
| 118 | + res.redirect('back'); |
| 119 | + }); |
| 120 | + } else { |
| 121 | + req.session.error = 'Authentication failed, please check your ' |
| 122 | + + ' username and password.' |
| 123 | + + ' (use "tj" and "foobar")'; |
| 124 | + res.redirect('/login'); |
| 125 | + } |
| 126 | + }); |
| 127 | +}); |
| 128 | + |
| 129 | +/* istanbul ignore next */ |
| 130 | +if (!module.parent) { |
| 131 | + app.listen(3000); |
| 132 | + console.log('Express started on port 3000'); |
| 133 | +} |
0 commit comments