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'
'''
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)
'''
# 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
}
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.0.1.tar.gz
(6.4 kB
view hashes)
Built Distribution
Close
Hashes for drf_temptoken-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 271b73501c3c64bfffaa31ae0f34634ddc336fcde17a937086cdda7eb11fd098 |
|
MD5 | 6ce8be478d52f38b164449b89fe3df62 |
|
BLAKE2b-256 | 2a80527aa854ad160b5c5a07bc84fcc77f36f2de27fbbd30352696e458cab712 |