Skip to main content

Django Session Header: Identify the session through a header

There are some situations where the browser may not allow any cookies at all to be used. In those cases, we would like to be able to fall back to something that is both secure, and capable. This package allows you to manually pass the sessionid using a header, so that you can continue to use Django’s excellent session management.

It extends Django’s built-in sessions to support sessions in places where cookies are not allowed. For most views, the handling will be seamless. Those that need to have sessions that persist despite the absence of cookies, there are a few extra features.

Usage

First, install the package.

pip install django-session-header

Replace django.contrib.sessions.middleware.SessionMiddleware in your settings.py with the following:

MIDDLEWARE_CLASSES = [
   # ...
   # 'django.contrib.session.middleware.SessionMiddleware',
   'django_session_header.middleware.SessionMiddleware',
]

And replace the Django Rest Framework SessionAuthentication class with django_session_header.authentication.SessionAuthentication:

REST_FRAMEWORK = {
    'DEFAUlT_AUTHENTICATION_CLASSES': [
        # ...
        # 'rest_framework.authentication.SessionAuthentication',
        'django_session_header.authentication.SessionAuthentication',
    ]
}

If a session was obtained via a session header, then request.session.csrf_exempt will be True. You can use this to conditionally apply CSRF protection. Or, if you prefer, you can replace Django’s normal CSRF middleware with django_session_header.middleware.CsrfViewMiddleware:

MIDDLEWARE_CLASSES = [
    # ...
    # 'django.middleware.csrf.CsrfViewMiddleware',
    'django_session_header.middleware.CsrfViewMiddleware',
]

The sessionid will be available in the X-SessionID response header, and you can now set the X-SessionID header on the request manually to avoid needing cookies to power your sessions.

Metadata

Release files for django-session-header-11 1.0.1

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-session-header-11 1.0.1
File Size Uploaded
django-session-header-11-1.0.1.tar.gz 4.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-session-header-11 1.0.1
File Interpreter ABI Platform
django_session_header_11-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 11.5 kB

Release files / django-session-header-11-1.0.1.tar.gz

Download URL django-session-header-11-1.0.1.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
f4ed65882ecba753d7c898809215a5c6b5092c05f5324aadebcc9fcc7e048a54
BLAKE2b-256 checksum
How to use checksums
0eece24a727bf3d60f8b341b96425f7bb2041b1fccb1c1162ee9e59a54561c36
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.7.8 Linux/5.8.18-1-MANJARO

Release files / django_session_header_11-1.0.1-py3-none-any.whl

Download URL django_session_header_11-1.0.1-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2c5c4eb0fbe0b65376ee495117a41b3009807376bb63add559504d5b7d7df263
BLAKE2b-256 checksum
How to use checksums
929fc0d038346ba11df02763ee07e225e9831fdc5293f4e4599294f9a873bd4a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.7.8 Linux/5.8.18-1-MANJARO

Release history Release notifications | RSS feed

This release

1.0.1 This release

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