mirror of
https://github.com/netbox-community/netbox.git
synced 2026-01-21 11:08:44 -06:00
Fixes #21181: Handle AuthenticationFailed exception on /media endpoint
This commit is contained in:
@@ -5,9 +5,11 @@ from django.conf import settings
|
|||||||
from django.contrib.auth.mixins import AccessMixin
|
from django.contrib.auth.mixins import AccessMixin
|
||||||
from django.core.exceptions import ImproperlyConfigured
|
from django.core.exceptions import ImproperlyConfigured
|
||||||
from django.db.models import QuerySet
|
from django.db.models import QuerySet
|
||||||
|
from django.http import HttpResponseForbidden
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django.urls.exceptions import NoReverseMatch
|
from django.urls.exceptions import NoReverseMatch
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
from rest_framework.exceptions import AuthenticationFailed
|
||||||
|
|
||||||
from netbox.api.authentication import TokenAuthentication
|
from netbox.api.authentication import TokenAuthentication
|
||||||
from netbox.plugins import PluginConfig
|
from netbox.plugins import PluginConfig
|
||||||
@@ -50,10 +52,12 @@ class TokenConditionalLoginRequiredMixin(ConditionalLoginRequiredMixin):
|
|||||||
# Attempt to authenticate the user using a DRF token, if provided
|
# Attempt to authenticate the user using a DRF token, if provided
|
||||||
if settings.LOGIN_REQUIRED and not request.user.is_authenticated:
|
if settings.LOGIN_REQUIRED and not request.user.is_authenticated:
|
||||||
authenticator = TokenAuthentication()
|
authenticator = TokenAuthentication()
|
||||||
auth_info = authenticator.authenticate(request)
|
try:
|
||||||
if auth_info is not None:
|
if auth_info := authenticator.authenticate(request) is not None:
|
||||||
request.user = auth_info[0] # User object
|
request.user = auth_info[0] # User object
|
||||||
request.auth = auth_info[1]
|
request.auth = auth_info[1]
|
||||||
|
except AuthenticationFailed:
|
||||||
|
return HttpResponseForbidden("Invalid token")
|
||||||
|
|
||||||
return super().dispatch(request, *args, **kwargs)
|
return super().dispatch(request, *args, **kwargs)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user