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
thaliawebsite\.middleware module
.. automodule:: thaliawebsite.middleware
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:]
token_obj = Token.objects.get(key=key)
request.user = token_obj.user
except Token.DoesNotExist:
return self.get_response(request)
......@@ -98,6 +98,7 @@ MIDDLEWARE = [
# Our middleware
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