From 21536f3c3c767cd08e06b7928962236b4271d8ae Mon Sep 17 00:00:00 2001 From: Emmanouil Konstantinidis Date: Mon, 27 Feb 2017 12:31:17 +0000 Subject: [PATCH] Pass authorisation header to coreapi client --- .../templates/rest_framework/docs/index.html | 25 ++++++++++++++++--- .../rest_framework/docs/sidebar.html | 2 +- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/rest_framework/templates/rest_framework/docs/index.html b/rest_framework/templates/rest_framework/docs/index.html index 22dc277c1..9a11811c1 100644 --- a/rest_framework/templates/rest_framework/docs/index.html +++ b/rest_framework/templates/rest_framework/docs/index.html @@ -68,6 +68,7 @@ } return cookieValue; } + const csrf = {'X-CSRFToken': getCookie('csrftoken')} const coreapi = window.coreapi @@ -173,7 +174,18 @@ form.find(".response-raw-response").text(panelText) } - const client = new coreapi.Client({csrf: csrf, requestCallback: requestCallback, responseCallback: responseCallback}) + let options = { + requestCallback: requestCallback, + responseCallback: responseCallback, + } + + if (window.auth && window.auth.type === 'token') { + options.headers = { + 'Authorization': window.auth.value + } + } + + const client = new coreapi.Client(options) client.action(doc, key, params).then(function (data) { var response = JSON.stringify(data, null, 2); @@ -205,15 +217,20 @@ $('form.authentication-form').submit(function(event) { event.preventDefault(); const form = $(this).closest("form"); - const token = window.token = form.find('input').val(); + const value = form.find('input').val(); $('#auth_token_modal').modal('hide'); $('#auth-control').children().removeClass('active'); - if (token) { + if (value) { + window.auth = { + 'type': 'token', + 'value': value, + }; $('#selected-authentication').text('token'); $('#auth-control').find("[data-auth='token']").addClass('active'); } else { + window.auth = null; $('#selected-authentication').text('none'); $('#auth-control').find("[data-auth='none']").addClass('active'); } @@ -221,7 +238,7 @@ $('#auth-control').find("[data-auth='none']").click(function (event) { event.preventDefault(); - window.token = null; + window.auth = null; $('#selected-authentication').text('none'); $('#auth-control').find("[data-auth='none']").addClass('active'); $('#auth-control').find("[data-auth='token']").removeClass('active'); diff --git a/rest_framework/templates/rest_framework/docs/sidebar.html b/rest_framework/templates/rest_framework/docs/sidebar.html index 19975b4c5..b0d4328dc 100644 --- a/rest_framework/templates/rest_framework/docs/sidebar.html +++ b/rest_framework/templates/rest_framework/docs/sidebar.html @@ -20,7 +20,7 @@ -