프로젝트

일반

사용자정보

통계
| 개정판:

root / HServer / 00.Server / 00.Program / node_modules / cookie-parser / index.js

이력 | 보기 | 이력해설 | 다운로드 (3.36 KB)

1 39 HKM
/*!
2
 * cookie-parser
3
 * Copyright(c) 2014 TJ Holowaychuk
4
 * Copyright(c) 2015 Douglas Christopher Wilson
5
 * MIT Licensed
6
 */
7
8
'use strict';
9
10
/**
11
 * Module dependencies.
12
 * @private
13
 */
14
15
var cookie = require('cookie');
16
var signature = require('cookie-signature');
17
18
/**
19
 * Module exports.
20
 * @public
21
 */
22
23
module.exports = cookieParser;
24
module.exports.JSONCookie = JSONCookie;
25
module.exports.JSONCookies = JSONCookies;
26
module.exports.signedCookie = signedCookie;
27
module.exports.signedCookies = signedCookies;
28
29
/**
30
 * Parse Cookie header and populate `req.cookies`
31
 * with an object keyed by the cookie names.
32
 *
33
 * @param {string|array} [secret] A string (or array of strings) representing cookie signing secret(s).
34
 * @param {Object} [options]
35
 * @return {Function}
36
 * @public
37
 */
38
39
function cookieParser(secret, options) {
40
  return function cookieParser(req, res, next) {
41
    if (req.cookies) {
42
      return next();
43
    }
44
45
    var cookies = req.headers.cookie;
46
    var secrets = !secret || Array.isArray(secret)
47
      ? (secret || [])
48
      : [secret];
49
50
    req.secret = secrets[0];
51
    req.cookies = Object.create(null);
52
    req.signedCookies = Object.create(null);
53
54
    // no cookies
55
    if (!cookies) {
56
      return next();
57
    }
58
59
    req.cookies = cookie.parse(cookies, options);
60
61
    // parse signed cookies
62
    if (secrets.length !== 0) {
63
      req.signedCookies = signedCookies(req.cookies, secrets);
64
      req.signedCookies = JSONCookies(req.signedCookies);
65
    }
66
67
    // parse JSON cookies
68
    req.cookies = JSONCookies(req.cookies);
69
70
    next();
71
  };
72
}
73
74
/**
75
 * Parse JSON cookie string.
76
 *
77
 * @param {String} str
78
 * @return {Object} Parsed object or undefined if not json cookie
79
 * @public
80
 */
81
82
function JSONCookie(str) {
83
  if (typeof str !== 'string' || str.substr(0, 2) !== 'j:') {
84
    return undefined;
85
  }
86
87
  try {
88
    return JSON.parse(str.slice(2));
89
  } catch (err) {
90
    return undefined;
91
  }
92
}
93
94
/**
95
 * Parse JSON cookies.
96
 *
97
 * @param {Object} obj
98
 * @return {Object}
99
 * @public
100
 */
101
102
function JSONCookies(obj) {
103
  var cookies = Object.keys(obj);
104
  var key;
105
  var val;
106
107
  for (var i = 0; i < cookies.length; i++) {
108
    key = cookies[i];
109
    val = JSONCookie(obj[key]);
110
111
    if (val) {
112
      obj[key] = val;
113
    }
114
  }
115
116
  return obj;
117
}
118
119
/**
120
 * Parse a signed cookie string, return the decoded value.
121
 *
122
 * @param {String} str signed cookie string
123
 * @param {string|array} secret
124
 * @return {String} decoded value
125
 * @public
126
 */
127
128
function signedCookie(str, secret) {
129
  if (typeof str !== 'string') {
130
    return undefined;
131
  }
132
133
  if (str.substr(0, 2) !== 's:') {
134
    return str;
135
  }
136
137
  var secrets = !secret || Array.isArray(secret)
138
    ? (secret || [])
139
    : [secret];
140
141
  for (var i = 0; i < secrets.length; i++) {
142
    var val = signature.unsign(str.slice(2), secrets[i]);
143
144
    if (val !== false) {
145
      return val;
146
    }
147
  }
148
149
  return false;
150
}
151
152
/**
153
 * Parse signed cookies, returning an object containing the decoded key/value
154
 * pairs, while removing the signed key from obj.
155
 *
156
 * @param {Object} obj
157
 * @param {string|array} secret
158
 * @return {Object}
159
 * @public
160
 */
161
162
function signedCookies(obj, secret) {
163
  var cookies = Object.keys(obj);
164
  var dec;
165
  var key;
166
  var ret = Object.create(null);
167
  var val;
168
169
  for (var i = 0; i < cookies.length; i++) {
170
    key = cookies[i];
171
    val = obj[key];
172
    dec = signedCookie(val, secret);
173
174
    if (val !== dec) {
175
      ret[key] = dec;
176
      delete obj[key];
177
    }
178
  }
179
180
  return ret;
181
}