| 1 |
Metadata-Version: 2.1 |
| 2 |
Name: django-titofisto |
| 3 |
Version: 1.1.0 |
| 4 |
Summary: Django Time-Token File Storage |
| 5 |
Home-page: https://edugit.org/AlekSIS/libs/django-titofisto |
| 6 |
License: Apache-2.0 |
| 7 |
Keywords: django,storage,media,secure |
| 8 |
Author: Dominik George |
| 9 |
Author-email: [email protected] |
| 10 |
Requires-Python: >=3.9,<4.0 |
| 11 |
Classifier: Development Status :: 4 - Beta |
| 12 |
Classifier: Environment :: Web Environment |
| 13 |
Classifier: Framework :: Django |
| 14 |
Classifier: Intended Audience :: Developers |
| 15 |
Classifier: License :: OSI Approved :: Apache Software License |
| 16 |
Classifier: Programming Language :: Python :: 3 |
| 17 |
Classifier: Programming Language :: Python :: 3.9 |
| 18 |
Classifier: Programming Language :: Python :: 3.10 |
| 19 |
Classifier: Programming Language :: Python :: 3.11 |
| 20 |
Requires-Dist: Django (>4.2,<6.0) |
| 21 |
Requires-Dist: shortuuid (>=1.0.11,<2.0.0) |
| 22 |
Project-URL: Repository, https://edugit.org/AlekSIS/libs/django-titofisto |
| 23 |
Description-Content-Type: text/x-rst |
| 24 |
|
| 25 |
Django Time-Token File Storage |
| 26 |
============================== |
| 27 |
|
| 28 |
This is a simple extension to Django's `FileSystemStorage` that adds a URL |
| 29 |
parameter carrying a shared token, which is only valid for a defined period |
| 30 |
of time. |
| 31 |
|
| 32 |
Additionally, a like-wise time-constrained file upload slot mechanism is |
| 33 |
available. |
| 34 |
|
| 35 |
Functionality |
| 36 |
------------- |
| 37 |
|
| 38 |
File storage with token-secured URLs |
| 39 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 40 |
|
| 41 |
This is a drop-in replacement for the Django `FileSystemStorage`, usable if |
| 42 |
media files are served by Django itself. It does currently not work if media |
| 43 |
files are served from an independent web server. |
| 44 |
|
| 45 |
The storage and its accompanying view do the following: |
| 46 |
|
| 47 |
* When a URL to a storage file is generated, a HMAC-based token is generated |
| 48 |
* The token and the timestamp when it was generated are appended as request |
| 49 |
parameters to the URL |
| 50 |
* Upon retrieval of the file through the accompanying view, the requested |
| 51 |
file name and the passed timestamp are used to recalculate the HMAC-based |
| 52 |
token |
| 53 |
* Only if the tokens match, and a configured timeout has not passed, is the |
| 54 |
file served |
| 55 |
|
| 56 |
The signature-based token ensures that the token is invalidated when: |
| 57 |
|
| 58 |
* The filename changes |
| 59 |
* The timestamp changes |
| 60 |
* The mtime of the file changes |
| 61 |
* The `SECRET_KEY` changes |
| 62 |
|
| 63 |
Time-constrained uplaod slot |
| 64 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 65 |
|
| 66 |
The upload slot mechanism can be used to generically handle file uploads |
| 67 |
by clients that can not upload files with a regular request. One example |
| 68 |
for this is a client using GraphQL to talk to Django, because GraphQL |
| 69 |
does not support file uploads and rather suggests to do uplaods |
| 70 |
out-of-band. |
| 71 |
|
| 72 |
With Titofisto's upload slot mechanism, calling code can request a secure |
| 73 |
URL which it can hand out to a client. When the client POSTs its file |
| 74 |
to the endpoint, a previously provided callback is called to handle the |
| 75 |
uploaded file. |
| 76 |
|
| 77 |
Installation |
| 78 |
------------ |
| 79 |
|
| 80 |
To add `django-titofisto`_ to a project, first add it as dependency to your |
| 81 |
project, e.g. using `poetry`_:: |
| 82 |
|
| 83 |
$ poetry add django-titofisto |
| 84 |
|
| 85 |
`django-titofisto` will use the base `FileSystemStorage` for almost everything, |
| 86 |
including determining the `MEDIA_ROOT`. It merely adds a token as URL parameter |
| 87 |
to whatever the base `FileSystemStorage.url()` method returns. |
| 88 |
|
| 89 |
Add the following to your settings:: |
| 90 |
|
| 91 |
DEFAULT_FILE_STORAGE = "titofisto.TitofistoStorage" |
| 92 |
TITOFISTO_TIMEOUT = 3600 # optional, this is the default |
| 93 |
TITOFISTO_PARAM = "titofisto_token" # optional, this is the default |
| 94 |
|
| 95 |
Add the following to your URL config:: |
| 96 |
|
| 97 |
from django.conf import settings |
| 98 |
from django.urls import include, path |
| 99 |
|
| 100 |
urlpatterns += [ |
| 101 |
path(settings.MEDIA_URL.removeprefix("/"), include("titofisto.urls")), |
| 102 |
] |
| 103 |
|
| 104 |
Django will start serving media files under the configured `MEDIA_URL`. |
| 105 |
|
| 106 |
Provide public media files |
| 107 |
~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 108 |
|
| 109 |
Sometimes, there might be media files, for example favicons, |
| 110 |
you want to be accessible without any authentication. Per default, |
| 111 |
`django-titofisto` will serve all files stored in the directory `public` without a token. |
| 112 |
You can disable or configure this behavior using these settings:: |
| 113 |
|
| 114 |
TITOFISTO_USE_PUBLIC_NAMESPACE = True # optional, this is the default |
| 115 |
TITOFISTO_PUBLIC_NAMESPACE = "public/" # optional, this is the default |
| 116 |
|
| 117 |
Use the time-constrained upload slot |
| 118 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 119 |
|
| 120 |
To enable the file upload mechanism, a setting must be set, because the |
| 121 |
default `upload/` prefix could potentially shadow expected media file |
| 122 |
URLs:: |
| 123 |
|
| 124 |
TITOFISTO_ENABLE_UPLOAD = True |
| 125 |
TITOFISTO_UPLOAD_NAMESPACE = "titofisto/upload/" |
| 126 |
|
| 127 |
A simplified example for using the upload slot mechanism is:: |
| 128 |
|
| 129 |
from titofisto.views import TitofistoUploadView |
| 130 |
|
| 131 |
def handle_file(request, pk): |
| 132 |
instance = MyModel.objects.get(pk=pk) |
| 133 |
instance.photo = request.FILES["photo"] |
| 134 |
instance.save() |
| 135 |
|
| 136 |
# This gets an uplaod URL to pass on to the client |
| 137 |
# On upload, the handler above will be called, with 15 as positional argument |
| 138 |
upload_url = TitofistoUploadView.get_upload_slot( |
| 139 |
"mymodule.handle_file", |
| 140 |
(15,) |
| 141 |
) |
| 142 |
|
| 143 |
Credits |
| 144 |
------- |
| 145 |
|
| 146 |
`django-titofisto` was developed for the `AlekSIS`_ school information system by |
| 147 |
its team:: |
| 148 |
|
| 149 |
Copyright © 2021, 2023 Dominik George <[email protected]> |
| 150 |
Copyright © 2021 Jonathan Weth <[email protected]> |
| 151 |
|
| 152 |
.. _django-titofisto: https://edugit.org/AlekSIS/libs/django-titofisto |
| 153 |
.. _poetry: https://python-poetry.org/ |
| 154 |
.. _Django's cache framework: https://docs.djangoproject.com/en/3.2/topics/cache/ |
| 155 |
.. _AlekSIS: https://aleksis.org/ |
| 156 |
|