Skip to main content

Client and server side session timeout with warnings

Project description

Latest version Unit tests Documentation Status Supported python versions License
Supported python versions

Python 3.10, 3.11, 3.12, 3.13, 3.14

Supported django versions

Django 4.2, 5.0, 5.1, 5.2, 6.0

A little javascript and middleware work together to ensure that the user was active during the past X minutes in any tab he has open. Otherwise, display a warning leaving a couple of minutes to show any kind of activity like moving the mouse. Otherwise, logout the user.

Documentation

https://django-session-security.readthedocs.io/

About

This app provides a mechanism to logout inactive authenticated users. An inactive browser should be logged out automatically if the user left his workstation, to protect sensitive data that may be displayed in the browser. It may be useful for CRMs, intranets, and such projects.

For example, if the user leaves for a coffee break, this app can force logout after say 5 minutes of inactivity.

Why not just set the session to expire after X minutes ?

Or “Why does this app even exist” ? Here are the reasons:

  • if the user session expires before the user is done reading a page: he will have to login again.

  • if the user session expires before the user is done filling a form: his work will be lost, and he will have to login again, and probably yell at you, dear django dev … at least I know I would !

This app allows to short circuit those limitations in session expiry.

How does it work ?

When the user loads a page, SessionSecurity middleware will set the last activity to now. The last activity is stored as datetime in request.session['_session_security']. To avoid having the middleware update that last activity datetime for a URL, add the url to settings.SESSION_SECURITY_PASSIVE_URLS.

When the user moves mouse, click, scroll or press a key, SessionSecurity will save the DateTime as a JavaScript attribute. It will send the number of seconds since when the last user activity was recorded to PingView, next time it should ping.

First, a warning should be shown after settings.SESSION_SECURITY_WARN_AFTER seconds. The warning displays a text like “Your session is about to expire, move the mouse to extend it”.

Before displaying this warning, SessionSecurity will upload the time since the last client-side activity was recorded. The middleware will take it if it is shorter than what it already has - ie. another more recent activity was detected in another browser tab. The PingView will respond with the number of seconds since the last activity - all browser tab included.

If there was no other, more recent, activity recorded by the server: it will show the warning. Otherwise it will update the last activity in javascript from the PingView response.

Same goes to expire after settings.SESSION_SECURITY_EXPIRE_AFTER seconds. Javascript will first make an ajax request to PingView to ensure that another more recent activity was not detected anywhere else - in any other browser tab.

Note to SSO (single sign-on) users

By default, this package reloads the current page after timeout, prompting a user to log back into the application to resume where they left off. When using SSO, however, this can produce confusing behavior. For example, if the SSO session is still alive, a user may by automatically logged back into the application.

To avoid this behavior, some users (c.f. issue #93) want the timeout to end the SSO login as well. On a properly configured application, this will happen if you set settings.SESSION_SECURITY_REDIRECT_TO_LOGOUT to True. When the timeout is reached, users will be redirected to the application’s logout page configured at settings.LOGOUT_REDIRECT_URL.

Please note that this is not an adequate security model. If a user closes the browser page before logging out, this setting will have no effect on the SSO session. At minimum, a similar timeout should be added to the SSO server for users on “public machines” to ensure the SSO session is also timed out.

Requirements

  • Python 3.10+

  • jQuery 1.7+

  • Django 4.2 to 6.0

  • django.contrib.staticfiles

Resources

You could subscribe to the mailing list ask questions or just be informed of package updates.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_session_security-2.6.8.tar.gz (104.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_session_security-2.6.8-py3-none-any.whl (116.1 kB view details)

Uploaded Python 3

File details

Details for the file django_session_security-2.6.8.tar.gz.

File metadata

  • Download URL: django_session_security-2.6.8.tar.gz
  • Upload date:
  • Size: 104.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for django_session_security-2.6.8.tar.gz
Algorithm Hash digest
SHA256 f8949d021eee5e237385413a1173bb90c3bd80ce2d56e412a5644608de815109
MD5 bc783acf9b0109ac8acf1ebf9579ae62
BLAKE2b-256 3a7a69a922ce628b7d91acb48101170741662f641cf7810d4a6273e3e37c702b

See more details on using hashes here.

File details

Details for the file django_session_security-2.6.8-py3-none-any.whl.

File metadata

File hashes

Hashes for django_session_security-2.6.8-py3-none-any.whl
Algorithm Hash digest
SHA256 702a7a867a668cb86b02101477954ae2181d8bac99844d70debc35bfbbaa80d9
MD5 6919d576cacdc07ae022f7347c54b40a
BLAKE2b-256 e3fb4d39107e662fee72d53cc80d71d5c9304c794d2638724b9e76586f98f3e6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page