A simple authentication module for django rest framework
Project description
DRF Authentify Documentation
drf-authentify is a near splitting replica of the simple django rest framework default token system, except better.
The major difference between django rest framework
default token and drf-authentify
are:
drf-authentify
allows multiple tokens per usersdrf-authentify
adds extra security layer by using access validation- bonus: drf-authentify provides utility methods to handle common use cases.
drf authentify aims to be as simple as possible, while providing a great set of features to meet your authentication demands without enforcing a certain pattern to your application flow.
Requirements
- Python >=3.8
- Django >=3.2
- djangorestframework 3
Installation
Installation is easy using pip
and will install all required libraries
$ pip install drf-authentify
Then add the drf-authentify
to your project by including the app to your INSTALLED_APPS
.
The app should preferably go somewhere after your regular apps.
INSTALLED_APPS = (
...
'drf_authentify'
)
drf-authentify
adds a model to your admin section called AuthToken, with this you can view and manage all tokens created on your applications. We already have a nice setup for you on django admin section.
Finally migrate all database entries.
$ python3 manage.py migrate
Global Configuration
For a one type fits all case, you can globally alter the following settings, or leave the default as it is.
DRF_AUTHENTIFY = {
"COOKIE_KEY": "token",
"TOKEN_EXPIRATION": 3000,
"ENABLE_AUTH_RESTRICTION": False,
"ALLOWED_HEADER_PREFIXES": ["bearer", "token"],
}
Customizing Tokens
-
ALLOWED_HEADER_PREFIXES: Here you can provide a list of prefixes that are allowed for your authentication header. We will validate this when you apply our authentication scheme
drf_authentify.auth.TokenAuthentication
as shown below. -
COOKIE_KEY: With this, you can customize what key we should use to retrieve your authentication cookie frmo each request. We will also validate this when you apply our authentication scheme
drf_authentify.auth.CookieAuthentication
as shown below. -
TOKEN_EXPIRATION: With this you can globally set the duration of each token generated, this can also be set per view, as you would see below.
-
ENABLE_AUTH_RESTRICTION: This can be used to disable/enable checks for authorization channels (cookie or authorization header).
Note: Do not forget to add custom header prefixes to your cors-header as this could cause cors errors.
Creating Tokens
Two utility methods have been provided for you to leverage for creating or generating user tokens on drf-authentify
. For ease, they are attached to the AuthToken model class.
from drf_authentify.models import AuthToken
def sample_view(request, *arg, **kwargs):
token = AuthToken.generate_cookie_token(user, context=None, expires=3000)
def sample_view(request, *arg, **kwargs):
token = AuthToken.generate_header_token(user, context=None, expires=3000)
drf-authentify
allows you to save contexts alongside your tokens if you need to, also feel free to alter the duration of a token validity using the expires parameters, we'll use the globally set TOKEN_EXPIRATION
or default if none is provided.
Deleting Tokens
To delete tokens, simply use one of the three utility methods provides on the AuthToken class.
from drf_authentify.utils import clear_request_tokens, delete_request_token, clear_expired_tokens, clear_user_tokens
# Remove single token based on request authenticated user
delete_request_token(request)
# Remove all user tokens based on request authenticated user
clear_request_tokens(request)
# Remove all tokens for user
clear_user_tokens(user)
# Remove all expired tokens
clear_expired_tokens()
Authentication Schemes
drf authentify provides you with two authentication classes to cover for both broad type of tokens you can generate. These are very import in django rest framework, and can be used either globally or per view.
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'drf_authentify.auth.CookieAuthentication',
'drf_authentify.auth.TokenAuthentication',
]
}
By adding this, you can appriopriately check for authentication status and return the user on the request object.
Note: For convenience, you can access the current token object in your authenticated view through request.auth, this would allow easy access context which can be used to store authorization scope and other important data.
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file drf_authentify-0.3.11.tar.gz
.
File metadata
- Download URL: drf_authentify-0.3.11.tar.gz
- Upload date:
- Size: 12.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d72fae93a4b37f4c5c5a6a82c4dbdd19c71e5c4cc4c0d2ff846d1564aff35a66 |
|
MD5 | 7411f3535f6c1c4ac1e0426c88c85029 |
|
BLAKE2b-256 | dbd49ab00cedf5adf794c6a0cecde4f260e0438dab7883d9bfe42ccfbfd986c3 |
File details
Details for the file drf_authentify-0.3.11-py3-none-any.whl
.
File metadata
- Download URL: drf_authentify-0.3.11-py3-none-any.whl
- Upload date:
- Size: 13.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6970c08e263c659160206788d68f3a56cb357c406d0582d0b9fa056105283547 |
|
MD5 | 42f142ed3fb56b81ca1b4f4aea78f131 |
|
BLAKE2b-256 | 327f513bc5c9e8e4d81a76fc2b0db5ce9a68c6cb85a44f50fa438919d05bcdf6 |