Skip to main content

Version of Django REST Framework auth Token allowing multiple Tokens per User.

Project description

djangorestframework-multitoken

Version of Django REST Framework auth Token allowing multiple Tokens per User.

Usage

Install:

pip install djangorestframework-multitoken

Enable:

# settings.py

INSTALLED_APPS = [
    ...
    'rest_framework_multitoken',
    ...
]

Migrate:

python manage.py migrate

Sync:

To create MultiTokens from existing Django REST Framework auth Tokens run:

python manage.py multitoken_sync

If you want to switch back to regular Django REST Framework auth Tokens run:

# WARNING be careful as data may be lost
python manage.py multitoken_sync --backwards

WARNING: The --backwards sync may lose data if you have created multiple MultiToken instances for a User. Only one of the MultiToken instances can be moved back to a regular Django REST Framework auth Token. The MultiToken selected to be moved back will be the primary token which is the newest active token for the User.

Note: to run either of those commands you must have both rest_framework.authtoken and rest_framework_multitoken in INSTALLED_APPS and respective database migrations applied.

Enjoy:

View and manage Tokens in the Django Admin under Multi Token.

Use MultiTokenAuthentication globally or on a per-view basis.

# settings.py - use MultiTokenAuthentication globally
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        ...
        'rest_framework_multitoken.authentication.MultiTokenAuthentication',
        ...
    ],
    ...
}


# views.py - use MultiTokenAuthentication on a per-view basis
from rest_framework_multitoken.authentication import MultiTokenAuthentication
class MyView(APIView):
    authentication_classes = [MultiTokenAuthentication]
    ...

Access the MultiToken instances for a User with the get_user_primary_token utility method or the multi_tokens backwards relationship.

# get_user_primary_token utility method
from rest_framework_multitoken.utils import get_user_primary_token
multi_token = get_user_primary_token(user)

# backwards relationship
multi_tokens = user.multi_tokens.filter(is_active=True)

License

BSD-3-Clause

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

djangorestframework-multitoken-0.1.tar.gz (5.6 kB view hashes)

Uploaded Source

Built Distribution

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page