Skip to main content

Django-single-session

PyPi version Documentation Status PyPi license Code style: black

A Django app that enforces that a user has only one active session: if the user logs in on another browser/device, then the previous sessions will log out.

The app will also add an extra action to the ModelAdmin of the user model (if there is such ModelAdmin), that will alow to log out all sessions of a given (set of) user(s).

Installation

The package can be fetched as django-single-session, so for example with pip with:

pip3 install django-single-session

One can install the app by adding the single_session app to the INSTALLED_APPS setting:

# settings.py

# ...

INSTALLED_APPS = [
    # ...,
    'django.contrib.sessions',
    # ...,
    'single_session'
    # ...
]

MIDDLEWARE = [
    # ...,
    'django.contrib.sessions.middleware.SessionMiddleware',
    # ...,
    'django.contrib.auth.middleware.AuthenticationMiddlware',
    # ...
]

SESSION_ENGINE = 'django.contrib.sessions.backends.db'

For the SESSION_ENGINE setting, the database backend, django.contrib.sessions.backends.db should be used, since that is the one where the item is linking to.

In order to work properly, the SessionMiddleware and AuthenticationMiddleware will be necessary, or another middleware class that will add a .session and .user attribute on the request object and will trigger the user_logged_in and user_logged_out signals with the proper session and user.

and running migrate to migrate the database properly:

python3 manage.py migrate single_session

This will by default enforce that a user will only have one logged in session. This will not proactively logout existing sessions: only if the user logs in with another browser or device, the old session(s) will be closed.

Configuration

One can disable the single session behavior by setting SINGLE_USER_SESSION in settings.py to False (or any other value with truthiness False).

You can customise this behaviour by making the SINGLE_USER_SESSION setting be a string representing the name of a function which takes a user object as an argument. If this function returns True then the user will be logged out. If it returns False then the user will not be logged out.

The tool will also clean up all sessions of a user in case that user logs out. This thus means that if a user logs out on one browser/device, they will log out on all other browsers/devices as well. This functionality is still enabled if SINGLE_USER_SESSION is set to False. You can disable this by setting the LOGOUT_ALL_SESSIONS setting in settings.py to False (or any other value with truthiness False).

Logging out (other) users

If there is a ModelAdmin for the user model (if you use the default user model, then there is such ModelAdmin), and the django.contrib.admin package is installed, then that ModelAdmin will have extra actions to log out normal users and admin users.

You can thus select users, and log these out with the "Log out the user on all sessions" action. This will invalidate all the sessions for (all) the selected user(s). In order to do this, the single_session.logout permission is required, so only admin users and users with such permission can log out other users. Users with such permission can log out users, but not admin users.

There is an extra permission named single_session.logout_all to log out all users, including admin users. Users with such permission can thus also log out admin users, so it might be better not to give such permission to all (staff) users.

Contributors

  • @alastair implemented a system such that one can add a string that points to a callback for the SINGLE_USER_SESSION setting, to make it possible to determine what users should be logged out.

Metadata

Release files for django-single-session 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-single-session 0.2.0
File Size Uploaded
django-single-session-0.2.0.tar.gz 15.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-single-session 0.2.0
File Interpreter ABI Platform
django_single_session-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 26.5 kB

Release files / django-single-session-0.2.0.tar.gz

Download URL django-single-session-0.2.0.tar.gz
Size 15.2 kB
Tags Source
SHA-256 checksum
How to use checksums
25b5b2f42f662bfc80eaeb3b491714b580e84164d837177b70996d4e74d50324
BLAKE2b-256 checksum
How to use checksums
45513deeb726edd245403295b23536640f72285944a04554e01060fa9eb1656b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.2

Release files / django_single_session-0.2.0-py3-none-any.whl

Download URL django_single_session-0.2.0-py3-none-any.whl
Size 11.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
45cc6b2fc93f8811bc3f8d8a2e9757ec2c48a0a0bb686e436fc1667e069bde03
BLAKE2b-256 checksum
How to use checksums
358c0001e3784e49baf8048d67f8e871c88e727729d1ce05890e5ce91f12722d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.2

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page