Merge "LOG SQL dump files getting installed"
[sdnc/oam.git] / dgbuilder / dgeflows / node_modules / cookie-parser / lib / parse.js
1 var signature = require('cookie-signature');
2
3 /**
4  * Parse signed cookies, returning an object
5  * containing the decoded key/value pairs,
6  * while removing the signed key from `obj`.
7  *
8  * @param {Object} obj
9  * @return {Object}
10  * @api private
11  */
12
13 exports.signedCookies = function(obj, secret){
14   var cookies = Object.keys(obj);
15   var dec;
16   var key;
17   var ret = Object.create(null);
18   var val;
19
20   for (var i = 0; i < cookies.length; i++) {
21     key = cookies[i];
22     val = obj[key];
23     dec = exports.signedCookie(val, secret);
24
25     if (val !== dec) {
26       ret[key] = dec;
27       delete obj[key];
28     }
29   }
30
31   return ret;
32 };
33
34 /**
35  * Parse a signed cookie string, return the decoded value
36  *
37  * @param {String} str signed cookie string
38  * @param {String} secret
39  * @return {String} decoded value
40  * @api private
41  */
42
43 exports.signedCookie = function(str, secret){
44   return str.substr(0, 2) === 's:'
45     ? signature.unsign(str.slice(2), secret)
46     : str;
47 };
48
49 /**
50  * Parse JSON cookies.
51  *
52  * @param {Object} obj
53  * @return {Object}
54  * @api private
55  */
56
57 exports.JSONCookies = function(obj){
58   var cookies = Object.keys(obj);
59   var key;
60   var val;
61
62   for (var i = 0; i < cookies.length; i++) {
63     key = cookies[i];
64     val = exports.JSONCookie(obj[key]);
65
66     if (val) {
67       obj[key] = val;
68     }
69   }
70
71   return obj;
72 };
73
74 /**
75  * Parse JSON cookie string
76  *
77  * @param {String} str
78  * @return {Object} Parsed object or null if not json cookie
79  * @api private
80  */
81
82 exports.JSONCookie = function(str) {
83   if (!str || str.substr(0, 2) !== 'j:') return;
84
85   try {
86     return JSON.parse(str.slice(2));
87   } catch (err) {
88     // no op
89   }
90 };