summaryrefslogtreecommitdiffstats
path: root/tools/pharos-dashboard/dashboard/static/js/csrf.js
diff options
context:
space:
mode:
authormaxbr <maxbr@mi.fu-berlin.de>2016-07-29 12:43:43 +0200
committerMax Breitenfeldt <max.breitenfeldt@gmail.com>2016-07-29 11:25:20 +0000
commit35c34c690ae9616d791c39fa218fe1621fa8d8d2 (patch)
tree6168caaeb4be9144ed6bfde0150850d6559c8b6f /tools/pharos-dashboard/dashboard/static/js/csrf.js
parent7ae1f204de947a3c12804c04f32e20d45eb99eba (diff)
import pharos dashboard code
JIRA: RELENG-12 The last commit was missing some JS/CSS dependencies of the site. This happened because they are in folders that are named 'build' or 'dist'. This commit adds a bower.json file, that specifies dependencies. Dependencies can now be installed by running 'bower install' in the dashboard/static folder. Change-Id: I054f319c66771f767e97711cb678d79d3bd6bee4 Signed-off-by: maxbr <maxbr@mi.fu-berlin.de>
Diffstat (limited to 'tools/pharos-dashboard/dashboard/static/js/csrf.js')
-rw-r--r--tools/pharos-dashboard/dashboard/static/js/csrf.js34
1 files changed, 34 insertions, 0 deletions
diff --git a/tools/pharos-dashboard/dashboard/static/js/csrf.js b/tools/pharos-dashboard/dashboard/static/js/csrf.js
new file mode 100644
index 00000000..12429b38
--- /dev/null
+++ b/tools/pharos-dashboard/dashboard/static/js/csrf.js
@@ -0,0 +1,34 @@
+/**
+ * use django csrf token in ajax requests
+ * source: https://docs.djangoproject.com/en/1.8/ref/csrf/#ajax
+ */
+// using jQuery
+function getCookie(name) {
+ var cookieValue = null;
+ if (document.cookie && document.cookie != '') {
+ var cookies = document.cookie.split(';');
+ for (var i = 0; i < cookies.length; i++) {
+ var cookie = jQuery.trim(cookies[i]);
+ // Does this cookie string begin with the name we want?
+ if (cookie.substring(0, name.length + 1) == (name + '=')) {
+ cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
+ break;
+ }
+ }
+ }
+ return cookieValue;
+}
+var csrftoken = getCookie('csrftoken');
+
+function csrfSafeMethod(method) {
+ // these HTTP methods do not require CSRF protection
+ return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
+}
+
+$.ajaxSetup({
+ beforeSend: function (xhr, settings) {
+ if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
+ xhr.setRequestHeader("X-CSRFToken", csrftoken);
+ }
+ }
+}); \ No newline at end of file