Skip to main content

Django Rest Framework Library to use Auth0 authentication

Project description

===== djangorestframework-auth0

Migrate from 0.2.1 to >0.4.5

If you're using the version 0.2.1 -or older- from this package, you'll need to update your Auth0 settings

From this

AUTH0 = {
    'AUTH0_CLIENT_ID': '<YOUR_AUTH0_CLIENT_ID>', # make sure it's the same string that aud attribute in your payload provides
    'AUTH0_CLIENT_SECRET': '<YOUR_AUTH0_CLIENT_SECRET>',
    'CLIENT_SECRET_BASE64_ENCODED': True,  # default to True, if you're Auth0 user since December, maybe you should set it to False
    ...
}

To this

AUTH0 = {
  'CLIENTS': {
      'default': {
          'AUTH0_CLIENT_ID': '<YOUR_AUTH0_CLIENT_ID>',  #make sure it's the same string that aud attribute in your payload provides
          'AUTH0_CLIENT_SECRET': '<YOUR_AUTH0_CLIENT_SECRET>',
          'CLIENT_SECRET_BASE64_ENCODED': True,  # default to True, if you're Auth0 user since December, maybe you should set it to False,
          'AUTH0_ALGORITHM': 'HS256',  # HS256 or RS256
          'PUBLIC_KEY': <YOUR_AUTH0_CERTIFICATE>,  # used only for RS256
      }
  },
  ...
}

If you wanna use RS256, please follow the sample project


Library to simply use Auth0 token authentication in DRF within djangorestframework-jwt

This library let you to login an specific user based on the JWT Token returned by Auth0 Javascript libraries

Detailed documentation will be in the "docs" directory.

Installation

  1. Using pip install the library cloning the repository with following command:
pip install rest_framework_auth0

Quick start

  1. Add "django.contrib.auth to INSTALLED_APPS settings like this:
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

  1. Add "rest_framework_auth0" to your INSTALLED_APPS after rest_framework_jwt setting like this:
INSTALLED_APPS = [
    ...,
    'rest_framework_jwt',
    'rest_framework_auth0',
]
  1. Add Auth0JSONWebTokenAuthentication in your DEFAULT_AUTHENTICATION_CLASSES located at settings.py from your project:
REST_FRAMEWORK = {
    ...,
    'DEFAULT_AUTHENTICATION_CLASSES': (
        ...,
        'rest_framework_auth0.authentication.Auth0JSONWebTokenAuthentication',
    ),
}
  1. Add your AUTH0_CLIENT_SECRET and AUTH0_CLIENT_ID in your settings.py file -must be the same secret and id than the frontend App-:
AUTH0 = {
  'CLIENTS': {
      'default': {
          'AUTH0_CLIENT_ID': '<YOUR_AUTH0_CLIENT_ID>',  #make sure it's the same string that aud attribute in your payload provides
          'AUTH0_CLIENT_SECRET': '<YOUR_AUTH0_CLIENT_SECRET>',
          'CLIENT_SECRET_BASE64_ENCODED': True,  # default to True, if you're Auth0 user since December, maybe you should set it to False
      }
  },
  'AUTH0_ALGORITHM': 'HS256',  # default used in Auth0 apps
  'JWT_AUTH_HEADER_PREFIX': 'JWT',  # default prefix used by djangorestframework_jwt
  'AUTHORIZATION_EXTENSION': False,  # default to False
  'USERNAME_FIELD': 'sub',  # default username field in auth0 token scope to use as token user
}
  1. Add the Authorization Header to all of your REST API request, prefixing JWT to your token:
Authorization: JWT <AUTH0_GIVEN_TOKEN>
  1. Use the decorator @token_required in all views you want to protect (not_ready_yet)

  2. That's it

Multiple Clients - Multiples App - One API

If you wanna to use multiple Auth0 App and/or Clients -for example if you're creating an open API, you can add as much as you want in the AUTH0.CLIENTS settings parameter

AUTH0 = {
  'CLIENTS': {
      'default': {
          'AUTH0_CLIENT_ID': '<YOUR_AUTH0_CLIENT_ID>',  #make sure it's the same string that aud attribute in your payload provides
          'AUTH0_CLIENT_SECRET': '<YOUR_AUTH0_CLIENT_SECRET>',
          'CLIENT_SECRET_BASE64_ENCODED': True,  # default to True, if you're Auth0 user since December, maybe you should set it to False
      }
      'web': {
          'AUTH0_CLIENT_ID': '<YOUR_AUTH0_CLIENT_ID>',  #make sure it's the same string that aud attribute in your payload provides
          'AUTH0_CLIENT_SECRET': '<YOUR_AUTH0_CLIENT_SECRET>',
          'CLIENT_SECRET_BASE64_ENCODED': True,  # default to True, if you're Auth0 user since December, maybe you should set it to False
      }
      'mobile': {
          'AUTH0_CLIENT_ID': '<YOUR_AUTH0_CLIENT_ID>',  #make sure it's the same string that aud attribute in your payload provides
          'AUTH0_CLIENT_SECRET': '<YOUR_AUTH0_CLIENT_SECRET>',
          'CLIENT_SECRET_BASE64_ENCODED': True,  # default to True, if you're Auth0 user since December, maybe you should set it to False
      }
  },
  ...
}

In order to select one of them when the authentication is needed -a POST request, for example- you need to add a header called Client-Code -by default, but you can customize it-. The names of the clients are case sensitive.

Sample project

A sample project can be found here

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

rest_framework_auth0-0.5.0.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rest_framework_auth0-0.5.0-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file rest_framework_auth0-0.5.0.tar.gz.

File metadata

  • Download URL: rest_framework_auth0-0.5.0.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for rest_framework_auth0-0.5.0.tar.gz
Algorithm Hash digest
SHA256 0d523aa3c0923dc460440b675e735035ba98a2850494ea3358cfcc852488863e
MD5 628ab28a10664eeb6d8e57903c64fa06
BLAKE2b-256 a3db2df903003a44624b65de4ce323a57b1287bff169c25a655fd7fdb1cfe783

See more details on using hashes here.

File details

Details for the file rest_framework_auth0-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: rest_framework_auth0-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for rest_framework_auth0-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8f563fb6930b6b1d3c20da965e4d43f65fb8fdffd11a366559d08d1fc5faae2d
MD5 3d0749c77c2ce6bf90e5dc26c1b50567
BLAKE2b-256 17d0088c97ff3dd3f76a18197183fedb96bdfe588896ed86ae35da918bec1443

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page