Advertisement
Guest User

Untitled

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