1010 */
1111
1212var cookie = require ( 'cookie' ) ;
13- var parse = require ( './lib/parse' ) ;
13+ var signature = require ( 'cookie-signature' ) ;
14+
15+ /**
16+ * Module exports.
17+ * @public
18+ */
19+
20+ module . exports = cookieParser ;
21+ module . exports . JSONCookie = JSONCookie ;
22+ module . exports . JSONCookies = JSONCookies ;
23+ module . exports . signedCookie = signedCookie ;
24+ module . exports . signedCookies = signedCookies ;
1425
1526/**
1627 * Parse Cookie header and populate `req.cookies`
@@ -22,7 +33,7 @@ var parse = require('./lib/parse');
2233 * @public
2334 */
2435
25- exports = module . exports = function cookieParser ( secret , options ) {
36+ function cookieParser ( secret , options ) {
2637 return function cookieParser ( req , res , next ) {
2738 if ( req . cookies ) return next ( ) ;
2839 var cookies = req . headers . cookie ;
@@ -40,23 +51,102 @@ exports = module.exports = function cookieParser(secret, options){
4051
4152 // parse signed cookies
4253 if ( secret ) {
43- req . signedCookies = parse . signedCookies ( req . cookies , secret ) ;
44- req . signedCookies = parse . JSONCookies ( req . signedCookies ) ;
54+ req . signedCookies = signedCookies ( req . cookies , secret ) ;
55+ req . signedCookies = JSONCookies ( req . signedCookies ) ;
4556 }
4657
4758 // parse JSON cookies
48- req . cookies = parse . JSONCookies ( req . cookies ) ;
59+ req . cookies = JSONCookies ( req . cookies ) ;
4960
5061 next ( ) ;
5162 } ;
52- } ;
63+ }
5364
5465/**
55- * Export parsing functions.
66+ * Parse JSON cookie string.
67+ *
68+ * @param {String } str
69+ * @return {Object } Parsed object or null if not json cookie
5670 * @public
5771 */
5872
59- exports . JSONCookie = parse . JSONCookie ;
60- exports . JSONCookies = parse . JSONCookies ;
61- exports . signedCookie = parse . signedCookie ;
62- exports . signedCookies = parse . signedCookies ;
73+ function JSONCookie ( str ) {
74+ if ( ! str || str . substr ( 0 , 2 ) !== 'j:' ) return ;
75+
76+ try {
77+ return JSON . parse ( str . slice ( 2 ) ) ;
78+ } catch ( err ) {
79+ // no op
80+ }
81+ }
82+
83+ /**
84+ * Parse JSON cookies.
85+ *
86+ * @param {Object } obj
87+ * @return {Object }
88+ * @public
89+ */
90+
91+ function JSONCookies ( obj ) {
92+ var cookies = Object . keys ( obj ) ;
93+ var key ;
94+ var val ;
95+
96+ for ( var i = 0 ; i < cookies . length ; i ++ ) {
97+ key = cookies [ i ] ;
98+ val = JSONCookie ( obj [ key ] ) ;
99+
100+ if ( val ) {
101+ obj [ key ] = val ;
102+ }
103+ }
104+
105+ return obj ;
106+ }
107+
108+ /**
109+ * Parse a signed cookie string, return the decoded value.
110+ *
111+ * @param {String } str signed cookie string
112+ * @param {String } secret
113+ * @return {String } decoded value
114+ * @public
115+ */
116+
117+ function signedCookie ( str , secret ) {
118+ return str . substr ( 0 , 2 ) === 's:'
119+ ? signature . unsign ( str . slice ( 2 ) , secret )
120+ : str ;
121+ }
122+
123+ /**
124+ * Parse signed cookies, returning an object
125+ * containing the decoded key/value pairs,
126+ * while removing the signed key from `obj`.
127+ *
128+ * @param {Object } obj
129+ * @return {Object }
130+ * @public
131+ */
132+
133+ function signedCookies ( obj , secret ) {
134+ var cookies = Object . keys ( obj ) ;
135+ var dec ;
136+ var key ;
137+ var ret = Object . create ( null ) ;
138+ var val ;
139+
140+ for ( var i = 0 ; i < cookies . length ; i ++ ) {
141+ key = cookies [ i ] ;
142+ val = obj [ key ] ;
143+ dec = signedCookie ( val , secret ) ;
144+
145+ if ( val !== dec ) {
146+ ret [ key ] = dec ;
147+ delete obj [ key ] ;
148+ }
149+ }
150+
151+ return ret ;
152+ }
0 commit comments