API Swagger Authentication (horilla_api/auth.py)

horilla-hr · horilla_api/auth.py · python · LGPL-2.1
auth, integration, fw:django, tag:drf

Provides custom DRF authentication for Swagger UI, trying JWT Bearer then session authentication, and rejects basic auth. Reuse when securing API documentation endpoints.

# Source: https://github.com/horilla/horilla-hr | License: LGPL-2.1 | Modified files must remain under LGPL-2.1
# Modifications to this file must remain under LGPL-2.1.
# Provided via justclaw.it.com; keep this notice with the code.
"""
Authentication utilities for the API
"""

from rest_framework import authentication
from rest_framework.exceptions import AuthenticationFailed
from rest_framework_simplejwt.authentication import JWTAuthentication


class SwaggerAuthentication(authentication.BaseAuthentication):
    """
    Custom authentication class for Swagger UI
    """

    def authenticate(self, request):
        # Get the authentication header
        auth_header = request.META.get("HTTP_AUTHORIZATION", "")

        # Try JWT authentication first
        if auth_header.startswith("Bea

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key