Merge branch 'auth-example'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parents 8b5773c, 3876a56 · Browse files · 3 files·+139 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+139 added−0 removed
- other:+139 added−0 removed
Provenance
- Commit
5cf248c- Tree
41368a6- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent 1
8b5773c- Parent 2
3876a56
3 files changed+139 added−0 removed
added:examples/auth/views/layout.ejs+21 added
1+<!DOCTYPE html>2+<html>3+ <head>4+ <title>Authentication Example</title>5+ <style>6+ body {7+ padding: 50px;8+ font: 13px Helvetica, Arial, sans-serif;9+ }10+ .error {11+ color: red12+ }13+ .success {14+ color: green;15+ }16+ </style>17+ </head>18+ <body>19+ <%- body %>20+ </body>21+</html>added:examples/auth/views/login.ejs+15 added
1+<h1>Login</h1>2+<%- message %>3+<form method="post" action="/login">4+ <p>5+ <label>Username:</label>6+ <input type="text" name="username">7+ </p>8+ <p>9+ <label>Password:</label>10+ <input type="text" name="password">11+ </p>12+ <p>13+ <input type="submit" value="Login">14+ </p>15+</form>added:examples/auth/app.js+103 added
1+2+/**3+ * Module dependencies.4+ */5+6+var express = require('../../lib/express'),7+ crypto = require('crypto');8+9+var app = express.createServer();10+11+app.set('views', __dirname + '/views');12+app.set('view engine', 'ejs');13+14+app.use(express.bodyDecoder());15+app.use(express.cookieDecoder());16+app.use(express.session());17+18+// Message helper, ideally we would use req.flash()19+// however this is more light-weight for an example20+21+app.dynamicHelpers({22+ message: function(req){23+ var err = req.session.error,24+ msg = req.session.success;25+ delete req.session.error;26+ delete req.session.success;27+ if (err) return '<p class="msg error">' + err + '</p>';28+ if (msg) return '<p class="msg success">' + msg + '</p>';29+ }30+});31+32+// Generate a salt for the user to prevent rainbow table attacks33+34+var users = {35+ tj: {36+ name: 'tj',37+ salt: 'randomly-generated-salt',38+ pass: md5('foobar' + 'randomly-generated-salt')39+ }40+};41+42+// Used to generate a hash of the plain-text password + salt43+44+function md5(str) {45+ return crypto.createHash('md5').update(str).digest('hex');46+}47+48+// Authenticate using our plain-object database of doom!49+50+function authenticate(name, pass, fn) {51+ var user = users[name];52+ // query the db for the given username53+ if (!user) return fn(new Error('cannot find user'));54+ // apply the same algorithm to the POSTed password, applying55+ // the md5 against the pass / salt, if there is a match we56+ // found the user57+ if (user.pass == md5(pass + user.salt)) return fn(null, user);58+ // Otherwise password is invalid59+ fn(new Error('invalid password'));60+}61+62+app.get('/', function(req, res){63+ res.redirect('/login');64+});65+66+app.get('/logout', function(req, res){67+ // destroy the user's session to log them out68+ // will be re-created next request69+ req.session.destroy(function(){70+ res.redirect('home');71+ });72+});73+74+app.get('/login', function(req, res){75+ if (req.session.user) {76+ req.session.success = 'Authenticated as ' + req.session.user.name77+ + ' click to <a href="/logout">logout</a>.';78+ }79+ res.render('login');80+});81+82+app.post('/login', function(req, res){83+ authenticate(req.body.username, req.body.password, function(err, user){84+ if (user) {85+ // Regenerate session when signing in86+ // to prevent fixation 87+ req.session.regenerate(function(){88+ // Store the user's primary key 89+ // in the session store to be retrieved,90+ // or in this case the entire user object91+ req.session.user = user;92+ });93+ } else {94+ req.session.error = 'Authentication failed, please check your '95+ + ' username and password.'96+ + ' (use "tj" and "foobar")';97+ }98+ res.redirect('back');99+ });100+});101+102+app.listen(3000);103+console.log('Express started on port 3000');