Django rest framework module to allow login via token (without User instance).
Project description
DRF Anonymous Login
Django rest framework module to allow login via token (without User instance). Any request with valid token in the
AUTH_HEADER (name configurable via setting.py, "HTTP_X_AUTHORIZATION_ANONYMOUS" by default) will be accepted.
Installation
- Install using pip:
pip install drf-anonymous-login
- Integrate
drf_anonymous_logininto yoursettings.py
INSTALLED_APPS = [
# ...
'drf_anonymous_login',
# ...
]
Usage
There are multiple ways to include the AnonymousLogin functionality to your endpoints. We recommend to use one of
the following approaches:
- Inherit from the
AnonymousLoginAuthenticationModelViewSetfor any model that is supposed to be accessible via valid token header. You'll find a simple exemplary usage scenario provided the testapp.
OR
-
Directly add the
AnonymousLoginAuthenticationandIsAuthenticatedto your ViewSet'sauthentication_classesandpermission_classesas implemented in the AnonymousLoginAuthenticationModelViewSet. -
Optionally add the
AnonymousLoginUserMixinto your app's User model in order to access itsis_anonymous_loginandanonymous_loginproperties:# myapp.models.py class User(AnonymousLoginUserMixin, AbstractUser): pass# settings.py AUTH_USER_MODEL = "myapp.User"
Configure token expiration
The tokens will not expire by default (expiration_datetime remains None). You can configure the
ANONYMOUS_LOGIN_EXPIRATION in your application's settings.py to define a default expiration in minutes, e.g.
to have any token only valid for 15 minutes, use:
# settings.py
...
ANONYMOUS_LOGIN_EXPIRATION=15
Auto-formatter setup
We use ruff (https://github.com/astral-sh/ruff) for local auto-formatting and for linting in the CI pipeline. The pre-commit framework (https://pre-commit.com) provides GIT hooks for these tools, so they are automatically applied before every commit.
Steps to activate:
- Install the pre-commit framework:
pip install pre-commit(for alternative installation options see https://pre-commit.com/#install) - Activate the framework (from the root directory of the repository):
pre-commit install
Hint: You can also run the formatters manually at any time with the following command: pre-commit run --all-files
Unit Tests
See folder tests/. The provided tests cover these criteria:
- success:
- access public endpoint without token
- access private endpoint with valid token
- cleanup task does not remove tokens before their expiration_datetime
- cleanup task removes tokens after their expiration_datetime
- failure:
- access private endpoint without token
- access private endpoint with invalid token
- access private endpoint with expired token
Follow below instructions to run the tests.
You may exchange the installed Django and DRF versions according to your requirements.
:warning: Depending on your local environment settings you might need to explicitly call python3 instead of python.
# install dependencies
python -m pip install --upgrade pip
pip install -e ".[dev]"
# run tests
cd tests && python manage.py test
Contributing
Contributions are welcomed! Read the Contributing Guide for more information.
Licensing
See LICENSE for more information.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file drf_anonymous_login-1.2.1.tar.gz.
File metadata
- Download URL: drf_anonymous_login-1.2.1.tar.gz
- Upload date:
- Size: 16.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dfa6605f0384e5d5d10fa4666f92595b22474ba400196a94c790370398ac2e10
|
|
| MD5 |
04f16f27ebab81ed7d3d20563a5bfeee
|
|
| BLAKE2b-256 |
a80cfb9c4667f181dd30a1c36eb72d50bcdfaf916c20b167d8d31e9b2eed8d57
|
File details
Details for the file drf_anonymous_login-1.2.1-py3-none-any.whl.
File metadata
- Download URL: drf_anonymous_login-1.2.1-py3-none-any.whl
- Upload date:
- Size: 9.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3333855d082e705c561b58c024cc9ac4866a2af90e3d2a2cdadc03e4d7701d0b
|
|
| MD5 |
274faa2e6626c31498482333166cb216
|
|
| BLAKE2b-256 |
80a6fe8e52549b842ffd8013dd510df58e19808b320d9eeddbe98604140ec0ff
|