Skip to main content

Django Time-Token File Storage

Project description

This is a simple extension to Django’s FileSystemStorage that adds a URL parameter carrying a shared token, which is only valid for a defined period of time.

Functionality

This is a drop-in replacement for the Django FileSystemStorage, usable if media files are served by Django itself. It does currently not work if media files are served from an independent web server.

The storage and its accompanying view do the following:

  • When a URL to a storage file is generated, a HMAC-based token is generated

  • The token and the timestamp when it was generated are appended as request parameters to the URL

  • Upon retrieval of the file through the accompanying view, the requested file name and the passed timestamp are used to recalculate the HMAC-based token

  • Only if the tokens match, and a configured timeout has not passed, is the file served

The HMAC-based token ensures that the token is invalidated when:

  • The filename changes

  • The timestamp changes

  • The mtime of the file changes

  • The SECRET_KEY changes

The HMAC is salted with the SECRET_KEY.

Installation

To add django-titofisto to a project, first add it as dependency to your project, e.g. using poetry:

$ poetry add django-titofisto

django-titofisto will use the base FileSystemStorage for almost everything, including determining the MEDIA_ROOT. It merely adds a token as URL parameter to whatever the base FileSystemStorage.url() method returns.

Add the following to your settings:

DEFAULT_FILE_STORAGE = "titofisto.TitofistoStorage"
TITOFISTO_TIMEOUT = 3600  # optional, this is the default
TITOFISTO_PARAM = "titofisto_token"  # optional, this is the default

Add the following to your URL config:

from django.conf import settings
from django.urls import include, path

urlpatterns += [
    path(settings.MEDIA_URL.removeprefix("/"), include("titofisto.urls")),
]

Django will start serving media files under the configured MEDIA_URL.

Provide public media files

Sometimes, there might be media files, for example favicons, you want to be accessible without any authentication. Per default, django-titofisto will serve all files stored in the directory public without a token. You can disable or configure this behavior using these settings:

TITOFISTO_USE_PUBLIC_NAMESPACE = True # optional, this is the default TITOFISTO_PUBLIC_NAMESPACE = “public/” # optional, this is the default

Credits

django-titofisto was developed for the AlekSIS school information system by its team.

Copyright © 2021 Dominik George <dominik.george@teckids.org> Copyright © 2021 Jonathan Weth <dev@jonathanweth.de>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-titofisto-0.2.0.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

django_titofisto-0.2.0-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file django-titofisto-0.2.0.tar.gz.

File metadata

  • Download URL: django-titofisto-0.2.0.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.11 CPython/3.9.8 Linux/5.14.0-4-amd64

File hashes

Hashes for django-titofisto-0.2.0.tar.gz
Algorithm Hash digest
SHA256 d6cde43d3aa32f19633d5098de3fd1a0148cb4856963f0b5c0e31b3de257df99
MD5 69e2a645c543e37d4f5cfdd77b4966d9
BLAKE2b-256 6b8e115b0b99a4fe427d22658bb12e5d689a1e4467bf75c158c4fe52d72480b0

See more details on using hashes here.

File details

Details for the file django_titofisto-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: django_titofisto-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 15.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.11 CPython/3.9.8 Linux/5.14.0-4-amd64

File hashes

Hashes for django_titofisto-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 38d5298fe64d3c12a3a48767d8f1af4bd86cfc12787af0faaf3e141a29a12bb8
MD5 2269fec6e5cda86801c302e54d2de8c9
BLAKE2b-256 428da7d79dca6e0820bca7f0d9299b31c1ec7bd2a87bba7db9d00d6bec307ea4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page