Commit 0c3d89ad authored by Thom Wiggers's avatar Thom Wiggers 📐
Browse files

Merge branch 'feature/token-middleware' into 'master'

Add TokenMiddleware

See merge request !738
parents db3f307b edbe1785
......@@ -42,6 +42,14 @@ thaliawebsite\.menus module
:undoc-members:
:show-inheritance:
thaliawebsite\.middleware module
--------------------------------
.. automodule:: thaliawebsite.middleware
:members:
:undoc-members:
:show-inheritance:
thaliawebsite\.sitemaps module
------------------------------
......
"""Defines the middleware of the thaliawebsite package"""
from rest_framework.authtoken.models import Token
class TokenMiddleware:
"""
Adds a piece of middleware to Django
that allows the user to authenticate using the Django Rest Framework
Token in the HTTP Authorization header.
You could use it to open a private thumbnail without
having a session, for example.
"""
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
if request.META.get('HTTP_AUTHORIZATION'):
key = request.META.get('HTTP_AUTHORIZATION')[6:]
try:
token_obj = Token.objects.get(key=key)
request.user = token_obj.user
except Token.DoesNotExist:
pass
return self.get_response(request)
......@@ -98,6 +98,7 @@ MIDDLEWARE = [
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.locale.LocaleMiddleware',
# Our middleware
'thaliawebsite.middleware.TokenMiddleware',
'members.middleware.MemberMiddleware',
]
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment