Merge pull request #20943 from netbox-community/20936-api-auth-check
Some checks are pending
CI / build (20.x, 3.12) (push) Waiting to run
CI / build (20.x, 3.13) (push) Waiting to run
CodeQL / Analyze (${{ matrix.language }}) (none, actions) (push) Waiting to run
CodeQL / Analyze (${{ matrix.language }}) (none, javascript-typescript) (push) Waiting to run
CodeQL / Analyze (${{ matrix.language }}) (none, python) (push) Waiting to run

Closes #20936: Add a REST API endpoint to validate authentication credentials
This commit is contained in:
bctiemann 2025-12-07 16:03:55 -05:00 committed by GitHub
commit 3483d979d4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 28 additions and 1 deletions

View File

@ -5,6 +5,7 @@ from django.conf import settings
from django_rq.queues import get_connection
from drf_spectacular.types import OpenApiTypes
from drf_spectacular.utils import extend_schema
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.reverse import reverse
from rest_framework.views import APIView
@ -12,6 +13,7 @@ from rq.worker import Worker
from netbox.api.authentication import IsAuthenticatedOrLoginNotRequired
from netbox.plugins.utils import get_installed_plugins
from users.api.serializers import UserSerializer
from utilities.apps import get_installed_apps
@ -62,3 +64,15 @@ class StatusView(APIView):
'python-version': platform.python_version(),
'rq-workers-running': Worker.count(get_connection('default')),
})
class AuthenticationCheckView(APIView):
"""
Return the user making the request, if authenticated successfully.
"""
permission_classes = [IsAuthenticated]
@extend_schema(responses={200: OpenApiTypes.OBJECT})
def get(self, request):
serializer = UserSerializer(request.user, context={'request': request})
return Response(serializer.data)

View File

@ -32,6 +32,18 @@ class AppTest(APITestCase):
self.assertEqual(response.status_code, 200)
def test_authentication_check(self):
url = reverse('api-authentication-check')
# Test an unauthenticated request
response = self.client.get(f'{url}')
self.assertEqual(response.status_code, 403)
# Test an authenticated request
response = self.client.get(f'{url}', **self.header)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['id'], self.user.pk)
class OptionalLimitOffsetPaginationTest(TestCase):

View File

@ -5,7 +5,7 @@ from django.views.decorators.cache import cache_page
from drf_spectacular.views import SpectacularAPIView, SpectacularRedocView, SpectacularSwaggerView
from account.views import LoginView, LogoutView
from netbox.api.views import APIRootView, StatusView
from netbox.api.views import APIRootView, AuthenticationCheckView, StatusView
from netbox.graphql.schema import schema
from netbox.graphql.views import NetBoxGraphQLView
from netbox.plugins.urls import plugin_patterns, plugin_api_patterns
@ -53,6 +53,7 @@ _patterns = [
path('api/vpn/', include('vpn.api.urls')),
path('api/wireless/', include('wireless.api.urls')),
path('api/status/', StatusView.as_view(), name='api-status'),
path('api/authentication-check/', AuthenticationCheckView.as_view(), name='api-authentication-check'),
# REST API schema
path(