Skip to content

django-httponly-jwt

JWT sessions for Django in HttpOnly cookies, renewed by the server so quietly that the frontend never holds a token.

Signing in sets two HttpOnly cookies. When the access token runs out, the server renews it on the way through - the request that found it expired is answered, and the answer carries the new one. The frontend sends requests with the cookies and goes to the sign-in screen on a 401.

  • No token in the page. HttpOnly cookies: no script can read them, so a script injected into the page cannot take the session away.
  • No refresh dance. No interceptor, no retry, no race between tabs: the server renews, and the request is answered.
  • A session ends when it should - at the next renewal after the account is switched off, and everywhere at once when the person signs out.
  • Refused at start-up when it cannot work or protects nothing - not on the day somebody finds out.

Install

pip install django-httponly-jwt
INSTALLED_APPS = [..., 'rest_framework_simplejwt.token_blacklist', 'httponly_jwt']

MIDDLEWARE = [..., 'httponly_jwt.middleware.RenewalMiddleware', ...]

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'httponly_jwt.authentication.CookieAuthentication',
        'httponly_jwt.authentication.HeaderAuthentication',
    ],
}

Then a sign-in view hands the person to start_session - see the quickstart.

Where to go

Introduction what goes wrong with tokens in the browser, and what this does about it
Quickstart a project signing people in, in five minutes
The guide every part, from signing in to a frontend on another site
How-to guides Next.js, moving off localStorage, an audit trail, signing out everywhere, testing
Reference settings, checks, signals, and the Python API
Design why it is built this way
Questions is a cookie safer, why Lax, what it costs

Django 5.2 to 6.1, Python 3.11 to 3.14, over djangorestframework-simplejwt. English and Persian. BSD licence.