Skip to main content

Django Loves Sandstorm

Django Loves Sandstorm (D♥S, DLS… and djangolovessandstorm) provides tools for adapting Django applications to work with Sandstorm. For authentication, D♥S uses HTTP headers provided by Sandstorm to manage SandstormUser objects as required by Django Auth.

Installation and Configuration

D♥S requires installation in your Python environment and configuration in your Django project. This section documents the required configuration.

Apps

If using the suggested SandstormUser user model, the djangolovessandstorm app must be added to the Django INSTALLED_APPS list:

INSTALLED_APPS = [
    …
    "djangolovessandstorm.apps.DLSConfig",
    …
]

Authentication Backends

D♥S provides the DLSAllowAllUsersBackend authentication backend, which populates the SandstormUser model from the HTTP headers provided by the Sandstorm HTTP bridge. Use it by adding it to the AUTHENTICATION_BACKENDS list in the Django settings:

AUTHENTICATION_BACKENDS = [
    "djangolovessandstorm.backends.DLSAllowAllUsersBackend",
]

Authentication Model

D♥S suggests the use of the SandstormUser model. Set the AUTH_USER_MODEL in Django settings:

AUTH_USER_MODEL = "djangolovessandstorm.SandstormUser"

Middleware

D♥S requires the use of middleware to read these HTTP headers and create the appropriate SandstormUser objects. This middleware is found in the djangolovessandstorm.middleware module.

It is added to the Django settings MIDDLEWARE list. In the list of middleware, SessionMiddleware should appear before AuthenticationMiddleware. AuthenticationMiddleware should appear before DLSMiddleware:

MIDDLEWARE = [
    …
    "django.contrib.sessions.middleware.SessionMiddleware",
    "
    …
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    …
    "djangolovessandstorm.middleware.DLSMiddleware",
    …
]

Permissions

To use the Django Auth superuser and staff roles, configure the corresponding permissions in the Django settings. D♥S will look for these permissions in the X-Sandstorm-Permissions HTTP header, which will appear based upon the settings of bridgeConfig.viewInfo.permissions inside sandstorm-pkgdef.capnp. For example, the following two lines in your Django settings, will result in Django expecting superuser and staff to be set in sandstorm-pkgdef.capnp:

DLS_SUPERUSER_PERMISSION = "superuser"
DLS_STAFF_PERMISSION = "staff"

To make this work, superuser and staff should be named as permissions in sandstorm-pkgdef.capnp:

bridgeConfig = (
  viewInfo = (
    permissions = [
      (
        name = "superuser",
        title = (defaultText = "superuser"),
        description = (defaultText = "full control"),
      ),
      (
        name = "staff",
        title = (defaultText = "staff"),
        description = (defaultText = "administrator interface access"),
      ),
    ],
    roles = [
      (
        title = (defaultText = "Full access"),
        permissions = [
          true,  # superuser
          true,  # staff
        ],
        verbPhrase = (defaultText = "full access"),
        description = (defaultText = "may view and alter all data and settings"),
      ),
      (
        title = (defaultText = "Staff"),
        permissions = [
          false, # superuser
          true,  # staff
        ],
        verbPhrase = (defaultText = "staff may administer some applications"),
        description = (defaultText = "may view and alter all some data and settings through Django Admin"),
      ),
    ],
  ),
),

How It Works

D♥S middleware processes requests, creating and updating SandstormUser objects as necessary. To understand how the code works, look first at the middleware.

DLSMiddleware inherits from django.contrib.auth.middleware.RemoteUserMiddleware. To understand the code, begin by reading the process_request method.

DLSMiddleware.process_request calls RemoteUserMiddleware.process_request, which performs some sanity checks before calling django.contrib.auth.authenticate and django.contrib.auth.login. If the authentication backend does not find the user in the database, a new user is create``d, ``save``d and configured (``configure_user). DLSAllowAllUsersBackend.configure_user stores the userʼs name and handle, and generates a session key. (The session key is necessary because a SandstormUser does not have a password hash, which would otherwise function as the session key.)

Metadata

Release files for djangolovessandstorm 0.0.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 djangolovessandstorm 0.0.5
File Size Uploaded Provenance Actions
djangolovessandstorm-0.0.5.tar.gz 8.9 kB Without provenance Details for djangolovessandstorm-0.0.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for djangolovessandstorm 0.0.5
File Interpreter ABI Platform Actions
djangolovessandstorm-0.0.5-py3-none-any.whl Python 3 none any Details for djangolovessandstorm-0.0.5-py3-none-any.whl

Total release size: 18.2 kB

Release files / djangolovessandstorm-0.0.5.tar.gz

Download URL djangolovessandstorm-0.0.5.tar.gz
Size 8.9 kB
Tags Source
SHA-256 checksum
How to use checksums
b70bef8c21490f293701aec7736018ba2a253bc9e87aa7cc8eaaba419e15cf47
BLAKE2b-256 checksum
How to use checksums
fd6d434797c8546e0f14868ecaa6bd8d13658c029aa81f1249abf0bba0aa352a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.0

Release files / djangolovessandstorm-0.0.5-py3-none-any.whl

Download URL djangolovessandstorm-0.0.5-py3-none-any.whl
Size 9.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
23d9b512b0c25232b24169338d0e0c3405b61341f25ded54cb9c538ae3fbf08c
BLAKE2b-256 checksum
How to use checksums
9e695460915de1f3baf7f59823cbdbe75d98fb3b22100fe68fca550076ba9c1b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.0

Security for djangolovessandstorm 0.0.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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