Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
thalia
concrexit
Commits
edbe1785
Verified
Commit
edbe1785
authored
Mar 01, 2018
by
Sébastiaan Versteeg
Browse files
Add TokenMiddleware
parent
db3f307b
Changes
3
Hide whitespace changes
Inline
Side-by-side
docs/thaliawebsite.rst
View file @
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
------------------------------
...
...
website/thaliawebsite/middleware.py
0 → 100644
View file @
edbe1785
"""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
)
website/thaliawebsite/settings/settings.py
View file @
edbe1785
...
...
@@ -98,6 +98,7 @@ MIDDLEWARE = [
'django.middleware.clickjacking.XFrameOptionsMiddleware'
,
'django.middleware.locale.LocaleMiddleware'
,
# Our middleware
'thaliawebsite.middleware.TokenMiddleware'
,
'members.middleware.MemberMiddleware'
,
]
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment