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.0.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 2 Python 3

File details

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

File metadata

File hashes

Hashes for djangorestframework-jwt-1.1.0.tar.gz
Algorithm Hash digest
SHA256 8d098388f582105f29738a4847eeadf286781984a7f02bdffe5718a55bbd1036
MD5 4d1347196e68e32546c17012fe6d062a
BLAKE2b-256 d78b3f38a8a95600f107f9e0f0c16ed370b4f408fd2acd0a6e08eee751600c3f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for djangorestframework_jwt-1.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6473118ea6dbfc2c00e626f9c480d968e6e7ea92af3bb182405dc01bfd6f7950
MD5 dd914b2ac30df68f9bab0a3342f9c2c3
BLAKE2b-256 f82e70e310061e10cdd4923b814722effbb6c4cfc7fd62791e663df6ce21a9ef

See more details on using hashes here.

Supported by

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