From a811d304c2bc9aafd9a16f1575baa0a9008feebb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Desfr=C3=AAnes?= <mickael.desfrenes@unicaen.fr> Date: Wed, 11 Dec 2024 21:18:00 +0100 Subject: [PATCH] trap InvalidToken --- pount/apps/iiif/views.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/pount/apps/iiif/views.py b/pount/apps/iiif/views.py index a765bea6..e0ec212b 100644 --- a/pount/apps/iiif/views.py +++ b/pount/apps/iiif/views.py @@ -5,6 +5,7 @@ from django.contrib.auth import get_user_model from django.core.exceptions import PermissionDenied from django.shortcuts import get_object_or_404 from rest_framework_simplejwt.authentication import JWTAuthentication +from rest_framework_simplejwt.exceptions import InvalidToken from revproxy.views import ProxyView from pount.apps.api.models import MediaFile @@ -53,16 +54,19 @@ def get_request_headers(self): ProxyView.get_request_headers = get_request_headers -class TestProxyView(ProxyView): +class IIIFProxyView(ProxyView): upstream = environ.get("IIIF_UPSTREAM_URL", "http://localhost:8182/iiif/") add_x_forwarded = True def dispatch(self, request, *args, **kwargs): - response = JWT_authenticator.authenticate(request) - if response is not None: - user, _ = response - file_id = request.path.lstrip("iiif/3").split(".tiled.tif")[0] - file = get_object_or_404(MediaFile, id=file_id) - if user.has_perm(ITEM_VIEW, file.item): - return super().dispatch(request, *args, **kwargs) + try: + response = JWT_authenticator.authenticate(request) + if response is not None: + user, _ = response + file_id = request.path.lstrip("iiif/3").split(".tiled.tif")[0] + file = get_object_or_404(MediaFile, id=file_id) + if user.has_perm(ITEM_VIEW, file.item): + return super().dispatch(request, *args, **kwargs) + except InvalidToken: + pass # should renew ? raise PermissionDenied() -- GitLab