From e9e77945a6fb1723a1bae51a4eeeb865670bcf74 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Thu, 14 May 2020 17:19:18 +0300 Subject: [PATCH] Return token and user on signup/login. --- django_etesync/serializers.py | 10 ++++++++-- django_etesync/views.py | 19 +++++++++++++------ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/django_etesync/serializers.py b/django_etesync/serializers.py index fabc7cb..7173d73 100644 --- a/django_etesync/serializers.py +++ b/django_etesync/serializers.py @@ -206,8 +206,14 @@ class UserSerializer(serializers.ModelSerializer): fields = (User.USERNAME_FIELD, User.EMAIL_FIELD) +class UserQuerySerializer(serializers.ModelSerializer): + class Meta: + model = User + fields = (User.USERNAME_FIELD, User.EMAIL_FIELD) + + class AuthenticationSignupSerializer(serializers.Serializer): - user = UserSerializer(many=False) + user = UserQuerySerializer(many=False) salt = BinaryBase64Field() pubkey = BinaryBase64Field() @@ -217,7 +223,7 @@ class AuthenticationSignupSerializer(serializers.Serializer): pubkey = validated_data.pop('pubkey') with transaction.atomic(): - instance = UserSerializer.Meta.model.objects.create(**validated_data) + instance = User.objects.create(**validated_data) instance.set_unusable_password() models.UserInfo.objects.create(salt=salt, pubkey=pubkey, owner=instance) diff --git a/django_etesync/views.py b/django_etesync/views.py index cb52ca5..eaf0e35 100644 --- a/django_etesync/views.py +++ b/django_etesync/views.py @@ -44,7 +44,8 @@ from .serializers import ( CollectionSerializer, CollectionItemSerializer, CollectionItemRevisionSerializer, - CollectionItemChunkSerializer + CollectionItemChunkSerializer, + UserSerializer, ) @@ -313,6 +314,12 @@ class AuthenticationViewSet(viewsets.ViewSet): def get_queryset(self): return User.objects.all() + def login_response_data(self, user): + return { + 'token': Token.objects.get_or_create(user=user)[0].key, + 'user': UserSerializer(user).data, + } + def list(self, request): return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED) @@ -320,9 +327,10 @@ class AuthenticationViewSet(viewsets.ViewSet): def signup(self, request): serializer = AuthenticationSignupSerializer(data=request.data) if serializer.is_valid(): - serializer.save() + user = serializer.save() - return Response({}, status=status.HTTP_201_CREATED) + data = self.login_response_data(user) + return Response(data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) @@ -360,6 +368,7 @@ class AuthenticationViewSet(viewsets.ViewSet): ret = { "salt": b64encode(salt), "challenge": b64encode(challenge), + "version": user.userinfo.version, } return Response(ret, status=status.HTTP_200_OK) @@ -401,9 +410,7 @@ class AuthenticationViewSet(viewsets.ViewSet): verify_key = nacl.signing.VerifyKey(user.userinfo.pubkey, encoder=nacl.encoding.RawEncoder) verify_key.verify(response_raw, signature) - data = { - 'token': Token.objects.get_or_create(user=user)[0].key, - } + data = self.login_response_data(user) return Response(data, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)