Skip to content

Commit

Permalink
Handle unauthorized exceptions in a custom handler
Browse files Browse the repository at this point in the history
closes pulp#1254
  • Loading branch information
lubosmj committed Apr 11, 2023
1 parent 6534175 commit 7f78491
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES/1254.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed a bug that disallowed users to configure custom authentication classes for the token server.
17 changes: 16 additions & 1 deletion pulp_container/app/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
from rest_framework.exceptions import NotFound, ParseError
from rest_framework import status, views
from rest_framework.exceptions import (
AuthenticationFailed,
NotAuthenticated,
NotFound,
ParseError,
)


def unauthorized_exception_handler(exc, context):
response = views.exception_handler(exc, context)

if isinstance(exc, (AuthenticationFailed, NotAuthenticated)):
response.status_code = status.HTTP_401_UNAUTHORIZED

return response


class RepositoryNotFound(NotFound):
Expand Down
2 changes: 0 additions & 2 deletions pulp_container/app/registry_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
from pulpcore.plugin.files import PulpTemporaryUploadedFile
from pulpcore.plugin.tasking import add_and_remove, dispatch
from pulpcore.plugin.util import get_objects_for_user
from rest_framework.authentication import BasicAuthentication
from rest_framework.exceptions import (
AuthenticationFailed,
NotAuthenticated,
Expand Down Expand Up @@ -357,7 +356,6 @@ class BearerTokenView(APIView):
"""

# Allow everyone to access but still value authenticated users.
authentication_classes = [BasicAuthentication]
permission_classes = []

def get(self, request):
Expand Down
8 changes: 8 additions & 0 deletions pulp_container/app/settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
from copy import deepcopy
from django.conf import settings

DRF_ACCESS_POLICY = {
"dynaconf_merge_unique": True,
"reusable_conditions": ["pulp_container.app.global_access_conditions"],
}

REST_FRAMEWORK = deepcopy(settings.REST_FRAMEWORK)
REST_FRAMEWORK.update(
{"EXCEPTION_HANDLER": "pulp_container.app.exceptions.unauthorized_exception_handler"}
)

0 comments on commit 7f78491

Please sign in to comment.