Skip to main content
django-sesame

django-sesame provides frictionless authentication with “Magic Links” for your Django project.

It generates URLs containing authentication tokens such as: https://example.com/?sesame=zxST9d0XT9xgfYLvoa9e2myN

Then it authenticates users based on tokens found in URLs.

More broadly, it supports a wide range of use cases for stateless, token-based authentication.

Please review (in)security considerations before using django-sesame.


Documentation is available on ReadTheDocs.


Requirements

django-sesame is tested with:

  • Django 5.2 (LTS), 6.0, and 6.1;

  • Python ≥ 3.11.

It requires django.contrib.auth.

Getting started

Install django-sesame:

$ pip install django-sesame

Open your project settings and add "sesame.backends.ModelBackend" to the AUTHENTICATION_BACKENDS setting. Extending the default value, this looks like:

AUTHENTICATION_BACKENDS = [
    "django.contrib.auth.backends.ModelBackend",
    "sesame.backends.ModelBackend",
]

Now, your project can authenticate users based on django-sesame tokens.

Quick example

Configure LoginView in your URLconf:

from django.urls import path
from sesame.views import LoginView

urlpatterns = [
    ...,
    path("sesame/login/", LoginView.as_view(), name="sesame-login"),
    ...,
]

Load a user from the database:

>>> from django.contrib.auth import get_user_model
>>> User = get_user_model()
>>> user = User.objects.first()

Generate a login URL for this user:

>>> from sesame.utils import get_query_string
>>> LOGIN_URL = "https://127.0.0.1:8000/sesame/login/"
>>> LOGIN_URL + get_query_string(user)
'https://127.0.0.1:8000/sesame/login/?sesame=zxST9d0XT9xgfYLvoa9e2myN'

(Your token will be different from this example.)

Make sure that you’re logged out. Open the login URL. You are logged in!

Use cases

Known use cases for django-sesame include:

  1. Login by email, an attractive option on mobile where typing passwords is uncomfortable. This technique is prominently deployed by Slack.

    If you’re doing this, you should define a small SESAME_MAX_AGE, perhaps 10 minutes.

  2. Authenticated links. For example, you can generate a report offline and, when it’s ready, email a link to access it. Authenticated links work even if the user isn’t logged in on the device where they’re opening it.

    Likewise, you should configure an appropriate SESAME_MAX_AGE, probably a few days.

    Since emails may be forwarded, authenticated links shouldn’t log the user in. They should only allow access to specific views.

  3. Sharing links, which are a variant of authenticated links. When a user shares content with a guest, you may create a phantom account for the guest and generate an authenticated link tied to that account or you may reuse the user’s account.

    Email forwarding is also likely in this context. Make sure that sharing links don’t log the user in.

  4. Authentication of WebSocket connections. The web application gets a token generated by the Django server and sends it over the WebSocket connection. The WebSocket server authenticate the connection with the token.

    Here’s an example with the websockets library.

  5. Non-critical private websites, for example for a family or club site, where users don’t expect to manage a personal account with a password. Authorized users can bookmark personalized authenticated URLs.

    Here you can rely on the default settings because that’s the original — admittedly, niche — use case for which django-sesame was built.

(In)security

The major security weakness in django-sesame is a direct consequence of the feature it implements: whoever obtains an authentication token is able to authenticate to your website.

URLs end up in countless insecure places: emails, referer headers, proxy logs, browser history, etc. You can’t avoid that. At best you can mitigate it by creating short-lived or single-use tokens.

Otherwise, a reasonable attempt was made to provide a secure solution. Tokens are secured with modern cryptography. There are configurable options for token invalidation.

Metadata

Release files for django-sesame 3.2.5

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-sesame 3.2.5
File Size Uploaded
django_sesame-3.2.5.tar.gz 17.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-sesame 3.2.5
File Interpreter ABI Platform
django_sesame-3.2.5-py3-none-any.whl Python 3 none any Details

Total release size: 37.9 kB

Release files / django_sesame-3.2.5.tar.gz

Download URL django_sesame-3.2.5.tar.gz
Size 17.6 kB
Tags Source
SHA-256 checksum
How to use checksums
eeb662069ea74d635ab11e0f93446c8a7c187f41b0788ddc3c6c72c6f8db2eb1
BLAKE2b-256 checksum
How to use checksums
c39c43e3b6981e864f23d438eb07f691c6d005c07d5d6ef149f367d0e4abf78b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.5 CPython/3.12.11 Darwin/27.0.0

Release files / django_sesame-3.2.5-py3-none-any.whl

Download URL django_sesame-3.2.5-py3-none-any.whl
Size 20.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dfca2b0e0d480408894d9d0423109dc659d186e1946184f62506eeda2b4377c1
BLAKE2b-256 checksum
How to use checksums
323502e4f7602e56427a9bcb9a8445c1b9d18d4e4b0c5f751daef8dda5b79232
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.5 CPython/3.12.11 Darwin/27.0.0

Release history Release notifications | RSS feed

This release

3.2.5 This release

2 release files

3.2.3

2 release files

3.2.2

2 release files

3.2.1

2 release files

3.2

2 release files

3.1

2 release files

3.0

2 release files

2.4

2 release files

2.3

2 release files

2.2

2 release files

2.1

2 release files

2.0

2 release files

1.8

2 release files

1.7

2 release files

1.6

2 release files

1.5

2 release files

1.4

2 release files

1.3

2 release files

1.2

2 release files

1.1

1 release file

1.0

1 release file

0.1

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