This library let you to authenticate an specific user on DRF based on the JWT Token returned by Auth0 Javascript libraries.
Project description
djangorestframework-auth0
This library let you to authenticate an specific user on DRF based on the JWT Token returned by Auth0 Javascript libraries.
Installation
- Using
pip
to install current release:
pip install rest_framework_auth0
- Using
pip
to install development version:
pip install git+https://github.com/mcueto/djangorestframework-auth0/
Quick start
- Make sure
django.contrib.auth
in on INSTALLED_APPS setting, otherwise add it by your own:
INSTALLED_APPS = [ ... 'django.contrib.auth', ... ]
This will allow us to login as an specific user as well as auto-creating users when they don't exist
- Add
rest_framework_auth0
to yourINSTALLED_APPS
setting:
INSTALLED_APPS = [ ..., 'rest_framework_auth0', ]
- Add
Auth0JSONWebTokenAuthentication
in your DEFAULT_AUTHENTICATION_CLASSES located at settings.py from your project:
REST_FRAMEWORK = { ..., 'DEFAULT_AUTHENTICATION_CLASSES': ( ..., 'rest_framework_auth0.authentication.Auth0JSONWebTokenAuthentication', ), }
- Add your
CLIENTS
&MANAGEMENT_API
settings in your settings.py file:
# Import cryptography libraries from cryptography.x509 import load_pem_x509_certificate from cryptography.hazmat.backends import default_backend # Read the your Auth0 client PEM certificate certificate_text = open('rsa_certificates/certificate.pem', 'rb').read() certificate = load_pem_x509_certificate(certificate_text, default_backend()) # Get your PEM certificate public_key certificate_publickey = certificate.public_key() # # # AUTH0 SETTINGS AUTH0 = { 'CLIENTS': { 'default': { 'AUTH0_CLIENT_ID': '<YOUR_AUTH0_CLIENT_ID>', 'AUTH0_AUDIENCE': '<YOUR_AUTH0_CLIENT_AUDIENCE>', 'AUTH0_ALGORITHM': 'RS256', # default used in Auth0 apps 'PUBLIC_KEY': certificate_publickey', } }, # Management API - For roles and permissions validation 'MANAGEMENT_API': { 'AUTH0_DOMAIN': '<YOUR_AUTH0_DOMAIN>', 'AUTH0_CLIENT_ID': '<YOUR_AUTH0_M2M_API_MANAGEMENT_CLIENT_ID>', 'AUTH0_CLIENT_SECRET': '<YOUR_AUTH0_M2M_API_MANAGEMENT_CLIENT_SECRET>' }, }
- Add the
Authorization
Header to all of your REST API request, prefixingBearer
to your token(default in common REST clients & Postman):
Authorization: Bearer <AUTH0_GIVEN_TOKEN>
- That's it, now only your Auth0 users can request data to your DRF endpoints
NOTE: In order to get the token authentication, the 'django.contrib.auth' app models migrations must be applied(python manage.py migrate).
Use cases
Sample Project
A sample project can be found here
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.
Filename, size | File type | Python version | Upload date | Hashes |
---|---|---|---|---|
Filename, size rest_framework_auth0-0.6.2-py3-none-any.whl (14.3 kB) | File type Wheel | Python version py3 | Upload date | Hashes View |
Filename, size rest-framework-auth0-0.6.2.tar.gz (12.2 kB) | File type Source | Python version None | Upload date | Hashes View |
Close
Hashes for rest_framework_auth0-0.6.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0cf2a1aa0d24dc78fef60122a932016e642b90540f7ea966fe6a8e67e09d815d |
|
MD5 | 2cb88be6f6d94474d43c4570fcaa2824 |
|
BLAKE2-256 | e199670f194b8d1c459a5da664b0bfb5c0bb8619efbaf885a407f755b3857885 |
Close
Hashes for rest-framework-auth0-0.6.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | e897e273f9084f9924539146ca388aaab833139e10a872d7e44d660627511645 |
|
MD5 | 21efd434db64205b4bc8154cb8a010e9 |
|
BLAKE2-256 | c50d92068166d5e100991576a4affa045c71d95633c4b1f25ba96149f06cf813 |