Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A few bugfixes #97

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
159 changes: 82 additions & 77 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,19 @@
* MIT Licensed
*/

'use strict'

/**
* Module dependencies.
* @private
*/

var cookie = require('cookie')
var signature = require('cookie-signature')
const cookie = require('cookie');
const signature = require('cookie-signature');

/**
* Module exports.
* @public
*/

module.exports = cookieParser
module.exports.JSONCookie = JSONCookie
module.exports.JSONCookies = JSONCookies
module.exports.signedCookie = signedCookie
module.exports.signedCookies = signedCookies

/**
* Parse Cookie header and populate `req.cookies`
* with an object keyed by the cookie names.
Expand All @@ -36,40 +28,47 @@ module.exports.signedCookies = signedCookies
* @public
*/

function cookieParser (secret, options) {
var secrets = !secret || Array.isArray(secret)
function cookieParser(secret, options) {
const secrets = !secret || Array.isArray(secret)
? (secret || [])
: [secret]

return function cookieParser (req, res, next) {
if (req.cookies) {
return next()
}

var cookies = req.headers.cookie

req.secret = secrets[0]
req.cookies = Object.create(null)
req.signedCookies = Object.create(null)

// no cookies
if (!cookies) {
return next()
: [secret];

return function cookieParser(req, res, next) {
try {
if (req.cookies) {
next();
return;
}

const cookies = req.headers.cookie;

req.secret = secrets[0];
req.cookies = Object.create(null);
req.signedCookies = Object.create(null);

// no cookies
if (!cookies) {
req.cookies = {};
next();
return;
}

req.cookies = cookie.parse(cookies, options);

// parse signed cookies
if (secrets.length !== 0) {
req.signedCookies = signedCookies(req.cookies, secrets);
req.signedCookies = JSONCookies(req.signedCookies);
}

// parse JSON cookies
req.cookies = JSONCookies(req.cookies);

next();
} catch (err) {
console.log(err);
}

req.cookies = cookie.parse(cookies, options)

// parse signed cookies
if (secrets.length !== 0) {
req.signedCookies = signedCookies(req.cookies, secrets)
req.signedCookies = JSONCookies(req.signedCookies)
}

// parse JSON cookies
req.cookies = JSONCookies(req.cookies)

next()
}
};
}

/**
Expand All @@ -80,15 +79,15 @@ function cookieParser (secret, options) {
* @public
*/

function JSONCookie (str) {
function JSONCookie(str) {
if (typeof str !== 'string' || str.substr(0, 2) !== 'j:') {
return undefined
return undefined;
}

try {
return JSON.parse(str.slice(2))
return JSON.parse(str.slice(2));
} catch (err) {
return undefined
return undefined;
}
}

Expand All @@ -100,21 +99,21 @@ function JSONCookie (str) {
* @public
*/

function JSONCookies (obj) {
var cookies = Object.keys(obj)
var key
var val
function JSONCookies(obj) {
const cookies = Object.keys(obj);
let key;
let val;

for (var i = 0; i < cookies.length; i++) {
key = cookies[i]
val = JSONCookie(obj[key])
for (let i = 0; i < cookies.length; i++) {
key = cookies[i];
val = JSONCookie(obj[key]);

if (val) {
obj[key] = val
obj[key] = val;
}
}

return obj
return obj;
}

/**
Expand All @@ -126,28 +125,28 @@ function JSONCookies (obj) {
* @public
*/

function signedCookie (str, secret) {
function signedCookie(str, secret) {
if (typeof str !== 'string') {
return undefined
return undefined;
}

if (str.substr(0, 2) !== 's:') {
return str
return str;
}

var secrets = !secret || Array.isArray(secret)
const secrets = !secret || Array.isArray(secret)
? (secret || [])
: [secret]
: [secret];

for (var i = 0; i < secrets.length; i++) {
var val = signature.unsign(str.slice(2), secrets[i])
for (let i = 0; i < secrets.length; i++) {
const val = signature.unsign(str.slice(2), secrets[i]);

if (val !== false) {
return val
return val;
}
}

return false
return false;
}

/**
Expand All @@ -160,23 +159,29 @@ function signedCookie (str, secret) {
* @public
*/

function signedCookies (obj, secret) {
var cookies = Object.keys(obj)
var dec
var key
var ret = Object.create(null)
var val
function signedCookies(obj, secret) {
const cookies = Object.keys(obj);
let dec;
let key;
const ret = Object.create(null);
let val;

for (var i = 0; i < cookies.length; i++) {
key = cookies[i]
val = obj[key]
dec = signedCookie(val, secret)
for (let i = 0; i < cookies.length; i++) {
key = cookies[i];
val = obj[key];
dec = signedCookie(val, secret);

if (val !== dec) {
ret[key] = dec
delete obj[key]
ret[key] = dec;
delete obj[key];
}
}

return ret
return ret;
}

module.exports = cookieParser;
module.exports.JSONCookie = JSONCookie;
module.exports.JSONCookies = JSONCookies;
module.exports.signedCookie = signedCookie;
module.exports.signedCookies = signedCookies;