A reusable Django app that will invalidate all active sessions after change password.
Project description
A reusable Django app that will invalidate all active sessions after change password.
Since Django 1.7 this feature implemented out of the box.
Installation
Install a package.
$ pip install django-password-session
Add “password_session” to your INSTALLED_APPS setting:
INSTALLED_APPS = (
...
'password_session',
)
Add middleware:
MIDDLEWARE_CLASSES = (
...
'password_session.middleware.CheckPasswordHash',
),
Make sure that you have the following settings:
INSTALLED_APPS = (
...
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
)
AUTHENTICATION_BACKENDS = (
...
'django.contrib.auth.backends.ModelBackend',
)
MIDDLEWARE_CLASSES = (
...
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
)
To avoid logging out from a current session you should call the following function directly after change password:
from password_session import update_session_auth_hash
update_session_auth_hash(request, user)
Example view
It’s a very simple view for change password just for demonstrating how to call the signal. In real situation this view should be more complicated.
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from password_session import update_session_auth_hash
@login_required(login_url='/admin/')
def change_password_view(request):
user = request.user
user.set_password(request.POST.get('password'))
user.save()
update_session_auth_hash(request, user)
return HttpResponse("Hello, %s! Your password has been changed!" % user.username)
Requirements
Python 2.6+ or 3+
Django 1.3+
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
Hashes for django-password-session-0.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | d7feaea40e0ec75d0f14af872ecf5aedf9f8aa3fe3ccd9bd30f78bea36051976 |
|
MD5 | f94379a5ab9f2ea60069d3e7a4dda0d6 |
|
BLAKE2b-256 | 6f98bcdf5648082b466c8b357b3ed35ebffd5b5da22810a20edc2d26e63bf765 |