scratch-www/src/lib/jar.js

103 lines
3.6 KiB
JavaScript
Raw Normal View History

2018-01-30 09:54:45 -05:00
var cookie = require('cookie');
var defaults = require('lodash.defaults');
var xhr = require('xhr');
var pako = require('pako');
2016-01-14 10:25:03 -05:00
/**
* Module that handles coookie interactions.
* (Cookies?!?! Jar?!?! Get it?!?! WE'RE AMAZING!!!!)
*
* get(name, callback) can be sync or async, as callback is optional
* set(name, value) synchronously sets the cookie
* use(name, uri, callback) can by sync or async, gets cookie from the uri if not there.
*/
2018-01-30 09:54:45 -05:00
var Jar = {
unsign: function (value, callback) {
2016-03-23 10:02:20 -04:00
// Return the usable content portion of a signed, compressed cookie generated by
// Django's signing module
// https://github.com/django/django/blob/stable/1.8.x/django/core/signing.py
if (typeof value === 'undefined') return callback(null, value);
try {
2018-01-30 09:54:45 -05:00
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
2016-03-23 10:02:20 -04:00
// using base64.urlsafe_b64encode
// https://docs.python.org/2/library/base64.html#base64.b64encode
2018-01-30 09:54:45 -05:00
b64Data = b64Data.replace(/[-_]/g, function (c) {return {'-':'+', '_':'/'}[c]; });
var strData = atob(b64Data);
if (decompress) {
2018-01-30 09:54:45 -05:00
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));
}
2018-01-30 09:54:45 -05:00
return callback(null, strData);
} catch (e) {
return callback(e);
}
},
2018-01-30 09:54:45 -05:00
get: function (name, callback) {
2016-03-23 09:50:41 -04:00
// Get cookie by name
2018-01-30 09:54:45 -05:00
var obj = cookie.parse(document.cookie) || {};
// Handle optional callback
if (typeof callback === 'function') {
if (typeof obj === 'undefined') return callback('Cookie not found.');
return callback(null, obj[name]);
}
return obj[name];
},
2018-01-30 09:54:45 -05:00
use: function (name, uri, callback) {
// Attempt to get cookie
2018-01-30 09:54:45 -05:00
Jar.get(name, function (err, obj) {
if (typeof obj !== 'undefined') return callback(null, obj);
// Make XHR request to cookie setter uri
xhr({
uri: uri
2018-01-30 09:54:45 -05:00
}, function (err) {
if (err) return callback(err);
Jar.get(name, callback);
});
});
},
2018-01-30 09:54:45 -05:00
set: function (name, value, opts) {
opts = opts || {};
defaults(opts, {
expires: new Date(new Date().setYear(new Date().getFullYear() + 1))
});
opts.path = '/';
2018-01-30 09:54:45 -05:00
var obj = cookie.serialize(name, value, opts);
document.cookie = obj;
},
2018-01-30 09:54:45 -05:00
getUnsignedValue: function (cookieName, signedValue, callback) {
// Get a value from a signed object
2018-01-30 09:54:45 -05:00
Jar.get(cookieName, function (err, value) {
if (err) return callback(err);
if (typeof value === 'undefined') return callback(null, value);
2018-01-30 09:54:45 -05:00
Jar.unsign(value, function (err, contents) {
if (err) return callback(err);
try {
2018-01-30 09:54:45 -05:00
var data = JSON.parse(contents);
} catch (err) {
return callback(err);
}
2018-01-30 09:54:45 -05:00
return callback(null, data[signedValue]);
});
});
}
2015-10-16 10:59:55 -04:00
};
2015-10-16 15:10:17 -04:00
module.exports = Jar;