2015-11-07 23:12:37 +08:00
|
|
|
from django.conf import settings
|
2009-03-15 13:54:28 +08:00
|
|
|
from django.contrib import auth
|
2012-09-10 04:25:06 +08:00
|
|
|
from django.contrib.auth import load_backend
|
|
|
|
from django.contrib.auth.backends import RemoteUserBackend
|
2009-03-15 13:54:28 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2015-11-07 23:12:37 +08:00
|
|
|
from django.utils.deprecation import MiddlewareMixin
|
2011-05-31 23:43:19 +08:00
|
|
|
from django.utils.functional import SimpleLazyObject
|
2009-03-15 13:54:28 +08:00
|
|
|
|
|
|
|
|
2011-05-31 23:43:19 +08:00
|
|
|
def get_user(request):
|
|
|
|
if not hasattr(request, '_cached_user'):
|
2011-06-06 20:11:48 +08:00
|
|
|
request._cached_user = auth.get_user(request)
|
2011-05-31 23:43:19 +08:00
|
|
|
return request._cached_user
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-03-15 13:54:28 +08:00
|
|
|
|
2015-11-07 23:12:37 +08:00
|
|
|
class AuthenticationMiddleware(MiddlewareMixin):
|
2006-05-02 09:31:56 +08:00
|
|
|
def process_request(self, request):
|
2014-03-31 20:10:59 +08:00
|
|
|
assert hasattr(request, 'session'), (
|
|
|
|
"The Django authentication middleware requires session middleware "
|
2015-11-07 23:12:37 +08:00
|
|
|
"to be installed. Edit your MIDDLEWARE%s setting to insert "
|
2014-03-31 20:10:59 +08:00
|
|
|
"'django.contrib.sessions.middleware.SessionMiddleware' before "
|
|
|
|
"'django.contrib.auth.middleware.AuthenticationMiddleware'."
|
2015-11-07 23:12:37 +08:00
|
|
|
) % ("_CLASSES" if settings.MIDDLEWARE is None else "")
|
2011-05-31 23:43:19 +08:00
|
|
|
request.user = SimpleLazyObject(lambda: get_user(request))
|
2009-03-15 13:54:28 +08:00
|
|
|
|
|
|
|
|
2015-11-07 23:12:37 +08:00
|
|
|
class RemoteUserMiddleware(MiddlewareMixin):
|
2009-03-15 13:54:28 +08:00
|
|
|
"""
|
2010-10-09 16:12:50 +08:00
|
|
|
Middleware for utilizing Web-server-provided authentication.
|
2009-03-15 13:54:28 +08:00
|
|
|
|
|
|
|
If request.user is not authenticated, then this middleware attempts to
|
|
|
|
authenticate the username passed in the ``REMOTE_USER`` request header.
|
|
|
|
If authentication is successful, the user is automatically logged in to
|
|
|
|
persist the user in the session.
|
|
|
|
|
|
|
|
The header used is configurable and defaults to ``REMOTE_USER``. Subclass
|
|
|
|
this class and change the ``header`` attribute if you need to use a
|
|
|
|
different header.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Name of request header to grab username from. This will be the key as
|
|
|
|
# used in the request.META dictionary, i.e. the normalization of headers to
|
|
|
|
# all uppercase and the addition of "HTTP_" prefix apply.
|
|
|
|
header = "REMOTE_USER"
|
2015-06-27 02:59:57 +08:00
|
|
|
force_logout_if_no_header = True
|
2009-03-15 13:54:28 +08:00
|
|
|
|
|
|
|
def process_request(self, request):
|
|
|
|
# AuthenticationMiddleware is required so that request.user exists.
|
|
|
|
if not hasattr(request, 'user'):
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"The Django remote user auth middleware requires the"
|
|
|
|
" authentication middleware to be installed. Edit your"
|
2015-11-07 23:12:37 +08:00
|
|
|
" MIDDLEWARE setting to insert"
|
2009-03-15 13:54:28 +08:00
|
|
|
" 'django.contrib.auth.middleware.AuthenticationMiddleware'"
|
|
|
|
" before the RemoteUserMiddleware class.")
|
|
|
|
try:
|
|
|
|
username = request.META[self.header]
|
|
|
|
except KeyError:
|
2012-09-10 04:25:06 +08:00
|
|
|
# If specified header doesn't exist then remove any existing
|
|
|
|
# authenticated remote-user, or return (leaving request.user set to
|
|
|
|
# AnonymousUser by the AuthenticationMiddleware).
|
2016-04-02 19:18:26 +08:00
|
|
|
if self.force_logout_if_no_header and request.user.is_authenticated:
|
2014-07-28 12:54:29 +08:00
|
|
|
self._remove_invalid_user(request)
|
2009-03-15 13:54:28 +08:00
|
|
|
return
|
|
|
|
# If the user is already authenticated and that user is the user we are
|
|
|
|
# getting passed in the headers, then the correct user is already
|
|
|
|
# persisted in the session and we don't need to continue.
|
2016-04-02 19:18:26 +08:00
|
|
|
if request.user.is_authenticated:
|
2012-10-13 11:44:50 +08:00
|
|
|
if request.user.get_username() == self.clean_username(username, request):
|
2009-03-15 13:54:28 +08:00
|
|
|
return
|
2014-07-28 12:54:29 +08:00
|
|
|
else:
|
|
|
|
# An authenticated user is associated with the request, but
|
|
|
|
# it does not match the authorized user in the header.
|
|
|
|
self._remove_invalid_user(request)
|
|
|
|
|
2009-03-15 13:54:28 +08:00
|
|
|
# We are seeing this user for the first time in this session, attempt
|
|
|
|
# to authenticate the user.
|
2016-07-11 22:40:39 +08:00
|
|
|
user = auth.authenticate(request, remote_user=username)
|
2009-03-15 13:54:28 +08:00
|
|
|
if user:
|
|
|
|
# User is valid. Set request.user and persist user in the session
|
|
|
|
# by logging the user in.
|
|
|
|
request.user = user
|
|
|
|
auth.login(request, user)
|
|
|
|
|
|
|
|
def clean_username(self, username, request):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Allow the backend to clean the username, if the backend defines a
|
2009-03-15 13:54:28 +08:00
|
|
|
clean_username method.
|
|
|
|
"""
|
|
|
|
backend_str = request.session[auth.BACKEND_SESSION_KEY]
|
|
|
|
backend = auth.load_backend(backend_str)
|
2017-09-07 20:16:21 +08:00
|
|
|
try:
|
2009-03-15 13:54:28 +08:00
|
|
|
username = backend.clean_username(username)
|
2017-09-07 20:16:21 +08:00
|
|
|
except AttributeError: # Backend has no clean_username method.
|
|
|
|
pass
|
2009-03-15 13:54:28 +08:00
|
|
|
return username
|
2014-07-28 12:54:29 +08:00
|
|
|
|
|
|
|
def _remove_invalid_user(self, request):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Remove the current authenticated user in the request which is invalid
|
2014-07-28 12:54:29 +08:00
|
|
|
but only if the user is authenticated via the RemoteUserBackend.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
stored_backend = load_backend(request.session.get(auth.BACKEND_SESSION_KEY, ''))
|
|
|
|
except ImportError:
|
|
|
|
# backend failed to load
|
|
|
|
auth.logout(request)
|
|
|
|
else:
|
|
|
|
if isinstance(stored_backend, RemoteUserBackend):
|
|
|
|
auth.logout(request)
|
2015-06-27 02:59:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
class PersistentRemoteUserMiddleware(RemoteUserMiddleware):
|
|
|
|
"""
|
|
|
|
Middleware for Web-server provided authentication on logon pages.
|
|
|
|
|
|
|
|
Like RemoteUserMiddleware but keeps the user authenticated even if
|
|
|
|
the header (``REMOTE_USER``) is not found in the request. Useful
|
|
|
|
for setups when the external authentication via ``REMOTE_USER``
|
|
|
|
is only expected to happen on some "logon" URL and the rest of
|
|
|
|
the application wants to use Django's authentication mechanism.
|
|
|
|
"""
|
|
|
|
force_logout_if_no_header = False
|