Django.fun

Django - Disable authenticator for Swagger

So I have a Django app with Swagger, but I also added a custom authenticator to every endpoint automatically with

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'cheers.utils.authenticator.CognitoAuthentication',
    ),
}

How do I turn this off for swagger? The reason I'm not sure is because Swagger is added to URLs it's not a view

enter image description here

How do I disable automatic authentication for swagger?
Also I guess a side question would be how to disable this URL when debug is False

Answers: 1

Answered by Rvector, Oct. 13, 2021, 1:21 p.m.

To disable authentication | permission on swagger urls, set permission_classes in get_schema_view like this :

urls.py

from django.urls import path
from rest_framework import permissions
from rest_framework.schemas import get_schema_view
from django.conf import settings

schema_view = get_schema_view(
    openapi.Info(
        title="Resource API",
        default_version="v1",
        description="A sample API for resource with DRF",
        terms_of_service="https://www.google.com/policies/terms/",
        contact=openapi.Contact(email="your_email@yopmail.com"),
        license=openapi.License(name="BSD License"),
    ),
    public=True,
    permission_classes=(
    permissions.AllowAny, ),  # Anyone have access to API documentation
)

if settings.DEBUG == False
    # urlpatterns
    urlpatterns = [
        # Production urls only
    ]
else:
    urlpatterns = [
        # Production urls + swagger doc urls
        path(
        'doc/',
        schema_view.with_ui('swagger', cache_timeout=0),
        name='schema-swagger-ui'),
    ]

For Django admin without authentication follow this post.