HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/projects/aigenerator/AI-LG-backend/Ai_logo_generation/helper.py
from django.http import HttpResponse
from django.conf import settings
import base64

def basic_auth_required(view_func):
    
    '''Protecting swagger url endpoint'''

    def _wrapped_view(request, *args, **kwargs):
        # Hardcoded credentials
        hardcoded_username = settings.SWAGGER_UI_PROTECTION_USERNAME
        hardcoded_password = settings.SWAGGER_UI_PROTECTION_PASSWORD

        # Check if the Authorization header is present
        auth_header = request.headers.get('Authorization')

        if auth_header is not None and auth_header.startswith('Basic '):
            # Decode the Base64 encoded credentials
            encoded_credentials = auth_header.split(' ')[1]
            decoded_credentials = base64.b64decode(encoded_credentials).decode('utf-8')
            username, password = decoded_credentials.split(':')

            # Validate the credentials
            if username == hardcoded_username and password == hardcoded_password:
                return view_func(request, *args, **kwargs)

        # If credentials are invalid or missing, prompt for Basic Auth
        response = HttpResponse('Unauthorized', status=401)
        response['WWW-Authenticate'] = 'Basic realm="Swagger UI"'
        return response

    return _wrapped_view