Skip to main content

Add secure token to djnago-rest-framework

Project description

  1. Add “drf_secure_token” to your INSTALLED_APPS setting like this:

    INSTALLED_APPS = [
        ...
        'rest_framework',
        'drf_secure_token',
    ]
  2. Add following lines to your settings.py:

    REST_FRAMEWORK = {
        'DEFAULT_AUTHENTICATION_CLASSES': [
            'rest_framework.authentication.BasicAuthentication',
            'drf_secure_token.authentication.SecureTokenAuthentication',
         ]
    }
  3. For updating token add this middleware to your MIDDLEWARE_CLASSES:

    MIDDLEWARE_CLASSES = (
        ...
        'drf_secure_token.middleware.UpdateTokenMiddleware',
    )
  4. Add UPDATE_TOKEN to your ‘dev’ settings if you don’t want to update token in DEBUG mode:

    UPDATE_TOKEN = False
  5. Add TOKEN_AGE to your settings:

    TOKEN_AGE = 60*10 # 10 min
  6. (Optional) To cleanup dead tokens celery can be used. Way to enable depends from celery version

6.1 Celery 4, just enable it with settings:

REMOVE_TOKENS_THROUGH_CELERY = True

6.2 Celery 5, add periodic task manually:

@app.on_after_finalize.connect
def setup_periodic_tasks(sender, **kwargs):
    from drf_secure_token.tasks import DELETE_OLD_TOKENS

    app.conf.beat_schedule.update({
        'drf_secure_token.tasks.delete_old_tokens': DELETE_OLD_TOKENS,
    })
  1. Run python manage.py migrate to create the drf_secure_token models.

Project details


Download files

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

Source Distribution

drf-secure-token-1.1.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

drf_secure_token-1.1.0-py2-none-any.whl (11.0 kB view details)

Uploaded Python 2

File details

Details for the file drf-secure-token-1.1.0.tar.gz.

File metadata

  • Download URL: drf-secure-token-1.1.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.6.1 requests/2.25.1 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/2.7.15+

File hashes

Hashes for drf-secure-token-1.1.0.tar.gz
Algorithm Hash digest
SHA256 34e1d3a5ce48c57e6800654c96bc8789367317d2a0d75cc1eaf0d32b773b558b
MD5 f70dd8578bd1c21dd9dcac8016ae65b3
BLAKE2b-256 e0ccbf4007755137afa92445d787d8b45b57f0afe0275575d48e448dd0bf92e0

See more details on using hashes here.

File details

Details for the file drf_secure_token-1.1.0-py2-none-any.whl.

File metadata

  • Download URL: drf_secure_token-1.1.0-py2-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.6.1 requests/2.25.1 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/2.7.15+

File hashes

Hashes for drf_secure_token-1.1.0-py2-none-any.whl
Algorithm Hash digest
SHA256 d43f85678e5752f9746b474b8c990e8b8c3d3b71ee44b4c14c79d40a82a50984
MD5 feda3ebecea4997a8b0e5802bca8f585
BLAKE2b-256 07a70223c2bf032cb9f41e2e92bc4de5e88d532d195a6cc56bb82de96cb3819a

See more details on using hashes here.

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