UNPKG

3.34 kBJavaScriptView Raw
1var task = require ('./base'),
2 util = require ('util');
3
4// - static
5
6var defaultDomain = project.config.consumerConfig.domain || "127.0.0.1";
7var cookieConfig = project.config.consumerConfig.session;
8var defaultCookieTpl = { // default tpl
9 name: "stoken",
10 domain: defaultDomain,
11 path: "/",
12 expirePeriod: "0"
13};
14
15// - - -
16
17var cookieParser = module.exports = function (config) {
18
19 this.init (config);
20
21};
22
23util.inherits (cookieParser, task);
24
25util.extend (cookieParser.prototype, {
26
27 run: function() {
28
29 var self = this;
30 self.failed('use method [parse|render|session]');
31
32 },
33
34 parse: function () {
35
36 var self = this;
37
38 var cookies = self.headers.cookie ? self.headers.cookie : null;
39 var cookiesObj = {length:0};
40
41 if (cookies) cookies.split('; ').map (function(item) {
42
43 var s = item.split('=');
44 if (s[0] && s[1]) {
45 cookiesObj[s[0]] = s[1];
46 cookiesObj.length++;
47 }
48
49 });
50
51 self.completed (cookiesObj);
52 },
53
54 render: function () {
55
56 var self = this;
57
58 var cookies = [];
59
60 self.cookies.map(function(cookie) {
61
62 cookies.push(self.serializeCookie(cookie));
63
64 });
65
66 self.output.setHeader ("Set-Cookie", cookies);
67
68 self.completed (cookies);
69 },
70
71 serializeCookie: function(cookie) {
72
73 var pairs = [cookie.name + '=' + encodeURIComponent(cookie.value)];
74
75 if (cookie.domain) pairs.push('domain=' + cookie.domain);
76 if (cookie.path) pairs.push('path=' + cookie.path);
77 if (cookie.expirePeriod) {
78
79 var expires = new Date();
80 var expirePeriod;
81
82 var firstChar = cookie.expirePeriod[0];
83 expirePeriod = parseInt(cookie.expirePeriod);
84
85 if (firstChar == "+" || firstChar == "-") {
86 expires.setTime(expires.getTime() + expirePeriod);
87 } else {
88 expires.setTime(expirePeriod);
89 }
90
91
92 pairs.push('expires=' + expires.toUTCString());
93 }
94 if (cookie.httpOnly) pairs.push('httpOnly');
95 if (cookie.secure) pairs.push('secure');
96
97 return pairs.join('; ');
98 },
99
100 session: function () {
101
102 var self = this;
103
104 var reqCookies = self.reqCookies;
105 delete reqCookies.length;
106
107 //cookie template
108 var cookieTpl = self.cookieTpl || cookieConfig.cookieTpl || defaultCookieTpl || {},
109 secret = self.secret || cookieConfig.secret || '';
110
111 if (!cookieTpl.domain) cookieTpl.domain = defaultDomain;
112
113
114 // name of session cookie
115 var name = cookieTpl.name;
116 var value = (reqCookies[name]) ? reqCookies[name] : self.generate(secret);
117
118 // - - -
119
120 self.request.sessionUID = value;
121
122 // - - -
123
124 var newCookie = {};
125
126 for (var key in cookieTpl) {
127 newCookie[key] = cookieTpl[key] || defaultCookieTpl[key];
128 }
129
130 newCookie.value = value;
131
132 console.log('SESSION', value);
133
134 self.completed (newCookie);
135 },
136
137 generate: function(secret) {
138
139 var self = this,
140 ip = self.request.connection.remoteAddress,
141 port = self.request.connection.remotePort,
142 date = self.request.connection._idleStart;
143 timestamp = (date.constructor == Date) ? date.getTime() : date;
144
145 timestamp = timestamp.toString(16);
146
147 var rnd = (~~(10e+6*Math.random())).toString(16);
148
149 var str = ip + ':' + port + '.' + timestamp + '.' + rnd;
150 str = (port % 2 == 0) ? (str + '.' + secret) : (secret + '.' + str);
151
152 var result = new Buffer(str).toString('base64').replace(/=+$/, '');
153
154 self.emit('log', 'Generated SessionID\n\t source = ' + str + '\n\t base64 = ' + result);
155
156 return result;
157 }
158
159});