Skip to content

Commit

Permalink
misc refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Jun 4, 2012
1 parent 8a1eb1a commit ff8ec11
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 46 deletions.
6 changes: 1 addition & 5 deletions lib/middleware/cookieSession.js
Expand Up @@ -63,14 +63,10 @@ module.exports = function cookieSession(options){
req.session = {};

// grab the session cookie value, check signature, and unpack the payload
var originalHash;
var rawCookie = req.cookies[key];
if (rawCookie) {
var unsigned = utils.parseSignedCookie(rawCookie, secret);

// store hash value, we will only set the cookie if the hash changes
originalHash = crc16(unsigned);

var originalHash = crc16(unsigned);
req.session = utils.parseJSONCookie(unsigned) || {};
}

Expand Down
31 changes: 10 additions & 21 deletions lib/utils.js
Expand Up @@ -188,12 +188,9 @@ exports.parseSignedCookies = function(obj, secret){
*/

exports.parseSignedCookie = function(str, secret){
// if not signed, return cookie string
if (0 !== str.indexOf('s:')) {
return str;
}

return exports.unsign(str.slice(2), secret);
return 0 == str.indexOf('s:')
? exports.unsign(str.slice(2), secret)
: str;
};

/**
Expand All @@ -205,15 +202,11 @@ exports.parseSignedCookie = function(str, secret){
*/

exports.parseJSONCookies = function(obj){

Object.keys(obj).forEach(function(key){
var val = obj[key];
var res = exports.parseJSONCookie(val);
if (res) {
obj[key] = res;
}
if (res) obj[key] = res;
});

return obj;
};

Expand All @@ -226,17 +219,13 @@ exports.parseJSONCookies = function(obj){
*/

exports.parseJSONCookie = function(str) {
if (0 !== str.indexOf('j:')) {
return null;
}

try {
return JSON.parse(str.slice(2));
} catch (err) {
// no op
if (0 == str.indexOf('j:')) {
try {
return JSON.parse(str.slice(2));
} catch (err) {
// no op
}
}

return null;
}

/**
Expand Down
37 changes: 17 additions & 20 deletions test/cookieSession.js
Expand Up @@ -272,31 +272,28 @@ describe('connect.cookieSession()', function(){
})

// backwards compat test for signed cookies through the `cookieParser` middleware
describe('when using signed cookies', function(){
it('should set-cookie', function(done){
var n = 0;
var app = connect()
.use(connect.cookieParser('keyboard cat'))
.use(connect.cookieSession())
.use(function(req, res, next){
req.session.foo = ++n;
res.end();
});
it('should support req.signedCookies', function(done){
var n = 0;
var app = connect()
.use(connect.cookieParser('keyboard cat'))
.use(connect.cookieSession())
.use(function(req, res, next){
req.session.foo = ++n;
res.end();
});

app.request()
.get('/')
.end(function(res){
res.headers.should.have.property('set-cookie');

app.request()
.get('/')
.set('Cookie', sess(res))
.end(function(res){
res.headers.should.have.property('set-cookie');

app.request()
.get('/')
.set('Cookie', sess(res))
.end(function(res){
res.headers.should.have.property('set-cookie');
done();
});
done();
});
})
});
})

})

0 comments on commit ff8ec11

Please sign in to comment.