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
Built Distribution
Close
Hashes for drf-temptoken-0.0.2.linux-x86_64.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 54f5559c7c3f973791dbc2b2cb21e95bbf2e1178ef55451cab4cae51616cbea5 |
|
MD5 | 8616432aca28bbde1714bd1914f960d8 |
|
BLAKE2b-256 | 6c9385a1041cbd5683984fbafcdae73ee314e170111ee62c8edf326e0c61da8a |
Close
Hashes for drf_temptoken-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dcb6e7710e5e11ddbd570380faad6371db5e5e7621ba0ecc42e33e3f12dabd6d |
|
MD5 | 29f342b6a446895b618e0b26c6d9895e |
|
BLAKE2b-256 | 03edc072c22c64be0af5b52ac8e7dc0a100c4cdfe2bedf870e52adf70c524829 |