Skip to main content

Installation Guide for drf-paseto

drf-paseto is a Django REST Framework authentication backend that uses PASETO (Platform-Agnostic Security Tokens) for secure authentication.

Step 1: Install the Package

First, you need to install the package. If the package is published on PyPI, you can install it using pip. Otherwise, if you're installing it from a local build, follow the steps below.

pip install drf_paseto

If you are installing it from a local repository, clone the repository and install it:

git clone https://github.com/bahmany/drf_paseto.git
cd drf-paseto
pip install .

Step 2: Add the Package to Your Installed Apps

Add drf_paseto_auth to the INSTALLED_APPS in your Django project's settings.py file:

INSTALLED_APPS = [
    ...
    'drf_paseto',
    ...
]

Step 3: Update Django REST Framework Authentication Settings

Update the REST_FRAMEWORK settings in your settings.py file to use PasetoAuthentication as the default authentication class:

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'drf_paseto_auth.authentication.PasetoAuthentication',
    ),
}

Step 4: Set Your PASETO Secret Key

Ensure that you have a secure secret key set up in your settings.py file. This key will be used to sign and verify the PASETO tokens.

SECRET_KEY = 'your-very-secure-and-random-secret-key'

You should replace 'your-very-secure-and-random-secret-key' with a secure, randomly generated string.

Step 5: Create a Login View to Generate PASETO Tokens

Create a new view in your Django app to authenticate users and generate PASETO tokens. Add the following code to your views.py:

from django.contrib.auth import authenticate
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from drf_paseto.authentication import PasetoAuthentication


class LoginView(APIView):
   authentication_classes = []  # No authentication needed for login

   def post(self, request):
      username = request.data.get('username')
      password = request.data.get('password')
      user = authenticate(username=username, password=password)

      if user is not None:
         token = PasetoAuthentication.generate_token(user)
         return Response({'token': token}, status=status.HTTP_200_OK)
      else:
         return Response({'error': 'Invalid credentials'}, status=status.HTTP_401_UNAUTHORIZED)

Step 6: Add URLs for the Login View

Add a URL pattern to your urls.py file to expose the login view:

from django.urls import path
from .views import LoginView

urlpatterns = [
    path('login/', LoginView.as_view(), name='login'),
]

Step 7: Protect Your API Endpoints

To protect your API endpoints with PASETO authentication, use the IsAuthenticated permission class. The custom PasetoAuthentication class will handle token verification.

from rest_framework.permissions import IsAuthenticated
from rest_framework.views import APIView
from rest_framework.response import Response

class ProtectedView(APIView):
    permission_classes = [IsAuthenticated]

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

Step 8: Test the Setup

  1. Start the Django development server:

    python manage.py runserver
    
  2. Login to get a PASETO token:

    Send a POST request to the /login/ endpoint with a valid username and password. You will receive a PASETO token in the response.

  3. Access Protected Endpoints:

    Use the received token to access protected endpoints by including it in the Authorization header as a Bearer token:

    Authorization: Bearer <your-paseto-token>
    

Conclusion

By following these steps, you have successfully installed and configured drf-paseto for PASETO-based authentication in your Django REST Framework project. This setup ensures a more secure token-based authentication mechanism compared to JWT.

Metadata

Release files for drf-paseto 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for drf-paseto 0.1.0
File Size Uploaded Provenance
drf_paseto-0.1.0.tar.gz 7.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for drf-paseto 0.1.0
File Interpreter ABI Platform
drf_paseto-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 15.7 kB

Release files / drf_paseto-0.1.0.tar.gz

Download URL drf_paseto-0.1.0.tar.gz
Size 7.6 kB
Tags Source
SHA-256 checksum
How to use checksums
ac3cda21885db9468f2357a0d73cb1a5be4e58bc257ccab09b95c10ebc52305d
BLAKE2b-256 checksum
How to use checksums
22a708078f4aef792dbdb2484ef8cb61e9cc5678005912ddc1b0f7aa65147442
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.0

Release files / drf_paseto-0.1.0-py3-none-any.whl

Download URL drf_paseto-0.1.0-py3-none-any.whl
Size 8.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
53c8c677ad51ba153afb8282e6fc61356305e97a984ab187439ecb1e7398865a
BLAKE2b-256 checksum
How to use checksums
916f296038ccbd3a7bd0e368d7624d26535c6861b0b36b9a1d283fd187f9e061
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.0

Security for drf-paseto 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page