diff --git a/CTFd/themes/admin/templates/base.html b/CTFd/themes/admin/templates/base.html index 3c9e4211..add5bed1 100644 --- a/CTFd/themes/admin/templates/base.html +++ b/CTFd/themes/admin/templates/base.html @@ -14,6 +14,7 @@ + @@ -23,6 +24,8 @@ var script_root = "{{ request.script_root }}"; var csrf_nonce = "{{ nonce }}"; var user_mode = "{{ get_config('user_mode') }}"; + CTFd.options.urlRoot = script_root; + CTFd.options.csrfNonce = csrf_nonce; {% block stylesheets %} {% endblock %} {% for stylesheet in get_registered_admin_stylesheets() %} diff --git a/CTFd/themes/core/static/js/CTFd.js b/CTFd/themes/core/static/js/CTFd.js index 742e24eb..497c2f8f 100644 --- a/CTFd/themes/core/static/js/CTFd.js +++ b/CTFd/themes/core/static/js/CTFd.js @@ -1,173 +1,34 @@ var CTFd = (function () { - var urlRoot = ''; + var options = { + urlRoot: '', + csrfNonce: '', + }; - var challenges = { - all: function(){ - return fetch(urlRoot + '/api/v1/challenges', { - credentials: 'same-origin', - headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json' - }, - }) - .then(function (response) { - return response.json(); - }).then(function (data) { - return data; - }); - }, - get: function(challengeId){ - return fetch(urlRoot + '/api/v1/challenges/' + challengeId, { - credentials: 'same-origin', - headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json' - }, - }) - .then(function (response) { - return response.json(); - }).then(function (data) { - data.solves = function () { - return fetch(urlRoot + '/api/v1/challenges/' + this.id + '/solves', { - credentials: 'same-origin', - headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json' - }, - }) - .then(function (response) { - return response.json(); - }).then(function (data) { - return data; - }); - }; - return data; - }); - }, - types: function(){ - return fetch(urlRoot + '/api/v1/challenges/types', { - credentials: 'same-origin', - headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json' - }, - }) - .then(function (response) { - return response.json(); - }).then(function (data) { - return data; - }); - }, - solves: function () { - return fetch(urlRoot + '/api/v1/statistics/challenges/solves', { - credentials: 'same-origin', - headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json' - }, - }) - .then(function (response) { - return response.json(); - }).then(function (data) { - return data; - }); + var challenges = {}; + + var scoreboard = function() {}; + + var teams = {}; + + var users = {}; + + var fetch = function(url, options) { + if (options === undefined) { + options = { + method: "GET", + credentials: "same-origin", + headers: {}, + }; } - }; + url = this.options.urlRoot + url; - var scoreboard = function() { - return fetch(urlRoot + '/api/v1/scoreboard', { - credentials: 'same-origin', - headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json' - }, - }) - .then(function (response) { - return response.json(); - }).then(function (data) { - return data; - }); - }; + options.credentials = 'same-origin'; + options.headers['Accept'] = 'application/json'; + options.headers['Content-Type'] = 'application/json'; + options.headers['CSRF-Token'] = this.options.csrfNonce; - var teams = { - all: function () { - return fetch(urlRoot + '/api/v1/teams', { - credentials: 'same-origin', - headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json' - }, - }) - .then(function (response) { - return response.json(); - }).then(function (data) { - return data; - }); - }, - get: function (teamId) { - return fetch(urlRoot + '/api/v1/teams/' + teamId, { - credentials: 'same-origin', - headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json' - }, - }) - .then(function (response) { - return response.json(); - }).then(function (data) { - data.solves = function () { - - }; - data.fails = function () { - - }; - data.awards = function () { - - }; - return data; - }); - }, - }; - - var users = { - all: function () { - return fetch(urlRoot + '/api/v1/users', { - credentials: 'same-origin', - headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json' - }, - }) - .then(function (response) { - return response.json(); - }).then(function (data) { - return data; - }); - }, - get: function (userId) { - return fetch(urlRoot + '/api/v1/users/' + userId, { - credentials: 'same-origin', - headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json' - }, - }) - .then(function (response) { - return response.json(); - }).then(function (data) { - data.solves = function () { - - }; - data.fails = function () { - - }; - data.awards = function () { - - }; - return data; - }); - }, + return window.fetch(url, options); }; return { @@ -175,5 +36,7 @@ var CTFd = (function () { scoreboard: scoreboard, teams: teams, users: users, + fetch: fetch, + options: options }; })(); \ No newline at end of file diff --git a/CTFd/themes/core/templates/base.html b/CTFd/themes/core/templates/base.html index cb73b7e7..e52071f3 100644 --- a/CTFd/themes/core/templates/base.html +++ b/CTFd/themes/core/templates/base.html @@ -4,15 +4,15 @@