Skip to main content

DRF temporary token authentication

Project description

drf-temptoken

DRF temporary authentication token

Installation

pip install drf-temptoken

Usage

Include drf_temptoken in INSTALLED_APPS

INSTALLED_APPS = [
    ...,
    'drf_temptoken'
]

Add drf_temptoken.auth.TempTokenAuthentication into your authentication classes

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'drf_temptoken.auth.TempTokenAuthentication',
    )
}

Create token for user

from django.contrib.auth import get_user_model
from drf_temptoken.utils import create_token, get_user_tokens

User = get_user_model()

user = User.objects.first()

token = create_token(user)

# Sets token's expiration date to current
token = token.expire()

key = token.key # Used in authentication process

tokens = get_user_tokens(user) # Returns a queryset of TempTokens belonging to the user

Default settings (can be overriden in Django's settings)

TMP_TOKEN_HEADER_PREFIX = 'TMP'

TMP_TOKEN_AUTH_HEADER = 'Authorization'

# Set any value in order to get the token from query
TMP_TOKEN_QUERY_PARAM = None



# Python's timedelta kwargs passed in order to set token's expiration date
TMP_TOKEN_TIME_DELTA_KWARGS = {
    'days': 7 # Token will be expired in 7 days by default
}

Auth backend will check for HTTP_AUTHORIZATION: TMP {token} by default

Assuming your token (token.key) is equal to "123", your request should look like this:

import requests

headers = {
    'Authorization': 'TMP 123'
}

response = requests.get(url, headers=headers)

Or like this if you set query param to _api_key:

import requests

url = 'https://example.com?_api_key=123'

response = requests.get(url)

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-temptoken-0.1.2.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

drf_temptoken-0.1.2-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file drf-temptoken-0.1.2.tar.gz.

File metadata

  • Download URL: drf-temptoken-0.1.2.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.4

File hashes

Hashes for drf-temptoken-0.1.2.tar.gz
Algorithm Hash digest
SHA256 5caccd465583b8ba19b708c6263585aea18119dbb31b86e4501b7c75feb631f7
MD5 21a07bb0f02bc862cb858731f6727666
BLAKE2b-256 b852cabb4e56d8bfe1e64296d50ce0b71db25b355e6fa6b3e24d897bf5cfa83b

See more details on using hashes here.

File details

Details for the file drf_temptoken-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for drf_temptoken-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 51a8f8a5d32723125308ddf1713b206eb8f12827efb93f57b6dff0662608c709
MD5 181865ed5d4df811cee659cdef9206f0
BLAKE2b-256 2177d177cd36971480c9c300a50190d41b35fb9dc9ae3ccc0e64209174efd4b3

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