From dfbfa01bc5e7ecb45f771bfbc14cc90e8e85e8b6 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Tue, 10 Mar 2020 17:49:23 +0200 Subject: [PATCH] CollectionItem: move version and encryption key to the item itself. --- .../migrations/0022_auto_20200310_1547.py | 33 +++++++++++++++++++ django_etesync/models.py | 4 +-- django_etesync/serializers.py | 9 ++--- 3 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 django_etesync/migrations/0022_auto_20200310_1547.py diff --git a/django_etesync/migrations/0022_auto_20200310_1547.py b/django_etesync/migrations/0022_auto_20200310_1547.py new file mode 100644 index 0000000..cbd0ee7 --- /dev/null +++ b/django_etesync/migrations/0022_auto_20200310_1547.py @@ -0,0 +1,33 @@ +# Generated by Django 3.0.3 on 2020-03-10 15:47 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('django_etesync', '0021_auto_20200310_1439'), + ] + + operations = [ + migrations.RemoveField( + model_name='collectionitemrevision', + name='encryptionKey', + ), + migrations.RemoveField( + model_name='collectionitemrevision', + name='version', + ), + migrations.AddField( + model_name='collectionitem', + name='encryptionKey', + field=models.BinaryField(default=b'aoesnutheounth', editable=True), + preserve_default=False, + ), + migrations.AddField( + model_name='collectionitem', + name='version', + field=models.PositiveSmallIntegerField(default=1), + preserve_default=False, + ), + ] diff --git a/django_etesync/models.py b/django_etesync/models.py index 7eebd1a..d996769 100644 --- a/django_etesync/models.py +++ b/django_etesync/models.py @@ -41,6 +41,8 @@ class CollectionItem(models.Model): uid = models.CharField(db_index=True, blank=False, null=False, max_length=44, validators=[UidValidator]) collection = models.ForeignKey(Collection, related_name='items', on_delete=models.CASCADE) + version = models.PositiveSmallIntegerField() + encryptionKey = models.BinaryField(editable=True, blank=False, null=False) class Meta: unique_together = ('uid', 'collection') @@ -76,8 +78,6 @@ class CollectionItemChunk(models.Model): class CollectionItemRevision(models.Model): - version = models.PositiveSmallIntegerField() - encryptionKey = models.BinaryField(editable=True, blank=False, null=False) item = models.ForeignKey(CollectionItem, related_name='revisions', on_delete=models.CASCADE) chunks = models.ManyToManyField(CollectionItemChunk, related_name='items') hmac = models.CharField(max_length=50, blank=False, null=False) diff --git a/django_etesync/serializers.py b/django_etesync/serializers.py index f3e0fbd..c24239b 100644 --- a/django_etesync/serializers.py +++ b/django_etesync/serializers.py @@ -54,7 +54,8 @@ class CollectionSerializer(serializers.ModelSerializer): return None def get_ctag(self, obj): - # FIXME: we need to have something that's more privacy friendly + # FIXME: we need to have something that's more privacy friendly. Can probably just generate a uid per revision + # on revision creation (on the server) and just use that. last_revision = models.CollectionItemRevision.objects.filter(item__collection=obj).last() if last_revision is None: # FIXME: what is the etag for None? Though if we use the revision for collection it should be shared anyway. @@ -86,7 +87,6 @@ class CollectionItemChunkSerializer(serializers.ModelSerializer): class CollectionItemRevisionBaseSerializer(serializers.ModelSerializer): - encryptionKey = BinaryBase64Field() chunks = serializers.SlugRelatedField( slug_field='uid', queryset=models.CollectionItemChunk.objects.all(), @@ -95,7 +95,7 @@ class CollectionItemRevisionBaseSerializer(serializers.ModelSerializer): class Meta: model = models.CollectionItemRevision - fields = ('version', 'encryptionKey', 'chunks', 'hmac', 'deleted') + fields = ('chunks', 'hmac', 'deleted') class CollectionItemRevisionSerializer(CollectionItemRevisionBaseSerializer): @@ -132,11 +132,12 @@ class CollectionItemRevisionInlineSerializer(CollectionItemRevisionBaseSerialize class CollectionItemSerializer(serializers.ModelSerializer): + encryptionKey = BinaryBase64Field() content = CollectionItemRevisionSerializer(many=False) class Meta: model = models.CollectionItem - fields = ('uid', 'content') + fields = ('uid', 'version', 'encryptionKey', 'content') def create(self, validated_data): """Function that's called when this serializer creates an item"""