mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-06 05:20:12 +03:00
Session Authentication
This commit is contained in:
parent
21536f3c3c
commit
ea4e6c1560
|
@ -801,7 +801,7 @@ class DocumentationRenderer(BaseRenderer):
|
||||||
template = 'rest_framework/docs/index.html'
|
template = 'rest_framework/docs/index.html'
|
||||||
code_style = 'emacs'
|
code_style = 'emacs'
|
||||||
|
|
||||||
def get_context(self, data):
|
def get_context(self, data, request):
|
||||||
from pygments.formatters import HtmlFormatter
|
from pygments.formatters import HtmlFormatter
|
||||||
from django.utils.html import mark_safe
|
from django.utils.html import mark_safe
|
||||||
formatter = HtmlFormatter(style=self.code_style)
|
formatter = HtmlFormatter(style=self.code_style)
|
||||||
|
@ -813,12 +813,13 @@ class DocumentationRenderer(BaseRenderer):
|
||||||
'document': data,
|
'document': data,
|
||||||
'langs': langs,
|
'langs': langs,
|
||||||
'code_style': code_style,
|
'code_style': code_style,
|
||||||
'schema': schema
|
'schema': schema,
|
||||||
|
'request': request
|
||||||
}
|
}
|
||||||
|
|
||||||
def render(self, data, accepted_media_type=None, renderer_context=None):
|
def render(self, data, accepted_media_type=None, renderer_context=None):
|
||||||
template = loader.get_template(self.template)
|
template = loader.get_template(self.template)
|
||||||
context = self.get_context(data)
|
context = self.get_context(data, renderer_context['request'])
|
||||||
return template_render(template, context, request=renderer_context['request'])
|
return template_render(template, context, request=renderer_context['request'])
|
||||||
|
|
||||||
|
|
||||||
|
|
1915
rest_framework/static/rest_framework/docs/js/coreapi.js
Normal file
1915
rest_framework/static/rest_framework/docs/js/coreapi.js
Normal file
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,35 @@
|
||||||
|
{% load rest_framework %}
|
||||||
|
|
||||||
|
<!-- Modal -->
|
||||||
|
<div class="modal fade auth-modal auth-session" id="auth_session_modal" tabindex="-1" role="dialog" aria-labelledby="session authentication modal">
|
||||||
|
<div class="modal-dialog modal-md" role="document">
|
||||||
|
<div class="modal-content">
|
||||||
|
<div class="modal-header">
|
||||||
|
<h3 class="modal-title"><i class="fa fa-key"></i> Session Authentication</h3>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<form class="form-horizontal authentication-session-form">
|
||||||
|
<div class="modal-body">
|
||||||
|
|
||||||
|
{% if user.is_authenticated %}
|
||||||
|
<h4 class="text-center">You are logged in as {{ user.username }}.</h4>
|
||||||
|
{% else %}
|
||||||
|
|
||||||
|
<div class="text-center">
|
||||||
|
<h4 class="text-center">You need to {% optional_docs_login request %} to enable Session Authentication.</h4>
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="modal-footer">
|
||||||
|
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
|
||||||
|
{% if user.is_authenticated %}
|
||||||
|
<button type="submit" class="btn btn-primary">Use Session Authentication</button>
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
|
@ -8,13 +8,13 @@
|
||||||
<h3 class="modal-title"><i class="fa fa-key"></i> Authentication Token</h3>
|
<h3 class="modal-title"><i class="fa fa-key"></i> Authentication Token</h3>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<form class="form-horizontal authentication-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="authorization" class="col-sm-2 control-label">Authorization:</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">
|
<input type="text" class="form-control" id="authorization" placeholder="Bearer XXXX-XXXX-XXXX-XXXX" aria-describedby="helpBlock" required>
|
||||||
<span id="helpBlock" class="help-block">The value to include for the <code>Authorization</code> header in outgoing HTTP requests.</span>
|
<span id="helpBlock" class="help-block">The value to include for the <code>Authorization</code> header in outgoing HTTP requests.</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -22,7 +22,7 @@
|
||||||
|
|
||||||
<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">Submit</button>
|
<button type="submit" class="btn btn-primary">Use Token Authentication</button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
|
|
@ -40,6 +40,7 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{% include "rest_framework/docs/auth/token.html" %}
|
{% include "rest_framework/docs/auth/token.html" %}
|
||||||
|
{% include "rest_framework/docs/auth/session.html" %}
|
||||||
|
|
||||||
<script src="{% static 'rest_framework/docs/js/jquery-1.10.2.min.js' %}"></script>
|
<script src="{% static 'rest_framework/docs/js/jquery-1.10.2.min.js' %}"></script>
|
||||||
<script src="{% static 'rest_framework/docs/js/bootstrap.min.js' %}"></script>
|
<script src="{% static 'rest_framework/docs/js/bootstrap.min.js' %}"></script>
|
||||||
|
@ -183,6 +184,10 @@
|
||||||
options.headers = {
|
options.headers = {
|
||||||
'Authorization': window.auth.value
|
'Authorization': window.auth.value
|
||||||
}
|
}
|
||||||
|
} else if (window.auth && window.auth.type === 'session') {
|
||||||
|
options.csrf = {
|
||||||
|
'X-CSRFToken': getCookie('csrftoken')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const client = new coreapi.Client(options)
|
const client = new coreapi.Client(options)
|
||||||
|
@ -214,35 +219,41 @@
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
$('form.authentication-form').submit(function(event) {
|
// Authentication: none
|
||||||
event.preventDefault();
|
|
||||||
const form = $(this).closest("form");
|
|
||||||
const value = form.find('input').val();
|
|
||||||
$('#auth_token_modal').modal('hide');
|
|
||||||
|
|
||||||
$('#auth-control').children().removeClass('active');
|
|
||||||
|
|
||||||
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');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
$('#auth-control').find("[data-auth='none']").click(function (event) {
|
$('#auth-control').find("[data-auth='none']").click(function (event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
window.auth = null;
|
window.auth = null;
|
||||||
$('#selected-authentication').text('none');
|
$('#selected-authentication').text('none');
|
||||||
|
$('#auth-control').children().removeClass('active');
|
||||||
$('#auth-control').find("[data-auth='none']").addClass('active');
|
$('#auth-control').find("[data-auth='none']").addClass('active');
|
||||||
$('#auth-control').find("[data-auth='token']").removeClass('active');
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Authentication: token
|
||||||
|
$('form.authentication-token-form').submit(function(event) {
|
||||||
|
event.preventDefault();
|
||||||
|
const form = $(this).closest("form");
|
||||||
|
const value = form.find('input').val();
|
||||||
|
window.auth = {
|
||||||
|
'type': 'token',
|
||||||
|
'value': value,
|
||||||
|
};
|
||||||
|
$('#selected-authentication').text('token');
|
||||||
|
$('#auth-control').children().removeClass('active');
|
||||||
|
$('#auth-control').find("[data-auth='token']").addClass('active');
|
||||||
|
$('#auth_token_modal').modal('hide');
|
||||||
|
});
|
||||||
|
|
||||||
|
// Authentication: session
|
||||||
|
$('form.authentication-session-form').submit(function(event) {
|
||||||
|
event.preventDefault();
|
||||||
|
window.auth = {
|
||||||
|
'type': 'session',
|
||||||
|
};
|
||||||
|
$('#selected-authentication').text('session');
|
||||||
|
$('#auth-control').children().removeClass('active');
|
||||||
|
$('#auth-control').find("[data-auth='session']").addClass('active');
|
||||||
|
$('#auth_session_modal').modal('hide');
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
<ul class="sub-menu collapse out" id="auth-control">
|
<ul class="sub-menu collapse out" id="auth-control">
|
||||||
<li data-auth="none" class="active"><a href="#" data-language="none">none</a></li>
|
<li data-auth="none" class="active"><a href="#" data-language="none">none</a></li>
|
||||||
<li data-auth="token" data-toggle="modal" data-target="#auth_token_modal"><a href="#">token</a></li>
|
<li data-auth="token" data-toggle="modal" data-target="#auth_token_modal"><a href="#">token</a></li>
|
||||||
|
<li data-auth="session" data-toggle="modal" data-target="#auth_session_modal"><a href="#">session</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<li data-toggle="collapse" data-target="#language-control" class="collapsed">
|
<li data-toggle="collapse" data-target="#language-control" class="collapsed">
|
||||||
|
|
|
@ -128,6 +128,22 @@ def optional_login(request):
|
||||||
return mark_safe(snippet)
|
return mark_safe(snippet)
|
||||||
|
|
||||||
|
|
||||||
|
@register.simple_tag
|
||||||
|
def optional_docs_login(request):
|
||||||
|
"""
|
||||||
|
Include a login snippet if REST framework's login view is in the URLconf.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
login_url = reverse('rest_framework:login')
|
||||||
|
except NoReverseMatch:
|
||||||
|
return 'log in'
|
||||||
|
|
||||||
|
snippet = "<a href='{href}?next={next}'>log in</a>"
|
||||||
|
snippet = format_html(snippet, href=login_url, next=escape(request.path))
|
||||||
|
|
||||||
|
return mark_safe(snippet)
|
||||||
|
|
||||||
|
|
||||||
@register.simple_tag
|
@register.simple_tag
|
||||||
def optional_logout(request, user):
|
def optional_logout(request, user):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue
Block a user