[Pkg-javascript-commits] [node-cookie-parser] 01/02: Imported Upstream version 1.1.0

Leo Iannacone l3on-guest at moszumanska.debian.org
Sat Jun 14 13:19:59 UTC 2014


This is an automated email from the git hooks/post-receive script.

l3on-guest pushed a commit to branch master
in repository node-cookie-parser.

commit 738f03896e29e04581e886d9aa8cffe40c6ba8a1
Author: Leo Iannacone <l3on at ubuntu.com>
Date:   Sat Jun 14 15:04:09 2014 +0200

    Imported Upstream version 1.1.0
---
 .travis.yml          |  5 +++
 History.md           | 18 +++++++++++
 LICENSE              | 22 ++++++++++++++
 README.md            | 39 ++++++++++++++++++++++++
 index.js             | 47 ++++++++++++++++++++++++++++
 lib/parse.js         | 61 +++++++++++++++++++++++++++++++++++++
 package.json         | 27 +++++++++++++++++
 test/cookieParser.js | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 305 insertions(+)

diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..99cdc74
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,5 @@
+language: node_js
+node_js:
+  - "0.8"
+  - "0.10"
+  - "0.11"
diff --git a/History.md b/History.md
new file mode 100644
index 0000000..d19d3f4
--- /dev/null
+++ b/History.md
@@ -0,0 +1,18 @@
+1.1.0 / 2014-05-12
+==================
+
+  * Support for NodeJS version 0.8
+  * deps: cookie at 0.1.2
+    - Fix for maxAge == 0
+    - made compat with expires field
+    - tweak maxAge NaN error message
+
+1.0.1 / 2014-02-20
+==================
+
+  * add missing dependencies
+
+1.0.0 / 2014-02-15
+==================
+
+  * Genesis from `connect`
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..a7693b0
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,22 @@
+(The MIT License)
+
+Copyright (c) 2014 TJ Holowaychuk <tj at vision-media.ca>
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..7dc5356
--- /dev/null
+++ b/README.md
@@ -0,0 +1,39 @@
+# cookie-parser [![Build Status](https://travis-ci.org/expressjs/cookie-parser.svg?branch=master)](https://travis-ci.org/expressjs/cookie-parser) [![NPM Version](https://badge.fury.io/js/cookie-parser.svg)](https://badge.fury.io/js/cookie-parser)
+
+Parse `Cookie` header and populate `req.cookies` with an object keyed by the cookie
+names. Optionally you may enabled signed cookie support by passing a `secret` string,
+which assigns `req.secret` so it may be used by other middleware.
+
+## Install
+
+```sh
+$ npm install cookie-parser
+```
+
+## API
+
+```js
+var cookieParser = require('cookie-parser')
+```
+
+### cookieParser(secret, options)
+
+- `secret` a string used for signing cookies. This is optional and if not specified, will not parse signed cookies.
+- `options` an object that is passed to `cookie.parse` as the second option. See [cookie](https://www.npmjs.org/package/cookie) for more information.
+  - `decode` a funcction to decode the value of the cookie
+
+## Example
+
+```js
+var cookieParser = require('cookie-parser');
+
+connect()
+ .use(cookieParser('optional secret string'))
+ .use(function(req, res, next){
+   res.end(JSON.stringify(req.cookies));
+ })
+```
+
+## License
+
+MIT
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..ed172fd
--- /dev/null
+++ b/index.js
@@ -0,0 +1,47 @@
+/*!
+* cookie-parser
+* MIT Licensed
+*/
+
+/**
+* Module dependencies.
+*/
+
+var cookie = require('cookie');
+var parse = require('./lib/parse');
+
+/**
+ * Parse Cookie header and populate `req.cookies`
+ * with an object keyed by the cookie names.
+ *
+ * @param {String} [secret]
+ * @param {Object} [options]
+ * @return {Function}
+ * @api public
+ */
+
+module.exports = function cookieParser(secret, options){
+  return function cookieParser(req, res, next) {
+    if (req.cookies) return next();
+    var cookies = req.headers.cookie;
+
+    req.secret = secret;
+    req.cookies = {};
+    req.signedCookies = {};
+
+    if (cookies) {
+      try {
+        req.cookies = cookie.parse(cookies, options);
+        if (secret) {
+          req.signedCookies = parse.signedCookies(req.cookies, secret);
+          req.signedCookies = parse.JSONCookies(req.signedCookies);
+        }
+        req.cookies = parse.JSONCookies(req.cookies);
+      } catch (err) {
+        err.status = 400;
+        return next(err);
+      }
+    }
+    next();
+  };
+};
diff --git a/lib/parse.js b/lib/parse.js
new file mode 100644
index 0000000..7991ab7
--- /dev/null
+++ b/lib/parse.js
@@ -0,0 +1,61 @@
+var signature = require('cookie-signature');
+
+/**
+ * Parse signed cookies, returning an object
+ * containing the decoded key/value pairs,
+ * while removing the signed key from `obj`.
+ *
+ * @param {Object} obj
+ * @return {Object}
+ * @api private
+ */
+
+exports.signedCookies = function(obj, secret){
+  var ret = {};
+  Object.keys(obj).forEach(function(key){
+    var val = obj[key];
+    if (0 == val.indexOf('s:')) {
+      val = signature.unsign(val.slice(2), secret);
+      if (val) {
+        ret[key] = val;
+        delete obj[key];
+      }
+    }
+  });
+  return ret;
+};
+
+/**
+ * Parse JSON cookies.
+ *
+ * @param {Object} obj
+ * @return {Object}
+ * @api private
+ */
+
+exports.JSONCookies = function(obj){
+  Object.keys(obj).forEach(function(key){
+    var val = obj[key];
+    var res = exports.JSONCookie(val);
+    if (res) obj[key] = res;
+  });
+  return obj;
+};
+
+/**
+ * Parse JSON cookie string
+ *
+ * @param {String} str
+ * @return {Object} Parsed object or null if not json cookie
+ * @api private
+ */
+
+exports.JSONCookie = function(str) {
+  if (0 == str.indexOf('j:')) {
+    try {
+      return JSON.parse(str.slice(2));
+    } catch (err) {
+      // no op
+    }
+  }
+};
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..a8d94a5
--- /dev/null
+++ b/package.json
@@ -0,0 +1,27 @@
+{
+  "name": "cookie-parser",
+  "version": "1.1.0",
+  "description": "cookie parsing with signatures",
+  "keywords": [
+    "cookie",
+    "middleware"
+  ],
+  "repository": "git://github.com/expressjs/cookie-parser.git",
+  "author": "TJ Holowaychuk <tj at vision-media.ca> (http://tjholowaychuk.com)",
+  "dependencies": {
+    "cookie": "0.1.2",
+    "cookie-signature": "1.0.3"
+  },
+  "devDependencies": {
+    "mocha": "~1.18.2",
+    "supertest": "~0.12.1"
+  },
+  "licenses": "MIT",
+  "main": "./index.js",
+  "engines": {
+    "node": ">= 0.8.0"
+  },
+  "scripts": {
+    "test": "mocha --ui bdd --reporter list -- test/*.js"
+  }
+}
diff --git a/test/cookieParser.js b/test/cookieParser.js
new file mode 100644
index 0000000..f430c80
--- /dev/null
+++ b/test/cookieParser.js
@@ -0,0 +1,86 @@
+
+var cookieParser = require('..')
+var http = require('http')
+var request = require('supertest')
+var signature = require('cookie-signature')
+
+describe('connect.cookieParser()', function(){
+  var server
+  before(function(){
+    server = createServer('keyboard cat')
+  })
+
+  describe('when no cookies are sent', function(){
+    it('should default req.cookies to {}', function(done){
+      request(server)
+      .get('/')
+      .expect(200, '{}', done)
+    })
+
+    it('should default req.signedCookies to {}', function(done){
+      request(server)
+      .get('/signed')
+      .expect(200, '{}', done)
+    })
+  })
+
+  describe('when cookies are sent', function(){
+    it('should populate req.cookies', function(done){
+      request(server)
+      .get('/')
+      .set('Cookie', 'foo=bar; bar=baz')
+      .expect(200, '{"foo":"bar","bar":"baz"}', done)
+    })
+  })
+
+  describe('when a secret is given', function(){
+    var val = signature.sign('foobarbaz', 'keyboard cat');
+    // TODO: "bar" fails...
+
+    it('should populate req.signedCookies', function(done){
+      request(server)
+      .get('/signed')
+      .set('Cookie', 'foo=s:' + val)
+      .expect(200, '{"foo":"foobarbaz"}', done)
+    })
+
+    it('should remove the signed value from req.cookies', function(done){
+      request(server)
+      .get('/')
+      .set('Cookie', 'foo=s:' + val)
+      .expect(200, '{}', done)
+    })
+
+    it('should omit invalid signatures', function(done){
+      server.listen()
+      request(server)
+      .get('/signed')
+      .set('Cookie', 'foo=' + val + '3')
+      .expect(200, '{}', function(err){
+        if (err) return done(err)
+        request(server)
+        .get('/')
+        .set('Cookie', 'foo=' + val + '3')
+        .expect(200, '{"foo":"foobarbaz.CP7AWaXDfAKIRfH49dQzKJx7sKzzSoPq7/AcBBRVwlI3"}', done)
+      });
+    })
+  })
+})
+
+function createServer(secret) {
+  var _parser = cookieParser(secret)
+  return http.createServer(function(req, res){
+    _parser(req, res, function(err){
+      if (err) {
+        res.statusCode = 500
+        res.end(err.message)
+        return
+      }
+
+      var cookies = '/signed' === req.url
+        ? req.signedCookies
+        : req.cookies
+      res.end(JSON.stringify(cookies))
+    })
+  })
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-cookie-parser.git



More information about the Pkg-javascript-commits mailing list