2020-02-19 18:53:43 +00:00
|
|
|
# Copyright © 2017 Tom Hacohen
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, version 3.
|
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2020-02-19 12:55:56 +00:00
|
|
|
|
2020-06-29 08:30:59 +00:00
|
|
|
import msgpack
|
2020-05-14 10:43:49 +00:00
|
|
|
|
2020-04-14 13:21:51 +00:00
|
|
|
from django.conf import settings
|
2020-06-03 12:49:38 +00:00
|
|
|
from django.contrib.auth import get_user_model, user_logged_in, user_logged_out
|
2020-05-24 15:19:22 +00:00
|
|
|
from django.core.exceptions import PermissionDenied
|
2020-04-19 14:32:40 +00:00
|
|
|
from django.db import transaction, IntegrityError
|
2020-10-15 07:50:07 +00:00
|
|
|
from django.db.models import Max, Value as V, Q
|
2020-06-24 10:20:07 +00:00
|
|
|
from django.db.models.functions import Coalesce, Greatest
|
2020-04-14 13:21:51 +00:00
|
|
|
from django.http import HttpResponseBadRequest, HttpResponse, Http404
|
2020-02-19 18:53:43 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
|
|
|
|
from rest_framework import status
|
|
|
|
from rest_framework import viewsets
|
2020-02-20 13:39:52 +00:00
|
|
|
from rest_framework.decorators import action as action_decorator
|
2020-02-19 18:53:43 +00:00
|
|
|
from rest_framework.response import Response
|
2020-06-29 10:01:40 +00:00
|
|
|
from rest_framework.parsers import JSONParser, FormParser, MultiPartParser
|
2020-06-29 11:50:06 +00:00
|
|
|
from rest_framework.renderers import BrowsableAPIRenderer
|
2020-09-09 14:07:32 +00:00
|
|
|
from rest_framework.exceptions import AuthenticationFailed
|
2020-10-09 10:10:41 +00:00
|
|
|
from rest_framework.permissions import IsAuthenticated
|
2020-05-14 10:43:49 +00:00
|
|
|
|
|
|
|
import nacl.encoding
|
|
|
|
import nacl.signing
|
|
|
|
import nacl.secret
|
|
|
|
import nacl.hash
|
2020-02-19 18:53:43 +00:00
|
|
|
|
2020-06-03 12:49:38 +00:00
|
|
|
from .token_auth.models import AuthToken
|
|
|
|
|
2020-06-29 10:01:40 +00:00
|
|
|
from .drf_msgpack.parsers import MessagePackParser
|
|
|
|
from .drf_msgpack.renderers import MessagePackRenderer
|
|
|
|
|
2020-05-20 10:48:46 +00:00
|
|
|
from . import app_settings, permissions
|
2020-06-29 11:50:06 +00:00
|
|
|
from .renderers import JSONRenderer
|
2020-05-26 15:52:44 +00:00
|
|
|
from .models import (
|
2020-11-14 15:04:41 +00:00
|
|
|
Collection,
|
|
|
|
CollectionItem,
|
|
|
|
CollectionItemRevision,
|
|
|
|
CollectionMember,
|
|
|
|
CollectionMemberRemoved,
|
|
|
|
CollectionInvitation,
|
|
|
|
Stoken,
|
|
|
|
UserInfo,
|
|
|
|
)
|
2020-02-19 18:53:43 +00:00
|
|
|
from .serializers import (
|
2020-11-14 15:04:41 +00:00
|
|
|
AuthenticationChangePasswordInnerSerializer,
|
|
|
|
AuthenticationSignupSerializer,
|
|
|
|
AuthenticationLoginChallengeSerializer,
|
|
|
|
AuthenticationLoginSerializer,
|
|
|
|
AuthenticationLoginInnerSerializer,
|
|
|
|
CollectionSerializer,
|
|
|
|
CollectionItemSerializer,
|
|
|
|
CollectionItemBulkGetSerializer,
|
|
|
|
CollectionItemDepSerializer,
|
|
|
|
CollectionItemRevisionSerializer,
|
|
|
|
CollectionItemChunkSerializer,
|
|
|
|
CollectionListMultiSerializer,
|
|
|
|
CollectionMemberSerializer,
|
|
|
|
CollectionInvitationSerializer,
|
|
|
|
InvitationAcceptSerializer,
|
|
|
|
UserInfoPubkeySerializer,
|
|
|
|
UserSerializer,
|
|
|
|
)
|
2020-07-13 11:30:18 +00:00
|
|
|
from .utils import get_user_queryset
|
2020-07-30 07:13:24 +00:00
|
|
|
from .exceptions import EtebaseValidationError
|
2020-08-04 10:42:28 +00:00
|
|
|
from .parsers import ChunkUploadParser
|
2020-09-02 09:54:27 +00:00
|
|
|
from .signals import user_signed_up
|
2020-02-19 18:53:43 +00:00
|
|
|
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
|
|
|
2020-06-29 08:30:59 +00:00
|
|
|
def msgpack_encode(content):
|
|
|
|
return msgpack.packb(content, use_bin_type=True)
|
|
|
|
|
|
|
|
|
|
|
|
def msgpack_decode(content):
|
|
|
|
return msgpack.unpackb(content, raw=False)
|
|
|
|
|
|
|
|
|
2020-02-19 18:53:43 +00:00
|
|
|
class BaseViewSet(viewsets.ModelViewSet):
|
|
|
|
authentication_classes = tuple(app_settings.API_AUTHENTICATORS)
|
|
|
|
permission_classes = tuple(app_settings.API_PERMISSIONS)
|
2020-09-13 11:37:48 +00:00
|
|
|
renderer_classes = [JSONRenderer, MessagePackRenderer] + ([BrowsableAPIRenderer] if settings.DEBUG else [])
|
2020-06-29 10:01:40 +00:00
|
|
|
parser_classes = [JSONParser, MessagePackParser, FormParser, MultiPartParser]
|
2020-05-27 09:13:54 +00:00
|
|
|
stoken_id_fields = None
|
2020-02-19 18:53:43 +00:00
|
|
|
|
|
|
|
def get_serializer_class(self):
|
|
|
|
serializer_class = self.serializer_class
|
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
if self.request.method == "PUT":
|
|
|
|
serializer_class = getattr(self, "serializer_update_class", serializer_class)
|
2020-02-19 18:53:43 +00:00
|
|
|
|
|
|
|
return serializer_class
|
|
|
|
|
|
|
|
def get_collection_queryset(self, queryset=Collection.objects):
|
2020-02-26 19:13:33 +00:00
|
|
|
user = self.request.user
|
|
|
|
return queryset.filter(members__user=user)
|
2020-02-19 18:53:43 +00:00
|
|
|
|
2020-06-24 08:30:37 +00:00
|
|
|
def get_stoken_obj_id(self, request):
|
2020-11-14 15:04:41 +00:00
|
|
|
return request.GET.get("stoken", None)
|
2020-06-24 08:30:37 +00:00
|
|
|
|
2020-05-27 07:09:45 +00:00
|
|
|
def get_stoken_obj(self, request):
|
2020-06-24 08:30:37 +00:00
|
|
|
stoken = self.get_stoken_obj_id(request)
|
2020-05-19 11:54:44 +00:00
|
|
|
|
2020-05-27 07:09:45 +00:00
|
|
|
if stoken is not None:
|
2020-07-30 07:13:24 +00:00
|
|
|
try:
|
|
|
|
return Stoken.objects.get(uid=stoken)
|
|
|
|
except Stoken.DoesNotExist:
|
2020-11-14 15:04:41 +00:00
|
|
|
raise EtebaseValidationError("bad_stoken", "Invalid stoken.", status_code=status.HTTP_400_BAD_REQUEST)
|
2020-05-19 11:54:44 +00:00
|
|
|
|
|
|
|
return None
|
|
|
|
|
2020-05-27 07:09:45 +00:00
|
|
|
def filter_by_stoken(self, request, queryset):
|
|
|
|
stoken_rev = self.get_stoken_obj(request)
|
2020-06-24 10:20:07 +00:00
|
|
|
|
|
|
|
aggr_fields = [Coalesce(Max(field), V(0)) for field in self.stoken_id_fields]
|
|
|
|
max_stoken = Greatest(*aggr_fields) if len(aggr_fields) > 1 else aggr_fields[0]
|
2020-11-14 15:04:41 +00:00
|
|
|
queryset = queryset.annotate(max_stoken=max_stoken).order_by("max_stoken")
|
2020-06-24 10:20:07 +00:00
|
|
|
|
2020-05-27 07:09:45 +00:00
|
|
|
if stoken_rev is not None:
|
2020-06-24 10:20:07 +00:00
|
|
|
queryset = queryset.filter(max_stoken__gt=stoken_rev.id)
|
2020-04-16 14:36:06 +00:00
|
|
|
|
2020-05-27 07:09:45 +00:00
|
|
|
return queryset, stoken_rev
|
2020-05-19 14:57:51 +00:00
|
|
|
|
2020-05-27 07:09:45 +00:00
|
|
|
def get_queryset_stoken(self, queryset):
|
2020-06-22 11:20:26 +00:00
|
|
|
maxid = -1
|
2020-06-24 10:20:07 +00:00
|
|
|
for row in queryset:
|
2020-11-14 15:04:41 +00:00
|
|
|
rowmaxid = getattr(row, "max_stoken") or -1
|
2020-06-22 11:20:26 +00:00
|
|
|
maxid = max(maxid, rowmaxid)
|
2020-10-13 10:37:05 +00:00
|
|
|
new_stoken = (maxid >= 0) and Stoken.objects.get(id=maxid)
|
2020-05-19 14:57:51 +00:00
|
|
|
|
2020-10-13 10:37:05 +00:00
|
|
|
return new_stoken or None
|
2020-05-19 14:57:51 +00:00
|
|
|
|
2020-05-27 07:09:45 +00:00
|
|
|
def filter_by_stoken_and_limit(self, request, queryset):
|
2020-11-14 15:04:41 +00:00
|
|
|
limit = int(request.GET.get("limit", 50))
|
2020-05-19 14:57:51 +00:00
|
|
|
|
2020-05-27 07:09:45 +00:00
|
|
|
queryset, stoken_rev = self.filter_by_stoken(request, queryset)
|
2020-05-19 14:57:51 +00:00
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
result = list(queryset[: limit + 1])
|
2020-06-24 11:34:03 +00:00
|
|
|
if len(result) < limit + 1:
|
|
|
|
done = True
|
|
|
|
else:
|
|
|
|
done = False
|
|
|
|
result = result[:-1]
|
|
|
|
|
2020-10-13 10:37:05 +00:00
|
|
|
new_stoken_obj = self.get_queryset_stoken(result) or stoken_rev
|
2020-04-16 14:36:06 +00:00
|
|
|
|
2020-10-13 10:37:05 +00:00
|
|
|
return result, new_stoken_obj, done
|
2020-04-16 14:36:06 +00:00
|
|
|
|
2020-05-27 14:14:38 +00:00
|
|
|
# Change how our list works by default
|
2020-07-13 11:26:39 +00:00
|
|
|
def list(self, request, collection_uid=None, *args, **kwargs):
|
2020-05-27 14:14:38 +00:00
|
|
|
queryset = self.get_queryset()
|
|
|
|
serializer = self.get_serializer(queryset, many=True)
|
|
|
|
|
|
|
|
ret = {
|
2020-11-14 15:04:41 +00:00
|
|
|
"data": serializer.data,
|
|
|
|
"done": True, # we always return all the items, so it's always done
|
2020-05-27 14:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Response(ret)
|
|
|
|
|
2020-02-19 18:53:43 +00:00
|
|
|
|
|
|
|
class CollectionViewSet(BaseViewSet):
|
2020-11-14 15:04:41 +00:00
|
|
|
allowed_methods = ["GET", "POST"]
|
|
|
|
permission_classes = BaseViewSet.permission_classes + (permissions.IsCollectionAdminOrReadOnly,)
|
2020-02-19 18:53:43 +00:00
|
|
|
queryset = Collection.objects.all()
|
|
|
|
serializer_class = CollectionSerializer
|
2020-11-14 15:04:41 +00:00
|
|
|
lookup_field = "main_item__uid"
|
|
|
|
lookup_url_kwarg = "uid"
|
|
|
|
stoken_id_fields = ["items__revisions__stoken__id", "members__stoken__id"]
|
2020-02-19 18:53:43 +00:00
|
|
|
|
2020-04-16 13:37:26 +00:00
|
|
|
def get_queryset(self, queryset=None):
|
|
|
|
if queryset is None:
|
|
|
|
queryset = type(self).queryset
|
2020-02-19 18:53:43 +00:00
|
|
|
return self.get_collection_queryset(queryset)
|
|
|
|
|
2020-04-15 12:23:07 +00:00
|
|
|
def get_serializer_context(self):
|
|
|
|
context = super().get_serializer_context()
|
2020-11-14 15:04:41 +00:00
|
|
|
prefetch = self.request.query_params.get("prefetch", "auto")
|
|
|
|
context.update({"request": self.request, "prefetch": prefetch})
|
2020-04-15 12:23:07 +00:00
|
|
|
return context
|
|
|
|
|
2020-07-13 11:26:39 +00:00
|
|
|
def destroy(self, request, uid=None, *args, **kwargs):
|
2020-02-19 18:53:43 +00:00
|
|
|
# FIXME: implement
|
|
|
|
return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED)
|
|
|
|
|
2020-07-13 11:26:39 +00:00
|
|
|
def partial_update(self, request, uid=None, *args, **kwargs):
|
2020-04-19 12:13:09 +00:00
|
|
|
return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED)
|
|
|
|
|
2020-05-26 15:14:39 +00:00
|
|
|
def update(self, request, *args, **kwargs):
|
2020-06-23 09:55:28 +00:00
|
|
|
return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED)
|
2020-05-26 15:14:39 +00:00
|
|
|
|
2020-02-19 18:53:43 +00:00
|
|
|
def create(self, request, *args, **kwargs):
|
2020-05-27 14:00:33 +00:00
|
|
|
serializer = self.get_serializer(data=request.data)
|
2020-07-10 06:27:34 +00:00
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
serializer.save(owner=self.request.user)
|
2020-02-19 18:53:43 +00:00
|
|
|
|
2020-07-10 06:27:34 +00:00
|
|
|
return Response({}, status=status.HTTP_201_CREATED)
|
2020-02-19 18:53:43 +00:00
|
|
|
|
2020-07-13 11:26:39 +00:00
|
|
|
def list(self, request, *args, **kwargs):
|
2020-02-19 18:53:43 +00:00
|
|
|
queryset = self.get_queryset()
|
2020-10-13 14:13:07 +00:00
|
|
|
return self.list_common(request, queryset, *args, **kwargs)
|
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
@action_decorator(detail=False, methods=["POST"])
|
2020-10-13 14:13:07 +00:00
|
|
|
def list_multi(self, request, *args, **kwargs):
|
|
|
|
serializer = CollectionListMultiSerializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
collection_types = serializer.validated_data["collectionTypes"]
|
2020-10-13 14:13:07 +00:00
|
|
|
|
|
|
|
queryset = self.get_queryset()
|
2020-11-01 09:17:44 +00:00
|
|
|
# FIXME: Remove the isnull part once we attach collection types to all objects ("collection-type-migration")
|
2020-10-15 07:50:07 +00:00
|
|
|
queryset = queryset.filter(
|
2020-11-14 15:04:41 +00:00
|
|
|
Q(members__collectionType__uid__in=collection_types) | Q(members__collectionType__isnull=True)
|
|
|
|
)
|
2020-10-13 14:13:07 +00:00
|
|
|
|
|
|
|
return self.list_common(request, queryset, *args, **kwargs)
|
|
|
|
|
|
|
|
def list_common(self, request, queryset, *args, **kwargs):
|
2020-10-13 10:37:05 +00:00
|
|
|
result, new_stoken_obj, done = self.filter_by_stoken_and_limit(request, queryset)
|
|
|
|
new_stoken = new_stoken_obj and new_stoken_obj.uid
|
2020-04-16 13:37:26 +00:00
|
|
|
|
2020-06-24 11:34:03 +00:00
|
|
|
serializer = self.get_serializer(result, many=True)
|
2020-04-16 13:37:26 +00:00
|
|
|
|
|
|
|
ret = {
|
2020-11-14 15:04:41 +00:00
|
|
|
"data": serializer.data,
|
|
|
|
"stoken": new_stoken,
|
|
|
|
"done": done,
|
2020-04-16 13:37:26 +00:00
|
|
|
}
|
2020-05-27 13:03:16 +00:00
|
|
|
|
|
|
|
stoken_obj = self.get_stoken_obj(request)
|
|
|
|
if stoken_obj is not None:
|
|
|
|
# FIXME: honour limit? (the limit should be combined for data and this because of stoken)
|
2020-10-13 10:29:29 +00:00
|
|
|
remed_qs = CollectionMemberRemoved.objects.filter(user=request.user, stoken__id__gt=stoken_obj.id)
|
2020-11-14 15:04:41 +00:00
|
|
|
if not ret["done"]:
|
2020-10-13 10:29:29 +00:00
|
|
|
# We only filter by the new_stoken if we are not done. This is because if we are done, the new stoken
|
|
|
|
# can point to the most recent collection change rather than most recent removed membership.
|
|
|
|
remed_qs = remed_qs.filter(stoken__id__lte=new_stoken_obj.id)
|
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
remed = remed_qs.values_list("collection__main_item__uid", flat=True)
|
2020-05-27 13:03:16 +00:00
|
|
|
if len(remed) > 0:
|
2020-11-14 15:04:41 +00:00
|
|
|
ret["removedMemberships"] = [{"uid": x} for x in remed]
|
2020-05-27 13:03:16 +00:00
|
|
|
|
2020-05-19 14:57:51 +00:00
|
|
|
return Response(ret)
|
2020-02-19 18:53:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CollectionItemViewSet(BaseViewSet):
|
2020-11-14 15:04:41 +00:00
|
|
|
allowed_methods = ["GET", "POST", "PUT"]
|
|
|
|
permission_classes = BaseViewSet.permission_classes + (permissions.HasWriteAccessOrReadOnly,)
|
2020-02-19 18:53:43 +00:00
|
|
|
queryset = CollectionItem.objects.all()
|
|
|
|
serializer_class = CollectionItemSerializer
|
2020-11-14 15:04:41 +00:00
|
|
|
lookup_field = "uid"
|
|
|
|
stoken_id_fields = ["revisions__stoken__id"]
|
2020-02-19 18:53:43 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
2020-11-14 15:04:41 +00:00
|
|
|
collection_uid = self.kwargs["collection_uid"]
|
2020-02-19 18:53:43 +00:00
|
|
|
try:
|
2020-06-23 09:55:28 +00:00
|
|
|
collection = self.get_collection_queryset(Collection.objects).get(main_item__uid=collection_uid)
|
2020-02-19 18:53:43 +00:00
|
|
|
except Collection.DoesNotExist:
|
|
|
|
raise Http404("Collection does not exist")
|
2020-02-20 20:41:39 +00:00
|
|
|
# XXX Potentially add this for performance: .prefetch_related('revisions__chunks')
|
2020-11-14 15:04:41 +00:00
|
|
|
queryset = type(self).queryset.filter(collection__pk=collection.pk, revisions__current=True)
|
2020-02-19 18:53:43 +00:00
|
|
|
|
|
|
|
return queryset
|
|
|
|
|
2020-04-15 12:23:07 +00:00
|
|
|
def get_serializer_context(self):
|
|
|
|
context = super().get_serializer_context()
|
2020-11-14 15:04:41 +00:00
|
|
|
prefetch = self.request.query_params.get("prefetch", "auto")
|
|
|
|
context.update({"request": self.request, "prefetch": prefetch})
|
2020-04-15 12:23:07 +00:00
|
|
|
return context
|
|
|
|
|
2020-07-13 11:26:39 +00:00
|
|
|
def create(self, request, collection_uid=None, *args, **kwargs):
|
2020-05-27 13:59:24 +00:00
|
|
|
# We create using batch and transaction
|
|
|
|
return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED)
|
2020-02-20 11:56:16 +00:00
|
|
|
|
2020-07-13 11:26:39 +00:00
|
|
|
def destroy(self, request, collection_uid=None, uid=None, *args, **kwargs):
|
2020-02-26 13:53:25 +00:00
|
|
|
# We can't have destroy because we need to get data from the user (in the body) such as hmac.
|
2020-02-20 11:56:16 +00:00
|
|
|
return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED)
|
|
|
|
|
2020-07-13 11:26:39 +00:00
|
|
|
def update(self, request, collection_uid=None, uid=None, *args, **kwargs):
|
2020-05-19 10:00:54 +00:00
|
|
|
return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED)
|
|
|
|
|
2020-07-13 11:26:39 +00:00
|
|
|
def partial_update(self, request, collection_uid=None, uid=None, *args, **kwargs):
|
2020-02-20 11:56:16 +00:00
|
|
|
return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED)
|
|
|
|
|
2020-07-13 11:26:39 +00:00
|
|
|
def list(self, request, collection_uid=None, *args, **kwargs):
|
2020-04-16 13:37:26 +00:00
|
|
|
queryset = self.get_queryset()
|
2020-06-23 10:02:45 +00:00
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
if not self.request.query_params.get("withCollection", False):
|
2020-06-23 10:02:45 +00:00
|
|
|
queryset = queryset.filter(parent__isnull=True)
|
|
|
|
|
2020-10-13 10:37:05 +00:00
|
|
|
result, new_stoken_obj, done = self.filter_by_stoken_and_limit(request, queryset)
|
|
|
|
new_stoken = new_stoken_obj and new_stoken_obj.uid
|
2020-04-16 13:37:26 +00:00
|
|
|
|
2020-06-24 11:34:03 +00:00
|
|
|
serializer = self.get_serializer(result, many=True)
|
2020-04-16 13:37:26 +00:00
|
|
|
|
|
|
|
ret = {
|
2020-11-14 15:04:41 +00:00
|
|
|
"data": serializer.data,
|
|
|
|
"stoken": new_stoken,
|
|
|
|
"done": done,
|
2020-04-16 13:37:26 +00:00
|
|
|
}
|
2020-05-19 14:57:51 +00:00
|
|
|
return Response(ret)
|
2020-04-16 13:37:26 +00:00
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
@action_decorator(detail=True, methods=["GET"])
|
2020-07-13 11:26:39 +00:00
|
|
|
def revision(self, request, collection_uid=None, uid=None, *args, **kwargs):
|
2020-06-23 09:55:28 +00:00
|
|
|
col = get_object_or_404(self.get_collection_queryset(Collection.objects), main_item__uid=collection_uid)
|
2020-06-23 15:25:23 +00:00
|
|
|
item = get_object_or_404(col.items, uid=uid)
|
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
limit = int(request.GET.get("limit", 50))
|
|
|
|
iterator = request.GET.get("iterator", None)
|
2020-06-23 15:25:23 +00:00
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
queryset = item.revisions.order_by("-id")
|
2020-06-23 15:25:23 +00:00
|
|
|
|
|
|
|
if iterator is not None:
|
|
|
|
iterator = get_object_or_404(queryset, uid=iterator)
|
|
|
|
queryset = queryset.filter(id__lt=iterator.id)
|
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
result = list(queryset[: limit + 1])
|
2020-06-24 11:38:29 +00:00
|
|
|
if len(result) < limit + 1:
|
|
|
|
done = True
|
|
|
|
else:
|
|
|
|
done = False
|
|
|
|
result = result[:-1]
|
2020-06-23 15:25:23 +00:00
|
|
|
|
2020-06-24 11:38:29 +00:00
|
|
|
serializer = CollectionItemRevisionSerializer(result, context=self.get_serializer_context(), many=True)
|
2020-06-23 15:25:23 +00:00
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
iterator = serializer.data[-1]["uid"] if len(result) > 0 else None
|
2020-02-20 14:35:20 +00:00
|
|
|
|
2020-04-16 14:38:07 +00:00
|
|
|
ret = {
|
2020-11-14 15:04:41 +00:00
|
|
|
"data": serializer.data,
|
|
|
|
"iterator": iterator,
|
|
|
|
"done": done,
|
2020-04-16 14:38:07 +00:00
|
|
|
}
|
|
|
|
return Response(ret)
|
2020-02-20 14:35:20 +00:00
|
|
|
|
2020-05-19 14:57:51 +00:00
|
|
|
# FIXME: rename to something consistent with what the clients have - maybe list_updates?
|
2020-11-14 15:04:41 +00:00
|
|
|
@action_decorator(detail=False, methods=["POST"])
|
2020-07-13 11:26:39 +00:00
|
|
|
def fetch_updates(self, request, collection_uid=None, *args, **kwargs):
|
2020-02-26 12:20:52 +00:00
|
|
|
queryset = self.get_queryset()
|
|
|
|
|
2020-05-19 14:57:51 +00:00
|
|
|
serializer = CollectionItemBulkGetSerializer(data=request.data, many=True)
|
2020-07-10 06:27:34 +00:00
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
# FIXME: make configurable?
|
|
|
|
item_limit = 200
|
2020-04-16 14:12:51 +00:00
|
|
|
|
2020-07-10 06:27:34 +00:00
|
|
|
if len(serializer.validated_data) > item_limit:
|
2020-11-14 15:04:41 +00:00
|
|
|
content = {"code": "too_many_items", "detail": "Request has too many items. Limit: {}".format(item_limit)}
|
2020-07-10 06:27:34 +00:00
|
|
|
return Response(content, status=status.HTTP_400_BAD_REQUEST)
|
2020-04-16 14:12:51 +00:00
|
|
|
|
2020-07-10 06:27:34 +00:00
|
|
|
queryset, stoken_rev = self.filter_by_stoken(request, queryset)
|
2020-05-19 14:57:51 +00:00
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
uids, etags = zip(*[(item["uid"], item.get("etag")) for item in serializer.validated_data])
|
2020-07-10 06:27:34 +00:00
|
|
|
revs = CollectionItemRevision.objects.filter(uid__in=etags, current=True)
|
|
|
|
queryset = queryset.filter(uid__in=uids).exclude(revisions__in=revs)
|
2020-05-19 14:57:51 +00:00
|
|
|
|
2020-10-13 10:37:05 +00:00
|
|
|
new_stoken_obj = self.get_queryset_stoken(queryset)
|
|
|
|
new_stoken = new_stoken_obj and new_stoken_obj.uid
|
2020-11-14 15:04:41 +00:00
|
|
|
stoken = stoken_rev and getattr(stoken_rev, "uid", None)
|
2020-07-10 06:27:34 +00:00
|
|
|
new_stoken = new_stoken or stoken
|
2020-05-19 14:57:51 +00:00
|
|
|
|
2020-07-10 06:27:34 +00:00
|
|
|
serializer = self.get_serializer(queryset, many=True)
|
2020-05-19 14:57:51 +00:00
|
|
|
|
2020-07-10 06:27:34 +00:00
|
|
|
ret = {
|
2020-11-14 15:04:41 +00:00
|
|
|
"data": serializer.data,
|
|
|
|
"stoken": new_stoken,
|
|
|
|
"done": True, # we always return all the items, so it's always done
|
2020-07-10 06:27:34 +00:00
|
|
|
}
|
|
|
|
return Response(ret)
|
2020-04-16 14:12:51 +00:00
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
@action_decorator(detail=False, methods=["POST"])
|
2020-07-13 11:26:39 +00:00
|
|
|
def batch(self, request, collection_uid=None, *args, **kwargs):
|
2020-06-11 08:17:01 +00:00
|
|
|
return self.transaction(request, collection_uid, validate_etag=False)
|
2020-05-19 10:10:50 +00:00
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
@action_decorator(detail=False, methods=["POST"])
|
2020-07-13 11:26:39 +00:00
|
|
|
def transaction(self, request, collection_uid=None, validate_etag=True, *args, **kwargs):
|
2020-11-14 15:04:41 +00:00
|
|
|
stoken = request.GET.get("stoken", None)
|
2020-06-11 08:17:01 +00:00
|
|
|
with transaction.atomic(): # We need this for locking on the collection object
|
|
|
|
collection_object = get_object_or_404(
|
|
|
|
self.get_collection_queryset(Collection.objects).select_for_update(), # Lock writes on the collection
|
2020-11-14 15:04:41 +00:00
|
|
|
main_item__uid=collection_uid,
|
|
|
|
)
|
2020-04-19 14:32:40 +00:00
|
|
|
|
2020-06-11 08:17:01 +00:00
|
|
|
if stoken is not None and stoken != collection_object.stoken:
|
2020-11-14 15:04:41 +00:00
|
|
|
content = {"code": "stale_stoken", "detail": "Stoken is too old"}
|
2020-07-30 07:13:24 +00:00
|
|
|
return Response(content, status=status.HTTP_409_CONFLICT)
|
2020-04-19 14:32:40 +00:00
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
items = request.data.get("items")
|
|
|
|
deps = request.data.get("deps", None)
|
2020-06-11 08:17:01 +00:00
|
|
|
# FIXME: It should just be one serializer
|
|
|
|
context = self.get_serializer_context()
|
2020-11-14 15:04:41 +00:00
|
|
|
context.update({"validate_etag": validate_etag})
|
2020-06-11 08:17:01 +00:00
|
|
|
serializer = self.get_serializer_class()(data=items, context=context, many=True)
|
|
|
|
deps_serializer = CollectionItemDepSerializer(data=deps, context=context, many=True)
|
|
|
|
|
|
|
|
ser_valid = serializer.is_valid()
|
2020-11-14 15:04:41 +00:00
|
|
|
deps_ser_valid = deps is None or deps_serializer.is_valid()
|
2020-06-11 08:17:01 +00:00
|
|
|
if ser_valid and deps_ser_valid:
|
2020-07-10 06:27:34 +00:00
|
|
|
items = serializer.save(collection=collection_object)
|
2020-06-11 08:17:01 +00:00
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
ret = {}
|
2020-06-11 08:17:01 +00:00
|
|
|
return Response(ret, status=status.HTTP_200_OK)
|
|
|
|
|
|
|
|
return Response(
|
2020-11-14 15:04:41 +00:00
|
|
|
{"items": serializer.errors, "deps": deps_serializer.errors if deps is not None else [],},
|
|
|
|
status=status.HTTP_409_CONFLICT,
|
|
|
|
)
|
2020-04-19 14:32:40 +00:00
|
|
|
|
2020-02-20 11:56:16 +00:00
|
|
|
|
|
|
|
class CollectionItemChunkViewSet(viewsets.ViewSet):
|
2020-11-14 15:04:41 +00:00
|
|
|
allowed_methods = ["GET", "PUT"]
|
2020-02-20 11:56:16 +00:00
|
|
|
authentication_classes = BaseViewSet.authentication_classes
|
|
|
|
permission_classes = BaseViewSet.permission_classes
|
2020-06-29 10:01:40 +00:00
|
|
|
renderer_classes = BaseViewSet.renderer_classes
|
2020-11-14 15:04:41 +00:00
|
|
|
parser_classes = (ChunkUploadParser,)
|
2020-02-20 12:42:35 +00:00
|
|
|
serializer_class = CollectionItemChunkSerializer
|
2020-11-14 15:04:41 +00:00
|
|
|
lookup_field = "uid"
|
2020-02-20 11:56:16 +00:00
|
|
|
|
2020-05-27 14:00:33 +00:00
|
|
|
def get_serializer_class(self):
|
|
|
|
return self.serializer_class
|
|
|
|
|
2020-02-20 15:33:34 +00:00
|
|
|
def get_collection_queryset(self, queryset=Collection.objects):
|
2020-02-26 19:13:33 +00:00
|
|
|
user = self.request.user
|
|
|
|
return queryset.filter(members__user=user)
|
2020-02-20 15:33:34 +00:00
|
|
|
|
2020-08-04 10:42:28 +00:00
|
|
|
def update(self, request, *args, collection_uid=None, collection_item_uid=None, uid=None, **kwargs):
|
2020-06-23 09:55:28 +00:00
|
|
|
col = get_object_or_404(self.get_collection_queryset(), main_item__uid=collection_uid)
|
2020-08-04 12:19:45 +00:00
|
|
|
# IGNORED FOR NOW: col_it = get_object_or_404(col.items, uid=collection_item_uid)
|
2020-02-20 11:56:16 +00:00
|
|
|
|
2020-08-04 10:42:28 +00:00
|
|
|
data = {
|
|
|
|
"uid": uid,
|
|
|
|
"chunkFile": request.data["file"],
|
|
|
|
}
|
|
|
|
|
|
|
|
serializer = self.get_serializer_class()(data=data)
|
2020-07-10 06:27:34 +00:00
|
|
|
serializer.is_valid(raise_exception=True)
|
2020-08-04 10:42:28 +00:00
|
|
|
try:
|
2020-08-04 12:19:45 +00:00
|
|
|
serializer.save(collection=col)
|
2020-08-04 10:42:28 +00:00
|
|
|
except IntegrityError:
|
|
|
|
return Response(
|
2020-11-14 15:04:41 +00:00
|
|
|
{"code": "chunk_exists", "detail": "Chunk already exists."}, status=status.HTTP_409_CONFLICT
|
2020-08-04 10:42:28 +00:00
|
|
|
)
|
2020-02-19 18:53:43 +00:00
|
|
|
|
2020-07-10 06:27:34 +00:00
|
|
|
return Response({}, status=status.HTTP_201_CREATED)
|
2020-02-20 13:39:52 +00:00
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
@action_decorator(detail=True, methods=["GET"])
|
2020-07-13 11:26:39 +00:00
|
|
|
def download(self, request, collection_uid=None, collection_item_uid=None, uid=None, *args, **kwargs):
|
2020-02-20 13:39:52 +00:00
|
|
|
import os
|
|
|
|
from django.views.static import serve
|
|
|
|
|
2020-06-23 09:55:28 +00:00
|
|
|
col = get_object_or_404(self.get_collection_queryset(), main_item__uid=collection_uid)
|
2020-08-04 12:19:45 +00:00
|
|
|
# IGNORED FOR NOW: col_it = get_object_or_404(col.items, uid=collection_item_uid)
|
|
|
|
chunk = get_object_or_404(col.chunks, uid=uid)
|
2020-02-20 13:39:52 +00:00
|
|
|
|
|
|
|
filename = chunk.chunkFile.path
|
|
|
|
dirname = os.path.dirname(filename)
|
|
|
|
basename = os.path.basename(filename)
|
|
|
|
|
|
|
|
# FIXME: DO NOT USE! Use django-send file or etc instead.
|
|
|
|
return serve(request, basename, dirname)
|
2020-04-14 13:21:51 +00:00
|
|
|
|
|
|
|
|
2020-05-20 10:48:46 +00:00
|
|
|
class CollectionMemberViewSet(BaseViewSet):
|
2020-11-14 15:04:41 +00:00
|
|
|
allowed_methods = ["GET", "PUT", "DELETE"]
|
2020-05-27 13:03:16 +00:00
|
|
|
our_base_permission_classes = BaseViewSet.permission_classes
|
2020-11-14 15:04:41 +00:00
|
|
|
permission_classes = our_base_permission_classes + (permissions.IsCollectionAdmin,)
|
2020-05-20 10:48:46 +00:00
|
|
|
queryset = CollectionMember.objects.all()
|
|
|
|
serializer_class = CollectionMemberSerializer
|
2020-11-14 15:04:41 +00:00
|
|
|
lookup_field = f"user__{User.USERNAME_FIELD}__iexact"
|
|
|
|
lookup_url_kwarg = "username"
|
|
|
|
stoken_id_fields = ["stoken__id"]
|
2020-05-20 10:48:46 +00:00
|
|
|
|
|
|
|
# FIXME: need to make sure that there's always an admin, and maybe also don't let an owner remove adm access
|
|
|
|
# (if we want to transfer, we need to do that specifically)
|
|
|
|
|
|
|
|
def get_queryset(self, queryset=None):
|
2020-11-14 15:04:41 +00:00
|
|
|
collection_uid = self.kwargs["collection_uid"]
|
2020-05-20 10:48:46 +00:00
|
|
|
try:
|
2020-06-23 09:55:28 +00:00
|
|
|
collection = self.get_collection_queryset(Collection.objects).get(main_item__uid=collection_uid)
|
2020-05-20 10:48:46 +00:00
|
|
|
except Collection.DoesNotExist:
|
2020-11-14 15:04:41 +00:00
|
|
|
raise Http404("Collection does not exist")
|
2020-05-20 10:48:46 +00:00
|
|
|
|
|
|
|
if queryset is None:
|
|
|
|
queryset = type(self).queryset
|
|
|
|
|
|
|
|
return queryset.filter(collection=collection)
|
|
|
|
|
2020-06-22 14:51:56 +00:00
|
|
|
# We override this method because we expect the stoken to be called iterator
|
2020-06-24 08:30:37 +00:00
|
|
|
def get_stoken_obj_id(self, request):
|
2020-11-14 15:04:41 +00:00
|
|
|
return request.GET.get("iterator", None)
|
2020-06-22 14:51:56 +00:00
|
|
|
|
2020-07-13 11:26:39 +00:00
|
|
|
def list(self, request, collection_uid=None, *args, **kwargs):
|
2020-11-14 15:04:41 +00:00
|
|
|
queryset = self.get_queryset().order_by("id")
|
2020-10-13 10:37:05 +00:00
|
|
|
result, new_stoken_obj, done = self.filter_by_stoken_and_limit(request, queryset)
|
|
|
|
new_stoken = new_stoken_obj and new_stoken_obj.uid
|
2020-06-24 11:34:03 +00:00
|
|
|
serializer = self.get_serializer(result, many=True)
|
2020-05-27 13:51:12 +00:00
|
|
|
|
|
|
|
ret = {
|
2020-11-14 15:04:41 +00:00
|
|
|
"data": serializer.data,
|
|
|
|
"iterator": new_stoken, # Here we call it an iterator, it's only stoken for collection/items
|
|
|
|
"done": done,
|
2020-05-27 13:51:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Response(ret)
|
|
|
|
|
2020-07-13 11:26:39 +00:00
|
|
|
def create(self, request, *args, **kwargs):
|
2020-05-20 10:48:46 +00:00
|
|
|
return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED)
|
|
|
|
|
2020-05-27 13:03:16 +00:00
|
|
|
# FIXME: block leaving if we are the last admins - should be deleted / assigned in this case depending if there
|
|
|
|
# are other memebers.
|
|
|
|
def perform_destroy(self, instance):
|
|
|
|
instance.revoke()
|
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
@action_decorator(detail=False, methods=["POST"], permission_classes=our_base_permission_classes)
|
2020-07-13 11:26:39 +00:00
|
|
|
def leave(self, request, collection_uid=None, *args, **kwargs):
|
2020-11-14 15:04:41 +00:00
|
|
|
collection_uid = self.kwargs["collection_uid"]
|
2020-06-23 09:55:28 +00:00
|
|
|
col = get_object_or_404(self.get_collection_queryset(Collection.objects), main_item__uid=collection_uid)
|
2020-05-27 13:03:16 +00:00
|
|
|
|
|
|
|
member = col.members.get(user=request.user)
|
|
|
|
self.perform_destroy(member)
|
|
|
|
|
|
|
|
return Response({})
|
|
|
|
|
2020-05-20 10:48:46 +00:00
|
|
|
|
2020-06-22 15:26:32 +00:00
|
|
|
class InvitationBaseViewSet(BaseViewSet):
|
2020-05-20 11:30:09 +00:00
|
|
|
queryset = CollectionInvitation.objects.all()
|
|
|
|
serializer_class = CollectionInvitationSerializer
|
2020-11-14 15:04:41 +00:00
|
|
|
lookup_field = "uid"
|
|
|
|
lookup_url_kwarg = "invitation_uid"
|
2020-05-20 11:30:09 +00:00
|
|
|
|
2020-07-13 11:26:39 +00:00
|
|
|
def list(self, request, collection_uid=None, *args, **kwargs):
|
2020-11-14 15:04:41 +00:00
|
|
|
limit = int(request.GET.get("limit", 50))
|
|
|
|
iterator = request.GET.get("iterator", None)
|
2020-06-22 15:26:32 +00:00
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
queryset = self.get_queryset().order_by("id")
|
2020-06-22 15:26:32 +00:00
|
|
|
|
|
|
|
if iterator is not None:
|
|
|
|
iterator = get_object_or_404(queryset, uid=iterator)
|
|
|
|
queryset = queryset.filter(id__gt=iterator.id)
|
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
result = list(queryset[: limit + 1])
|
2020-06-24 11:38:29 +00:00
|
|
|
if len(result) < limit + 1:
|
|
|
|
done = True
|
|
|
|
else:
|
|
|
|
done = False
|
|
|
|
result = result[:-1]
|
2020-06-22 15:26:32 +00:00
|
|
|
|
2020-06-24 11:38:29 +00:00
|
|
|
serializer = self.get_serializer(result, many=True)
|
2020-06-22 15:26:32 +00:00
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
iterator = serializer.data[-1]["uid"] if len(result) > 0 else None
|
2020-06-22 15:26:32 +00:00
|
|
|
|
|
|
|
ret = {
|
2020-11-14 15:04:41 +00:00
|
|
|
"data": serializer.data,
|
|
|
|
"iterator": iterator,
|
|
|
|
"done": done,
|
2020-06-22 15:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Response(ret)
|
|
|
|
|
|
|
|
|
|
|
|
class InvitationOutgoingViewSet(InvitationBaseViewSet):
|
2020-11-14 15:04:41 +00:00
|
|
|
allowed_methods = ["GET", "POST", "PUT", "DELETE"]
|
2020-06-22 15:26:32 +00:00
|
|
|
|
2020-05-20 11:30:09 +00:00
|
|
|
def get_queryset(self, queryset=None):
|
|
|
|
if queryset is None:
|
|
|
|
queryset = type(self).queryset
|
|
|
|
|
2020-05-24 15:19:22 +00:00
|
|
|
return queryset.filter(fromMember__user=self.request.user)
|
|
|
|
|
|
|
|
def create(self, request, *args, **kwargs):
|
2020-05-27 14:00:33 +00:00
|
|
|
serializer = self.get_serializer(data=request.data)
|
2020-07-10 06:27:34 +00:00
|
|
|
serializer.is_valid(raise_exception=True)
|
2020-11-14 15:04:41 +00:00
|
|
|
collection_uid = serializer.validated_data.get("collection", {}).get("uid")
|
2020-05-24 15:19:22 +00:00
|
|
|
|
2020-07-10 06:27:34 +00:00
|
|
|
try:
|
|
|
|
collection = self.get_collection_queryset(Collection.objects).get(main_item__uid=collection_uid)
|
|
|
|
except Collection.DoesNotExist:
|
2020-11-14 15:04:41 +00:00
|
|
|
raise Http404("Collection does not exist")
|
2020-05-24 15:19:22 +00:00
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
if request.user == serializer.validated_data.get("user"):
|
|
|
|
content = {"code": "self_invite", "detail": "Inviting yourself is invalid"}
|
2020-08-28 10:55:15 +00:00
|
|
|
return Response(content, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
2020-07-10 06:27:34 +00:00
|
|
|
if not permissions.is_collection_admin(collection, request.user):
|
2020-11-14 15:04:41 +00:00
|
|
|
raise PermissionDenied(
|
|
|
|
{"code": "admin_access_required", "detail": "User is not an admin of this collection"}
|
|
|
|
)
|
2020-05-24 15:19:22 +00:00
|
|
|
|
2020-07-10 06:27:34 +00:00
|
|
|
serializer.save(collection=collection)
|
2020-05-24 15:19:22 +00:00
|
|
|
|
2020-07-10 06:27:34 +00:00
|
|
|
return Response({}, status=status.HTTP_201_CREATED)
|
2020-05-20 11:30:09 +00:00
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
@action_decorator(detail=False, allowed_methods=["GET"], methods=["GET"])
|
2020-07-13 11:26:39 +00:00
|
|
|
def fetch_user_profile(self, request, *args, **kwargs):
|
2020-11-14 15:04:41 +00:00
|
|
|
username = request.GET.get("username")
|
2020-07-13 12:26:05 +00:00
|
|
|
kwargs = {User.USERNAME_FIELD: username.lower()}
|
2020-07-13 11:30:18 +00:00
|
|
|
user = get_object_or_404(get_user_queryset(User.objects.all(), self), **kwargs)
|
|
|
|
user_info = get_object_or_404(UserInfo.objects.all(), owner=user)
|
2020-05-24 12:20:55 +00:00
|
|
|
serializer = UserInfoPubkeySerializer(user_info)
|
|
|
|
return Response(serializer.data)
|
|
|
|
|
2020-05-20 11:30:09 +00:00
|
|
|
|
2020-06-22 15:26:32 +00:00
|
|
|
class InvitationIncomingViewSet(InvitationBaseViewSet):
|
2020-11-14 15:04:41 +00:00
|
|
|
allowed_methods = ["GET", "DELETE"]
|
2020-05-20 12:15:24 +00:00
|
|
|
|
|
|
|
def get_queryset(self, queryset=None):
|
|
|
|
if queryset is None:
|
|
|
|
queryset = type(self).queryset
|
|
|
|
|
|
|
|
return queryset.filter(user=self.request.user)
|
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
@action_decorator(detail=True, allowed_methods=["POST"], methods=["POST"])
|
2020-07-13 11:26:39 +00:00
|
|
|
def accept(self, request, invitation_uid=None, *args, **kwargs):
|
2020-05-20 12:15:24 +00:00
|
|
|
invitation = get_object_or_404(self.get_queryset(), uid=invitation_uid)
|
|
|
|
context = self.get_serializer_context()
|
2020-11-14 15:04:41 +00:00
|
|
|
context.update({"invitation": invitation})
|
2020-05-20 12:15:24 +00:00
|
|
|
|
|
|
|
serializer = InvitationAcceptSerializer(data=request.data, context=context)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
serializer.save()
|
|
|
|
return Response(status=status.HTTP_201_CREATED)
|
|
|
|
|
|
|
|
|
2020-05-14 10:43:49 +00:00
|
|
|
class AuthenticationViewSet(viewsets.ViewSet):
|
2020-11-14 15:04:41 +00:00
|
|
|
allowed_methods = ["POST"]
|
2020-06-03 11:21:52 +00:00
|
|
|
authentication_classes = BaseViewSet.authentication_classes
|
2020-06-29 10:01:40 +00:00
|
|
|
renderer_classes = BaseViewSet.renderer_classes
|
|
|
|
parser_classes = BaseViewSet.parser_classes
|
2020-05-14 10:43:49 +00:00
|
|
|
|
|
|
|
def get_encryption_key(self, salt):
|
|
|
|
key = nacl.hash.blake2b(settings.SECRET_KEY.encode(), encoder=nacl.encoding.RawEncoder)
|
2020-11-14 15:04:41 +00:00
|
|
|
return nacl.hash.blake2b(
|
|
|
|
b"",
|
|
|
|
key=key,
|
|
|
|
salt=salt[: nacl.hash.BLAKE2B_SALTBYTES],
|
|
|
|
person=b"etebase-auth",
|
|
|
|
encoder=nacl.encoding.RawEncoder,
|
|
|
|
)
|
2020-05-14 10:43:49 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
2020-07-13 11:30:18 +00:00
|
|
|
return get_user_queryset(User.objects.all(), self)
|
2020-05-14 10:43:49 +00:00
|
|
|
|
2020-07-13 13:03:34 +00:00
|
|
|
def get_serializer_context(self):
|
2020-11-14 15:04:41 +00:00
|
|
|
return {"request": self.request, "format": self.format_kwarg, "view": self}
|
2020-07-13 13:03:34 +00:00
|
|
|
|
2020-05-14 14:19:18 +00:00
|
|
|
def login_response_data(self, user):
|
|
|
|
return {
|
2020-11-14 15:04:41 +00:00
|
|
|
"token": AuthToken.objects.create(user=user).key,
|
|
|
|
"user": UserSerializer(user).data,
|
2020-05-14 14:19:18 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 11:26:39 +00:00
|
|
|
def list(self, request, *args, **kwargs):
|
2020-05-14 10:43:49 +00:00
|
|
|
return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED)
|
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
@action_decorator(detail=False, methods=["POST"])
|
2020-07-13 11:26:39 +00:00
|
|
|
def signup(self, request, *args, **kwargs):
|
2020-07-13 13:03:34 +00:00
|
|
|
serializer = AuthenticationSignupSerializer(data=request.data, context=self.get_serializer_context())
|
2020-05-31 13:06:59 +00:00
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
user = serializer.save()
|
2020-05-14 10:43:49 +00:00
|
|
|
|
2020-09-02 09:54:27 +00:00
|
|
|
user_signed_up.send(sender=user.__class__, request=request, user=user)
|
|
|
|
|
2020-05-31 13:06:59 +00:00
|
|
|
data = self.login_response_data(user)
|
|
|
|
return Response(data, status=status.HTTP_201_CREATED)
|
2020-05-14 10:43:49 +00:00
|
|
|
|
2020-05-31 13:13:43 +00:00
|
|
|
def get_login_user(self, username):
|
2020-11-14 15:04:41 +00:00
|
|
|
kwargs = {User.USERNAME_FIELD + "__iexact": username.lower()}
|
2020-09-09 14:07:32 +00:00
|
|
|
try:
|
2020-09-10 10:31:54 +00:00
|
|
|
user = self.get_queryset().get(**kwargs)
|
2020-11-14 15:04:41 +00:00
|
|
|
if not hasattr(user, "userinfo"):
|
|
|
|
raise AuthenticationFailed({"code": "user_not_init", "detail": "User not properly init"})
|
2020-09-10 10:31:54 +00:00
|
|
|
return user
|
2020-09-09 14:07:32 +00:00
|
|
|
except User.DoesNotExist:
|
2020-11-14 15:04:41 +00:00
|
|
|
raise AuthenticationFailed({"code": "user_not_found", "detail": "User not found"})
|
2020-09-09 14:07:32 +00:00
|
|
|
|
2020-06-17 11:38:02 +00:00
|
|
|
def validate_login_request(self, request, validated_data, response_raw, signature, expected_action):
|
|
|
|
from datetime import datetime
|
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
username = validated_data.get("username")
|
2020-06-17 11:38:02 +00:00
|
|
|
user = self.get_login_user(username)
|
2020-11-14 15:04:41 +00:00
|
|
|
host = validated_data["host"]
|
|
|
|
challenge = validated_data["challenge"]
|
|
|
|
action = validated_data["action"]
|
2020-06-17 11:38:02 +00:00
|
|
|
|
|
|
|
salt = bytes(user.userinfo.salt)
|
|
|
|
enc_key = self.get_encryption_key(salt)
|
|
|
|
box = nacl.secret.SecretBox(enc_key)
|
|
|
|
|
2020-06-29 08:30:59 +00:00
|
|
|
challenge_data = msgpack_decode(box.decrypt(challenge))
|
2020-06-17 11:38:02 +00:00
|
|
|
now = int(datetime.now().timestamp())
|
|
|
|
if action != expected_action:
|
2020-11-14 15:04:41 +00:00
|
|
|
content = {"code": "wrong_action", "detail": 'Expected "{}" but got something else'.format(expected_action)}
|
2020-06-17 11:38:02 +00:00
|
|
|
return Response(content, status=status.HTTP_400_BAD_REQUEST)
|
2020-11-14 15:04:41 +00:00
|
|
|
elif now - challenge_data["timestamp"] > app_settings.CHALLENGE_VALID_SECONDS:
|
|
|
|
content = {"code": "challenge_expired", "detail": "Login challange has expired"}
|
2020-06-17 11:38:02 +00:00
|
|
|
return Response(content, status=status.HTTP_400_BAD_REQUEST)
|
2020-11-14 15:04:41 +00:00
|
|
|
elif challenge_data["userId"] != user.id:
|
|
|
|
content = {"code": "wrong_user", "detail": "This challenge is for the wrong user"}
|
2020-06-17 11:38:02 +00:00
|
|
|
return Response(content, status=status.HTTP_400_BAD_REQUEST)
|
2020-11-14 15:04:41 +00:00
|
|
|
elif not settings.DEBUG and host.split(":", 1)[0] != request.get_host():
|
2020-06-17 11:38:02 +00:00
|
|
|
detail = 'Found wrong host name. Got: "{}" expected: "{}"'.format(host, request.get_host())
|
2020-11-14 15:04:41 +00:00
|
|
|
content = {"code": "wrong_host", "detail": detail}
|
2020-06-17 11:38:02 +00:00
|
|
|
return Response(content, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
|
|
|
verify_key = nacl.signing.VerifyKey(bytes(user.userinfo.loginPubkey), encoder=nacl.encoding.RawEncoder)
|
|
|
|
|
|
|
|
try:
|
|
|
|
verify_key.verify(response_raw, signature)
|
|
|
|
except nacl.exceptions.BadSignatureError:
|
2020-11-14 15:04:41 +00:00
|
|
|
return Response(
|
|
|
|
{"code": "login_bad_signature", "detail": "Wrong password for user."},
|
|
|
|
status=status.HTTP_401_UNAUTHORIZED,
|
|
|
|
)
|
2020-06-17 11:38:02 +00:00
|
|
|
|
|
|
|
return None
|
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
@action_decorator(detail=False, methods=["GET"])
|
2020-08-18 09:02:56 +00:00
|
|
|
def is_etebase(self, request, *args, **kwargs):
|
|
|
|
return Response({}, status=status.HTTP_200_OK)
|
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
@action_decorator(detail=False, methods=["POST"])
|
2020-07-13 11:26:39 +00:00
|
|
|
def login_challenge(self, request, *args, **kwargs):
|
2020-05-14 10:43:49 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
serializer = AuthenticationLoginChallengeSerializer(data=request.data)
|
2020-07-10 06:27:34 +00:00
|
|
|
serializer.is_valid(raise_exception=True)
|
2020-11-14 15:04:41 +00:00
|
|
|
username = serializer.validated_data.get("username")
|
2020-07-10 06:27:34 +00:00
|
|
|
user = self.get_login_user(username)
|
|
|
|
|
|
|
|
salt = bytes(user.userinfo.salt)
|
|
|
|
enc_key = self.get_encryption_key(salt)
|
|
|
|
box = nacl.secret.SecretBox(enc_key)
|
|
|
|
|
|
|
|
challenge_data = {
|
|
|
|
"timestamp": int(datetime.now().timestamp()),
|
|
|
|
"userId": user.id,
|
|
|
|
}
|
|
|
|
challenge = box.encrypt(msgpack_encode(challenge_data), encoder=nacl.encoding.RawEncoder)
|
|
|
|
|
|
|
|
ret = {
|
|
|
|
"salt": salt,
|
|
|
|
"challenge": challenge,
|
|
|
|
"version": user.userinfo.version,
|
|
|
|
}
|
|
|
|
return Response(ret, status=status.HTTP_200_OK)
|
2020-05-14 10:43:49 +00:00
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
@action_decorator(detail=False, methods=["POST"])
|
2020-07-13 11:26:39 +00:00
|
|
|
def login(self, request, *args, **kwargs):
|
2020-05-14 12:42:42 +00:00
|
|
|
outer_serializer = AuthenticationLoginSerializer(data=request.data)
|
2020-06-17 11:38:02 +00:00
|
|
|
outer_serializer.is_valid(raise_exception=True)
|
2020-05-14 12:42:42 +00:00
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
response_raw = outer_serializer.validated_data["response"]
|
2020-06-29 08:30:59 +00:00
|
|
|
response = msgpack_decode(response_raw)
|
2020-11-14 15:04:41 +00:00
|
|
|
signature = outer_serializer.validated_data["signature"]
|
2020-05-31 13:28:54 +00:00
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
context = {"host": request.get_host()}
|
2020-06-29 08:30:59 +00:00
|
|
|
serializer = AuthenticationLoginInnerSerializer(data=response, context=context)
|
2020-06-17 11:38:02 +00:00
|
|
|
serializer.is_valid(raise_exception=True)
|
2020-05-14 12:42:42 +00:00
|
|
|
|
2020-06-17 11:38:02 +00:00
|
|
|
bad_login_response = self.validate_login_request(
|
2020-11-14 15:04:41 +00:00
|
|
|
request, serializer.validated_data, response_raw, signature, "login"
|
|
|
|
)
|
2020-06-17 11:38:02 +00:00
|
|
|
if bad_login_response is not None:
|
|
|
|
return bad_login_response
|
2020-05-31 15:26:21 +00:00
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
username = serializer.validated_data.get("username")
|
2020-06-17 11:38:02 +00:00
|
|
|
user = self.get_login_user(username)
|
2020-05-31 15:26:21 +00:00
|
|
|
|
2020-06-17 11:38:02 +00:00
|
|
|
data = self.login_response_data(user)
|
2020-05-14 10:43:49 +00:00
|
|
|
|
2020-06-17 11:38:02 +00:00
|
|
|
user_logged_in.send(sender=user.__class__, request=request, user=user)
|
|
|
|
|
|
|
|
return Response(data, status=status.HTTP_200_OK)
|
2020-05-14 10:43:49 +00:00
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
@action_decorator(detail=False, methods=["POST"], permission_classes=[IsAuthenticated])
|
2020-07-13 11:26:39 +00:00
|
|
|
def logout(self, request, *args, **kwargs):
|
2020-06-03 13:19:07 +00:00
|
|
|
request.auth.delete()
|
2020-06-03 12:49:38 +00:00
|
|
|
user_logged_out.send(sender=request.user.__class__, request=request, user=request.user)
|
|
|
|
return Response(status=status.HTTP_204_NO_CONTENT)
|
2020-05-14 10:43:49 +00:00
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
@action_decorator(detail=False, methods=["POST"], permission_classes=BaseViewSet.permission_classes)
|
2020-07-13 11:26:39 +00:00
|
|
|
def change_password(self, request, *args, **kwargs):
|
2020-06-17 11:38:02 +00:00
|
|
|
outer_serializer = AuthenticationLoginSerializer(data=request.data)
|
|
|
|
outer_serializer.is_valid(raise_exception=True)
|
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
response_raw = outer_serializer.validated_data["response"]
|
2020-06-29 08:30:59 +00:00
|
|
|
response = msgpack_decode(response_raw)
|
2020-11-14 15:04:41 +00:00
|
|
|
signature = outer_serializer.validated_data["signature"]
|
2020-06-17 11:38:02 +00:00
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
context = {"host": request.get_host()}
|
2020-06-29 08:30:59 +00:00
|
|
|
serializer = AuthenticationChangePasswordInnerSerializer(request.user.userinfo, data=response, context=context)
|
2020-06-03 11:35:44 +00:00
|
|
|
serializer.is_valid(raise_exception=True)
|
2020-06-17 11:38:02 +00:00
|
|
|
|
|
|
|
bad_login_response = self.validate_login_request(
|
2020-11-14 15:04:41 +00:00
|
|
|
request, serializer.validated_data, response_raw, signature, "changePassword"
|
|
|
|
)
|
2020-06-17 11:38:02 +00:00
|
|
|
if bad_login_response is not None:
|
|
|
|
return bad_login_response
|
|
|
|
|
2020-06-03 11:35:44 +00:00
|
|
|
serializer.save()
|
|
|
|
|
2020-06-17 11:38:02 +00:00
|
|
|
return Response({}, status=status.HTTP_200_OK)
|
2020-06-03 11:35:44 +00:00
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
@action_decorator(detail=False, methods=["POST"], permission_classes=[IsAuthenticated])
|
2020-10-08 18:03:54 +00:00
|
|
|
def dashboard_url(self, request, *args, **kwargs):
|
|
|
|
get_dashboard_url = app_settings.DASHBOARD_URL_FUNC
|
|
|
|
if get_dashboard_url is None:
|
2020-11-14 15:04:41 +00:00
|
|
|
raise EtebaseValidationError(
|
|
|
|
"not_supported", "This server doesn't have a user dashboard.", status_code=status.HTTP_400_BAD_REQUEST
|
|
|
|
)
|
2020-10-08 18:03:54 +00:00
|
|
|
|
|
|
|
ret = {
|
2020-11-14 15:04:41 +00:00
|
|
|
"url": get_dashboard_url(request, *args, **kwargs),
|
2020-10-08 18:03:54 +00:00
|
|
|
}
|
|
|
|
return Response(ret)
|
|
|
|
|
2020-05-14 10:43:49 +00:00
|
|
|
|
2020-05-15 09:44:25 +00:00
|
|
|
class TestAuthenticationViewSet(viewsets.ViewSet):
|
2020-11-14 15:04:41 +00:00
|
|
|
allowed_methods = ["POST"]
|
2020-06-29 10:01:40 +00:00
|
|
|
renderer_classes = BaseViewSet.renderer_classes
|
|
|
|
parser_classes = BaseViewSet.parser_classes
|
2020-04-14 13:21:51 +00:00
|
|
|
|
2020-07-13 13:03:34 +00:00
|
|
|
def get_serializer_context(self):
|
2020-11-14 15:04:41 +00:00
|
|
|
return {"request": self.request, "format": self.format_kwarg, "view": self}
|
2020-07-13 13:03:34 +00:00
|
|
|
|
2020-07-13 11:26:39 +00:00
|
|
|
def list(self, request, *args, **kwargs):
|
2020-05-15 09:44:25 +00:00
|
|
|
return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED)
|
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
@action_decorator(detail=False, methods=["POST"])
|
2020-05-15 09:44:25 +00:00
|
|
|
def reset(self, request, *args, **kwargs):
|
2020-04-14 13:21:51 +00:00
|
|
|
# Only run when in DEBUG mode! It's only used for tests
|
|
|
|
if not settings.DEBUG:
|
|
|
|
return HttpResponseBadRequest("Only allowed in debug mode.")
|
|
|
|
|
2020-07-05 12:43:37 +00:00
|
|
|
with transaction.atomic():
|
2020-07-13 11:30:18 +00:00
|
|
|
user_queryset = get_user_queryset(User.objects.all(), self)
|
2020-11-14 15:04:41 +00:00
|
|
|
user = get_object_or_404(user_queryset, username=request.data.get("user").get("username"))
|
2020-06-03 14:22:10 +00:00
|
|
|
|
2020-07-05 12:43:37 +00:00
|
|
|
# Only allow test users for extra safety
|
2020-11-14 15:04:41 +00:00
|
|
|
if not getattr(user, User.USERNAME_FIELD).startswith("test_user"):
|
2020-07-05 12:43:37 +00:00
|
|
|
return HttpResponseBadRequest("Endpoint not allowed for user.")
|
2020-04-14 13:21:51 +00:00
|
|
|
|
2020-11-14 15:04:41 +00:00
|
|
|
if hasattr(user, "userinfo"):
|
2020-07-05 12:43:37 +00:00
|
|
|
user.userinfo.delete()
|
2020-06-03 14:22:10 +00:00
|
|
|
|
2020-07-13 13:03:34 +00:00
|
|
|
serializer = AuthenticationSignupSerializer(data=request.data, context=self.get_serializer_context())
|
2020-07-05 12:43:37 +00:00
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
serializer.save()
|
2020-06-03 14:22:10 +00:00
|
|
|
|
2020-07-05 12:43:37 +00:00
|
|
|
# Delete all of the journal data for this user for a clear test env
|
|
|
|
user.collection_set.all().delete()
|
2020-10-25 10:14:55 +00:00
|
|
|
user.collectionmember_set.all().delete()
|
2020-07-05 12:43:37 +00:00
|
|
|
user.incoming_invitations.all().delete()
|
2020-04-14 13:21:51 +00:00
|
|
|
|
2020-07-05 12:43:37 +00:00
|
|
|
# FIXME: also delete chunk files!!!
|
2020-04-14 13:21:51 +00:00
|
|
|
|
|
|
|
return HttpResponse()
|