Verified Commit edbe1785 authored by Sébastiaan Versteeg's avatar Sébastiaan Versteeg
Browse files

Add TokenMiddleware

parent db3f307b
...@@ -42,6 +42,14 @@ thaliawebsite\.menus module ...@@ -42,6 +42,14 @@ thaliawebsite\.menus module
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:
thaliawebsite\.middleware module
--------------------------------
.. automodule:: thaliawebsite.middleware
:members:
:undoc-members:
:show-inheritance:
thaliawebsite\.sitemaps module 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 = [ ...@@ -98,6 +98,7 @@ MIDDLEWARE = [
'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.locale.LocaleMiddleware', 'django.middleware.locale.LocaleMiddleware',
# Our middleware # Our middleware
'thaliawebsite.middleware.TokenMiddleware',
'members.middleware.MemberMiddleware', '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