Skip to main content

Django Rest Multi Token Auth

PyPI Test status Codecov

This django app is an extension for the Django Rest Framework. It tries to overcome the limitation of Token Authentication, which only uses a single token per user.

How to use

Install:

pip install drf-multitokenauth

Add 'drf_multitokenauth' to your INSTALLED_APPS in your Django settings file:

INSTALLED_APPS = (
    ...
    'django.contrib.auth',
    ...
    'rest_framework',
    ...
    'drf_multitokenauth',
    ...
)

Configure Django REST Framework to use 'drf_multitokenauth.coreauthentication.MultiTokenAuthentication':

REST_FRAMEWORK = {
    ...
    'DEFAULT_AUTHENTICATION_CLASSES': [
        ...
        'drf_multitokenauth.coreauthentication.MultiTokenAuthentication',
        ...
    ],
    ...
}

And add the auth urls to your Django url settings:

from django.conf.urls import include
from django.urls import re_path


urlpatterns = [
    ...
    re_path(r'^api/auth/', include('drf_multitokenauth.urls', namespace='multi_token_auth')),
    ...
]    

The following endpoints are provided:

  • login - takes username, password and an optional token_name; on success an auth token is returned
  • logout

Signals

  • pre_auth(username, password) - Fired when an authentication (login) is starting
  • post_auth(user) - Fired on successful auth

Tests

See folder tests/. Basically, all endpoints are covered with multiple unit tests.

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 .
python setup.py install

# run tests
cd tests && python manage.py test

Cache Backend

If you want to use a cache for the session store, you can install django-memoize and add 'memoize' to INSTALLED_APPS.

Then you need to use CachedMultiTokenAuthentication instead of MultiTokenAuthentication.

pip install django-memoize

Django Compatibility Matrix

If your project uses an older verison of Django or Django Rest Framework, you can choose an older version of this project.

This Project Python Version Django Version Django Rest Framework
2.1.* 3.9+ 4.2, 5.0, 5.1 3.15
2.0.* 3.7+ 3.2, 4.0, 4.1 3.12, 3.13
1.5.* 3.7+ 3.2, 4.0, 4.1 3.12, 3.13
1.4.* 3.6+ 2.2, 3.2 3.9, 3.10, 3.11, 3.12
1.3.* 2.7, 3.4+ 1.11, 2.0 3.6, 3.7, 3.8
1.2.* 2.7, 3.4+ 1.8, 1.11, 2.0 3.6, 3.7, 3.8

Make sure to use at least DRF 3.10 when using Django 3.0 or newer.

Releases prior to 2.0.0 where published as django-rest-multitokenauth. Newer releases are published as drf-multitokenauth.

Migrating from 1.x to 2.x

  1. Uninstall django-rest-multitokenauth
  2. Install drf-multitokenauth
  3. Run the migration SQL bellow:
    ALTER TABLE django_rest_multitokenauth_multitoken RENAME to drf_multitokenauth_multitoken;
    UPDATE django_migrations SET app = 'drf_multitokenauth' WHERE app = 'django_rest_multitokenauth';
    UPDATE django_content_type SET app_label = 'drf_multitokenauth' WHERE app_label = 'django_rest_multitokenauth';
    
  4. Run Django migrations

Changelog / Releases

All releases should be listed in the releases tab on github.

See CHANGELOG.md for a more detailed listing.

License

This project is published with the BSD 3 Clause License. See https://choosealicense.com/licenses/bsd-3-clause-clear/ for more information about what this means.

Metadata

Release files for drf-multitokenauth 2.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for drf-multitokenauth 2.1.0
File Size Uploaded
drf-multitokenauth-2.1.0.tar.gz 11.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for drf-multitokenauth 2.1.0
File Interpreter ABI Platform
drf_multitokenauth-2.1.0-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 23.2 kB

Release files / drf-multitokenauth-2.1.0.tar.gz

Download URL drf-multitokenauth-2.1.0.tar.gz
Size 11.3 kB
Tags Source
SHA-256 checksum
How to use checksums
69170f3f7c4368a81670bac3d5a511f8127291d32a9521a35ebcc54fec38d83d
BLAKE2b-256 checksum
How to use checksums
984ffaa200d514c78f2ba785e599f82a9ba54a07b86037c7d9f96177f503714b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release files / drf_multitokenauth-2.1.0-py2.py3-none-any.whl

Download URL drf_multitokenauth-2.1.0-py2.py3-none-any.whl
Size 11.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
97e743f49ee14f8f0dac787e2b53b3fbe01bd93d9fc8871cc9c7871ca61772a4
BLAKE2b-256 checksum
How to use checksums
bb60c204de33223516b7cd51bf84a5ffe10adc72d8c09bad55bc147613abe948
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release history Release notifications | RSS feed

This release

2.1.0 This release

2 release files

2.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page