Skip to content

Commit adae877

Browse files
author
Andrea Bonomi
committed
Refresh CSTR token
1 parent d333c4e commit adae877

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

airflow_code_editor/app_builder_view.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ def tree_base(self, path=None):
7777
def tree(self, path=None):
7878
return self._tree(path, args=request.args)
7979

80+
@expose("/ping", methods=["GET"])
81+
@auth.has_access(PERMISSIONS)
82+
def ping(self):
83+
return self._ping()
84+
8085
def _render(self, template, *args, **kargs):
8186
return self.render_template(
8287
template + "_appbuilder.html",

airflow_code_editor/code_editor_view.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import logging
2121
import mimetypes
2222
from flask import abort, request, send_file
23+
from flask_wtf.csrf import generate_csrf
2324
from airflow.version import version
2425
from airflow_code_editor.commons import HTTP_404_NOT_FOUND
2526
from airflow_code_editor.tree import get_tree
@@ -134,3 +135,6 @@ def _format(self):
134135

135136
def _tree(self, path, args = {}):
136137
return {'value': get_tree(path, args)}
138+
139+
def _ping(self):
140+
return {'value': generate_csrf()}

airflow_code_editor/flask_admin_view.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def save(self, path=None):
6565
def load(self, path=None):
6666
return self._load(path)
6767

68-
@expose("/format", methods=["GET"])
68+
@expose("/format", methods=["POST"])
6969
@login_required
7070
def format(self, path=None):
7171
return self._load(path)
@@ -80,6 +80,11 @@ def tree_base(self, path=None):
8080
def tree(self, path=None):
8181
return self._tree(path, args=request.args)
8282

83+
@expose("/ping", methods=["GET"])
84+
@login_required
85+
def ping(self):
86+
return self._ping()
87+
8388
def _render(self, template, *args, **kargs):
8489
return self.render(
8590
template + "_admin.html",

airflow_code_editor/static/vue_components.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
})(function(webui) {
99
"use strict";
1010

11+
var csrfToken = null;
12+
var CSRF_REFRESH = 1000 * 60 * 10;
13+
1114
function prepareHref(path) {
1215
// Return the full path of the URL
1316
return document.location.pathname + path;
@@ -47,6 +50,24 @@
4750
}
4851
}
4952

53+
function beforeSend(xhr, settings) {
54+
if (!/^(GET|HEAD|OPTIONS|TRACE)$/i.test(settings.type)) {
55+
xhr.setRequestHeader("X-CSRFToken", csrfToken);
56+
}
57+
}
58+
59+
function refreshCsrfToken() {
60+
// Refresh CSRF Token
61+
jQuery.get(prepareHref('ping'))
62+
.done(function(data) {
63+
csrfToken = data.value;
64+
setTimeout(refreshCsrfToken, CSRF_REFRESH);
65+
})
66+
.fail(function(jqXHR, textStatus, errorThrown) {
67+
setTimeout(refreshCsrfToken, CSRF_REFRESH);
68+
});
69+
}
70+
5071
function TreeEntry(data, isGit, path) {
5172
var self = this;
5273
if (data) {
@@ -696,19 +717,17 @@
696717
});
697718

698719

699-
webui.init = function(csrfToken) {
720+
webui.init = function(csrfTokenParam) {
700721
// Init
701722
CodeMirror.modeURL = '/static/code_editor/mode/%N/%N.js';
702723
// Disable animation
703724
BootstrapDialog.configDefaultOptions({ animate: false });
704725
// CSRF Token setup
726+
csrfToken = csrfTokenParam;
705727
jQuery.ajaxSetup({
706-
beforeSend: function(xhr, settings) {
707-
if (!/^(GET|HEAD|OPTIONS|TRACE)$/i.test(settings.type)) {
708-
xhr.setRequestHeader("X-CSRFToken", csrfToken);
709-
}
710-
}
728+
beforeSend: beforeSend
711729
});
730+
setTimeout(refreshCsrfToken, CSRF_REFRESH);
712731
// Append global container to body
713732
jQuery('#global-container').appendTo(jQuery("body"));
714733
// Init app

0 commit comments

Comments
 (0)