Django identity.external middlewares
Set of middlewares to simplify consumption of external identity information in Web projects set up with Django Web framework.
When non-standard (different than
REMOTE_USER) environment variable is
used to pass information about externally authenticated user, this
middleware can be used to customize the variable name without writing
For example, when consuming the information from some authentication
HTTP proxy, HTTP request header values are passed as
environment variables. If the authenticated user name is in
HTTP request header, it is available in
environment variable. Setting variable
HTTP_X_REMOTE_USER, for example with Apache HTTP Server directive
SetEnv REMOTE_USER_VAR HTTP_X_REMOTE_USER
MIDDLEWARE list after
MIDDLEWARE = [ ... 'django.contrib.auth.middleware.AuthenticationMiddleware', 'identity.external.PersistentRemoteUserMiddlewareVar', ... ]
with value from environment variable
When user is externally authenticated, for example via
user attributes can be provided by the external authentication source.
This middleware will update user's email address, first and last name, and group membership in groups prefixed with ext: with information coming from environment variables
REMOTE_USER prefix of these variables can be changed with the
REMOTE_USER_VAR environment variable, just like with
The values are used verbating, as provided by Django. When
REMOTE_USER_VALUES_ENCODING environment variable is set to
the values are expected to be in this format and decoded to Unicode.
Users that are in external group
admins (and thus get assigned to group
ext:admins in Django) will also get the
is_staff flag set and thus
will be able to log in to the admin application.
ext: prefixed groups have to be already created in Django database for
the user membership to be updated in them.
MIDDLEWARE list, this middleware has to be listed after the
authenticating middleware, for example
MIDDLEWARE = [ ... 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.PersistentRemoteUserMiddleware', 'identity.external.RemoteUserAttrMiddleware', ... ]
- External authentication for Django projects: https://www.adelton.com/django/external-authentication-for-django-projects Presentation at EuroPython 2015.