Convert Figma logo to code with AI

jpadilla logodjango-rest-framework-jwt

JSON Web Token Authentication support for Django REST Framework

3,183
654
3,183
163

Top Related Projects

A JSON Web Token authentication plugin for the Django REST Framework.

A JSON Web Token authentication plugin for the Django REST Framework.

Quick Overview

Django REST framework JWT is a JSON Web Token authentication plugin for Django REST Framework. It provides a simple and secure way to implement token-based authentication in Django REST Framework applications, allowing for stateless authentication between clients and servers.

Pros

  • Easy integration with Django REST Framework
  • Stateless authentication, reducing server-side storage requirements
  • Supports token expiration and refresh mechanisms
  • Compatible with various client-side frameworks and mobile applications

Cons

  • No longer actively maintained (last update in 2018)
  • Lacks some modern security features found in newer JWT libraries
  • Limited built-in support for token revocation
  • May require additional configuration for complex authentication scenarios

Code Examples

  1. Setting up JWT authentication in Django settings:
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
    ],
}

JWT_AUTH = {
    'JWT_EXPIRATION_DELTA': datetime.timedelta(days=1),
    'JWT_ALLOW_REFRESH': True,
    'JWT_REFRESH_EXPIRATION_DELTA': datetime.timedelta(days=7),
}
  1. Obtaining a JWT token:
from rest_framework_jwt.views import obtain_jwt_token

urlpatterns = [
    path('api-token-auth/', obtain_jwt_token),
]
  1. Refreshing a JWT token:
from rest_framework_jwt.views import refresh_jwt_token

urlpatterns = [
    path('api-token-refresh/', refresh_jwt_token),
]
  1. Using JWT authentication in a view:
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from rest_framework.permissions import IsAuthenticated

class ProtectedView(APIView):
    authentication_classes = [JSONWebTokenAuthentication]
    permission_classes = [IsAuthenticated]

    def get(self, request):
        return Response({'message': 'This is a protected view'})

Getting Started

  1. Install the package:

    pip install djangorestframework-jwt
    
  2. Add rest_framework_jwt to your INSTALLED_APPS in settings.py:

    INSTALLED_APPS = [
        # ...
        'rest_framework',
        'rest_framework_jwt',
    ]
    
  3. Configure authentication classes in settings.py:

    REST_FRAMEWORK = {
        'DEFAULT_AUTHENTICATION_CLASSES': [
            'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
        ],
    }
    
  4. Add JWT views to your urls.py:

    from rest_framework_jwt.views import obtain_jwt_token, refresh_jwt_token
    
    urlpatterns = [
        path('api-token-auth/', obtain_jwt_token),
        path('api-token-refresh/', refresh_jwt_token),
    ]
    
  5. Use the JSONWebTokenAuthentication class in your views or set it as the default authentication class.

Competitor Comparisons

A JSON Web Token authentication plugin for the Django REST Framework.

Pros of djangorestframework-simplejwt

  • More actively maintained with regular updates and bug fixes
  • Supports token blacklisting for enhanced security
  • Offers more customization options and flexibility

Cons of djangorestframework-simplejwt

  • Slightly more complex setup and configuration
  • May require additional dependencies for certain features

Code Comparison

django-rest-framework-jwt:

from rest_framework_jwt.views import obtain_jwt_token

urlpatterns = [
    path('api-token-auth/', obtain_jwt_token),
]

djangorestframework-simplejwt:

from rest_framework_simplejwt.views import TokenObtainPairView

urlpatterns = [
    path('api/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
]

Both libraries provide similar functionality for JWT authentication in Django REST Framework. However, djangorestframework-simplejwt offers more features and is actively maintained, making it a better choice for new projects. It provides token blacklisting, refresh tokens, and more customization options out of the box. On the other hand, django-rest-framework-jwt is simpler to set up but lacks some advanced features and has not been updated recently. The code comparison shows that both libraries have similar usage patterns, with slight differences in import statements and view names.

A JSON Web Token authentication plugin for the Django REST Framework.

Pros of djangorestframework-simplejwt

  • More actively maintained with regular updates and bug fixes
  • Supports token blacklisting for enhanced security
  • Offers more customization options and flexibility

Cons of djangorestframework-simplejwt

  • Slightly more complex setup and configuration
  • May require additional dependencies for certain features

Code Comparison

django-rest-framework-jwt:

from rest_framework_jwt.views import obtain_jwt_token

urlpatterns = [
    path('api-token-auth/', obtain_jwt_token),
]

djangorestframework-simplejwt:

from rest_framework_simplejwt.views import TokenObtainPairView

urlpatterns = [
    path('api/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
]

Both libraries provide similar functionality for JWT authentication in Django REST Framework. However, djangorestframework-simplejwt offers more features and is actively maintained, making it a better choice for new projects. It provides token blacklisting, refresh tokens, and more customization options out of the box. On the other hand, django-rest-framework-jwt is simpler to set up but lacks some advanced features and has not been updated recently. The code comparison shows that both libraries have similar usage patterns, with slight differences in import statements and view names.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

REST framework JWT Auth


Notice

This project is currently unmaintained. Check #484 for more details and suggested alternatives_.


|build-status-image| |pypi-version|

JSON Web Token Authentication support for Django REST Framework

Full documentation for the project is available at docs_.

Overview

This package provides JSON Web Token Authentication_ support for Django REST framework_.

If you want to know more about JWT, check out the following resources:

  • DjangoCon 2014 - JSON Web Tokens Video_ | Slides_
  • Auth with JSON Web Tokens_
  • JWT.io_

Requirements

  • Python (2.7, 3.3, 3.4, 3.5, 3.6)
  • Django (1.8, 1.9, 1.10, 1.11)
  • Django REST Framework (3.1, 3.2, 3.3, 3.4, 3.5, 3.6)

Installation

Install using pip\ ...

.. code:: bash

$ pip install djangorestframework-jwt

Documentation & Support

Full documentation for the project is available at docs_.

You may also want to follow the author_ on Twitter.

.. _Check #484 for more details and suggested alternatives: https://github.com/jpadilla/django-rest-framework-jwt/issues/484 .. _docs: https://jpadilla.github.io/django-rest-framework-jwt/ .. _JSON Web Token Authentication: http://tools.ietf.org/html/draft-ietf-oauth-json-web-token .. _Django REST framework: http://django-rest-framework.org/ .. _Video: https://www.youtube.com/watch?v=825hodQ61bg .. _Slides: https://speakerdeck.com/jpadilla/djangocon-json-web-tokens .. _Auth with JSON Web Tokens: http://jpadilla.com/post/73791304724/auth-with-json-web-tokens .. _JWT.io: http://jwt.io/ .. _author: https://twitter.com/blimp

.. |build-status-image| image:: https://secure.travis-ci.org/jpadilla/django-rest-framework-jwt.svg?branch=master :target: http://travis-ci.org/jpadilla/django-rest-framework-jwt?branch=master .. |pypi-version| image:: https://img.shields.io/pypi/v/djangorestframework-jwt.svg :target: https://pypi.python.org/pypi/djangorestframework-jwt