skip to navigation
skip to content

Not Logged In

djangorestframework-jwt 1.0.1

JSON Web Token based authentication for Django REST framework

Latest Version: 1.7.2

# Django REST framework JWT Auth

[![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](

## Installation

Install using `pip`...

$ pip install djangorestframework-jwt

## Usage

In your ``, add `JSONWebTokenAuthentication` to Django REST framework's `DEFAULT_AUTHENTICATION_CLASSES`.


In your `` 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}`.

$ curl -X POST -H "Content-Type: application/json" -d '{"token":"<existing_token>}' http://localhost:8000/api-token-refresh/

Refresh with tokens can be repeated (token1 -> token2 -> token3), but this chain of token stores the time that the original token (obtained with username/password credentials), as `orig_iat`. You can only keep refreshing tokens up to `JWT_REFRESH_EXPIRATION_DELTA`.

A typical use case might be a web app where you'd like to keep the user "logged in" the site without having to re-enter their password, or get kicked out by surprise before their token expired. Imagine they had a 1-hour token and are just at the last minute while they're still doing something. With mobile you could perhaps store the username/password to get a new token, but this is not a great idea in a browser. Each time the user loads the page, you can check if there is an existing non-expired token and if it's close to being expired, refresh it to extend their session. In other words, if a user is actively using your site, they can keep their "session" alive.

## Additional Settings
There are some additional settings that you can override similar to how you'd do it with Django REST framework itself. Here are all the available defaults.





'JWT_EXPIRATION_DELTA': datetime.timedelta(seconds=300),

'JWT_REFRESH_EXPIRATION_DELTA': datetime.timedelta(days=7),

This packages uses the JSON Web Token Python implementation, [PyJWT]( and allows to modify some of it's available options.

This is the secret key used to encrypt the JWT. Make sure this is safe and not shared or public.

Default is your project's `settings.SECRET_KEY`.


Possible values:

> * HS256 - HMAC using SHA-256 hash algorithm (default)
> * HS384 - HMAC using SHA-384 hash algorithm
> * HS512 - HMAC using SHA-512 hash algorithm
> * RS256 - RSASSA-PKCS1-v1_5 signature algorithm using SHA-256 hash algorithm
> * RS384 - RSASSA-PKCS1-v1_5 signature algorithm using SHA-384 hash algorithm
> * RS512 - RSASSA-PKCS1-v1_5 signature algorithm using SHA-512 hash algorithm

> For the RSASSA-PKCS1-v1_5 algorithms, the "secret" argument in jwt.encode is supposed to be a private RSA key as
> imported with Crypto.PublicKey.RSA.importKey. Likewise, the "secret" argument in jwt.decode is supposed to be the
> public RSA key imported with the same method.

Default is `"HS256"`.


If the secret is wrong, it will raise a jwt.DecodeError telling you as such. You can still get at the payload by setting the `JWT_VERIFY` to `False`.

Default is `True`.


You can turn off expiration time verification with by setting `JWT_VERIFY_EXPIRATION` to `False`.

Default is `True`.


> This allows you to validate an expiration time which is in the past but no very far. For example, if you have a JWT payload with an expiration time set to 30 seconds after creation but you know that sometimes you will process it after 30 seconds, you can set a leeway of 10 seconds in order to have some margin.

Default is `0` seconds.

This is an instance of Python's `datetime.timedelta`. This will be added to `datetime.utcnow()` to set the expiration time.

Default is `datetime.timedelta(seconds=300)`(5 minutes).

Enable token refresh functionality. Token issued from `rest_framework_jwt.views.obtain_jwt_token` will have an `orig_iat` field. Default is `False`

Limit on token refresh, is a `datetime.timedelta` instance. This is how much time after the original token that future tokens can be refreshed from.

Default is `datetime.timedelta(days=7)` (7 days).

Specify a custom function to generate the token payload

If you store `user_id` differently than the default payload handler does, implement this function to fetch `user_id` from the payload.

You can modify the Authorization header value prefix that is required to be sent together with the token. The default value is `JWT`. This decision was introduced in PR [#4]( to allow using both this package and OAuth2 in DRF.

Another common value used for tokens and Authorization headers is `Bearer`.

Default is `JWT`.
File Type Py Version Uploaded on Size
djangorestframework-jwt-1.0.1.tar.gz (md5) Source 2014-09-03 12KB
djangorestframework_jwt-1.0.1-py2.py3-none-any.whl (md5) Python Wheel 2.7 2014-09-03 15KB
  • Downloads (All Versions):
  • 832 downloads in the last day
  • 5227 downloads in the last week
  • 20297 downloads in the last month