Added { signed: true } option to res.cookie()
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 · parent 58cfd60 · Browse files · 3 files·+46 added−64 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+46 added−64 removed
- tests:+41 added−46 removed
- other:+5 added−18 removed
Provenance
- Commit
1150a88- Tree
a2b8624- Author
TJ Holowaychuk <tj@vision-media.ca>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
58cfd60
3 files changed+46 added−64 removed
modified:lib/response.js+5 added-18 removed
439 unmodified lines440 : opts);441 };442 443 /**444- * Set a signed cookie with the given `name` and `val`.445- * See `res.cookie()` for details.446- *447- * @param {String} name448- * @param {String|Object} val449- * @param {Object} options450- * @api public451- */452-453-res.signedCookie = function(name, val, options){454- var secret = this.req.secret;455- if (!secret) throw new Error('connect.cookieParser("secret") required for signed cookies');456- if ('object' == typeof val) val = 'j:' + JSON.stringify(val);457- val = utils.sign(val, secret);458- return this.cookie(name, val, options);459-};460-461-/**444 * Set cookie `name` to `val`, with the given `options`.445 *446 * Options:447 *448 * - `maxAge` max-age in milliseconds, converted to `expires`449+ * - `signed` sign the cookie450 * - `path` defaults to "/"451 *452 * Examples:453 *10 unmodified lines464 */465 466 res.cookie = function(name, val, options){467 options = options || {};468+ var secret = this.req.secret;469+ var signed = options.signed;470+ if (signed && !secret) throw new Error('connect.cookieParser("secret") required for signed cookies');471 if ('object' == typeof val) val = 'j:' + JSON.stringify(val);472+ if (signed) val = utils.sign(val, secret);473 if ('maxAge' in options) options.expires = new Date(Date.now() + options.maxAge);474 if (null == options.path) options.path = '/';475 var cookie = utils.serializeCookie(name, val, options);476 this.set('Set-Cookie', cookie);159 unmodified linesmodified:test/res.cookie.js+41 added
90 unmodified lines91929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613790 unmodified lines done(); }) }) })
describe('signed', function(){ it('should generate a signed JSON cookie', function(done){ var app = express();
app.use(express.cookieParser('foo bar baz'));
app.use(function(req, res){ res.cookie('user', { name: 'tobi' }, { signed: true }).end(); });
request(app) .get('/') .end(function(res){ var val = res.headers['set-cookie'][0]; val = decodeURIComponent(val.split('.')[0]); val.should.equal('user=j:{"name":"tobi"}'); done(); }) }) })
describe('.signedCookie(name, string)', function(){ it('should set a signed cookie', function(done){ var app = express();
app.use(express.cookieParser('foo bar baz'));
app.use(function(req, res){ res.cookie('name', 'tobi', { signed: true }).end(); });
request(app) .get('/') .end(function(res){ var val = ['name=tobi.xJjV2iZ6EI7C8E5kzwbfA9PVLl1ZR07UTnuTgQQ4EnQ; path=/']; res.headers['set-cookie'].should.eql(val); done(); }) }) }) })})deleted:test/res.signedCookie.js-46 removed
12345678910111213141516171819202122232425262728293031323334353637383940414243444546
var express = require('../') , request = require('./support/http');
describe('res', function(){ describe('.signedCookie(name, object)', function(){ it('should generate a signed JSON cookie', function(done){ var app = express();
app.use(express.cookieParser('foo bar baz'));
app.use(function(req, res){ res.signedCookie('user', { name: 'tobi' }).end(); }); request(app) .get('/') .end(function(res){ var val = res.headers['set-cookie'][0]; val = decodeURIComponent(val.split('.')[0]); val.should.equal('user=j:{"name":"tobi"}'); done(); }) }) }) describe('.signedCookie(name, string)', function(){ it('should set a signed cookie', function(done){ var app = express();
app.use(express.cookieParser('foo bar baz'));
app.use(function(req, res){ res.signedCookie('name', 'tobi').end(); }); request(app) .get('/') .end(function(res){ var val = ['name=tobi.xJjV2iZ6EI7C8E5kzwbfA9PVLl1ZR07UTnuTgQQ4EnQ; path=/']; res.headers['set-cookie'].should.eql(val); done(); }) }) })})