mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-06 05:20:12 +03:00
Use CoreAPI authentication support in API docs.
This commit is contained in:
parent
065bb275a4
commit
e7178a5f15
|
@ -1,28 +1,13 @@
|
||||||
function normalizeHTTPHeader(str) {
|
function normalizeHTTPHeader (str) {
|
||||||
return (str.charAt(0).toUpperCase() + str.substring(1))
|
// Capitalize HTTP headers for display.
|
||||||
.replace( /-(.)/g, function($1) { return $1.toUpperCase(); })
|
return (str.charAt(0).toUpperCase() + str.substring(1))
|
||||||
.replace( /(Www)/g, function($1) { return 'WWW'; })
|
.replace(/-(.)/g, function ($1) { return $1.toUpperCase() })
|
||||||
.replace( /(Xss)/g, function($1) { return 'XSS'; })
|
.replace(/(Www)/g, function ($1) { return 'WWW' })
|
||||||
.replace( /(Md5)/g, function($1) { return 'MD5'; })
|
.replace(/(Xss)/g, function ($1) { return 'XSS' })
|
||||||
|
.replace(/(Md5)/g, function ($1) { return 'MD5' })
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCookie(name) {
|
let responseDisplay = 'data'
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
let responseDisplay = 'data';
|
|
||||||
const coreapi = window.coreapi
|
const coreapi = window.coreapi
|
||||||
const schema = window.schema
|
const schema = window.schema
|
||||||
|
|
||||||
|
@ -127,22 +112,23 @@ $('form.api-interaction').submit(function(event) {
|
||||||
|
|
||||||
// Setup authentication options.
|
// Setup authentication options.
|
||||||
if (window.auth && window.auth.type === 'token') {
|
if (window.auth && window.auth.type === 'token') {
|
||||||
// Header authentication
|
// Header authentication
|
||||||
options.headers = {
|
options.auth = new coreapi.auth.TokenAuthentication({
|
||||||
'Authorization': window.auth.value
|
prefix: window.auth.scheme,
|
||||||
}
|
token: window.auth.token
|
||||||
|
})
|
||||||
} else if (window.auth && window.auth.type === 'basic') {
|
} else if (window.auth && window.auth.type === 'basic') {
|
||||||
// Basic authentication
|
// Basic authentication
|
||||||
const token = window.auth.username + ':' + window.auth.password
|
options.auth = new coreapi.auth.BasicAuthentication({
|
||||||
const hash = window.btoa(token)
|
username: window.auth.username,
|
||||||
options.headers = {
|
password: window.auth.password
|
||||||
'Authorization': 'Basic ' + hash
|
})
|
||||||
}
|
|
||||||
} else if (window.auth && window.auth.type === 'session') {
|
} else if (window.auth && window.auth.type === 'session') {
|
||||||
// Session authentication
|
// Session authentication
|
||||||
options.csrf = {
|
options.auth = new coreapi.auth.SessionAuthentication({
|
||||||
'X-CSRFToken': getCookie('csrftoken')
|
csrfCookieName: 'csrftoken',
|
||||||
}
|
csrfHeaderName: 'X-CSRFToken'
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const client = new coreapi.Client(options)
|
const client = new coreapi.Client(options)
|
||||||
|
@ -202,10 +188,12 @@ $('#auth-control').find("[data-auth='none']").click(function (event) {
|
||||||
$('form.authentication-token-form').submit(function(event) {
|
$('form.authentication-token-form').submit(function(event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
const form = $(this).closest("form");
|
const form = $(this).closest("form");
|
||||||
const value = form.find('input').val();
|
const scheme = form.find('input#scheme').val();
|
||||||
|
const token = form.find('input#token').val();
|
||||||
window.auth = {
|
window.auth = {
|
||||||
'type': 'token',
|
'type': 'token',
|
||||||
'value': value,
|
'scheme': scheme,
|
||||||
|
'token': token
|
||||||
};
|
};
|
||||||
$('#selected-authentication').text('header');
|
$('#selected-authentication').text('header');
|
||||||
$('#auth-control').children().removeClass('active');
|
$('#auth-control').children().removeClass('active');
|
||||||
|
@ -222,7 +210,7 @@ $('form.authentication-basic-form').submit(function(event) {
|
||||||
window.auth = {
|
window.auth = {
|
||||||
'type': 'basic',
|
'type': 'basic',
|
||||||
'username': username,
|
'username': username,
|
||||||
'password': password,
|
'password': password
|
||||||
};
|
};
|
||||||
$('#selected-authentication').text('basic');
|
$('#selected-authentication').text('basic');
|
||||||
$('#auth-control').children().removeClass('active');
|
$('#auth-control').children().removeClass('active');
|
||||||
|
|
File diff suppressed because one or more lines are too long
2040
rest_framework/static/rest_framework/js/coreapi-0.1.0.js
Normal file
2040
rest_framework/static/rest_framework/js/coreapi-0.1.0.js
Normal file
File diff suppressed because one or more lines are too long
|
@ -5,24 +5,29 @@
|
||||||
<div class="modal-dialog modal-md" role="document">
|
<div class="modal-dialog modal-md" role="document">
|
||||||
<div class="modal-content">
|
<div class="modal-content">
|
||||||
<div class="modal-header">
|
<div class="modal-header">
|
||||||
<h3 class="modal-title"><i class="fa fa-key"></i> Authentication Header</h3>
|
<h3 class="modal-title"><i class="fa fa-key"></i> Token Authentication</h3>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<form class="form-horizontal authentication-token-form">
|
<form class="form-horizontal authentication-token-form">
|
||||||
<div class="modal-body">
|
<div class="modal-body">
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="authorization" class="col-sm-2 control-label">Authorization:</label>
|
<label for="prefix" class="col-sm-2 control-label">Scheme:</label>
|
||||||
|
|
||||||
<div class="col-sm-10">
|
<div class="col-sm-10">
|
||||||
<input type="text" class="form-control" id="authorization" placeholder="Bearer XXXX-XXXX-XXXX-XXXX" aria-describedby="helpBlock" required>
|
<input type="text" class="form-control" id="scheme" placeholder="Bearer" aria-describedby="schemeHelpBlock" required>
|
||||||
<span id="helpBlock" class="help-block">The value to include for the <code>Authorization</code> header in outgoing HTTP requests.</span>
|
<span id="schemeHelpBlock" class="help-block">Either a registered authentication scheme such as <code>Bearer</code>, or a custom schema such as <code>Token</code> or <code>JWT</code>.</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="token" class="col-sm-2 control-label">Token:</label>
|
||||||
|
<div class="col-sm-10">
|
||||||
|
<input type="text" class="form-control" id="token" placeholder="XXXX-XXXX-XXXX-XXXX" aria-describedby="helpBlock" required>
|
||||||
|
<span id="tokenHelpBlock" class="help-block">A valid API token.</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="modal-footer">
|
<div class="modal-footer">
|
||||||
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
|
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
|
||||||
<button type="submit" class="btn btn-primary">Use Authentication Header</button>
|
<button type="submit" class="btn btn-primary">Use Token Authentication</button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
<link href="{% static 'rest_framework/docs/img/favicon.ico' %}" rel="shortcut icon">
|
<link href="{% static 'rest_framework/docs/img/favicon.ico' %}" rel="shortcut icon">
|
||||||
|
|
||||||
{% if code_style %}<style>{{ code_style }}</style>{% endif %}
|
{% if code_style %}<style>{{ code_style }}</style>{% endif %}
|
||||||
<script src="{% static 'rest_framework/js/coreapi-0.0.20.js' %}"></script>
|
<script src="{% static 'rest_framework/js/coreapi-0.1.0.js' %}"></script>
|
||||||
<script src="{% url 'api-docs:schema-js' %}"></script>
|
<script src="{% url 'api-docs:schema-js' %}"></script>
|
||||||
|
|
||||||
</head>
|
</head>
|
||||||
|
|
|
@ -22,7 +22,7 @@
|
||||||
</li>
|
</li>
|
||||||
<ul class="sub-menu collapse out" id="auth-control">
|
<ul class="sub-menu collapse out" id="auth-control">
|
||||||
<li data-auth="none" {% if not user.is_authenticated %}class="active"{% endif %}><a href="#" data-language="none">none</a></li>
|
<li data-auth="none" {% if not user.is_authenticated %}class="active"{% endif %}><a href="#" data-language="none">none</a></li>
|
||||||
<li data-auth="token" data-toggle="modal" data-target="#auth_token_modal"><a href="#">header</a></li>
|
<li data-auth="token" data-toggle="modal" data-target="#auth_token_modal"><a href="#">token</a></li>
|
||||||
<li data-auth="basic" data-toggle="modal" data-target="#auth_basic_modal"><a href="#">basic</a></li>
|
<li data-auth="basic" data-toggle="modal" data-target="#auth_basic_modal"><a href="#">basic</a></li>
|
||||||
<li data-auth="session" data-toggle="modal" data-target="#auth_session_modal" {% if user.is_authenticated %}class="active"{% endif %}><a href="#">session</a></li>
|
<li data-auth="session" data-toggle="modal" data-target="#auth_session_modal" {% if user.is_authenticated %}class="active"{% endif %}><a href="#">session</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user