From a54f8947bb97d56953c3a501e1df244c5e940879 Mon Sep 17 00:00:00 2001 From: Mark Hoekveen Date: Thu, 22 May 2025 22:30:06 +0200 Subject: [PATCH] Default authentication on endpoints Ik was er namelijk alweer eentje vergeten dus heh. --- muzak/settings.py | 3 +++ playlist/views/api/artist.py | 5 ----- playlist/views/api/profile.py | 11 ----------- playlist/views/api/root.py | 4 ++-- playlist/views/api/track.py | 6 ------ playlist/views/api/user.py | 6 ------ 6 files changed, 5 insertions(+), 30 deletions(-) diff --git a/muzak/settings.py b/muzak/settings.py index b5b1e97..2dbf665 100644 --- a/muzak/settings.py +++ b/muzak/settings.py @@ -91,6 +91,9 @@ REST_FRAMEWORK = { 'mozilla_django_oidc.contrib.drf.OIDCAuthentication', 'rest_framework.authentication.SessionAuthentication', ], + 'DEFAULT_PERMISSION_CLASSES': [ + 'rest_framework.permissions.IsAuthenticated', + ] } OIDC_RP_CLIENT_ID = os.getenv('OIDC_RP_CLIENT_ID') diff --git a/playlist/views/api/artist.py b/playlist/views/api/artist.py index 75d084d..89185df 100644 --- a/playlist/views/api/artist.py +++ b/playlist/views/api/artist.py @@ -1,15 +1,10 @@ from itertools import permutations from rest_framework.views import APIView from rest_framework.response import Response -from rest_framework.authentication import SessionAuthentication -from rest_framework.permissions import IsAuthenticated from playlist.models import Artist from playlist.serializers import ArtistSerializer class ArtistListView(APIView): - authentication_classes = [SessionAuthentication] - permission_classes = [IsAuthenticated] - def get(self, request): artists = Artist.objects.all() serializer = ArtistSerializer(artists, many=True, context={'request': request}) diff --git a/playlist/views/api/profile.py b/playlist/views/api/profile.py index 3cfa494..6aa6175 100644 --- a/playlist/views/api/profile.py +++ b/playlist/views/api/profile.py @@ -2,33 +2,22 @@ from pickle import BUILD from playlist import serializers from rest_framework.views import APIView from rest_framework.response import Response -from rest_framework.authentication import SessionAuthentication -from rest_framework.permissions import IsAuthenticated from playlist.serializers import ProfileSerializer, BackgroundSerializer from playlist.models import Profile, Background class ProfileDetailView(APIView): - authentication_classes = [SessionAuthentication] - permission_classes = [IsAuthenticated] - def get(self, request, id): profile = Profile.objects.get(pk=id) serializer = ProfileSerializer(profile) return Response(serializer.data) class BackgroundListView(APIView): - authentication_classes = [SessionAuthentication] - permission_classes = [IsAuthenticated] - def get(self, request): backgrounds = Background.objects.all() serializer = BackgroundSerializer(backgrounds, many=True) return Response(serializer.data) class BackgroundDetailView(APIView): - authentication_classes = [SessionAuthentication] - permission_classes = [IsAuthenticated] - def get(self, request, id): background = Background.objects.get(pk=id) serializer = BackgroundSerializer(background) diff --git a/playlist/views/api/root.py b/playlist/views/api/root.py index 60c43fb..44538bf 100644 --- a/playlist/views/api/root.py +++ b/playlist/views/api/root.py @@ -1,9 +1,9 @@ -from rest_framework.decorators import api_view +from rest_framework.decorators import api_view, permission_classes from rest_framework.response import Response from rest_framework.reverse import reverse - @api_view(['GET']) +@permission_classes([]) def api_root(request, format=None): return Response({ 'tracks': reverse('api-tracks', request=request, format=format), diff --git a/playlist/views/api/track.py b/playlist/views/api/track.py index d4d534f..d247839 100644 --- a/playlist/views/api/track.py +++ b/playlist/views/api/track.py @@ -1,15 +1,11 @@ from rest_framework.views import APIView from rest_framework.response import Response -from rest_framework.authentication import SessionAuthentication -from rest_framework.permissions import IsAuthenticated from playlist.models import Track, Profile from playlist.serializers import TrackSerializer from playlist.spotify import spt from playlist.tasks import get_banter, get_and_dither_image class TrackListView(APIView): - authentication_classes = [SessionAuthentication] - permission_classes = [IsAuthenticated] def get(self, request): tracks = Track.objects.all() serializer = TrackSerializer(tracks, many=True, context={'request': request}) @@ -32,8 +28,6 @@ class TrackListView(APIView): return Response({'error': 'Only superusers can delete all tracks'}, status=403) class TrackDetailView(APIView): - authentication_classes = [SessionAuthentication] - permission_classes = [IsAuthenticated] def get(self, request, id): track = Track.objects.get(pk=id) serializer = TrackSerializer(track, context={'request': request}) diff --git a/playlist/views/api/user.py b/playlist/views/api/user.py index 45c5ee6..f585c78 100644 --- a/playlist/views/api/user.py +++ b/playlist/views/api/user.py @@ -2,20 +2,14 @@ from rest_framework.views import APIView from rest_framework.response import Response from django.contrib.auth.models import User from playlist.serializers import UserSerializer -from rest_framework.authentication import SessionAuthentication -from rest_framework.permissions import IsAuthenticated class UserListView(APIView): - authentication_classes = [SessionAuthentication] - permission_classes = [IsAuthenticated] def get(self, request): users = User.objects.all() serializer = UserSerializer(users, many=True, context={'request': request}) return Response(serializer.data) class UserDetailView(APIView): - authentication_classes = [SessionAuthentication] - permission_classes = [IsAuthenticated] def get(self, request, id): user = User.objects.get(pk=id) serializer = UserSerializer(user, context={'request': request})