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.
Python JavaScript HTML CSS
Latest commit 53088eb Mar 2, 2016 @kiowa kiowa committed with jpic Fix #62 Catch TypeError when converting string to time
This happens when upgrading from old versions and
we have old sessions that has some old time formats.
Permalink
Failed to load latest commit information.
docs Fixed script documentation Feb 7, 2017
session_security Fix #62 Catch TypeError when converting string to time Apr 25, 2017
test_project Require SESSION_EXPIRE_AT_BROWSER_CLOSE Aug 17, 2016
.gitignore Django 1.9 support Dec 8, 2015
.travis.yml Test python 3.5 instead of 3.5 and django 1.11 instead of 1.7 Feb 5, 2017
AUTHORS Add myself to Authors Dec 14, 2015
CHANGELOG Release 2.5.1 [ci skip] Feb 7, 2017
MANIFEST.in Version bump 1.2.2 Feb 14, 2013
README.rst Update README Feb 5, 2017
setup.py Release 2.5.1 [ci skip] Feb 7, 2017
tox.ini

README.rst

https://secure.travis-ci.org/yourlabs/django-session-security.png?branch=master

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.

Requirements

  • Python 2.7 or 3.5+
  • jQuery 1.7+
  • Django 1.8 to 2.0
  • django.contrib.staticfiles or #YoYo

Resources

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