Skip to content

Commit

Permalink
Ajout authentification REST
Browse files Browse the repository at this point in the history
  • Loading branch information
rigaux committed Nov 6, 2024
1 parent b0fb5e8 commit 5b864e8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 12 deletions.
4 changes: 4 additions & 0 deletions rest/json_examples/curl_commands.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ curl -u sameh:collabscore -X POST http://neuma.huma-num.fr/rest/collections/all:
curl -u rigaux:fuf3a3wu -X POST http://neuma.huma-num.fr/rest/collections/all:collabscore:saintsaens-ref:C006_0/_sources/iiif/_apply_editions/ \
-d @replace_clef.json -H "Content-Type: application/json" > t.xml

curl -u rigaux:Fuf3a3wu! -X POST http://localhost:8000/rest/collections/all%3Acollabscore%3Asaintsaens-ref%3AC006_0/_sources/iiif/_editions/ \
-d @eds_replace_clef.json -H "Content-Type: application/json"


Le type frozen est-il obligatoire ?
15 changes: 14 additions & 1 deletion rest/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
PAGE_TYPE = "ScorePage"


"""
For login
"""
class LoginSerializer(serializers.Serializer):
data = serializers.CharField()

"""
For simple services that return a message
"""
Expand Down Expand Up @@ -136,7 +142,14 @@ def to_representation(self, instance):
source_dict = instance.to_serializable("abs_url")
return source_dict.to_json()



class EditionsSerializer(serializers.Serializer):
"""
Serialization of editions on sources
"""
name = serializers.CharField(default="noop")
params = serializers.CharField()

class ArcIdxCorpusSerializer(serializers.Serializer):
id = serializers.CharField(source="ref")
name = serializers.CharField(source="title")
Expand Down
3 changes: 3 additions & 0 deletions rest/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

from rest_framework import permissions
from rest_framework.urlpatterns import format_suffix_patterns
from rest_framework.authtoken.views import obtain_auth_token

app_name="rest"

urlpatterns = [
# Welcome message
path('', views.welcome, name='welcome'),
# To obtain a token
path('login/', views.CustomAuthToken.as_view()),
###############
## MISC SERVICE
###############
Expand Down
38 changes: 28 additions & 10 deletions rest/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
from rest_framework.views import APIView
from rest_framework import mixins
from rest_framework import generics

from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.authtoken.models import Token

from django.http import Http404

from rest_framework.response import Response
Expand Down Expand Up @@ -66,12 +70,14 @@
)

from .serializers import (
LoginSerializer,
MessageSerializer,
ModelSerializer,
ConceptSerializer,
CorpusSerializer,
OpusSerializer,
SourceSerializer,
EditionsSerializer,
AnnotationStatsSerializer,
AnnotationSerializer,
ModelStatsSerializer,
Expand Down Expand Up @@ -127,6 +133,22 @@ def __init__(self, data, **kwargs):
Services implementation
'''

class CustomAuthToken(ObtainAuthToken):
#serializer_class = LoginSerializer

@extend_schema(operation_id="Login")
def post(self, request):
serializer = self.serializer_class(data=request.data,
context={'request': request})
serializer.is_valid(raise_exception=True)
user = serializer.validated_data['user']
token, created = Token.objects.get_or_create(user=user)
return Response({
'token': token.key,
'user_id': user.pk,
'email': user.email
})

@extend_schema(operation_id="NeumaApi_v3",
description="Welcome message to the Neuma REST API",
responses=MessageSerializer)
Expand Down Expand Up @@ -540,8 +562,6 @@ def get(self, request, full_neuma_ref, source_ref, format=None):

#return JSONResponse({"status": "ko", "message": f"No manifest for source {source_ref}"})


@extend_schema(operation_id="SourceApplyEditions")
class SourceApplyEditions(APIView):

"""
Expand All @@ -564,6 +584,7 @@ def get_object(self, full_neuma_ref, source_ref):
except OpusSource.DoesNotExist:
raise Http404

@extend_schema(operation_id="SourceApplyEditions")
def post(self, request, full_neuma_ref, source_ref):
source = self.get_object(full_neuma_ref, source_ref)
xml_file_name = source.apply_editions(request.data)
Expand All @@ -581,14 +602,13 @@ def post(self, request, full_neuma_ref, source_ref):
' Access-Control-Request-Headers, credentials'
return resp

@extend_schema(operation_id="SourceEditions")
class SourceEditions(APIView):

"""
Return the JSON editions of a source
Operations on editions
"""
serializer_class = SourceSerializer
serializer_class = EditionsSerializer
#serializer_class = MessageSerializer

def get_queryset(self):
opus_ref = self.kwargs['full_neuma_ref']
Expand Down Expand Up @@ -619,9 +639,8 @@ def put(self, request, full_neuma_ref, source_ref):

@extend_schema(operation_id="SourceEditionsPost")
def post(self, request, full_neuma_ref, source_ref):
source = self.get_object(full_neuma_ref, source_ref)
print (f"Reception de {request.data}")

#SourceEditions.serializer_class = MessageSerializer
source = self.get_object(full_neuma_ref, source_ref)
editions_json = request.data
for edition in editions_json:
print (f"Received key {edition['name']}")
Expand All @@ -634,7 +653,6 @@ def post(self, request, full_neuma_ref, source_ref):
class SourceFile (APIView):
"""
Return the file of a source
"""

def get_queryset(self):
Expand Down
3 changes: 2 additions & 1 deletion scorelib/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
"home",
"manager",
"rest",
"rest_framework",
"rest_framework",
'rest_framework.authtoken',
"mptt",
'drf_spectacular',
'corsheaders',
Expand Down

0 comments on commit 5b864e8

Please sign in to comment.