Django Expiring Token provides a very lightweight extension to DRF's existing token authentication. It implements the following functionalities:
- Tokens expire after the set time.
- On each authenticated request, the expiration time is updated by the set time in
settings.py.
-
Do NOT add
restframework.authtoken
to yourINSTALLED_APPS
. -
Add
django_expiring_token
to yourINSTALLED_APPS
setting like this:INSTALLED_APPS = [ ... 'rest_framework', 'django_expiring_token', ]
-
Include the URLconf in your project urls.py like this:
path('custom-url/', include('django_expiring_token.urls')),
-
Add the expiration time in
settings.py
:EXPIRING_TOKEN_DURATION = timedelta(hours=1) # Any timedelta setting can be used! If not set, the default value is 1 day
-
Add the default authentication class in
REST_FRAMEWORK
settings insettings.py
REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( ... 'django_expiring_token.authentication.ExpiringTokenAuthentication', ... ), }
-
Run
python manage.py migrate
to create package migrations -
Start the development server and you are good to go.
This build is tested against Python versions 3.6, 3.7, 3.8, 3.9 with Django versions 2.2+ To run tests
- Install
coverage
pip install coverage
- Run tests
coverage run runtest.py