Fix import order between Bugsink and DRF

Move DRF/OpenAPI authentication import out of bugsink/__init__.py into
bugsink/authentication.py.

Before: running bugsink-manage imported the bugsink package, whose __init__
pulled in drf_spectacular → DRF → DRF’s api_settings before Django settings
were configured, raising ImproperlyConfigured. (Did not happen in dev where
apps are loaded after settings.)

After: DRF is first touched during server load via urls → routers → views →
schemas. Only then does DRF’s api_settings resolve DEFAULT_AUTHENTICATION_CLASSES,
importing bugsink.authentication at a point where settings are already ready.
This commit is contained in:
Klaas van Schelven
2025-09-16 11:08:21 +02:00
parent e346f8d5c2
commit aa799e9c94
2 changed files with 15 additions and 14 deletions

View File

@@ -1,6 +1,5 @@
from django.db.backends.signals import connection_created
from django.contrib.auth.management.commands.createsuperuser import Command as CreateSuperUserCommand
from drf_spectacular.extensions import OpenApiAuthenticationExtension
def set_pragmas(sender, connection, **kwargs):
@@ -41,16 +40,3 @@ def _get_input_message(self, field, default=None):
unpatched_get_input_message = CreateSuperUserCommand._get_input_message
CreateSuperUserCommand._get_input_message = _get_input_message
class BearerTokenAuthenticationExtension(OpenApiAuthenticationExtension):
# Will be auto-discovered b/c in __init__.py and subclass of OpenApiAuthenticationExtension
target_class = 'bugsink.authentication.BearerTokenAuthentication'
name = 'BearerAuth'
def get_security_definition(self, auto_schema):
return {
'type': 'http',
'scheme': 'bearer',
'bearerFormat': 'token',
}

View File

@@ -1,6 +1,8 @@
from django.contrib.auth.models import AnonymousUser
from rest_framework.authentication import BaseAuthentication
from rest_framework import exceptions
from drf_spectacular.extensions import OpenApiAuthenticationExtension
from bsmain.models import AuthToken
@@ -29,3 +31,16 @@ class BearerTokenAuthentication(BaseAuthentication):
def authenticate_header(self, request):
# tells DRF what to send in WWW-Authenticate on 401 responses, hinting the required auth scheme
return self.keyword
class BearerTokenAuthenticationExtension(OpenApiAuthenticationExtension):
# auto-discovered b/c authentication is loaded in settnigs and this is a subclass of OpenApiAuthenticationExtension
target_class = 'bugsink.authentication.BearerTokenAuthentication'
name = 'BearerAuth'
def get_security_definition(self, auto_schema):
return {
'type': 'http',
'scheme': 'bearer',
'bearerFormat': 'token',
}