Skip to main content

JSON Web Token based authentication for Django REST framework

Project description

Build Status PyPI version

Overview

This package provides JSON Web Token Authentication support for Django REST framework.

If you want to read more about JWT, here’s a great blog post by the guys at Auth0 that talks about Cookie vs Token based authentication.

Requirements

  • Python (2.7, 3.2, 3.3, 3.4)

  • Django (1.6, 1.7)

  • Django REST Framework (2.4.3, 2.4.4, 3.0.0)

Installation

Install using pip

$ pip install djangorestframework-jwt

Usage

In your settings.py, add JSONWebTokenAuthentication to Django REST framework’s DEFAULT_AUTHENTICATION_CLASSES.

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
    ),
}

In your urls.py add the following URL route to enable obtaining a token via a POST included the user’s username and password.

urlpatterns = patterns(
    '',
    # ...

    url(r'^api-token-auth/', 'rest_framework_jwt.views.obtain_jwt_token'),
)

You can easily test if the endpoint is working by doing the following in your terminal, if you had a user created with the username admin and password abc123.

$ curl -X POST -d "username=admin&password=abc123" http://localhost:8000/api-token-auth/

Alternatively, you can use all the content types supported by the Django REST framework to obtain the auth token. For example:

$ curl -X POST -H "Content-Type: application/json" -d '{"username":"admin","password":"abc123"}' http://localhost:8000/api-token-auth/

Now in order to access protected api urls you must include the Authorization: JWT <your_token> header.

$ curl -H "Authorization: JWT <your_token>" http://localhost:8000/protected-url/

Refresh Token

If JWT_ALLOW_REFRESH is True, issued tokens can be “refreshed” to obtain a new brand token with renewed expiration time. Add a URL pattern like this:

url(r'^api-token-refresh/', 'rest_framework_jwt.views.refresh_jwt_token'),

Pass in an existing token to the refresh endpoint as follows: {"token": EXISTING_TOKEN}. Note that only non-expired tokens will work. The JSON response looks the same as the normal obtain token endpoint {"token": NEW_TOKEN}.

```bash $ curl -X POST -H “Content-Type: application/json” -d ’{“token”

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

djangorestframework-jwt-1.1.1.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

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

djangorestframework_jwt-1.1.1-py2.py3-none-any.whl (16.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file djangorestframework-jwt-1.1.1.tar.gz.

File metadata

File hashes

Hashes for djangorestframework-jwt-1.1.1.tar.gz
Algorithm Hash digest
SHA256 5fc40de379df1910cf4ca04658514cacf897dd7eb58938a89f7c1d9a8fb7776c
MD5 a3d94cbb6984925a3c85254980a06fcd
BLAKE2b-256 9187af2b359cb121c282d003c2a34a34ba364d46f144733bc77e2240756136a4

See more details on using hashes here.

File details

Details for the file djangorestframework_jwt-1.1.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for djangorestframework_jwt-1.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e6d680304a3cd34e2b5c6a1d3887cc92d1f744d3359b17b6309f892e8ba2e012
MD5 12182abe72ae0d4d83a96880302a6315
BLAKE2b-256 8e48ef9b8041c16c9f7d3bd46f45eadaba126e4b5943d75dd8edc32ced64b1d3

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