web: add fetchApi convenience method

This commit is contained in:
Maximilian Hils 2016-06-02 10:34:16 -07:00
parent 89fc438e32
commit 73e494770f
4 changed files with 18 additions and 12 deletions

View File

@ -87,9 +87,6 @@ class WebState(flow.State):
data=[] data=[]
) )
def load_flows(self, flows):
super(WebState, self).load_flows(flows)
class Options(object): class Options(object):
attributes = [ attributes = [

View File

@ -390,7 +390,7 @@ class Application(tornado.web.Application):
settings = dict( settings = dict(
template_path=os.path.join(os.path.dirname(__file__), "templates"), template_path=os.path.join(os.path.dirname(__file__), "templates"),
static_path=os.path.join(os.path.dirname(__file__), "static"), static_path=os.path.join(os.path.dirname(__file__), "static"),
xsrf_cookies=False, xsrf_cookies=True,
cookie_secret=os.urandom(256), cookie_secret=os.urandom(256),
debug=debug, debug=debug,
wauthenticator=wauthenticator, wauthenticator=wauthenticator,

View File

@ -1,7 +1,6 @@
import $ from "jquery"; import $ from "jquery";
import _ from "lodash";
import {AppDispatcher} from "./dispatcher.js"; import {AppDispatcher} from "./dispatcher.js";
import {getCookie} from "./utils.js"; import {fetchApi} from "./utils.js";
export var ActionTypes = { export var ActionTypes = {
// Connection // Connection
@ -122,12 +121,10 @@ export var FlowActions = {
download: () => window.location = "/flows/dump", download: () => window.location = "/flows/dump",
upload: (file) => { upload: (file) => {
var xsrf = $.param({_xsrf: getCookie("_xsrf")});
//console.log(xsrf);
var filereader = new FileReader(); var filereader = new FileReader();
filereader.file = file; filereader.file = file;
filereader.onload = (e) => { filereader.onload = (e) => {
fetch("/flows/dump?"+xsrf, { fetchApi("/flows/dump", {
method: 'post', method: 'post',
body: e.currentTarget.result body: e.currentTarget.result
}) })

View File

@ -76,11 +76,11 @@ export function reverseString(s) {
) + end; ) + end;
} }
export function getCookie(name) { function getCookie(name) {
var r = document.cookie.match(new RegExp("\\b" + name + "=([^;]*)\\b")); var r = document.cookie.match(new RegExp("\\b" + name + "=([^;]*)\\b"));
return r ? r[1] : undefined; return r ? r[1] : undefined;
} }
var xsrf = $.param({_xsrf: getCookie("_xsrf")}); const xsrf = `_xsrf=${getCookie("_xsrf")}`;
//Tornado XSRF Protection. //Tornado XSRF Protection.
$.ajaxPrefilter(function (options) { $.ajaxPrefilter(function (options) {
@ -101,4 +101,16 @@ $(document).ajaxError(function (event, jqXHR, ajaxSettings, thrownError) {
console.error(thrownError, message, arguments); console.error(thrownError, message, arguments);
actions.EventLogActions.add_event(thrownError + ": " + message); actions.EventLogActions.add_event(thrownError + ": " + message);
alert(message); alert(message);
}); });
export function fetchApi(url, options) {
if(url.indexOf("?") === -1){
url += "?" + xsrf;
} else {
url += "&" + xsrf;
}
return fetch(url, {
...options,
credentials: 'same-origin'
});
}