Difficulty with Django Rest Framework (DRF) Swagger Authentication Configuration

28 views Asked by At

I'm encountering issues configuring authentication for the Swagger in the Django Rest Framework. I've set up the drf_yasg package along with the necessary configurations in settings.py and urls.py. However, when I try to log out using Swagger, I get a 403 Forbidden error, and subsequent attempts to access Swagger result in a blank page.

settings.py:

SWAGGER_SETTINGS = {
    'SECURITY_DEFINITIONS': {
        'basic': {
            'type': 'basic'
        }
    },
    'LOGIN_URL': 'rest_framework:login',  # Configuring login URL for Swagger
    'LOGOUT_URL': 'rest_framework:logout',  # Configuring logout URL for Swagger
}

REDOC_SETTINGS = {
    'LAZY_RENDERING': False,
}

urls.py

from django.contrib import admin
from django.urls import path, include
from drf_yasg import openapi
from drf_yasg.views import get_schema_view
from rest_framework import permissions

API_TITLE = "Blog API"
API_DESCRIPTION = "A Web API for creating and editing blog posts."

# Configuration for drf-yasg
schema_view = get_schema_view(
    openapi.Info(
        title=API_TITLE,
        default_version='v1',
        description=API_DESCRIPTION,
    ),
    public=True,
    permission_classes=[permissions.IsAuthenticated],  # Setting permission for the schema_view
)

urlpatterns = [
    # Routes for admin views and API
    # ... (other routes)

    # Routes for Swagger UI documentation in different formats
    path('swagger<format>/', schema_view.without_ui(cache_timeout=0), name='schema-json'),
    path('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),

    # Route for ReDoc documentation
    path('redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
]

I've configured the LOGIN_URL and LOGOUT_URL in SWAGGER_SETTINGS and used permission_classes=[permissions.IsAuthenticated] for the schema_view. Despite this, the logout from Swagger results in a 403 Forbidden error and subsequent issues. Any insights on what might be causing this and how to resolve it would be greatly appreciated.

I appreciate any help you can provide.

0

There are 0 answers