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_login
into 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
AnonymousLoginAuthenticationModelViewSet
for 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
AnonymousLoginAuthentication
andIsAuthenticated
to your ViewSet'sauthentication_classes
andpermission_classes
as implemented in the AnonymousLoginAuthenticationModelViewSet.
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
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 -r requirements.txt
# setup environment
pip install -e .
# 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
Hashes for drf-anonymous-login-1.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 19c4dddebcc1e500383e6cde4e4467e04032420f10155c54717f5f7e9290eb86 |
|
MD5 | 89f3a83dc8029058e3a70d5225b67bac |
|
BLAKE2b-256 | eedd24193e34660defbccbe89e31efe7d3297afa981f9e1151cc4536017842bb |
Hashes for drf_anonymous_login-1.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e6c94999094f097835a461c7dd97897602f3e869381e8bc13c13046e990967f7 |
|
MD5 | d6982adec8420ab8c42000fb52ae056b |
|
BLAKE2b-256 | 0d51113c2c2bcdf8c8fc14ccfacba6e62a847c128197693c939a795732d897a2 |