A Django Rest Token Authentication system like telegram which will be using JWT as core with extended features.
Project description
django-token-manager
A Django Rest Token Authentication system like telegram which will be using JWT as core with extended features.
Features
- Validation of tokens first with jwt algorithms to filter not valid token formats, before hitting database.
- Able to see list of active tokens of each user_id
- Able to delete each token if needed
- Able to remove all other tokens of each users and keep just existing one
- Fetch useful info for each token request like os, ip and ...
Why django-token-manager
Reason to use this module is that by default if you are using jwt system for token authorization of client, you don't have control on existing tokens. Of course you can set a expire date for each token. But if the expiration date isn't arrived yet, you can't delete this token. What happens if you want to delete all sessions of a user. With jwt you don't have control on it, and you should wait for expiration of token to be arrived.
The purpose of this package, is to give more control on jwt tokens. For this there will be a
lookup_id in payload of each jwt token. First token with be validated with jwt algorithms.
Then payload lookup_id will be checked on database and if available will give access.
And with this solution no need to query on a big string (session string) on database,
if the jwt token is valid, will just query on a db_index ed field lookup_id
.
Requirements
To use this package following needed. if not provided will be installed automatically.
Django>=2.0
djangorestframework>=3.0
django-jalali>=3.1.0
django-rest-captcha>=0.1.0
Installation
Note: This package is well tested on django>=2.0
. But if you are using older versions can be
used with minor changes in structure.
install using pip:
$ pip install django-token-manager
Usage
Now register app in your settings.py
file.
INSTALLED_APPS = [
"token_manager",
]
In your settings.py
, add JSONWebTokenAuthentication
to Django REST framework's DEFAULT_AUTHENTICATION_CLASSES
.
REST_FRAMEWORK = {
...,
'DEFAULT_AUTHENTICATION_CLASSES': (
'token_manager.authentication.JSONWebTokenAuthentication',
...
),
}
This package uses user-agents
package for fetching user agent info like device OS, IP address and ...
So you should register below to be able to use it:
MIDDLEWARE = [
...,
'django_user_agents.middleware.UserAgentMiddleware',
]
Remember to apply migration files in database:
python manage.py migrate
To get token:
curl -X POST -d "username=admin&password=admin" "http://localhost:8000/token/get/"
Verify token:
curl -H "Authorization: JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb29rdXBfaWQiOjUsInVzZXJfaWQiOjEsInVzZXJuYW1lIjoiYWRtaW4iLCJleHAiOjE1OTg1MjY4MjEsImVtYWlsIjoiIn0.l6JyGgAs_hBRejX1BpvA7PjubM2m89lV35PTVUBnV_I" "http://localhost:8000/token/manage/"
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 django-token-manager-1.1.2.tar.gz
.
File metadata
- Download URL: django-token-manager-1.1.2.tar.gz
- Upload date:
- Size: 24.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e1e7ba86dfa9c3ce99a2d1706241137e6c638053a1564ba9b0e83ccabe0a297d |
|
MD5 | ba23fc53a2cd71ae48a4aec8e0e2c3d1 |
|
BLAKE2b-256 | b0708ad6801f1873c7a54143546cbc54d10f396d5fdc7de0804d91a458ce5860 |
File details
Details for the file django_token_manager-1.1.2-py3-none-any.whl
.
File metadata
- Download URL: django_token_manager-1.1.2-py3-none-any.whl
- Upload date:
- Size: 28.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 081ed5804e074e7ee352e927afba178d2c83e746b8649a4209d798addc62f9bc |
|
MD5 | d8bdefce537d4ca5f7b2936d88a54a6b |
|
BLAKE2b-256 | 084aefdf8fe809b6e733bf1f7d77aaa5106c9dd0686566a5980d75b8e63f4a53 |