diff --git a/node_modules/connect-architect/connect.session/decrypt.js b/node_modules/connect-architect/connect.session/decrypt.js new file mode 100644 index 00000000..cb4d450f --- /dev/null +++ b/node_modules/connect-architect/connect.session/decrypt.js @@ -0,0 +1,28 @@ +"use strict"; + +var utils = require("./session/utils"); + +function decrypt(secret, rawCookie) { + // ensure secret is available or bail + if (!secret) throw new Error('`secret` option required for sessions'); + + // secret is always an array of secrets + secret = [].concat(secret); + + for (var i = 0; i < secret.length; i++) { + var unsignedCookie = utils.parseSignedCookie(rawCookie, secret[i]); + + if (unsignedCookie && unsignedCookie !== rawCookie) { + var usedSecret = secret[i]; + + return { + unsignedCookie: unsignedCookie, + usedSecret: usedSecret + }; + } + } + + return {}; +} + +module.exports.decrypt = decrypt; \ No newline at end of file diff --git a/node_modules/connect-architect/connect.session/decrypt_test.js b/node_modules/connect-architect/connect.session/decrypt_test.js new file mode 100644 index 00000000..a588387b --- /dev/null +++ b/node_modules/connect-architect/connect.session/decrypt_test.js @@ -0,0 +1,39 @@ +#!/usr/bin/env node + +/*global describe it before after beforeEach afterEach */ +"use strict"; + +"use server"; + +require("c9/inline-mocha")(module); +require("c9/setup_paths"); + +var Cookie = require("cookie"); +var assert = require("assert"); +var ConnectCookie = require("./session/cookie"); + +var encrypt = require("./encrypt"); +var decrypt = require("./decrypt"); + +describe("decrypt", function() { + + it("Should decrypt when secret is a string", function(){ + var sessionID = Math.random().toString(36); + var secret = Math.random().toString(36); + var cookieVal = encrypt(sessionID, "connect.sid", new ConnectCookie({}), secret); + var cookie = Cookie.parse(cookieVal); + var val = decrypt.decrypt(secret, cookie["connect.sid"]); + + assert.deepEqual(val, { unsignedCookie: sessionID, usedSecret: secret }); + }); + + it("Should decrypt when secret is an array", function(){ + var sessionID = Math.random().toString(36); + var secret = [Math.random().toString(36), Math.random().toString(36), Math.random().toString(36)]; + var cookieVal = encrypt(sessionID, "connect.sid", new ConnectCookie({}), secret[1]); + var cookie = Cookie.parse(cookieVal); + var val = decrypt.decrypt(secret, cookie["connect.sid"]); + + assert.deepEqual(val, { unsignedCookie: sessionID, usedSecret: secret[1] }); + }); +}); \ No newline at end of file diff --git a/node_modules/connect-architect/connect.session/encrypt.js b/node_modules/connect-architect/connect.session/encrypt.js new file mode 100644 index 00000000..6960e0f6 --- /dev/null +++ b/node_modules/connect-architect/connect.session/encrypt.js @@ -0,0 +1,12 @@ +"use strict"; + +var signature = require("cookie-signature"); + +function encrypt(sessionID, key, cookie, secret) { + var val = 's:' + signature.sign(sessionID, secret); + val = cookie.serialize(key, val); + + return val; +} + +module.exports = encrypt; \ No newline at end of file