From aaf263f9b6bf4c319d481aa84a41b229e710af9d Mon Sep 17 00:00:00 2001 From: Ray Schamp Date: Thu, 15 Oct 2015 09:37:05 -0600 Subject: [PATCH] Generalize the csrf cookie fetcher So we can use it with other cookies, like scratchlanguage --- src/mixins/api.jsx | 28 +++++++-------------------- src/mixins/cookieMixinFactory.jsx | 32 +++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 21 deletions(-) create mode 100644 src/mixins/cookieMixinFactory.jsx diff --git a/src/mixins/api.jsx b/src/mixins/api.jsx index a5873e450..c65826a17 100644 --- a/src/mixins/api.jsx +++ b/src/mixins/api.jsx @@ -1,28 +1,14 @@ -var cookie = require('cookie'); var defaults = require('lodash.defaults'); var xhr = require('xhr'); var log = require('../lib/log.js'); +var CookieMixinFactory = require('./cookieMixinFactory.jsx'); + var Api = { - getCsrf: function (callback) { - var obj = cookie.parse(document.cookie) || {}; - if (typeof obj.scratchcsrftoken === 'undefined') return callback('Cookie not found.'); - callback(null, obj.scratchcsrftoken); - }, - useCsrf: function (callback) { - this.getCsrf(function (err, csrftoken) { - if (csrftoken) return callback(null, csrftoken); - xhr({ - 'uri': '/csrf_token/' - }, function (err) { - if (err) return callback(err); - this.getCsrf(function (err, csrftoken) { - if (err) return callback(err); - callback(err, csrftoken); - }); - }.bind(this)); - }.bind(this)); - }, + mixins: [ + // Provides useScratchcsrftoken + CookieMixinFactory('scratchcsrftoken', '/csrf_token/') + ], api: function (opts, callback) { defaults(opts, { host: process.env.API_HOST, @@ -45,7 +31,7 @@ var Api = { }.bind(this); if (opts.useCsrf) { - this.useCsrf(function (err, csrftoken) { + this.useScratchcsrftoken(function (err, csrftoken) { if (err) return log.error('Error while retrieving CSRF token', err); opts.json.csrftoken = csrftoken; opts.headers['X-CSRFToken'] = csrftoken; diff --git a/src/mixins/cookieMixinFactory.jsx b/src/mixins/cookieMixinFactory.jsx new file mode 100644 index 000000000..b08ab4320 --- /dev/null +++ b/src/mixins/cookieMixinFactory.jsx @@ -0,0 +1,32 @@ +var cookie = require('cookie'); +var xhr = require('xhr'); + + +var cookieMixinFactory = function (cookieName, cookieSetter) { + var capitalizedCookieName = cookieName.charAt(0).toUpperCase() + cookieName.slice(1); + var getterName = "get" + capitalizedCookieName; + var userName = "use" + capitalizedCookieName; + var mixin = {} + mixin[getterName] = function (callback) { + var obj = cookie.parse(document.cookie) || {}; + if (typeof obj[cookieName] === 'undefined') return callback('Cookie not found.'); + callback(null, obj[cookieName]); + }; + mixin[userName] = function (callback) { + this[getterName](function (err, cookieValue) { + if (cookieValue) return callback(null, cookieValue); + xhr({ + 'uri': cookieSetter + }, function (err) { + if (err) return callback(err); + this[getterName](function (err, cookieValue) { + if (err) return callback(err); + callback(err, cookieValue); + }); + }.bind(this)); + }.bind(this)); + } + return mixin; +}; + +module.exports = cookieMixinFactory;