Skip to main content

Simple Token-based authentication.

Overview

A lot of people talk about having Token Auth for their REST APIs… but what does it actually mean? And what benefit is it?

The token is cryptographically signed chunk of data. In this case it contains the user ID, backend, and a timestamp of when it was issued.

This lets you generate and issue tokens to phone apps, services, etc, and not have to deal with logins, passwords, CSRF, etc.

Install

Add to settings.MIDDLEWARE, after the default authentication middleware:

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'nap_token.middleware.NapTokenMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'cloudselect.middleware.CORSDefeat',
]

Usage

When you want to log in a user, call nap_token.get_auth_token(user), passing a User instance returned from django.contrib.auth.authenticate.

It will return a signed, timestamped token. The client need only pass this in a Authorization header, formatted as ‘Bearer {token}’, for the request to act as that user. If the token is absent, expired, or invalid, requset.user will fall back to the normal Session Based Auth.

Issuing Tokens

As a quick and dirty example of how to issue tokens, here’s an approach that will issue a token for a user who can log in:

from django.http import HttpResponse
from django.contrib.auth.views import LoginView

from nap_token import get_auth_token

class TokenView(LoginView):

    def form_valid(self, form):
        user = form.get_user()
        return HttpResponse(get_auth_token(user))

Release files for nap-token-auth 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nap-token-auth 0.1.2
File Size Uploaded
nap-token-auth-0.1.2.tar.gz 3.1 kB Details

Release files / nap-token-auth-0.1.2.tar.gz

Download URL nap-token-auth-0.1.2.tar.gz
Size 3.1 kB
Tags Source
SHA-256 checksum
How to use checksums
30a9899d53215156beb5a10d28fa61255a20576792fd1aa1ba4e79448e2e479d
BLAKE2b-256 checksum
How to use checksums
3a59a9276eba5d7c3832d5086781207fd4dad07265cb396a27e56e8554408950
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.2 This release

1 release file

0.1.0

1 release file

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