mirror of
https://github.com/jlengrand/bugsink.git
synced 2026-03-10 08:01:17 +00:00
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:
@@ -1,6 +1,5 @@
|
|||||||
from django.db.backends.signals import connection_created
|
from django.db.backends.signals import connection_created
|
||||||
from django.contrib.auth.management.commands.createsuperuser import Command as CreateSuperUserCommand
|
from django.contrib.auth.management.commands.createsuperuser import Command as CreateSuperUserCommand
|
||||||
from drf_spectacular.extensions import OpenApiAuthenticationExtension
|
|
||||||
|
|
||||||
|
|
||||||
def set_pragmas(sender, connection, **kwargs):
|
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
|
unpatched_get_input_message = CreateSuperUserCommand._get_input_message
|
||||||
CreateSuperUserCommand._get_input_message = _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',
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
from django.contrib.auth.models import AnonymousUser
|
from django.contrib.auth.models import AnonymousUser
|
||||||
from rest_framework.authentication import BaseAuthentication
|
from rest_framework.authentication import BaseAuthentication
|
||||||
from rest_framework import exceptions
|
from rest_framework import exceptions
|
||||||
|
from drf_spectacular.extensions import OpenApiAuthenticationExtension
|
||||||
|
|
||||||
from bsmain.models import AuthToken
|
from bsmain.models import AuthToken
|
||||||
|
|
||||||
|
|
||||||
@@ -29,3 +31,16 @@ class BearerTokenAuthentication(BaseAuthentication):
|
|||||||
def authenticate_header(self, request):
|
def authenticate_header(self, request):
|
||||||
# tells DRF what to send in WWW-Authenticate on 401 responses, hinting the required auth scheme
|
# tells DRF what to send in WWW-Authenticate on 401 responses, hinting the required auth scheme
|
||||||
return self.keyword
|
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',
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user