mirror of
https://github.com/scratchfoundation/scratch-www.git
synced 2025-02-17 00:21:20 -05:00
Generalize the csrf cookie fetcher
So we can use it with other cookies, like scratchlanguage
This commit is contained in:
parent
54920f5e9f
commit
aaf263f9b6
2 changed files with 39 additions and 21 deletions
|
@ -1,28 +1,14 @@
|
||||||
var cookie = require('cookie');
|
|
||||||
var defaults = require('lodash.defaults');
|
var defaults = require('lodash.defaults');
|
||||||
var xhr = require('xhr');
|
var xhr = require('xhr');
|
||||||
var log = require('../lib/log.js');
|
var log = require('../lib/log.js');
|
||||||
|
|
||||||
|
var CookieMixinFactory = require('./cookieMixinFactory.jsx');
|
||||||
|
|
||||||
var Api = {
|
var Api = {
|
||||||
getCsrf: function (callback) {
|
mixins: [
|
||||||
var obj = cookie.parse(document.cookie) || {};
|
// Provides useScratchcsrftoken
|
||||||
if (typeof obj.scratchcsrftoken === 'undefined') return callback('Cookie not found.');
|
CookieMixinFactory('scratchcsrftoken', '/csrf_token/')
|
||||||
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));
|
|
||||||
},
|
|
||||||
api: function (opts, callback) {
|
api: function (opts, callback) {
|
||||||
defaults(opts, {
|
defaults(opts, {
|
||||||
host: process.env.API_HOST,
|
host: process.env.API_HOST,
|
||||||
|
@ -45,7 +31,7 @@ var Api = {
|
||||||
}.bind(this);
|
}.bind(this);
|
||||||
|
|
||||||
if (opts.useCsrf) {
|
if (opts.useCsrf) {
|
||||||
this.useCsrf(function (err, csrftoken) {
|
this.useScratchcsrftoken(function (err, csrftoken) {
|
||||||
if (err) return log.error('Error while retrieving CSRF token', err);
|
if (err) return log.error('Error while retrieving CSRF token', err);
|
||||||
opts.json.csrftoken = csrftoken;
|
opts.json.csrftoken = csrftoken;
|
||||||
opts.headers['X-CSRFToken'] = csrftoken;
|
opts.headers['X-CSRFToken'] = csrftoken;
|
||||||
|
|
32
src/mixins/cookieMixinFactory.jsx
Normal file
32
src/mixins/cookieMixinFactory.jsx
Normal file
|
@ -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;
|
Loading…
Reference in a new issue