Skip to main content
https://coveralls.io/repos/github/smartfile/django-session-jwt/badge.svg?branch=master https://travis-ci.org/smartfile/django-session-jwt.svg?branch=master

django-session-jwt

This is a session middleware that extends the django.contrib.sessions system. It is compatible with Django sessions insofar as it utilizes a session key stored in a cookie. The difference is in the format of the coookie.

django.contrib.sessions stores a cookie named settings.SESSION_COOKIE_NAME which contains a value such as: "5a6aybftilfw60wl9ehwrg4ybzawr9b4". The session key is a key used server-side to fetch additional data associated with a session. This data is stored in a backend such as a database or memcache.

django_session_jwt.sessions enhances this behavior by modifying the format of the session cookie. Instead of writing the session key as the value of the cookie, it writes a JWT which contains the session key. In addition to the session key, the JWT can contain other desirable fields.

The reason for this extension is to allow one to utilize django server-side sessions without modification while also providing a JWT for use by other federated services. While this could be accomplished by using multiple cookies, the JWT is both a client-side store as well as containing the key to a server-side store.

Installation

This module respects the settings for django.contrib.sessions and provides additional options for controlling the JWT.

pip install django-session-jwt

Then modify settings.py:

SESSION_ENGINE='django_session_jwt.engine'
SESSION_COOKIE_NAME='sessionid'

DJANGO_SESSION_JWT = {
    'FIELDS': (
        'a list',
        ('of attributes', 'a'),
        'of the user',
        ('object to place', 'p'),
        'in the jwt',
    ),
    'KEY': 'string value or path to PEM key file',
    ,
    ...
}

As an optimization, the FIELDS list can contain tuples ('field_name', 'sn') providing a short name for the field. The JWT will contain the short name, but it will be converted when decoded. This can help reduce the size of the jWT.

Using the JWT

By default, the JWT will contain a single value "sk" and will be slightly larger than the default session cookie. The whole point of this application is to allow you to add additional fields to the JWT that can be used by other services running along side your Django application.

Once installed and configured, the browser will send the session cookie with each request. This cookie is verified and the sk / session key is utilized to set up Django sessions within the Django application. External applications can utilize the JWT directly, so you should define any “global” fields in the DJANGO_SESSION_JWT['FIELDS'] list / tuple. This way, legacy data stored within Django’s session does not pollute the JWT and vice/versa.

You can use a symmetric key or asymmetric key pair. In the simplest case, you can set DJANGO_SESSION_JWT['KEY'] = SECRET_KEY. You will then need to distribute the SECRET_KEY to all federated services. Another option is to use an asymmetric key pair such as an RSA key pair. This way the Django application alone holds the private key for signing JWTs while federated services hold only the public key for verifying the signature. A hybrid configuration might share the private key with a number of federated services for the purpose of issuing or extending JWTs while limiting other services to just the public key.

No library is provided for consuming the JWT, federated services should use available JWT libraries for verifying and extracting fields from the JWT.

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-jwt-0.4.tar.gz (6.7 kB view details)

Uploaded Source

File details

Details for the file django-session-jwt-0.4.tar.gz.

File metadata

  • Download URL: django-session-jwt-0.4.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/44.1.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/2.7.15

File hashes

Hashes for django-session-jwt-0.4.tar.gz
Algorithm Hash digest
SHA256 d95b322356cafe55894c8edfba8efe8597ee6678783446dbe4d3032c0ea6c1b8
MD5 a3193f7b0b7cb2d64eab36a942ac46e6
BLAKE2b-256 900de9cd7d1df325849e3dd23f8b1afb607882021fada3f1620131a7ca084978

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 Sentry Error logging StatusPage Status page