Commit 96bc1b1a authored by Ray Schamp's avatar Ray Schamp

Add method for reading session cookie

Assumes the session cookie is stored as JSON which may or may not have been compressed via zlib (indicated by a leading `.`), which is then base64-encoded, and made URL-safe by replacing all `+` and `/` characters with `-` and `_` respectively.
parent 9cd5c980
var cookie = require('cookie'); var cookie = require('cookie');
var xhr = require('xhr'); var xhr = require('xhr');
var pako = require('pako');
/** /**
* Module that handles coookie interactions. * Module that handles coookie interactions.
...@@ -10,6 +11,33 @@ var xhr = require('xhr'); ...@@ -10,6 +11,33 @@ var xhr = require('xhr');
* use(name, uri, callback) – can by sync or async, gets cookie from the uri if not there. * use(name, uri, callback) – can by sync or async, gets cookie from the uri if not there.
*/ */
var Jar = { var Jar = {
unsign: function (value, callback) {
// Return the usable content portion of a signed, compressed cookie
if (!value) return callback('No value to unsign');
try {
var b64Data = value.split(':')[0];
var decompress = false;
if (b64Data[0] === '.') {
decompress = true;
b64Data = b64Data.substring(1);
}
// Django makes its base64 strings url safe by replacing + and / with - and _ respectively
b64Data = b64Data.replace(/[-_]/g, function (c) {return {'-':'+', '_':'/'}[c]; });
var strData = atob(b64Data);
if (decompress) {
var charData = strData.split('').map(function (c) { return c.charCodeAt(0); });
var binData = new Uint8Array(charData);
var data = pako.inflate(binData);
strData = String.fromCharCode.apply(null, new Uint16Array(data));
}
return callback(null, strData);
} catch (e) {
return callback(e);
}
},
get: function (name, callback) { get: function (name, callback) {
// Get cookie by name // Get cookie by name
var obj = cookie.parse(document.cookie) || {}; var obj = cookie.parse(document.cookie) || {};
......
...@@ -26,6 +26,12 @@ ...@@ -26,6 +26,12 @@
*/ */
(function(){try{new e("test")}catch(t){var e=function(t,e){var n;return e=e||{bubbles:!1,cancelable:!1,detail:void 0},n=document.createEvent("CustomEvent"),n.initCustomEvent(t,e.bubbles,e.cancelable,e.detail),n};e.prototype=window.Event.prototype,window.CustomEvent=e}})(); (function(){try{new e("test")}catch(t){var e=function(t,e){var n;return e=e||{bubbles:!1,cancelable:!1,detail:void 0},n=document.createEvent("CustomEvent"),n.initCustomEvent(t,e.bubbles,e.cancelable,e.detail),n};e.prototype=window.Event.prototype,window.CustomEvent=e}})();
/*!
* https://github.com/davidchambers/Base64.js
* see https://github.com/davidchambers/Base64.js/blob/master/LICENSE
*/
!function(){function t(t){this.message=t}var r="undefined"!=typeof exports?exports:this,e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";t.prototype=new Error,t.prototype.name="InvalidCharacterError",r.btoa||(r.btoa=function(r){for(var o,n,a=String(r),i=0,c=e,d="";a.charAt(0|i)||(c="=",i%1);d+=c.charAt(63&o>>8-i%1*8)){if(n=a.charCodeAt(i+=.75),n>255)throw new t("'btoa' failed: The string to be encoded contains characters outside of the Latin1 range.");o=o<<8|n}return d}),r.atob||(r.atob=function(r){var o=String(r).replace(/=+$/,"");if(o.length%4==1)throw new t("'atob' failed: The string to be decoded is not correctly encoded.");for(var n,a,i=0,c=0,d="";a=o.charAt(c++);~a&&(n=i%4?64*n+a:a,i++%4)?d+=String.fromCharCode(255&n>>(-2*i&6)):0)a=e.indexOf(a);return d})}();
/*! /*!
* https://github.com/andyearnshaw/Intl.js * https://github.com/andyearnshaw/Intl.js
* @license The MIT License (MIT) Copyright (c) 2013 Andy Earnshaw * @license The MIT License (MIT) Copyright (c) 2013 Andy Earnshaw
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment